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

Method _embedTeXFont

lib/matplotlib/backends/backend_pdf.py:964–1014  ·  view source on GitHub ↗
(self, dvifont)

Source from the content-addressed store, hash-verified

962 return fontdictObject
963
964 def _embedTeXFont(self, dvifont):
965 tex_font_map = dviread.PsfontsMap(dviread.find_tex_file('pdftex.map'))
966 psfont = tex_font_map[dvifont.texname]
967 if psfont.filename is None:
968 raise ValueError(
969 "No usable font file found for {} (TeX: {}); "
970 "the font may lack a Type-1 version"
971 .format(psfont.psname, dvifont.texname))
972
973 # The font dictionary is the top-level object describing a font
974 fontdictObject = self.reserveObject('font dictionary')
975 fontdict = {
976 'Type': Name('Font'),
977 'Subtype': Name('Type1'),
978 }
979
980 # Read the font file and apply any encoding changes and effects
981 t1font = _type1font.Type1Font(psfont.filename)
982 if psfont.encoding is not None:
983 t1font = t1font.with_encoding(
984 {i: c for i, c in enumerate(dviread._parse_enc(psfont.encoding))}
985 )
986 if psfont.effects:
987 t1font = t1font.transform(psfont.effects)
988
989 # Reduce the font to only the glyphs used in the document, get the encoding
990 # for that subset, and compute various properties based on the encoding.
991 font_path = FontPath(dvifont.fname, dvifont.face_index)
992 charmap = self._character_tracker.used[font_path][0]
993 chars = {
994 # DVI type 1 fonts always map single glyph to single character.
995 ord(self._character_tracker.subset_to_unicode(font_path, 0, ccode))
996 for ccode in charmap
997 }
998 t1font = t1font.subset(chars, self._get_subset_prefix(charmap.values()))
999 fontdict['BaseFont'] = Name(t1font.prop['FontName'])
1000 # createType1Descriptor writes the font data as a side effect
1001 fontdict['FontDescriptor'] = self.createType1Descriptor(t1font)
1002 encoding = t1font.prop['Encoding']
1003 fontdict['Encoding'] = self._generate_encoding(encoding)
1004 fc = fontdict['FirstChar'] = min(encoding.keys(), default=0)
1005 lc = fontdict['LastChar'] = max(encoding.keys(), default=255)
1006 # Convert glyph widths from TeX 12.20 fixed point to 1/1000 text space units
1007 font_metrics = dvifont._metrics
1008 widths = [(1000 * glyph_metrics.tex_width) >> 20
1009 if (glyph_metrics := font_metrics.get_metrics(char)) else 0
1010 for char in range(fc, lc + 1)]
1011 fontdict['Widths'] = widthsObject = self.reserveObject('glyph widths')
1012 self.writeObject(widthsObject, widths)
1013 self.writeObject(fontdictObject, fontdict)
1014 return fontdictObject
1015
1016 def _generate_encoding(self, encoding):
1017 prev = -2

Callers 1

writeFontsMethod · 0.95

Calls 13

reserveObjectMethod · 0.95
with_encodingMethod · 0.95
transformMethod · 0.95
subsetMethod · 0.95
_get_subset_prefixMethod · 0.95
createType1DescriptorMethod · 0.95
_generate_encodingMethod · 0.95
writeObjectMethod · 0.95
FontPathClass · 0.90
NameClass · 0.85
subset_to_unicodeMethod · 0.80
valuesMethod · 0.80

Tested by

no test coverage detected