Skip to content

Commit a036962

Browse files
committed
Implement subsetting for PS backend
1 parent 19d5e1a commit a036962

File tree

1 file changed

+34
-4
lines changed

1 file changed

+34
-4
lines changed

lib/matplotlib/backends/backend_ps.py

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,18 @@
77
from enum import Enum
88
import functools
99
import glob
10-
from io import StringIO
10+
from io import BytesIO, StringIO, TextIOWrapper
1111
import logging
1212
import math
1313
import os
1414
import pathlib
15+
import tempfile
1516
import re
1617
import shutil
1718
from tempfile import TemporaryDirectory
1819
import time
1920

21+
from fontTools import subset
2022
import numpy as np
2123

2224
import matplotlib as mpl
@@ -27,7 +29,7 @@
2729
GraphicsContextBase, RendererBase)
2830
from matplotlib.cbook import is_writable_file_like, file_requires_unicode
2931
from matplotlib.font_manager import get_font
30-
from matplotlib.ft2font import LOAD_NO_HINTING, LOAD_NO_SCALE
32+
from matplotlib.ft2font import LOAD_NO_HINTING, LOAD_NO_SCALE, FT2Font
3133
from matplotlib._ttconv import convert_ttf_to_ps
3234
from matplotlib.mathtext import MathTextParser
3335
from matplotlib._mathtext_data import uni2type1
@@ -954,8 +956,36 @@ def print_figure_impl(fh):
954956
fh.write(_font_to_ps_type3(font_path, glyph_ids))
955957
else:
956958
try:
957-
convert_ttf_to_ps(os.fsencode(font_path),
958-
fh, fonttype, glyph_ids)
959+
_log.debug(
960+
f"SUBSET {font_path} characters: "
961+
f"{''.join(chr(c) for c in chars)}"
962+
)
963+
fontdata = getSubset(
964+
font_path, "".join(chr(c) for c in chars)
965+
)
966+
_log.debug(
967+
f"SUBSET {font_path} "
968+
f"{os.stat(font_path).st_size} "
969+
f"↦ {len(fontdata)}"
970+
)
971+
972+
# give ttconv a subsetted font
973+
# along with updated glyph_ids
974+
with tempfile.NamedTemporaryFile(
975+
suffix=".ttf"
976+
) as tmp:
977+
tmp.write(fontdata)
978+
tmp.seek(0, 0)
979+
font = FT2Font(tmp.name)
980+
glyph_ids = [
981+
font.get_char_index(c) for c in chars
982+
]
983+
convert_ttf_to_ps(
984+
os.fsencode(tmp.name),
985+
fh,
986+
fonttype,
987+
glyph_ids,
988+
)
959989
except RuntimeError:
960990
_log.warning(
961991
"The PostScript backend does not currently "

0 commit comments

Comments
 (0)