fontgen.cc

Go to the documentation of this file.
00001 
00007 /*
00008 Copyright (C) 2002  The Exult Team
00009 
00010 This program is free software; you can redistribute it and/or
00011 modify it under the terms of the GNU General Public License
00012 as published by the Free Software Foundation; either version 2
00013 of the License, or (at your option) any later version.
00014 
00015 This program is distributed in the hope that it will be useful,
00016 but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018 GNU General Public License for more details.
00019 
00020 You should have received a copy of the GNU General Public License
00021 along with this program; if not, write to the Free Software
00022 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00023 */
00024 
00025 #ifdef HAVE_CONFIG_H
00026 #  include <config.h>
00027 #endif
00028 
00029 #if defined(HAVE_FREETYPE2) && !defined(__zaurus__)
00030 
00031 #include <ft2build.h>
00032 #include FT_FREETYPE_H
00033 
00034 #include <string.h>
00035 #include "vgafile.h"
00036 
00037 /*
00038  *  Generate a shadow around a character.
00039  */
00040 
00041 static void Gen_shadow
00042   (
00043   unsigned char *pixels,
00044   int w, int h,     // Dimensions.
00045   unsigned char fg,   // Foreground color index.
00046   unsigned char shadow    // Shadow color index
00047   )
00048   {
00049   int r, c;
00050 
00051   for (r = 0; r < h; r++)
00052     for (c = 0; c < w; c++)
00053       {
00054       if (pixels[r*w+c] != fg)
00055         continue;
00056       int rr, cc; // Fill surrounding pixels;
00057       for (rr = r - 1; rr <= r + 1; rr++)
00058         {
00059         if (rr < 0 || rr >= h)
00060           continue;
00061         for (cc = c - 1; cc <= c + 1; cc++)
00062           if (cc >= 0 && cc < w &&
00063               pixels[rr*w+cc] != fg)
00064             pixels[rr*w+cc] = shadow;
00065         }
00066       }
00067   }
00068 
00069 /*
00070  *  Fill a shape with each frame containing the glyph for its ASCII
00071  *  code.  The shape has 128 frames.
00072  *
00073  *  Output: True if successful, false if error.
00074  */
00075 
00076 bool Gen_font_shape
00077   (
00078   Shape *shape,     // Shape to set frames.
00079   const char *fontfile,   // Filename of font.
00080   int nframes,      // # frames to generate, starting at 0.
00081   int pixels_ht,      // Desired height in pixels.
00082   unsigned char fg,   // Foreground color index.
00083   unsigned char bg,   // Background color index.
00084   int shadow      // Shadow color, or -1
00085   )
00086   {
00087   FT_Library library;   // Initialize.
00088   int error = FT_Init_FreeType(&library);
00089   if (error)
00090     return false;
00091   FT_Face face;     // Gets the font.
00092   error = FT_New_Face(library, fontfile, 0, &face);
00093   if (error)
00094     return false;
00095   error = FT_Set_Pixel_Sizes(face, 0, pixels_ht);
00096           // Glyphs are rendered here:
00097   FT_GlyphSlot glyph = face->glyph;
00098   if (error)
00099     return false;
00100   shape->resize(nframes);   // Make it big enough.
00101   for (int chr = 0; chr < nframes; chr++)
00102     {     // Get each glyph.
00103     error = FT_Load_Char(face, chr, 
00104         FT_LOAD_RENDER|FT_LOAD_MONOCHROME);
00105     if (error)
00106       {
00107       //+++++Do we need to store an empty frame?
00108       continue;
00109       }
00110     int w = glyph->bitmap.width, h = glyph->bitmap.rows;
00111     int sw = w, sh = h; // Shape width/height.
00112     int offset = 0;   // Starting row, col.
00113     if (!sw)    // 0 width (like for a space)?
00114       sw = glyph->metrics.horiAdvance/64; // Guessin...
00115     if (!sh)
00116       sh = glyph->metrics.vertAdvance/64;
00117     if (shadow != -1) // Make room for shadow.
00118       {
00119       sw += 2;
00120       sh += 2;
00121       offset = 1;
00122       }
00123           // Allocate our buffer.
00124     int cnt = sw*sh;  // Total #pixels.
00125     unsigned char *pixels = new unsigned char[cnt];
00126     memset(pixels, bg, cnt);// Fill with background.
00127           // I believe this is 1 bit/pixel:
00128     unsigned char *src = glyph->bitmap.buffer;
00129     unsigned char *dest = pixels + offset*sw;
00130     for (int row = 0; row < h; row++)
00131       {
00132       for (int b = 0; b < w; b++)
00133         if (src[b/8]&(0x80>>(b%8)))
00134           dest[offset + b] = fg;
00135       dest += sw; // Advance to next row.
00136       src += glyph->bitmap.pitch;
00137       }
00138     if (shadow >= 0)
00139       Gen_shadow(pixels, sw, sh, fg, (unsigned char) shadow);
00140           // Not sure about dims here+++++
00141     Shape_frame *frame = new Shape_frame(pixels,
00142       sw, sh, glyph->bitmap_left + offset, 
00143         glyph->bitmap_top + offset, true);
00144     delete pixels;
00145     shape->set_frame(frame, chr);
00146     }
00147   FT_Done_FreeType(library);
00148   return true;
00149   }
00150 
00151 #endif  /* HAVE_FREETYPE2 */
00152 

Generated on Mon Jul 9 14:42:50 2007 for ExultEngine by  doxygen 1.5.1