Font.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // zlib open source license
  2. //
  3. // Copyright (c) 2018 to 2022 David Forsgren Piuva
  4. //
  5. // This software is provided 'as-is', without any express or implied
  6. // warranty. In no event will the authors be held liable for any damages
  7. // arising from the use of this software.
  8. //
  9. // Permission is granted to anyone to use this software for any purpose,
  10. // including commercial applications, and to alter it and redistribute it
  11. // freely, subject to the following restrictions:
  12. //
  13. // 1. The origin of this software must not be misrepresented; you must not
  14. // claim that you wrote the original software. If you use this software
  15. // in a product, an acknowledgment in the product documentation would be
  16. // appreciated but is not required.
  17. //
  18. // 2. Altered source versions must be plainly marked as such, and must not be
  19. // misrepresented as being the original software.
  20. //
  21. // 3. This notice may not be removed or altered from any source
  22. // distribution.
  23. #ifndef DFPSR_GUI_FONT
  24. #define DFPSR_GUI_FONT
  25. #include "../base/Handle.h"
  26. #include "../api/stringAPI.h"
  27. #include "../api/imageAPI.h"
  28. #include "../math/IRect.h"
  29. #include "../math/IVector.h"
  30. namespace dsr {
  31. struct RasterCharacter {
  32. public:
  33. // Image to draw
  34. ImageU8 image;
  35. // Look-up value
  36. DsrChar unicodeValue = 0;
  37. // The width of the character
  38. int32_t width = 0;
  39. // Y offset
  40. int32_t offsetY = 0;
  41. public:
  42. // Constructor
  43. RasterCharacter() {}
  44. RasterCharacter(const ImageU8& image, DsrChar unicodeValue, int32_t offsetY);
  45. // Destructor
  46. ~RasterCharacter() {}
  47. };
  48. class RasterFontImpl {
  49. public:
  50. // Font identity
  51. const String name;
  52. const int32_t size = 0; // From the top of one row to another
  53. // Settings
  54. int32_t spacing = 0; // The extra pixels between each character
  55. int32_t spaceWidth = 0; // The size of a whole space character including spacing
  56. int32_t tabWidth = 0; // The size of a whole tab including spacing
  57. int32_t widest = 0; // The maximum character width excluding spacing
  58. // A list of character images with their unicode keys
  59. List<RasterCharacter> characters;
  60. // TODO: A way to map all UTF-32 characters
  61. // Indices to characters within the 16-bit range
  62. // indices[x] = -1 for non-existing character codes
  63. // The indices[0..255] contains the Latin-1 subset
  64. int32_t indices[65536];
  65. public:
  66. // Constructor
  67. RasterFontImpl(const String& name, int32_t size, int32_t spacing, int32_t spaceWidth);
  68. static Handle<RasterFontImpl> createLatinOne(const String& name, const ImageU8& atlas);
  69. // Destructor
  70. ~RasterFontImpl();
  71. public:
  72. // Allready registered unicode characters will be ignored if reused, so load overlapping sets in order of priority
  73. void registerCharacter(const ImageU8& image, DsrChar unicodeValue, int32_t offsetY);
  74. // Call after construction to register up to 256 characters in a 16x16 grid from the atlas
  75. void registerLatinOne16x16(const ImageU8& atlas);
  76. // Returns the width of a character including spacing in pixels
  77. int32_t getCharacterWidth(DsrChar unicodeValue) const;
  78. // Returns the total length of characters in pixels as if printing content
  79. // If multiple lines exists it will simply keep adding to the total by ignoring line-breaks
  80. int64_t getLineWidth(const ReadableString& content) const;
  81. // Prints a character and returns the horizontal stride in pixels
  82. int32_t printCharacter(ImageRgbaU8& target, DsrChar unicodeValue, const IVector2D& location, const ColorRgbaI32& color) const;
  83. // Prints a whole line of text from location
  84. void printLine(ImageRgbaU8& target, const ReadableString& content, const IVector2D& location, const ColorRgbaI32& color) const;
  85. // Prints multiple lines of text within a bound
  86. void printMultiLine(ImageRgbaU8& target, const ReadableString& content, const IRect& bound, const ColorRgbaI32& color) const;
  87. };
  88. // See DFPSR/api/fontAPI.h for the procedural interface
  89. }
  90. #endif