From edb103716a81f0b9249c60e2ec5b8ce92799ee15 Mon Sep 17 00:00:00 2001 From: Francesco Abbate Date: Mon, 1 Jun 2020 11:25:22 +0200 Subject: [PATCH] Add function to render a single unicode codepoint --- src/font_renderer_alpha.h | 60 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/src/font_renderer_alpha.h b/src/font_renderer_alpha.h index f596bbe2..a5a74091 100644 --- a/src/font_renderer_alpha.h +++ b/src/font_renderer_alpha.h @@ -137,6 +137,47 @@ public: x += x_delta / scale_x; } + template + void draw_codepoint(Rasterizer& ras, Scanline& sl, + RenSolid& ren_solid, const color_type color, + int codepoint, double& x, double& y, double height) + { + const double scale_x = 100; + + m_feng.height(height); + m_feng.width(height * scale_x); + m_feng.hinting(m_hinting); + + // Represent the delta in x scaled by scale_x. + double x_delta = 0; + double start_x = x; + + const agg::glyph_cache* glyph = m_fman.glyph(codepoint); + if(glyph) + { + if(m_kerning) + { + m_fman.add_kerning(&x_delta, &y); + } + + m_fman.init_embedded_adaptors(glyph, 0, 0); + if(glyph->data_type == agg::glyph_data_outline) + { + double ty = m_hinting ? floor(y + 0.5) : y; + ras.reset(); + m_mtx.reset(); + m_mtx *= agg::trans_affine_scaling(1.0 / scale_x, 1); + m_mtx *= agg::trans_affine_translation(start_x + x_delta / scale_x, ty); + ras.add_path(m_trans); + ren_solid.color(color); + agg::render_scanlines(ras, sl, ren_solid); + } + + y += glyph->advance_y; + x += (x_delta + glyph->advance_x) / scale_x; + } + } + void clear(agg::rendering_buffer& ren_buf, const color_type color) { pixfmt_type pf(ren_buf); base_ren_type ren_base(pf); @@ -161,4 +202,23 @@ public: renderer_solid ren_solid(ren_base); draw_text(ras, sl, ren_solid, text_color, text, x, y, text_size); } + + void render_codepoint(agg::rendering_buffer& ren_buf, + const double text_size, + const color_type text_color, + double& x, double& y, + int codepoint) + { + if (!m_font_loaded) { + return; + } + agg::scanline_u8 sl; + agg::rasterizer_scanline_aa<> ras; + ras.clip_box(0, 0, ren_buf.width(), ren_buf.height()); + + agg::pixfmt_alpha8 pf(ren_buf); + base_ren_type ren_base(pf); + renderer_solid ren_solid(ren_base); + draw_codepoint(ras, sl, ren_solid, text_color, codepoint, x, y, text_size); + } };