fontAPI.h 4.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. // zlib open source license
  2. //
  3. // Copyright (c) 2020 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_API_FONT
  24. #define DFPSR_API_FONT
  25. #include "../image/Image.h"
  26. #include "stringAPI.h"
  27. namespace dsr {
  28. // A handle to a raster font
  29. class RasterFontImpl;
  30. using RasterFont = Handle<RasterFontImpl>;
  31. // Get a handle to the default font
  32. RasterFont font_getDefault();
  33. // Create a new font mapped to the Latin-1 character sub-set using a fixed size grid of 16 x 16 sub-images
  34. // atlas contains 16 x 16 character images starting with character codes 0 to 15 and continuing left to right on the next cell rows in the atlas
  35. // Pre-conditions:
  36. // * atlas must exist
  37. // * atlas must have dimensions evenly divisible by 16
  38. // image_getWidth(atlas) % 16 == 0
  39. // image_getHeight(atlas) % 16 == 0
  40. // * Each cell must include at least one pixel
  41. // image_getWidth(atlas) >= 16
  42. // image_getHeight(atlas) >= 16
  43. RasterFont font_createLatinOne(const String& name, const ImageU8& atlas);
  44. // Post-condition: Returns true iff font exists
  45. inline bool font_exists(const RasterFont font) { return font.isNotNull(); }
  46. // Pre-condition: font must exist
  47. // Post-condition: Returns font's name, as given on construction
  48. String font_getName(const RasterFont font);
  49. // Pre-condition: font must exist
  50. // Post-condition:
  51. // Returns the font's size in pixels, which is the height of an individual character image before cropping
  52. // If ceated using font_createLatinOne then it's image_getHeight(atlas) / 16
  53. int32_t font_getSize(const RasterFont font);
  54. // Pre-condition: font must exist
  55. // Post-condition: Returns the font's empty space between characters in pixels.
  56. int32_t font_getSpacing(const RasterFont font);
  57. // Pre-condition: font must exist
  58. // Post-condition: Returns the font's maximum tab width in pixels, which is the alignment the write location will jump to when reading a tab.
  59. int32_t font_getTabWidth(const RasterFont font);
  60. // Pre-condition: font must exist
  61. // Post-condition: Returns the width of a character including spacing in pixels
  62. int32_t font_getCharacterWidth(const RasterFont font, DsrChar unicodeValue);
  63. // Pre-condition: font must exist
  64. // Post-condition: Returns the width of the widest character including spacing in pixels
  65. int32_t font_getMonospaceWidth(const RasterFont font);
  66. // Pre-condition: font must exist
  67. // Post-condition: Returns the total length of content in pixels while ignoring line-breaks
  68. int32_t font_getLineWidth(const RasterFont font, const ReadableString& content);
  69. // Pre-condition: font and target must exist
  70. // Side-effects: Prints a character and returns the horizontal stride in pixels
  71. int32_t font_printCharacter(ImageRgbaU8& target, const RasterFont font, DsrChar unicodeValue, const IVector2D& location, const ColorRgbaI32& color);
  72. // Pre-condition: font and target must exist
  73. // Side-effects: Prints content from location while ignoring line-breaks
  74. void font_printLine(ImageRgbaU8& target, const RasterFont font, const ReadableString& content, const IVector2D& location, const ColorRgbaI32& color);
  75. // Pre-condition: font and target must exist
  76. // Side-effects: Prints multiple lines of text within bound
  77. // Guarantees that:
  78. // * No characters are clipped against bound
  79. // It may however clip against the target image's bound if you make the bound larger than the image for partial updates or scrolling effects
  80. // * No pixels are drawn outside of bound
  81. void font_printMultiLine(ImageRgbaU8& target, const RasterFont font, const ReadableString& content, const IRect& bound, const ColorRgbaI32& color);
  82. }
  83. #endif