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

Skip to content

Commit 931bcf3

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 the other text drawing methods as well.
1 parent 65fbe95 commit 931bcf3

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
@@ -229,13 +229,25 @@ def draw_text(self, gc, x, y, s, prop, angle, ismath=False, mtext=None):
229229
if ismath:
230230
return self.draw_mathtext(gc, x, y, s, prop, angle)
231231
font = self._prepare_font(prop)
232-
font.set_text(s, angle, flags=get_hinting_flag(),
233-
features=mtext.get_fontfeatures() if mtext is not None else None,
234-
language=mtext.get_language() if mtext is not None else None)
235-
for bitmap in font._render_glyphs(
236-
x, self.height - y,
237-
RenderMode.NORMAL if gc.get_antialiased() else RenderMode.MONO,
238-
):
232+
cos = math.cos(math.radians(angle))
233+
sin = math.sin(math.radians(angle))
234+
load_flags = get_hinting_flag()
235+
items = font._layout(
236+
s, flags=load_flags,
237+
features=mtext.get_fontfeatures() if mtext is not None else None,
238+
language=mtext.get_language() if mtext is not None else None)
239+
for item in items:
240+
hf = item.ft_object._hinting_factor
241+
item.ft_object._set_transform(
242+
[[round(0x10000 * cos / hf), round(0x10000 * -sin)],
243+
[round(0x10000 * sin / hf), round(0x10000 * cos)]],
244+
[round(0x40 * (x + item.x * cos - item.y * sin)),
245+
# FreeType's y is upwards.
246+
round(0x40 * (self.height - y + item.x * sin + item.y * cos))]
247+
)
248+
bitmap = item.ft_object._render_glyph(
249+
item.glyph_index, load_flags,
250+
RenderMode.NORMAL if gc.get_antialiased() else RenderMode.MONO)
239251
buffer = bitmap.buffer
240252
if not gc.get_antialiased():
241253
buffer *= 0xff

src/ft2font_wrapper.cpp

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1933,16 +1933,6 @@ PYBIND11_MODULE(ft2font, m, py::mod_gil_not_used())
19331933
FT_CHECK(FT_Render_Glyph, face->glyph, render_mode);
19341934
return PyPositionedBitmap{face->glyph};
19351935
})
1936-
.def("_render_glyphs",
1937-
[](PyFT2Font *self, double x, double y, FT_Render_Mode render_mode) {
1938-
auto origin = FT_Vector{std::lround(x * 64), std::lround(y * 64)};
1939-
auto pbs = std::vector<PyPositionedBitmap>{};
1940-
for (auto &g: self->get_glyphs()) {
1941-
FT_CHECK(FT_Glyph_To_Bitmap, &g, render_mode, &origin, 1);
1942-
pbs.emplace_back(reinterpret_cast<FT_BitmapGlyph>(g));
1943-
}
1944-
return pbs;
1945-
})
19461936
;
19471937

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

0 commit comments

Comments
 (0)