@@ -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
0 commit comments