PageRenderTime 55ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 1ms

/Mantis/GRAPHICS/Gui/inc/cegui/src/CEGUIFont.cpp

https://bitbucket.org/ahernandez52/school-mantis-1
C++ | 282 lines | 182 code | 45 blank | 55 comment | 25 complexity | 51fb7377eb3ce94de1ee6d1e9fd35b30 MD5 | raw file
  1. /***********************************************************************
  2. filename: CEGUIFont.cpp
  3. created: 21/2/2004
  4. author: Paul D Turner <paul@cegui.org.uk>
  5. *************************************************************************/
  6. /***************************************************************************
  7. * Copyright (C) 2004 - 2009 Paul D Turner & The CEGUI Development Team
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining
  10. * a copy of this software and associated documentation files (the
  11. * "Software"), to deal in the Software without restriction, including
  12. * without limitation the rights to use, copy, modify, merge, publish,
  13. * distribute, sublicense, and/or sell copies of the Software, and to
  14. * permit persons to whom the Software is furnished to do so, subject to
  15. * the following conditions:
  16. *
  17. * The above copyright notice and this permission notice shall be
  18. * included in all copies or substantial portions of the Software.
  19. *
  20. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  23. * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
  24. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  25. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  26. * OTHER DEALINGS IN THE SOFTWARE.
  27. ***************************************************************************/
  28. #include "CEGUIFont.h"
  29. #include "CEGUIExceptions.h"
  30. #include "CEGUIFont_xmlHandler.h"
  31. #include "CEGUIPropertyHelper.h"
  32. namespace CEGUI
  33. {
  34. //----------------------------------------------------------------------------//
  35. // amount of bits in a uint
  36. #define BITS_PER_UINT (sizeof (uint) * 8)
  37. // must be a power of two
  38. #define GLYPHS_PER_PAGE 256
  39. //----------------------------------------------------------------------------//
  40. const argb_t Font::DefaultColour = 0xFFFFFFFF;
  41. String Font::d_defaultResourceGroup;
  42. //----------------------------------------------------------------------------//
  43. Font::Font(const String& name, const String& type_name, const String& filename,
  44. const String& resource_group, const bool auto_scaled,
  45. const float native_horz_res, const float native_vert_res) :
  46. d_name(name),
  47. d_type(type_name),
  48. d_filename(filename),
  49. d_resourceGroup(resource_group),
  50. d_ascender(0),
  51. d_descender(0),
  52. d_height(0),
  53. d_autoScale(auto_scaled),
  54. d_nativeHorzRes(native_horz_res),
  55. d_nativeVertRes(native_vert_res),
  56. d_maxCodepoint(0),
  57. d_glyphPageLoaded(0)
  58. {
  59. addFontProperties();
  60. const Size size(System::getSingleton().getRenderer()->getDisplaySize());
  61. d_horzScaling = size.d_width / d_nativeHorzRes;
  62. d_vertScaling = size.d_height / d_nativeVertRes;
  63. }
  64. //----------------------------------------------------------------------------//
  65. Font::~Font()
  66. {
  67. delete[] d_glyphPageLoaded;
  68. }
  69. //----------------------------------------------------------------------------//
  70. const String& Font::getName() const
  71. {
  72. return d_name;
  73. }
  74. //----------------------------------------------------------------------------//
  75. const String& Font::getTypeName() const
  76. {
  77. return d_type;
  78. }
  79. //----------------------------------------------------------------------------//
  80. void Font::setMaxCodepoint(utf32 codepoint)
  81. {
  82. d_maxCodepoint = codepoint;
  83. delete[] d_glyphPageLoaded;
  84. uint npages = (codepoint + GLYPHS_PER_PAGE) / GLYPHS_PER_PAGE;
  85. uint size = (npages + BITS_PER_UINT - 1) / BITS_PER_UINT;
  86. d_glyphPageLoaded = new uint[size];
  87. memset(d_glyphPageLoaded, 0, size * sizeof(uint));
  88. }
  89. //----------------------------------------------------------------------------//
  90. const FontGlyph* Font::getGlyphData(utf32 codepoint) const
  91. {
  92. if (codepoint > d_maxCodepoint)
  93. return 0;
  94. if (d_glyphPageLoaded)
  95. {
  96. // Check if glyph page has been rasterised
  97. uint page = codepoint / GLYPHS_PER_PAGE;
  98. uint mask = 1 << (page & (BITS_PER_UINT - 1));
  99. if (!(d_glyphPageLoaded[page / BITS_PER_UINT] & mask))
  100. {
  101. d_glyphPageLoaded[page / BITS_PER_UINT] |= mask;
  102. rasterise(codepoint & ~(GLYPHS_PER_PAGE - 1),
  103. codepoint | (GLYPHS_PER_PAGE - 1));
  104. }
  105. }
  106. CodepointMap::const_iterator pos = d_cp_map.find(codepoint);
  107. return (pos != d_cp_map.end()) ? &pos->second : 0;
  108. }
  109. //----------------------------------------------------------------------------//
  110. float Font::getTextExtent(const String& text, float x_scale) const
  111. {
  112. const FontGlyph* glyph;
  113. float cur_extent = 0, adv_extent = 0, width;
  114. for (size_t c = 0; c < text.length(); ++c)
  115. {
  116. glyph = getGlyphData(text[c]);
  117. if (glyph)
  118. {
  119. width = glyph->getRenderedAdvance(x_scale);
  120. if (adv_extent + width > cur_extent)
  121. cur_extent = adv_extent + width;
  122. adv_extent += glyph->getAdvance(x_scale);
  123. }
  124. }
  125. return ceguimax(adv_extent, cur_extent);
  126. }
  127. //----------------------------------------------------------------------------//
  128. size_t Font::getCharAtPixel(const String& text, size_t start_char, float pixel,
  129. float x_scale) const
  130. {
  131. const FontGlyph* glyph;
  132. float cur_extent = 0;
  133. size_t char_count = text.length();
  134. // handle simple cases
  135. if ((pixel <= 0) || (char_count <= start_char))
  136. return start_char;
  137. for (size_t c = start_char; c < char_count; ++c)
  138. {
  139. glyph = getGlyphData(text[c]);
  140. if (glyph)
  141. {
  142. cur_extent += glyph->getAdvance(x_scale);
  143. if (pixel < cur_extent)
  144. return c;
  145. }
  146. }
  147. return char_count;
  148. }
  149. //----------------------------------------------------------------------------//
  150. void Font::drawText(GeometryBuffer& buffer, const String& text,
  151. const Vector2& position, const Rect* clip_rect,
  152. const ColourRect& colours, const float space_extra,
  153. const float x_scale, const float y_scale)
  154. {
  155. const float base_y = position.d_y + getBaseline(y_scale);
  156. Vector2 glyph_pos(position);
  157. for (size_t c = 0; c < text.length(); ++c)
  158. {
  159. const FontGlyph* glyph;
  160. if ((glyph = getGlyphData(text[c]))) // NB: assignment
  161. {
  162. const Image* const img = glyph->getImage();
  163. glyph_pos.d_y =
  164. base_y - (img->getOffsetY() - img->getOffsetY() * y_scale);
  165. img->draw(buffer, glyph_pos,
  166. glyph->getSize(x_scale, y_scale), clip_rect, colours);
  167. glyph_pos.d_x += glyph->getAdvance(x_scale);
  168. // apply extra spacing to space chars
  169. if (text[c] == ' ')
  170. glyph_pos.d_x += space_extra;
  171. }
  172. }
  173. }
  174. //----------------------------------------------------------------------------//
  175. void Font::setNativeResolution(const Size& size)
  176. {
  177. d_nativeHorzRes = size.d_width;
  178. d_nativeVertRes = size.d_height;
  179. // re-calculate scaling factors & notify images as required
  180. notifyDisplaySizeChanged(
  181. System::getSingleton().getRenderer()->getDisplaySize());
  182. }
  183. //----------------------------------------------------------------------------//
  184. Size Font::getNativeResolution() const
  185. {
  186. return Size(d_nativeHorzRes, d_nativeVertRes);
  187. }
  188. //----------------------------------------------------------------------------//
  189. void Font::setAutoScaled(const bool auto_scaled)
  190. {
  191. if (auto_scaled == d_autoScale)
  192. return;
  193. d_autoScale = auto_scaled;
  194. updateFont();
  195. }
  196. //----------------------------------------------------------------------------//
  197. bool Font::isAutoScaled() const
  198. {
  199. return d_autoScale;
  200. }
  201. //----------------------------------------------------------------------------//
  202. void Font::notifyDisplaySizeChanged(const Size& size)
  203. {
  204. d_horzScaling = size.d_width / d_nativeHorzRes;
  205. d_vertScaling = size.d_height / d_nativeVertRes;
  206. if (d_autoScale)
  207. updateFont();
  208. }
  209. //----------------------------------------------------------------------------//
  210. void Font::rasterise(utf32, utf32) const
  211. {
  212. // do nothing by default
  213. }
  214. //----------------------------------------------------------------------------//
  215. void Font::writeXMLToStream(XMLSerializer& xml_stream) const
  216. {
  217. // output starting <Font ... > element
  218. xml_stream.openTag("Font")
  219. .attribute(Font_xmlHandler::FontNameAttribute, d_name)
  220. .attribute(Font_xmlHandler::FontFilenameAttribute, d_filename);
  221. if (!d_resourceGroup.empty())
  222. xml_stream.attribute(Font_xmlHandler::FontResourceGroupAttribute,
  223. d_resourceGroup);
  224. if (d_nativeHorzRes != DefaultNativeHorzRes)
  225. xml_stream.attribute(Font_xmlHandler::FontNativeHorzResAttribute,
  226. PropertyHelper::uintToString(static_cast<uint>(d_nativeHorzRes)));
  227. if (d_nativeVertRes != DefaultNativeVertRes)
  228. xml_stream.attribute(Font_xmlHandler::FontNativeVertResAttribute,
  229. PropertyHelper::uintToString(static_cast<uint>(d_nativeVertRes)));
  230. if (d_autoScale)
  231. xml_stream.attribute(Font_xmlHandler::FontAutoScaledAttribute, "True");
  232. writeXMLToStream_impl(xml_stream);
  233. // output closing </Font> element.
  234. xml_stream.closeTag();
  235. }
  236. //----------------------------------------------------------------------------//
  237. } // End of CEGUI namespace section