Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
d0906aa
Parse fallback_list through wrapper
aitikgupta Jul 25, 2021
f6f2fd7
Define new/modify previous FT2Font functions
aitikgupta Jul 25, 2021
5199773
Implement new/modify previous FT2Font functions
aitikgupta Jul 25, 2021
88da97f
Parse multiple fonts for a single FT2Font object
aitikgupta Jul 25, 2021
c6c3a45
Trigger font fallback for Agg backend
aitikgupta Jul 25, 2021
e868fbc
Remove prints
aitikgupta Aug 7, 2021
4230469
Cleanup wrapper
aitikgupta Aug 7, 2021
991c796
Remove stale prints
aitikgupta Aug 7, 2021
fa385eb
Do not warn for get_char_index
aitikgupta Aug 7, 2021
471ae8c
Left != Right kerning comment
aitikgupta Aug 7, 2021
86878c8
Windows compiler fix
aitikgupta Aug 7, 2021
5ccb7de
Add fallback test for Agg backend
aitikgupta Aug 13, 2021
ed495d3
Debug fontNames
aitikgupta Jul 28, 2021
d87ada8
Segfaults on exit
aitikgupta Jul 29, 2021
adebae4
More work on PDF backend
aitikgupta Jul 31, 2021
9983c66
Implement another approach
aitikgupta Aug 1, 2021
2ef863e
Revisit the approach
aitikgupta Aug 3, 2021
f12c295
Type3 PDF Backend works!
aitikgupta Aug 6, 2021
1327b82
Type42 PDF fallback works!
aitikgupta Aug 6, 2021
799ffde
Create a fill_glyphs method
aitikgupta Aug 7, 2021
7ce1805
Use fill_glyphs instead of set_text
aitikgupta Aug 7, 2021
ac1c9c9
Cleanup wrapper
aitikgupta Aug 7, 2021
8a30aaf
Few cleanups
aitikgupta Aug 7, 2021
e675047
Rebase from Agg backend
aitikgupta Aug 7, 2021
1b53070
Specify font number for TTC fonts
aitikgupta Aug 13, 2021
0e0d92c
Flake8 fixes
aitikgupta Aug 13, 2021
cb50510
Add multi-font tests for PDF backend
aitikgupta Aug 13, 2021
70b0e18
Add baseline images
aitikgupta Aug 13, 2021
42b6d6f
Merge branch 'master' into pdf-fallback
aitikgupta Aug 13, 2021
8fd0729
Fix memory leak and render tofu
aitikgupta Aug 17, 2021
7c88286
Check if fallback font is available
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
Prev Previous commit
Next Next commit
Implement another approach
  • Loading branch information
aitikgupta committed Aug 13, 2021
commit 9983c6610cb39c84e8ef2121170fcebee64c51d1
3 changes: 2 additions & 1 deletion lib/matplotlib/_text_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,12 @@ def layout(string, font, *, kern_mode=KERNING_DEFAULT):
print("Inside _text_helpers.py")
# breakpoint()
for char in string:
print("\nchecking:", char, "\n")
glyph_idx = font.get_char_index(ord(char))
kern = (font.get_kerning(prev_glyph_idx, glyph_idx, kern_mode) / 64
if prev_glyph_idx is not None else 0.)
x += kern
glyph = font.load_glyph(glyph_idx, flags=LOAD_NO_HINTING, fallback=True)
glyph = font.load_glyph(glyph_idx, flags=LOAD_NO_HINTING)
yield LayoutItem(char, glyph_idx, x, kern)
x += glyph.linearHoriAdvance / 65536
prev_glyph_idx = glyph_idx
1 change: 0 additions & 1 deletion lib/matplotlib/backends/backend_agg.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,6 @@ def draw_text(self, gc, x, y, s, prop, angle, ismath=False, mtext=None):
return None
# We pass '0' for angle here, since it will be rotated (in raster
# space) in the following call to draw_text_image).
print("BEFORE")
font.set_text(s, 0, flags=flags)
font.draw_glyphs_to_bitmap(
antialiased=mpl.rcParams['text.antialiased'])
Expand Down
38 changes: 21 additions & 17 deletions lib/matplotlib/backends/backend_pdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -879,6 +879,7 @@ def fontName(self, fontprop):
_log.debug('Assigning font %s = %r', Fx, fname)
Fxs.append(Fx)

