source: trunk/src/gfx/texture_font.cc @ 367

Last change on this file since 367 was 367, checked in by epyon, 10 years ago
  • fixed compilation and warnings on gcc
  • slowly removing/merging external includes
File size: 5.4 KB
Line 
1// Copyright (C) 2012-2014 ChaosForge Ltd
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#include "nv/gfx/texture_font.hh"
7
8#include "nv/lib/freetype2.hh"
9#include "nv/core/logging.hh"
10
11using namespace nv;
12
13texture_glyph::texture_glyph()
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_glyph::get_kerning( const uint16 other )
19{
20        auto i = kerning.find( other );
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_hinting( true ), m_filtering( true ), m_rlibrary( nullptr ), m_rface( nullptr )
28{
29        load_freetype_library();
30        size_t hres = 64;
31        FT_Error error;
32        FT_Matrix matrix = {
33                (int)((1.0/hres) * 0x10000L),
34                (int)((0.0)      * 0x10000L),
35                (int)((0.0)      * 0x10000L),
36                (int)((1.0)      * 0x10000L)
37        };
38
39        m_lcd_weights[0] = 0x10;
40        m_lcd_weights[1] = 0x40;
41        m_lcd_weights[2] = 0x70;
42        m_lcd_weights[3] = 0x40;
43        m_lcd_weights[4] = 0x10;
44         
45        error = FT_Init_FreeType( (FT_Library*)(&m_rlibrary) );
46        if ( error ) NV_THROW( std::runtime_error, "FT_Error" );
47
48        error = FT_New_Face( (FT_Library)(m_rlibrary), filename, 0, (FT_Face*)(&m_rface) );
49        if ( error ) NV_THROW( std::runtime_error, "FT_Error" );
50
51        error = FT_Set_Char_Size( (FT_Face)(m_rface), (int)(size*64), 0, 72*64, 72 );
52        if ( error ) NV_THROW( std::runtime_error, "FT_Error" );
53
54    FT_Set_Transform( (FT_Face)(m_rface), &matrix, NULL );
55
56    FT_Size_Metrics metrics = ((FT_Face)(m_rface))->size->metrics;
57    m_ascender  = (float)(metrics.ascender >> 6) / 100.0f;
58    m_descender = (float)(metrics.descender >> 6) / 100.0f;
59    m_height    = (float)(metrics.height >> 6) / 100.0f;
60    m_linegap   = m_height - m_ascender + m_descender;
61}
62
63const texture_glyph* texture_font::get_glyph( uint16 charcode ) const
64{
65        auto i = m_glyphs.find( charcode );
66        if ( i == m_glyphs.end() )
67        {
68                return nullptr;
69        }
70        return &(i->second);
71}
72
73static uint8* convert_to_rgba(uint8* rgb, const int lines, const int line_count, const int line_bpitch )
74{
75        uint8* result = new uint8[ lines * line_count * 4 ];
76        uint8* rgba   = result;
77        for(int l=0; l<lines; ++l)
78        {
79                for(int c=0; c<line_count; ++c)
80                {
81                        for(int j=0; j<3; ++j) {
82                                rgba[j]  = rgb[j];
83                        }
84                        rgba[3] = uint8( uint16( rgb[0] + rgb[1] + rgb[2] ) / 3 );
85                        rgba += 4;
86                        rgb  += 3;
87                }
88                rgb += line_bpitch - ( 3 * line_count );
89        }
90        return result;
91}
92
93bool texture_font::load_glyphs( const string& codes )
94{
95        FT_Face face     = (FT_Face)(m_rface);
96        size_t depth     = m_atlas->get_depth();
97        glm::ivec2 asize = m_atlas->get_size();
98        FT_Int32 flags = 0;
99        flags |= FT_LOAD_RENDER;
100        if( !m_hinting )
101        {
102                flags |= FT_LOAD_NO_HINTING | FT_LOAD_NO_AUTOHINT;
103        }
104        else
105        {
106                flags |= FT_LOAD_FORCE_AUTOHINT;
107        }
108
109        if( m_atlas->get_depth() >= 3 )
110        {
111                FT_Library_SetLcdFilter( (FT_Library)(m_rlibrary), FT_LCD_FILTER_LIGHT );
112                flags |= FT_LOAD_TARGET_LCD;
113                if ( m_filtering )
114                {
115                        FT_Library_SetLcdFilterWeights( (FT_Library)(m_rlibrary), m_lcd_weights );
116                }
117        }
118
119        for ( char ch : codes )
120        {
121                uint16 c = static_cast<uint16>( ch );
122                FT_UInt glyph_index = FT_Get_Char_Index( face, c );
123                FT_Error error = FT_Load_Glyph( face, glyph_index, flags );
124                if ( error )
125                {
126                        NV_LOG_ERROR( "FT_Error while loading glyphs, error: ", error, " code: ", c );
127                        NV_THROW( std::runtime_error, "FT_Error while loading glyphs" );
128                }
129
130                FT_GlyphSlot slot   = face->glyph;
131                FT_Bitmap ft_bitmap = slot->bitmap;
132                int ft_bitmap_width = slot->bitmap.width;
133                int ft_bitmap_rows  = slot->bitmap.rows;
134                int ft_glyph_top    = slot->bitmap_top;
135                int ft_glyph_left   = slot->bitmap_left;
136                int reg_width       = ft_bitmap_width / (depth > 3 ? 3 : (int)depth);
137
138                glm::ivec2 gsize( reg_width + 1, ft_bitmap_rows + 1 );
139                region r = m_atlas->get_region( gsize );
140                if ( r.pos.x < 0 )
141                {
142                        NV_LOG_ERROR( "Atlas full while loading glyphs, r.pos.x: ", r.pos.x, " code: ", c );
143                        NV_THROW( std::runtime_error, "Atlas full while loading glyphs" );
144                }
145                if (depth == 4)
146                {
147                        r.size.x -= 1;
148                        r.size.y -= 1;
149                        uint8* data = convert_to_rgba(ft_bitmap.buffer, r.size.y, r.size.x, ft_bitmap.pitch );
150                        m_atlas->set_region( r, data );
151                        delete data;
152                }
153                else
154                {
155                        r.size.x -= 1;
156                        r.size.y -= 1;
157                        m_atlas->set_region( r, ft_bitmap.buffer, ft_bitmap.pitch );
158                }
159
160                m_glyphs[ c ] = texture_glyph();
161                texture_glyph* g = &(m_glyphs.find( c )->second);
162
163                g->charcode = c;
164                g->size     = gsize;
165                g->offset   = glm::ivec2( ft_glyph_left, ft_glyph_top );
166                g->tl       = glm::vec2( r.pos.x/(float)asize.x, r.pos.y/(float)asize.y );
167                g->br       = glm::vec2( ( r.pos.x + gsize.x )/(float)asize.x, (r.pos.y + gsize.y )/(float)asize.y );
168
169                // Discard hinting to get advance
170                FT_Load_Glyph( face, glyph_index, FT_LOAD_RENDER | FT_LOAD_NO_HINTING);
171                slot = face->glyph;
172                g->advance = glm::ivec2( slot->advance.x/64.0, slot->advance.y/64.0 );
173        }
174        generate_kerning();
175        return true;
176}
177
178texture_font::~texture_font()
179{
180        if ( m_rface )    FT_Done_Face( (FT_Face)(m_rface) );
181        if ( m_rlibrary ) FT_Done_FreeType( (FT_Library)(m_rlibrary) );
182}
183
184void texture_font::generate_kerning()
185{
186
187}
188
Note: See TracBrowser for help on using the repository browser.