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

Appearance settings

Commit adebae4

Browse filesBrowse files
committed
More work on PDF backend
1 parent d87ada8 commit adebae4
Copy full SHA for adebae4

File tree

Expand file treeCollapse file tree

5 files changed

+39
-6
lines changed
Filter options
Expand file treeCollapse file tree

5 files changed

+39
-6
lines changed

‎lib/matplotlib/_text_helpers.py

Copy file name to clipboardExpand all lines: lib/matplotlib/_text_helpers.py
+3-1Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,14 @@ def layout(string, font, *, kern_mode=KERNING_DEFAULT):
5757
"""
5858
x = 0
5959
prev_glyph_idx = None
60+
print("Inside _text_helpers.py")
61+
# breakpoint()
6062
for char in string:
6163
glyph_idx = font.get_char_index(ord(char))
6264
kern = (font.get_kerning(prev_glyph_idx, glyph_idx, kern_mode) / 64
6365
if prev_glyph_idx is not None else 0.)
6466
x += kern
65-
glyph = font.load_glyph(glyph_idx, flags=LOAD_NO_HINTING)
67+
glyph = font.load_glyph(glyph_idx, flags=LOAD_NO_HINTING, fallback=True)
6668
yield LayoutItem(char, glyph_idx, x, kern)
6769
x += glyph.linearHoriAdvance / 65536
6870
prev_glyph_idx = glyph_idx

‎lib/matplotlib/backends/_backend_pdf_ps.py

Copy file name to clipboardExpand all lines: lib/matplotlib/backends/_backend_pdf_ps.py
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ def _get_font_afm(self, prop):
143143
return _cached_get_afm_from_fname(fname)
144144

145145
def _get_font_ttf(self, prop):
146-
fname = font_manager.findfont(prop)
146+
fname = font_manager.find_fontsprop(prop)
147147
font = font_manager.get_font(fname)
148148
font.clear()
149149
font.set_size(prop.get_size_in_points(), 72)

‎lib/matplotlib/backends/backend_pdf.py

Copy file name to clipboardExpand all lines: lib/matplotlib/backends/backend_pdf.py
+18-3Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -692,6 +692,7 @@ def __init__(self, filename, metadata=None):
692692
# differently encoded Type-1 fonts may share the same descriptor
693693
self.type1Descriptors = {}
694694
self._character_tracker = _backend_pdf_ps.CharacterTracker()
695+
# self._char_to_font = None
695696

696697
self.alphaStates = {} # maps alpha values to graphics state objects
697698
self._alpha_state_seq = (Name(f'A{i}') for i in itertools.count(1))
@@ -911,7 +912,8 @@ def dviFontName(self, dvifont):
911912
return pdfname
912913

913914
def writeFonts(self):
914-
print(self.fontNames)
915+
# print("OHNO")
916+
# breakpoint()
915917
fonts = {}
916918
for dviname, info in sorted(self.dviFontInfo.items()):
917919
Fx = info.pdfname
@@ -927,7 +929,15 @@ def writeFonts(self):
927929
else:
928930
# a normal TrueType font
929931
_log.debug('Writing TrueType font.')
932+
# characters = []
933+
print("lalalla")
934+
# breakpoint()
935+
# for key, val in self._char_to_font:
936+
# if val.fname == filename:
937+
# print("nice", filename)
938+
# characters.append(key)
930939
chars = self._character_tracker.used.get(filename)
940+
print("chars:", chars, " for font:", filename)
931941
if chars:
932942
fonts[Fx] = self.embedTTF(filename, chars)
933943
self.writeObject(self.fontObject, fonts)
@@ -1149,7 +1159,7 @@ def embedTTFType3(font, characters, descriptor):
11491159
def get_char_width(charcode):
11501160
s = ord(cp1252.decoding_table[charcode])
11511161
width = font.load_char(
1152-
s, flags=LOAD_NO_SCALE | LOAD_NO_HINTING).horiAdvance
1162+
s, fallback=False, flags=LOAD_NO_SCALE | LOAD_NO_HINTING).horiAdvance
11531163
return cvt(width)
11541164

11551165
with warnings.catch_warnings():
@@ -2344,7 +2354,12 @@ def draw_text(self, gc, x, y, s, prop, angle, ismath=False, mtext=None):
23442354
fonttype = 1
23452355
else:
23462356
font = self._get_font_ttf(prop)
2347-
self.file._character_tracker.track(font, s)
2357+
font.set_text(s)
2358+
char_to_font = font.get_char_to_font()
2359+
for char, font in char_to_font.items():
2360+
print(chr(char), "to:", font.fname)
2361+
self.file._character_tracker.track(font, chr(char))
2362+
print("\nFONT_TO_CHAR:", self.file._character_tracker.used)
23482363
fonttype = mpl.rcParams['pdf.fonttype']
23492364

23502365
if gc.get_url() is not None:

‎src/ft2font.cpp

Copy file name to clipboardExpand all lines: src/ft2font.cpp
+11Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -735,6 +735,17 @@ void FT2Font::load_glyph(FT_UInt glyph_index,
735735

736736
void FT2Font::load_glyph(FT_UInt glyph_index, FT_Int32 flags)
737737
{
738+
// search cache first
739+
if (fallback == 1 && glyph_to_font.find(glyph_index) != glyph_to_font.end()) {
740+
ft_object = glyph_to_font[glyph_index];
741+
return;
742+
}
743+
// can not do fallback without a charcode
744+
// so ignore exact condition fallback == 1
745+
746+
// set as self
747+
ft_object = this;
748+
738749
if (FT_Error error = FT_Load_Glyph(face, glyph_index, flags)) {
739750
throw_ft_error("Could not load glyph", error);
740751
}

‎src/ft2font_wrapper.cpp

Copy file name to clipboardExpand all lines: src/ft2font_wrapper.cpp
+6-1Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -766,7 +766,8 @@ static PyObject *PyFT2Font_load_char(PyFT2Font *self, PyObject *args, PyObject *
766766
long charcode;
767767
int fallback = 1;
768768
FT_Int32 flags = FT_LOAD_FORCE_AUTOHINT;
769-
const char *names[] = { "charcode", "flags", NULL };
769+
const char *names[] = { "charcode", "flags", "fallback", NULL };
770+
printf("Loading char!\n");
770771

771772
/* This makes a technically incorrect assumption that FT_Int32 is
772773
int. In theory it can also be long, if the size of int is less
@@ -775,6 +776,10 @@ static PyObject *PyFT2Font_load_char(PyFT2Font *self, PyObject *args, PyObject *
775776
&flags)) {
776777
return NULL;
777778
}
779+
780+
FT2Font *ft_object = NULL;
781+
CALL_CPP("load_char", (self->x->load_char(charcode, flags, fallback, ft_object)));
782+
printf("Char %ld loaded to: %lu\n", charcode, ft_object->get_face()->num_glyphs);
778783

779784
FT2Font *ft_object = NULL;
780785
CALL_CPP("load_char", (self->x->load_char(charcode, flags, ft_object, (bool)fallback)));

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.