Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork7.9k
Simplify dviFontInfo layout in backend pdf.#30082
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Open
anntzer wants to merge1 commit intomatplotlib:mainChoose a base branch fromanntzer:simple-dvifontinfo
base:main
Could not load branches
Branch not found:{{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline, and old review comments may become outdated.
Uh oh!
There was an error while loading.Please reload this page.
Open
Changes fromall commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
94 changes: 50 additions & 44 deletionslib/matplotlib/backends/backend_pdf.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -721,7 +721,7 @@ | ||
self._internal_font_seq = (Name(f'F{i}') for i in itertools.count(1)) | ||
self._fontNames = {} # maps filenames to internal font names | ||
self._dviFontInfo = {} # mapspdfnames todvifonts | ||
# differently encoded Type-1 fonts may share the same descriptor | ||
self._type1Descriptors = {} | ||
self._character_tracker = _backend_pdf_ps.CharacterTracker() | ||
@@ -766,10 +766,31 @@ | ||
self.writeObject(self.resourceObject, resources) | ||
fontNames = _api.deprecated("3.11")(property(lambda self: self._fontNames)) | ||
type1Descriptors = _api.deprecated("3.11")( | ||
property(lambda self: self._type1Descriptors)) | ||
@_api.deprecated("3.11") | ||
@property | ||
def dviFontInfo(self): | ||
QuLogic marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
d = {} | ||
tex_font_map = dviread.PsfontsMap(dviread.find_tex_file('pdftex.map')) | ||
for pdfname, dvifont in self._dviFontInfo.items(): | ||
psfont = tex_font_map[dvifont.texname] | ||
if psfont.filename is None: | ||
raise ValueError( | ||
"No usable font file found for {} (TeX: {}); " | ||
"the font may lack a Type-1 version" | ||
.format(psfont.psname, dvifont.texname)) | ||
d[dvifont.texname] = types.SimpleNamespace( | ||
dvifont=dvifont, | ||
pdfname=pdfname, | ||
fontfile=psfont.filename, | ||
basefont=psfont.psname, | ||
encodingfile=psfont.encoding, | ||
effects=psfont.effects, | ||
) | ||
return d | ||
def newPage(self, width, height): | ||
self.endStream() | ||
@@ -930,39 +951,19 @@ | ||
def dviFontName(self, dvifont): | ||
""" | ||
Given a dvi font object, return a name suitable for Op.selectfont. | ||
Register the font internally (in ``_dviFontInfo``) if not yet registered. | ||
""" | ||
pdfname = Name(f"F-{dvifont.texname.decode('ascii')}") | ||
QuLogic marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
_log.debug('Assigning font %s = %s (dvi)', pdfname, dvifont.texname) | ||
self._dviFontInfo[pdfname] = dvifont | ||
return Name(pdfname) | ||
def writeFonts(self): | ||
fonts = {} | ||
for pdfname, dvifont in sorted(self._dviFontInfo.items()): | ||
_log.debug('Embedding Type-1 font %s from dvi.', dvifont.texname) | ||
fonts[pdfname] = self._embedTeXFont(dvifont) | ||
for filename in sorted(self._fontNames): | ||
Fx = self._fontNames[filename] | ||
_log.debug('Embedding font %s.', filename) | ||
@@ -990,13 +991,18 @@ | ||
self.writeObject(fontdictObject, fontdict) | ||
return fontdictObject | ||
def _embedTeXFont(self, dvifont): | ||
tex_font_map = dviread.PsfontsMap(dviread.find_tex_file('pdftex.map')) | ||
psfont = tex_font_map[dvifont.texname] | ||
if psfont.filename is None: | ||
raise ValueError( | ||
"No usable font file found for {} (TeX: {}); " | ||
"the font may lack a Type-1 version" | ||
.format(psfont.psname, dvifont.texname)) | ||
# Widths | ||
widthsObject = self.reserveObject('font widths') | ||
tfm = dvifont._tfm | ||
# convert from TeX's 12.20 representation to 1/1000 text space units. | ||
widths = [(1000 * metrics.tex_width) >> 20 | ||
if (metrics := tfm.get_metrics(char)) else 0 | ||
@@ -1014,28 +1020,28 @@ | ||
} | ||
# Encoding (if needed) | ||
ifpsfont.encoding is not None: | ||
fontdict['Encoding'] = { | ||
'Type': Name('Encoding'), | ||
'Differences': [ | ||
0, *map(Name, dviread._parse_enc(psfont.encoding))], | ||
} | ||
# We have a font file to embed - read it in and apply any effects | ||
t1font = _type1font.Type1Font(psfont.filename) | ||
ifpsfont.effects: | ||
t1font = t1font.transform(psfont.effects) | ||
fontdict['BaseFont'] = Name(t1font.prop['FontName']) | ||
# Font descriptors may be shared between differently encoded | ||
# Type-1 fonts, so only create a new descriptor if there is no | ||
# existing descriptor for this font. | ||
effects = (psfont.effects.get('slant', 0.0), | ||
psfont.effects.get('extend', 1.0)) | ||
fontdesc = self._type1Descriptors.get((psfont.filename, effects)) | ||
if fontdesc is None: | ||
fontdesc = self._type1Descriptors[psfont.filename, effects] = \ | ||
self.createType1Descriptor(t1font) | ||
fontdict['FontDescriptor'] = fontdesc | ||
self.writeObject(fontdictObject, fontdict) | ||
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.