# return only the first for Op.selectfont to work
return Fxs[0]

def dviFontName(self, dvifont):
Expand Down Expand Up @@ -912,8 +913,7 @@ def dviFontName(self, dvifont):
return pdfname

def writeFonts(self):
# print("OHNO")
# breakpoint()
print("fonts: ", self.fontNames)
fonts = {}
for dviname, info in sorted(self.dviFontInfo.items()):
Fx = info.pdfname
Expand All @@ -929,13 +929,6 @@ def writeFonts(self):
else:
# a normal TrueType font
_log.debug('Writing TrueType font.')
# characters = []
print("lalalla")
# breakpoint()
# for key, val in self._char_to_font:
# if val.fname == filename:
# print("nice", filename)
# characters.append(key)
chars = self._character_tracker.used.get(filename)
print("chars:", chars, " for font:", filename)
if chars:
Expand Down Expand Up @@ -1079,14 +1072,14 @@ def createType1Descriptor(self, t1font, fontfile):
return fontdescObject

def _get_xobject_symbol_name(self, filename, symbol_name):
# since filename is a string
Fx = self.fontName(filename)
# TODO: XObject symbol name should be multiple names?
# list(map(lambda x: x.name.decode(), Fxs))
# Fx = Fxs[0]
return "-".join([
x = "-".join([
Fx.name.decode(),
os.path.splitext(os.path.basename(filename))[0],
symbol_name])
print("\n\nXOBJECT", x, "\n\n")
return x

_identityToUnicodeCMap = b"""/CIDInit /ProcSet findresource begin
12 dict begin
Expand All @@ -1113,6 +1106,7 @@ def embedTTF(self, filename, characters):
"""Embed the TTF font from the named file into the document."""

font = get_font(filename)
print("embedding:", font.fname)
fonttype = mpl.rcParams['pdf.fonttype']

def cvt(length, upe=font.units_per_EM, nearest=True):
Expand Down Expand Up @@ -1159,9 +1153,8 @@ def embedTTFType3(font, characters, descriptor):
def get_char_width(charcode):
s = ord(cp1252.decoding_table[charcode])
width = font.load_char(
s, fallback=False, flags=LOAD_NO_SCALE | LOAD_NO_HINTING).horiAdvance
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 @@ -2424,6 +2417,11 @@ 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.

x = self.file.fontName(prop)
print(x)
# breakpoint()

self.file.output(Op.begin_text,
self.file.fontName(prop), fontsize, Op.selectfont)
prev_start_x = 0
Expand All @@ -2439,12 +2437,18 @@ 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.
glyph_to_font = font.get_glyph_to_font()
for start_x, glyph_idx in multibyte_glyphs:
self._draw_xobject_glyph(font, fontsize, glyph_idx, start_x, 0)
self._draw_xobject_glyph(glyph_to_font, fontsize, glyph_idx, start_x, 0)
self.file.output(Op.grestore)
# print("fine here")

def _draw_xobject_glyph(self, font, fontsize, glyph_idx, x, y):
def _draw_xobject_glyph(self, glyph_to_font, fontsize, glyph_idx, x, y):
"""Draw a multibyte character from a Type 3 font as an XObject."""
if glyph_idx not in glyph_to_font:
# ideally raise.
pass
font = glyph_to_font[glyph_idx]
symbol_name = font.get_glyph_name(glyph_idx)
name = self.file._get_xobject_symbol_name(font.fname, symbol_name)
self.file.output(
Expand Down
20 changes: 14 additions & 6 deletions src/ft2font.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,7 @@ FT2Font::~FT2Font()
}

if (face) {
printf("Deleting face from: %lu\n", face->num_glyphs);
FT_Done_Face(face);
}
}
Expand Down Expand Up @@ -555,11 +556,11 @@ void FT2Font::set_text(
if (bbox.xMin > bbox.xMax) {
bbox.xMin = bbox.yMin = bbox.xMax = bbox.yMax = 0;
}
printf("\nMap: \n");
// print out num_glyphs for the final FT2Font so its easy to track
for (std::pair<const FT_UInt, FT2Font *> &x: glyph_to_font) {
printf("%u: %lu \n", x.first, x.second->get_face()->num_glyphs);
}
// printf("\nMap: \n");
// // print out num_glyphs for the final FT2Font so its easy to track
// for (std::pair<const FT_UInt, FT2Font *> &x: glyph_to_font) {
// printf("%u: %lu \n", x.first, x.second->get_face()->num_glyphs);
// }
}

void FT2Font::fill_glyphs(
Expand Down Expand Up @@ -685,6 +686,7 @@ bool FT2Font::load_char_with_fallback(FT2Font *&ft_object_with_glyph,
bool override = false)
{
FT_UInt glyph_index = FT_Get_Char_Index(face, charcode);
// printf("fallback glyph id: %u\n", glyph_index);

if (glyph_index || override) {
charcode_error = FT_Load_Glyph(face, glyph_index, flags);
Expand Down Expand Up @@ -736,7 +738,8 @@ void FT2Font::load_glyph(FT_UInt glyph_index,
void FT2Font::load_glyph(FT_UInt glyph_index, FT_Int32 flags)
{
// search cache first
if (fallback == 1 && glyph_to_font.find(glyph_index) != glyph_to_font.end()) {
if (fallback && glyph_to_font.find(glyph_index) != glyph_to_font.end()) {
// printf("load_glyph: Already present in cache.\n");
ft_object = glyph_to_font[glyph_index];
return;
}
Expand Down Expand Up @@ -778,6 +781,11 @@ void FT2Font::get_cbox(FT_BBox &bbox)
FT_Glyph_Get_CBox(glyphs.back(), ft_glyph_bbox_subpixels, &bbox);
}

void FT2Font::get_cbox(FT_BBox &bbox)
{
FT_Glyph_Get_CBox(glyphs.back(), ft_glyph_bbox_subpixels, &bbox);
}

void FT2Font::get_width_height(long *width, long *height)
{
*width = advance;
Expand Down
12 changes: 8 additions & 4 deletions src/ft2font_wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -766,8 +766,7 @@ static PyObject *PyFT2Font_load_char(PyFT2Font *self, PyObject *args, PyObject *
long charcode;
int fallback = 1;
FT_Int32 flags = FT_LOAD_FORCE_AUTOHINT;
const char *names[] = { "charcode", "flags", "fallback", NULL };
printf("Loading char!\n");
const char *names[] = { "charcode", "flags", NULL };

/* This makes a technically incorrect assumption that FT_Int32 is
int. In theory it can also be long, if the size of int is less
Expand All @@ -776,9 +775,9 @@ static PyObject *PyFT2Font_load_char(PyFT2Font *self, PyObject *args, PyObject *
&flags)) {
return NULL;
}

FT2Font *ft_object = NULL;
CALL_CPP("load_char", (self->x->load_char(charcode, flags, fallback, ft_object)));
CALL_CPP("load_char", (self->x->load_char(charcode, flags, ft_object, (bool)fallback)));
printf("Char %ld loaded to: %lu\n", charcode, ft_object->get_face()->num_glyphs);

FT2Font *ft_object = NULL;
Expand Down Expand Up @@ -819,6 +818,11 @@ static PyObject *PyFT2Font_load_glyph(PyFT2Font *self, PyObject *args, PyObject
return NULL;
}

// if (!PyArg_ParseTupleAndKeywords(
// args, kwds, "I|i$p:load_glyph", (char **)names, &glyph_index, &flags, &fallback)) {
// return NULL;
// }

FT2Font *ft_object = NULL;
CALL_CPP("load_glyph", (self->x->load_glyph(glyph_index, flags, ft_object, (bool)fallback)));

Expand Down