FontFace.h 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. // Copyright (c) 2008-2023 the Urho3D project
  2. // License: MIT
  3. #pragma once
  4. #include "../Container/ArrayPtr.h"
  5. #include "../Math/AreaAllocator.h"
  6. namespace Urho3D
  7. {
  8. class Font;
  9. class Image;
  10. class Texture2D;
  11. /// %Font glyph description.
  12. struct URHO3D_API FontGlyph
  13. {
  14. /// X position in texture.
  15. short x_{};
  16. /// Y position in texture.
  17. short y_{};
  18. /// Width in texture.
  19. short texWidth_{};
  20. /// Height in texture.
  21. short texHeight_{};
  22. /// Width on screen.
  23. float width_{};
  24. /// Height on screen.
  25. float height_{};
  26. /// Glyph X offset from origin.
  27. float offsetX_{};
  28. /// Glyph Y offset from origin.
  29. float offsetY_{};
  30. /// Horizontal advance.
  31. float advanceX_{};
  32. /// Texture page. NINDEX if not yet resident on any texture.
  33. i32 page_{NINDEX};
  34. /// Used flag.
  35. bool used_{};
  36. };
  37. /// %Font face description.
  38. class URHO3D_API FontFace : public RefCounted
  39. {
  40. friend class Font;
  41. public:
  42. /// Construct.
  43. explicit FontFace(Font* font);
  44. /// Destruct.
  45. ~FontFace() override;
  46. /// Load font face.
  47. virtual bool Load(const unsigned char* fontData, unsigned fontDataSize, float pointSize) = 0;
  48. /// Return pointer to the glyph structure corresponding to a character. Return null if glyph not found.
  49. virtual const FontGlyph* GetGlyph(c32 c);
  50. /// Return if font face uses mutable glyphs.
  51. virtual bool HasMutableGlyphs() const { return false; }
  52. /// Return the kerning for a character and the next character.
  53. float GetKerning(c32 c, c32 d) const;
  54. /// Return true when one of the texture has a data loss.
  55. bool IsDataLost() const;
  56. /// Return point size.
  57. float GetPointSize() const { return pointSize_; }
  58. /// Return row height.
  59. float GetRowHeight() const { return rowHeight_; }
  60. /// Return textures.
  61. const Vector<SharedPtr<Texture2D>>& GetTextures() const { return textures_; }
  62. protected:
  63. friend class FontFaceBitmap;
  64. /// Create a texture for font rendering.
  65. SharedPtr<Texture2D> CreateFaceTexture();
  66. /// Load font face texture from image resource.
  67. SharedPtr<Texture2D> LoadFaceTexture(const SharedPtr<Image>& image);
  68. /// Parent font.
  69. Font* font_{};
  70. /// Glyph mapping.
  71. HashMap<c32, FontGlyph> glyphMapping_;
  72. /// Kerning mapping.
  73. HashMap<u32, float> kerningMapping_;
  74. /// Glyph texture pages.
  75. Vector<SharedPtr<Texture2D>> textures_;
  76. /// Point size.
  77. float pointSize_{};
  78. /// Row height.
  79. float rowHeight_{};
  80. };
  81. }