source: trunk/src/gl/texture_font.cc @ 21

Last change on this file since 21 was 21, checked in by epyon, 12 years ago
  • bugfixes
  • missing files added
File size: 4.3 KB
Line 
1// Copyright (C) 2012-2013 ChaosForge / Kornel Kisielewicz
2// http://chaosforge.org/
3//
4// This file is part of NV Libraries.
5// For conditions of distribution and use, see copyright notice in nv.hh
6
7#include "nv/gl/texture_font.hh"
8
9#include "nv/lib/freetype2.hh"
10
11using namespace nv;
12
13texture_gylph::texture_gylph()
14        : charcode(0), size(0,0), offset(0,0), advance(0.f,0.f), tl(0.f,0.f), br(0.f,0.f)
15{
16}
17
18float texture_gylph::get_kerning( const uint16 charcode )
19{
20        auto i = kerning.find( charcode );
21        return i != kerning.end() ? i->second : 0.0f;
22}
23
24texture_font::texture_font( texture_atlas* atlas, const char * filename, float size )
25        : m_atlas( atlas ), m_filename(filename), m_size( size ),
26        m_height(0), m_linegap(0), m_ascender(0), m_descender(0),
27        m_rlibrary( nullptr ), m_rface( nullptr ), m_hinting( true ), m_filtering( true )
28{
29        size_t hres = 64;
30        FT_Error error;
31        FT_Matrix matrix = {
32                (int)((1.0/hres) * 0x10000L),
33                (int)((0.0)      * 0x10000L),
34                (int)((0.0)      * 0x10000L),
35                (int)((1.0)      * 0x10000L)
36        };
37
38        m_lcd_weights[0] = 0x10;
39        m_lcd_weights[1] = 0x40;
40        m_lcd_weights[2] = 0x70;
41        m_lcd_weights[3] = 0x40;
42        m_lcd_weights[4] = 0x10;
43         
44        error = FT_Init_FreeType( (FT_Library*)(&m_rlibrary) );
45        if ( error ) throw std::runtime_error( "FT_Error" );
46
47        error = FT_New_Face( (FT_Library)(m_rlibrary), filename, 0, (FT_Face*)(&m_rface) );
48        if ( error ) throw std::runtime_error( "FT_Error" );
49
50        error = FT_Set_Char_Size( (FT_Face)(m_rface), (int)(size*64), 0, 72*64, 72 );
51        if ( error ) throw std::runtime_error( "FT_Error" );
52
53    FT_Set_Transform( (FT_Face)(m_rface), &matrix, NULL );
54
55    FT_Size_Metrics metrics = ((FT_Face)(m_rface))->size->metrics;
56    m_ascender = (metrics.ascender >> 6) / 100.0;
57    m_descender = (metrics.descender >> 6) / 100.0;
58    m_height = (metrics.height >> 6) / 100.0;
59    m_linegap = m_height - m_ascender + m_descender;
60}
61
62const texture_gylph* texture_font::get_gylph( uint16 charcode ) const
63{
64        auto i = m_gylphs.find( charcode );
65        if ( i == m_gylphs.end() )
66        {
67                return nullptr;
68        }
69        return &(i->second);
70}
71
72bool texture_font::load_glyphs( const std::string& codes )
73{
74        FT_Face face     = (FT_Face)(m_rface);
75        size_t depth     = m_atlas->get_depth();
76        glm::ivec2 asize = m_atlas->get_size();
77        FT_Int32 flags = 0;
78        flags |= FT_LOAD_RENDER;
79        if( !m_hinting )
80        {
81                flags |= FT_LOAD_NO_HINTING | FT_LOAD_NO_AUTOHINT;
82        }
83        else
84        {
85                flags |= FT_LOAD_FORCE_AUTOHINT;
86        }
87
88        if( m_atlas->get_depth() == 3 )
89        {
90                FT_Library_SetLcdFilter( (FT_Library)(m_rlibrary), FT_LCD_FILTER_LIGHT );
91                flags |= FT_LOAD_TARGET_LCD;
92                if ( m_filtering )
93                {
94                        FT_Library_SetLcdFilterWeights( (FT_Library)(m_rlibrary), m_lcd_weights );
95                }
96        }
97
98        for ( char c : codes )
99        {
100                FT_UInt glyph_index = FT_Get_Char_Index( face, c );
101                FT_Error error = FT_Load_Glyph( face, glyph_index, flags );
102                if ( error ) throw std::runtime_error( "FT_Error while loading gylphs" );
103
104                FT_GlyphSlot slot   = face->glyph;
105                FT_Bitmap ft_bitmap = slot->bitmap;
106                int ft_bitmap_width = slot->bitmap.width;
107                int ft_bitmap_rows  = slot->bitmap.rows;
108                int ft_glyph_top    = slot->bitmap_top;
109                int ft_glyph_left   = slot->bitmap_left;
110
111                glm::ivec2 gsize( ft_bitmap_width/depth + 1, ft_bitmap_rows + 1 );
112                region r = m_atlas->get_region( gsize );
113                if ( r.pos.x < 0 )
114                {
115                        throw std::runtime_error( "Atlas full while loading gylphs" );
116                }
117                r.size.x -= 1;
118                r.size.y -= 1;
119                m_atlas->set_region( r, ft_bitmap.buffer, ft_bitmap.pitch );
120
121                m_gylphs[ c ] = texture_gylph();
122                texture_gylph* g = &(m_gylphs.find( c )->second);
123
124                g->charcode = c;
125                g->size     = gsize;
126                g->offset   = glm::ivec2( ft_glyph_left, ft_glyph_top );
127                g->tl       = glm::vec2( r.pos.x/(float)asize.x, r.pos.y/(float)asize.y );
128                g->br       = glm::vec2( ( r.pos.x + gsize.x )/(float)asize.x, (r.pos.y + gsize.y )/(float)asize.y );
129
130                // Discard hinting to get advance
131                FT_Load_Glyph( face, glyph_index, FT_LOAD_RENDER | FT_LOAD_NO_HINTING);
132                slot = face->glyph;
133                g->advance = glm::ivec2( slot->advance.x/64.0, slot->advance.y/64.0 );
134        }
135        generate_kerning();
136        return true;
137}
138
139texture_font::~texture_font()
140{
141        if ( m_rface )    FT_Done_Face( (FT_Face)(m_rface) );
142        if ( m_rlibrary ) FT_Done_FreeType( (FT_Library)(m_rlibrary) );
143}
144
145void texture_font::generate_kerning()
146{
147
148}
149
Note: See TracBrowser for help on using the repository browser.