X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Ffont.cpp;h=7cea86957dc16568c4119de3943a66e9c1c8e5d4;hb=f098a871fc6dc7b61a5aca5581fa327e4124c036;hp=f15e372afaabfaf0946f1aa9ec6093ff80008e66;hpb=84bc56b96c21c831104a22e0cbd0f3b72ab5d8c3;p=libs%2Fgl.git diff --git a/source/font.cpp b/source/font.cpp index f15e372a..7cea8695 100644 --- a/source/font.cpp +++ b/source/font.cpp @@ -1,3 +1,10 @@ +/* $Id$ + +This file is part of libmspgl +Copyright © 2007 Mikko Rasa, Mikkosoft Productions +Distributed under the LGPL +*/ + #include #include "font.h" #include "texture2d.h" @@ -11,15 +18,23 @@ Font::Font(): tex(0), own_tex(false), default_size(1), + ascent(1), + descent(0), varray((TEXCOORD2, VERTEX2)) { } +Font::~Font() +{ + if(own_tex) + delete tex; +} + void Font::set_texture(const Texture2D &t) { tex=&t; } -void Font::add_glyph(wchar_t code, float x1, float y1, float x2, float y2, float w, float h, float ox, float oy, float adv) +void Font::add_glyph(unsigned code, float x1, float y1, float x2, float y2, float w, float h, float ox, float oy, float adv) { Glyph glyph; glyph.code=code; @@ -49,12 +64,12 @@ float Font::get_string_width(const string &str) const return x; } -float Font::get_string_width(const wstring &str) const +float Font::get_string_width(const string &str, Codecs::Decoder &dec) const { float x=0; - for(wstring::const_iterator i=str.begin(); i!=str.end(); ++i) - x+=get_glyph_advance(*i); + for(string::const_iterator i=str.begin(); i!=str.end();) + x+=get_glyph_advance(dec.decode_char(str, i)); return x; } @@ -69,22 +84,16 @@ void Font::draw_string(const string &str) const glPopMatrix(); } -void Font::draw_string(const wstring &str) const +void Font::draw_string(const string &str, Codecs::Decoder &dec) const { prepare_render(); - for(wstring::const_iterator i=str.begin(); i!=str.end(); ++i) - draw_glyph(*i); + for(string::const_iterator i=str.begin(); i!=str.end();) + draw_glyph(dec.decode_char(str, i)); glPopMatrix(); } -Font::~Font() -{ - if(own_tex) - delete tex; -} - void Font::create_glyph_vertices() { varray.clear(); @@ -117,7 +126,7 @@ void Font::prepare_render() const glPushMatrix(); } -void Font::draw_glyph(wchar_t code) const +void Font::draw_glyph(unsigned code) const { GlyphMap::const_iterator i=glyphs.find(code); if(i==glyphs.end()) @@ -128,7 +137,7 @@ void Font::draw_glyph(wchar_t code) const glTranslatef(i->second.advance, 0, 0); } -float Font::get_glyph_advance(wchar_t code) const +float Font::get_glyph_advance(unsigned code) const { GlyphMap::const_iterator i=glyphs.find(code); if(i==glyphs.end()) @@ -142,6 +151,8 @@ Font::Loader::Loader(Font &f): font(f) { add("default_size", &Font::default_size); + add("ascent", &Font::ascent); + add("descent", &Font::descent); add("texture", &Loader::texture); add("glyph", &Loader::glyph); } @@ -173,8 +184,8 @@ Font::Glyph::Loader::Loader(Glyph &g): glyph(g) { add("texcoords", &Loader::texcoords); - add("size", &Loader::size); - add("offset", &Loader::offset); + add("size", &Glyph::w, &Glyph::h); + add("offset", &Glyph::off_x, &Glyph::off_y); add("advance", &Glyph::advance); } @@ -186,17 +197,5 @@ void Font::Glyph::Loader::texcoords(float x1, float y1, float x2, float y2) glyph.y2=y2; } -void Font::Glyph::Loader::size(float w, float h) -{ - glyph.w=w; - glyph.h=h; -} - -void Font::Glyph::Loader::offset(float x, float y) -{ - glyph.off_x=x; - glyph.off_y=y; -} - } // namespace GL } // namespace Msp