juce_GlyphArrangement.cpp 25 KB

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