diff --git a/src/Font.cc b/src/Font.cc index 8ac47af..88b57c0 100644 --- a/src/Font.cc +++ b/src/Font.cc @@ -64,6 +64,17 @@ void Font_free(void * ptr) delete font; } +static GlyphRef get_glyph(Font * font, FT_ULong char_code) +{ + auto it = font->glyphs.find(char_code); + if (it != font->glyphs.end()) + return it->second; + GlyphRef glyph = new Glyph(); + glyph = glyph->load(font->face, char_code) ? glyph : NULL; + font->glyphs[char_code] = glyph; + return glyph; +} + static VALUE Font_new(VALUE klass, VALUE fname, VALUE size) { Init_FreeType(); @@ -83,19 +94,20 @@ static VALUE Font_new(VALUE klass, VALUE fname, VALUE size) FT_Set_Pixel_Sizes(font->face, 0, NUM2INT(size)); -#if 0 - GlyphRef glyph = load_glyph('g'); - if (glyph == NULL) - { - return false; - } - m_glyphs['g'] = glyph; -#endif + static const char preload_glyphs[] = "HMgjqxy_|^"; + + for (unsigned int i = 0; i < sizeof(preload_glyphs) - 1u; i++) + { + if (get_glyph(font, preload_glyphs[i]) == NULL) + { + delete font; + rb_raise(rb_eRuntimeError, + "Error loading glyph '%c' from font '%s'", + preload_glyphs[i], + fname_cstr); + } + } -#if 0 - m_advance = glyph->get_advance(); - m_line_height = round_up_26_6(m_face->size->metrics.height); -#endif #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 @@ -103,31 +115,18 @@ static VALUE Font_new(VALUE klass, VALUE fname, VALUE size) m_baseline_offset = 1 - m_face->glyph->bitmap_top + m_face->glyph->bitmap.rows; #endif - return Data_Wrap_Struct(klass, NULL, Font_free, font); + 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))); + return rv; } void Font_Init() { ruby_class = rb_define_class("Font", rb_cObject); rb_define_singleton_method(ruby_class, "new", (VALUE(*)(...))Font_new, 2); -} - -static GlyphRef load_glyph(Font * font, FT_ULong char_code) -{ - GlyphRef glyph = new Glyph(); - if (glyph->load(font->face, char_code)) - return glyph; - return NULL; -} - -static GlyphRef get_glyph(Font * font, FT_ULong char_code) -{ - auto it = font->glyphs.find(char_code); - if (it != font->glyphs.end()) - return it->second; - GlyphRef g = load_glyph(font, char_code); - font->glyphs[char_code] = g; - return g; + rb_define_attr(ruby_class, "advance", 1, 0); + rb_define_attr(ruby_class, "line_height", 1, 0); } Glyph::Glyph()