@@ -105,6 +105,44 @@ impl TtfFontStore {
105105 bold : Option < Vec < u8 > > ,
106106 oblique : Option < Vec < u8 > > ,
107107 label : & str ,
108+ ) -> Result < Self , FontError > {
109+ Self :: from_bytes_with_abbrevs ( regular, bold, oblique, Vec :: new ( ) , label)
110+ }
111+
112+ /// [`Self::from_bytes`], additionally registering `named` faces under the
113+ /// ABBREVS a document actually asks for by name.
114+ ///
115+ /// The three slots [`Self::from_bytes`] offers are *styles* of one face —
116+ /// regular, bold, oblique — and a document cannot reach any of them by
117+ /// name, because a store built that way has an EMPTY abbrev map and
118+ /// `resolve_font_abbrev` therefore answers `None` for everything. Every
119+ /// `set-font` then falls through to `rustyfi-lang`'s three-face spelling
120+ /// heuristic, which cannot fail and so cannot report that the abbrev was
121+ /// unknown: `set-font HanIdeographic (`ipaexm`, ..)` silently selects the
122+ /// Latin regular face and the document typesets in `.notdef`. That is
123+ /// exactly what a browser build hits, since it has no
124+ /// `fonts.satysfi-hash` to build a [`crate::fonts::FontRegistry`] from —
125+ /// so this is the byte-oriented counterpart of
126+ /// [`crate::fonts::FontRegistry::build_store`], and it allocates keys the
127+ /// same way that does: `FontKey(0/1/2)` stay regular/bold/oblique, and
128+ /// each named face gets its own slot after them.
129+ ///
130+ /// Duplicate BYTES share one slot's file, so registering one CJK face
131+ /// under both `ipaexm` and `ipaexg` (what a single-face browser build
132+ /// does — `stdja` asks for the first in body text and the second in
133+ /// headings) costs one copy in memory and one embedded font in the PDF,
134+ /// not two. `build_store` dedups by canonical path; there is no path
135+ /// here, so the comparison is on the bytes themselves — a memcmp per
136+ /// already-loaded file, over the handful of files a store holds.
137+ ///
138+ /// A repeated abbrev keeps the LAST face given for it, the same way a
139+ /// repeated key in a `fonts.satysfi-hash` object would.
140+ pub fn from_bytes_with_abbrevs (
141+ regular : Vec < u8 > ,
142+ bold : Option < Vec < u8 > > ,
143+ oblique : Option < Vec < u8 > > ,
144+ named : impl IntoIterator < Item = ( String , Vec < u8 > ) > ,
145+ label : & str ,
108146 ) -> Result < Self , FontError > {
109147 let validate = |bytes : & [ u8 ] | {
110148 // Fail at construction rather than at the first metrics call, the
@@ -128,15 +166,57 @@ impl TtfFontStore {
128166 }
129167 }
130168
169+ let mut abbrevs: BTreeMap < String , FontKey > = BTreeMap :: new ( ) ;
170+ for ( abbrev, bytes) in named {
171+ validate ( & bytes) ?;
172+ let file = match files. iter ( ) . position ( |f| * f == bytes) {
173+ Some ( index) => index,
174+ None => {
175+ files. push ( bytes) ;
176+ files. len ( ) - 1
177+ }
178+ } ;
179+ let key = FontKey ( slots. len ( ) as u16 ) ;
180+ slots. push ( file) ;
181+ abbrevs. insert ( abbrev, key) ;
182+ }
183+
131184 Ok ( TtfFontStore {
132185 files,
133186 slots,
134- abbrevs : BTreeMap :: new ( ) ,
187+ abbrevs,
135188 script_defaults : [ None ; 4 ] ,
136189 math_default : None ,
137190 } )
138191 }
139192
193+ /// Point `script`'s DEFAULT font at `font`, as
194+ /// `default-font.satysfi-hash`'s `scripts` block does for a store built
195+ /// from a font root.
196+ ///
197+ /// `get-initial-context` overlays these onto the context it hands
198+ /// `document`, so this is what makes a document typeset Japanese without
199+ /// naming a font at all — `stdja-mini` and every bare `@require:`-less
200+ /// document never call `set-font`, and would otherwise get the Latin
201+ /// regular face for every script no matter what faces the store holds.
202+ ///
203+ /// `ratio` scales the font size for this script (0.88 is what the bundled
204+ /// configuration uses for CJK, which is upstream's own figure) and
205+ /// `rising` shifts the baseline. Takes a [`FontKey`] rather than an abbrev
206+ /// so that a name this store does not carry cannot be accepted and then
207+ /// silently do nothing — get one from [`Self::abbrev_key`].
208+ #[ must_use]
209+ pub fn with_script_default (
210+ mut self ,
211+ script : Script ,
212+ font : FontKey ,
213+ ratio : f64 ,
214+ rising : f64 ,
215+ ) -> Self {
216+ self . script_defaults [ script as usize ] = Some ( ( font, ratio, rising) ) ;
217+ self
218+ }
219+
140220 /// Builder used only by [`crate::fonts::FontRegistry::build_store`]:
141221 /// construct a store with the three default slots already
142222 /// loaded (via [`Self::load`]) plus every other configured abbrev's
@@ -709,6 +789,89 @@ mod tests {
709789 assert ! ( matches!( err, FontError :: Parse { .. } ) , "{err}" ) ;
710790 }
711791
792+ /// The gap `from_bytes_with_abbrevs` closes: a store built from bytes
793+ /// alone answers `None` to EVERY abbrev, which is what sends a browser
794+ /// build's `set-font HanIdeographic (`ipaexm`, ..)` through
795+ /// `rustyfi-lang`'s three-face spelling heuristic and onto the Latin face.
796+ #[ test]
797+ fn from_bytes_alone_resolves_no_abbrev_at_all ( ) {
798+ let Some ( regular) = system_font ( ) else {
799+ return ;
800+ } ;
801+ let store = TtfFontStore :: from_bytes ( regular, None , None , "regular.ttf" )
802+ . expect ( "a system font should parse" ) ;
803+ assert_eq ! ( store. resolve_font_abbrev( "ipaexm" ) , None ) ;
804+ assert_eq ! ( store. num_slots( ) , 3 ) ;
805+ }
806+
807+ /// Named faces get their own keys after the three style slots, and two
808+ /// abbrevs naming the SAME bytes share one file — the case a single-face
809+ /// browser build is entirely made of, since `stdja` asks for `ipaexm` in
810+ /// body text and `ipaexg` in headings and only one CJK face is fetched.
811+ #[ test]
812+ fn named_abbrevs_get_keys_past_the_style_slots_and_share_equal_bytes ( ) {
813+ let Some ( regular) = system_font ( ) else {
814+ return ;
815+ } ;
816+ let cjk = regular. clone ( ) ;
817+ let store = TtfFontStore :: from_bytes_with_abbrevs (
818+ regular,
819+ None ,
820+ None ,
821+ [
822+ ( "ipaexm" . to_string ( ) , cjk. clone ( ) ) ,
823+ ( "ipaexg" . to_string ( ) , cjk) ,
824+ ] ,
825+ "regular.ttf" ,
826+ )
827+ . expect ( "a system font should parse" ) ;
828+
829+ let mincho = store. resolve_font_abbrev ( "ipaexm" ) . expect ( "ipaexm" ) ;
830+ let gothic = store. resolve_font_abbrev ( "ipaexg" ) . expect ( "ipaexg" ) ;
831+ assert_eq ! ( mincho, FontKey ( 3 ) ) ;
832+ assert_eq ! ( gothic, FontKey ( 4 ) ) ;
833+ // Distinct keys, one physical file — so the PDF embeds it once. Here
834+ // that file is also the regular face's, which is the point of deduping
835+ // on bytes rather than on identity.
836+ assert_eq ! ( store. file_index( mincho) , store. file_index( gothic) ) ;
837+ assert_eq ! ( store. num_files( ) , 1 ) ;
838+ // Unregistered names still decline, so the heuristic still runs for
839+ // them rather than this quietly becoming a catch-all.
840+ assert_eq ! ( store. resolve_font_abbrev( "lmsans" ) , None ) ;
841+ }
842+
843+ /// `get-initial-context` reads these, so they are what lets a document
844+ /// that never calls `set-font` — `stdja-mini`, or no class at all —
845+ /// typeset Japanese.
846+ #[ test]
847+ fn script_defaults_can_be_set_on_a_byte_built_store ( ) {
848+ let Some ( regular) = system_font ( ) else {
849+ return ;
850+ } ;
851+ let cjk = regular. clone ( ) ;
852+ let store = TtfFontStore :: from_bytes_with_abbrevs (
853+ regular,
854+ None ,
855+ None ,
856+ [ ( "ipaexg" . to_string ( ) , cjk) ] ,
857+ "regular.ttf" ,
858+ )
859+ . expect ( "a system font should parse" ) ;
860+ let key = store. resolve_font_abbrev ( "ipaexg" ) . expect ( "ipaexg" ) ;
861+ let store = store
862+ . with_script_default ( Script :: HanIdeographic , key, 0.88 , 0.0 )
863+ . with_script_default ( Script :: Kana , key, 0.88 , 0.0 ) ;
864+
865+ assert_eq ! (
866+ store. default_script_font( Script :: HanIdeographic ) ,
867+ Some ( ( key, 0.88 , 0.0 ) )
868+ ) ;
869+ assert_eq ! ( store. default_script_font( Script :: Kana ) , Some ( ( key, 0.88 , 0.0 ) ) ) ;
870+ // Untouched scripts stay unconfigured, so `get-initial-context`'s
871+ // overlay leaves `ctx.font` alone for Latin.
872+ assert_eq ! ( store. default_script_font( Script :: Latin ) , None ) ;
873+ }
874+
712875 /// A real font from the system, when one is installed. Returns `None`
713876 /// rather than failing: which faces exist varies by machine, and a font
714877 /// test must not be the reason an unrelated change looks broken.
0 commit comments