@ -3,7 +3,7 @@
@@ -3,7 +3,7 @@
# include <limits>
# include <iostream>
TextRasterizer : : TextRasterizer ( std : : string fontPath , int size , int resolution ) {
TextRasterizer : : TextRasterizer ( const std : : string & fontPath , int size , int resolution ) {
FT_Init_FreeType ( & lib ) ;
face = std : : make_unique < FT_Face > ( ) ;
@ -35,35 +35,38 @@ TextRasterizer::~TextRasterizer() {
@@ -35,35 +35,38 @@ TextRasterizer::~TextRasterizer() {
FT_Done_FreeType ( lib ) ;
}
std : : unique_ptr < Glyph [ ] > TextRasterizer : : rasterize ( std : : string text , int x , int y , unsigned int & glyphCount ) {
std : : unique_ptr < const Glyph [ ] > TextRasterizer : : rasterize ( const std : : string & text , const int x , const int y , unsigned int & glyphCount ) const {
hb_buffer_reset ( buffer ) ;
hb_buffer_set_direction ( buffer , HB_DIRECTION_LTR ) ;
hb_buffer_set_language ( buffer , hb_language_from_string ( " en " , 2 ) ) ;
hb_buffer_add_utf8 ( buffer , " Hello, World! " , text . length ( ) , 0 , text . length ( ) ) ;
hb_buffer_add_utf8 ( buffer , text . c_str ( ) , text . length ( ) , 0 , text . length ( ) ) ;
hb_feature_t KerningOn = { HB_TAG ( ' k ' , ' e ' , ' r ' , ' n ' ) , 1 , 0 , std : : numeric_limits < unsigned int > : : max ( ) } ;
const hb_feature_t KerningOn = { HB_TAG ( ' k ' , ' e ' , ' r ' , ' n ' ) , 1 , 0 , std : : numeric_limits < unsigned int > : : max ( ) } ;
hb_shape ( font , buffer , & KerningOn , 1 ) ;
hb_glyph_info_t * glyphInfo = hb_buffer_get_glyph_infos ( buffer , & glyphCount ) ;
hb_glyph_position_t * glyphPos = hb_buffer_get_glyph_positions ( buffer , & glyphCount ) ;
const hb_glyph_info_t * glyphInfo = hb_buffer_get_glyph_infos ( buffer , & glyphCount ) ;
const hb_glyph_position_t * glyphPos = hb_buffer_get_glyph_positions ( buffer , & glyphCount ) ;
std : : unique_ptr < Glyph [ ] > glyphs = std : : unique_ptr < Glyph [ ] > ( new Glyph [ glyphCount ] ) ;
int cx = x ;
int cy = y ;
for ( int i = 0 ; i < glyphCount ; i + + ) {
if ( FT_Load_Glyph ( * face , glyphInfo [ i ] . codepoint , FT_LOAD_DEFAULT ) ) {
std : : cout < < " Could not load glyph " < < std : : endl ;
return nullptr ;
}
FT_GlyphSlot slot = ( * face ) - > glyph ;
const FT_GlyphSlot slot = ( * face ) - > glyph ;
if ( FT_Render_Glyph ( slot , FT_RENDER_MODE_NORMAL ) ) {
std : : cout < < " Could not render glyph " < < std : : endl ;
return nullptr ;
}
FT_Bitmap ftBitmap = slot - > bitmap ;
const FT_Bitmap ftBitmap = slot - > bitmap ;
glyphs [ i ] . textureWidth = pow ( 2 , ceil ( log ( ftBitmap . width ) / log ( 2 ) ) ) ;
glyphs [ i ] . textureHeight = pow ( 2 , ceil ( log ( ftBitmap . rows ) / log ( 2 ) ) ) ;
@ -72,13 +75,13 @@ std::unique_ptr<Glyph[]> TextRasterizer::rasterize(std::string text, int x, int
@@ -72,13 +75,13 @@ std::unique_ptr<Glyph[]> TextRasterizer::rasterize(std::string text, int x, int
memcpy ( glyphs [ i ] . textureData . get ( ) + iy * glyphs [ i ] . textureWidth , ftBitmap . buffer + iy * ftBitmap . width , ftBitmap . width ) ;
}
float xa = ( float ) glyphPos [ i ] . x_advance / 64 ;
float ya = ( float ) glyphPos [ i ] . y_advance / 64 ;
float xo = ( float ) glyphPos [ i ] . x_offset / 64 ;
float yo = ( float ) glyphPos [ i ] . y_offset / 64 ;
const float xa = ( float ) glyphPos [ i ] . x_advance / 64 ;
const float ya = ( float ) glyphPos [ i ] . y_advance / 64 ;
const float xo = ( float ) glyphPos [ i ] . x_offset / 64 ;
const float yo = ( float ) glyphPos [ i ] . y_offset / 64 ;
glyphs [ i ] . x0 = x + xo + slot - > bitmap_left ;
glyphs [ i ] . y0 = floor ( y + yo + slot - > bitmap_top ) ;
glyphs [ i ] . x0 = c x + xo + slot - > bitmap_left ;
glyphs [ i ] . y0 = floor ( c y + yo + slot - > bitmap_top ) ;
glyphs [ i ] . x1 = glyphs [ i ] . x0 + ftBitmap . width ;
glyphs [ i ] . y1 = floor ( glyphs [ i ] . y0 - ftBitmap . rows ) ;
@ -87,14 +90,14 @@ std::unique_ptr<Glyph[]> TextRasterizer::rasterize(std::string text, int x, int
@@ -87,14 +90,14 @@ std::unique_ptr<Glyph[]> TextRasterizer::rasterize(std::string text, int x, int
glyphs [ i ] . s1 = ( float ) ftBitmap . width / glyphs [ i ] . textureWidth ;
glyphs [ i ] . t1 = ( float ) ftBitmap . rows / glyphs [ i ] . textureHeight ;
x + = xa ;
y + = ya ;
c x + = xa ;
cx + = ya ;
}
return glyphs ;
}
bool TextRasterizer : : isUnicodeBMP ( FT_Face face ) {
const bool TextRasterizer : : isUnicodeBMP ( const FT_Face & face ) const {
for ( int i = 0 ; i < face - > num_charmaps ; i + + ) {
if ( ( ( face - > charmaps [ i ] - > platform_id = = 0 ) & & ( face - > charmaps [ i ] - > encoding_id = = 3 ) ) | | ( ( face - > charmaps [ i ] - > platform_id = = 3 ) & & ( face - > charmaps [ i ] - > encoding_id = = 1 ) ) ) {
FT_Set_Charmap ( face , face - > charmaps [ i ] ) ;