Font.h 2.9 KB

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