2
0

fontAPI.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. #include "fontAPI.h"
  24. #include "imageAPI.h"
  25. #include "../font/Font.h"
  26. #include "../font/defaultFont.h"
  27. namespace dsr {
  28. static const RasterFont defaultFont = RasterFontImpl::createLatinOne(U"UbuntuMono", image_fromAscii(defaultFontAscii));
  29. RasterFont font_getDefault() {
  30. return defaultFont;
  31. }
  32. RasterFont font_createLatinOne(const String& name, const ImageU8& atlas) {
  33. if (!image_exists(atlas)) {
  34. throwError("Cannot create the Latin-1 font called ", name, " from an empty image handle.\n");
  35. } else if (image_getWidth(atlas) % 16 == 0 && image_getHeight(atlas) % 16 == 0
  36. && image_getWidth(atlas) >= 16 && image_getHeight(atlas) >= 16) {
  37. throwError("Cannot create the Latin-1 font called ", name, " from an image of ", image_getWidth(atlas), "x", image_getHeight(atlas), " pixels.\n");
  38. }
  39. return RasterFontImpl::createLatinOne(name, atlas);
  40. }
  41. String font_getName(const RasterFont font) {
  42. if (!font_exists(font)) {
  43. throwError("font_getName: font must exist!");
  44. }
  45. return font->name;
  46. }
  47. int32_t font_getSize(const RasterFont font) {
  48. if (!font_exists(font)) {
  49. throwError("font_getSize: font must exist!");
  50. }
  51. return font->size;
  52. }
  53. int32_t font_getSpacing(const RasterFont font) {
  54. if (!font_exists(font)) {
  55. throwError("font_getSpacing: font must exist!");
  56. }
  57. return font->spacing;
  58. }
  59. int32_t font_getTabWidth(const RasterFont font) {
  60. if (!font_exists(font)) {
  61. throwError("font_getTabWidth: font must exist!");
  62. }
  63. return font->tabWidth;
  64. }
  65. int32_t font_getCharacterWidth(const RasterFont font, DsrChar unicodeValue) {
  66. if (!font_exists(font)) {
  67. throwError("font_getCharacterWidth: font must exist!");
  68. }
  69. return font->getCharacterWidth(unicodeValue);
  70. }
  71. int32_t font_getMonospaceWidth(const RasterFont font) {
  72. if (!font_exists(font)) {
  73. throwError("font_getMonospaceWidth: font must exist!");
  74. }
  75. return font->widest + font->spacing;
  76. }
  77. int32_t font_getLineWidth(const RasterFont font, const ReadableString& content) {
  78. if (!font_exists(font)) {
  79. throwError("font_getLineWidth: font must exist!");
  80. }
  81. return font->getLineWidth(content);
  82. }
  83. int32_t font_printCharacter(ImageRgbaU8& target, const RasterFont font, DsrChar unicodeValue, const IVector2D& location, const ColorRgbaI32& color) {
  84. if (!image_exists(target)) {
  85. throwError("font_printCharacter: target must exist!");
  86. } else if (!font_exists(font)) {
  87. throwError("font_printCharacter: font must exist!");
  88. }
  89. return font->printCharacter(target, unicodeValue, location, color);
  90. }
  91. void font_printLine(ImageRgbaU8& target, const RasterFont font, const ReadableString& content, const IVector2D& location, const ColorRgbaI32& color) {
  92. if (!image_exists(target)) {
  93. throwError("font_printLine: target must exist!");
  94. } else if (!font_exists(font)) {
  95. throwError("font_printLine: font must exist!");
  96. } else {
  97. font->printLine(target, content, location, color);
  98. }
  99. }
  100. void font_printMultiLine(ImageRgbaU8& target, const RasterFont font, const ReadableString& content, const IRect& bound, const ColorRgbaI32& color) {
  101. if (!image_exists(target)) {
  102. throwError("font_printMultiLine: target must exist!");
  103. } else if (!font_exists(font)) {
  104. throwError("font_printMultiLine: font must exist!");
  105. } else {
  106. font->printMultiLine(target, content, bound, color);
  107. }
  108. }
  109. }