fontAPI.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. #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_getCharacterWidth(const RasterFont font, DsrChar unicodeValue) {
  57. if (!font_exists(font)) {
  58. throwError("font_getCharacterWidth: font must exist!");
  59. }
  60. return font->getCharacterWidth(unicodeValue);
  61. }
  62. int32_t font_getLineWidth(const RasterFont font, const ReadableString& content) {
  63. if (!font_exists(font)) {
  64. throwError("font_getLineWidth: font must exist!");
  65. }
  66. return font->getLineWidth(content);
  67. }
  68. int32_t font_printCharacter(ImageRgbaU8& target, const RasterFont font, DsrChar unicodeValue, const IVector2D& location, const ColorRgbaI32& color) {
  69. if (!image_exists(target)) {
  70. throwError("font_printCharacter: target must exist!");
  71. } else if (!font_exists(font)) {
  72. throwError("font_printCharacter: font must exist!");
  73. }
  74. return font->printCharacter(target, unicodeValue, location, color);
  75. }
  76. void font_printLine(ImageRgbaU8& target, const RasterFont font, const ReadableString& content, const IVector2D& location, const ColorRgbaI32& color) {
  77. if (!image_exists(target)) {
  78. throwError("font_printLine: target must exist!");
  79. } else if (!font_exists(font)) {
  80. throwError("font_printLine: font must exist!");
  81. } else {
  82. font->printLine(target, content, location, color);
  83. }
  84. }
  85. void font_printMultiLine(ImageRgbaU8& target, const RasterFont font, const ReadableString& content, const IRect& bound, const ColorRgbaI32& color) {
  86. if (!image_exists(target)) {
  87. throwError("font_printMultiLine: target must exist!");
  88. } else if (!font_exists(font)) {
  89. throwError("font_printMultiLine: font must exist!");
  90. } else {
  91. font->printMultiLine(target, content, bound, color);
  92. }
  93. }
  94. }