imagefont.cpp

00001 /*      _______   __   __   __   ______   __   __   _______   __   __
00002  *     / _____/\ / /\ / /\ / /\ / ____/\ / /\ / /\ / ___  /\ /  |\/ /\
00003  *    / /\____\// / // / // / // /\___\// /_// / // /\_/ / // , |/ / /
00004  *   / / /__   / / // / // / // / /    / ___  / // ___  / // /| ' / /
00005  *  / /_// /\ / /_// / // / // /_/_   / / // / // /\_/ / // / |  / /
00006  * /______/ //______/ //_/ //_____/\ /_/ //_/ //_/ //_/ //_/ /|_/ /
00007  * \______\/ \______\/ \_\/ \_____\/ \_\/ \_\/ \_\/ \_\/ \_\/ \_\/
00008  *
00009  * Copyright (c) 2004, 2005, 2006 Olof Naessén and Per Larsson
00010  *
00011  *                                                         Js_./
00012  * Per Larsson a.k.a finalman                          _RqZ{a<^_aa
00013  * Olof Naessén a.k.a jansem/yakslem                _asww7!uY`>  )\a//
00014  *                                                 _Qhm`] _f "'c  1!5m
00015  * Visit: http://guichan.darkbits.org             )Qk<P ` _: :+' .'  "{[
00016  *                                               .)j(] .d_/ '-(  P .   S
00017  * License: (BSD)                                <Td/Z <fP"5(\"??"\a.  .L
00018  * Redistribution and use in source and          _dV>ws?a-?'      ._/L  #'
00019  * binary forms, with or without                 )4d[#7r, .   '     )d`)[
00020  * modification, are permitted provided         _Q-5'5W..j/?'   -?!\)cam'
00021  * that the following conditions are met:       j<<WP+k/);.        _W=j f
00022  * 1. Redistributions of source code must       .$%w\/]Q  . ."'  .  mj$
00023  *    retain the above copyright notice,        ]E.pYY(Q]>.   a     J@\
00024  *    this list of conditions and the           j(]1u<sE"L,. .   ./^ ]{a
00025  *    following disclaimer.                     4'_uomm\.  )L);-4     (3=
00026  * 2. Redistributions in binary form must        )_]X{Z('a_"a7'<a"a,  ]"[
00027  *    reproduce the above copyright notice,       #}<]m7`Za??4,P-"'7. ).m
00028  *    this list of conditions and the            ]d2e)Q(<Q(  ?94   b-  LQ/
00029  *    following disclaimer in the                <B!</]C)d_, '(<' .f. =C+m
00030  *    documentation and/or other materials      .Z!=J ]e []('-4f _ ) -.)m]'
00031  *    provided with the distribution.          .w[5]' _[ /.)_-"+?   _/ <W"
00032  * 3. Neither the name of Guichan nor the      :$we` _! + _/ .        j?
00033  *    names of its contributors may be used     =3)= _f  (_yQmWW$#(    "
00034  *    to endorse or promote products derived     -   W,  sQQQQmZQ#Wwa]..
00035  *    from this software without specific        (js, \[QQW$QWW#?!V"".
00036  *    prior written permission.                    ]y:.<\..          .
00037  *                                                 -]n w/ '         [.
00038  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT       )/ )/           !
00039  * HOLDERS AND CONTRIBUTORS "AS IS" AND ANY         <  (; sac    ,    '
00040  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING,               ]^ .-  %
00041  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF            c <   r
00042  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR            aga<  <La
00043  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE          5%  )P'-3L
00044  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR        _bQf` y`..)a
00045  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,          ,J?4P'.P"_(\?d'.,
00046  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES               _Pa,)!f/<[]/  ?"
00047  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT      _2-..:. .r+_,.. .
00048  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,     ?a.<%"'  " -'.a_ _,
00049  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION)                     ^
00050  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
00051  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00052  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00053  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
00054  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00055  */
00056 
00057 /*
00058  * For comments regarding functions please see the header file.
00059  */
00060 
00061 #include "guichan/imagefont.hpp"
00062 
00063 #include <sstream>
00064 
00065 #include "guichan/color.hpp"
00066 #include "guichan/exception.hpp"
00067 #include "guichan/graphics.hpp"
00068 #include "guichan/image.hpp"
00069 
00070 namespace gcn
00071 {
00072     ImageFont::ImageFont(const std::string& filename, const std::string& glyphs)
00073     {
00074         mFilename = filename;
00075         mImage = Image::load(filename, false);
00076 
00077         Color separator = mImage->getPixel(0, 0);
00078 
00079         int i = 0;
00080         for (i=0; separator == mImage->getPixel(i, 0)
00081                  && i < mImage->getWidth(); ++i)
00082         {
00083         }
00084 
00085         if (i >= mImage->getWidth())
00086         {
00087             throw GCN_EXCEPTION("Corrupt image.");
00088         }
00089 
00090         int j = 0;
00091         for (j = 0; j < mImage->getHeight(); ++j)
00092         {
00093             if (separator == mImage->getPixel(i, j))
00094             {
00095                 break;
00096             }
00097         }
00098 
00099         mHeight = j;
00100         int x = 0, y = 0;
00101         unsigned char k;
00102 
00103         for (i=0; i < (int)glyphs.size(); ++i)
00104         {
00105             k = glyphs.at(i);
00106             addGlyph(k, x, y, separator);
00107         }
00108 
00109         int w = mImage->getWidth();
00110         int h = mImage->getHeight();
00111         mImage->convertToDisplayFormat();
00112 
00113         mRowSpacing = 0;
00114         mGlyphSpacing = 0;
00115     }
00116 
00117     ImageFont::ImageFont(const std::string& filename, unsigned char glyphsFrom,
00118                          unsigned char glyphsTo)
00119     {
00120         mFilename = filename;
00121         mImage = Image::load(filename, false);
00122 
00123         Color separator = mImage->getPixel(0, 0);
00124 
00125         int i = 0;
00126         for (i=0; separator == mImage->getPixel(i, 0)
00127                  && i < mImage->getWidth(); ++i)
00128         {
00129         }
00130 
00131         if (i >= mImage->getWidth())
00132         {
00133             throw GCN_EXCEPTION("Corrupt image.");
00134         }
00135 
00136         int j = 0;
00137         for (j = 0; j < mImage->getHeight(); ++j)
00138         {
00139             if (separator == mImage->getPixel(i, j))
00140             {
00141                 break;
00142             }
00143         }
00144 
00145         mHeight = j;
00146         int x = 0, y = 0;
00147         unsigned char k;
00148 
00149         for (i=glyphsFrom; i<glyphsTo+1; i++)
00150         {
00151             addGlyph(i, x, y, separator);
00152         }
00153 
00154         int w = mImage->getWidth();
00155         int h = mImage->getHeight();
00156         mImage->convertToDisplayFormat();
00157 
00158         mRowSpacing = 0;
00159         mGlyphSpacing = 0;
00160     }
00161 
00162     ImageFont::~ImageFont()
00163     {
00164         delete mImage;
00165     }
00166 
00167     int ImageFont::getWidth(unsigned char glyph) const
00168     {
00169         if (mGlyph[glyph].width == 0)
00170         {
00171             return mGlyph[(int)(' ')].width + mGlyphSpacing;
00172         }
00173 
00174         return mGlyph[glyph].width + mGlyphSpacing;
00175     }
00176 
00177     int ImageFont::getHeight() const
00178     {
00179         return mHeight + mRowSpacing;
00180     }
00181 
00182     int ImageFont::drawGlyph(Graphics* graphics, unsigned char glyph,
00183                              int x, int y)
00184     {
00185         // This is needed for drawing the Glyph in the middle if we have spacing
00186         int yoffset = getRowSpacing() >> 1;
00187 
00188         if (mGlyph[glyph].width == 0)
00189         {
00190             graphics->drawRectangle(Rectangle(x, y + 1 + yoffset,
00191                                               mGlyph[(int)(' ')].width - 1,
00192                                               mGlyph[(int)(' ')].height - 2));
00193 
00194             return mGlyph[(int)(' ')].width + mGlyphSpacing;
00195         }
00196 
00197         graphics->drawImage(mImage, mGlyph[glyph].x, mGlyph[glyph].y, x,
00198                             y + yoffset, mGlyph[glyph].width,
00199                             mGlyph[glyph].height);
00200 
00201         return mGlyph[glyph].width + mGlyphSpacing;
00202     }
00203 
00204     void ImageFont::drawString(Graphics* graphics, const std::string& text,
00205                                int x, int y)
00206     {
00207         unsigned int i;
00208 
00209         for (i = 0; i< text.size(); ++i)
00210         {
00211             drawGlyph(graphics, text.at(i), x, y);
00212             x += getWidth(text.at(i));
00213         }
00214     }
00215 
00216     void ImageFont::setRowSpacing(int spacing)
00217     {
00218         mRowSpacing = spacing;
00219     }
00220 
00221     int ImageFont::getRowSpacing()
00222     {
00223         return mRowSpacing;
00224     }
00225 
00226     void ImageFont::setGlyphSpacing(int spacing)
00227     {
00228         mGlyphSpacing = spacing;
00229     }
00230 
00231     int ImageFont::getGlyphSpacing()
00232     {
00233         return mGlyphSpacing;
00234     }
00235 
00236     void ImageFont::addGlyph(unsigned char c, int &x,
00237                              int &y, const Color& separator)
00238     {
00239         Color color;
00240         do
00241         {
00242             ++x;
00243 
00244             if (x >= mImage->getWidth())
00245             {
00246                 y += mHeight + 1;
00247                 x = 0;
00248 
00249                 if (y >= mImage->getHeight())
00250                 {
00251                     std::string str;
00252                     std::ostringstream os(str);
00253                     os << "Image ";
00254                     os << mFilename;
00255                     os << " with font is corrupt near character '";
00256                     os << c;
00257                     os << "'";
00258                     throw GCN_EXCEPTION(os.str());
00259                 }
00260             }
00261 
00262             color = mImage->getPixel(x, y);
00263 
00264         } while (color == separator);
00265 
00266         int w = 0;
00267 
00268         do
00269         {
00270             ++w;
00271 
00272             if (x+w >= mImage->getWidth())
00273             {
00274                 std::string str;
00275                 std::ostringstream os(str);
00276                 os << "Image ";
00277                 os << mFilename;
00278                 os << " with font is corrupt near character '";
00279                 os << c;
00280                 os << "'";
00281                 throw GCN_EXCEPTION(os.str());
00282             }
00283 
00284             color = mImage->getPixel(x + w, y);
00285 
00286         } while (color != separator);
00287 
00288         mGlyph[c] = Rectangle(x, y, w, mHeight);
00289 
00290         x += w;
00291     }
00292 
00293     int ImageFont::getWidth(const std::string& text) const
00294     {
00295         unsigned int i;
00296         int size = 0;
00297 
00298         for (i = 0; i < text.size(); ++i)
00299         {
00300             size += getWidth(text.at(i));
00301         }
00302 
00303         return size;
00304     }
00305 
00306     int ImageFont::getStringIndexAt(const std::string& text, int x)
00307     {
00308         unsigned int i;
00309         int size = 0;
00310 
00311         for (i = 0; i < text.size(); ++i)
00312         {
00313             size += getWidth(text.at(i));
00314 
00315             if (size > x)
00316             {
00317                 return i;
00318             }
00319         }
00320 
00321         return text.size();
00322     }
00323 }

Generated on Sat Jul 29 19:38:48 2006 for Guichan by  doxygen 1.4.7