Font.cpp 8.6 KB

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