SimpleFont.C

Go to the documentation of this file.
00001 /*! @file Image/SimpleFont.C simple raster font definition */
00002 
00003 // //////////////////////////////////////////////////////////////////// //
00004 // The iLab Neuromorphic Vision C++ Toolkit - Copyright (C) 2000-2005   //
00005 // by the University of Southern California (USC) and the iLab at USC.  //
00006 // See http://iLab.usc.edu for information about this project.          //
00007 // //////////////////////////////////////////////////////////////////// //
00008 // Major portions of the iLab Neuromorphic Vision Toolkit are protected //
00009 // under the U.S. patent ``Computation of Intrinsic Perceptual Saliency //
00010 // in Visual Environments, and Applications'' by Christof Koch and      //
00011 // Laurent Itti, California Institute of Technology, 2001 (patent       //
00012 // pending; application number 09/912,225 filed July 23, 2001; see      //
00013 // http://pair.uspto.gov/cgi-bin/final/home.pl for current status).     //
00014 // //////////////////////////////////////////////////////////////////// //
00015 // This file is part of the iLab Neuromorphic Vision C++ Toolkit.       //
00016 //                                                                      //
00017 // The iLab Neuromorphic Vision C++ Toolkit is free software; you can   //
00018 // redistribute it and/or modify it under the terms of the GNU General  //
00019 // Public License as published by the Free Software Foundation; either  //
00020 // version 2 of the License, or (at your option) any later version.     //
00021 //                                                                      //
00022 // The iLab Neuromorphic Vision C++ Toolkit is distributed in the hope  //
00023 // that it will be useful, but WITHOUT ANY WARRANTY; without even the   //
00024 // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      //
00025 // PURPOSE.  See the GNU General Public License for more details.       //
00026 //                                                                      //
00027 // You should have received a copy of the GNU General Public License    //
00028 // along with the iLab Neuromorphic Vision C++ Toolkit; if not, write   //
00029 // to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,   //
00030 // Boston, MA 02111-1307 USA.                                           //
00031 // //////////////////////////////////////////////////////////////////// //
00032 
00033 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/Image/SimpleFont.C $
00034 // $Id: SimpleFont.C 10285 2008-09-23 18:23:28Z rand $
00035 
00036 
00037 #include "Image/SimpleFont.H"
00038 
00039 #include "Util/Assert.H"
00040 #include "Util/log.H"
00041 
00042 #include <sstream>
00043 
00044 /* NOTE: To add a new font, you should
00045 
00046    (1) first generate a tightly cropped image of the 95 chars
00047        belonging in the font (see AppMedia/app-font2c.C for details);
00048 
00049    (2) add that image file to etc/fonts/ so that it can be used in the
00050        future to regenerate the font definition if needed;
00051 
00052    (3) add your font name to the list of fonts in
00053        devscripts/update-fonts.sh and then re-run that script;
00054 
00055    (4) add #include "Image/YOURFONTNAME.h" below;
00056 
00057    (5) add a SimpleFont wrapper for your new font to the g_std_fonts
00058        list below, so that it can be retrieved by the various
00059        SimpleFont helper functions like SimpleFont::FIXED() and
00060        SimpleFont::fixedMaxWidth().
00061 */
00062 #include "Image/font6x10.h"
00063 #include "Image/font7x13.h"
00064 #include "Image/font8x13bold.h"
00065 #include "Image/font9x15bold.h"
00066 #include "Image/font10x20.h"
00067 #include "Image/font11x22.h"
00068 #include "Image/font12x22.h"
00069 #include "Image/font14x26.h"
00070 #include "Image/font15x28.h"
00071 #include "Image/font16x29.h"
00072 #include "Image/font20x38.h"
00073 
00074 static const SimpleFont g_std_fonts[11] =
00075   {
00076     SimpleFont(6U, 10U, &font6x10[0][0]),
00077     SimpleFont(7U, 13U, &font7x13[0][0]),
00078     SimpleFont(8U, 13U, &font8x13bold[0][0]),
00079     SimpleFont(9U, 15U, &font9x15bold[0][0]),
00080     SimpleFont(10U, 20U, &font10x20[0][0]),
00081     SimpleFont(11U, 22U, &font11x22[0][0]),
00082     SimpleFont(12U, 22U, &font12x22[0][0]),
00083     SimpleFont(14U, 26U, &font14x26[0][0]),
00084     SimpleFont(15U, 28U, &font15x28[0][0]),
00085     SimpleFont(16U, 29U, &font16x29[0][0]),
00086     SimpleFont(20U, 38U, &font20x38[0][0])
00087   };
00088 
00089 // ######################################################################
00090 SimpleFont::SimpleFont(const uint cw, const uint ch,
00091                        const unsigned char *cdata) :
00092   ww(cw), hh(ch), data(cdata)
00093 { }
00094 
00095 // ######################################################################
00096 SimpleFont::~SimpleFont()
00097 { }
00098 
00099 // ######################################################################
00100 const unsigned char * SimpleFont::charptr(const int c) const
00101 {
00102   if (c < 32 || c > 126) return data; // return 'space' if out of range
00103   return data + (c - 32) * ww * hh;
00104 }
00105 
00106 // ######################################################################
00107 SimpleFont SimpleFont::FIXED(const uint width)
00108 {
00109   std::ostringstream oss;
00110 
00111   for (size_t i = 0; i < numStdFonts(); ++i)
00112     {
00113       if (g_std_fonts[i].ww == width)
00114         return g_std_fonts[i];
00115 
00116       if (i > 0)
00117         oss << ", ";
00118       oss << g_std_fonts[i].ww;
00119     }
00120 
00121   LFATAL("Invalid width %u - valid are: %s", width, oss.str().c_str());
00122   return SimpleFont(0U, 0U, NULL); // keep compiler happy
00123 }
00124 
00125 // ######################################################################
00126 SimpleFont SimpleFont::fixedMaxWidth(const uint maxwidth)
00127 {
00128   ASSERT(numStdFonts() > 0);
00129 
00130   size_t best_match = 0;
00131 
00132   for (size_t i = 0; i < numStdFonts(); ++i)
00133     {
00134       if (g_std_fonts[i].ww <= maxwidth ||
00135           g_std_fonts[i].ww < g_std_fonts[best_match].ww)
00136         best_match = i;
00137     }
00138 
00139   return g_std_fonts[best_match];
00140 }
00141 
00142 // ######################################################################
00143 SimpleFont SimpleFont::fixedMaxHeight(const uint maxheight)
00144 {
00145   ASSERT(numStdFonts() > 0);
00146 
00147   size_t best_match = 0;
00148 
00149   for (size_t i = 0; i < numStdFonts(); ++i)
00150     {
00151       if (g_std_fonts[i].hh <= maxheight ||
00152           g_std_fonts[i].hh < g_std_fonts[best_match].hh)
00153         best_match = i;
00154     }
00155 
00156   return g_std_fonts[best_match];
00157 }
00158 
00159 // ######################################################################
00160 SimpleFont SimpleFont::stdFont(const size_t n)
00161 {
00162   if (n >= numStdFonts())
00163     LFATAL("expected n < %"ZU", but got n = %"ZU, numStdFonts(), n);
00164 
00165   return g_std_fonts[n];
00166 }
00167 
00168 // ######################################################################
00169 size_t SimpleFont::numStdFonts()
00170 {
00171   return sizeof(g_std_fonts) / sizeof(g_std_fonts[0]);
00172 }
Generated on Sun May 8 08:40:57 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3