FontFaceHandle.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. /*
  2. * This source file is part of libRocket, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://www.librocket.com
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. *
  26. */
  27. #include "precompiled.h"
  28. #include "FontFaceHandle.h"
  29. #include <algorithm>
  30. #include "../../Include/Rocket/Core.h"
  31. #include "FontFaceLayer.h"
  32. #include "TextureLayout.h"
  33. namespace Rocket {
  34. namespace Core {
  35. class FontEffectSort
  36. {
  37. public:
  38. bool operator()(const FontEffect* lhs, const FontEffect* rhs)
  39. {
  40. return lhs->GetZIndex() < rhs->GetZIndex();
  41. }
  42. };
  43. FontFaceHandle::FontFaceHandle()
  44. {
  45. size = 0;
  46. average_advance = 0;
  47. x_height = 0;
  48. line_height = 0;
  49. baseline = 0;
  50. underline_position = 0;
  51. underline_thickness = 0;
  52. base_layer = NULL;
  53. }
  54. FontFaceHandle::~FontFaceHandle()
  55. {
  56. for (FontGlyphList::iterator i = glyphs.begin(); i != glyphs.end(); ++i)
  57. delete[] i->bitmap_data;
  58. for (FontLayerMap::iterator i = layers.begin(); i != layers.end(); ++i)
  59. delete i->second;
  60. }
  61. // Returns the point size of this font face.
  62. int FontFaceHandle::GetSize() const
  63. {
  64. return size;
  65. }
  66. // Returns the average advance of all glyphs in this font face.
  67. int FontFaceHandle::GetCharacterWidth() const
  68. {
  69. return average_advance;
  70. }
  71. // Returns the pixel height of a lower-case x in this font face.
  72. int FontFaceHandle::GetXHeight() const
  73. {
  74. return x_height;
  75. }
  76. // Returns the default height between this font face's baselines.
  77. int FontFaceHandle::GetLineHeight() const
  78. {
  79. return line_height;
  80. }
  81. // Returns the font's baseline.
  82. int FontFaceHandle::GetBaseline() const
  83. {
  84. return baseline;
  85. }
  86. // Returns the font's glyphs.
  87. const FontGlyphList& FontFaceHandle::GetGlyphs() const
  88. {
  89. return glyphs;
  90. }
  91. // Returns the width a string will take up if rendered with this handle.
  92. int FontFaceHandle::GetStringWidth(const WString& string, word prior_character) const
  93. {
  94. int width = 0;
  95. for (size_t i = 0; i < string.Length(); i++)
  96. {
  97. word character_code = string[i];
  98. if (character_code >= glyphs.size())
  99. continue;
  100. const FontGlyph &glyph = glyphs[character_code];
  101. // Adjust the cursor for the kerning between this character and the previous one.
  102. if (prior_character != 0)
  103. width += GetKerning(prior_character, string[i]);
  104. // Adjust the cursor for this character's advance.
  105. width += glyph.advance;
  106. prior_character = character_code;
  107. }
  108. return width;
  109. }
  110. // Generates, if required, the layer configuration for a given array of font effects.
  111. int FontFaceHandle::GenerateLayerConfiguration(FontEffectMap& font_effects)
  112. {
  113. if (font_effects.empty())
  114. return 0;
  115. // Prepare a list of effects, sorted by z-index.
  116. FontEffectList sorted_effects;
  117. for (FontEffectMap::const_iterator i = font_effects.begin(); i != font_effects.end(); ++i)
  118. sorted_effects.push_back(i->second);
  119. std::sort(sorted_effects.begin(), sorted_effects.end(), FontEffectSort());
  120. // Check each existing configuration for a match with this arrangement of effects.
  121. int configuration_index = 1;
  122. for (; configuration_index < (int) layer_configurations.size(); ++configuration_index)
  123. {
  124. const LayerConfiguration& configuration = layer_configurations[configuration_index];
  125. // Check the size is correct. For a math, there should be one layer in the configuration
  126. // plus an extra for the base layer.
  127. if (configuration.size() != sorted_effects.size() + 1)
  128. continue;
  129. // Check through each layer, checking it was created by the same effect as the one we're
  130. // checking.
  131. size_t effect_index = 0;
  132. for (size_t i = 0; i < configuration.size(); ++i)
  133. {
  134. // Skip the base layer ...
  135. if (configuration[i]->GetFontEffect() == NULL)
  136. continue;
  137. // If the ith layer's effect doesn't match the equivalent effect, then this
  138. // configuration can't match.
  139. if (configuration[i]->GetFontEffect() != sorted_effects[effect_index])
  140. break;
  141. // Check the next one ...
  142. ++effect_index;
  143. }
  144. if (effect_index == sorted_effects.size())
  145. return configuration_index;
  146. }
  147. // No match, so we have to generate a new layer configuration.
  148. layer_configurations.push_back(LayerConfiguration());
  149. LayerConfiguration& layer_configuration = layer_configurations.back();
  150. bool added_base_layer = false;
  151. for (size_t i = 0; i < sorted_effects.size(); ++i)
  152. {
  153. if (!added_base_layer &&
  154. sorted_effects[i]->GetZIndex() >= 0)
  155. {
  156. layer_configuration.push_back(base_layer);
  157. added_base_layer = true;
  158. }
  159. layer_configuration.push_back(GenerateLayer(sorted_effects[i]));
  160. }
  161. // Add the base layer now if we still haven't added it.
  162. if (!added_base_layer)
  163. layer_configuration.push_back(base_layer);
  164. return (int) (layer_configurations.size() - 1);
  165. }
  166. // Generates the texture data for a layer (for the texture database).
  167. bool FontFaceHandle::GenerateLayerTexture(const byte*& texture_data, Vector2i& texture_dimensions, FontEffect* layer_id, int texture_id)
  168. {
  169. FontLayerMap::iterator layer_iterator = layers.find(layer_id);
  170. if (layer_iterator == layers.end())
  171. return false;
  172. return layer_iterator->second->GenerateTexture(texture_data, texture_dimensions, texture_id);
  173. }
  174. // Generates the geometry required to render a single line of text.
  175. int FontFaceHandle::GenerateString(GeometryList& geometry, const WString& string, const Vector2f& position, const Colourb& colour, int layer_configuration_index) const
  176. {
  177. int geometry_index = 0;
  178. int line_width = 0;
  179. ROCKET_ASSERT(layer_configuration_index >= 0);
  180. ROCKET_ASSERT(layer_configuration_index < (int) layer_configurations.size());
  181. // Fetch the requested configuration and generate the geometry for each one.
  182. const LayerConfiguration& layer_configuration = layer_configurations[layer_configuration_index];
  183. for (size_t i = 0; i < layer_configuration.size(); ++i)
  184. {
  185. FontFaceLayer* layer = layer_configuration[i];
  186. Colourb layer_colour;
  187. if (layer == base_layer)
  188. layer_colour = colour;
  189. else
  190. layer_colour = layer->GetColour();
  191. // Resize the geometry list if required.
  192. if ((int) geometry.size() < geometry_index + layer->GetNumTextures())
  193. geometry.resize(geometry_index + layer->GetNumTextures());
  194. // Bind the textures to the geometries.
  195. for (int i = 0; i < layer->GetNumTextures(); ++i)
  196. geometry[geometry_index + i].SetTexture(layer->GetTexture(i));
  197. line_width = 0;
  198. word prior_character = 0;
  199. const word* string_iterator = string.CString();
  200. const word* string_end = string.CString() + string.Length();
  201. for (; string_iterator != string_end; string_iterator++)
  202. {
  203. if (*string_iterator >= glyphs.size())
  204. continue;
  205. const FontGlyph &glyph = glyphs[*string_iterator];
  206. // Adjust the cursor for the kerning between this character and the previous one.
  207. if (prior_character != 0)
  208. line_width += GetKerning(prior_character, *string_iterator);
  209. layer->GenerateGeometry(&geometry[geometry_index], *string_iterator, Vector2f(position.x + line_width, position.y), layer_colour);
  210. line_width += glyph.advance;
  211. prior_character = *string_iterator;
  212. }
  213. geometry_index += layer->GetNumTextures();
  214. }
  215. // Cull any excess geometry from a previous generation.
  216. geometry.resize(geometry_index);
  217. return line_width;
  218. }
  219. // Generates the geometry required to render a line above, below or through a line of text.
  220. void FontFaceHandle::GenerateLine(Geometry* geometry, const Vector2f& position, int width, Font::Line height, const Colourb& colour) const
  221. {
  222. std::vector< Vertex >& line_vertices = geometry->GetVertices();
  223. std::vector< int >& line_indices = geometry->GetIndices();
  224. float offset;
  225. switch (height)
  226. {
  227. case Font::UNDERLINE: offset = -underline_position; break;
  228. case Font::OVERLINE: // where to place? offset = -line_height - underline_position; break;
  229. case Font::STRIKE_THROUGH: // where to place? offset = -line_height * 0.5f; break;
  230. default: return;
  231. }
  232. line_vertices.resize(line_vertices.size() + 4);
  233. line_indices.resize(line_indices.size() + 6);
  234. GeometryUtilities::GenerateQuad(&line_vertices[0] + (line_vertices.size() - 4), &line_indices[0] + (line_indices.size() - 6), Vector2f(position.x, position.y + offset), Vector2f((float) width, underline_thickness), colour, (int)line_vertices.size() - 4);
  235. }
  236. // Returns the font face's raw charset (the charset range as a string).
  237. const String& FontFaceHandle::GetRawCharset() const
  238. {
  239. return raw_charset;
  240. }
  241. // Returns the font face's charset.
  242. const UnicodeRangeList& FontFaceHandle::GetCharset() const
  243. {
  244. return charset;
  245. }
  246. // Destroys the handle.
  247. void FontFaceHandle::OnReferenceDeactivate()
  248. {
  249. delete this;
  250. }
  251. // Generates (or shares) a layer derived from a font effect.
  252. FontFaceLayer* FontFaceHandle::GenerateLayer(FontEffect* font_effect)
  253. {
  254. // See if this effect has been instanced before, as part of a different configuration.
  255. FontLayerMap::iterator i = layers.find(font_effect);
  256. if (i != layers.end())
  257. return i->second;
  258. FontFaceLayer* layer = new FontFaceLayer();
  259. layers[font_effect] = layer;
  260. if (font_effect == NULL)
  261. {
  262. layer->Initialise(this);
  263. }
  264. else
  265. {
  266. // Determine which, if any, layer the new layer should copy its geometry and textures from.
  267. FontFaceLayer* clone = NULL;
  268. bool deep_clone = true;
  269. String generation_key;
  270. if (!font_effect->HasUniqueTexture())
  271. {
  272. clone = base_layer;
  273. deep_clone = false;
  274. }
  275. else
  276. {
  277. generation_key = font_effect->GetName() + ";" + font_effect->GetGenerationKey();
  278. FontLayerCache::iterator cache_iterator = layer_cache.find(generation_key);
  279. if (cache_iterator != layer_cache.end())
  280. clone = cache_iterator->second;
  281. }
  282. // Create a new layer.
  283. layer->Initialise(this, font_effect, clone, deep_clone);
  284. // Cache the layer in the layer cache if it generated its own textures (ie, didn't clone).
  285. if (clone == NULL)
  286. layer_cache[generation_key] = layer;
  287. }
  288. return layer;
  289. }
  290. }
  291. }