Browse Source

write in UTF8

isundil 6 years ago
parent
commit
fe8beacd06

+ 1 - 0
Makefile

@@ -8,6 +8,7 @@ CP=	cp
 CXX=	g++
 
 CXXFLAGS=	-g3 \
+			-std=c++11 \
 			-I${ANOPE_SRC}/include -I${ANOPE_SRC}/build/include \
 			-I dependencies/isundil-pngwriter/ \
 			-I /usr/include/freetype2/ \

+ 44 - 72
dependencies/isundil-pngwriter/pngwriter.cc

@@ -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);

+ 4 - 0
dependencies/isundil-pngwriter/pngwriter.h

@@ -86,6 +86,7 @@ class pngwriterfont
 {
     public:
         pngwriterfont(const char *path, std::string& error);
+        pngwriterfont(const char *path);
         ~pngwriterfont();
 
         class LoadingError: public std::exception
@@ -476,6 +477,8 @@ class pngwriter
     * Tip: The quickest way to get a string into UTF-8 is to write it in an adequate text editor, and save it as a file
     * in UTF-8 encoding, which can then be read in in binary mode.
     * */
+   void plot_text_utf8(pngwriterfont& font, int fontsize, int x_start, int y_start, double angle, char * text, double red, double green, double blue);
+   void plot_text_utf8(pngwriterfont& font, int fontsize, int x_start, int y_start, double angle, char * text, int red, int green, int blue);
    void plot_text_utf8(char * face_path, int fontsize, int x_start, int y_start, double angle, char * text, double red, double green, double blue);
    void plot_text_utf8(char * face_path, int fontsize, int x_start, int y_start, double angle, char * text, int red, int green, int blue);
 
@@ -753,6 +756,7 @@ class pngwriter
    int static get_text_width(pngwriterfont &font, int fontsize,  char * text);
    int static get_text_width(char * face_path, int fontsize,  char * text);
 
+   int static get_text_width_utf8(pngwriterfont &font, int fontsize, char * text);
    int static get_text_width_utf8(char * face_path, int fontsize, char * text);
 
 

+ 4 - 4
src/commands/is_create.cpp

@@ -138,7 +138,7 @@ ImageWriter& ImageWriter::SetQuotePosition(size_t top, size_t left, size_t right
 
 int ImageWriter::GetTextLength(pngwriter &writer, pngwriterfont &font, char fontSize, char *str, int maxWidth) const
 {
-	int w = writer.get_text_width(font, fontSize, str);
+	int w = writer.get_text_width_utf8(font, fontSize, str);
 	if (w <= maxWidth)
 		return strlen(str);
 	return (int) (strlen(str) * ((double) maxWidth / (double) w));
@@ -150,20 +150,20 @@ void ImageWriter::WriteText(pngwriter &writer, pngwriterfont &font, char fontSiz
 {
 	char tmp = str[strTo];
 	str[strTo] = 0;
-	writer.plot_text(font, fontSize, px, py, 0, str +strFrom, rgb.r, rgb.g, rgb.b);
+	writer.plot_text_utf8(font, fontSize, px, py, 0, str +strFrom, rgb.r, rgb.g, rgb.b);
 	str[strTo] = tmp;
 }
 
 int ImageWriter::WriteText(pngwriter &writer, pngwriterfont& font, char fontSize, const Anope::string &str, int lineIndex, const RGB &rgb)
 {
 	const char lineHeight = fontSize +6,
-		  marginSize = fontSize;
+		  marginSize = fontSize * 0.45;
 	char *cstrFull = strdup(str.c_str());
 	int maxLen = GetTextLength(writer, font, fontSize, cstrFull, right -left -2*marginSize);
 	int written = 0;
 
 	do {
-		WriteText(writer, font, fontSize, cstrFull, written, written +maxLen, left +marginSize, top -marginSize -(lineHeight * lineIndex) -(fontSize), rgb);
+		WriteText(writer, font, fontSize, cstrFull, written, written +maxLen, left +marginSize, top -marginSize -((lineHeight +marginSize) * lineIndex) -(fontSize), rgb);
 		written += maxLen;
 		++lineIndex;
 	} while (written < str.length());