fontAPI.h 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. // zlib open source license
  2. //
  3. // Copyright (c) 2020 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 "types.h"
  26. #include "../api/stringAPI.h"
  27. namespace dsr {
  28. // Get a handle to the default font
  29. RasterFont font_getDefault();
  30. // Create a new font mapped to the Latin-1 character sub-set using a fixed size grid of 16 x 16 sub-images
  31. // 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
  32. // Pre-conditions:
  33. // * atlas must exist
  34. // * atlas must have dimensions evenly divisible by 16
  35. // image_getWidth(atlas) % 16 == 0
  36. // image_getHeight(atlas) % 16 == 0
  37. // * Each cell must include at least one pixel
  38. // image_getWidth(atlas) >= 16
  39. // image_getHeight(atlas) >= 16
  40. RasterFont font_createLatinOne(const String& name, const ImageU8& atlas);
  41. // Post-condition: Returns true iff font exists
  42. bool font_exists(const RasterFont font);
  43. // Pre-condition: font must exist
  44. // Post-condition: Returns font's name, as given on construction
  45. String font_getName(const RasterFont font);
  46. // Pre-condition: font must exist
  47. // Post-condition:
  48. // Returns the font's size, which is the height of an individual character image before cropping
  49. // If ceated using font_createLatinOne then it's image_getHeight(atlas) / 16
  50. int32_t font_getSize(const RasterFont font);
  51. // Pre-condition: font must exist
  52. // Post-condition: Returns the width of a character including spacing in pixels
  53. int32_t font_getCharacterWidth(const RasterFont font, DsrChar unicodeValue);
  54. // Pre-condition: font must exist
  55. // Post-condition: Returns the total length of content in pixels while ignoring line-breaks
  56. int32_t font_getLineWidth(const RasterFont font, const ReadableString& content);
  57. // Pre-condition: font and target must exist
  58. // Side-effects: Prints a character and returns the horizontal stride in pixels
  59. int32_t font_printCharacter(ImageRgbaU8& target, const RasterFont font, DsrChar unicodeValue, const IVector2D& location, const ColorRgbaI32& color);
  60. // Pre-condition: font and target must exist
  61. // Side-effects: Prints content from location while ignoring line-breaks
  62. void font_printLine(ImageRgbaU8& target, const RasterFont font, const ReadableString& content, const IVector2D& location, const ColorRgbaI32& color);
  63. // Pre-condition: font and target must exist
  64. // Side-effects: Prints multiple lines of text within bound
  65. // Guarantees that:
  66. // * No characters are clipped against bound
  67. // 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
  68. // * No pixels are drawn outside of bound
  69. void font_printMultiLine(ImageRgbaU8& target, const RasterFont font, const ReadableString& content, const IRect& bound, const ColorRgbaI32& color);
  70. }
  71. #endif