Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commite980d97

Browse files
committed
Drop the FT2Font intermediate buffer.
Directly render FT glyphs to the Agg buffer. In particular, thisnaturally provides, with no extra work, subpixel positioning of glyphs(which could also have been implemented in the old framework, but wouldhave required careful tracking of subpixel offets).Note that all baseline images should be regenerated. The new APIs addedto FT2Font are also up to bikeshedding (but they are all private).
1 parentdef8fa4 commite980d97

File tree

3 files changed

+104
-29
lines changed

3 files changed

+104
-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
defdraw_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+
forfont,size,char,dx,dyinparse.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+
ifnotangle:
194+
fordx,dy,w,hinparse.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+
iflen(rgba)==3orgc.get_forced_alpha():
202+
rgba=rgba[:3]+ (gc.get_alpha(),)
203+
gc1=self.new_gc()
204+
gc1.set_linewidth(0)
205+
fordx,dy,w,hinparse.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
defdraw_text(self,gc,x,y,s,prop,angle,ismath=False,mtext=None):
186216
# docstring inherited
187217
ifismath:
188218
returnself.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+
forbitmapinfont._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
defget_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
returnsuper().get_text_width_height_descent(s,prop,ismath)
212234

213235
ifismath:
214-
ox,oy,width,height,descent,font_image= \
215-
self.mathtext_parser.parse(s,self.dpi,prop)
216-
returnwidth,height,descent
236+
parse=self.mathtext_parser.parse(s,self.dpi,prop)
237+
returnparse.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
@@ -149,7 +149,9 @@ class FT2Font
149149
py::array_t<uint8_t, py::array::c_style> image;
150150
FT_Face face;
151151
FT_Vector pen;/* untransformed origin*/
152+
public:
152153
std::vector<FT_Glyph> glyphs;
154+
private:
153155
std::vector<FT2Font *> fallbacks;
154156
std::unordered_map<FT_UInt, FT2Font *> glyph_to_font;
155157
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
@@ -1772,7 +1772,59 @@ 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+
// TODO: throw_ft_error.
1781+
// x, y are upwards here
1782+
.def("_render_glyph", [](PyFT2Font *self, FT_UInt idx,
1783+
double x,double y,double angle,
1784+
LoadFlags flags) {
1785+
auto face = self->x->get_face();
1786+
auto hf = self->x->get_hinting_factor();
1787+
auto c =std::cos(angle * M_PI /180) *0x10000L,
1788+
s =std::sin(angle * M_PI /180) *0x10000L;
1789+
auto matrix = FT_Matrix{
1790+
std::lround(c / hf),std::lround(-s),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+
if (auto error =FT_Load_Glyph(face, idx,static_cast<FT_Int32>(flags))) {
1794+
throwstd::runtime_error("Could not load glyph");
1795+
}
1796+
if (auto error =FT_Render_Glyph(face->glyph, FT_RENDER_MODE_NORMAL)) {
1797+
throwstd::runtime_error("Could not convert glyph to bitmap");
1798+
}
1799+
py::dict d;
1800+
d["left"] = face->glyph->bitmap_left;
1801+
d["top"] = face->glyph->bitmap_top;
1802+
d["buffer"] = py::array_t<uint8_t>{
1803+
{face->glyph->bitmap.rows, face->glyph->bitmap.width},
1804+
{face->glyph->bitmap.pitch,1},
1805+
face->glyph->bitmap.buffer};
1806+
return d;
1807+
})
1808+
.def("_render_glyphs", [](PyFT2Font *self,double x,double y) {
1809+
auto origin = FT_Vector{std::lround(x *64),std::lround(y *64)};
1810+
py::list gs;
1811+
for (auto &g: self->x->glyphs) {
1812+
if (auto error =FT_Glyph_To_Bitmap(&g, FT_RENDER_MODE_NORMAL, &origin,1)) {
1813+
throwstd::runtime_error("Could not convert glyph to bitmap");
1814+
}
1815+
auto bg =reinterpret_cast<FT_BitmapGlyph>(g);
1816+
py::dict d;
1817+
d["left"] = bg->left;
1818+
d["top"] = bg->top;
1819+
d["buffer"] = py::array_t<uint8_t>{
1820+
{bg->bitmap.rows, bg->bitmap.width},
1821+
{bg->bitmap.pitch,1},
1822+
bg->bitmap.buffer};
1823+
gs.append(d);
1824+
}
1825+
return gs;
1826+
})
1827+
;
17761828

17771829
m.attr("__freetype_version__") = version_string;
17781830
m.attr("__freetype_build_type__") = FREETYPE_BUILD_TYPE;

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp