Font: compute baseline_offset

This commit is contained in:
Josh Holtrop 2014-07-26 11:02:37 -04:00
parent de5b4e7413
commit 5c52ed0c4d

View File

@ -95,10 +95,13 @@ static VALUE Font_new(VALUE klass, VALUE fname, VALUE size)
FT_Set_Pixel_Sizes(font->face, 0, NUM2INT(size));
static const char preload_glyphs[] = "HMgjqxy_|^";
int max_top = -9999;
int min_bottom = 9999;
for (unsigned int i = 0; i < sizeof(preload_glyphs) - 1u; i++)
{
if (get_glyph(font, preload_glyphs[i]) == NULL)
GlyphRef g = get_glyph(font, preload_glyphs[i]);
if (g == NULL)
{
delete font;
rb_raise(rb_eRuntimeError,
@ -106,18 +109,20 @@ static VALUE Font_new(VALUE klass, VALUE fname, VALUE size)
preload_glyphs[i],
fname_cstr);
}
if (font->face->glyph->bitmap_top > max_top)
max_top = font->face->glyph->bitmap_top;
int bitmap_bottom = font->face->glyph->bitmap_top - font->face->glyph->bitmap.rows;
if (bitmap_bottom < min_bottom)
min_bottom = bitmap_bottom;
}
#if 0
m_baseline_offset = round_up_26_6((m_face->size->metrics.height - (m_face->size->metrics.ascender - m_face->size->metrics.descender)) / 2 - m_face->size->metrics.descender);
#endif
#if 0
m_baseline_offset = 1 - m_face->glyph->bitmap_top + m_face->glyph->bitmap.rows;
#endif
int line_height = round_up_26_6(font->face->size->metrics.height);
int baseline_offset = (line_height - (max_top - min_bottom)) / 2;
VALUE rv = Data_Wrap_Struct(klass, NULL, Font_free, font);
rb_iv_set(rv, "@advance", INT2FIX(get_glyph(font, 'x')->get_advance()));
rb_iv_set(rv, "@line_height", INT2FIX(round_up_26_6(font->face->size->metrics.height)));
rb_iv_set(rv, "@line_height", INT2FIX(line_height));
rb_iv_set(rv, "@baseline_offset", INT2FIX(baseline_offset));
return rv;
}