Font.cpp 8.4 KB

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