Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Implement multi-font embedding for PS Backend #20832

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 32 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
8b2257d
Parse fallback_list through wrapper
aitikgupta Jul 25, 2021
8114a54
Define new/modify previous FT2Font functions
aitikgupta Jul 25, 2021
a4b78c8
Implement new/modify previous FT2Font functions
aitikgupta Jul 25, 2021
67dbcf8
Parse multiple fonts for a single FT2Font object
aitikgupta Jul 25, 2021
69cf28b
Trigger font fallback for Agg backend
aitikgupta Jul 25, 2021
d5aef82
Remove prints
aitikgupta Aug 7, 2021
aa82ecd
Cleanup wrapper
aitikgupta Aug 7, 2021
bea7a2d
Remove stale prints
aitikgupta Aug 7, 2021
5117398
Do not warn for get_char_index
aitikgupta Aug 7, 2021
fc413dc
Left != Right kerning comment
aitikgupta Aug 7, 2021
3f42cbd
Windows compiler fix
aitikgupta Aug 7, 2021
9c062cd
Add fallback test for Agg backend
aitikgupta Aug 13, 2021
cf4f743
Debug fontNames
aitikgupta Jul 28, 2021
9950a3e
Segfaults on exit
aitikgupta Jul 29, 2021
ab2f95a
More work on PDF backend
aitikgupta Jul 31, 2021
41e247d
Implement another approach
aitikgupta Aug 1, 2021
e5b4dae
Revisit the approach
aitikgupta Aug 3, 2021
3af91d8
Type3 PDF Backend works!
aitikgupta Aug 6, 2021
7ee77f2
Type42 PDF fallback works!
aitikgupta Aug 6, 2021
3cacd45
Create a fill_glyphs method
aitikgupta Aug 7, 2021
8719e2e
Use fill_glyphs instead of set_text
aitikgupta Aug 7, 2021
e6c7998
Cleanup wrapper
aitikgupta Aug 7, 2021
f86ec41
Few cleanups
aitikgupta Aug 7, 2021
a6d21cd
Rebase from Agg backend
aitikgupta Aug 7, 2021
ecf0556
Use multi-font output for PS backend
aitikgupta Aug 13, 2021
b55f011
Add multi-font tests for PS backend
aitikgupta Aug 13, 2021
83b227d
Add baseline images
aitikgupta Aug 13, 2021
913ad4c
Specify font number for TTC fonts
aitikgupta Aug 13, 2021
6fe8f44
Flake8 fixes
aitikgupta Aug 13, 2021
a3e7f03
Fix memory leak and render tofu
aitikgupta Aug 18, 2021
986ecfe
Check if fallback font is available
aitikgupta Aug 18, 2021
1c85f46
Check for Agg as well
aitikgupta Aug 18, 2021
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
5 changes: 3 additions & 2 deletions lib/matplotlib/_text_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@


LayoutItem = dataclasses.make_dataclass(
"LayoutItem", ["char", "glyph_idx", "x", "prev_kern"])
"LayoutItem", ["ft_object", "char", "glyph_idx", "x", "prev_kern"])


def warn_on_missing_glyph(codepoint):
Expand Down Expand Up @@ -63,6 +63,7 @@ def layout(string, font, *, kern_mode=KERNING_DEFAULT):
if prev_glyph_idx is not None else 0.)
x += kern
glyph = font.load_glyph(glyph_idx, flags=LOAD_NO_HINTING)
yield LayoutItem(char, glyph_idx, x, kern)
ft_object = font.get_glyph_to_font().get(glyph_idx, font)
yield LayoutItem(ft_object, char, glyph_idx, x, kern)
x += glyph.linearHoriAdvance / 65536
prev_glyph_idx = glyph_idx
9 changes: 7 additions & 2 deletions lib/matplotlib/backends/_backend_pdf_ps.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Common functionality between the PDF and PS backends.
"""

import os
from io import BytesIO
import functools

Expand Down Expand Up @@ -37,7 +38,11 @@ def get_glyphs_subset(fontfile, characters):
options = subset.Options(glyph_names=True, recommended_glyphs=True)

# prevent subsetting FontForge Timestamp and other tables
options.drop_tables += ['FFTM', 'PfEd']
options.drop_tables += ['FFTM', 'PfEd', 'BDF']

# if fontfile is a ttc, specify font number
if os.path.splitext(fontfile)[1] == ".ttc":
options.font_number = 0

with subset.load_font(fontfile, options) as font:
subsetter = subset.Subsetter(options=options)
Expand Down Expand Up @@ -136,7 +141,7 @@ def _get_font_afm(self, prop):
return _cached_get_afm_from_fname(fname)

def _get_font_ttf(self, prop):
fname = font_manager.findfont(prop)
fname = font_manager.find_fontsprop(prop)
font = font_manager.get_font(fname)
font.clear()
font.set_size(prop.get_size_in_points(), 72)
Expand Down
4 changes: 2 additions & 2 deletions lib/matplotlib/backends/backend_agg.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
from matplotlib.backend_bases import (
_Backend, _check_savefig_extra_args, FigureCanvasBase, FigureManagerBase,
RendererBase)
from matplotlib.font_manager import findfont, get_font
from matplotlib.font_manager import find_fontsprop, get_font
from matplotlib.ft2font import (LOAD_FORCE_AUTOHINT, LOAD_NO_HINTING,
LOAD_DEFAULT, LOAD_NO_AUTOHINT)
from matplotlib.mathtext import MathTextParser
Expand Down Expand Up @@ -251,7 +251,7 @@ def _get_agg_font(self, prop):
"""
Get the font for text instance t, caching for efficiency
"""
fname = findfont(prop)
fname = find_fontsprop(prop)
font = get_font(fname)

font.clear()
Expand Down
76 changes: 47 additions & 29 deletions lib/matplotlib/backends/backend_pdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
GraphicsContextBase, RendererBase)
from matplotlib.backends.backend_mixed import MixedModeRenderer
from matplotlib.figure import Figure
from matplotlib.font_manager import findfont, get_font
from matplotlib.font_manager import findfont, find_fontsprop, get_font
from matplotlib.afm import AFM
import matplotlib.type1font as type1font
import matplotlib.dviread as dviread
Expand Down Expand Up @@ -861,20 +861,28 @@ def fontName(self, fontprop):
"""

