Font.h 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 "AreaAllocator.h"
  25. #include "Resource.h"
  26. #include "SharedArrayPtr.h"
  27. class Graphics;
  28. class Texture;
  29. static const int MAX_FONT_CHARS = 256;
  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. };
  51. /// Font face description
  52. struct FontFace
  53. {
  54. /// Point size
  55. int pointSize_;
  56. /// Row height
  57. int rowHeight_;
  58. /// Texture
  59. SharedPtr<Texture> texture_;
  60. /// Glyph index mapping
  61. unsigned short glyphIndex_[MAX_FONT_CHARS];
  62. /// Glyphs
  63. std::vector<FontGlyph> glyphs_;
  64. };
  65. /// Font resource
  66. class Font : public Resource
  67. {
  68. OBJECT(Font);
  69. public:
  70. /// Construct
  71. Font(Context* context);
  72. /// Destruct
  73. virtual ~Font();
  74. /// Register object factory
  75. static void RegisterObject(Context* context);
  76. /// Load resource. Return true if successful
  77. virtual bool Load(Deserializer& source);
  78. /// Return font face. Pack and render to a texture if not rendered yet. Return null on error
  79. const FontFace* GetFace(int pointSize);
  80. private:
  81. /// Created faces
  82. std::map<int, FontFace> faces_;
  83. /// Font data
  84. SharedArrayPtr<unsigned char> fontData_;
  85. /// Size of font data
  86. unsigned fontDataSize_;
  87. };