Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 03792bd

Browse files
committed
Change RendererAgg.draw_text to use FT2Font._render_glyph
This makes it more similar to `draw_mathtext`, and is necessary for colour fonts, where the previously-cached bitmap would have been downgraded to black&white strokes by FreeType when copied. It should also reduce the necessary code paths with `draw_tex` as well in the future.
1 parent da4d754 commit 03792bd

2 files changed

Lines changed: 19 additions & 17 deletions

File tree

lib/matplotlib/backends/backend_agg.py

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -228,13 +228,25 @@ def draw_text(self, gc, x, y, s, prop, angle, ismath=False, mtext=None):
228228
if ismath:
229229
return self.draw_mathtext(gc, x, y, s, prop, angle)
230230
font = self._prepare_font(prop)
231-
font.set_text(s, angle, flags=get_hinting_flag(),
232-
features=mtext.get_fontfeatures() if mtext is not None else None,
233-
language=mtext.get_language() if mtext is not None else None)
234-
for bitmap in font._render_glyphs(
235-
x, self.height - y,
236-
RenderMode.NORMAL if gc.get_antialiased() else RenderMode.MONO,
237-
):
231+
cos = math.cos(math.radians(angle))
232+
sin = math.sin(math.radians(angle))
233+
load_flags = get_hinting_flag()
234+
items = font._layout(
235+
s, flags=load_flags,
236+
features=mtext.get_fontfeatures() if mtext is not None else None,
237+
language=mtext.get_language() if mtext is not None else None)
238+
for item in items:
239+
hf = item.ft_object._hinting_factor
240+
item.ft_object._set_transform(
241+
[[round(0x10000 * cos / hf), round(0x10000 * -sin)],
242+
[round(0x10000 * sin / hf), round(0x10000 * cos)]],
243+
[round(0x40 * (x + item.x * cos - item.y * sin)),
244+
# FreeType's y is upwards.
245+
round(0x40 * (self.height - y + item.x * sin + item.y * cos))]
246+
)
247+
bitmap = item.ft_object._render_glyph(
248+
item.glyph_index, load_flags,
249+
RenderMode.NORMAL if gc.get_antialiased() else RenderMode.MONO)
238250
buffer = bitmap.buffer
239251
if not gc.get_antialiased():
240252
buffer *= 0xff

src/ft2font_wrapper.cpp

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1916,16 +1916,6 @@ PYBIND11_MODULE(ft2font, m, py::mod_gil_not_used())
19161916
FT_CHECK(FT_Render_Glyph, face->glyph, render_mode);
19171917
return PyPositionedBitmap{face->glyph};
19181918
})
1919-
.def("_render_glyphs",
1920-
[](PyFT2Font *self, double x, double y, FT_Render_Mode render_mode) {
1921-
auto origin = FT_Vector{std::lround(x * 64), std::lround(y * 64)};
1922-
auto pbs = std::vector<PyPositionedBitmap>{};
1923-
for (auto &g: self->get_glyphs()) {
1924-
FT_CHECK(FT_Glyph_To_Bitmap, &g, render_mode, &origin, 1);
1925-
pbs.emplace_back(reinterpret_cast<FT_BitmapGlyph>(g));
1926-
}
1927-
return pbs;
1928-
})
19291919
;
19301920

19311921
m.attr("__freetype_version__") = version_string;

0 commit comments

Comments
 (0)