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