]> git.tdb.fi Git - libs/gl.git/blobdiff - source/font.cpp
Windows compatibility:
[libs/gl.git] / source / font.cpp
index 956acb462324dddb2603442981ef476f98be6d1f..637569f205358a8a08edf846fbac8c55a480e7e1 100644 (file)
@@ -1,4 +1,11 @@
-#include <GL/gl.h>
+/* $Id$
+
+This file is part of libmspgl
+Copyright © 2007  Mikko Rasa, Mikkosoft Productions
+Distributed under the LGPL
+*/
+
+#include "gl.h"
 #include "font.h"
 #include "texture2d.h"
 
@@ -9,7 +16,6 @@ namespace GL {
 
 Font::Font():
        tex(0),
-       own_tex(false),
        default_size(1),
        ascent(1),
        descent(0),
@@ -21,7 +27,7 @@ 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;
@@ -41,52 +47,26 @@ void Font::add_glyph(wchar_t code, float x1, float y1, float x2, float y2, float
        create_glyph_vertices(glyph, *va_builder);
 }
 
-float Font::get_string_width(const string &str) const
+float Font::get_string_width(const string &str, Codecs::Decoder &dec) const
 {
        float x=0;
 
-       for(string::const_iterator i=str.begin(); i!=str.end(); ++i)
-               x+=get_glyph_advance(static_cast<unsigned char>(*i));
+       for(string::const_iterator i=str.begin(); i!=str.end();)
+               x+=get_glyph_advance(dec.decode_char(str, i));
 
        return x;
 }
 
-float Font::get_string_width(const wstring &str) const
-{
-       float x=0;
-
-       for(wstring::const_iterator i=str.begin(); i!=str.end(); ++i)
-               x+=get_glyph_advance(*i);
-
-       return x;
-}
-
-void Font::draw_string(const string &str) const
+void Font::draw_string(const string &str, Codecs::Decoder &dec) const
 {
        prepare_render();
 
-       for(string::const_iterator i=str.begin(); i!=str.end(); ++i)
-               draw_glyph(static_cast<unsigned char>(*i));
+       for(string::const_iterator i=str.begin(); i!=str.end();)
+               draw_glyph(dec.decode_char(str, i));
 
        glPopMatrix();
 }
 
-void Font::draw_string(const wstring &str) const
-{
-       prepare_render();
-
-       for(wstring::const_iterator i=str.begin(); i!=str.end(); ++i)
-               draw_glyph(*i);
-
-       glPopMatrix();
-}
-
-Font::~Font()
-{
-       if(own_tex)
-               delete tex;
-}
-
 void Font::create_glyph_vertices()
 {
        varray.clear();
@@ -119,18 +99,21 @@ 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())
                return;
 
+       const Glyph &glyph=i->second;
+       (void)glyph;
+
        glDrawArrays(GL_QUADS, i->second.index*4, 4);
 
        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())
@@ -141,13 +124,24 @@ float Font::get_glyph_advance(wchar_t code) const
 
 
 Font::Loader::Loader(Font &f):
-       font(f)
+       font(f),
+       coll(0)
 {
-       add("default_size", &Font::default_size);
-       add("ascent",  &Font::ascent);
-       add("descent", &Font::descent);
-       add("texture", &Loader::texture);
-       add("glyph",   &Loader::glyph);
+       init();
+}
+
+Font::Loader::Loader(Font &f, Collection &c):
+       font(f),
+       coll(&c)
+{
+       init();
+}
+
+DataFile::Collection &Font::Loader::get_collection()
+{
+       if(!coll)
+               throw InvalidState("No collection");
+       return *coll;
 }
 
 Font::Loader::~Loader()
@@ -155,13 +149,13 @@ Font::Loader::~Loader()
        font.create_glyph_vertices();
 }
 
-void Font::Loader::texture(const string &t)
+void Font::Loader::init()
 {
-       Texture2D *tex=new Texture2D;
-       tex->parameter(GL_GENERATE_MIPMAP, 1);
-       font.tex=tex;
-       tex->image(t);
-       font.own_tex=true;
+       add("default_size", &Font::default_size);
+       add("ascent",  &Font::ascent);
+       add("descent", &Font::descent);
+       add("texture", &Font::tex);
+       add("glyph",   &Loader::glyph);
 }
 
 void Font::Loader::glyph(unsigned c)
@@ -177,8 +171,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);
 }
 
@@ -190,17 +184,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