@@ -2281,21 +2281,23 @@ def draw_text(self, gc, x, y, s, prop, angle, ismath=False, mtext=None):
22812281 # complication is avoided, but of course, those fonts can not be
22822282 # subsetted.)
22832283 else :
2284- singlebyte_chunks = [] # List of (start_x, list-of-1-byte-chars).
2285- multibyte_glyphs = [] # List of (start_x, glyph_index).
2286- prev_was_singlebyte = False
2287- for char , (glyph_idx , glyph_x ) in zip (
2288- s ,
2289- _text_layout .layout (s , font , kern_mode = KERNING_UNFITTED )):
2290- if ord (char ) <= 255 :
2291- if prev_was_singlebyte :
2292- singlebyte_chunks [- 1 ][1 ].append (char )
2293- else :
2294- singlebyte_chunks .append ((glyph_x , [char ]))
2295- prev_was_singlebyte = True
2284+ # List of (start_x, [prev_kern, char, char, ...]), w/o zero kerns.
2285+ singlebyte_chunks = []
2286+ # List of (start_x, glyph_index).
2287+ multibyte_glyphs = []
2288+ prev_was_multibyte = True
2289+ for item in _text_layout .layout (
2290+ s , font , kern_mode = KERNING_UNFITTED ):
2291+ if ord (item .char ) <= 255 :
2292+ if prev_was_multibyte :
2293+ singlebyte_chunks .append ((item .x , []))
2294+ if item .prev_kern :
2295+ singlebyte_chunks [- 1 ][1 ].append (item .prev_kern )
2296+ singlebyte_chunks [- 1 ][1 ].append (item .char )
2297+ prev_was_multibyte = False
22962298 else :
2297- multibyte_glyphs .append ((glyph_x , glyph_idx ))
2298- prev_was_singlebyte = False
2299+ multibyte_glyphs .append ((item . x , item . glyph_idx ))
2300+ prev_was_multibyte = True
22992301 # Do the rotation and global translation as a single matrix
23002302 # concatenation up front
23012303 self .file .output (Op .gsave )
@@ -2307,10 +2309,15 @@ def draw_text(self, gc, x, y, s, prop, angle, ismath=False, mtext=None):
23072309 self .file .output (Op .begin_text ,
23082310 self .file .fontName (prop ), fontsize , Op .selectfont )
23092311 prev_start_x = 0
2310- for start_x , chars in singlebyte_chunks :
2312+ for start_x , kerns_or_chars in singlebyte_chunks :
23112313 self ._setup_textpos (start_x , 0 , 0 , prev_start_x , 0 , 0 )
2312- self .file .output (self .encode_string ('' .join (chars ), fonttype ),
2313- Op .show )
2314+ self .file .output (
2315+ # See pdf spec "Text space details" for the 1000/fontsize
2316+ # (aka. 1000/T_fs) factor.
2317+ [- 1000 * next (group ) / fontsize if tp == float # a kern
2318+ else self .encode_string ("" .join (group ), fonttype )
2319+ for tp , group in itertools .groupby (kerns_or_chars , type )],
2320+ Op .showkern )
23142321 prev_start_x = start_x
23152322 self .file .output (Op .end_text )
23162323 # Then emit all the multibyte characters, one at a time.
0 commit comments