Skip to content

Fix #347: Faster text rendering in Agg #5361

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 5, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions doc/users/whats_new/faster-text-rendering.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Faster text rendering
---------------------

Rendering text in the Agg backend is now less fuzzy and about 20%
faster to draw.
92 changes: 58 additions & 34 deletions src/_backend_agg.h
Original file line number Diff line number Diff line change
Expand Up @@ -734,41 +734,65 @@ inline void RendererAgg::draw_text_image(GCAgg &gc, ImageArray &image, int x, in
typedef agg::renderer_scanline_aa<renderer_base, color_span_alloc_type, span_gen_type>
renderer_type;

theRasterizer.reset_clipping();
rendererBase.reset_clipping(true);
set_clipbox(gc.cliprect, theRasterizer);
if (angle != 0.0) {
agg::rendering_buffer srcbuf(
image.data(), (unsigned)image.dim(1),
(unsigned)image.dim(0), (unsigned)image.dim(1));
agg::pixfmt_gray8 pixf_img(srcbuf);

theRasterizer.reset_clipping();
rendererBase.reset_clipping(true);
set_clipbox(gc.cliprect, theRasterizer);

agg::trans_affine mtx;
mtx *= agg::trans_affine_translation(0, -image.dim(0));
mtx *= agg::trans_affine_rotation(-angle * agg::pi / 180.0);
mtx *= agg::trans_affine_translation(x, y);

agg::rendering_buffer srcbuf(
image.data(), (unsigned)image.dim(1), (unsigned)image.dim(0), (unsigned)image.dim(1));
agg::pixfmt_gray8 pixf_img(srcbuf);

agg::trans_affine mtx;
mtx *= agg::trans_affine_translation(0, -image.dim(0));
mtx *= agg::trans_affine_rotation(-angle * agg::pi / 180.0);
mtx *= agg::trans_affine_translation(x, y);

agg::path_storage rect;
rect.move_to(0, 0);
rect.line_to(image.dim(1), 0);
rect.line_to(image.dim(1), image.dim(0));
rect.line_to(0, image.dim(0));
rect.line_to(0, 0);
agg::conv_transform<agg::path_storage> rect2(rect, mtx);

agg::trans_affine inv_mtx(mtx);
inv_mtx.invert();

agg::image_filter_lut filter;
filter.calculate(agg::image_filter_spline36());
interpolator_type interpolator(inv_mtx);
color_span_alloc_type sa;
image_accessor_type ia(pixf_img, agg::gray8(0));
image_span_gen_type image_span_generator(ia, interpolator, filter);
span_gen_type output_span_generator(&image_span_generator, gc.color);
renderer_type ri(rendererBase, sa, output_span_generator);

theRasterizer.add_path(rect2);
agg::render_scanlines(theRasterizer, slineP8, ri);
agg::path_storage rect;
rect.move_to(0, 0);
rect.line_to(image.dim(1), 0);
rect.line_to(image.dim(1), image.dim(0));
rect.line_to(0, image.dim(0));
rect.line_to(0, 0);
agg::conv_transform<agg::path_storage> rect2(rect, mtx);

agg::trans_affine inv_mtx(mtx);
inv_mtx.invert();

agg::image_filter_lut filter;
filter.calculate(agg::image_filter_spline36());
interpolator_type interpolator(inv_mtx);
color_span_alloc_type sa;
image_accessor_type ia(pixf_img, agg::gray8(0));
image_span_gen_type image_span_generator(ia, interpolator, filter);
span_gen_type output_span_generator(&image_span_generator, gc.color);
renderer_type ri(rendererBase, sa, output_span_generator);

theRasterizer.add_path(rect2);
agg::render_scanlines(theRasterizer, slineP8, ri);
} else {
agg::rect_i fig, text;

fig.init(0, 0, width, height);
text.init(x, y - image.dim(0), x + image.dim(1), y);
text.clip(fig);

if (gc.cliprect.x1 != 0.0 || gc.cliprect.y1 != 0.0 || gc.cliprect.x2 != 0.0 || gc.cliprect.y2 != 0.0) {
agg::rect_i clip;

clip.init(int(mpl_round(gc.cliprect.x1)),
int(mpl_round(gc.cliprect.y1)),
int(mpl_round(gc.cliprect.x2)),
int(mpl_round(gc.cliprect.y2)));
text.clip(clip);
}

for (int yi = text.y1; yi < text.y2; ++yi) {
pixFmt.blend_solid_hspan(text.x1, yi, (text.x2 - text.x1), gc.color,
&image(yi - (y - image.dim(0)), text.x1 - x));
}
}
}

class span_conv_alpha
Expand Down