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