GenericShaper.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /**
  2. * Copyright (c) 2006-2023 LOVE Development Team
  3. *
  4. * This software is provided 'as-is', without any express or implied
  5. * warranty. In no event will the authors be held liable for any damages
  6. * arising from the use of this software.
  7. *
  8. * Permission is granted to anyone to use this software for any purpose,
  9. * including commercial applications, and to alter it and redistribute it
  10. * freely, subject to the following restrictions:
  11. *
  12. * 1. The origin of this software must not be misrepresented; you must not
  13. * claim that you wrote the original software. If you use this software
  14. * in a product, an acknowledgment in the product documentation would be
  15. * appreciated but is not required.
  16. * 2. Altered source versions must be plainly marked as such, and must not be
  17. * misrepresented as being the original software.
  18. * 3. This notice may not be removed or altered from any source distribution.
  19. **/
  20. // LOVE
  21. #include "GenericShaper.h"
  22. #include "Rasterizer.h"
  23. #include "common/Optional.h"
  24. namespace love
  25. {
  26. namespace font
  27. {
  28. GenericShaper::GenericShaper(Rasterizer *rasterizer)
  29. : TextShaper(rasterizer)
  30. {
  31. }
  32. GenericShaper::~GenericShaper()
  33. {
  34. }
  35. void GenericShaper::computeGlyphPositions(const ColoredCodepoints &codepoints, Range range, Vector2 offset, float extraspacing, std::vector<GlyphPosition> *positions, std::vector<IndexedColor> *colors, TextInfo *info)
  36. {
  37. if (!range.isValid())
  38. range = Range(0, codepoints.cps.size());
  39. if (rasterizers[0]->getDataType() == Rasterizer::DATA_TRUETYPE)
  40. offset.y += getBaseline();
  41. // Spacing counter and newline handling.
  42. Vector2 curpos = offset;
  43. int maxwidth = 0;
  44. uint32 prevglyph = 0;
  45. if (positions)
  46. positions->reserve(range.getSize());
  47. int colorindex = 0;
  48. int ncolors = (int) codepoints.colors.size();
  49. Optional<Colorf> colorToAdd;
  50. // Make sure the right color is applied to the start of the glyph list,
  51. // when the start isn't 0.
  52. if (colors && range.getOffset() > 0 && !codepoints.colors.empty())
  53. {
  54. for (; colorindex < ncolors; colorindex++)
  55. {
  56. if (codepoints.colors[colorindex].index >= (int) range.getOffset())
  57. break;
  58. colorToAdd.set(codepoints.colors[colorindex].color);
  59. }
  60. }
  61. for (int i = (int) range.getMin(); i <= (int) range.getMax(); i++)
  62. {
  63. uint32 g = codepoints.cps[i];
  64. // Do this before anything else so we don't miss colors corresponding
  65. // to newlines. The actual add to the list happens after newline
  66. // handling, to make sure the resulting index is valid in the positions
  67. // array.
  68. if (colors && colorindex < ncolors && codepoints.colors[colorindex].index == i)
  69. {
  70. colorToAdd.set(codepoints.colors[colorindex].color);
  71. colorindex++;
  72. }
  73. if (g == '\n')
  74. {
  75. if (curpos.x > maxwidth)
  76. maxwidth = (int)curpos.x;
  77. // Wrap newline, but do not output a position for it.
  78. curpos.y += floorf(getHeight() * getLineHeight() + 0.5f);
  79. curpos.x = offset.x;
  80. prevglyph = 0;
  81. continue;
  82. }
  83. // Ignore carriage returns
  84. if (g == '\r')
  85. {
  86. prevglyph = g;
  87. continue;
  88. }
  89. if (colorToAdd.hasValue && colors && positions)
  90. {
  91. IndexedColor c = {colorToAdd.value, (int) positions->size()};
  92. colors->push_back(c);
  93. colorToAdd.clear();
  94. }
  95. // Add kerning to the current horizontal offset.
  96. curpos.x += getKerning(prevglyph, g);
  97. GlyphIndex glyphindex;
  98. int advance = getGlyphAdvance(g, &glyphindex);
  99. if (positions)
  100. positions->push_back({ Vector2(curpos.x, curpos.y), glyphindex });
  101. // Advance the x position for the next glyph.
  102. curpos.x += advance;
  103. // Account for extra spacing given to space characters.
  104. if (g == ' ' && extraspacing != 0.0f)
  105. curpos.x = floorf(curpos.x + extraspacing);
  106. prevglyph = g;
  107. }
  108. if (curpos.x > maxwidth)
  109. maxwidth = (int)curpos.x;
  110. if (info != nullptr)
  111. {
  112. info->width = maxwidth - offset.x;
  113. info->height = curpos.y - offset.y;
  114. if (curpos.x > offset.x)
  115. info->height += floorf(getHeight() * getLineHeight() + 0.5f);
  116. }
  117. }
  118. int GenericShaper::computeWordWrapIndex(const ColoredCodepoints &codepoints, Range range, float wraplimit, float *width)
  119. {
  120. if (!range.isValid())
  121. range = Range(0, codepoints.cps.size());
  122. uint32 prevglyph = 0;
  123. float w = 0.0f;
  124. float outwidth = 0.0f;
  125. float widthbeforelastspace = 0.0f;
  126. int firstindexafterspace = -1;
  127. for (int i = (int)range.getMin(); i <= (int)range.getMax(); i++)
  128. {
  129. uint32 g = codepoints.cps[i];
  130. if (g == '\r')
  131. {
  132. prevglyph = g;
  133. continue;
  134. }
  135. float newwidth = w + getKerning(prevglyph, g) + getGlyphAdvance(g);
  136. // Don't count trailing spaces in the output width.
  137. if (isWhitespace(g))
  138. {
  139. if (!isWhitespace(prevglyph))
  140. widthbeforelastspace = w;
  141. }
  142. else
  143. {
  144. if (isWhitespace(prevglyph))
  145. firstindexafterspace = i;
  146. // Only wrap when there's a non-space character.
  147. if (newwidth > wraplimit)
  148. {
  149. // If this is the first character, wrap from the next one instead of this one.
  150. int wrapindex = i > (int)range.first ? i : (int)range.first + 1;
  151. // Rewind to after the last seen space when wrapping.
  152. if (firstindexafterspace != -1)
  153. {
  154. wrapindex = firstindexafterspace;
  155. outwidth = widthbeforelastspace;
  156. }
  157. if (width)
  158. *width = outwidth;
  159. return wrapindex;
  160. }
  161. outwidth = newwidth;
  162. }
  163. w = newwidth;
  164. prevglyph = g;
  165. }
  166. if (width)
  167. *width = outwidth;
  168. // There wasn't any wrap in the middle of the range.
  169. return range.last + 1;
  170. }
  171. } // font
  172. } // love