ShapeFont.cpp

Go to the documentation of this file.
00001 /*
00002 Copyright (C) 2003-2006 The Pentagram team
00003 
00004 This program is free software; you can redistribute it and/or
00005 modify it under the terms of the GNU General Public License
00006 as published by the Free Software Foundation; either version 2
00007 of the License, or (at your option) any later version.
00008 
00009 This program is distributed in the hope that it will be useful,
00010 but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 GNU General Public License for more details.
00013 
00014 You should have received a copy of the GNU General Public License
00015 along with this program; if not, write to the Free Software
00016 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00017 */
00018 
00019 #include "pent_include.h"
00020 #include "ShapeFont.h"
00021 #include "Shape.h"
00022 #include "ShapeFrame.h"
00023 #include "ShapeRenderedText.h"
00024 
00025 DEFINE_RUNTIME_CLASSTYPE_CODE_MULTI2(ShapeFont,Pentagram::Font,Shape);
00026 
00027 
00028 ShapeFont::ShapeFont(const uint8* data,uint32 size,
00029                                          const ConvertShapeFormat *format,
00030                                          const uint16 flexId, const uint32 shapenum)
00031         : Font(), Shape(data,size,format, flexId, shapenum),
00032           height(0), baseline(0), vlead(-1), hlead(0)
00033 {
00034 
00035 }
00036 
00037 ShapeFont::~ShapeFont()
00038 {
00039 
00040 }
00041 
00042 
00043 int ShapeFont::getWidth(char c)
00044 {
00045         return getFrame(static_cast<unsigned char>(c))->width;
00046 }
00047 
00048 int ShapeFont::getHeight()
00049 {
00050         if (height == 0)
00051         {
00052                 for (uint32 i = 0; i < frameCount(); i++) 
00053                 {
00054                         int h = getFrame(i)->height;
00055                         
00056                         if (h > height) height = h;
00057                 }
00058         }
00059 
00060         return height;
00061 }
00062 
00063 int ShapeFont::getBaseline()
00064 {
00065         if (baseline == 0)
00066         {
00067                 for (uint32 i = 0; i < frameCount(); i++) 
00068                 {
00069                         int b = getFrame(i)->yoff;
00070                         
00071                         if (b > baseline) baseline = b;
00072                 }
00073         }
00074 
00075         return baseline;
00076 }
00077 
00078 int ShapeFont::getBaselineSkip()
00079 {
00080         return getHeight() + getVlead();
00081 }
00082 
00083 void ShapeFont::getStringSize(const std::string& text, int& width, int& height)
00084 {
00085         width = hlead;
00086         height = getHeight();
00087 
00088         for (unsigned int i = 0; i < text.size(); ++i)
00089         {
00090                 if (text[i] == '\n' || text[i] == '\r') {
00091                         // ignore
00092                 } else {
00093                         width += getWidth(text[i])-hlead;
00094                 }
00095         }
00096 }
00097 
00098 RenderedText* ShapeFont::renderText(const std::string& text,
00099                                                                         unsigned int& remaining,
00100                                                                         int width, int height, TextAlign align,
00101                                                                         bool u8specials,
00102                                                                         std::string::size_type cursor)
00103 {
00104         int resultwidth, resultheight;
00105         std::list<PositionedText> lines;
00106         lines = typesetText<Traits>(this, text, remaining,
00107                                                                 width, height, align, u8specials,
00108                                                                 resultwidth, resultheight, cursor);
00109 
00110         return new ShapeRenderedText(lines, resultwidth, resultheight,
00111                                                                  getVlead(), this);
00112 }

Generated on Fri Jul 27 22:27:36 2007 for pentagram by  doxygen 1.4.7