TrueTypeFont.cpp

Go to the documentation of this file.
00001 ////////////////////////////////////////////////////////////////////////////////
00002 //    Scorched3D (c) 2000-2003
00003 //
00004 //    This file is part of Scorched3D.
00005 //
00006 //    Scorched3D is free software; you can redistribute it and/or modify
00007 //    it under the terms of the GNU General Public License as published by
00008 //    the Free Software Foundation; either version 2 of the License, or
00009 //    (at your option) any later version.
00010 //
00011 //    Scorched3D is distributed in the hope that it will be useful,
00012 //    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 //    GNU General Public License for more details.
00015 //
00016 //    You should have received a copy of the GNU General Public License
00017 //    along with Scorched3D; if not, write to the Free Software
00018 //    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019 ////////////////////////////////////////////////////////////////////////////////
00020 
00021 #include <wx/utils.h>
00022 #include <wxdialogs/TrueTypeFont.h>
00023 #include <common/Defines.h>
00024 
00025 TrueTypeFont::TrueTypeFont(const std::string &typeFace, unsigned int h)
00026 {
00027         // Create And Initilize A FreeType Font Library.
00028         if (FT_Init_FreeType(&library)) 
00029         {
00030                 return;
00031         }
00032 
00033         // This Is Where We Load In The Font Information From The File.
00034         // Of All The Places Where The Code Might Die, This Is The Most Likely,
00035         // As FT_New_Face Will Fail If The Font File Does Not Exist Or Is Somehow Broken.
00036         if (FT_New_Face(library, typeFace.c_str(), 0, &face)) 
00037         {
00038                 return;
00039         }
00040         
00041         // For Some Twisted Reason, FreeType Measures Font Size
00042         // In Terms Of 1/64ths Of Pixels.  Thus, To Make A Font
00043         // h Pixels High, We Need To Request A Size Of h*64.
00044         // (h << 6 Is Just A Prettier Way Of Writing h*64)
00045         FT_Set_Char_Size(face, h << 6, h << 6, 96, 96);
00046 
00047         // This Is Where We Actually Create Each Of The Fonts Characters
00048         for(unsigned char i=0;i<128;i++)
00049         {
00050                 createCharacter(face, i);
00051         }
00052 }
00053 
00054 TrueTypeFont::~TrueTypeFont()
00055 {
00056         // We Don't Need The Face Information Now That The Display
00057         // Lists Have Been Created, So We Free The Assosiated Resources.
00058         FT_Done_Face(face);
00059 
00060         // Ditto For The Font Library.
00061         FT_Done_FreeType(library);
00062 }
00063 
00064 bool TrueTypeFont::createCharacter(FT_Face face, unsigned char ch)
00065 {
00066         // The First Thing We Do Is Get FreeType To Render Our Character
00067         // Into A Bitmap.  This Actually Requires A Couple Of FreeType Commands:
00068 
00069         // Load The Glyph For Our Character.
00070         if(FT_Load_Glyph( face, FT_Get_Char_Index( face, ch ), FT_LOAD_DEFAULT )) 
00071         { 
00072                 return false; 
00073         }
00074 
00075         // Move The Face's Glyph Into A Glyph Object.
00076         if(FT_Get_Glyph( face->glyph, &glyphs[ch] ))
00077         { 
00078                 return false; 
00079         }
00080 
00081         // Convert The Glyph To A Bitmap.
00082         FT_Glyph_To_Bitmap( &glyphs[ch], ft_render_mode_normal, 0, 1 );
00083         FT_BitmapGlyph bitmap_glyph = (FT_BitmapGlyph) glyphs[ch];
00084 
00085         // This Reference Will Make Accessing The Bitmap Easier.
00086         bitmaps[ch]= &bitmap_glyph->bitmap;
00087 
00088         return true;
00089 }
00090 
00091 bool TrueTypeFont::getImageForText(const std::string &text, wxImage &image)
00092 {
00093         int width = 0;
00094         int height = 0;
00095         int topheight = 0;
00096 
00097         // Find out the total image height and width
00098         FT_GlyphSlot  slot = face->glyph; 
00099         for (const char *c=text.c_str(); *c; c++)
00100         {
00101                 FT_Load_Char(face, *c, FT_LOAD_RENDER);
00102                 FT_Bitmap &bitmap = slot->bitmap;
00103 
00104                 width += slot->advance.x >> 6;
00105                 if (bitmap.rows > height) height = bitmap.rows;
00106                 if (bitmap.rows - slot->bitmap_top > topheight) 
00107                         topheight = bitmap.rows - slot->bitmap_top;
00108         }
00109 
00110         // Create the image
00111         image.Create(width, height + topheight);
00112 
00113         // For each char
00114         int posx = 0;
00115         int posy = 0;
00116         for (const char *c=text.c_str(); *c; c++)
00117         {
00118                 FT_Load_Char( face, *c, FT_LOAD_RENDER);
00119                 FT_Bitmap &bitmap = slot->bitmap;
00120 
00121                 int x = slot->bitmap_left;
00122                 int y = height - slot->bitmap_top;
00123 
00124                 for (int j=0; j<bitmap.rows; j++)
00125                 {
00126                         for (int i=0; i<bitmap.width; i++)
00127                         {
00128                                 int rx = (i + posx + x);
00129                                 int ry = (j + posy + y);
00130 
00131                                 if (rx >= image.GetWidth() || rx < 0) continue;
00132                                 if (ry >= image.GetHeight() || ry < 0) continue;
00133 
00134                                 unsigned char *dest =
00135                                         image.GetData() + ((ry * image.GetWidth()) + rx) * 3;
00136                                 unsigned char src = bitmap.buffer[i + bitmap.width * j];
00137 
00138                                 src = (unsigned int) (float(src) * 0.9f);
00139 
00140 
00141                                 dest[0] = dest[1] = dest[2] = src;
00142                         }
00143                 }
00144 
00145                 posx += slot->advance.x >> 6;
00146         }
00147 
00148         return true;
00149 }

Generated on Mon Feb 16 15:14:52 2009 for Scorched3D by  doxygen 1.5.3