Skip to content

Commit 66fdae0

Browse files
committed
Drop the FT2Font intermediate buffer.
Directly render FT glyphs to the Agg buffer. In particular, this naturally provides, with no extra work, subpixel positioning of glyphs (which could also have been implemented in the old framework, but would have required careful tracking of subpixel offets). Note that all baseline images should be regenerated. The new APIs added to FT2Font are also up to bikeshedding (but they are all private).
1 parent de00c49 commit 66fdae0

File tree

3 files changed

+104
-29
lines changed

3 files changed

+104
-29
lines changed

lib/matplotlib/backends/backend_agg.py

Lines changed: 49 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ def __init__(self, width, height, dpi):
7070
self._filter_renderers = []
7171

7272
self._update_methods()
73-
self.mathtext_parser = MathTextParser('agg')
73+
self.mathtext_parser = MathTextParser('path')
7474

7575
self.bbox = Bbox.from_bounds(0, 0, self.width, self.height)
7676

@@ -172,36 +172,58 @@ def draw_path(self, gc, path, transform, rgbFace=None):
172172

173173
def draw_mathtext(self, gc, x, y, s, prop, angle):
174174
"""Draw mathtext using :mod:`matplotlib.mathtext`."""
175-
ox, oy, width, height, descent, font_image = \
176-
self.mathtext_parser.parse(s, self.dpi, prop,
177-
antialiased=gc.get_antialiased())
178-
179-
xd = descent * sin(radians(angle))
180-
yd = descent * cos(radians(angle))
181-
x = round(x + ox + xd)
182-
y = round(y - oy + yd)
183-
self._renderer.draw_text_image(font_image, x, y + 1, angle, gc)
175+
# y is downwards.
176+
parse = self.mathtext_parser.parse(
177+
s, self.dpi, prop, antialiased=gc.get_antialiased())
178+
c = cos(radians(angle))
179+
s = sin(radians(angle))
180+
for font, size, char, dx, dy in parse.glyphs: # dy is upwards.
181+
font.set_size(size, self.dpi)
182+
bitmap = font._render_glyph(
183+
font.get_char_index(char),
184+
# The "y" parameter is upwards (per FreeType).
185+
x + dx * c - dy * s, self.height - y + dx * s + dy * c, angle,
186+
get_hinting_flag())
187+
# draw_text_image's y is downwards & the bitmap bottom side.
188+
self._renderer.draw_text_image(
189+
bitmap["buffer"],
190+
bitmap["left"],
191+
int(self.height) - bitmap["top"] + bitmap["buffer"].shape[0],
192+
0, gc)
193+
if not angle:
194+
for dx, dy, w, h in parse.rects: # dy is upwards & the rect top side.
195+
self._renderer.draw_text_image(
196+
np.full((round(h), round(w)), np.uint8(0xff)),
197+
round(x + dx), round(y - dy - h),
198+
0, gc)
199+
else:
200+
rgba = gc.get_rgb()
201+
if len(rgba) == 3 or gc.get_forced_alpha():
202+
rgba = rgba[:3] + (gc.get_alpha(),)
203+
gc1 = self.new_gc()
204+
gc1.set_linewidth(0)
205+
for dx, dy, w, h in parse.rects: # dy is upwards & the rect top side.
206+
path = Path._create_closed(
207+
[(dx, dy), (dx + w, dy), (dx + w, dy + h), (dx, dy + h)])
208+
self._renderer.draw_path(
209+
gc1, path,
210+
mpl.transforms.Affine2D()
211+
.rotate_deg(angle).translate(x, self.height - y),
212+
rgba)
213+
gc1.restore()
184214

185215
def draw_text(self, gc, x, y, s, prop, angle, ismath=False, mtext=None):
186216
# docstring inherited
187217
if ismath:
188218
return self.draw_mathtext(gc, x, y, s, prop, angle)
189219
font = self._prepare_font(prop)
190-
# We pass '0' for angle here, since it will be rotated (in raster
191-
# space) in the following call to draw_text_image).
192-
font.set_text(s, 0, flags=get_hinting_flag())
193-
font.draw_glyphs_to_bitmap(
194-
antialiased=gc.get_antialiased())
195-
d = font.get_descent() / 64.0
196-
# The descent needs to be adjusted for the angle.
197-
xo, yo = font.get_bitmap_offset()
198-
xo /= 64.0
199-
yo /= 64.0
200-
xd = d * sin(radians(angle))
201-
yd = d * cos(radians(angle))
202-
x = round(x + xo + xd)
203-
y = round(y + yo + yd)
204-
self._renderer.draw_text_image(font, x, y + 1, angle, gc)
220+
font.set_text(s, angle, flags=get_hinting_flag())
221+
for bitmap in font._render_glyphs(x, self.height - y):
222+
self._renderer.draw_text_image(
223+
bitmap["buffer"],
224+
bitmap["left"],
225+
int(self.height) - bitmap["top"] + bitmap["buffer"].shape[0],
226+
0, gc)
205227

