@@ -2375,47 +2375,48 @@ def draw_text(self, gc, x, y, s, prop, angle, ismath=False, mtext=None): |
2375 | 2375 | # the regular text show command (TJ) with appropriate kerning between |
2376 | 2376 | # chunks, whereas multibyte characters use the XObject command (Do). |
2377 | 2377 | else: |
2378 | | - # List of (ft_object, start_x, [prev_kern, char, char, ...]), |
2379 | | - # w/o zero kerns. |
2380 | | - singlebyte_chunks = [] |
2381 | | - # List of (ft_object, start_x, glyph_index). |
2382 | | - multibyte_glyphs = [] |
2383 | | - prev_was_multibyte = True |
2384 | | - prev_font = font |
2385 | | - for item in _text_helpers.layout(s, font, kern_mode=Kerning.UNFITTED): |
2386 | | - if _font_supports_glyph(fonttype, ord(item.char)): |
2387 | | - if prev_was_multibyte or item.ft_object != prev_font: |
2388 | | - singlebyte_chunks.append((item.ft_object, item.x, [])) |
2389 | | - prev_font = item.ft_object |
2390 | | - if item.prev_kern: |
2391 | | - singlebyte_chunks[-1][2].append(item.prev_kern) |
2392 | | - singlebyte_chunks[-1][2].append(item.char) |
2393 | | - prev_was_multibyte = False |
2394 | | - else: |
2395 | | - multibyte_glyphs.append((item.ft_object, item.x, item.glyph_index)) |
2396 | | - prev_was_multibyte = True |
| 2378 | + def output_singlebyte_chunk(kerns_or_chars): |
| 2379 | + self.file.output( |
| 2380 | + # See pdf spec "Text space details" for the 1000/fontsize |
| 2381 | + # (aka. 1000/T_fs) factor. |
| 2382 | + [(-1000 * next(group) / fontsize) if tp == float # a kern |
| 2383 | + else self.encode_string("".join(group), fonttype) |
| 2384 | + for tp, group in itertools.groupby(kerns_or_chars, type)], |
| 2385 | + Op.showkern) |
2397 | 2386 | # Do the rotation and global translation as a single matrix |
2398 | 2387 | # concatenation up front |
2399 | 2388 | self.file.output(Op.gsave) |
2400 | 2389 | a = math.radians(angle) |
2401 | 2390 | self.file.output(math.cos(a), math.sin(a), |
2402 | 2391 | -math.sin(a), math.cos(a), |
2403 | 2392 | x, y, Op.concat_matrix) |
| 2393 | + # List of [prev_kern, char, char, ...] w/o zero kerns. |
| 2394 | + singlebyte_chunk = [] |
| 2395 | + # List of (ft_object, start_x, glyph_index). |
| 2396 | + multibyte_glyphs = [] |
| 2397 | + prev_font = None |
| 2398 | + prev_start_x = 0 |
2404 | 2399 | # Emit all the 1-byte characters in a BT/ET group. |
2405 | 2400 | self.file.output(Op.begin_text) |
2406 | | - prev_start_x = 0 |
2407 | | - for ft_object, start_x, kerns_or_chars in singlebyte_chunks: |
2408 | | - ft_name = self.file.fontName(ft_object.fname) |
2409 | | - self.file.output(ft_name, fontsize, Op.selectfont) |
2410 | | - self._setup_textpos(start_x, 0, 0, prev_start_x, 0, 0) |
2411 | | - self.file.output( |
2412 | | - # See pdf spec "Text space details" for the 1000/fontsize |
2413 | | - # (aka. 1000/T_fs) factor. |
2414 | | - [-1000 * next(group) / fontsize if tp == float # a kern |
2415 | | - else self.encode_string("".join(group), fonttype) |
2416 | | - for tp, group in itertools.groupby(kerns_or_chars, type)], |
2417 | | - Op.showkern) |
2418 | | - prev_start_x = start_x |
| 2401 | + for item in _text_helpers.layout(s, font, kern_mode=Kerning.UNFITTED): |
| 2402 | + if _font_supports_glyph(fonttype, ord(item.char)): |
| 2403 | + if item.ft_object != prev_font: |
| 2404 | + if singlebyte_chunk: |
| 2405 | + output_singlebyte_chunk(singlebyte_chunk) |
| 2406 | + ft_name = self.file.fontName(item.ft_object.fname) |
| 2407 | + self.file.output(ft_name, fontsize, Op.selectfont) |
| 2408 | + self._setup_textpos(item.x, 0, 0, prev_start_x, 0, 0) |
| 2409 | + singlebyte_chunk = [] |
| 2410 | + prev_font = item.ft_object |
| 2411 | + prev_start_x = item.x |
| 2412 | + if item.prev_kern: |
| 2413 | + singlebyte_chunk.append(item.prev_kern) |
| 2414 | + singlebyte_chunk.append(item.char) |
| 2415 | + else: |
| 2416 | + prev_font = None |
| 2417 | + multibyte_glyphs.append((item.ft_object, item.x, item.glyph_index)) |
| 2418 | + if singlebyte_chunk: |
| 2419 | + output_singlebyte_chunk(singlebyte_chunk) |
2419 | 2420 | self.file.output(Op.end_text) |
2420 | 2421 | # Then emit all the multibyte characters, one at a time. |
2421 | 2422 | for ft_object, start_x, glyph_index in multibyte_glyphs: |
|