| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 70dc388 commit df670cf
2 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -107,11 +107,11 @@ class GlyphMap: | |||
| 107 | 107 | """ | |
| 108 | 108 | A two-way glyph mapping. | |
| 109 | 109 | ||
| 110 | - The forward glyph map is from (character code, glyph index)-pairs to (subset index, | ||
| 111 | - subset character code)-pairs. | ||
| 110 | + The forward glyph map is from (character string, glyph index)-pairs to | ||
| 111 | + (subset index, subset character code)-pairs. | ||
| 112 | 112 | ||
| 113 | 113 | The inverse glyph map is from to (subset index, subset character code)-pairs to | |
| 114 | - (character code, glyph index)-pairs. | ||
| 114 | + (character string, glyph index)-pairs. | ||
| 115 | 115 | """ | |
| 116 | 116 | ||
| 117 | 117 | def __init__(self) -> None: | |
@@ -120,22 +120,21 @@ def __init__(self) -> None: | |||
| 120 | 120 | self._inverse: dict[tuple[int, CharacterCodeType], | |
| 121 | 121 | tuple[CharacterCodeType, GlyphIndexType]] = {} | |
| 122 | 122 | ||
| 123 | - def get(self, charcode: CharacterCodeType, | ||
| 123 | + def get(self, charcodes: str, | ||
| 124 | 124 | glyph_index: GlyphIndexType) -> tuple[int, CharacterCodeType] | None: | |
| 125 | 125 | """ | |
| 126 | - Get the forward mapping from a (character code, glyph index)-pair. | ||
| 126 | + Get the forward mapping from a (character string, glyph index)-pair. | ||
| 127 | 127 | ||
| 128 | 128 | This may return *None* if the pair is not currently mapped. | |
| 129 | 129 | """ | |
| 130 | - return self._forward.get((charcode, glyph_index)) | ||
| 130 | + return self._forward.get((charcodes, glyph_index)) | ||
| 131 | 131 | ||
| 132 | 132 | def iget(self, subset: int, | |
| 133 | - subset_charcode: CharacterCodeType) -> tuple[CharacterCodeType, | ||
| 134 | - GlyphIndexType]: | ||
| 133 | + subset_charcode: CharacterCodeType) -> tuple[str, GlyphIndexType]: | ||
| 135 | 134 | """Get the inverse mapping from a (subset, subset charcode)-pair.""" | |
| 136 | 135 | return self._inverse[(subset, subset_charcode)] | |
| 137 | 136 | ||
| 138 | - def add(self, charcode: CharacterCodeType, glyph_index: GlyphIndexType, subset: int, | ||
| 137 | + def add(self, charcode: str, glyph_index: GlyphIndexType, subset: int, | ||
| 139 | 138 | subset_charcode: CharacterCodeType) -> None: | |
| 140 | 139 | """ | |
| 141 | 140 | Add a mapping to this instance. | |
@@ -219,18 +218,19 @@ def track(self, font: FT2Font, s: str) -> list[tuple[int, CharacterCodeType]]: | |||
| 219 | 218 | for c, f in font._get_fontmap(s).items() | |
| 220 | 219 | ] | |
| 221 | 220 | ||
| 222 | - def track_glyph( | ||
| 223 | - self, font: FT2Font, charcode: CharacterCodeType, | ||
| 224 | - glyph: GlyphIndexType) -> tuple[int, CharacterCodeType]: | ||
| 221 | + def track_glyph(self, font: FT2Font, chars: str | CharacterCodeType, | ||
| 222 | + glyph: GlyphIndexType) -> tuple[int, CharacterCodeType]: | ||
| 225 | 223 | """ | |
| 226 | 224 | Record character code *charcode* at glyph index *glyph* as using font *font*. | |
| 227 | 225 | ||
| 228 | 226 | Parameters | |
| 229 | 227 | ---------- | |
| 230 | 228 | font : FT2Font | |
| 231 | 229 | A font that is being used for the provided string. | |
| 232 | - charcode : CharacterCodeType | ||
| 233 | - The character code to record. | ||
| 230 | + chars : str or CharacterCodeType | ||
| 231 | + The character(s) to record. This may be a single character code, or multiple | ||
| 232 | + characters in a string, if the glyph maps to several characters. It will be | ||
| 233 | + normalized to a string internally. | ||
| 234 | 234 | glyph : GlyphIndexType | |
| 235 | 235 | The corresponding glyph index to record. | |
| 236 | 236 | ||
@@ -243,13 +243,21 @@ def track_glyph( | |||
| 243 | 243 | The character code within the above subset. If *subset_size* was not | |
| 244 | 244 | specified on this instance, then this is just *charcode* unmodified. | |
| 245 | 245 | """ | |
| 246 | + if isinstance(chars, str): | ||
| 247 | + charcode = ord(chars[0]) | ||
| 248 | + else: | ||
| 249 | + charcode = chars | ||
| 250 | + chars = chr(chars) | ||
| 251 | + | ||
| 246 | 252 | glyph_map = self.glyph_maps.setdefault(font.fname, GlyphMap()) | |
| 247 | - if result := glyph_map.get(charcode, glyph): | ||
| 253 | + if result := glyph_map.get(chars, glyph): | ||
| 248 | 254 | return result | |
| 249 | 255 | ||
| 250 | 256 | subset_maps = self.used.setdefault(font.fname, [{}]) | |
| 251 | - # Default to preserving the character code as it was. | ||
| 252 | 257 | use_next_charmap = ( | |
| 258 | + # Multi-character glyphs always go in the non-0 subset. | ||
| 259 | + len(chars) > 1 or | ||
| 260 | + # Default to preserving the character code as it was. | ||
| 253 | 261 | self.subset_size != 0 | |
| 254 | 262 | and ( | |
| 255 | 263 | # But start filling a new subset if outside the first block; this | |
@@ -270,11 +278,11 @@ def track_glyph( | |||
| 270 | 278 | subset = 0 | |
| 271 | 279 | subset_charcode = charcode | |
| 272 | 280 | subset_maps[subset][subset_charcode] = glyph | |
| 273 | - glyph_map.add(charcode, glyph, subset, subset_charcode) | ||
| 281 | + glyph_map.add(chars, glyph, subset, subset_charcode) | ||
| 274 | 282 | return (subset, subset_charcode) | |
| 275 | 283 | ||
| 276 | 284 | def subset_to_unicode(self, fontname: str, subset: int, | |
| 277 | - subset_charcode: CharacterCodeType) -> CharacterCodeType: | ||
| 285 | + subset_charcode: CharacterCodeType) -> str: | ||
| 278 | 286 | """ | |
| 279 | 287 | Map a subset index and character code to a Unicode character code. | |
| 280 | 288 | ||
@@ -289,8 +297,8 @@ def subset_to_unicode(self, fontname: str, subset: int, | |||
| 289 | 297 | ||
| 290 | 298 | Returns | |
| 291 | 299 | ------- | |
| 292 | - CharacterCodeType | ||
| 293 | - The Unicode character code corresponding to the subsetted one. | ||
| 300 | + str | ||
| 301 | + The Unicode character(s) corresponding to the subsetted character code. | ||
| 294 | 302 | """ | |
| 295 | 303 | return self.glyph_maps[fontname].iget(subset, subset_charcode)[0] | |
| 296 | 304 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -993,7 +993,8 @@ def _embedTeXFont(self, dvifont): | |||
| 993 | 993 | # for that subset, and compute various properties based on the encoding. | |
| 994 | 994 | charmap = self._character_tracker.used[dvifont.fname][0] | |
| 995 | 995 | chars = { | |
| 996 | - self._character_tracker.subset_to_unicode(dvifont.fname, 0, ccode) | ||
| 996 | + # DVI type 1 fonts always map single glyph to single character. | ||
| 997 | + ord(self._character_tracker.subset_to_unicode(dvifont.fname, 0, ccode)) | ||
| 997 | 998 | for ccode in charmap | |
| 998 | 999 | } | |
| 999 | 1000 | t1font = t1font.subset(chars, self._get_subset_prefix(charmap.values())) | |
@@ -1147,10 +1148,10 @@ def generate_unicode_cmap(subset_index, charmap): | |||
| 1147 | 1148 | last_ccode = ccode | |
| 1148 | 1149 | ||
| 1149 | 1150 | def _to_unicode(ccode): | |
| 1150 | - real_ccode = self._character_tracker.subset_to_unicode( | ||
| 1151 | + chars = self._character_tracker.subset_to_unicode( | ||
| 1151 | 1152 | filename, subset_index, ccode) | |
| 1152 | - unicodestr = chr(real_ccode).encode('utf-16be').hex() | ||
| 1153 | - return f'<{unicodestr}>' | ||
| 1153 | + hexstr = chars.encode('utf-16be').hex() | ||
| 1154 | + return f'<{hexstr}>' | ||
| 1154 | 1155 | ||
| 1155 | 1156 | width = 2 if fonttype == 3 else 4 | |
| 1156 | 1157 | unicode_bfrange = [] | |
@@ -2329,7 +2330,7 @@ def output_singlebyte_chunk(kerns_or_chars): | |||
| 2329 | 2330 | for item in _text_helpers.layout(s, font, kern_mode=Kerning.UNFITTED, | |
| 2330 | 2331 | language=language): | |
| 2331 | 2332 | subset, charcode = self.file._character_tracker.track_glyph( | |
| 2332 | - item.ft_object, ord(item.char), item.glyph_index) | ||
| 2333 | + item.ft_object, item.char, item.glyph_index) | ||
| 2333 | 2334 | if (item.ft_object, subset) != prev_font: | |
| 2334 | 2335 | if singlebyte_chunk: | |
| 2335 | 2336 | output_singlebyte_chunk(singlebyte_chunk) | |
| Back | FazBrowse Home | New Git URL |
0 commit comments