Skip to content

Commit 4942861

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 2918b73 commit 4942861

File tree

3 files changed

+98
-29
lines changed

3 files changed

+98
-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
@@ -174,7 +174,9 @@ class FT2Font
174174
py::array_t<uint8_t, py::array::c_style> image;
175175
FT_Face face;
176176
FT_Vector pen; /* untransformed origin */
177+
public:
177178
std::vector<FT_Glyph> glyphs;
179+
private:
178180
std::vector<FT2Font *> fallbacks;
179181
std::unordered_map<FT_UInt, FT2Font *> glyph_to_font;
180182
std::unordered_map<long, FT2Font *> char_to_font;

src/ft2font_wrapper.cpp

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1772,7 +1772,53 @@ PYBIND11_MODULE(ft2font, m, py::mod_gil_not_used())
17721772

17731773
.def_buffer([](PyFT2Font &self) -> py::buffer_info {
17741774
return self.x->get_image().request();
1775-
});
1775+
})
1776+
1777+
// TODO: Return a nicer structure than dicts.
1778+
// NOTE: The lifetime of the buffers is limited and could get invalidated...
1779+
// TODO: Real antialiasing flag.
1780+
// x, y are upwards here
1781+
.def("_render_glyph", [](PyFT2Font *self, FT_UInt idx,
1782+
double x, double y, double angle,
1783+
LoadFlags flags) {
1784+
auto face = self->x->get_face();
1785+
auto hf = self->x->get_hinting_factor();
1786+
auto c = std::cos(angle * M_PI / 180) * 0x10000L,
1787+
s = std::sin(angle * M_PI / 180) * 0x10000L;
1788+
auto matrix = FT_Matrix{
1789+
std::lround(c / hf), std::lround(-s),
1790+
std::lround(s / hf), std::lround(c)};
1791+
auto delta = FT_Vector{std::lround(x * 64), std::lround(y * 64)};
1792+
FT_Set_Transform(face, &matrix, &delta);
1793+
FT_CHECK(FT_Load_Glyph, face, idx, static_cast<FT_Int32>(flags));
1794+
FT_CHECK(FT_Render_Glyph, face->glyph, FT_RENDER_MODE_NORMAL);
1795+
py::dict d;
1796+
d["left"] = face->glyph->bitmap_left;
1797+
d["top"] = face->glyph->bitmap_top;
1798+
d["buffer"] = py::array_t<uint8_t>{
1799+
{face->glyph->bitmap.rows, face->glyph->bitmap.width},
1800+
{face->glyph->bitmap.pitch, 1},
1801+
face->glyph->bitmap.buffer};
1802+
return d;
1803+
})
1804+
.def("_render_glyphs", [](PyFT2Font *self, double x, double y) {
1805+
auto origin = FT_Vector{std::lround(x * 64), std::lround(y * 64)};
1806+
py::list gs;
1807+
for (auto &g: self->x->glyphs) {
1808+
FT_CHECK(FT_Glyph_To_Bitmap, &g, FT_RENDER_MODE_NORMAL, &origin, 1);
1809+
auto bg = reinterpret_cast<FT_BitmapGlyph>(g);
1810+
py::dict d;
1811+
d["left"] = bg->left;
1812+
d["top"] = bg->top;
1813+
d["buffer"] = py::array_t<uint8_t>{
1814+
{bg->bitmap.rows, bg->bitmap.width},
1815+
{bg->bitmap.pitch, 1},
1816+
bg->bitmap.buffer};
1817+
gs.append(d);
1818+
}
1819+
return gs;
1820+
})
1821+
;
17761822

17771823
m.attr("__freetype_version__") = version_string;
17781824
m.attr("__freetype_build_type__") = FREETYPE_BUILD_TYPE;

0 commit comments

Comments
 (0)