Font.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. //
  2. // Copyright (c) 2008-2014 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #pragma once
  23. #include "AreaAllocator.h"
  24. #include "ArrayPtr.h"
  25. #include "List.h"
  26. #include "Resource.h"
  27. namespace Urho3D
  28. {
  29. class Font;
  30. class FreeTypeLibrary;
  31. class Graphics;
  32. class Image;
  33. class Texture2D;
  34. static const int FONT_TEXTURE_MIN_SIZE = 128;
  35. static const int FONT_DPI = 96;
  36. /// Mutable font glyph description.
  37. struct MutableGlyph
  38. {
  39. /// Construct.
  40. MutableGlyph();
  41. /// The actual glyph index that currently occupies this mutable slot. M_MAX_UNSIGNED if none.
  42. unsigned glyphIndex_;
  43. /// X position in texture.
  44. short x_;
  45. /// Y position in texture.
  46. short y_;
  47. };
  48. /// %Font glyph description.
  49. struct FontGlyph
  50. {
  51. /// Construct.
  52. FontGlyph();
  53. /// X position in texture.
  54. short x_;
  55. /// Y position in texture.
  56. short y_;
  57. /// Width.
  58. short width_;
  59. /// Height.
  60. short height_;
  61. /// Glyph X offset from origin.
  62. short offsetX_;
  63. /// Glyph Y offset from origin.
  64. short offsetY_;
  65. /// Horizontal advance.
  66. short advanceX_;
  67. /// Texture page. M_MAX_UNSIGNED if not yet resident on any texture.
  68. unsigned page_;
  69. /// Used flag.
  70. bool used_;
  71. /// Kerning information.
  72. HashMap<unsigned, unsigned> kerning_;
  73. /// Mutable glyph list iterator.
  74. List<MutableGlyph*>::Iterator iterator_;
  75. };
  76. /// %Font file type.
  77. enum FONT_TYPE
  78. {
  79. FONT_NONE = 0,
  80. FONT_FREETYPE,
  81. FONT_BITMAP,
  82. MAX_FONT_TYPES
  83. };
  84. /// %Font face description.
  85. class URHO3D_API FontFace : public RefCounted
  86. {
  87. friend class Font;
  88. public:
  89. /// Construct.
  90. FontFace(Font* font);
  91. /// Destruct.
  92. ~FontFace();
  93. /// Return pointer to the glyph structure corresponding to a character. Return null if glyph not found.
  94. const FontGlyph* GetGlyph(unsigned c);
  95. /// Return the kerning for a character and the next character.
  96. short GetKerning(unsigned c, unsigned d) const;
  97. /// Return true when one of the texture has a data loss.
  98. bool IsDataLost() const;
  99. /// Return point size.
  100. int GetPointSize() const { return pointSize_; }
  101. /// Return row height.
  102. int GetRowHeight() const { return rowHeight_; }
  103. /// Return textures.
  104. const Vector<SharedPtr<Texture2D> >& GetTextures() const { return textures_; }
  105. /// Return if font face uses mutable glyphs.
  106. bool HasMutableGlyphs() const { return !mutableGlyphs_.Empty(); }
  107. private:
  108. /// Render all glyphs of the face into a single texture. Return true if could fit them. Called by Font.
  109. bool RenderAllGlyphs(int maxWidth, int maxHeight);
  110. /// Render one glyph on demand into the current texture.
  111. void RenderGlyph(unsigned index);
  112. /// Render a glyph bitmap into a memory buffer.
  113. void RenderGlyphBitmap(unsigned index, unsigned char* dest, unsigned pitch, int loadMode);
  114. /// Setup next texture for dynamic glyph rendering.
  115. void SetupNextTexture(int width, int height);
  116. /// Setup mutable glyph rendering, in which glyphs form a regular-sized grid.
  117. void SetupMutableGlyphs(int textureWidth, int textureHeight, int maxGlyphWidth, int maxGlyphHeight);
  118. /// Parent font.
  119. Font* font_;
  120. /// FreeType face. Non-null after creation only in dynamic mode.
  121. void* face_;
  122. /// Glyphs.
  123. Vector<FontGlyph> glyphs_;
  124. /// Glyph index mapping.
  125. HashMap<unsigned, unsigned> glyphMapping_;
  126. /// Glyph texture pages.
  127. Vector<SharedPtr<Texture2D> > textures_;
  128. /// Mutable glyph list.
  129. List<MutableGlyph*> mutableGlyphs_;
  130. /// Point size.
  131. int pointSize_;
  132. /// Row height.
  133. int rowHeight_;
  134. /// Mutable glyph cell width, including 1 pixel padding.
  135. int cellWidth_;
  136. /// Mutable glyph cell height, including 1 pixel padding.
  137. int cellHeight_;
  138. /// Kerning flag.
  139. bool hasKerning_;
  140. /// Glyph area allocator.
  141. AreaAllocator allocator_;
  142. /// Glyph rendering bitmap in dynamic mode.
  143. SharedArrayPtr<unsigned char> bitmap_;
  144. /// Glyph rendering bitmap byte size.
  145. unsigned bitmapSize_;
  146. };
  147. /// %Font resource.
  148. class URHO3D_API Font : public Resource
  149. {
  150. OBJECT(Font);
  151. public:
  152. /// Construct.
  153. Font(Context* context);
  154. /// Destruct.
  155. virtual ~Font();
  156. /// Register object factory.
  157. static void RegisterObject(Context* context);
  158. /// Load resource. Return true if successful.
  159. virtual bool Load(Deserializer& source);
  160. /// Save resource as a new bitmap font type in XML format. Return true if successful.
  161. bool SaveXML(Serializer& dest, int pointSize, bool usedGlyphs = false);
  162. /// Return font face. Pack and render to a texture if not rendered yet. Return null on error.
  163. FontFace* GetFace(int pointSize);
  164. /// Release font faces and recreate them next time when requested. Called when font textures lost or global font properties change.
  165. void ReleaseFaces();
  166. /// Create a texture for font rendering.
  167. SharedPtr<Texture2D> CreateFaceTexture();
  168. /// Load font face texture from image resource.
  169. SharedPtr<Texture2D> LoadFaceTexture(SharedPtr<Image> image);
  170. private:
  171. /// Return font face using FreeTyp. Called internally. Return null on error.
  172. FontFace* GetFaceFreeType(int pointSize);
  173. /// Return bitmap font face. Called internally. Return null on error.
  174. FontFace* GetFaceBitmap(int pointSize);
  175. /// Convert graphics format to number of components.
  176. unsigned ConvertFormatToNumComponents(unsigned format);
  177. /// Pack used glyphs into smallest texture size and smallest number of texture.
  178. SharedPtr<FontFace> Pack(FontFace* fontFace);
  179. /// Save font face texture as image resource.
  180. SharedPtr<Image> SaveFaceTexture(Texture2D* texture);
  181. /// Save font face texture as image file.
  182. bool SaveFaceTexture(Texture2D* texture, const String& fileName);
  183. /// FreeType library.
  184. SharedPtr<FreeTypeLibrary> freeType_;
  185. /// Created faces.
  186. HashMap<int, SharedPtr<FontFace> > faces_;
  187. /// Font data.
  188. SharedArrayPtr<unsigned char> fontData_;
  189. /// Size of font data.
  190. unsigned fontDataSize_;
  191. /// Font type.
  192. FONT_TYPE fontType_;
  193. };
  194. }