Crazy Eddies GUI System  0.6.0
CEGUIFont.h
1 /***********************************************************************
2  filename: CEGUIFont.h
3  created: 21/2/2004
4  author: Paul D Turner
5 
6  purpose: Defines interface for the Font class
7 *************************************************************************/
8 /***************************************************************************
9  * Copyright (C) 2004 - 2006 Paul D Turner & The CEGUI Development Team
10  *
11  * Permission is hereby granted, free of charge, to any person obtaining
12  * a copy of this software and associated documentation files (the
13  * "Software"), to deal in the Software without restriction, including
14  * without limitation the rights to use, copy, modify, merge, publish,
15  * distribute, sublicense, and/or sell copies of the Software, and to
16  * permit persons to whom the Software is furnished to do so, subject to
17  * the following conditions:
18  *
19  * The above copyright notice and this permission notice shall be
20  * included in all copies or substantial portions of the Software.
21  *
22  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
25  * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
26  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
27  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
28  * OTHER DEALINGS IN THE SOFTWARE.
29  ***************************************************************************/
30 #ifndef _CEGUIFont_h_
31 #define _CEGUIFont_h_
32 
33 #include "CEGUIBase.h"
34 #include "CEGUIPropertySet.h"
35 #include "CEGUIString.h"
36 #include "CEGUIRect.h"
37 #include "CEGUIVector.h"
38 #include "CEGUIColourRect.h"
39 #include "CEGUIXMLSerializer.h"
40 #include "CEGUIImage.h"
41 
42 #include <map>
43 
44 
45 #if defined(_MSC_VER)
46 # pragma warning(push)
47 # pragma warning(disable : 4251)
48 #endif
49 
50 
51 // Start of CEGUI namespace section
52 namespace CEGUI
53 {
54 
55 // Forward declarations for font properties
56 namespace FontProperties
57 {
58  class NativeRes;
59  class Name;
60  class FileName;
61  class ResourceGroup;
62  class AutoScaled;
63 }
64 
70 {
87 };
88 
89 
97 class FontGlyph
98 {
99 private:
101  const Image* d_image;
103  float d_advance;
104 
105 public:
111  { }
112 
117  FontGlyph (float advance) : d_image (0), d_advance (advance)
118  { }
119 
124  FontGlyph (float advance, const Image *image) : d_image (image), d_advance (advance)
125  { }
126 
131  const Image* getImage () const
132  { return d_image; }
133 
138  const Imageset* getImageset () const
139  { return d_image->getImageset (); }
140 
145  Size getSize (float x_scale, float y_scale) const
146  { return Size (getWidth (x_scale), getHeight (y_scale)); }
147 
152  float getWidth (float x_scale) const
153  { return d_image->getWidth () * x_scale; }
154 
159  float getHeight (float y_scale) const
160  { return d_image->getHeight () * y_scale; }
161 
169  float getRenderedAdvance (float x_scale) const
170  { return (d_image->getWidth () + d_image->getOffsetX ()) * x_scale; }
171 
181  float getAdvance (float x_scale = 1.0) const
182  { return d_advance * x_scale; }
183 
188  void setAdvance (float advance)
189  { d_advance = advance; }
190 
195  void setImage (const Image* image)
196  { d_image = image; }
197 };
198 
199 
211 class CEGUIEXPORT Font : public PropertySet
212 {
213 protected:
214  /*************************************************************************
215  Friends so that only FontManager can create and destroy font objects
216  *************************************************************************/
217  friend class FontManager;
218  friend class Font_xmlHandler;
219  friend class FontProperties::NativeRes;
220  friend class FontProperties::Name;
221  friend class FontProperties::FileName;
222  friend class FontProperties::ResourceGroup;
223  friend class FontProperties::AutoScaled;
224 
225  /*************************************************************************
226  Implementation Data
227  *************************************************************************/
228  typedef std::map<utf32, FontGlyph> CodepointMap;
230  CodepointMap d_cp_map;
231 
240 
242  float d_ascender;
244  float d_descender;
246  float d_height;
247 
258 
261 
276 
277  /*************************************************************************
278  Construction & Destruction
279  *************************************************************************/
300  Font (const String& name, const String& fontname,
301  const String& resourceGroup = "");
302 
330  Font (const XMLAttributes& attributes);
331 
336  virtual ~Font ();
337 
342  virtual void defineMapping (const XMLAttributes& attributes);
343 
348  virtual void updateFont () = 0;
349 
354  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);
355 
360  size_t getNextWord (const String& in_string, size_t start_idx, String& out_string) const;
361 
366  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);
367 
372  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);
373 
378  float getWrappedTextExtent (const String& text, float wrapWidth, float x_scale = 1.0f);
379 
392  const FontGlyph* getGlyphData (utf32 codepoint);
393 
399  void setMaxCodepoint (utf32 codepoint);
400 
415  virtual void rasterize (utf32 start_codepoint, utf32 end_codepoint);
416 
427  void writeXMLToStream (XMLSerializer& xml_stream) const;
428 
437  virtual void writeXMLToStream_impl (XMLSerializer& xml_stream) const = 0;
438 
443  void addFontProperties ();
444 
445 public:
447  static const argb_t DefaultColour;
448 
457  virtual void load () = 0;
458 
470  bool isCodepointAvailable (utf32 cp) const
471  { return (d_cp_map.find(cp) != d_cp_map.end()); }
472 
473  /*************************************************************************
474  Text drawing methods
475  *************************************************************************/
509  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);
510 
540  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)
541  { return drawText (text, draw_area, z, clip_rect, fmt, ColourRect (DefaultColour, DefaultColour, DefaultColour, DefaultColour), x_scale, y_scale); }
542 
569  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)
570  { drawText (text, draw_area, z, clip_rect, LeftAligned, ColourRect (DefaultColour, DefaultColour, DefaultColour, DefaultColour), x_scale, y_scale); }
571 
603  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)
604  { return drawText (text, draw_area, z, draw_area, fmt, colours, x_scale, y_scale); }
605 
633  size_t drawText (const String& text, const Rect& draw_area, float z, TextFormatting fmt, float x_scale = 1.0f, float y_scale = 1.0f)
634  { return drawText (text, draw_area, z, draw_area, fmt, ColourRect (DefaultColour, DefaultColour, DefaultColour, DefaultColour), x_scale, y_scale); }
635 
660  void drawText (const String& text, const Rect& draw_area, float z, float x_scale = 1.0f, float y_scale = 1.0f)
661  { drawText (text, draw_area, z, draw_area, LeftAligned, ColourRect (DefaultColour, DefaultColour, DefaultColour, DefaultColour), x_scale, y_scale); }
662 
690  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)
691  { 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); }
692 
716  void drawText (const String& text, const Vector3& position, const Rect& clip_rect, float x_scale = 1.0f, float y_scale = 1.0f)
717  { 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); }
718 
729  virtual void setNativeResolution (const Size& size);
730 
741  virtual void notifyScreenResolution (const Size& size);
742 
743  /*************************************************************************
744  Informational methods
745  *************************************************************************/
758  float getLineSpacing (float y_scale = 1.0f) const
759  { return d_height * y_scale; }
760 
773  float getFontHeight (float y_scale = 1.0f) const
774  { return (d_ascender - d_descender) * y_scale; }
775 
788  float getBaseline (float y_scale = 1.0f) const
789  { return d_ascender * y_scale; }
790 
808  float getTextExtent (const String& text, float x_scale = 1.0f);
809 
833  size_t getCharAtPixel (const String& text, float pixel, float x_scale = 1.0f)
834  { return getCharAtPixel(text, 0, pixel, x_scale); }
835 
864  size_t getCharAtPixel (const String& text, size_t start_char, float pixel, float x_scale = 1.0f);
865 
892  size_t getFormattedLineCount (const String& text, const Rect& format_area, TextFormatting fmt, float x_scale = 1.0f);
893 
920  float getFormattedTextExtent (const String& text, const Rect& format_area, TextFormatting fmt, float x_scale = 1.0f);
921 
922 
933  static void setDefaultResourceGroup (const String& resourceGroup)
934  { d_defaultResourceGroup = resourceGroup; }
935 
936 
945  static const String& getDefaultResourceGroup ()
946  { return d_defaultResourceGroup; }
947 };
948 
949 } // End of CEGUI namespace section
950 
951 #if defined(_MSC_VER)
952 # pragma warning(pop)
953 #endif
954 
955 
956 #endif // end of guard _CEGUIFont_h_