Skip to main content

Crate mos_fonts

Crate mos_fonts 

Source
Expand description

Font discovery, shaping, and metrics (manifest §22.1).

Two font-emission paths live behind one Font enum:

  • Font::Base14; the 14 standard PDF base fonts. No glyph data ships; the PDF reader supplies outlines. Advance widths come from bundled Adobe AFMs, addressed through [pdf_base14_metrics]. WinAnsi natives go out as their canonical byte; the small set of extended Latin glyphs each face carries (Latin Extended-A beyond WinAnsi, the math operators, fi/fl ligatures) goes out through a per-document /Differences remap that mos-pdf plans. Characters outside both tiers: Cyrillic, CJK, emoji: silently substitute to ? in both the width and emit paths (no warning, no panic; callers that want non-Latin should pick the embedded family).
  • Font::Embedded: a bundled Noto Sans cut shaped with rustybuzz (HarfBuzz Rust port). The PDF backend embeds a subset of the actual TrueType outlines as a Type 0 CID font with a /ToUnicode CMap, so the output is a real Unicode-aware document: copy/paste round-trips through Cyrillic, Greek, accented Latin, and anything else Noto Sans covers.

Six cuts ship in this crate’s data/ directory: four Noto Sans style cuts (Regular, Bold, Italic, BoldItalic) for proportional body text, one Noto Sans Mono Regular cut for `raw` runs, and one Noto Sans Math cut for per-glyph fallback (see SOURCES.md under the crate root). Style selection happens through FontFamily, which the layout engine receives from the eval lowerer.

Modules§

embedded 🔒
Embedded TrueType faces + shaping.
family 🔒
font 🔒
metrics 🔒
normalize 🔒
resources 🔒
shape 🔒

Structs§

EmbeddedFont
A bundled TrueType face: the raw bytes plus the metrics and parsed rustybuzz::Face needed to shape text and emit a PDF FontDescriptor.
FontFamily
A four-cut family: Regular, Bold, Italic, BoldItalic. The layout engine picks one slot per styled run (*emphasis* → italic, **strong** → bold, raw → fixed-width family, body → regular).
ShapedGlyph
One glyph in a shaped run. Cluster values are byte offsets into the source UTF-8 string.
ShapedRun
Output of shape_text: the shaped glyph stream and the total advance width at the requested point size.
WordSubRun
One face’s slice of a per-glyph-fallback shaping result. A word shaped through shape_with_fallback produces a Vec<WordSubRun>; each sub-run is self-contained: its text is the source UTF-8 slice covered by exactly this sub-run, its glyphscluster offsets are rebased to the sub-run’s local text (so plan_embedded can build /ToUnicode without knowing about the parent word), and its advance_pt is the sum of per-glyph advances at the requested point size.

Enums§

Base14Font
One of the 14 standard PDF fonts every conformant PDF reader ships built in (PDF 1.7 §9.6.2.2).
EmbeddedFontId
Stable identifier for each bundled embedded cut. Used as the enum payload of Font::Embedded so Font stays Copy/Hash/Eq without resorting to pointer identity.
Font
A renderable font – either one of the Adobe Core 14 (no data embedded; outlines from the PDF reader) or a bundled TrueType cut (data embedded, subset per-document).

Functions§

advance_units_to_pt
Convert a font-unit advance to PDF user-space points at size_pt, given the face’s units-per-em. Values are carried as i32 because shapers use signed advances, but current embedded output normalizes to PDF-emittable hmtx advances in 0..=65535. Preserve sign here anyway so future positioned shaping cannot turn a negative adjustment into a huge positive width.
ascent
Ascender height for font at size points.
descent
Descender depth for font at size points, as a positive number (the AFM/TTF storage convention is negative; both backends normalise on the way out).
extended_glyph_name
Returns the PostScript glyph name for ch if and only if ch is in the extended tier: i.e. a Core 14 AFM glyph that has no WinAnsi byte and therefore must be reached through a custom /Encoding /Differences slot. The extended tier covers:
glyph_width
Width of a single glyph in font at size points. For Base14 faces this is one AFM lookup; for embedded faces it shapes the single character. Used by the paragraph engine for character-wise hyphenation of oversized words.
nfc_text
Return text in Unicode NFC form, borrowing when it is already normalized.
shape
Shape text against font using rustybuzz. Returns the glyph stream in visual order (LTR for this slice). Glyph IDs, advances, and offsets come from rustybuzz, so substitutions, kerning, and combining-mark positioning are preserved. An empty text returns an empty Vec without invoking the shaper.
shape_text
Shape text against font and return both the glyph stream and the advance widths in user-space points. Callers that need only the width can use text_width; callers that will also emit glyphs downstream should use this to avoid shaping twice.
shape_with_fallback
Shape text against primary with per-glyph fallback. Walks each HarfBuzz cluster in the primary’s shaped output; clusters that contain any .notdef (GID 0) glyph are re-shaped against each embedded face in fallbacks in order. The first fallback to produce a glyph stream with no .notdef wins the whole cluster (cluster- granular replacement, never partial: partial replacement would duplicate bases, drop marks, break ligatures).
subset
Subset font to just the glyph IDs in gids (always include GID 0, .notdef, which the PDF spec mandates). Returns the trimmed TTF bytes suitable for embedding as a /FontFile2 stream.
text_width
Advance width of text rendered in font at size points.
winansi_byte
Returns the PDF WinAnsiEncoding byte that encodes ch, or None if ch has no slot in WinAnsi.