Skip to content

Commit 152e27f

Browse files
committed
Merge branch 'colour-font' into libraqm-full
2 parents 72a7c33 + 20f3e15 commit 152e27f

File tree

2 files changed

+43
-15
lines changed

2 files changed

+43
-15
lines changed

lib/matplotlib/backends/backend_agg.py

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ def draw_mathtext(self, gc, x, y, s, prop, angle):
184184
font.get_char_index(char),
185185
# The "y" parameter is upwards (per FreeType).
186186
x + dx * c - dy * s, self.height - y + dx * s + dy * c, angle,
187-
get_hinting_flag())
187+
get_hinting_flag() | LoadFlags.COLOR)
188188
# draw_text_image's y is downwards & the bitmap bottom side.
189189
self._renderer.draw_text_image(
190190
bitmap["buffer"],
@@ -218,15 +218,28 @@ def draw_text(self, gc, x, y, s, prop, angle, ismath=False, mtext=None):
218218
if ismath:
219219
return self.draw_mathtext(gc, x, y, s, prop, angle)
220220
font = self._prepare_font(prop)
221-
font.set_text(s, 0, flags=get_hinting_flag(),
221+
font.set_text(s, 0, flags=get_hinting_flag() | LoadFlags.COLOR,
222222
features=mtext.get_fontfeatures() if mtext is not None else None,
223223
language=mtext.get_language() if mtext is not None else None)
224224
for bitmap in font._render_glyphs(x, self.height - y):
225-
self._renderer.draw_text_image(
226-
bitmap["buffer"],
227-
bitmap["left"],
228-
int(self.height) - bitmap["top"] + bitmap["buffer"].shape[0],
229-
0, gc)
225+
# glyph_index = font.get_char_index(ord(c))
226+
# glyph = font.load_glyph(glyph_index)
227+
# bitmap = font._render_glyph(
228+
# glyph_index, x, self.height - y, angle,
229+
# get_hinting_flag() | LoadFlags.COLOR)
230+
if bitmap["buffer"].ndim == 3:
231+
self._renderer.draw_image(
232+
gc,
233+
bitmap["left"],
234+
bitmap["top"] - bitmap["buffer"].shape[0],
235+
bitmap["buffer"][::-1, :, [2, 1, 0, 3]])
236+
else:
237+
self._renderer.draw_text_image(
238+
bitmap["buffer"],
239+
bitmap["left"],
240+
int(self.height) - bitmap["top"] + bitmap["buffer"].shape[0],
241+
0, gc)
242+
# x += glyph.horiAdvance / 64
230243

231244
def get_text_width_height_descent(self, s, prop, ismath):
232245
# docstring inherited

src/ft2font_wrapper.cpp

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1871,10 +1871,18 @@ PYBIND11_MODULE(ft2font, m, py::mod_gil_not_used())
18711871
py::dict d;
18721872
d["left"] = face->glyph->bitmap_left;
18731873
d["top"] = face->glyph->bitmap_top;
1874-
d["buffer"] = py::array_t<uint8_t>{
1875-
{face->glyph->bitmap.rows, face->glyph->bitmap.width},
1876-
{face->glyph->bitmap.pitch, 1},
1877-
face->glyph->bitmap.buffer};
1874+
if (face->glyph->bitmap.pixel_mode == FT_PIXEL_MODE_BGRA) {
1875+
d["buffer"] = py::array_t<uint8_t>{
1876+
py::array::ShapeContainer({face->glyph->bitmap.rows,
1877+
face->glyph->bitmap.width, 4}),
1878+
py::array::StridesContainer({face->glyph->bitmap.pitch, 4, 1}),
1879+
face->glyph->bitmap.buffer};
1880+
} else {
1881+
d["buffer"] = py::array_t<uint8_t>{
1882+
{face->glyph->bitmap.rows, face->glyph->bitmap.width},
1883+
{face->glyph->bitmap.pitch, 1},
1884+
face->glyph->bitmap.buffer};
1885+
}
18781886
return d;
18791887
})
18801888
.def("_render_glyphs", [](PyFT2Font *self, double x, double y) {
@@ -1886,10 +1894,17 @@ PYBIND11_MODULE(ft2font, m, py::mod_gil_not_used())
18861894
py::dict d;
18871895
d["left"] = bg->left;
18881896
d["top"] = bg->top;
1889-
d["buffer"] = py::array_t<uint8_t>{
1890-
{bg->bitmap.rows, bg->bitmap.width},
1891-
{bg->bitmap.pitch, 1},
1892-
bg->bitmap.buffer};
1897+
if (bg->bitmap.pixel_mode == FT_PIXEL_MODE_BGRA) {
1898+
d["buffer"] = py::array_t<uint8_t>{
1899+
py::array::ShapeContainer({bg->bitmap.rows, bg->bitmap.width, 4}),
1900+
py::array::StridesContainer({bg->bitmap.pitch, 4, 1}),
1901+
bg->bitmap.buffer};
1902+
} else {
1903+
d["buffer"] = py::array_t<uint8_t>{
1904+
{bg->bitmap.rows, bg->bitmap.width},
1905+
{bg->bitmap.pitch, 1},
1906+
bg->bitmap.buffer};
1907+
}
18931908
gs.append(d);
18941909
}
18951910
return gs;

0 commit comments

Comments
 (0)