Adjust the font's height when creating the bitmap

Now the font's height to compute the bitmap is computed correcty but:

- STB truetype takes the pixel height as a float
- AGG takes an integer
This commit is contained in:
Francesco Abbate 2020-05-30 20:01:37 +02:00
parent 4334d09bab
commit 7c57b1d6ca
3 changed files with 16 additions and 53 deletions

View File

@ -79,6 +79,9 @@ So BakeFontBitmap gets
pixel_height = (ascent - descent) * font->size / unitsPerEm; pixel_height = (ascent - descent) * font->size / unitsPerEm;
``` ```
In turns BakeFontBitmap passes pixel_height to ScaleForPixelHeight() and uses the
resulting 'scale' to scale the glyphs by passing it to MakeGlyphBitmap().
This is equal almost equal to font->height except the 0.5, the missing linegap calculation This is equal almost equal to font->height except the 0.5, the missing linegap calculation
and the fact that this latter is an integer instead of a float. and the fact that this latter is an integer instead of a float.
@ -109,54 +112,7 @@ FT_Done_FreeType (end with library)
## Freetype2's metrics related function and structs ## Freetype2's metrics related function and structs
FT_New_Face FT_New_Face return a FT_Face (a pointer) with font face information.
```c
typedef struct FT_FaceRec_
{
FT_Long num_faces;
FT_Long face_index;
FT_Long face_flags;
FT_Long style_flags;
FT_Long num_glyphs;
FT_String* family_name;
FT_String* style_name;
FT_Int num_fixed_sizes;
FT_Bitmap_Size* available_sizes;
FT_Int num_charmaps;
FT_CharMap* charmaps;
FT_Generic generic;
/*# The following member variables (down to `underline_thickness`) */
/*# are only relevant to scalable outlines; cf. @FT_Bitmap_Size */
/*# for bitmap fonts. */
FT_BBox bbox;
FT_UShort units_per_EM;
FT_Short ascender;
FT_Short descender;
FT_Short height;
FT_Short max_advance_width;
FT_Short max_advance_height;
FT_Short underline_position;
FT_Short underline_thickness;
FT_GlyphSlot glyph;
FT_Size size;
FT_CharMap charmap;
/* private part omitted. */
} FT_FaceRec;
```
## AGG font engine's font size ## AGG font engine's font size

View File

@ -25,6 +25,9 @@ struct RenFontA {
font_renderer_alpha *renderer; font_renderer_alpha *renderer;
float size; float size;
int height; int height;
// For some reason the font's height used in renderer.c
// when calling BakeFontBitmap is 0.5 off from 'height'.
float height_bitmap;
}; };
template <typename T> template <typename T>
@ -57,7 +60,7 @@ RenFontA* ren_load_font_agg(const char *filename, float size) {
font = (RenFontA *) check_alloc(calloc(1, sizeof(RenFontA))); font = (RenFontA *) check_alloc(calloc(1, sizeof(RenFontA)));
font->size = size; font->size = size;
font->renderer = new font_renderer_alpha(true, false); font->renderer = new font_renderer_alpha(false, false);
font->renderer->load_font(filename); font->renderer->load_font(filename);
int ascender, descender; int ascender, descender;
@ -66,6 +69,7 @@ RenFontA* ren_load_font_agg(const char *filename, float size) {
float scale = font->renderer->scale_for_em_to_pixels(size); float scale = font->renderer->scale_for_em_to_pixels(size);
font->height = (ascender - descender) * scale + 0.5; font->height = (ascender - descender) * scale + 0.5;
font->height_bitmap = (ascender - descender) * scale;
fprintf(stderr, "Font height: %d\n", font->height); fprintf(stderr, "Font height: %d\n", font->height);
@ -84,15 +88,17 @@ retry:
memset(set->image->pixels, 0x00, width * height * pixel_size); memset(set->image->pixels, 0x00, width * height * pixel_size);
fprintf(stderr, "Using height: %g in BakeFontBitmap\n", font->height_bitmap);
agg::rendering_buffer ren_buf((agg::int8u *) set->image->pixels, width, height, -width * pixel_size); agg::rendering_buffer ren_buf((agg::int8u *) set->image->pixels, width, height, -width * pixel_size);
// FIXME: figure out how to precisely layout each glyph. // FIXME: figure out how to precisely layout each glyph.
double x = 4, y = height - font->size * 3 / 2; double x = 4, y = height - font->height * 3 / 2;
int res = 0; int res = 0;
const agg::alpha8 text_color(0xff); const agg::alpha8 text_color(0xff);
for (int i = 0; i < 256; i++) { for (int i = 0; i < 256; i++) {
if (x + font->size * 3 / 2 > width) { if (x + font->height * 3 / 2 > width) {
x = 4; x = 4;
y -= font->size * 3 / 2; y -= font->height * 3 / 2;
} }
if (y < 0) { if (y < 0) {
res = -1; res = -1;
@ -101,7 +107,7 @@ retry:
// FIXME: we are ignoring idx and with a char we cannot pass codepoint > 255. // FIXME: we are ignoring idx and with a char we cannot pass codepoint > 255.
char text[2] = {char(i % 256), 0}; char text[2] = {char(i % 256), 0};
// FIXME: using font->size below is wrong. // FIXME: using font->size below is wrong.
font->renderer->render_text(ren_buf, font->size, text_color, x, y, text); font->renderer->render_text(ren_buf, font->height_bitmap, text_color, x, y, text);
} }
/* retry with a larger image buffer if the buffer wasn't large enough */ /* retry with a larger image buffer if the buffer wasn't large enough */

View File

@ -107,6 +107,7 @@ retry:
float s = float s =
stbtt_ScaleForMappingEmToPixels(&font->stbfont, 1) / stbtt_ScaleForMappingEmToPixels(&font->stbfont, 1) /
stbtt_ScaleForPixelHeight(&font->stbfont, 1); stbtt_ScaleForPixelHeight(&font->stbfont, 1);
fprintf(stderr, "Using height: %g in BakeFontBitmap\n", font->size * s);
int res = stbtt_BakeFontBitmap( int res = stbtt_BakeFontBitmap(
font->data, 0, font->size * s, (void*) set->image->pixels, font->data, 0, font->size * s, (void*) set->image->pixels,
width, height, idx * 256, 256, set->glyphs); width, height, idx * 256, 256, set->glyphs);