Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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:main
base:main
Choose a base branch
Loading
fromanntzer:simple-dvifontinfo
Open
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 50 additions & 44 deletionslib/matplotlib/backends/backend_pdf.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -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 = {} # mapsdvi fontnames toembedding information
self._dviFontInfo = {} # mapspdfnames todvifonts
# differently encoded Type-1 fonts may share the same descriptor
self._type1Descriptors = {}
self._character_tracker = _backend_pdf_ps.CharacterTracker()
Expand DownExpand Up@@ -766,10 +766,31 @@
self.writeObject(self.resourceObject, resources)

fontNames = _api.deprecated("3.11")(property(lambda self: self._fontNames))
dviFontInfo = _api.deprecated("3.11")(property(lambda self: self._dviFontInfo))
type1Descriptors = _api.deprecated("3.11")(
property(lambda self: self._type1Descriptors))

@_api.deprecated("3.11")
@property
def dviFontInfo(self):
d = {}
tex_font_map = dviread.PsfontsMap(dviread.find_tex_file('pdftex.map'))

Check warning on line 776 in lib/matplotlib/backends/backend_pdf.py

View check run for this annotation

Codecov/ codecov/patch

lib/matplotlib/backends/backend_pdf.py#L775-L776

Added lines #L775 - L776 were not covered by tests
for pdfname, dvifont in self._dviFontInfo.items():
psfont = tex_font_map[dvifont.texname]

Check warning on line 778 in lib/matplotlib/backends/backend_pdf.py

View check run for this annotation

Codecov/ codecov/patch

lib/matplotlib/backends/backend_pdf.py#L778

Added line #L778 was not covered by tests
if psfont.filename is None:
raise ValueError(

Check warning on line 780 in lib/matplotlib/backends/backend_pdf.py

View check run for this annotation

Codecov/ codecov/patch

lib/matplotlib/backends/backend_pdf.py#L780

Added line #L780 was not covered by tests
"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(

Check warning on line 784 in lib/matplotlib/backends/backend_pdf.py

View check run for this annotation

Codecov/ codecov/patch

lib/matplotlib/backends/backend_pdf.py#L784

Added line #L784 was not covered by tests
dvifont=dvifont,
pdfname=pdfname,
fontfile=psfont.filename,
basefont=psfont.psname,
encodingfile=psfont.encoding,
effects=psfont.effects,
)
return d

Check warning on line 792 in lib/matplotlib/backends/backend_pdf.py

View check run for this annotation

Codecov/ codecov/patch

lib/matplotlib/backends/backend_pdf.py#L792

Added line #L792 was not covered by tests

def newPage(self, width, height):
self.endStream()

Expand DownExpand Up@@ -930,39 +951,19 @@
def dviFontName(self, dvifont):
"""
Given a dvi font object, return a name suitable for Op.selectfont.
This registers the font information internally (in ``_dviFontInfo``) if
not yet registered.
"""

dvi_info = self._dviFontInfo.get(dvifont.texname)
if dvi_info is not None:
return dvi_info.pdfname

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))

pdfname = next(self._internal_font_seq)
Register the font internally (in ``_dviFontInfo``) if not yet registered.
"""
pdfname = Name(f"F-{dvifont.texname.decode('ascii')}")
_log.debug('Assigning font %s = %s (dvi)', pdfname, dvifont.texname)
self._dviFontInfo[dvifont.texname] = types.SimpleNamespace(
dvifont=dvifont,
pdfname=pdfname,
fontfile=psfont.filename,
basefont=psfont.psname,
encodingfile=psfont.encoding,
effects=psfont.effects)
return pdfname
self._dviFontInfo[pdfname] = dvifont
return Name(pdfname)

def writeFonts(self):
fonts = {}
for dviname, info in sorted(self._dviFontInfo.items()):
Fx = info.pdfname
_log.debug('Embedding Type-1 font %s from dvi.', dviname)
fonts[Fx] = self._embedTeXFont(info)
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)
Expand DownExpand Up@@ -990,13 +991,18 @@
self.writeObject(fontdictObject, fontdict)
return fontdictObject

def _embedTeXFont(self, fontinfo):
_log.debug('Embedding TeX font %s - fontinfo=%s',
fontinfo.dvifont.texname, fontinfo.__dict__)
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 =fontinfo.dvifont._tfm
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
Expand All@@ -1014,28 +1020,28 @@
}

# Encoding (if needed)
iffontinfo.encodingfile is not None:
ifpsfont.encoding is not None:
fontdict['Encoding'] = {
'Type': Name('Encoding'),
'Differences': [
0, *map(Name, dviread._parse_enc(fontinfo.encodingfile))],
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(fontinfo.fontfile)
iffontinfo.effects:
t1font = t1font.transform(fontinfo.effects)
t1font = _type1font.Type1Font(psfont.filename)
ifpsfont.effects:
t1font = t1font.transform(psfont.effects)

Check warning on line 1033 in lib/matplotlib/backends/backend_pdf.py

View check run for this annotation

Codecov/ codecov/patch

lib/matplotlib/backends/backend_pdf.py#L1033

Added line #L1033 was not covered by tests
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 = (fontinfo.effects.get('slant', 0.0),
fontinfo.effects.get('extend', 1.0))
fontdesc = self._type1Descriptors.get((fontinfo.fontfile, effects))
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.createType1Descriptor(t1font)
self._type1Descriptors[(fontinfo.fontfile, effects)] = fontdesc
fontdesc = self._type1Descriptors[psfont.filename, effects] = \
self.createType1Descriptor(t1font)
fontdict['FontDescriptor'] = fontdesc

self.writeObject(fontdictObject, fontdict)
Expand Down
Loading

[8]ページ先頭

©2009-2025 Movatter.jp