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 26KB

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