CEGUIFont.h

00001 /***********************************************************************
00002     filename:   CEGUIFont.h
00003     created:    21/2/2004
00004     author:     Paul D Turner
00005 
00006     purpose:    Defines interface for the Font class
00007 *************************************************************************/
00008 /***************************************************************************
00009  *   Copyright (C) 2004 - 2006 Paul D Turner & The CEGUI Development Team
00010  *
00011  *   Permission is hereby granted, free of charge, to any person obtaining
00012  *   a copy of this software and associated documentation files (the
00013  *   "Software"), to deal in the Software without restriction, including
00014  *   without limitation the rights to use, copy, modify, merge, publish,
00015  *   distribute, sublicense, and/or sell copies of the Software, and to
00016  *   permit persons to whom the Software is furnished to do so, subject to
00017  *   the following conditions:
00018  *
00019  *   The above copyright notice and this permission notice shall be
00020  *   included in all copies or substantial portions of the Software.
00021  *
00022  *   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00023  *   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00024  *   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
00025  *   IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
00026  *   OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
00027  *   ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
00028  *   OTHER DEALINGS IN THE SOFTWARE.
00029  ***************************************************************************/
00030 #ifndef _CEGUIFont_h_
00031 #define _CEGUIFont_h_
00032 
00033 #include "CEGUIBase.h"
00034 #include "CEGUIPropertySet.h"
00035 #include "CEGUIString.h"
00036 #include "CEGUIRect.h"
00037 #include "CEGUIVector.h"
00038 #include "CEGUIColourRect.h"
00039 #include "CEGUIXMLSerializer.h"
00040 #include "CEGUIImage.h"
00041 
00042 #include <map>
00043 
00044 
00045 #if defined(_MSC_VER)
00046 #   pragma warning(push)
00047 #   pragma warning(disable : 4251)
00048 #endif
00049 
00050 
00051 // Start of CEGUI namespace section
00052 namespace CEGUI
00053 {
00054 
00055 // Forward declarations for font properties
00056 namespace FontProperties
00057 {
00058     class NativeRes;
00059     class Name;
00060     class FileName;
00061     class ResourceGroup;
00062     class AutoScaled;
00063 }
00064 
00069 enum TextFormatting
00070 {
00072     LeftAligned,
00074     RightAligned,
00076     Centred,
00078     Justified,
00080     WordWrapLeftAligned,
00082     WordWrapRightAligned,
00084     WordWrapCentred,
00086     WordWrapJustified
00087 };
00088 
00089 
00097 class FontGlyph
00098 {
00099 private:
00101     const Image* d_image;
00103     float d_advance;
00104 
00105 public:
00110     FontGlyph ()
00111     { }
00112 
00117     FontGlyph (float advance) : d_image (0), d_advance (advance)
00118     { }
00119 
00124     FontGlyph (float advance, const Image *image) : d_image (image), d_advance (advance)
00125     { }
00126 
00131     const Image* getImage () const
00132     { return d_image; }
00133 
00138     const Imageset* getImageset () const
00139     { return d_image->getImageset (); }
00140 
00145     Size getSize (float x_scale, float y_scale) const
00146     { return Size (getWidth (x_scale), getHeight (y_scale)); }
00147 
00152     float getWidth (float x_scale) const
00153     { return d_image->getWidth () * x_scale; }
00154 
00159     float getHeight (float y_scale) const
00160     { return d_image->getHeight () * y_scale; }
00161 
00169     float getRenderedAdvance (float x_scale) const
00170     { return (d_image->getWidth () + d_image->getOffsetX ()) * x_scale; }
00171 
00181     float getAdvance (float x_scale = 1.0) const
00182     { return d_advance * x_scale; }
00183 
00188     void setAdvance (float advance)
00189     { d_advance = advance; }
00190 
00195     void setImage (const Image* image)
00196     { d_image = image; }
00197 };
00198 
00199 
00211 class CEGUIEXPORT Font : public PropertySet
00212 {
00213 protected:
00214     /*************************************************************************
00215         Friends so that only FontManager can create and destroy font objects
00216     *************************************************************************/
00217     friend class FontManager;
00218     friend class Font_xmlHandler;
00219     friend class FontProperties::NativeRes;
00220     friend class FontProperties::Name;
00221     friend class FontProperties::FileName;
00222     friend class FontProperties::ResourceGroup;
00223     friend class FontProperties::AutoScaled;
00224 
00225     /*************************************************************************
00226         Implementation Data
00227     *************************************************************************/
00228     typedef std::map<utf32, FontGlyph> CodepointMap;
00230     CodepointMap d_cp_map;
00231 
00233     String d_name;
00235     String d_fileName;
00237     String d_resourceGroup;
00239     static String d_defaultResourceGroup;
00240 
00242     float d_ascender;
00244     float d_descender;
00246     float d_height;
00247 
00249     bool d_autoScale;
00251     float d_horzScaling;
00253     float d_vertScaling;
00255     float d_nativeHorzRes;
00257     float d_nativeVertRes;
00258 
00260     utf32 d_maxCodepoint;
00261 
00275     uint *d_glyphPageLoaded;
00276 
00277     /*************************************************************************
00278         Construction & Destruction
00279     *************************************************************************/
00300     Font (const String& name, const String& fontname,
00301           const String& resourceGroup = "");
00302 
00330     Font (const XMLAttributes& attributes);
00331 
00336     virtual ~Font ();
00337 
00342     virtual void defineMapping (const XMLAttributes& attributes);
00343 
00348     virtual void updateFont () = 0;
00349 
00354     size_t drawWrappedText (const String& text, const Rect& draw_area, float z, const Rect& clip_rect, TextFormatting fmt, const ColourRect& colours, float x_scale = 1.0f, float y_scale = 1.0f);
00355 
00360     size_t getNextWord (const String& in_string, size_t start_idx, String& out_string) const;
00361 
00366     void drawTextLine (const String& text, const Vector3& position, const Rect& clip_rect, const ColourRect& colours, float x_scale = 1.0f, float y_scale = 1.0f);
00367 
00372     void drawTextLineJustified (const String& text, const Rect& draw_area, const Vector3& position, const Rect& clip_rect, const ColourRect& colours, float x_scale = 1.0f, float y_scale = 1.0f);
00373 
00378     float getWrappedTextExtent (const String& text, float wrapWidth, float x_scale = 1.0f);
00379 
00392     const FontGlyph* getGlyphData (utf32 codepoint);
00393 
00399     void setMaxCodepoint (utf32 codepoint);
00400 
00415     virtual void rasterize (utf32 start_codepoint, utf32 end_codepoint);
00416 
00427     void writeXMLToStream (XMLSerializer& xml_stream) const;
00428 
00437     virtual void writeXMLToStream_impl (XMLSerializer& xml_stream) const = 0;
00438 
00443     void addFontProperties ();
00444 
00445 public:
00447     static const argb_t DefaultColour;
00448 
00457     virtual void load () = 0;
00458 
00470     bool isCodepointAvailable (utf32 cp) const
00471     { return (d_cp_map.find(cp) != d_cp_map.end()); }
00472 
00473     /*************************************************************************
00474         Text drawing methods
00475     *************************************************************************/
00509     size_t drawText (const String& text, const Rect& draw_area, float z, const Rect& clip_rect, TextFormatting fmt, const ColourRect& colours, float x_scale = 1.0f, float y_scale = 1.0f);
00510 
00540     size_t drawText (const String& text, const Rect& draw_area, float z, const Rect& clip_rect, TextFormatting fmt, float x_scale = 1.0f, float y_scale = 1.0f)
00541     { return drawText (text, draw_area, z, clip_rect, fmt, ColourRect (DefaultColour, DefaultColour, DefaultColour, DefaultColour), x_scale, y_scale); }
00542 
00569     void drawText (const String& text, const Rect& draw_area, float z, const Rect& clip_rect, float x_scale = 1.0f, float y_scale = 1.0f)
00570     { drawText (text, draw_area, z, clip_rect, LeftAligned, ColourRect (DefaultColour, DefaultColour, DefaultColour, DefaultColour), x_scale, y_scale); }
00571 
00603     size_t drawText (const String& text, const Rect& draw_area, float z, TextFormatting fmt, const ColourRect& colours, float x_scale = 1.0f, float y_scale = 1.0f)
00604     { return drawText (text, draw_area, z, draw_area, fmt, colours, x_scale, y_scale); }
00605 
00633     size_t drawText (const String& text, const Rect& draw_area, float z, TextFormatting fmt, float x_scale = 1.0f, float y_scale = 1.0f)
00634     { return drawText (text, draw_area, z, draw_area, fmt, ColourRect (DefaultColour, DefaultColour, DefaultColour, DefaultColour), x_scale, y_scale); }
00635 
00660     void drawText (const String& text, const Rect& draw_area, float z, float x_scale = 1.0f, float y_scale = 1.0f)
00661     { drawText (text, draw_area, z, draw_area, LeftAligned, ColourRect (DefaultColour, DefaultColour, DefaultColour, DefaultColour), x_scale, y_scale); }
00662 
00690     void drawText (const String& text, const Vector3& position, const Rect& clip_rect, const ColourRect& colours, float x_scale = 1.0f, float y_scale = 1.0f)
00691     { drawText (text, Rect (position.d_x, position.d_y, position.d_x, position.d_y), position.d_z, clip_rect, LeftAligned, colours, x_scale, y_scale); }
00692 
00716     void drawText (const String& text, const Vector3& position, const Rect& clip_rect, float x_scale = 1.0f, float y_scale = 1.0f)
00717     { drawText (text, Rect (position.d_x, position.d_y, position.d_x, position.d_y), position.d_z, clip_rect, LeftAligned, ColourRect(DefaultColour, DefaultColour, DefaultColour, DefaultColour), x_scale, y_scale); }
00718 
00729     virtual void setNativeResolution (const Size& size);
00730 
00741     virtual void notifyScreenResolution (const Size& size);
00742 
00743     /*************************************************************************
00744         Informational methods
00745     *************************************************************************/
00758     float getLineSpacing (float y_scale = 1.0f) const
00759     { return d_height * y_scale; }
00760 
00773     float getFontHeight (float y_scale = 1.0f) const
00774     { return (d_ascender - d_descender) * y_scale; }
00775 
00788     float getBaseline (float y_scale = 1.0f) const
00789     { return d_ascender * y_scale; }
00790 
00808     float getTextExtent (const String& text, float x_scale = 1.0f);
00809 
00833     size_t getCharAtPixel (const String& text, float pixel, float x_scale = 1.0f)
00834     { return getCharAtPixel(text, 0, pixel, x_scale); }
00835 
00864     size_t getCharAtPixel (const String& text, size_t start_char, float pixel, float x_scale = 1.0f);
00865 
00892     size_t getFormattedLineCount (const String& text, const Rect& format_area, TextFormatting fmt, float x_scale = 1.0f);
00893 
00920     float getFormattedTextExtent (const String& text, const Rect& format_area, TextFormatting fmt, float x_scale = 1.0f);
00921 
00922 
00933     static void setDefaultResourceGroup (const String& resourceGroup)
00934     { d_defaultResourceGroup = resourceGroup; }
00935 
00936 
00945     static const String& getDefaultResourceGroup ()
00946     { return d_defaultResourceGroup; }
00947 };
00948 
00949 } // End of  CEGUI namespace section
00950 
00951 #if defined(_MSC_VER)
00952 #   pragma warning(pop)
00953 #endif
00954 
00955 
00956 #endif  // end of guard _CEGUIFont_h_

Generated on Sat Jun 28 14:35:44 2008 for Crazy Eddies GUI System by  doxygen 1.5.4