206228
def get_text_width_height_descent(self, s, prop, ismath):
207229
# docstring inherited
@@ -211,9 +233,8 @@ def get_text_width_height_descent(self, s, prop, ismath):
211233
return super().get_text_width_height_descent(s, prop, ismath)
212234

213235
if ismath:
214-
ox, oy, width, height, descent, font_image = \
215-
self.mathtext_parser.parse(s, self.dpi, prop)
216-
return width, height, descent
236+
parse = self.mathtext_parser.parse(s, self.dpi, prop)
237+
return parse.width, parse.height, parse.depth
217238

218239
font = self._prepare_font(prop)
219240
font.set_text(s, 0.0, flags=get_hinting_flag())

src/ft2font.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,9 @@ class FT2Font
144144
FT2Image image;
145145
FT_Face face;
146146
FT_Vector pen; /* untransformed origin */
147+
public:
147148
std::vector<FT_Glyph> glyphs;
149+
private:
148150
std::vector<FT2Font *> fallbacks;
149151
std::unordered_map<FT_UInt, FT2Font *> glyph_to_font;
150152
std::unordered_map<long, FT2Font *> char_to_font;

src/ft2font_wrapper.cpp

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1764,7 +1764,59 @@ PYBIND11_MODULE(ft2font, m, py::mod_gil_not_used())
17641764
std::vector<py::size_t> shape { im.get_height(), im.get_width() };
17651765
std::vector<py::size_t> strides { im.get_width(), 1 };
17661766
return py::buffer_info(im.get_buffer(), shape, strides);
1767-
});
1767+
})
1768+
1769+
// TODO: Return a nicer structure than dicts.
1770+
// NOTE: The lifetime of the buffers is limited and could get invalidated...
1771+
// TODO: Real antialiasing flag.
1772+
// TODO: throw_ft_error.
1773+
// x, y are upwards here
1774+
.def("_render_glyph", [](PyFT2Font *self, FT_UInt idx,
1775+
double x, double y, double angle,
1776+
LoadFlags flags) {
1777+
auto face = self->x->get_face();
1778+
auto hf = self->x->get_hinting_factor();
1779+
auto c = std::cos(angle * M_PI / 180) * 0x10000L,
1780+
s = std::sin(angle * M_PI / 180) * 0x10000L;
1781+
auto matrix = FT_Matrix{
1782+
std::lround(c / hf), std::lround(-s), std::lround(s / hf), std::lround(c)};
1783+
auto delta = FT_Vector{std::lround(x * 64), std::lround(y * 64)};
1784+
FT_Set_Transform(face, &matrix, &delta);
1785+
if (auto error = FT_Load_Glyph(face, idx, static_cast<FT_Int32>(flags))) {
1786+
throw std::runtime_error("Could not load glyph");
1787+
}
1788+
if (auto error = FT_Render_Glyph(face->glyph, FT_RENDER_MODE_NORMAL)) {
1789+
throw std::runtime_error("Could not convert glyph to bitmap");
1790+
}
1791+
py::dict d;
1792+
d["left"] = face->glyph->bitmap_left;
1793+
d["top"] = face->glyph->bitmap_top;
1794+
d["buffer"] = py::array_t<uint8_t>{
1795+
{face->glyph->bitmap.rows, face->glyph->bitmap.width},
1796+
{face->glyph->bitmap.pitch, 1},
1797+
face->glyph->bitmap.buffer};
1798+
return d;
1799+
})
1800+
.def("_render_glyphs", [](PyFT2Font *self, double x, double y) {
1801+
auto origin = FT_Vector{std::lround(x * 64), std::lround(y * 64)};
1802+
py::list gs;
1803+
for (auto &g: self->x->glyphs) {
1804+
if (auto error = FT_Glyph_To_Bitmap(&g, FT_RENDER_MODE_NORMAL, &origin, 1)) {
1805+
throw std::runtime_error("Could not convert glyph to bitmap");
1806+
}
1807+
auto bg = reinterpret_cast<FT_BitmapGlyph>(g);
1808+
py::dict d;
1809+
d["left"] = bg->left;
1810+
d["top"] = bg->top;
1811+
d["buffer"] = py::array_t<uint8_t>{
1812+
{bg->bitmap.rows, bg->bitmap.width},
1813+
{bg->bitmap.pitch, 1},
1814+
bg->bitmap.buffer};
1815+
gs.append(d);
1816+
}
1817+
return gs;
1818+
})
1819+
;
17681820

17691821
m.attr("__freetype_version__") = version_string;
17701822
m.attr("__freetype_build_type__") = FREETYPE_BUILD_TYPE;

0 commit comments

Comments
 (0)