Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

juce_GlyphArrangement.cpp 25KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2022 - Raw Material Software Limited
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. By using JUCE, you agree to the terms of both the JUCE 7 End-User License
  8. Agreement and JUCE Privacy Policy.
  9. End User License Agreement: www.juce.com/juce-7-licence
  10. Privacy Policy: www.juce.com/juce-privacy-policy
  11. Or: You may also use this code under the terms of the GPL v3 (see
  12. www.gnu.org/licenses).
  13. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  14. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  15. DISCLAIMED.
  16. ==============================================================================
  17. */
  18. namespace juce
  19. {
  20. static constexpr bool isNonBreakingSpace (const juce_wchar c)
  21. {
  22. return c == 0x00a0
  23. || c == 0x2007
  24. || c == 0x202f
  25. || c == 0x2060;
  26. }
  27. PositionedGlyph::PositionedGlyph() noexcept
  28. : character (0), glyph (0), x (0), y (0), w (0), whitespace (false)
  29. {
  30. }
  31. PositionedGlyph::PositionedGlyph (const Font& font_, juce_wchar character_, int glyphNumber,
  32. float anchorX, float baselineY, float width, bool whitespace_)
  33. : font (font_), character (character_), glyph (glyphNumber),
  34. x (anchorX), y (baselineY), w (width), whitespace (whitespace_)
  35. {
  36. }
  37. static void drawGlyphWithFont (Graphics& g, int glyph, const Font& font, AffineTransform t)
  38. {
  39. auto& context = g.getInternalContext();
  40. context.setFont (font);
  41. context.drawGlyph (glyph, t);
  42. }
  43. void PositionedGlyph::draw (Graphics& g) const
  44. {
  45. if (! isWhitespace())
  46. drawGlyphWithFont (g, glyph, font, AffineTransform::translation (x, y));
  47. }
  48. void PositionedGlyph::draw (Graphics& g, AffineTransform transform) const
  49. {
  50. if (! isWhitespace())
  51. drawGlyphWithFont (g, glyph, font, AffineTransform::translation (x, y).followedBy (transform));
  52. }
  53. void PositionedGlyph::createPath (Path& path) const
  54. {
  55. if (! isWhitespace())
  56. {
  57. if (auto t = font.getTypefacePtr())
  58. {
  59. Path p;
  60. t->getOutlineForGlyph (glyph, p);
  61. path.addPath (p, AffineTransform::scale (font.getHeight() * font.getHorizontalScale(), font.getHeight())
  62. .translated (x, y));
  63. }
  64. }
  65. }
  66. bool PositionedGlyph::hitTest (float px, float py) const
  67. {
  68. if (getBounds().contains (px, py) && ! isWhitespace())
  69. {
  70. if (auto t = font.getTypefacePtr())
  71. {
  72. Path p;
  73. t->getOutlineForGlyph (glyph, p);
  74. AffineTransform::translation (-x, -y)
  75. .scaled (1.0f / (font.getHeight() * font.getHorizontalScale()), 1.0f / font.getHeight())
  76. .transformPoint (px, py);
  77. return p.contains (px, py);
  78. }
  79. }
  80. return false;
  81. }
  82. void PositionedGlyph::moveBy (float deltaX, float deltaY)
  83. {
  84. x += deltaX;
  85. y += deltaY;
  86. }
  87. //==============================================================================
  88. GlyphArrangement::GlyphArrangement()
  89. {
  90. glyphs.ensureStorageAllocated (128);
  91. }
  92. //==============================================================================
  93. void GlyphArrangement::clear()
  94. {
  95. glyphs.clear();
  96. }
  97. PositionedGlyph& GlyphArrangement::getGlyph (int index) noexcept
  98. {
  99. return glyphs.getReference (index);
  100. }
  101. //==============================================================================
  102. void GlyphArrangement::addGlyphArrangement (const GlyphArrangement& other)
  103. {
  104. glyphs.addArray (other.glyphs);
  105. }
  106. void GlyphArrangement::addGlyph (const PositionedGlyph& glyph)
  107. {
  108. glyphs.add (glyph);
  109. }
  110. void GlyphArrangement::removeRangeOfGlyphs (int startIndex, int num)
  111. {
  112. glyphs.removeRange (startIndex, num < 0 ? glyphs.size() : num);
  113. }
  114. //==============================================================================
  115. void GlyphArrangement::addLineOfText (const Font& font, const String& text, float xOffset, float yOffset)
  116. {
  117. addCurtailedLineOfText (font, text, xOffset, yOffset, 1.0e10f, false);
  118. }
  119. void GlyphArrangement::addCurtailedLineOfText (const Font& font, const String& text,
  120. float xOffset, float yOffset,
  121. float maxWidthPixels, bool useEllipsis)
  122. {
  123. if (text.isNotEmpty())
  124. {
  125. Array<int> newGlyphs;
  126. Array<float> xOffsets;
  127. font.getGlyphPositions (text, newGlyphs, xOffsets);
  128. auto textLen = newGlyphs.size();
  129. glyphs.ensureStorageAllocated (glyphs.size() + textLen);
  130. auto t = text.getCharPointer();
  131. for (int i = 0; i < textLen; ++i)
  132. {
  133. auto nextX = xOffsets.getUnchecked (i + 1);
  134. if (nextX > maxWidthPixels + 1.0f)
  135. {
  136. // curtail the string if it's too wide..
  137. if (useEllipsis && textLen > 3 && glyphs.size() >= 3)
  138. insertEllipsis (font, xOffset + maxWidthPixels, 0, glyphs.size());
  139. break;
  140. }
  141. auto thisX = xOffsets.getUnchecked (i);
  142. auto isWhitespace = isNonBreakingSpace (*t) || t.isWhitespace();
  143. glyphs.add (PositionedGlyph (font, t.getAndAdvance(),
  144. newGlyphs.getUnchecked(i),
  145. xOffset + thisX, yOffset,
  146. nextX - thisX, isWhitespace));
  147. }
  148. }
  149. }
  150. int GlyphArrangement::insertEllipsis (const Font& font, float maxXPos, int startIndex, int endIndex)
  151. {
  152. int numDeleted = 0;
  153. if (! glyphs.isEmpty())
  154. {
  155. Array<int> dotGlyphs;
  156. Array<float> dotXs;
  157. font.getGlyphPositions ("..", dotGlyphs, dotXs);
  158. auto dx = dotXs[1];
  159. float xOffset = 0.0f, yOffset = 0.0f;
  160. while (endIndex > startIndex)
  161. {
  162. auto& pg = glyphs.getReference (--endIndex);
  163. xOffset = pg.x;
  164. yOffset = pg.y;
  165. glyphs.remove (endIndex);
  166. ++numDeleted;
  167. if (xOffset + dx * 3 <= maxXPos)
  168. break;
  169. }
  170. for (int i = 3; --i >= 0;)
  171. {
  172. glyphs.insert (endIndex++, PositionedGlyph (font, '.', dotGlyphs.getFirst(),
  173. xOffset, yOffset, dx, false));
  174. --numDeleted;
  175. xOffset += dx;
  176. if (xOffset > maxXPos)
  177. break;
  178. }
  179. }
  180. return numDeleted;
  181. }
  182. void GlyphArrangement::addJustifiedText (const Font& font, const String& text,
  183. float x, float y, float maxLineWidth,
  184. Justification horizontalLayout,
  185. float leading)
  186. {
  187. auto lineStartIndex = glyphs.size();
  188. addLineOfText (font, text, x, y);
  189. auto originalY = y;
  190. while (lineStartIndex < glyphs.size())
  191. {
  192. int i = lineStartIndex;
  193. if (glyphs.getReference(i).getCharacter() != '\n'
  194. && glyphs.getReference(i).getCharacter() != '\r')
  195. ++i;
  196. auto lineMaxX = glyphs.getReference (lineStartIndex).getLeft() + maxLineWidth;
  197. int lastWordBreakIndex = -1;
  198. while (i < glyphs.size())
  199. {
  200. auto& pg = glyphs.getReference (i);
  201. auto c = pg.getCharacter();
  202. if (c == '\r' || c == '\n')
  203. {
  204. ++i;
  205. if (c == '\r' && i < glyphs.size()
  206. && glyphs.getReference(i).getCharacter() == '\n')
  207. ++i;
  208. break;
  209. }
  210. if (pg.isWhitespace())
  211. {
  212. lastWordBreakIndex = i + 1;
  213. }
  214. else if (pg.getRight() - 0.0001f >= lineMaxX)
  215. {
  216. if (lastWordBreakIndex >= 0)
  217. i = lastWordBreakIndex;
  218. break;
  219. }
  220. ++i;
  221. }
  222. auto currentLineStartX = glyphs.getReference (lineStartIndex).getLeft();
  223. auto currentLineEndX = currentLineStartX;
  224. for (int j = i; --j >= lineStartIndex;)
  225. {
  226. if (! glyphs.getReference (j).isWhitespace())
  227. {
  228. currentLineEndX = glyphs.getReference (j).getRight();
  229. break;
  230. }
  231. }
  232. float deltaX = 0.0f;
  233. if (horizontalLayout.testFlags (Justification::horizontallyJustified))
  234. spreadOutLine (lineStartIndex, i - lineStartIndex, maxLineWidth);
  235. else if (horizontalLayout.testFlags (Justification::horizontallyCentred))
  236. deltaX = (maxLineWidth - (currentLineEndX - currentLineStartX)) * 0.5f;
  237. else if (horizontalLayout.testFlags (Justification::right))
  238. deltaX = maxLineWidth - (currentLineEndX - currentLineStartX);
  239. moveRangeOfGlyphs (lineStartIndex, i - lineStartIndex,
  240. x + deltaX - currentLineStartX, y - originalY);
  241. lineStartIndex = i;
  242. y += font.getHeight() + leading;
  243. }
  244. }
  245. void GlyphArrangement::addFittedText (const Font& f, const String& text,
  246. float x, float y, float width, float height,
  247. Justification layout, int maximumLines,
  248. float minimumHorizontalScale)
  249. {
  250. if (minimumHorizontalScale == 0.0f)
  251. minimumHorizontalScale = Font::getDefaultMinimumHorizontalScaleFactor();
  252. // doesn't make much sense if this is outside a sensible range of 0.5 to 1.0
  253. jassert (minimumHorizontalScale > 0 && minimumHorizontalScale <= 1.0f);
  254. if (text.containsAnyOf ("\r\n"))
  255. {
  256. addLinesWithLineBreaks (text, f, x, y, width, height, layout);
  257. }
  258. else
  259. {
  260. auto startIndex = glyphs.size();
  261. auto trimmed = text.trim();
  262. addLineOfText (f, trimmed, x, y);
  263. auto numGlyphs = glyphs.size() - startIndex;
  264. if (numGlyphs > 0)
  265. {
  266. auto lineWidth = glyphs.getReference (glyphs.size() - 1).getRight()
  267. - glyphs.getReference (startIndex).getLeft();
  268. if (lineWidth > 0)
  269. {
  270. if (lineWidth * minimumHorizontalScale < width)
  271. {
  272. if (lineWidth > width)
  273. stretchRangeOfGlyphs (startIndex, numGlyphs, width / lineWidth);
  274. justifyGlyphs (startIndex, numGlyphs, x, y, width, height, layout);
  275. }
  276. else if (maximumLines <= 1)
  277. {
  278. fitLineIntoSpace (startIndex, numGlyphs, x, y, width, height,
  279. f, layout, minimumHorizontalScale);
  280. }
  281. else
  282. {
  283. splitLines (trimmed, f, startIndex, x, y, width, height,
  284. maximumLines, lineWidth, layout, minimumHorizontalScale);
  285. }
  286. }
  287. }
  288. }
  289. }
  290. //==============================================================================
  291. void GlyphArrangement::moveRangeOfGlyphs (int startIndex, int num, const float dx, const float dy)
  292. {
  293. jassert (startIndex >= 0);
  294. if (dx != 0.0f || dy != 0.0f)
  295. {
  296. if (num < 0 || startIndex + num > glyphs.size())
  297. num = glyphs.size() - startIndex;
  298. while (--num >= 0)
  299. glyphs.getReference (startIndex++).moveBy (dx, dy);
  300. }
  301. }
  302. void GlyphArrangement::addLinesWithLineBreaks (const String& text, const Font& f,
  303. float x, float y, float width, float height, Justification layout)
  304. {
  305. GlyphArrangement ga;
  306. ga.addJustifiedText (f, text, x, y, width, layout);
  307. auto bb = ga.getBoundingBox (0, -1, false);
  308. auto dy = y - bb.getY();
  309. if (layout.testFlags (Justification::verticallyCentred)) dy += (height - bb.getHeight()) * 0.5f;
  310. else if (layout.testFlags (Justification::bottom)) dy += (height - bb.getHeight());
  311. ga.moveRangeOfGlyphs (0, -1, 0.0f, dy);
  312. glyphs.addArray (ga.glyphs);
  313. }
  314. int GlyphArrangement::fitLineIntoSpace (int start, int numGlyphs, float x, float y, float w, float h, const Font& font,
  315. Justification justification, float minimumHorizontalScale)
  316. {
  317. int numDeleted = 0;
  318. auto lineStartX = glyphs.getReference (start).getLeft();
  319. auto lineWidth = glyphs.getReference (start + numGlyphs - 1).getRight() - lineStartX;
  320. if (lineWidth > w)
  321. {
  322. if (minimumHorizontalScale < 1.0f)
  323. {
  324. stretchRangeOfGlyphs (start, numGlyphs, jmax (minimumHorizontalScale, w / lineWidth));
  325. lineWidth = glyphs.getReference (start + numGlyphs - 1).getRight() - lineStartX - 0.5f;
  326. }
  327. if (lineWidth > w)
  328. {
  329. numDeleted = insertEllipsis (font, lineStartX + w, start, start + numGlyphs);
  330. numGlyphs -= numDeleted;
  331. }
  332. }
  333. justifyGlyphs (start, numGlyphs, x, y, w, h, justification);
  334. return numDeleted;
  335. }
  336. void GlyphArrangement::stretchRangeOfGlyphs (int startIndex, int num, float horizontalScaleFactor)
  337. {
  338. jassert (startIndex >= 0);
  339. if (num < 0 || startIndex + num > glyphs.size())
  340. num = glyphs.size() - startIndex;
  341. if (num > 0)
  342. {
  343. auto xAnchor = glyphs.getReference (startIndex).getLeft();
  344. while (--num >= 0)
  345. {
  346. auto& pg = glyphs.getReference (startIndex++);
  347. pg.x = xAnchor + (pg.x - xAnchor) * horizontalScaleFactor;
  348. pg.font.setHorizontalScale (pg.font.getHorizontalScale() * horizontalScaleFactor);
  349. pg.w *= horizontalScaleFactor;
  350. }
  351. }
  352. }
  353. Rectangle<float> GlyphArrangement::getBoundingBox (int startIndex, int num, bool includeWhitespace) const
  354. {
  355. jassert (startIndex >= 0);
  356. if (num < 0 || startIndex + num > glyphs.size())
  357. num = glyphs.size() - startIndex;
  358. Rectangle<float> result;
  359. while (--num >= 0)
  360. {
  361. auto& pg = glyphs.getReference (startIndex++);
  362. if (includeWhitespace || ! pg.isWhitespace())
  363. result = result.getUnion (pg.getBounds());
  364. }
  365. return result;
  366. }
  367. void GlyphArrangement::justifyGlyphs (int startIndex, int num,
  368. float x, float y, float width, float height,
  369. Justification justification)
  370. {
  371. jassert (num >= 0 && startIndex >= 0);
  372. if (glyphs.size() > 0 && num > 0)
  373. {
  374. auto bb = getBoundingBox (startIndex, num, ! justification.testFlags (Justification::horizontallyJustified
  375. | Justification::horizontallyCentred));
  376. float deltaX = x, deltaY = y;
  377. if (justification.testFlags (Justification::horizontallyJustified)) deltaX -= bb.getX();
  378. else if (justification.testFlags (Justification::horizontallyCentred)) deltaX += (width - bb.getWidth()) * 0.5f - bb.getX();
  379. else if (justification.testFlags (Justification::right)) deltaX += width - bb.getRight();
  380. else deltaX -= bb.getX();
  381. if (justification.testFlags (Justification::top)) deltaY -= bb.getY();
  382. else if (justification.testFlags (Justification::bottom)) deltaY += height - bb.getBottom();
  383. else deltaY += (height - bb.getHeight()) * 0.5f - bb.getY();
  384. moveRangeOfGlyphs (startIndex, num, deltaX, deltaY);
  385. if (justification.testFlags (Justification::horizontallyJustified))
  386. {
  387. int lineStart = 0;
  388. auto baseY = glyphs.getReference (startIndex).getBaselineY();
  389. int i;
  390. for (i = 0; i < num; ++i)
  391. {
  392. auto glyphY = glyphs.getReference (startIndex + i).getBaselineY();
  393. if (glyphY != baseY)
  394. {
  395. spreadOutLine (startIndex + lineStart, i - lineStart, width);
  396. lineStart = i;
  397. baseY = glyphY;
  398. }
  399. }
  400. if (i > lineStart)
  401. spreadOutLine (startIndex + lineStart, i - lineStart, width);
  402. }
  403. }
  404. }
  405. void GlyphArrangement::spreadOutLine (int start, int num, float targetWidth)
  406. {
  407. if (start + num < glyphs.size()
  408. && glyphs.getReference (start + num - 1).getCharacter() != '\r'
  409. && glyphs.getReference (start + num - 1).getCharacter() != '\n')
  410. {
  411. int numSpaces = 0;
  412. int spacesAtEnd = 0;
  413. for (int i = 0; i < num; ++i)
  414. {
  415. if (glyphs.getReference (start + i).isWhitespace())
  416. {
  417. ++spacesAtEnd;
  418. ++numSpaces;
  419. }
  420. else
  421. {
  422. spacesAtEnd = 0;
  423. }
  424. }
  425. numSpaces -= spacesAtEnd;
  426. if (numSpaces > 0)
  427. {
  428. auto startX = glyphs.getReference (start).getLeft();
  429. auto endX = glyphs.getReference (start + num - 1 - spacesAtEnd).getRight();
  430. auto extraPaddingBetweenWords = (targetWidth - (endX - startX)) / (float) numSpaces;
  431. float deltaX = 0.0f;
  432. for (int i = 0; i < num; ++i)
  433. {
  434. glyphs.getReference (start + i).moveBy (deltaX, 0.0f);
  435. if (glyphs.getReference (start + i).isWhitespace())
  436. deltaX += extraPaddingBetweenWords;
  437. }
  438. }
  439. }
  440. }
  441. static bool isBreakableGlyph (const PositionedGlyph& g) noexcept
  442. {
  443. return ! isNonBreakingSpace (g.getCharacter()) && (g.isWhitespace() || g.getCharacter() == '-');
  444. }
  445. void GlyphArrangement::splitLines (const String& text, Font font, int startIndex,
  446. float x, float y, float width, float height, int maximumLines,
  447. float lineWidth, Justification layout, float minimumHorizontalScale)
  448. {
  449. auto length = text.length();
  450. auto originalStartIndex = startIndex;
  451. int numLines = 1;
  452. if (length <= 12 && ! text.containsAnyOf (" -\t\r\n"))
  453. maximumLines = 1;
  454. maximumLines = jmin (maximumLines, length);
  455. while (numLines < maximumLines)
  456. {
  457. ++numLines;
  458. auto newFontHeight = height / (float) numLines;
  459. if (newFontHeight < font.getHeight())
  460. {
  461. font.setHeight (jmax (8.0f, newFontHeight));
  462. removeRangeOfGlyphs (startIndex, -1);
  463. addLineOfText (font, text, x, y);
  464. lineWidth = glyphs.getReference (glyphs.size() - 1).getRight()
  465. - glyphs.getReference (startIndex).getLeft();
  466. }
  467. // Try to estimate the point at which there are enough lines to fit the text,
  468. // allowing for unevenness in the lengths due to differently sized words.
  469. const float lineLengthUnevennessAllowance = 80.0f;
  470. if ((float) numLines > (lineWidth + lineLengthUnevennessAllowance) / width || newFontHeight < 8.0f)
  471. break;
  472. }
  473. if (numLines < 1)
  474. numLines = 1;
  475. int lineIndex = 0;
  476. auto lineY = y;
  477. auto widthPerLine = jmin (width / minimumHorizontalScale,
  478. lineWidth / (float) numLines);
  479. while (lineY < y + height)
  480. {
  481. auto endIndex = startIndex;
  482. auto lineStartX = glyphs.getReference (startIndex).getLeft();
  483. auto lineBottomY = lineY + font.getHeight();
  484. if (lineIndex++ >= numLines - 1
  485. || lineBottomY >= y + height)
  486. {
  487. widthPerLine = width;
  488. endIndex = glyphs.size();
  489. }
  490. else
  491. {
  492. while (endIndex < glyphs.size())
  493. {
  494. if (glyphs.getReference (endIndex).getRight() - lineStartX > widthPerLine)
  495. {
  496. // got to a point where the line's too long, so skip forward to find a
  497. // good place to break it..
  498. auto searchStartIndex = endIndex;
  499. while (endIndex < glyphs.size())
  500. {
  501. auto& g = glyphs.getReference (endIndex);
  502. if ((g.getRight() - lineStartX) * minimumHorizontalScale < width)
  503. {
  504. if (isBreakableGlyph (g))
  505. {
  506. ++endIndex;
  507. break;
  508. }
  509. }
  510. else
  511. {
  512. // can't find a suitable break, so try looking backwards..
  513. endIndex = searchStartIndex;
  514. for (int back = 1; back < jmin (7, endIndex - startIndex - 1); ++back)
  515. {
  516. if (isBreakableGlyph (glyphs.getReference (endIndex - back)))
  517. {
  518. endIndex -= back - 1;
  519. break;
  520. }
  521. }
  522. break;
  523. }
  524. ++endIndex;
  525. }
  526. break;
  527. }
  528. ++endIndex;
  529. }
  530. auto wsStart = endIndex;
  531. auto wsEnd = endIndex;
  532. while (wsStart > 0 && glyphs.getReference (wsStart - 1).isWhitespace())
  533. --wsStart;
  534. while (wsEnd < glyphs.size() && glyphs.getReference (wsEnd).isWhitespace())
  535. ++wsEnd;
  536. removeRangeOfGlyphs (wsStart, wsEnd - wsStart);
  537. endIndex = jmax (wsStart, startIndex + 1);
  538. }
  539. endIndex -= fitLineIntoSpace (startIndex, endIndex - startIndex,
  540. x, lineY, width, font.getHeight(), font,
  541. layout.getOnlyHorizontalFlags() | Justification::verticallyCentred,
  542. minimumHorizontalScale);
  543. startIndex = endIndex;
  544. lineY = lineBottomY;
  545. if (startIndex >= glyphs.size())
  546. break;
  547. }
  548. justifyGlyphs (originalStartIndex, glyphs.size() - originalStartIndex,
  549. x, y, width, height, layout.getFlags() & ~Justification::horizontallyJustified);
  550. }
  551. //==============================================================================
  552. void GlyphArrangement::drawGlyphUnderline (const Graphics& g, const PositionedGlyph& pg,
  553. int i, AffineTransform transform) const
  554. {
  555. auto lineThickness = (pg.font.getDescent()) * 0.3f;
  556. auto nextX = pg.x + pg.w;
  557. if (i < glyphs.size() - 1 && glyphs.getReference (i + 1).y == pg.y)
  558. nextX = glyphs.getReference (i + 1).x;
  559. Path p;
  560. p.addRectangle (pg.x, pg.y + lineThickness * 2.0f, nextX - pg.x, lineThickness);
  561. g.fillPath (p, transform);
  562. }
  563. void GlyphArrangement::draw (const Graphics& g) const
  564. {
  565. draw (g, {});
  566. }
  567. void GlyphArrangement::draw (const Graphics& g, AffineTransform transform) const
  568. {
  569. auto& context = g.getInternalContext();
  570. auto lastFont = context.getFont();
  571. bool needToRestore = false;
  572. for (int i = 0; i < glyphs.size(); ++i)
  573. {
  574. auto& pg = glyphs.getReference (i);
  575. if (pg.font.isUnderlined())
  576. drawGlyphUnderline (g, pg, i, transform);
  577. if (! pg.isWhitespace())
  578. {
  579. if (lastFont != pg.font)
  580. {
  581. lastFont = pg.font;
  582. if (! needToRestore)
  583. {
  584. needToRestore = true;
  585. context.saveState();
  586. }
  587. context.setFont (lastFont);
  588. }
  589. context.drawGlyph (pg.glyph, AffineTransform::translation (pg.x, pg.y)
  590. .followedBy (transform));
  591. }
  592. }
  593. if (needToRestore)
  594. context.restoreState();
  595. }
  596. void GlyphArrangement::createPath (Path& path) const
  597. {
  598. for (auto& g : glyphs)
  599. g.createPath (path);
  600. }
  601. int GlyphArrangement::findGlyphIndexAt (float x, float y) const
  602. {
  603. for (int i = 0; i < glyphs.size(); ++i)
  604. if (glyphs.getReference (i).hitTest (x, y))
  605. return i;
  606. return -1;
  607. }
  608. } // namespace juce