Font.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. //
  2. // Copyright (c) 2008-2013 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 "ArrayPtr.h"
  24. #include "Resource.h"
  25. namespace Urho3D
  26. {
  27. class Graphics;
  28. class Image;
  29. class Texture;
  30. static const int FONT_TEXTURE_MIN_SIZE = 128;
  31. static const int FONT_TEXTURE_MAX_SIZE = 2048;
  32. static const int FONT_DPI = 96;
  33. /// %Font glyph description.
  34. struct FontGlyph
  35. {
  36. /// Construct.
  37. FontGlyph();
  38. /// X position in texture.
  39. short x_;
  40. /// Y position in texture.
  41. short y_;
  42. /// Width.
  43. short width_;
  44. /// Height.
  45. short height_;
  46. /// Glyph X offset from origin.
  47. short offsetX_;
  48. /// Glyph Y offset from origin.
  49. short offsetY_;
  50. /// Horizontal advance.
  51. short advanceX_;
  52. /// Page.
  53. unsigned page_;
  54. /// Kerning information.
  55. HashMap<unsigned, unsigned> kerning_;
  56. /// Used flag.
  57. mutable bool used_;
  58. };
  59. /// %Font file type.
  60. enum FONT_TYPE
  61. {
  62. FONT_NONE = 0,
  63. FONT_TTF,
  64. FONT_BITMAP,
  65. MAX_FONT_TYPES
  66. };
  67. /// %Font face description.
  68. class URHO3D_API FontFace : public RefCounted
  69. {
  70. public:
  71. /// Construct.
  72. FontFace();
  73. /// Destruct.
  74. ~FontFace();
  75. /// Return pointer to the glyph structure corresponding to a character. Return null if glyph not found.
  76. const FontGlyph* GetGlyph(unsigned c) const;
  77. /// Return the kerning for a character and the next character.
  78. short GetKerning(unsigned c, unsigned d) const;
  79. /// Return true when one of the texture has a data loss.
  80. bool IsDataLost() const;
  81. /// Texture.
  82. Vector<SharedPtr<Texture> > textures_;
  83. /// Glyphs.
  84. Vector<FontGlyph> glyphs_;
  85. /// Point size.
  86. int pointSize_;
  87. /// Row height.
  88. int rowHeight_;
  89. /// Glyph index mapping.
  90. HashMap<unsigned, unsigned> glyphMapping_;
  91. /// Kerning flag.
  92. bool hasKerning_;
  93. };
  94. /// %Font resource.
  95. class URHO3D_API Font : public Resource
  96. {
  97. OBJECT(Font);
  98. public:
  99. /// Construct.
  100. Font(Context* context);
  101. /// Destruct.
  102. virtual ~Font();
  103. /// Register object factory.
  104. static void RegisterObject(Context* context);
  105. /// Load resource. Return true if successful.
  106. virtual bool Load(Deserializer& source);
  107. /// Save resource as a new bitmap font type in XML format. Return true if successful.
  108. bool SaveXML(Serializer& dest, int pointSize, bool usedGlyphs = false);
  109. /// Return font face. Pack and render to a texture if not rendered yet. Return null on error.
  110. const FontFace* GetFace(int pointSize);
  111. private:
  112. /// Return True-type font face. Called internally. Return null on error.
  113. const FontFace* GetFaceTTF(int pointSize);
  114. /// Return bitmap font face. Called internally. Return null on error.
  115. const FontFace* GetFaceBitmap(int pointSize);
  116. /// Convert graphics format to number of components.
  117. unsigned ConvertFormatToNumComponents(unsigned format);
  118. /// Pack used glyphs into smallest texture size and smallest number of texture.
  119. SharedPtr<FontFace> Pack(const FontFace* fontFace);
  120. /// Load font face texture from image resource.
  121. SharedPtr<Texture> LoadFaceTexture(SharedPtr<Image> image);
  122. /// Save font face texture as image resource.
  123. SharedPtr<Image> SaveFaceTexture(Texture* texture);
  124. /// Save font face texture as image file.
  125. bool SaveFaceTexture(Texture* texture, const String& fileName);
  126. /// Created faces.
  127. HashMap<int, SharedPtr<FontFace> > faces_;
  128. /// Font data.
  129. SharedArrayPtr<unsigned char> fontData_;
  130. /// Size of font data.
  131. unsigned fontDataSize_;
  132. /// Font type.
  133. FONT_TYPE fontType_;
  134. };
  135. }