Rendering Text with PNGwriter


To show off PNGwriter's text rendering abilities, I'll show you how to create the following image:

Text rendered with PNGwriter

Click here for the original high-resolution PNG version.




Note that the entire image has been generated from scratch, using only PNGwriter's internal drawing functions, 5 text files in UTF-8 encoding containing the strings to be rendered, and a few different TrueType font files.

The strings were (If you cannot see the strings, you may lack the adequate fonts to display non-Western character sets):

  • 執筆

  • PNG

  • ピエンジライターは

  • 便利です!

  • Я люблю мать Россию!



The command-line arguments used to run the program were:

./jap fonts/kochi-mincho.ttf file1.txt fonts/texsbi.ttf russ.txt fonts/tallpaul.ttf file3.txt fonts/kochi-mincho.ttf file4.txt fonts/kochi-mincho.ttf file5.txt



This is the program:

#include <stdlib.h>
#include <pngwriter.h>
#include <iostream>
#include <fstream>
using namespace std;

int main(int argc, char * argv[])
{
   if(argc==1)
     {
	std::cout << "Font, textfile name1, 2, 3, 4, 5, font2, font 3, etc\n";
     }

   std::cout << "Going to do 1:\n";
   char * filename1 = argv[2];
   char * buffer;
   long size;
   ifstream file (filename1, ios::in|ios::binary|ios::ate);
   size = file.tellg();
   std::cout << "Size:" << size << "\n";
   file.seekg (0, ios::beg);
   buffer = new char [size];
   file.read (buffer, size);
   file.close();

   std::cout << "Going to do 2:\n";
   char * filename2 = argv[4];
   char * buffer2;
   ifstream file2 (filename2, ios::in|ios::binary|ios::ate);
   size = file2.tellg();
   std::cout << "Size:" << size << "\n";
   file2.seekg (0, ios::beg);
   buffer2 = new char [size];
   file2.read (buffer2, size);
   file2.close();

   std::cout << "Going to do 3:\n";
   char * filename3 = argv[6];
   char * buffer3;
   ifstream file3 (filename3, ios::in|ios::binary|ios::ate);
   size = file3.tellg();
   std::cout << "Size:" << size << "\n";
   file3.seekg (0, ios::beg);
   buffer3 = new char [size];
   file3.read (buffer3, size);
   file3.close();
   
   std::cout << "Going to do 4:\n";
   char * filename4 = argv[8];
   char * buffer4;
   ifstream file4 (filename4, ios::in|ios::binary|ios::ate);
   size = file4.tellg();
      std::cout << "Size:" << size << "\n";
   file4.seekg (0, ios::beg);
   buffer4 = new char [size];
   file4.read (buffer4, size);
   file4.close();

   std::cout << "Going to do 5:\n";
   char * filename5 = argv[10];
   char * buffer5;
   ifstream file5 (filename5, ios::in|ios::binary|ios::ate);
   size = file5.tellg();
   std::cout << "Size:" << size << "\n";
   file5.seekg (0, ios::beg);
   buffer5 = new char [size];
   file5.read (buffer5, size);
   file5.close();


   double gradstart=0.0;
   double gradend=0.3;
   
   //Creating the PNGwriter instance
   pngwriter image(1000,1000,1.0,"out.png");

   //Creating the background gradient
   for(int h=1;h<=1000;h++)
     {
	image.line(h,1,h,1000,1.0 -(gradend - gradstart)*h/1000, 1.0 -(gradend - gradstart)*h/1000, 1.0);
     }
	  
   //Creating the yellow rectangle
   image.filledsquare(1,850,1000,950,1.0,1.0,0.0);

   //This is the text that says PNG in pink, rotated.
   image.plot_text_utf8(argv[5],800,300,-100,0.76, buffer3,1.0,0.3,1.0);

   //This is the text in Russian.
   image.plot_text_utf8(argv[3],60,25,870,0.0, buffer2,0.0,0.0,1.0);
   
   //This is the translucent rectangle behind the Japanese phrase.
   for(int m=300; m<=1000;m++)
     {
	for(int n= 400;n<=690;n++)
	  {
	     image.plot_blend(m,n,0.7,1.0,0.4,0.0);
	  }
	
     }
   
   // These two are the japanese phrases, in Katakana and Hiragana.
   image.plot_text_utf8(argv[7],50,310,503,0.2, buffer4,0.7,0.0,0.0);
   image.plot_text_utf8(argv[9],50,315,430,0.2, buffer5,0.7,0.0,0.0);

   //This is the blue rectangle behind the kanji.
   for(int m=1; m<=750;m++)
     {
	for(int n= 1;n<=350;n++)
	  {
	     image.plot_blend(m,n,0.6,0.0,0.0,1.0);
	  }
	
     }

   //This writes the kanji in the lower left corner.
   image.plot_text_utf8(argv[1],200,50,50,0.0, buffer,0.0,0.0,0.0);
   
   image.close();
   delete[] buffer;
   system("open out.png");
   return 0;
}





© 2002, 2003, 2004, 2005, 2006, 2007 Paul Blackburn