@@ -14,6 +14,8 @@ pub(crate) struct FallbackFontProgram {
1414}
1515
1616impl FallbackFontProgram {
17+ const NOTO_SANS_CJK_JP_REGULAR : & [ u8 ] = include_bytes ! ( "../assets/NotoSansCJKjp-Regular.otf" ) ;
18+
1719 /// Select fallback font bytes and metadata for a font dictionary.
1820 pub ( crate ) fn from_dictionary (
1921 dictionary : & Dictionary ,
@@ -22,8 +24,17 @@ impl FallbackFontProgram {
2224 let flags = FontFlags :: from_dictionary ( dictionary, objects) ?;
2325 let standard14 = Standard14Font :: from_dictionary ( dictionary, objects, flags) ;
2426 let is_cjk = is_cjk_cid_font ( dictionary, objects) ?;
27+ let font_file = if is_cjk {
28+ Self :: NOTO_SANS_CJK_JP_REGULAR
29+ } else {
30+ standard14. fallback_font_bytes ( )
31+ } ;
2532
26- Ok ( fallback_program ( flags, standard14, is_cjk) )
33+ Ok ( Self {
34+ font_file,
35+ standard14,
36+ flags,
37+ } )
2738 }
2839}
2940
@@ -47,7 +58,7 @@ pub(crate) fn fallback_true_type_from_dictionary(
4758 let encoding = Encoding :: from_dictionary ( dictionary, objects)
4859 . ok ( )
4960 . flatten ( ) ;
50- let to_unicode = to_unicode_cmap ( dictionary, objects) ?;
61+ let to_unicode = ToUnicodeCMap :: from_dictionary ( dictionary, objects) ?;
5162
5263 Ok ( TrueTypeFont {
5364 font_file : fallback. font_file . into ( ) ,
@@ -71,56 +82,20 @@ pub(crate) fn fallback_true_type_from_dictionary_best_effort(
7182 let flags = FontFlags :: from_dictionary ( dictionary, objects) . unwrap_or_default ( ) ;
7283 let is_cjk = is_cjk_cid_font ( dictionary, objects) . unwrap_or ( false ) ;
7384 let standard14 = Standard14Font :: from_dictionary ( dictionary, objects, flags) ;
74- let fallback = fallback_program ( flags, standard14, is_cjk) ;
75- let mut font = TrueTypeFont :: from_bytes ( fallback. font_file , Some ( fallback. standard14 ) ) ;
76- font. flags = fallback. flags ;
77-
78- font
79- }
80-
81- /// Build the fallback font program descriptor from already-decided inputs.
82- ///
83- /// `standard14` selects the Standard 14 identity for simple-font fallback,
84- /// while `is_cjk` switches the program bytes to the bundled CJK fallback for
85- /// CID fonts that declare a supported CJK ordering.
86- fn fallback_program (
87- flags : FontFlags ,
88- standard14 : Standard14Font ,
89- is_cjk : bool ,
90- ) -> FallbackFontProgram {
9185 let font_file = if is_cjk {
92- include_bytes ! ( "../assets/NotoSansCJKjp-Regular.otf" ) . as_slice ( )
86+ FallbackFontProgram :: NOTO_SANS_CJK_JP_REGULAR
9387 } else {
9488 standard14. fallback_font_bytes ( )
9589 } ;
96-
97- FallbackFontProgram {
90+ let fallback = FallbackFontProgram {
9891 font_file,
9992 standard14,
10093 flags,
101- }
102- }
94+ } ;
95+ let mut font = TrueTypeFont :: from_bytes ( fallback. font_file , Some ( fallback. standard14 ) ) ;
96+ font. flags = fallback. flags ;
10397
104- /// Parse an optional ToUnicode CMap from a font dictionary.
105- ///
106- /// # Paramaters
107- ///
108- /// - `dictionary`: The PDF font dictionary that may contain `/ToUnicode`.
109- /// - `objects`: The resolver used to dereference indirect PDF objects.
110- ///
111- /// # Returns
112- ///
113- /// The parsed ToUnicode CMap when a readable `/ToUnicode` stream is present.
114- fn to_unicode_cmap (
115- dictionary : & Dictionary ,
116- objects : & dyn ObjectResolver ,
117- ) -> Result < Option < ToUnicodeCMap > , FontError > {
118- dictionary
119- . get ( "ToUnicode" )
120- . and_then ( |e| e. try_stream ( objects) . ok ( ) )
121- . map ( |s| ToUnicodeCMap :: try_from ( s. raw_data ( ) ) )
122- . transpose ( )
123- . map_err ( FontError :: from)
98+ font
12499}
125100
126101/// Detect whether a CID font dictionary uses a known CJK CID ordering.
0 commit comments