pingus-cvs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Pingus-CVS] r3989 - trunk/fontgen


From: grumbel at BerliOS
Subject: [Pingus-CVS] r3989 - trunk/fontgen
Date: Thu, 12 Mar 2009 19:43:03 +0100

Author: grumbel
Date: 2009-03-12 19:43:02 +0100 (Thu, 12 Mar 2009)
New Revision: 3989

Modified:
   trunk/fontgen/README
   trunk/fontgen/SConstruct
   trunk/fontgen/fontgen.cpp
Log:
Some updates to fontgen

Modified: trunk/fontgen/README
===================================================================
--- trunk/fontgen/README        2009-03-05 03:05:03 UTC (rev 3988)
+++ trunk/fontgen/README        2009-03-12 18:43:02 UTC (rev 3989)
@@ -5,4 +5,19 @@
 into a .pgm along with a text file describing which character maps to
 which coordinates.
 
+Arguments:
+
+TTFFILE: filename of the .ttf (or any other format supported by freetype)
+
+SIZE: size of the font (in pixel?!)
+
+BORDER: an border that is left around each glyph, useful when one
+wants to add effects such as shadow or a black border
+
+IMAGEWIDTH: width of the resulting image
+
+IMAGEHEIGHT: height of the temporary image (ugly, but needed at the moment)
+
+UNICODES: optional list of characters that shall be generated, default is the 
whole font
+
 # EOF #

Modified: trunk/fontgen/SConstruct
===================================================================
--- trunk/fontgen/SConstruct    2009-03-05 03:05:03 UTC (rev 3988)
+++ trunk/fontgen/SConstruct    2009-03-12 18:43:02 UTC (rev 3989)
@@ -3,6 +3,6 @@
 env['CXXFLAGS'] += ['-g', '-O0', '-Wall']
 env['CPPPATH'] += ['-I../pingus/src/']
 env['LIBS']     += ['jpeg']
-env.Program('fontgen', ['fontgen.cpp', 'bitmap.cpp', 
'../pingus/src/utf8_iterator.cpp'])
+env.Program('fontgen', ['fontgen.cpp', 'bitmap.cpp', '../pingus/src/utf8.cpp'])
 
 # EOF #

Modified: trunk/fontgen/fontgen.cpp
===================================================================
--- trunk/fontgen/fontgen.cpp   2009-03-05 03:05:03 UTC (rev 3988)
+++ trunk/fontgen/fontgen.cpp   2009-03-12 18:43:02 UTC (rev 3989)
@@ -8,7 +8,7 @@
 #include <vector>
 #include <iostream>
 #include <fstream>
-#include "utf8_iterator.hpp"
+#include "utf8.hpp"
 
 #include <ft2build.h>
 #include FT_FREETYPE_H
@@ -216,53 +216,124 @@
   FT_Done_Face(face);
 }
   
-int main(int argc, char** argv)
+void print_usage(const char* arg0)
 {
-  if (argc != 6 && argc != 7)
-    {
-      std::cout << "Usage: " << argv[0] << " TTFFILE SIZE BORDER IMAGEWIDTH 
IMAGEHEIGHT [UNICODES]" << std::endl;
-      return EXIT_FAILURE;
-    }
+  std::cout << "Usage: " << arg0 << " generate TTFFILE SIZE BORDER IMAGEWIDTH 
IMAGEHEIGHT [UNICODES]\n"
+            << "       " << arg0 << " listchars TTFFILE" << std::endl;
+  exit(EXIT_FAILURE);
+}
 
