|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760 |
- /*
- ==============================================================================
-
- This file is part of the JUCE library.
- Copyright (c) 2013 - Raw Material Software Ltd.
-
- Permission is granted to use this software under the terms of either:
- a) the GPL v2 (or any later version)
- b) the Affero GPL v3
-
- Details of these licenses can be found at: www.gnu.org/licenses
-
- JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
- WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
- A PARTICULAR PURPOSE. See the GNU General Public License for more details.
-
- ------------------------------------------------------------------------------
-
- To release a closed-source product which uses JUCE, commercial licenses are
- available: visit www.juce.com for more information.
-
- ==============================================================================
- */
-
- PositionedGlyph::PositionedGlyph() noexcept
- : character (0), glyph (0), x (0), y (0), w (0), whitespace (false)
- {
- }
-
- PositionedGlyph::PositionedGlyph (const Font& font_, const juce_wchar character_, const int glyph_,
- const float x_, const float y_, const float w_, const bool whitespace_)
- : font (font_), character (character_), glyph (glyph_),
- x (x_), y (y_), w (w_), whitespace (whitespace_)
- {
- }
-
- PositionedGlyph::PositionedGlyph (const PositionedGlyph& other)
- : font (other.font), character (other.character), glyph (other.glyph),
- x (other.x), y (other.y), w (other.w), whitespace (other.whitespace)
- {
- }
-
- PositionedGlyph::~PositionedGlyph() {}
-
- PositionedGlyph& PositionedGlyph::operator= (const PositionedGlyph& other)
- {
- font = other.font;
- character = other.character;
- glyph = other.glyph;
- x = other.x;
- y = other.y;
- w = other.w;
- whitespace = other.whitespace;
- return *this;
- }
-
- static inline void drawGlyphWithFont (const Graphics& g, int glyph, const Font& font, const AffineTransform& t)
- {
- LowLevelGraphicsContext& context = g.getInternalContext();
- context.setFont (font);
- context.drawGlyph (glyph, t);
- }
-
- void PositionedGlyph::draw (const Graphics& g) const
- {
- if (! isWhitespace())
- drawGlyphWithFont (g, glyph, font, AffineTransform::translation (x, y));
- }
-
- void PositionedGlyph::draw (const Graphics& g, const AffineTransform& transform) const
- {
- if (! isWhitespace())
- drawGlyphWithFont (g, glyph, font, AffineTransform::translation (x, y).followedBy (transform));
- }
-
- void PositionedGlyph::createPath (Path& path) const
- {
- if (! isWhitespace())
- {
- if (Typeface* const t = font.getTypeface())
- {
- Path p;
- t->getOutlineForGlyph (glyph, p);
-
- path.addPath (p, AffineTransform::scale (font.getHeight() * font.getHorizontalScale(), font.getHeight())
- .translated (x, y));
- }
- }
- }
-
- bool PositionedGlyph::hitTest (float px, float py) const
- {
- if (getBounds().contains (px, py) && ! isWhitespace())
- {
- if (Typeface* const t = font.getTypeface())
- {
- Path p;
- t->getOutlineForGlyph (glyph, p);
-
- AffineTransform::translation (-x, -y)
- .scaled (1.0f / (font.getHeight() * font.getHorizontalScale()), 1.0f / font.getHeight())
- .transformPoint (px, py);
-
- return p.contains (px, py);
- }
- }
-
- return false;
- }
-
- void PositionedGlyph::moveBy (const float deltaX,
- const float deltaY)
- {
- x += deltaX;
- y += deltaY;
- }
-
-
- //==============================================================================
- GlyphArrangement::GlyphArrangement()
- {
- glyphs.ensureStorageAllocated (128);
- }
-
- GlyphArrangement::GlyphArrangement (const GlyphArrangement& other)
- : glyphs (other.glyphs)
- {
- }
-
- GlyphArrangement& GlyphArrangement::operator= (const GlyphArrangement& other)
- {
- glyphs = other.glyphs;
- return *this;
- }
-
- GlyphArrangement::~GlyphArrangement()
- {
- }
-
- //==============================================================================
- void GlyphArrangement::clear()
- {
- glyphs.clear();
- }
-
- PositionedGlyph& GlyphArrangement::getGlyph (const int index) const noexcept
- {
- return glyphs.getReference (index);
- }
-
- //==============================================================================
- void GlyphArrangement::addGlyphArrangement (const GlyphArrangement& other)
- {
- glyphs.addArray (other.glyphs);
- }
-
- void GlyphArrangement::addGlyph (const PositionedGlyph& glyph)
- {
- glyphs.add (glyph);
- }
-
- void GlyphArrangement::removeRangeOfGlyphs (int startIndex, const int num)
- {
- glyphs.removeRange (startIndex, num < 0 ? glyphs.size() : num);
- }
-
- //==============================================================================
- void GlyphArrangement::addLineOfText (const Font& font,
- const String& text,
- const float xOffset,
- const float yOffset)
- {
- addCurtailedLineOfText (font, text, xOffset, yOffset, 1.0e10f, false);
- }
-
- void GlyphArrangement::addCurtailedLineOfText (const Font& font,
- const String& text,
- const float xOffset,
- const float yOffset,
- const float maxWidthPixels,
- const bool useEllipsis)
- {
- if (text.isNotEmpty())
- {
- Array <int> newGlyphs;
- Array <float> xOffsets;
- font.getGlyphPositions (text, newGlyphs, xOffsets);
- const int textLen = newGlyphs.size();
- glyphs.ensureStorageAllocated (glyphs.size() + textLen);
-
- String::CharPointerType t (text.getCharPointer());
-
- for (int i = 0; i < textLen; ++i)
- {
- const float nextX = xOffsets.getUnchecked (i + 1);
-
- if (nextX > maxWidthPixels + 1.0f)
- {
- // curtail the string if it's too wide..
- if (useEllipsis && textLen > 3 && glyphs.size() >= 3)
- insertEllipsis (font, xOffset + maxWidthPixels, 0, glyphs.size());
-
- break;
- }
- else
- {
- const float thisX = xOffsets.getUnchecked (i);
- const bool isWhitespace = t.isWhitespace();
-
- glyphs.add (PositionedGlyph (font, t.getAndAdvance(),
- newGlyphs.getUnchecked(i),
- xOffset + thisX, yOffset,
- nextX - thisX, isWhitespace));
- }
- }
- }
- }
-
- int GlyphArrangement::insertEllipsis (const Font& font, const float maxXPos,
- const int startIndex, int endIndex)
- {
- int numDeleted = 0;
-
- if (glyphs.size() > 0)
- {
- Array<int> dotGlyphs;
- Array<float> dotXs;
- font.getGlyphPositions ("..", dotGlyphs, dotXs);
-
- const float dx = dotXs[1];
- float xOffset = 0.0f, yOffset = 0.0f;
-
- while (endIndex > startIndex)
- {
- const PositionedGlyph& pg = glyphs.getReference (--endIndex);
- xOffset = pg.x;
- yOffset = pg.y;
-
- glyphs.remove (endIndex);
- ++numDeleted;
-
- if (xOffset + dx * 3 <= maxXPos)
- break;
- }
-
- for (int i = 3; --i >= 0;)
- {
- glyphs.insert (endIndex++, PositionedGlyph (font, '.', dotGlyphs.getFirst(),
- xOffset, yOffset, dx, false));
- --numDeleted;
- xOffset += dx;
-
- if (xOffset > maxXPos)
- break;
- }
- }
-
- return numDeleted;
- }
-
- void GlyphArrangement::addJustifiedText (const Font& font,
- const String& text,
- float x, float y,
- const float maxLineWidth,
- Justification horizontalLayout)
- {
- int lineStartIndex = glyphs.size();
- addLineOfText (font, text, x, y);
-
- const float originalY = y;
-
- while (lineStartIndex < glyphs.size())
- {
- int i = lineStartIndex;
-
- if (glyphs.getReference(i).getCharacter() != '\n'
- && glyphs.getReference(i).getCharacter() != '\r')
- ++i;
-
- const float lineMaxX = glyphs.getReference (lineStartIndex).getLeft() + maxLineWidth;
- int lastWordBreakIndex = -1;
-
- while (i < glyphs.size())
- {
- const PositionedGlyph& pg = glyphs.getReference (i);
- const juce_wchar c = pg.getCharacter();
-
- if (c == '\r' || c == '\n')
- {
- ++i;
-
- if (c == '\r' && i < glyphs.size()
- && glyphs.getReference(i).getCharacter() == '\n')
- ++i;
-
- break;
- }
- else if (pg.isWhitespace())
- {
- lastWordBreakIndex = i + 1;
- }
- else if (pg.getRight() - 0.0001f >= lineMaxX)
- {
- if (lastWordBreakIndex >= 0)
- i = lastWordBreakIndex;
-
- break;
- }
-
- ++i;
- }
-
- const float currentLineStartX = glyphs.getReference (lineStartIndex).getLeft();
- float currentLineEndX = currentLineStartX;
-
- for (int j = i; --j >= lineStartIndex;)
- {
- if (! glyphs.getReference (j).isWhitespace())
- {
- currentLineEndX = glyphs.getReference (j).getRight();
- break;
- }
- }
-
- float deltaX = 0.0f;
-
- if (horizontalLayout.testFlags (Justification::horizontallyJustified))
- spreadOutLine (lineStartIndex, i - lineStartIndex, maxLineWidth);
- else if (horizontalLayout.testFlags (Justification::horizontallyCentred))
- deltaX = (maxLineWidth - (currentLineEndX - currentLineStartX)) * 0.5f;
- else if (horizontalLayout.testFlags (Justification::right))
- deltaX = maxLineWidth - (currentLineEndX - currentLineStartX);
-
- moveRangeOfGlyphs (lineStartIndex, i - lineStartIndex,
- x + deltaX - currentLineStartX, y - originalY);
-
- lineStartIndex = i;
-
- y += font.getHeight();
- }
- }
-
- void GlyphArrangement::addFittedText (const Font& f,
- const String& text,
- const float x, const float y,
- const float width, const float height,
- Justification layout,
- int maximumLines,
- const float minimumHorizontalScale)
- {
- // doesn't make much sense if this is outside a sensible range of 0.5 to 1.0
- jassert (minimumHorizontalScale > 0 && minimumHorizontalScale <= 1.0f);
-
- if (text.containsAnyOf ("\r\n"))
- {
- GlyphArrangement ga;
- ga.addJustifiedText (f, text, x, y, width, layout);
-
- const Rectangle<float> bb (ga.getBoundingBox (0, -1, false));
-
- float dy = y - bb.getY();
-
- if (layout.testFlags (Justification::verticallyCentred)) dy += (height - bb.getHeight()) * 0.5f;
- else if (layout.testFlags (Justification::bottom)) dy += (height - bb.getHeight());
-
- ga.moveRangeOfGlyphs (0, -1, 0.0f, dy);
-
- glyphs.addArray (ga.glyphs);
- return;
- }
-
- int startIndex = glyphs.size();
- addLineOfText (f, text.trim(), x, y);
-
- if (glyphs.size() > startIndex)
- {
- float lineWidth = glyphs.getReference (glyphs.size() - 1).getRight()
- - glyphs.getReference (startIndex).getLeft();
-
- if (lineWidth <= 0)
- return;
-
- if (lineWidth * minimumHorizontalScale < width)
- {
- if (lineWidth > width)
- stretchRangeOfGlyphs (startIndex, glyphs.size() - startIndex,
- width / lineWidth);
-
- justifyGlyphs (startIndex, glyphs.size() - startIndex,
- x, y, width, height, layout);
- }
- else if (maximumLines <= 1)
- {
- fitLineIntoSpace (startIndex, glyphs.size() - startIndex,
- x, y, width, height, f, layout, minimumHorizontalScale);
- }
- else
- {
- Font font (f);
- String txt (text.trim());
- const int length = txt.length();
- const int originalStartIndex = startIndex;
- int numLines = 1;
-
- if (length <= 12 && ! txt.containsAnyOf (" -\t\r\n"))
- maximumLines = 1;
-
- maximumLines = jmin (maximumLines, length);
-
- while (numLines < maximumLines)
- {
- ++numLines;
-
- const float newFontHeight = height / (float) numLines;
-
- if (newFontHeight < font.getHeight())
- {
- font.setHeight (jmax (8.0f, newFontHeight));
-
- removeRangeOfGlyphs (startIndex, -1);
- addLineOfText (font, txt, x, y);
-
- lineWidth = glyphs.getReference (glyphs.size() - 1).getRight()
- - glyphs.getReference (startIndex).getLeft();
- }
-
- if (numLines > lineWidth / width || newFontHeight < 8.0f)
- break;
- }
-
- if (numLines < 1)
- numLines = 1;
-
- float lineY = y;
- float widthPerLine = lineWidth / numLines;
-
- for (int line = 0; line < numLines; ++line)
- {
- int i = startIndex;
- float lineStartX = glyphs.getReference (startIndex).getLeft();
-
- if (line == numLines - 1)
- {
- widthPerLine = width;
- i = glyphs.size();
- }
- else
- {
- while (i < glyphs.size())
- {
- lineWidth = (glyphs.getReference (i).getRight() - lineStartX);
-
- if (lineWidth > widthPerLine)
- {
- // got to a point where the line's too long, so skip forward to find a
- // good place to break it..
- const int searchStartIndex = i;
-
- while (i < glyphs.size())
- {
- if ((glyphs.getReference (i).getRight() - lineStartX) * minimumHorizontalScale < width)
- {
- if (glyphs.getReference (i).isWhitespace()
- || glyphs.getReference (i).getCharacter() == '-')
- {
- ++i;
- break;
- }
- }
- else
- {
- // can't find a suitable break, so try looking backwards..
- i = searchStartIndex;
-
- for (int back = 1; back < jmin (7, i - startIndex - 1); ++back)
- {
- if (glyphs.getReference (i - back).isWhitespace()
- || glyphs.getReference (i - back).getCharacter() == '-')
- {
- i -= back - 1;
- break;
- }
- }
-
- break;
- }
-
- ++i;
- }
-
- break;
- }
-
- ++i;
- }
-
- int wsStart = i;
- while (wsStart > 0 && glyphs.getReference (wsStart - 1).isWhitespace())
- --wsStart;
-
- int wsEnd = i;
-
- while (wsEnd < glyphs.size() && glyphs.getReference (wsEnd).isWhitespace())
- ++wsEnd;
-
- removeRangeOfGlyphs (wsStart, wsEnd - wsStart);
- i = jmax (wsStart, startIndex + 1);
- }
-
- i -= fitLineIntoSpace (startIndex, i - startIndex,
- x, lineY, width, font.getHeight(), font,
- layout.getOnlyHorizontalFlags() | Justification::verticallyCentred,
- minimumHorizontalScale);
-
- startIndex = i;
- lineY += font.getHeight();
-
- if (startIndex >= glyphs.size())
- break;
- }
-
- justifyGlyphs (originalStartIndex, glyphs.size() - originalStartIndex,
- x, y, width, height, layout.getFlags() & ~Justification::horizontallyJustified);
- }
- }
- }
-
- //==============================================================================
- void GlyphArrangement::moveRangeOfGlyphs (int startIndex, int num, const float dx, const float dy)
- {
- jassert (startIndex >= 0);
-
- if (dx != 0.0f || dy != 0.0f)
- {
- if (num < 0 || startIndex + num > glyphs.size())
- num = glyphs.size() - startIndex;
-
- while (--num >= 0)
- glyphs.getReference (startIndex++).moveBy (dx, dy);
- }
- }
-
- int GlyphArrangement::fitLineIntoSpace (int start, int numGlyphs, float x, float y, float w, float h, const Font& font,
- Justification justification, float minimumHorizontalScale)
- {
- int numDeleted = 0;
- const float lineStartX = glyphs.getReference (start).getLeft();
- float lineWidth = glyphs.getReference (start + numGlyphs - 1).getRight() - lineStartX;
-
- if (lineWidth > w)
- {
- if (minimumHorizontalScale < 1.0f)
- {
- stretchRangeOfGlyphs (start, numGlyphs, jmax (minimumHorizontalScale, w / lineWidth));
- lineWidth = glyphs.getReference (start + numGlyphs - 1).getRight() - lineStartX - 0.5f;
- }
-
- if (lineWidth > w)
- {
- numDeleted = insertEllipsis (font, lineStartX + w, start, start + numGlyphs);
- numGlyphs -= numDeleted;
- }
- }
-
- justifyGlyphs (start, numGlyphs, x, y, w, h, justification);
- return numDeleted;
- }
-
- void GlyphArrangement::stretchRangeOfGlyphs (int startIndex, int num,
- const float horizontalScaleFactor)
- {
- jassert (startIndex >= 0);
-
- if (num < 0 || startIndex + num > glyphs.size())
- num = glyphs.size() - startIndex;
-
- if (num > 0)
- {
- const float xAnchor = glyphs.getReference (startIndex).getLeft();
-
- while (--num >= 0)
- {
- PositionedGlyph& pg = glyphs.getReference (startIndex++);
-
- pg.x = xAnchor + (pg.x - xAnchor) * horizontalScaleFactor;
- pg.font.setHorizontalScale (pg.font.getHorizontalScale() * horizontalScaleFactor);
- pg.w *= horizontalScaleFactor;
- }
- }
- }
-
- Rectangle<float> GlyphArrangement::getBoundingBox (int startIndex, int num, const bool includeWhitespace) const
- {
- jassert (startIndex >= 0);
-
- if (num < 0 || startIndex + num > glyphs.size())
- num = glyphs.size() - startIndex;
-
- Rectangle<float> result;
-
- while (--num >= 0)
- {
- const PositionedGlyph& pg = glyphs.getReference (startIndex++);
-
- if (includeWhitespace || ! pg.isWhitespace())
- result = result.getUnion (pg.getBounds());
- }
-
- return result;
- }
-
- void GlyphArrangement::justifyGlyphs (const int startIndex, const int num,
- const float x, const float y, const float width, const float height,
- Justification justification)
- {
- jassert (num >= 0 && startIndex >= 0);
-
- if (glyphs.size() > 0 && num > 0)
- {
- const Rectangle<float> bb (getBoundingBox (startIndex, num, ! justification.testFlags (Justification::horizontallyJustified
- | Justification::horizontallyCentred)));
- float deltaX = 0.0f, deltaY = 0.0f;
-
- if (justification.testFlags (Justification::horizontallyJustified)) deltaX = x - bb.getX();
- else if (justification.testFlags (Justification::horizontallyCentred)) deltaX = x + (width - bb.getWidth()) * 0.5f - bb.getX();
- else if (justification.testFlags (Justification::right)) deltaX = x + width - bb.getRight();
- else deltaX = x - bb.getX();
-
- if (justification.testFlags (Justification::top)) deltaY = y - bb.getY();
- else if (justification.testFlags (Justification::bottom)) deltaY = y + height - bb.getBottom();
- else deltaY = y + (height - bb.getHeight()) * 0.5f - bb.getY();
-
- moveRangeOfGlyphs (startIndex, num, deltaX, deltaY);
-
- if (justification.testFlags (Justification::horizontallyJustified))
- {
- int lineStart = 0;
- float baseY = glyphs.getReference (startIndex).getBaselineY();
-
- int i;
- for (i = 0; i < num; ++i)
- {
- const float glyphY = glyphs.getReference (startIndex + i).getBaselineY();
-
- if (glyphY != baseY)
- {
- spreadOutLine (startIndex + lineStart, i - lineStart, width);
-
- lineStart = i;
- baseY = glyphY;
- }
- }
-
- if (i > lineStart)
- spreadOutLine (startIndex + lineStart, i - lineStart, width);
- }
- }
- }
-
- void GlyphArrangement::spreadOutLine (const int start, const int num, const float targetWidth)
- {
- if (start + num < glyphs.size()
- && glyphs.getReference (start + num - 1).getCharacter() != '\r'
- && glyphs.getReference (start + num - 1).getCharacter() != '\n')
- {
- int numSpaces = 0;
- int spacesAtEnd = 0;
-
- for (int i = 0; i < num; ++i)
- {
- if (glyphs.getReference (start + i).isWhitespace())
- {
- ++spacesAtEnd;
- ++numSpaces;
- }
- else
- {
- spacesAtEnd = 0;
- }
- }
-
- numSpaces -= spacesAtEnd;
-
- if (numSpaces > 0)
- {
- const float startX = glyphs.getReference (start).getLeft();
- const float endX = glyphs.getReference (start + num - 1 - spacesAtEnd).getRight();
-
- const float extraPaddingBetweenWords
- = (targetWidth - (endX - startX)) / (float) numSpaces;
-
- float deltaX = 0.0f;
-
- for (int i = 0; i < num; ++i)
- {
- glyphs.getReference (start + i).moveBy (deltaX, 0.0f);
-
- if (glyphs.getReference (start + i).isWhitespace())
- deltaX += extraPaddingBetweenWords;
- }
- }
- }
- }
-
- //==============================================================================
- inline void GlyphArrangement::drawGlyphUnderline (const Graphics& g, const PositionedGlyph& pg,
- const int i, const AffineTransform& transform) const
- {
- const float lineThickness = (pg.font.getDescent()) * 0.3f;
-
- float nextX = pg.x + pg.w;
-
- if (i < glyphs.size() - 1 && glyphs.getReference (i + 1).y == pg.y)
- nextX = glyphs.getReference (i + 1).x;
-
- Path p;
- p.addRectangle (pg.x, pg.y + lineThickness * 2.0f, nextX - pg.x, lineThickness);
- g.fillPath (p, transform);
- }
-
- void GlyphArrangement::draw (const Graphics& g) const
- {
- for (int i = 0; i < glyphs.size(); ++i)
- {
- const PositionedGlyph& pg = glyphs.getReference(i);
-
- if (pg.font.isUnderlined())
- drawGlyphUnderline (g, pg, i, AffineTransform::identity);
-
- pg.draw (g);
- }
- }
-
- void GlyphArrangement::draw (const Graphics& g, const AffineTransform& transform) const
- {
- for (int i = 0; i < glyphs.size(); ++i)
- {
- const PositionedGlyph& pg = glyphs.getReference(i);
-
- if (pg.font.isUnderlined())
- drawGlyphUnderline (g, pg, i, transform);
-
- pg.draw (g, transform);
- }
- }
-
- void GlyphArrangement::createPath (Path& path) const
- {
- for (int i = 0; i < glyphs.size(); ++i)
- glyphs.getReference (i).createPath (path);
- }
-
- int GlyphArrangement::findGlyphIndexAt (const float x, const float y) const
- {
- for (int i = 0; i < glyphs.size(); ++i)
- if (glyphs.getReference (i).hitTest (x, y))
- return i;
-
- return -1;
- }
|