FontFaceHandle.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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 ROCKETCOREBITMAPFONTFONTFACEHANDLE_H
  28. #define ROCKETCOREBITMAPFONTFONTFACEHANDLE_H
  29. #include "../UnicodeRange.h"
  30. #include "../../../Include/Rocket/Core/Font.h"
  31. #include "../../../Include/Rocket/Core/FontEffect.h"
  32. #include "../../../Include/Rocket/Core/FontGlyph.h"
  33. #include "../../../Include/Rocket/Core/Geometry.h"
  34. #include "../../../Include/Rocket/Core/String.h"
  35. #include "../../../Include/Rocket/Core/Texture.h"
  36. #include "../FontFaceHandle.h"
  37. #include "BitmapFontDefinitions.h"
  38. namespace Rocket {
  39. namespace Core {
  40. namespace BitmapFont {
  41. /**
  42. @author Peter Curry
  43. */
  44. class FontFaceHandle : public Rocket::Core::FontFaceHandle
  45. {
  46. public:
  47. FontFaceHandle();
  48. virtual ~FontFaceHandle();
  49. /// Initialises the handle so it is able to render text.
  50. /// @param[in] ft_face The FreeType face that this handle is rendering.
  51. /// @param[in] charset The comma-separated list of unicode ranges this handle must support.
  52. /// @param[in] size The size, in points, of the face this handle should render at.
  53. /// @return True if the handle initialised successfully and is ready for rendering, false if an error occured.
  54. bool Initialise(BitmapFontDefinitions *bm_face, const String& charset, int size);
  55. /// Returns the width a string will take up if rendered with this handle.
  56. /// @param[in] string The string to measure.
  57. /// @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.
  58. /// @return The width, in pixels, this string will occupy if rendered with this handle.
  59. int GetStringWidth(const WString& string, word prior_character = 0) const;
  60. /// Generates, if required, the layer configuration for a given array of font effects.
  61. /// @param[in] font_effects The list of font effects to generate the configuration for.
  62. /// @return The index to use when generating geometry using this configuration.
  63. int GenerateLayerConfiguration(Rocket::Core::FontEffectMap& font_effects);
  64. /// Generates the texture data for a layer (for the texture database).
  65. /// @param[out] texture_data The pointer to be set to the generated texture data.
  66. /// @param[out] texture_dimensions The dimensions of the texture.
  67. /// @param[in] layer_id The id of the layer to request the texture data from.
  68. /// @param[in] texture_id The index of the texture within the layer to generate.
  69. bool GenerateLayerTexture(const byte*& texture_data, Vector2i& texture_dimensions, FontEffect* layer_id, int texture_id);
  70. /// Generates the geometry required to render a single line of text.
  71. /// @param[out] geometry An array of geometries to generate the geometry into.
  72. /// @param[in] string The string to render.
  73. /// @param[in] position The position of the baseline of the first character to render.
  74. /// @param[in] colour The colour to render the text.
  75. /// @return The width, in pixels, of the string geometry.
  76. int GenerateString(GeometryList& geometry, const WString& string, const Vector2f& position, const Colourb& colour, int layer_configuration = 0) const;
  77. /// Generates the geometry required to render a line above, below or through a line of text.
  78. /// @param[out] geometry The geometry to append the newly created geometry into.
  79. /// @param[in] position The position of the baseline of the lined text.
  80. /// @param[in] width The width of the string to line.
  81. /// @param[in] height The height to render the line at.
  82. /// @param[in] colour The colour to draw the line in.
  83. void GenerateLine(Geometry* geometry, const Vector2f& position, int width, Font::Line height, const Colourb& colour) const;
  84. const String & GetTextureSource() const
  85. {
  86. return texture_source;
  87. }
  88. unsigned int GetTextureWidth() const
  89. {
  90. return texture_width;
  91. }
  92. unsigned int GetTextureHeight() const
  93. {
  94. return texture_height;
  95. }
  96. protected:
  97. /// Destroys the handle.
  98. virtual void OnReferenceDeactivate();
  99. private:
  100. void GenerateMetrics(BitmapFontDefinitions *bm_face);
  101. void BuildGlyphMap(BitmapFontDefinitions *bm_face, const UnicodeRange& unicode_range);
  102. void BuildGlyph(FontGlyph& glyph, CharacterInfo *ft_glyph);
  103. int GetKerning(word lhs, word rhs) const;
  104. // Generates (or shares) a layer derived from a font effect.
  105. virtual FontFaceLayer* GenerateLayer(FontEffect* font_effect);
  106. BitmapFontDefinitions * bm_face;
  107. String texture_source;
  108. String texture_directory;
  109. unsigned int texture_width;
  110. unsigned int texture_height;
  111. };
  112. }
  113. }
  114. }
  115. #endif