-  std::string ttf_filename = argv[1];
-  int  pixel_size   = atoi(argv[2]);
-  int  border       = atoi(argv[3]);
-  int  image_width  = atoi(argv[4]);
-  int  image_height = atoi(argv[5]);
-
-  std::set<FT_ULong> unicodes;
-
-  if (argc == 7)
-    {
-      std::string codes = argv[6];
-      for(UTF8Iterator i(codes); !i.done(); ++i)
-        {
-          unicodes.insert(*i);
-        }
-    }
-
-  std::cout << "Generating image from " << ttf_filename << " with size " << 
pixel_size << " and width " << image_width << std::endl;
-
+void list_chars(const std::string& filename)
+{
   try 
     {
       FT_Error   error;
   
       error = FT_Init_FreeType(&library);
       if (error)
-        throw std::runtime_error("could not initialize FreeType");   
+        {
+          throw std::runtime_error("could not initialize FreeType");   
+        }
+      else
+        {
+          // Read the TTF font file content into buffer
+          std::ifstream fin(filename.c_str());
+          std::istreambuf_iterator<char> first(fin), last;
+          std::vector<char> buffer(first, last); 
 
-      std::vector<Glyph> glyphs;
+          FT_Face face;
+          if (FT_New_Memory_Face(library, 
+                                 reinterpret_cast<FT_Byte*>(&*buffer.begin()), 
buffer.size(), 
+                                 0, &face))
+            {
+              throw std::runtime_error("Couldn't load font: '" + filename + 
"'");
+            }
+      
+          FT_Set_Pixel_Sizes(face, 12, 12);
 
-      generate_font(ttf_filename, pixel_size, unicodes, glyphs);
-      generate_image(glyphs, pixel_size, border, image_width, image_height, 
"/tmp/out.pgm", "/tmp/out.font");
+          {
+            FT_Error   error;
+            error = FT_Select_Charmap(face,  FT_ENCODING_UNICODE);
+            if (error)
+              {
+                std::cout << "Error: Couldn't set Unicode charmap" << 
std::endl;
+                exit(EXIT_FAILURE);
+              }
+          }
+
+          FT_UInt   glyph_index = 0;                                           
     
+          FT_ULong  charcode = FT_Get_First_Char( face, &glyph_index );
+          while ( glyph_index != 0 )                                           
 
+            {
+              std::cout << unicode_to_utf8(charcode) << std::endl;
+              charcode = FT_Get_Next_Char( face, charcode, &glyph_index );
+            }
+  
+          FT_Done_Face(face);
         
-      FT_Done_FreeType(library);
+          FT_Done_FreeType(library);
+        }
     } 
   catch(std::exception& err)
     {
       std::cout << "Error: " << err.what() << std::endl;
+    }      
+}
+
+int main(int argc, char** argv)
+{
+  if (argc == 3 && strcmp(argv[1], "listchars") == 0)
+    {
+      list_chars(argv[2]);
     }
+  else if ((argc == 7 || argc == 8) &&
+           strcmp(argv[1], "generate") == 0)
+    {
+      std::string ttf_filename = argv[2];
+      int  pixel_size   = atoi(argv[3]);
+      int  border       = atoi(argv[4]);
+      int  image_width  = atoi(argv[5]);
+      int  image_height = atoi(argv[6]);
 
+      std::set<FT_ULong> unicodes;
+
+      if (argc == 8)
+        {
+          std::string codes = argv[7];
+          UTF8::iterator i(codes);
+          while(i.next())
+            {
+              unicodes.insert(*i);
+            }
+        }
+
+      std::cout << "Generating image from " << ttf_filename << " with size " 
<< pixel_size << " and width " << image_width << std::endl;
+
+      try 
+        {
+          FT_Error   error;
+  
+          error = FT_Init_FreeType(&library);
+          if (error)
+            throw std::runtime_error("could not initialize FreeType");   
+
+          std::vector<Glyph> glyphs;
+
+          generate_font(ttf_filename, pixel_size, unicodes, glyphs);
+          generate_image(glyphs, pixel_size, border, image_width, 
image_height, "/tmp/out.pgm", "/tmp/out.font");
+        
+          FT_Done_FreeType(library);
+        } 
+      catch(std::exception& err)
+        {
+          std::cout << "Error: " << err.what() << std::endl;
+        }    
+    }
+  else
+    {
+      print_usage(argv[0]);
+    }
+
   return 0;
 }
   





reply via email to

[Prev in Thread] Current Thread [Next in Thread]