isundil 6 жил өмнө
parent
commit
f0456b7557

+ 13 - 13
dependencies/isundil-pngwriter/pngwriter.cc

@@ -1929,13 +1929,13 @@ 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)
+void pngwriter::plot_text_utf8( char * face_path, int fontsize, int x_start, int y_start, double angle,  const char * text, double red, double green, double blue)
 {
     pngwriterfont font(face_path);
-    plot_text(font, fontsize, x_start, y_start, angle, text, red, green, blue);
+    plot_text_utf8(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)
+void pngwriter::plot_text_utf8(pngwriterfont& font, int fontsize, int x_start, int y_start, double angle, const char * text, double red, double green, double blue)
 {
    FT_Matrix   matrix;      // transformation matrix
    FT_Vector   pen;
@@ -2135,12 +2135,12 @@ void pngwriter::plot_text( char * face_path, int fontsize, int x_start, int y_st
    plot_text( 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( char * face_path, int fontsize, int x_start, int y_start, double angle, char * text, int red, int green, int blue)
+void pngwriter::plot_text_utf8( char * face_path, int fontsize, int x_start, int y_start, double angle, const char * text, int red, int green, int blue)
 {
    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)
+void pngwriter::plot_text_utf8(pngwriterfont& font, int fontsize, int x_start, int y_start, double angle, const 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   );
 }
@@ -2173,13 +2173,13 @@ void pngwriter::my_draw_bitmap( FT_Bitmap * bitmap, int x, int y, double red, do
 
 //put in freetype section
 
-int pngwriter::get_text_width(char* fontPath, int fontsize, char * text)
+int pngwriter::get_text_width(char* fontPath, int fontsize, const char * text)
 {
     pngwriterfont font(fontPath);
     return get_text_width(font, fontsize, text);
 }
 
-int pngwriter::get_text_width(pngwriterfont& font, int fontsize, char * text)
+int pngwriter::get_text_width(pngwriterfont& font, int fontsize, const char * text)
 {
    FT_Matrix   matrix;      // transformation matrix
    FT_Vector   pen;
@@ -2280,13 +2280,13 @@ int pngwriter::get_text_width(pngwriterfont& font, int fontsize, char * text)
 }
 
 
-int pngwriter::get_text_width_utf8(char * face_path, int fontsize,  char * text)
+int pngwriter::get_text_width_utf8(char * face_path, int fontsize, const char * text)
 {
     pngwriterfont font(font);
     return get_text_width_utf8(font, fontsize, text);
 }
 
-int pngwriter::get_text_width_utf8(pngwriterfont& font, int fontsize,  char * text)
+int pngwriter::get_text_width_utf8(pngwriterfont& font, int fontsize, const char * text)
 {
    FT_Matrix   matrix;      // transformation matrix
    FT_Vector   pen;
@@ -2495,27 +2495,27 @@ void pngwriter::plot_text( char *, int, int, int, double, char *, double, double
 
 }
 
-void pngwriter::plot_text_utf8( char *, int, int, int, double, char *, int, int, int )
+void pngwriter::plot_text_utf8( char *, int, int, int, double, const char *, int, int, int )
 {
    std::cerr << " PNGwriter::plot_text_utf8 - ERROR **:  PNGwriter was compiled without Freetype support! Recompile PNGwriter with Freetype support (once you have Freetype installed, that is. Websites: www.freetype.org and pngwriter.sourceforge.net)." << std::endl;
    return;
 }
 
-void pngwriter::plot_text_utf8( char *, int, int, int, double, char *, double, double, double)
+void pngwriter::plot_text_utf8( char *, int, int, int, double, const char *, double, double, double)
 {
    std::cerr << " PNGwriter::plot_text_utf8 - ERROR **:  PNGwriter was compiled without Freetype support! Recompile PNGwriter with Freetype support (once you have Freetype installed, that is. Websites: www.freetype.org and pngwriter.sourceforge.net)." << std::endl;
    return;
 }
 
 //////////// Get text width
-int pngwriter::get_text_width(char *, int, char *)
+int pngwriter::get_text_width(char *, int, const char *)
 {
    std::cerr << " PNGwriter::get_text_width - ERROR **:  PNGwriter was compiled without Freetype support! Recompile PNGwriter with Freetype support (once you have Freetype installed, that is. Websites: www.freetype.org and pngwriter.sourceforge.net)." << std::endl;
    return 0;
 }
 
 
-int pngwriter::get_text_width_utf8(char *, int,  char *)
+int pngwriter::get_text_width_utf8(char *, int, const char *)
 {
    std::cerr << " PNGwriter::get_text_width_utf8 - ERROR **:  PNGwriter was compiled without Freetype support! Recompile PNGwriter with Freetype support (once you have Freetype installed, that is. Websites: www.freetype.org and pngwriter.sourceforge.net)." << std::endl;
    return 0;

+ 8 - 8
dependencies/isundil-pngwriter/pngwriter.h

@@ -477,10 +477,10 @@ 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);
+   void plot_text_utf8(pngwriterfont& font, int fontsize, int x_start, int y_start, double angle, const char * text, double red, double green, double blue);
+   void plot_text_utf8(pngwriterfont& font, int fontsize, int x_start, int y_start, double angle, const 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, const 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, const char * text, int red, int green, int blue);
 
 
    /* Bilinear Interpolation of Image
@@ -753,11 +753,11 @@ class pngwriter
     *     4  (x_start - size*sin(angle), y_start + size*cos(angle))
     * */
 
-   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(pngwriterfont &font, int fontsize,  const char * text);
+   int static get_text_width(char * face_path, int fontsize,  const 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);
+   int static get_text_width_utf8(pngwriterfont &font, int fontsize, const char * text);
+   int static get_text_width_utf8(char * face_path, int fontsize, const char * text);
 
 
 };

+ 55 - 25
src/commands/is_create.cpp

@@ -46,6 +46,43 @@ ARGB::ARGB(long value): RGB(value)
 	a = ((unsigned char)(value >> 24) / 255.d);
 }
 
+struct TextItem {
+	std::string text;
+	int px;
+	int py;
+	int width;
+	int height;
+
+	TextItem(pngwriter &writer, pngwriterfont& font, const std::string& str, int strFrom, int strTo, int px, int py, int fontSize)
+	{
+		text = str.substr(strFrom, strTo -strFrom);
+		this->px = px;
+		this->py = py;
+		width = writer.get_text_width_utf8(font, fontSize, str.c_str());
+		height = fontSize;
+	}
+};
+
+class TextLines
+{
+	public:
+		TextLines(int lineSpacingHeight): lineSpacing(lineSpacingHeight){}
+
+		void push_back(const TextItem& line)
+		{
+			lines.push_back(line);
+			height += line.height;
+		}
+
+		std::vector<TextItem> lines;
+		int getHeight() { return height + lineSpacing*lines.size(); }
+		int getLineSpacing() { return lineSpacing; }
+
+	private:
+		int height = {};
+		const int lineSpacing;
+};
+
 class ImageWriter
 {
 public:
@@ -73,9 +110,9 @@ protected:
 	char fontSize;
 	Anope::string fontPath;
 
-	void WriteText(pngwriter &writer, pngwriterfont &font, char fontSize, char *str, int strFrom, int strTo, int px, int py, const RGB &rgb) const;
-	int WriteText(pngwriter &writer, pngwriterfont &font, char fontSize, const Anope::string &str, int lineIndex, const RGB &rgb);
-	int GetTextLength(pngwriter &writer, pngwriterfont &font, char fontSize, char *str, int maxWidth) const;
+	void WriteText(pngwriter &writer, pngwriterfont &font, char fontSize, const TextItem& line, int offsetY, const RGB &rgb) const;
+	void WriteDryRun(pngwriter &writer, pngwriterfont& font, char fontSize, const Anope::string &str, TextLines& lines, const RGB &rgb);
+	int GetTextLength(pngwriter &writer, pngwriterfont &font, char fontSize, const std::string &str, int maxWidth) const;
 };
 
 ImageWriter& ImageWriter::SetFontSize(char value)
@@ -136,40 +173,29 @@ ImageWriter& ImageWriter::SetQuotePosition(size_t top, size_t left, size_t right
 	return *this;
 }
 
-int ImageWriter::GetTextLength(pngwriter &writer, pngwriterfont &font, char fontSize, char *str, int maxWidth) const
+int ImageWriter::GetTextLength(pngwriter &writer, pngwriterfont &font, char fontSize, const std::string &str, int maxWidth) const
 {
-	int w = writer.get_text_width_utf8(font, fontSize, str);
+	int w = writer.get_text_width_utf8(font, fontSize, str.c_str());
 	if (w <= maxWidth)
-		return strlen(str);
-	return (int) (strlen(str) * ((double) maxWidth / (double) w));
+		return str.size();
+	return (int) (str.size() * ((double) maxWidth / (double) w));
 }
 
-void ImageWriter::WriteText(pngwriter &writer, pngwriterfont &font, char fontSize,
-		char *str, int strFrom, int strTo,
-		int px, int py, const RGB &rgb) const
+void ImageWriter::WriteText(pngwriter &writer, pngwriterfont &font, char fontSize, const TextItem& line, int offsetY, const RGB &rgb) const
 {
-	char tmp = str[strTo];
-	str[strTo] = 0;
-	writer.plot_text_utf8(font, fontSize, px, py, 0, str +strFrom, rgb.r, rgb.g, rgb.b);
-	str[strTo] = tmp;
+	writer.plot_text_utf8(font, fontSize, line.px, offsetY +line.py, 0, const_cast<char*>(line.text.c_str()), rgb.r, rgb.g, rgb.b);
 }
 
-int ImageWriter::WriteText(pngwriter &writer, pngwriterfont& font, char fontSize, const Anope::string &str, int lineIndex, const RGB &rgb)
+void ImageWriter::WriteDryRun(pngwriter &writer, pngwriterfont& font, char fontSize, const Anope::string &str, TextLines& lines, const RGB &rgb)
 {
-	const char lineHeight = fontSize +6,
-		  marginSize = fontSize * 0.45;
-	char *cstrFull = strdup(str.c_str());
-	int maxLen = GetTextLength(writer, font, fontSize, cstrFull, right -left -2*marginSize);
+	const char marginSize = lines.getLineSpacing();
+	int maxLen = GetTextLength(writer, font, fontSize, str.c_str(), right -left -2*marginSize);
 	int written = 0;
 
 	do {
-		WriteText(writer, font, fontSize, cstrFull, written, written +maxLen, left +marginSize, top -marginSize -((lineHeight +marginSize) * lineIndex) -(fontSize), rgb);
+		lines.push_back(TextItem(writer, font, str.c_str(), written, written +maxLen, left +marginSize, top -marginSize -lines.getHeight() -fontSize, fontSize));
 		written += maxLen;
-		++lineIndex;
 	} while (written < str.length());
-
-	free(cstrFull);
-	return lineIndex;
 }
 
 bool ImageWriter::Build(User *u)
@@ -194,8 +220,12 @@ bool ImageWriter::Build(User *u)
 			bgColor.a, bgColor.r, bgColor.g, bgColor.b);
 
 	size_t i =0;
+	TextLines lines(fontSize * 0.55);
 	for (const Anope::string& str: text)
-		i = WriteText(writer, font, fontSize, str, i, fgColor);
+		WriteDryRun(writer, font, fontSize, str, lines, fgColor);
+	int offsetY = lines.getHeight() > (bottom -top) ? 0 : (bottom -lines.getHeight() +lines.getLineSpacing()) /2;
+	for (const TextItem& line: lines.lines)
+		WriteText(writer, font, fontSize, line, offsetY, fgColor);
 	writer.close();
 	chmod(outputName.c_str(), 0644);
 	return true;