Font.h 3.2 KB

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