Skip to content

azw3: resolve kindle:embed refs inside served CSS flows - #25

Open
Imaclean74 wants to merge 2 commits into
zacharydenton:masterfrom
Imaclean74:feat/azw3-css-flow-extraction
Open

Imaclean74 wants to merge 2 commits into
zacharydenton:masterfrom
Imaclean74:feat/azw3-css-flow-extraction

Conversation

@Imaclean74

@Imaclean74 Imaclean74 commented Jul 20, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #26.

Problem

Reworked for 0.5.0: the original version of this PR added CSS-flow discovery and serving, which b21f08e has since implemented upstream — thanks! This PR is now just the remaining half.

KF8 stylesheet flows are served from the FDST flow table as of 0.5.0, but they are served raw. The CSS content itself can reference embedded resources — the KF8 writer (both Amazon's tooling and boko's own rewrite_css_references_fast) stores CSS-referenced resources as url(kindle:embed:XXXX[?mime=...]), the same base32 numbering used for kindle:embed in chapter HTML (see the MobileRead KF8 documentation). Chapter HTML gets those references rewritten by transform_kindle_refs, but CSS does not: @font-face src and background-image URLs come back as kindle: URLs no EPUB consumer can resolve — embedded fonts and CSS-referenced images silently break, and the output fails EPUB 3.3 resource checks.

Fix

rewrite_css_embed_refs in src/import/azw3.rs, applied in load_asset's styles branch: each embed index maps to the discovered asset path — images/image_NNNN.ext vs fonts/font_NNNN.ext picks the right prefix and extension straight from discovery — with a best-guess image path as fallback for unresolvable indices (matching transform_kindle_refs' behaviour in HTML).

Tests

  • 5 unit tests: font ref with ?mime= suffix, bare image ref, quoted ref, unresolvable-index fallback, and no-op on embed-free CSS
  • azw3_roundtrip_resolves_embed_refs_inside_css: a synthetic EPUB (via the existing common::EpubBuilder) whose CSS references an image goes EPUB→AZW3→import; the reference must come back pointing at a discovered, loadable asset. Fails on current master with url(kindle:embed:0001) served verbatim.

cargo fmt --check, cargo clippy --lib, cargo test all pass.

@Imaclean74
Imaclean74 marked this pull request as draft July 20, 2026 14:55
Stylesheet flows are served from the FDST flow table, but the CSS content
itself can reference embedded resources — @font-face src and background
images are written as url(kindle:embed:XXXX[?mime=...]) by the KF8 writer.
Served raw, those references dangle: no EPUB consumer can resolve a
kindle: URL, so embedded fonts and CSS-referenced images silently break.

rewrite_css_embed_refs maps each embed index to the discovered asset path
(images/image_NNNN.ext vs fonts/font_NNNN.ext picks the right extension
from discovery), falling back to a best-guess image path for indices that
resolve to nothing.

Round-trip test: an EPUB whose CSS references an image survives
EPUB→AZW3→import with the reference pointing at a discovered, loadable
asset; unit tests cover font/image/mime-suffix/quoted/fallback cases.
@Imaclean74
Imaclean74 force-pushed the feat/azw3-css-flow-extraction branch from 9cbf0cc to 078d1ef Compare July 25, 2026 12:01
@Imaclean74 Imaclean74 changed the title azw3: serve CSS flows as styles/styleNNNN.css assets azw3: resolve kindle:embed refs inside served CSS flows Jul 25, 2026
@Imaclean74
Imaclean74 marked this pull request as ready for review July 27, 2026 11:28
CSS urls resolve against the stylesheet's own location, and KF8 CSS is
served at styles/styleNNNN.css, so the root-relative images/... and
fonts/... paths produced by rewrite_css_embed_refs resolved into styles/
and broke every CSS-referenced image and font (font_faces() resolves src
the same way). Paths are now written relative to the stylesheet, e.g.
../images/image_0000.png.

The round-trip test now resolves the url relative to the CSS asset, as a
reading system does, instead of only checking that an asset name appears
in the CSS; it fails against the previous behaviour.
@Imaclean74

Copy link
Copy Markdown
Contributor Author

Pushed a follow-up commit (e122548) fixing a bug in this PR.

rewrite_css_embed_refs wrote root-relative paths (images/image_0000.png), but CSS url()s resolve against the stylesheet, which is served at styles/styleNNNN.css. So every CSS-referenced image or font resolved into styles/ and broke. Paths are now written relative to the stylesheet (../images/image_0000.png). The round-trip test now resolves the url the way a reading system does, and it fails against the previous commit.

Note on CI: the Clippy failure isn't from this PR. The runner moved to Rust 1.99, whose needless_borrows_for_generic_args lint now flags src/dom/optimize/vacuum.rs:61 on master too. Happy to send a one-line fix as a separate PR if that's useful.

@Imaclean74

Copy link
Copy Markdown
Contributor Author

Opened #29 with the one-line Rust 1.99 Clippy fix. Once that lands, the red Clippy check here (and on other open PRs) should clear on a re-run.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

AZW3 import: kindle:embed refs inside stylesheet flows are served unresolved

1 participant