FontFaceHandle.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. #ifndef ROCKETCOREFONTFACEHANDLE_H
  28. #define ROCKETCOREFONTFACEHANDLE_H
  29. #include <Rocket/Core/ReferenceCountable.h>
  30. #include "UnicodeRange.h"
  31. #include <Rocket/Core/Font.h>
  32. #include <Rocket/Core/FontEffect.h>
  33. #include <Rocket/Core/FontGlyph.h>
  34. #include <Rocket/Core/Geometry.h>
  35. #include <Rocket/Core/String.h>
  36. #include <Rocket/Core/Texture.h>
  37. #include <ft2build.h>
  38. #include FT_FREETYPE_H
  39. namespace Rocket {
  40. namespace Core {
  41. class FontFaceLayer;
  42. /**
  43. @author Peter Curry
  44. */
  45. class FontFaceHandle : public ReferenceCountable
  46. {
  47. public:
  48. FontFaceHandle();
  49. virtual ~FontFaceHandle();
  50. /// Initialises the handle so it is able to render text.
  51. /// @param[in] ft_face The FreeType face that this handle is rendering.
  52. /// @param[in] charset The comma-separated list of unicode ranges this handle must support.
  53. /// @param[in] size The size, in points, of the face this handle should render at.
  54. /// @return True if the handle initialised successfully and is ready for rendering, false if an error occured.
  55. bool Initialise(FT_Face ft_face, const String& charset, int size);
  56. /// Returns the average advance of all glyphs in this font face.
  57. /// @return An approximate width of the characters in this font face.
  58. int GetCharacterWidth() const;
  59. /// Returns the point size of this font face.
  60. /// @return The face's point size.
  61. int GetSize() const;
  62. /// Returns the pixel height of a lower-case x in this font face.
  63. /// @return The height of a lower-case x.
  64. int GetXHeight() const;
  65. /// Returns the default height between this font face's baselines.
  66. /// @return The default line height.
  67. int GetLineHeight() const;
  68. /// Returns the font's baseline, as a pixel offset from the bottom of the font.
  69. /// @return The font's baseline.
  70. int GetBaseline() const;
  71. /// Returns the font's glyphs.
  72. /// @return The font's glyphs.
  73. const FontGlyphList& GetGlyphs() const;
  74. /// Returns the width a string will take up if rendered with this handle.
  75. /// @param[in] string The string to measure.
  76. /// @param[in] prior_character The optionally-specified character that immediately precedes the string. This may have an impact on the string width due to kerning.
  77. /// @return The width, in pixels, this string will occupy if rendered with this handle.
  78. int GetStringWidth(const WString& string, word prior_character = 0) const;
  79. /// Generates, if required, the layer configuration for a given array of font effects.
  80. /// @param[in] font_effects The list of font effects to generate the configuration for.
  81. /// @return The index to use when generating geometry using this configuration.
  82. int GenerateLayerConfiguration(FontEffectMap& font_effects);
  83. /// Generates the texture data for a layer (for the texture database).
  84. /// @param[out] texture_data The pointer to be set to the generated texture data.
  85. /// @param[out] texture_dimensions The dimensions of the texture.
  86. /// @param[in] layer_id The id of the layer to request the texture data from.
  87. /// @param[in] texture_id The index of the texture within the layer to generate.
  88. bool GenerateLayerTexture(const byte*& texture_data, Vector2i& texture_dimensions, FontEffect* layer_id, int texture_id);
  89. /// Generates the geometry required to render a single line of text.
  90. /// @param[out] geometry An array of geometries to generate the geometry into.
  91. /// @param[in] string The string to render.
  92. /// @param[in] position The position of the baseline of the first character to render.
  93. /// @param[in] colour The colour to render the text.
  94. /// @return The width, in pixels, of the string geometry.
  95. int GenerateString(GeometryList& geometry, const WString& string, const Vector2f& position, const Colourb& colour, int layer_configuration = 0) const;
  96. /// Generates the geometry required to render a line above, below or through a line of text.
  97. /// @param[out] geometry The geometry to append the newly created geometry into.
  98. /// @param[in] position The position of the baseline of the lined text.
  99. /// @param[in] width The width of the string to line.
  100. /// @param[in] height The height to render the line at.
  101. /// @param[in] colour The colour to draw the line in.
  102. void GenerateLine(Geometry* geometry, const Vector2f& position, int width, Font::Line height, const Colourb& colour) const;
  103. /// Returns the font face's raw charset (the charset range as a string).
  104. /// @return The font face's charset.
  105. const String& GetRawCharset() const;
  106. /// Returns the font face's charset.
  107. /// @return The font face's charset.
  108. const UnicodeRangeList& GetCharset() const;
  109. protected:
  110. /// Destroys the handle.
  111. virtual void OnReferenceDeactivate();
  112. private:
  113. void GenerateMetrics(void);
  114. void BuildGlyphMap(const UnicodeRange& unicode_range);
  115. void BuildGlyph(FontGlyph& glyph, FT_GlyphSlot ft_glyph);
  116. int GetKerning(word lhs, word rhs) const;
  117. // Generates (or shares) a layer derived from a font effect.
  118. FontFaceLayer* GenerateLayer(FontEffect* font_effect);
  119. typedef std::vector< int > GlyphKerningList;
  120. typedef std::vector< GlyphKerningList > FontKerningList;
  121. FT_Face ft_face;
  122. FontGlyphList glyphs;
  123. typedef std::map< const FontEffect*, FontFaceLayer* > FontLayerMap;
  124. typedef std::map< String, FontFaceLayer* > FontLayerCache;
  125. typedef std::vector< FontFaceLayer* > LayerConfiguration;
  126. typedef std::vector< LayerConfiguration > LayerConfigurationList;
  127. // The list of all font layers, index by the effect that instanced them.
  128. FontFaceLayer* base_layer;
  129. FontLayerMap layers;
  130. // Each font layer that generated geometry or textures, indexed by the respective generation
  131. // key.
  132. FontLayerCache layer_cache;
  133. // All configurations currently in use on this handle. New configurations will be generated as
  134. // required.
  135. LayerConfigurationList layer_configurations;
  136. // The average advance (in pixels) of all of this face's glyphs.
  137. int average_advance;
  138. int size;
  139. int x_height;
  140. int line_height;
  141. int baseline;
  142. float underline_position;
  143. float underline_thickness;
  144. String raw_charset;
  145. UnicodeRangeList charset;
  146. unsigned int max_codepoint;
  147. };
  148. }
  149. }
  150. #endif