(self, gc, x, y, s, prop, angle, ismath=False, mtext=None)
| 761 | |
| 762 | @_log_if_debug_on |
| 763 | def draw_text(self, gc, x, y, s, prop, angle, ismath=False, mtext=None): |
| 764 | # docstring inherited |
| 765 | |
| 766 | if self._is_transparent(gc.get_rgb()): |
| 767 | return # Special handling for fully transparent. |
| 768 | |
| 769 | if ismath == 'TeX': |
| 770 | return self.draw_tex(gc, x, y, s, prop, angle) |
| 771 | |
| 772 | if ismath: |
| 773 | return self.draw_mathtext(gc, x, y, s, prop, angle) |
| 774 | |
| 775 | stream = [] # list of (ps_name, x, y, char_name) |
| 776 | |
| 777 | if mpl.rcParams['ps.useafm']: |
| 778 | font = self._get_font_afm(prop) |
| 779 | ps_name = font.postscript_name.encode("ascii", "replace").decode("ascii") |
| 780 | scale = 0.001 * prop.get_size_in_points() |
| 781 | thisx = 0 |
| 782 | last_name = '' # kerns returns 0 for ''. |
| 783 | for c in s: |
| 784 | name = uni2type1.get(ord(c), f"uni{ord(c):04X}") |
| 785 | try: |
| 786 | width = font.get_width_from_char_name(name) |
| 787 | except KeyError: |
| 788 | name = 'question' |
| 789 | width = font.get_width_char(ord('?')) |
| 790 | kern = font.get_kern_dist_from_name(last_name, name) |
| 791 | last_name = name |
| 792 | thisx += kern * scale |
| 793 | stream.append((ps_name, thisx, 0, name)) |
| 794 | thisx += width * scale |
| 795 | |
| 796 | else: |
| 797 | if mtext is not None: |
| 798 | features = mtext.get_fontfeatures() |
| 799 | language = mtext.get_language() |
| 800 | else: |
| 801 | features = language = None |
| 802 | font = self._get_font_ttf(prop) |
| 803 | for item in _text_helpers.layout(s, font, features=features, |
| 804 | language=language): |
| 805 | # NOTE: We ignore the character code in the subset, because PS uses the |
| 806 | # glyph name to write text. The subset is only used to ensure that each |
| 807 | # one does not overflow format limits. |
| 808 | subset, _ = self._character_tracker.track_glyph( |
| 809 | item.ft_object, item.char, item.glyph_index) |
| 810 | ps_name = (item.ft_object.postscript_name |
| 811 | .encode("ascii", "replace").decode("ascii")) |
| 812 | glyph_name = item.ft_object.get_glyph_name(item.glyph_index) |
| 813 | stream.append((f'{ps_name}-{subset}', item.x, item.y, glyph_name)) |
| 814 | self.set_color(*gc.get_rgb()) |
| 815 | |
| 816 | for ps_name, group in itertools.groupby(stream, lambda entry: entry[0]): |
| 817 | self.set_font(ps_name, prop.get_size_in_points(), False) |
| 818 | thetext = "\n".join(f"{x:g} {y:g} m /{name:s} glyphshow" |
| 819 | for _, x, y, name in group) |
| 820 | self._pswriter.write(f"""\ |
no test coverage detected