|
|
@@ -67,6 +67,13 @@ pngwriterfont::pngwriterfont(const char *face_path, std::string& errorStr)
|
|
|
loaded = true;
|
|
|
}
|
|
|
|
|
|
+pngwriterfont::pngwriterfont(const char *face_path)
|
|
|
+{
|
|
|
+ std::string err;
|
|
|
+ pngwriterfont(face_path, err);
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
pngwriterfont::~pngwriterfont()
|
|
|
{
|
|
|
/* Free the face and the library objects */
|
|
|
@@ -1818,7 +1825,7 @@ void pngwriter::write_png(void)
|
|
|
///////////////////////////////////////////
|
|
|
void pngwriter::plot_text( char * face_path, int fontsize, int x_start, int y_start, double angle, char * text, double red, double green, double blue)
|
|
|
{
|
|
|
- pngwriterfont font(font);
|
|
|
+ pngwriterfont font(face_path);
|
|
|
plot_text(font, fontsize, x_start, y_start, angle, text, red, green, blue);
|
|
|
}
|
|
|
|
|
|
@@ -1924,8 +1931,12 @@ void pngwriter::plot_text(pngwriterfont& font, int fontsize, int x_start, int y_
|
|
|
|
|
|
void pngwriter::plot_text_utf8( char * face_path, int fontsize, int x_start, int y_start, double angle, char * text, double red, double green, double blue)
|
|
|
{
|
|
|
- FT_Library library;
|
|
|
- FT_Face face;
|
|
|
+ pngwriterfont font(face_path);
|
|
|
+ plot_text(font, fontsize, x_start, y_start, angle, text, red, green, blue);
|
|
|
+}
|
|
|
+
|
|
|
+void pngwriter::plot_text_utf8(pngwriterfont& font, int fontsize, int x_start, int y_start, double angle, char * text, double red, double green, double blue)
|
|
|
+{
|
|
|
FT_Matrix matrix; // transformation matrix
|
|
|
FT_Vector pen;
|
|
|
|
|
|
@@ -2030,50 +2041,30 @@ void pngwriter::plot_text_utf8( char * face_path, int fontsize, int x_start, int
|
|
|
std::cout << "Num chars is: "<< num_chars << std::endl;
|
|
|
*/
|
|
|
|
|
|
- /* Initialize FT Library object */
|
|
|
- error = FT_Init_FreeType( &library );
|
|
|
- if (error) {
|
|
|
- std::cerr << " PNGwriter::plot_text_utf8 - ERROR **: FreeType: Could not init Library." << std::endl;
|
|
|
- delete[] ucs4text;
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- /* Initialize FT face object */
|
|
|
- error = FT_New_Face( library,face_path,0,&face );
|
|
|
- if ( error == FT_Err_Unknown_File_Format ) {
|
|
|
- std::cerr << " PNGwriter::plot_text_utf8 - ERROR **: FreeType: Font was opened, but type not supported." << std::endl;
|
|
|
- delete[] ucs4text;
|
|
|
- return;
|
|
|
- } else if (error) {
|
|
|
- std::cerr << " PNGwriter::plot_text - ERROR **: FreeType: Could not find or load font file." << std::endl;
|
|
|
- delete[] ucs4text;
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
/* Set the Char size */
|
|
|
- error = FT_Set_Char_Size( face, /* handle to face object */
|
|
|
+ error = FT_Set_Char_Size(font.getFontFace(), /* handle to face object */
|
|
|
0, /* char_width in 1/64th of points */
|
|
|
fontsize*64, /* char_height in 1/64th of points */
|
|
|
100, /* horizontal device resolution */
|
|
|
100 ); /* vertical device resolution */
|
|
|
|
|
|
/* A way of accesing the glyph directly */
|
|
|
- FT_GlyphSlot slot = face->glyph; // a small shortcut
|
|
|
+ FT_GlyphSlot slot = font.getFontFace()->glyph; // a small shortcut
|
|
|
|
|
|
/* Does the font file support kerning? */
|
|
|
- use_kerning = FT_HAS_KERNING( face );
|
|
|
+ use_kerning = FT_HAS_KERNING(font.getFontFace());
|
|
|
|
|
|
int n;
|
|
|
for ( n = 0; n < num_chars; n++ )
|
|
|
{
|
|
|
/* Convert character code to glyph index */
|
|
|
- glyph_index = FT_Get_Char_Index( face, ucs4text[n] );
|
|
|
+ glyph_index = FT_Get_Char_Index(font.getFontFace(), ucs4text[n]);
|
|
|
|
|
|
/* Retrieve kerning distance and move pen position */
|
|
|
if ( use_kerning && previous&& glyph_index )
|
|
|
{
|
|
|
FT_Vector delta;
|
|
|
- FT_Get_Kerning( face,
|
|
|
+ FT_Get_Kerning(font.getFontFace(),
|
|
|
previous,
|
|
|
glyph_index,
|
|
|
ft_kerning_default, //FT_KERNING_DEFAULT,
|
|
|
@@ -2085,7 +2076,7 @@ void pngwriter::plot_text_utf8( char * face_path, int fontsize, int x_start, int
|
|
|
}
|
|
|
|
|
|
/* Set transform */
|
|
|
- FT_Set_Transform( face, &matrix, &pen );
|
|
|
+ FT_Set_Transform(font.getFontFace(), &matrix, &pen );
|
|
|
|
|
|
/*set char size*/
|
|
|
|
|
|
@@ -2096,10 +2087,10 @@ void pngwriter::plot_text_utf8( char * face_path, int fontsize, int x_start, int
|
|
|
}
|
|
|
|
|
|
/* Retrieve glyph index from character code */
|
|
|
- glyph_index = FT_Get_Char_Index( face, ucs4text[n] );
|
|
|
+ glyph_index = FT_Get_Char_Index(font.getFontFace(), ucs4text[n]);
|
|
|
|
|
|
/* Load glyph image into the slot (erase previous one) */
|
|
|
- error = FT_Load_Glyph( face, glyph_index, FT_LOAD_DEFAULT );
|
|
|
+ error = FT_Load_Glyph(font.getFontFace(), glyph_index, FT_LOAD_DEFAULT );
|
|
|
if (error) {
|
|
|
std::cerr << " PNGwriter::plot_text_utf8 - ERROR **: FreeType: Could not load glyph (in loop). (FreeType error " << std::hex << error <<")." << std::endl;
|
|
|
std::cerr.copyfmt(std::ios(NULL));
|
|
|
@@ -2108,7 +2099,7 @@ void pngwriter::plot_text_utf8( char * face_path, int fontsize, int x_start, int
|
|
|
}
|
|
|
|
|
|
/* Convert to an anti-aliased bitmap */
|
|
|
- error = FT_Render_Glyph( face->glyph, ft_render_mode_normal );
|
|
|
+ error = FT_Render_Glyph(font.getFontFace()->glyph, ft_render_mode_normal );
|
|
|
if (error) {
|
|
|
std::cerr << " PNGwriter::plot_text_utf8 - ERROR **: FreeType: Render glyph error." << std::endl;
|
|
|
delete[] ucs4text;
|
|
|
@@ -2131,10 +2122,6 @@ void pngwriter::plot_text_utf8( char * face_path, int fontsize, int x_start, int
|
|
|
previous = glyph_index;
|
|
|
}
|
|
|
|
|
|
- /* Free the face and the library objects */
|
|
|
- FT_Done_Face ( face );
|
|
|
- FT_Done_FreeType( library );
|
|
|
-
|
|
|
delete[] ucs4text;
|
|
|
}
|
|
|
|
|
|
@@ -2153,6 +2140,11 @@ void pngwriter::plot_text_utf8( char * face_path, int fontsize, int x_start, int
|
|
|
plot_text_utf8( face_path, fontsize, x_start, y_start, angle, text, ((double) red)/65535.0, ((double) green)/65535.0, ((double) blue)/65535.0 );
|
|
|
}
|
|
|
|
|
|
+void pngwriter::plot_text_utf8(pngwriterfont& font, int fontsize, int x_start, int y_start, double angle, char * text, int red, int green, int blue)
|
|
|
+{
|
|
|
+ plot_text_utf8(font, fontsize, x_start, y_start, angle, text, ((double) red)/65535.0, ((double) green)/65535.0, ((double) blue)/65535.0 );
|
|
|
+}
|
|
|
+
|
|
|
void pngwriter::my_draw_bitmap( FT_Bitmap * bitmap, int x, int y, double red, double green, double blue)
|
|
|
{
|
|
|
double temp;
|
|
|
@@ -2183,7 +2175,7 @@ void pngwriter::my_draw_bitmap( FT_Bitmap * bitmap, int x, int y, double red, do
|
|
|
|
|
|
int pngwriter::get_text_width(char* fontPath, int fontsize, char * text)
|
|
|
{
|
|
|
- pngwriterfont font(font);
|
|
|
+ pngwriterfont font(fontPath);
|
|
|
return get_text_width(font, fontsize, text);
|
|
|
}
|
|
|
|
|
|
@@ -2290,8 +2282,12 @@ int pngwriter::get_text_width(pngwriterfont& font, int fontsize, char * text)
|
|
|
|
|
|
int pngwriter::get_text_width_utf8(char * face_path, int fontsize, char * text)
|
|
|
{
|
|
|
- FT_Library library;
|
|
|
- FT_Face face;
|
|
|
+ pngwriterfont font(font);
|
|
|
+ return get_text_width_utf8(font, fontsize, text);
|
|
|
+}
|
|
|
+
|
|
|
+int pngwriter::get_text_width_utf8(pngwriterfont& font, int fontsize, char * text)
|
|
|
+{
|
|
|
FT_Matrix matrix; // transformation matrix
|
|
|
FT_Vector pen;
|
|
|
|
|
|
@@ -2396,50 +2392,30 @@ int pngwriter::get_text_width_utf8(char * face_path, int fontsize, char * text)
|
|
|
std::cout << "Num chars is: "<< num_chars << std::endl;
|
|
|
*/
|
|
|
|
|
|
- /* Initialize FT Library object */
|
|
|
- error = FT_Init_FreeType( &library );
|
|
|
- if (error) {
|
|
|
- std::cerr << " PNGwriter::get_text_width_utf8 - ERROR **: FreeType: Could not init Library." << std::endl;
|
|
|
- delete[] ucs4text;
|
|
|
- return 0;
|
|
|
- }
|
|
|
-
|
|
|
- /* Initialize FT face object */
|
|
|
- error = FT_New_Face( library,face_path,0,&face );
|
|
|
- if ( error == FT_Err_Unknown_File_Format ) {
|
|
|
- std::cerr << " PNGwriter::get_text_width_utf8 - ERROR **: FreeType: Font was opened, but type not supported." << std::endl;
|
|
|
- delete[] ucs4text;
|
|
|
- return 0;
|
|
|
- } else if (error) {
|
|
|
- std::cerr << " PNGwriter::plot_text - ERROR **: FreeType: Could not find or load font file." << std::endl;
|
|
|
- delete[] ucs4text;
|
|
|
- return 0;
|
|
|
- }
|
|
|
-
|
|
|
/* Set the Char size */
|
|
|
- error = FT_Set_Char_Size( face, /* handle to face object */
|
|
|
+ error = FT_Set_Char_Size( font.getFontFace(), /* handle to face object */
|
|
|
0, /* char_width in 1/64th of points */
|
|
|
fontsize*64, /* char_height in 1/64th of points */
|
|
|
100, /* horizontal device resolution */
|
|
|
100 ); /* vertical device resolution */
|
|
|
|
|
|
/* A way of accesing the glyph directly */
|
|
|
- FT_GlyphSlot slot = face->glyph; // a small shortcut
|
|
|
+ FT_GlyphSlot slot = font.getFontFace()->glyph; // a small shortcut
|
|
|
|
|
|
/* Does the font file support kerning? */
|
|
|
- use_kerning = FT_HAS_KERNING( face );
|
|
|
+ use_kerning = FT_HAS_KERNING(font.getFontFace());
|
|
|
|
|
|
int n;
|
|
|
for ( n = 0; n < num_chars; n++ )
|
|
|
{
|
|
|
/* Convert character code to glyph index */
|
|
|
- glyph_index = FT_Get_Char_Index( face, ucs4text[n] );
|
|
|
+ glyph_index = FT_Get_Char_Index(font.getFontFace(), ucs4text[n]);
|
|
|
|
|
|
/* Retrieve kerning distance and move pen position */
|
|
|
if ( use_kerning && previous&& glyph_index )
|
|
|
{
|
|
|
FT_Vector delta;
|
|
|
- FT_Get_Kerning( face,
|
|
|
+ FT_Get_Kerning(font.getFontFace(),
|
|
|
previous,
|
|
|
glyph_index,
|
|
|
ft_kerning_default, //FT_KERNING_DEFAULT,
|
|
|
@@ -2451,7 +2427,7 @@ int pngwriter::get_text_width_utf8(char * face_path, int fontsize, char * text)
|
|
|
}
|
|
|
|
|
|
/* Set transform */
|
|
|
- FT_Set_Transform( face, &matrix, &pen );
|
|
|
+ FT_Set_Transform(font.getFontFace(), &matrix, &pen );
|
|
|
|
|
|
/*set char size*/
|
|
|
|
|
|
@@ -2462,10 +2438,10 @@ int pngwriter::get_text_width_utf8(char * face_path, int fontsize, char * text)
|
|
|
}
|
|
|
|
|
|
/* Retrieve glyph index from character code */
|
|
|
- glyph_index = FT_Get_Char_Index( face, ucs4text[n] );
|
|
|
+ glyph_index = FT_Get_Char_Index(font.getFontFace(), ucs4text[n]);
|
|
|
|
|
|
/* Load glyph image into the slot (erase previous one) */
|
|
|
- error = FT_Load_Glyph( face, glyph_index, FT_LOAD_DEFAULT );
|
|
|
+ error = FT_Load_Glyph(font.getFontFace(), glyph_index, FT_LOAD_DEFAULT);
|
|
|
if (error) {
|
|
|
std::cerr << " PNGwriter::get_text_width_utf8 - ERROR **: FreeType: Could not load glyph (in loop). (FreeType error " << std::hex << error <<")." << std::endl;
|
|
|
std::cerr.copyfmt(std::ios(NULL));
|
|
|
@@ -2474,7 +2450,7 @@ int pngwriter::get_text_width_utf8(char * face_path, int fontsize, char * text)
|
|
|
}
|
|
|
|
|
|
/* Convert to an anti-aliased bitmap */
|
|
|
- error = FT_Render_Glyph( face->glyph, ft_render_mode_normal );
|
|
|
+ error = FT_Render_Glyph(font.getFontFace()->glyph, ft_render_mode_normal );
|
|
|
if (error) {
|
|
|
std::cerr << " PNGwriter::get_text_width_utf8 - ERROR **: FreeType: Render glyph error." << std::endl;
|
|
|
delete[] ucs4text;
|
|
|
@@ -2497,10 +2473,6 @@ int pngwriter::get_text_width_utf8(char * face_path, int fontsize, char * text)
|
|
|
previous = glyph_index;
|
|
|
}
|
|
|
|
|
|
- /* Free the face and the library objects */
|
|
|
- FT_Done_Face ( face );
|
|
|
- FT_Done_FreeType( library );
|
|
|
-
|
|
|
delete[] ucs4text;
|
|
|
|
|
|
return (int) (((double) pen.x)/64.0);
|