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

Method embedTTF

lib/matplotlib/backends/backend_pdf.py:1121–1377  ·  view source on GitHub ↗

Embed the TTF font from the named file into the document.

(self, filename, subset_index, charmap)

Source from the content-addressed store, hash-verified

1119end"""
1120
1121 def embedTTF(self, filename, subset_index, charmap):
1122 """Embed the TTF font from the named file into the document."""
1123 font = get_font(filename)
1124 fonttype = mpl.rcParams['pdf.fonttype']
1125
1126 def cvt(length, upe=font.units_per_EM, nearest=True):
1127 """Convert font coordinates to PDF glyph coordinates."""
1128 value = length / upe * 1000
1129 if nearest:
1130 return round(value)
1131 # Best(?) to round away from zero for bounding boxes and the like.
1132 if value < 0:
1133 return math.floor(value)
1134 else:
1135 return math.ceil(value)
1136
1137 def generate_unicode_cmap(subset_index, charmap):
1138 # Make the ToUnicode CMap.
1139 last_ccode = -2
1140 unicode_groups = []
1141 for ccode in sorted(charmap.keys()):
1142 if ccode != last_ccode + 1:
1143 unicode_groups.append([ccode, ccode])
1144 else:
1145 unicode_groups[-1][1] = ccode
1146 last_ccode = ccode
1147
1148 def _to_unicode(ccode):
1149 chars = self._character_tracker.subset_to_unicode(
1150 filename, subset_index, ccode)
1151 hexstr = chars.encode('utf-16be').hex()
1152 return f'<{hexstr}>'
1153
1154 width = 2 if fonttype == 3 else 4
1155 unicode_bfrange = []
1156 for start, end in unicode_groups:
1157 real_values = ' '.join(_to_unicode(x) for x in range(start, end+1))
1158 unicode_bfrange.append(
1159 f'<{start:0{width}x}> <{end:0{width}x}> [{real_values}]')
1160 unicode_cmap = (self._identityToUnicodeCMap %
1161 (len(unicode_groups),
1162 '\n'.join(unicode_bfrange).encode('ascii')))
1163
1164 return unicode_cmap
1165
1166 def embedTTFType3(font, subset_index, charmap, descriptor):
1167 """The Type 3-specific part of embedding a Truetype font"""
1168 widthsObject = self.reserveObject('font widths')
1169 fontdescObject = self.reserveObject('font descriptor')
1170 fontdictObject = self.reserveObject('font dictionary')
1171 charprocsObject = self.reserveObject('character procs')
1172 toUnicodeMapObject = self.reserveObject('ToUnicode map')
1173 differencesArray = []
1174 firstchar, lastchar = min(charmap), max(charmap)
1175 bbox = [cvt(x, nearest=False) for x in font.bbox]
1176
1177 fontdict = {
1178 'Type': Name('Font'),

Callers 1

writeFontsMethod · 0.95

Calls 3

_get_subsetted_psnameMethod · 0.95
get_fontFunction · 0.90
NameClass · 0.85

Tested by

no test coverage detected