Skip to content

Commit c457f01

Browse files
committed
Merge pull request #5361 from mdboom/fast-text
Fix #347: Faster text rendering in Agg
2 parents 18beeb9 + 83a1025 commit c457f01

File tree

2 files changed

+63
-34
lines changed

2 files changed

+63
-34
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Faster text rendering
2+
---------------------
3+
4+
Rendering text in the Agg backend is now less fuzzy and about 20%
5+
faster to draw.

src/_backend_agg.h

+58-34
Original file line numberDiff line numberDiff line change
@@ -734,41 +734,65 @@ inline void RendererAgg::draw_text_image(GCAgg &gc, ImageArray &image, int x, in
734734
typedef agg::renderer_scanline_aa<renderer_base, color_span_alloc_type, span_gen_type>
735735
renderer_type;
736736

737-
theRasterizer.reset_clipping();
738-
rendererBase.reset_clipping(true);
739-
set_clipbox(gc.cliprect, theRasterizer);
737+
if (angle != 0.0) {
738+
agg::rendering_buffer srcbuf(
739+
image.data(), (unsigned)image.dim(1),
740+
(unsigned)image.dim(0), (unsigned)image.dim(1));
741+
agg::pixfmt_gray8 pixf_img(srcbuf);
742+
743+
theRasterizer.reset_clipping();
744+
rendererBase.reset_clipping(true);
745+
set_clipbox(gc.cliprect, theRasterizer);
746+
747+
agg::trans_affine mtx;
748+
mtx *= agg::trans_affine_translation(0, -image.dim(0));
749+
mtx *= agg::trans_affine_rotation(-angle * agg::pi / 180.0);
750+
mtx *= agg::trans_affine_translation(x, y);
740751

741-
agg::rendering_buffer srcbuf(
742-
image.data(), (unsigned)image.dim(1), (unsigned)image.dim(0), (unsigned)image.dim(1));
743-
agg::pixfmt_gray8 pixf_img(srcbuf);
744-
745-
agg::trans_affine mtx;
746-
mtx *= agg::trans_affine_translation(0, -image.dim(0));
747-
mtx *= agg::trans_affine_rotation(-angle * agg::pi / 180.0);
748-
mtx *= agg::trans_affine_translation(x, y);
749-
750-
agg::path_storage rect;
751-
rect.move_to(0, 0);
752-
rect.line_to(image.dim(1), 0);
753-
rect.line_to(image.dim(1), image.dim(0));
754-
rect.line_to(0, image.dim(0));
755-
rect.line_to(0, 0);
756-
agg::conv_transform<agg::path_storage> rect2(rect, mtx);
757-
758-
agg::trans_affine inv_mtx(mtx);
759-
inv_mtx.invert();
760-
761-
agg::image_filter_lut filter;
762-
filter.calculate(agg::image_filter_spline36());
763-
interpolator_type interpolator(inv_mtx);
764-
color_span_alloc_type sa;
765-
image_accessor_type ia(pixf_img, agg::gray8(0));
766-
image_span_gen_type image_span_generator(ia, interpolator, filter);
767-
span_gen_type output_span_generator(&image_span_generator, gc.color);
768-
renderer_type ri(rendererBase, sa, output_span_generator);
769-
770-
theRasterizer.add_path(rect2);
771-
agg::render_scanlines(theRasterizer, slineP8, ri);
752+
agg::path_storage rect;
753+
rect.move_to(0, 0);
754+
rect.line_to(image.dim(1), 0);
755+
rect.line_to(image.dim(1), image.dim(0));
756+
rect.line_to(0, image.dim(0));
757+
rect.line_to(0, 0);
758+
agg::conv_transform<agg::path_storage> rect2(rect, mtx);
759+
760+
agg::trans_affine inv_mtx(mtx);
761+
inv_mtx.invert();
762+
763+
agg::image_filter_lut filter;
764+
filter.calculate(agg::image_filter_spline36());
765+
interpolator_type interpolator(inv_mtx);
766+
color_span_alloc_type sa;
767+
image_accessor_type ia(pixf_img, agg::gray8(0));
768+
image_span_gen_type image_span_generator(ia, interpolator, filter);
769+
span_gen_type output_span_generator(&image_span_generator, gc.color);
770+
renderer_type ri(rendererBase, sa, output_span_generator);
771+
772+
theRasterizer.add_path(rect2);
773+
agg::render_scanlines(theRasterizer, slineP8, ri);
774+
} else {
775+
agg::rect_i fig, text;
776+
777+
fig.init(0, 0, width, height);
778+
text.init(x, y - image.dim(0), x + image.dim(1), y);
779+
text.clip(fig);
780+
781+
if (gc.cliprect.x1 != 0.0 || gc.cliprect.y1 != 0.0 || gc.cliprect.x2 != 0.0 || gc.cliprect.y2 != 0.0) {
782+
agg::rect_i clip;
783+
784+
clip.init(int(mpl_round(gc.cliprect.x1)),
785+
int(mpl_round(gc.cliprect.y1)),
786+
int(mpl_round(gc.cliprect.x2)),
787+
int(mpl_round(gc.cliprect.y2)));
788+
text.clip(clip);
789+
}
790+
791+
for (int yi = text.y1; yi < text.y2; ++yi) {
792+
pixFmt.blend_solid_hspan(text.x1, yi, (text.x2 - text.x1), gc.color,
793+
&image(yi - (y - image.dim(0)), text.x1 - x));
794+
}
795+
}
772796
}
773797

774798
class span_conv_alpha

0 commit comments

Comments
 (0)