Font.h 3.2 KB

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