Font.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2011 Lasse Öörni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #pragma once
  24. #include "ArrayPtr.h"
  25. #include "Resource.h"
  26. class Graphics;
  27. class Texture;
  28. static const int MAX_FONT_CHARS = 256;
  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. PODVector<short> kerning_;
  51. };
  52. /// %Font face description.
  53. class FontFace : public RefCounted
  54. {
  55. public:
  56. /// Construct.
  57. FontFace();
  58. /// Destruct.
  59. ~FontFace();
  60. /// Return the glyph structure corresponding to a character.
  61. const FontGlyph& GetGlyph(unsigned char c) const;
  62. /// Return the kerning for a character and the next character.
  63. short GetKerning(unsigned char c, unsigned char d) const;
  64. /// Texture.
  65. SharedPtr<Texture> texture_;
  66. /// Glyphs.
  67. Vector<FontGlyph> glyphs_;
  68. /// Point size.
  69. int pointSize_;
  70. /// Row height.
  71. int rowHeight_;
  72. /// Glyph index mapping.
  73. unsigned short glyphIndex_[MAX_FONT_CHARS];
  74. /// Kerning flag.
  75. bool hasKerning_;
  76. };
  77. /// %Font resource.
  78. class Font : public Resource
  79. {
  80. OBJECT(Font);
  81. public:
  82. /// Construct.
  83. Font(Context* context);
  84. /// Destruct.
  85. virtual ~Font();
  86. /// Register object factory.
  87. static void RegisterObject(Context* context);
  88. /// Load resource. Return true if successful.
  89. virtual bool Load(Deserializer& source);
  90. /// Return font face. Pack and render to a texture if not rendered yet. Return null on error.
  91. const FontFace* GetFace(int pointSize);
  92. private:
  93. /// Created faces.
  94. Map<int, SharedPtr<FontFace> > faces_;
  95. /// Font data.
  96. SharedArrayPtr<unsigned char> fontData_;
  97. /// Size of font data.
  98. unsigned fontDataSize_;
  99. };