Move font resize in a separate function

This commit is contained in:
Francesco Abbate 2020-06-06 19:05:45 +02:00
parent 8e796831f2
commit 81289d651e
4 changed files with 25 additions and 13 deletions

View File

@ -14,8 +14,8 @@ public:
// Conventional LUT values: (1./3., 2./9., 1./9.) // Conventional LUT values: (1./3., 2./9., 1./9.)
// The values below are fine tuned as in the Elementary Plot library. // The values below are fine tuned as in the Elementary Plot library.
FontRendererImpl(bool hinting, bool kerning, float gamma_value) : FontRendererImpl(bool hinting, bool kerning, bool subpixel, float gamma_value) :
m_renderer(hinting, kerning), m_renderer(hinting, kerning, subpixel),
m_gamma_lut(double(gamma_value)), m_gamma_lut(double(gamma_value)),
m_blender(), m_blender(),
m_lcd_lut(0.448, 0.184, 0.092) m_lcd_lut(0.448, 0.184, 0.092)
@ -38,7 +38,8 @@ private:
FontRenderer *FontRendererNew(unsigned int flags, float gamma) { FontRenderer *FontRendererNew(unsigned int flags, float gamma) {
bool hinting = ((flags & FONT_RENDERER_HINTING) != 0); bool hinting = ((flags & FONT_RENDERER_HINTING) != 0);
bool kerning = ((flags & FONT_RENDERER_KERNING) != 0); bool kerning = ((flags & FONT_RENDERER_KERNING) != 0);
return new FontRendererImpl(hinting, kerning, gamma); bool subpixel = ((flags & FONT_RENDERER_SUBPIXEL) != 0);
return new FontRendererImpl(hinting, kerning, subpixel, gamma);
} }
void FontRendererFree(FontRenderer *font_renderer) { void FontRendererFree(FontRenderer *font_renderer) {
@ -180,6 +181,7 @@ int FontRendererBakeFontBitmap(FontRenderer *font_renderer, int font_height,
#else #else
const int font_height_reduced = font_height; const int font_height_reduced = font_height;
#endif #endif
renderer_alpha.set_font_height(font_height_reduced);
agg::int8u *cover_swap_buffer = new agg::int8u[pixels_width * subpixel_scale]; agg::int8u *cover_swap_buffer = new agg::int8u[pixels_width * subpixel_scale];
for (int i = 0; i < num_chars; i++) { for (int i = 0; i < num_chars; i++) {
int codepoint = first_char + i; int codepoint = first_char + i;
@ -194,7 +196,7 @@ int FontRendererBakeFontBitmap(FontRenderer *font_renderer, int font_height,
const int y_baseline = y - pad_y - ascender_px; const int y_baseline = y - pad_y - ascender_px;
double x_next = x, y_next = y_baseline; double x_next = x, y_next = y_baseline;
renderer_alpha.render_codepoint(ren_buf, font_height_reduced, text_color, x_next, y_next, codepoint, subpixel_scale); renderer_alpha.render_codepoint(ren_buf, text_color, x_next, y_next, codepoint, subpixel_scale);
int x_next_i = (subpixel_scale == 1 ? int(x_next + 1.0) : ceil_to_multiple(x_next + 0.5, subpixel_scale)); int x_next_i = (subpixel_scale == 1 ? int(x_next + 1.0) : ceil_to_multiple(x_next + 0.5, subpixel_scale));
// Below x and x_next_i will always be integer multiples of subpixel_scale. // Below x and x_next_i will always be integer multiples of subpixel_scale.

View File

@ -19,6 +19,7 @@ typedef struct FontRendererImpl FontRenderer;
enum { enum {
FONT_RENDERER_HINTING = 1 << 0, FONT_RENDERER_HINTING = 1 << 0,
FONT_RENDERER_KERNING = 1 << 1, FONT_RENDERER_KERNING = 1 << 1,
FONT_RENDERER_SUBPIXEL = 1 << 2,
}; };
typedef struct { typedef struct {

View File

@ -26,6 +26,7 @@ class font_renderer_alpha
// Font rendering options. // Font rendering options.
bool m_hinting; bool m_hinting;
bool m_kerning; bool m_kerning;
bool m_subpixel;
bool m_font_loaded; bool m_font_loaded;
@ -36,11 +37,12 @@ class font_renderer_alpha
public: public:
typedef agg::pixfmt_alpha8::color_type color_type; typedef agg::pixfmt_alpha8::color_type color_type;
font_renderer_alpha(bool hinting, bool kerning): font_renderer_alpha(bool hinting, bool kerning, bool subpixel):
m_feng(), m_feng(),
m_fman(m_feng), m_fman(m_feng),
m_hinting(hinting), m_hinting(hinting),
m_kerning(kerning), m_kerning(kerning),
m_subpixel(subpixel),
m_font_loaded(false), m_font_loaded(false),
m_curves(m_fman.path_adaptor()), m_curves(m_fman.path_adaptor()),
m_trans(m_curves, m_mtx) m_trans(m_curves, m_mtx)
@ -74,21 +76,29 @@ public:
bool load_font(const char *font_filename) { bool load_font(const char *font_filename) {
if(m_feng.load_font(font_filename, 0, agg::glyph_ren_outline)) { if(m_feng.load_font(font_filename, 0, agg::glyph_ren_outline)) {
m_font_loaded = true; m_font_loaded = true;
m_feng.hinting(m_hinting);
} }
return m_font_loaded; return m_font_loaded;
} }
void set_font_height(double height) {
const double scale_x = 100.0;
m_feng.height(height);
if (m_subpixel) {
const int subpixel_scale = 3;
m_feng.width(height * scale_x * subpixel_scale);
} else {
m_feng.width(height * scale_x);
}
}
template<class Rasterizer, class Scanline, class RenSolid> template<class Rasterizer, class Scanline, class RenSolid>
void draw_codepoint(Rasterizer& ras, Scanline& sl, void draw_codepoint(Rasterizer& ras, Scanline& sl,
RenSolid& ren_solid, const color_type color, RenSolid& ren_solid, const color_type color,
int codepoint, double& x, double& y, double height, int subpixel_scale) int codepoint, double& x, double& y, int subpixel_scale)
{ {
const double scale_x = 100; const double scale_x = 100;
m_feng.height(height);
m_feng.width(height * scale_x * subpixel_scale);
m_feng.hinting(m_hinting);
// Represent the delta in x scaled by scale_x. // Represent the delta in x scaled by scale_x.
double x_delta = 0; double x_delta = 0;
double start_x = x; double start_x = x;
@ -126,7 +136,6 @@ public:
} }
void render_codepoint(agg::rendering_buffer& ren_buf, void render_codepoint(agg::rendering_buffer& ren_buf,
const double text_size,
const color_type text_color, const color_type text_color,
double& x, double& y, double& x, double& y,
int codepoint, int subpixel_scale) int codepoint, int subpixel_scale)
@ -141,6 +150,6 @@ public:
agg::pixfmt_alpha8 pf(ren_buf); agg::pixfmt_alpha8 pf(ren_buf);
base_ren_type ren_base(pf); base_ren_type ren_base(pf);
renderer_solid ren_solid(ren_base); renderer_solid ren_solid(ren_base);
draw_codepoint(ras, sl, ren_solid, text_color, codepoint, x, y, text_size, subpixel_scale); draw_codepoint(ras, sl, ren_solid, text_color, codepoint, x, y, subpixel_scale);
} }
}; };

View File

@ -232,7 +232,7 @@ RenFont* ren_load_font(const char *filename, float size) {
font->size = size; font->size = size;
const float gamma = 1.5; const float gamma = 1.5;
font->renderer = FontRendererNew(FONT_RENDERER_HINTING, gamma); font->renderer = FontRendererNew(FONT_RENDERER_HINTING|FONT_RENDERER_SUBPIXEL, gamma);
if (FontRendererLoadFont(font->renderer, filename)) { if (FontRendererLoadFont(font->renderer, filename)) {
free(font); free(font);
return NULL; return NULL;