Font.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. // zlib open source license
  2. //
  3. // Copyright (c) 2018 to 2019 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 <stdint.h>
  24. #include "Font.h"
  25. #include "defaultFont.h"
  26. #include "../api/imageAPI.h"
  27. #include "../api/drawAPI.h"
  28. using namespace dsr;
  29. std::shared_ptr<RasterFont> defaultFont = RasterFont::createLatinOne(U"UbuntuMono", image_fromAscii(defaultFontAscii));;
  30. std::shared_ptr<RasterFont> dsr::font_getDefault() {
  31. return defaultFont;
  32. }
  33. RasterCharacter::RasterCharacter(const ImageU8& image, DsrChar unicodeValue, int32_t offsetY)
  34. : image(image), unicodeValue(unicodeValue), width(image_getWidth(image)), offsetY(offsetY) {}
  35. RasterFont::RasterFont(const String& name, int32_t size, int32_t spacing, int32_t spaceWidth)
  36. : name(name), size(size), spacing(spacing), spaceWidth(spaceWidth), tabWidth(spaceWidth * 4) {
  37. for (int i = 0; i < 65536; i++) {
  38. this->indices[i] = -1;
  39. }
  40. }
  41. RasterFont::~RasterFont() {}
  42. std::shared_ptr<RasterFont> RasterFont::createLatinOne(const String& name, const ImageU8& atlas) {
  43. int32_t size = image_getHeight(atlas) / 16;
  44. std::shared_ptr<RasterFont> result = std::make_shared<RasterFont>(name, size, size / 16, size / 2);
  45. result->registerLatinOne16x16(atlas);
  46. return result;
  47. }
  48. void RasterFont::registerCharacter(const ImageU8& image, DsrChar unicodeValue, int32_t offsetY) {
  49. if (this->indices[unicodeValue] == -1) {
  50. // Add the unicode character
  51. this->characters.pushConstruct(image, unicodeValue, offsetY);
  52. // Add to latin-1 table if inside the range
  53. if (unicodeValue < 65536) {
  54. this->indices[unicodeValue] = this->characters.length() - 1;
  55. }
  56. }
  57. }
  58. static IRect getCharacterBound(const ImageU8& image, const IRect& searchRegion) {
  59. // Inclusive intervals for speed
  60. int32_t minX = searchRegion.right();
  61. int32_t maxX = searchRegion.left();
  62. int32_t minY = searchRegion.bottom();
  63. int32_t maxY = searchRegion.top();
  64. for (int y = searchRegion.top(); y < searchRegion.bottom(); y++) {
  65. for (int x = searchRegion.left(); x < searchRegion.right(); x++) {
  66. if (image_readPixel_border(image, x, y)) {
  67. if (x < minX) minX = x;
  68. if (x > maxX) maxX = x;
  69. if (y < minY) minY = y;
  70. if (y > maxY) maxY = y;
  71. }
  72. }
  73. }
  74. // Convert to width and height
  75. return IRect(minX, minY, (maxX + 1) - minX, (maxY + 1) - minY);
  76. }
  77. // Call after construction to register up to 256 characters in a 16x16 grid from the atlas
  78. void RasterFont::registerLatinOne16x16(const ImageU8& atlas) {
  79. int32_t charWidth = image_getWidth(atlas) / 16;
  80. int32_t charHeight = image_getWidth(atlas) / 16;
  81. for (int y = 0; y < 16; y++) {
  82. for (int x = 0; x < 16; x++) {
  83. IRect searchRegion = IRect(x * charWidth, y * charHeight, charWidth, charHeight);
  84. IRect croppedRegion = getCharacterBound(atlas, searchRegion);
  85. if (croppedRegion.hasArea()) {
  86. int32_t offsetY = croppedRegion.top() - searchRegion.top();
  87. ImageU8 fullImage = image_getSubImage(atlas, croppedRegion);
  88. this->registerCharacter(fullImage, y * 16 + x, offsetY);
  89. }
  90. }
  91. }
  92. }
  93. int32_t RasterFont::getCharacterWidth(DsrChar unicodeValue) const {
  94. if (unicodeValue == 0 || unicodeValue == 10 || unicodeValue == 13) {
  95. return 0;
  96. } else {
  97. int32_t index = this->indices[unicodeValue];
  98. if (index > -1) {
  99. return this->characters[index].width + this->spacing;
  100. } else {
  101. return spaceWidth;
  102. }
  103. }
  104. }
  105. // Prints a character and returns the horizontal stride in pixels
  106. int32_t RasterFont::printCharacter(ImageRgbaU8& target, DsrChar unicodeValue, const IVector2D& location, const ColorRgbaI32& color) const {
  107. if (unicodeValue < 65536) {
  108. int32_t index = this->indices[unicodeValue];
  109. if (index > -1) {
  110. const RasterCharacter *source = &(this->characters[index]);
  111. draw_silhouette(target, source->image, color, location.x, location.y + source->offsetY);
  112. }
  113. return this->getCharacterWidth(unicodeValue);
  114. } else {
  115. // TODO: Look up characters outside of the 16-bit range from a sparse data structure
  116. return 0;
  117. }
  118. }
  119. // Lets the print coordinate x jump to the next tab stop starting from the left origin
  120. static void tabJump(int &x, int leftOrigin, int tabWidth) {
  121. // Get the pixel location relative to the origin
  122. int localX = x - leftOrigin;
  123. // Get the remaining pixels until the next tab stop
  124. // If modulo returns zero at a tab stop, it will jump to the next with a full tab width
  125. int remainder = tabWidth - (localX % tabWidth);
  126. x += remainder;
  127. }
  128. void RasterFont::printLine(ImageRgbaU8& target, const ReadableString& content, const IVector2D& location, const ColorRgbaI32& color) const {
  129. IVector2D currentLocation = location;
  130. for (int i = 0; i < content.length(); i++) {
  131. DsrChar code = content[i];
  132. if (code == 9) { // Tab
  133. tabJump(currentLocation.x, location.x, this->tabWidth);
  134. } else {
  135. // TODO: Would right to left printing of Arabic text be too advanced to have in the core framework?
  136. currentLocation.x += this->printCharacter(target, code, currentLocation, color);
  137. }
  138. }
  139. }
  140. void RasterFont::printMultiLine(ImageRgbaU8& target, const ReadableString& content, const IRect& bound, const ColorRgbaI32& color) const {
  141. int y = bound.top(); // The upper vertical location of the currently printed row in pixels.
  142. int lineWidth = 0; // The size of the currently scanned row, to make sure that it can be printed.
  143. int rowStartIndex = 0; // The start of the current row or the unprinted remainder that didn't fit inside the bound.
  144. int lastWordBreak = 0; // The last scanned location where the current row could've been broken off.
  145. bool wordStarted = false; // True iff the physical line after word wrapping has scanned the beginning of a word.
  146. for (int i = 0; i <= content.length(); i++) {
  147. // Fake an additional line-break at the end
  148. DsrChar code = (i >= content.length()) ? 10 : content[i];
  149. if (code == 10) {
  150. // Print the completed line
  151. this->printLine(target, content.exclusiveRange(rowStartIndex, i), IVector2D(bound.left(), y), color);
  152. y += this->size; if (y >= bound.bottom()) { return; }
  153. lineWidth = 0;
  154. rowStartIndex = i + 1;
  155. lastWordBreak = rowStartIndex;
  156. wordStarted = false;
  157. } else {
  158. int newCharWidth = this->getCharacterWidth(code);
  159. if (code == ' ' || code == 9) {
  160. if (wordStarted) {
  161. lastWordBreak = i;
  162. wordStarted = false;
  163. }
  164. } else {
  165. wordStarted = true;
  166. if (lineWidth + newCharWidth > bound.width()) {
  167. int splitIndex = lastWordBreak;
  168. if (lastWordBreak == rowStartIndex) {
  169. // The word is too big to be printed as a whole
  170. if (i > rowStartIndex) {
  171. splitIndex = i - 1;
  172. } else {
  173. // Not enough space to print a single character, skipping content to avoid printing outside.
  174. splitIndex = i;
  175. }
  176. }
  177. this->printLine(target, content.exclusiveRange(rowStartIndex, splitIndex), IVector2D(bound.left(), y), color);
  178. y += this->size; if (y >= bound.bottom()) { return; }
  179. lineWidth = 0;
  180. // Continue after splitIndex
  181. i = splitIndex + 1;
  182. rowStartIndex = i;
  183. lastWordBreak = i;
  184. wordStarted = false;
  185. }
  186. }
  187. if (code == 9) { // Tab
  188. tabJump(lineWidth, bound.left(), this->tabWidth);
  189. } else {
  190. lineWidth += newCharWidth;
  191. }
  192. }
  193. }
  194. }
  195. int32_t RasterFont::getLineWidth(const ReadableString& content) const {
  196. int32_t result = 0;
  197. for (int i = 0; i < content.length(); i++) {
  198. DsrChar code = content[i];
  199. if (code == 9) { // Tab
  200. tabJump(result, 0, this->tabWidth);
  201. } else {
  202. result += this->getCharacterWidth(code);
  203. }
  204. }
  205. return result;
  206. }