Skip to content

Commit 8ad0be4

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 rendering of mathtext boxes (e.g., fraction bars) is currently missing, but should be "easy". Likewise, all baseline images should be regenerated. Finally, the new APIs added to FT2Font are likely up to bikeshedding (but they are all private).
1 parent de00c49 commit 8ad0be4

File tree

3 files changed

+81
-29
lines changed

3 files changed

+81
-29
lines changed

lib/matplotlib/backends/backend_agg.py

Lines changed: 26 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,35 @@ 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+
parse = self.mathtext_parser.parse(
176+
s, self.dpi, prop, antialiased=gc.get_antialiased())
177+
c = cos(radians(angle))
178+
s = sin(radians(angle))
179+
for font, size, char, dx, dy in parse.glyphs:
180+
font.set_size(size, self.dpi)
181+
bitmap = font._render_glyph(
182+
font.get_char_index(char),
183+
x + dx * c - dy * s, self.height - y + dx * s + dy * c, angle,
184+
get_hinting_flag())
185+
self._renderer.draw_text_image(
186+
bitmap["buffer"],
187+
bitmap["left"],
188+
int(self.height) - bitmap["top"] + bitmap["buffer"].shape[0],
189+
0, gc)
190+
# TODO: Also draw rects (fraction bars, etc.) with draw_path().
184191

185192
def draw_text(self, gc, x, y, s, prop, angle, ismath=False, mtext=None):
186193
# docstring inherited
187194
if ismath:
188195
return self.draw_mathtext(gc, x, y, s, prop, angle)
189196
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)
197+
font.set_text(s, angle, flags=get_hinting_flag())
198+
for bitmap in font._render_glyphs(x, self.height - y):
199+
self._renderer.draw_text_image(
200+
bitmap["buffer"],
201+
bitmap["left"],
202+
int(self.height) - bitmap["top"] + bitmap["buffer"].shape[0],
203+
0, gc)
205204

206205
def get_text_width_height_descent(self, s, prop, ismath):
207206
# docstring inherited
@@ -211,9 +210,8 @@ def get_text_width_height_descent(self, s, prop, ismath):
211210
return super().get_text_width_height_descent(s, prop, ismath)
212211

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

218216
font = self._prepare_font(prop)
219217
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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy