fontAPI.cpp 4.3 KB

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