2
0

Font.h 2.9 KB

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