MCPcopy Index your code
hub / github.com/matplotlib/matplotlib / draw_text

Method draw_text

lib/matplotlib/backends/backend_ps.py:763–827  ·  view source on GitHub ↗
(self, gc, x, y, s, prop, angle, ismath=False, mtext=None)

Source from the content-addressed store, hash-verified

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"""\

Callers 1

draw_texMethod · 0.95

Calls 15

_is_transparentMethod · 0.95
draw_texMethod · 0.95
draw_mathtextMethod · 0.95
set_colorMethod · 0.95
set_fontMethod · 0.95
_get_clip_cmdMethod · 0.95
_get_font_afmMethod · 0.80
get_width_charMethod · 0.80
get_fontfeaturesMethod · 0.80
get_languageMethod · 0.80

Tested by

no test coverage detected