Font.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 Texture2D;
  29. static const int FONT_TEXTURE_MIN_SIZE = 128;
  30. static const int FONT_TEXTURE_MAX_SIZE = 2048;
  31. static const int FONT_DPI = 96;
  32. /// %Font glyph description.
  33. struct FontGlyph
  34. {
  35. /// X position in texture.
  36. short x_;
  37. /// Y position in texture.
  38. short y_;
  39. /// Width.
  40. short width_;
  41. /// Height.
  42. short height_;
  43. /// Glyph X offset from origin.
  44. short offsetX_;
  45. /// Glyph Y offset from origin.
  46. short offsetY_;
  47. /// Horizontal advance.
  48. short advanceX_;
  49. /// Kerning information.
  50. HashMap<unsigned, unsigned> kerning_;
  51. };
  52. /// %Font file type.
  53. enum FONT_TYPE
  54. {
  55. FONT_TTF,
  56. FONT_IMAGE,
  57. FONT_NONE
  58. };
  59. /// %Font face description.
  60. class FontFace : public RefCounted
  61. {
  62. public:
  63. /// Construct.
  64. FontFace();
  65. /// Destruct.
  66. ~FontFace();
  67. /// Return the glyph structure corresponding to a character.
  68. const FontGlyph& GetGlyph(unsigned c) const;
  69. /// Return the kerning for a character and the next character.
  70. short GetKerning(unsigned c, unsigned d) const;
  71. /// Texture.
  72. SharedPtr<Texture2D> texture_;
  73. /// Glyphs.
  74. Vector<FontGlyph> glyphs_;
  75. /// Point size.
  76. int pointSize_;
  77. /// Row height.
  78. int rowHeight_;
  79. /// Glyph index mapping.
  80. HashMap<unsigned, unsigned> glyphMapping_;
  81. /// Kerning flag.
  82. bool hasKerning_;
  83. };
  84. class Texture2D;
  85. /// %Font resource.
  86. class Font : public Resource
  87. {
  88. OBJECT(Font);
  89. public:
  90. /// Construct.
  91. Font(Context* context);
  92. /// Destruct.
  93. virtual ~Font();
  94. /// Register object factory.
  95. static void RegisterObject(Context* context);
  96. /// Load resource. Return true if successful.
  97. virtual bool Load(Deserializer& source);
  98. const FontFace* GetFace(int pointSize);
  99. private:
  100. bool LoadTTFont(Deserializer& source);
  101. bool LoadImageFont(Deserializer& source);
  102. /// Return font face. Pack and render to a texture if not rendered yet. Return null on error.
  103. const FontFace* GetFaceTTF(int pointSize);
  104. const FontFace* GetFaceImage(int pointSize);
  105. private:
  106. /// Created faces.
  107. HashMap<int, SharedPtr<FontFace> > faces_;
  108. /// Font data.
  109. SharedArrayPtr<unsigned char> fontData_;
  110. /// Size of font data.
  111. unsigned fontDataSize_;
  112. /// Font type
  113. FONT_TYPE fontType_;
  114. /// ImageFont Data
  115. SharedPtr<FontFace> fontFace_;
  116. };
  117. }