Font.h 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  2. // SPDX-License-Identifier: MIT
  3. #pragma once
  4. #include <Jolt/Core/Reference.h>
  5. #include <Jolt/Core/Color.h>
  6. #include <Jolt/Math/Float2.h>
  7. #include <Renderer/RenderPrimitive.h>
  8. #include <Renderer/Texture.h>
  9. #include <Renderer/PipelineState.h>
  10. class Renderer;
  11. class Texture;
  12. /// Font class, used to display text in 3D mode. Does variable width fonts with kerning. Font names are identical to the Windows font names.
  13. class Font : public RefTarget<Font>
  14. {
  15. public:
  16. /// Constants
  17. static const int cBeginChar = ' '; ///< First character that is drawable in the character set
  18. static const int cEndChar = 256; ///< Last character + 1 that is drawable in the character set
  19. static const int cNumChars = cEndChar - cBeginChar; ///< Number of drawable characters in the character set
  20. /// Constructor
  21. Font(Renderer *inRenderer);
  22. /// Create a font
  23. bool Create(const char *inFontName, int inCharHeight);
  24. /// Properties
  25. const String & GetFontName() const { return mFontName; }
  26. int GetCharHeight() const { return mCharHeight; }
  27. /// Get extents of a string, assuming the height of the text is 1 and with the normal aspect ratio of the font
  28. Float2 MeasureText(const string_view &inText) const;
  29. /// Draw a string at a specific location
  30. /// If the string is drawn with the identity matrix, it's top left will start at (0, 0, 0)
  31. /// The text width is in the X direction and the text height is in the Y direction and it will have a height of 1
  32. void DrawText3D(Mat44Arg inTransform, const string_view &inText, ColorArg inColor = Color::sWhite) const;
  33. private:
  34. /// Create a primitive for a string
  35. bool CreateString(Mat44Arg inTransform, const string_view &inText, ColorArg inColor, RenderPrimitive &ioPrimitive) const;
  36. struct FontVertex
  37. {
  38. Float3 mPosition;
  39. Float2 mTexCoord;
  40. Color mColor;
  41. };
  42. /// Properties of the font
  43. String mFontName; ///< Name of the font
  44. int mCharHeight; ///< Height of a character
  45. int mHorizontalTexels; ///< Number of texels horizontally, determines the scale of mStartU, mWidth and mSpacing
  46. int mVerticalTexels; ///< Number of texels vertically, determines the scale of mStartV
  47. uint16 mStartU[cNumChars]; ///< Start U in texels
  48. uint16 mStartV[cNumChars]; ///< Start V in texels
  49. uint8 mWidth[cNumChars]; ///< Width of character in texels
  50. uint8 mSpacing[cNumChars][cNumChars]; ///< Spacing between characters in texels
  51. /// Structures used for drawing
  52. Renderer * mRenderer; ///< Our renderer
  53. Ref<Texture> mTexture; ///< The texture containing all characters
  54. unique_ptr<PipelineState> mPipelineState; ///< The state used to render characters
  55. };