|
7 | 7 | from enum import Enum
|
8 | 8 | import functools
|
9 | 9 | import glob
|
10 |
| -from io import StringIO |
| 10 | +from io import BytesIO, StringIO, TextIOWrapper |
11 | 11 | import logging
|
12 | 12 | import math
|
13 | 13 | import os
|
14 | 14 | import pathlib
|
| 15 | +import tempfile |
15 | 16 | import re
|
16 | 17 | import shutil
|
17 | 18 | from tempfile import TemporaryDirectory
|
18 | 19 | import time
|
19 | 20 |
|
| 21 | +from fontTools import subset |
20 | 22 | import numpy as np
|
21 | 23 |
|
22 | 24 | import matplotlib as mpl
|
|
27 | 29 | GraphicsContextBase, RendererBase)
|
28 | 30 | from matplotlib.cbook import is_writable_file_like, file_requires_unicode
|
29 | 31 | 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 |
31 | 33 | from matplotlib._ttconv import convert_ttf_to_ps
|
32 | 34 | from matplotlib.mathtext import MathTextParser
|
33 | 35 | from matplotlib._mathtext_data import uni2type1
|
@@ -954,8 +956,36 @@ def print_figure_impl(fh):
|
954 | 956 | fh.write(_font_to_ps_type3(font_path, glyph_ids))
|
955 | 957 | else:
|
956 | 958 | 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 | + ) |
959 | 989 | except RuntimeError:
|
960 | 990 | _log.warning(
|
961 | 991 | "The PostScript backend does not currently "
|
|
0 commit comments