if isinstance(fontprop, str):
filename = fontprop
filenames = [fontprop]
elif mpl.rcParams['pdf.use14corefonts']:
filename = findfont(
fontprop, fontext='afm', directory=RendererPdf._afm_font_dir)
filenames = find_fontsprop(
fontprop, fontext='afm', directory=RendererPdf._afm_font_dir
).values()
else:
filename = findfont(fontprop)

Fx = self.fontNames.get(filename)
if Fx is None:
Fx = next(self._internal_font_seq)
self.fontNames[filename] = Fx
_log.debug('Assigning font %s = %r', Fx, filename)

return Fx
filenames = find_fontsprop(fontprop).values()
first_Fx = None
for fname in filenames:
Fx = self.fontNames.get(fname)
if not first_Fx:
first_Fx = Fx
if Fx is None:
Fx = next(self._internal_font_seq)
self.fontNames[fname] = Fx
_log.debug('Assigning font %s = %r', Fx, fname)
if not first_Fx:
first_Fx = Fx

# find_fontsprop's first value always adheres to
# findfont's value, so technically no behaviour change
return first_Fx

def dviFontName(self, dvifont):
"""
Expand Down Expand Up @@ -1143,7 +1151,6 @@ def get_char_width(charcode):
width = font.load_char(
s, flags=LOAD_NO_SCALE | LOAD_NO_HINTING).horiAdvance
return cvt(width)

with warnings.catch_warnings():
# Ignore 'Required glyph missing from current font' warning
# from ft2font: here we're just building the widths table, but
Expand Down Expand Up @@ -2331,7 +2338,9 @@ def draw_text(self, gc, x, y, s, prop, angle, ismath=False, mtext=None):
fonttype = 1
else:
font = self._get_font_ttf(prop)
self.file._character_tracker.track(font, s)
char_to_font = font.fill_glyphs(s)
for char, font in char_to_font.items():
self.file._character_tracker.track(font, chr(char))
fonttype = mpl.rcParams['pdf.fonttype']

if gc.get_url() is not None:
Expand Down Expand Up @@ -2371,22 +2380,27 @@ def draw_text(self, gc, x, y, s, prop, angle, ismath=False, mtext=None):
# the regular text show command (TJ) with appropriate kerning between
# chunks, whereas multibyte characters use the XObject command (Do).
else:
# List of (start_x, [prev_kern, char, char, ...]), w/o zero kerns.
# List of (ft_object, start_x, [prev_kern, char, char, ...]),
# w/o zero kerns.
singlebyte_chunks = []
# List of (start_x, glyph_index).
# List of (ft_object, start_x, glyph_index).
multibyte_glyphs = []
prev_was_multibyte = True
prev_font = font
for item in _text_helpers.layout(
s, font, kern_mode=KERNING_UNFITTED):
if _font_supports_glyph(fonttype, ord(item.char)):
if prev_was_multibyte:
singlebyte_chunks.append((item.x, []))
singlebyte_chunks.append((item.ft_object, item.x, []))
prev_font = item.ft_object
if item.prev_kern:
singlebyte_chunks[-1][1].append(item.prev_kern)
singlebyte_chunks[-1][1].append(item.char)
singlebyte_chunks[-1][2].append(item.prev_kern)
singlebyte_chunks[-1][2].append(item.char)
prev_was_multibyte = False
else:
multibyte_glyphs.append((item.x, item.glyph_idx))
multibyte_glyphs.append(
(item.ft_object, item.x, item.glyph_idx)
)
prev_was_multibyte = True
# Do the rotation and global translation as a single matrix
# concatenation up front
Expand All @@ -2396,10 +2410,12 @@ def draw_text(self, gc, x, y, s, prop, angle, ismath=False, mtext=None):
-math.sin(a), math.cos(a),
x, y, Op.concat_matrix)
# Emit all the 1-byte characters in a BT/ET group.
self.file.output(Op.begin_text,
self.file.fontName(prop), fontsize, Op.selectfont)

self.file.output(Op.begin_text)
prev_start_x = 0
for start_x, kerns_or_chars in singlebyte_chunks:
for ft_object, start_x, kerns_or_chars in singlebyte_chunks:
ft_name = self.file.fontName(ft_object.fname)
self.file.output(ft_name, fontsize, Op.selectfont)
self._setup_textpos(start_x, 0, 0, prev_start_x, 0, 0)
self.file.output(
# See pdf spec "Text space details" for the 1000/fontsize
Expand All @@ -2411,14 +2427,16 @@ def draw_text(self, gc, x, y, s, prop, angle, ismath=False, mtext=None):
prev_start_x = start_x
self.file.output(Op.end_text)
# Then emit all the multibyte characters, one at a time.
for start_x, glyph_idx in multibyte_glyphs:
self._draw_xobject_glyph(font, fontsize, glyph_idx, start_x, 0)
for ft_object, start_x, glyph_idx in multibyte_glyphs:
self._draw_xobject_glyph(
ft_object, fontsize, glyph_idx, start_x, 0
)
self.file.output(Op.grestore)

def _draw_xobject_glyph(self, font, fontsize, glyph_idx, x, y):
def _draw_xobject_glyph(self, ft_object, fontsize, glyph_idx, x, y):
"""Draw a multibyte character from a Type 3 font as an XObject."""
symbol_name = font.get_glyph_name(glyph_idx)
name = self.file._get_xobject_symbol_name(font.fname, symbol_name)
symbol_name = ft_object.get_glyph_name(glyph_idx)
name = self.file._get_xobject_symbol_name(ft_object.fname, symbol_name)
self.file.output(
Op.gsave,
0.001 * fontsize, 0, 0, 0.001 * fontsize, x, y, Op.concat_matrix,
Expand Down
43 changes: 30 additions & 13 deletions lib/matplotlib/backends/backend_ps.py
Original file line number Diff line number Diff line change
Expand Up @@ -640,10 +640,12 @@ def draw_text(self, gc, x, y, s, prop, angle, ismath=False, mtext=None):
if ismath:
return self.draw_mathtext(gc, x, y, s, prop, angle)

self.set_color(*gc.get_rgb())

if mpl.rcParams['ps.useafm']:
font = self._get_font_afm(prop)
scale = 0.001 * prop.get_size_in_points()

stream = []
thisx = 0
last_name = None # kerns returns 0 for None.
xs_names = []
Expand All @@ -659,21 +661,36 @@ def draw_text(self, gc, x, y, s, prop, angle, ismath=False, mtext=None):
thisx += kern * scale
xs_names.append((thisx, name))
thisx += width * scale
ps_name = (font.postscript_name
.encode("ascii", "replace").decode("ascii"))
stream.append((ps_name, xs_names))

else:
font = self._get_font_ttf(prop)
font.set_text(s, 0, flags=LOAD_NO_HINTING)
self._character_tracker.track(font, s)
xs_names = [(item.x, font.get_glyph_name(item.glyph_idx))
for item in _text_helpers.layout(s, font)]

self.set_color(*gc.get_rgb())
ps_name = (font.postscript_name
.encode("ascii", "replace").decode("ascii"))
self.set_font(ps_name, prop.get_size_in_points())
thetext = "\n".join(f"{x:f} 0 m /{name:s} glyphshow"
for x, name in xs_names)
self._pswriter.write(f"""\
char_to_font = font.fill_glyphs(s, 0, flags=LOAD_NO_HINTING)
for char, font in char_to_font.items():
self._character_tracker.track(font, chr(char))
stream = []
prev_font = curr_stream = None
for item in _text_helpers.layout(s, font):
ps_name = (item.ft_object.postscript_name
.encode("ascii", "replace").decode("ascii"))
if item.ft_object is not prev_font:
if curr_stream:
stream.append(curr_stream)
prev_font = item.ft_object
curr_stream = [ps_name, []]
curr_stream[1].append(
(item.x, item.ft_object.get_glyph_name(item.glyph_idx))
)
# append the last entry
stream.append(curr_stream)

for ps_name, xs_names in stream:
self.set_font(ps_name, prop.get_size_in_points(), False)
thetext = "\n".join(f"{x:f} 0 m /{name:s} glyphshow"
for x, name in xs_names)
self._pswriter.write(f"""\
gsave
{self._get_clip_cmd(gc)}
{x:f} {y:f} translate
Expand Down
Loading