From ccc89aaae9a40af8f02c2029fc78858bb37a81ea Mon Sep 17 00:00:00 2001 From: jules Date: Thu, 20 Dec 2012 20:46:17 +0000 Subject: [PATCH] Minor tidying-up. --- .../fonts/juce_GlyphArrangement.cpp | 93 +++++++------------ .../fonts/juce_GlyphArrangement.h | 1 + 2 files changed, 37 insertions(+), 57 deletions(-) diff --git a/modules/juce_graphics/fonts/juce_GlyphArrangement.cpp b/modules/juce_graphics/fonts/juce_GlyphArrangement.cpp index c9d7bc6052..2783dc8d2f 100644 --- a/modules/juce_graphics/fonts/juce_GlyphArrangement.cpp +++ b/modules/juce_graphics/fonts/juce_GlyphArrangement.cpp @@ -55,33 +55,30 @@ PositionedGlyph& PositionedGlyph::operator= (const PositionedGlyph& other) 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()) - { - LowLevelGraphicsContext& context = g.getInternalContext(); - context.setFont (font); - context.drawGlyph (glyph, AffineTransform::translation (x, y)); - } + drawGlyphWithFont (g, glyph, font, AffineTransform::translation (x, y)); } void PositionedGlyph::draw (const Graphics& g, const AffineTransform& transform) const { if (! isWhitespace()) - { - LowLevelGraphicsContext& context = g.getInternalContext(); - context.setFont (font); - context.drawGlyph (glyph, AffineTransform::translation (x, y).followedBy (transform)); - } + drawGlyphWithFont (g, glyph, font, AffineTransform::translation (x, y).followedBy (transform)); } void PositionedGlyph::createPath (Path& path) const { if (! isWhitespace()) { - Typeface* const t = font.getTypeface(); - - if (t != nullptr) + if (Typeface* const t = font.getTypeface()) { Path p; t->getOutlineForGlyph (glyph, p); @@ -96,9 +93,7 @@ bool PositionedGlyph::hitTest (float px, float py) const { if (getBounds().contains (px, py) && ! isWhitespace()) { - Typeface* const t = font.getTypeface(); - - if (t != nullptr) + if (Typeface* const t = font.getTypeface()) { Path p; t->getOutlineForGlyph (glyph, p); @@ -625,25 +620,16 @@ void GlyphArrangement::justifyGlyphs (const int startIndex, const int num, { const Rectangle bb (getBoundingBox (startIndex, num, ! justification.testFlags (Justification::horizontallyJustified | Justification::horizontallyCentred))); - float deltaX = 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(); + float deltaX = 0.0f, deltaY = 0.0f; - float 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(); + 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); @@ -718,6 +704,21 @@ void GlyphArrangement::spreadOutLine (const int start, const int num, const floa } //============================================================================== +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) @@ -725,17 +726,7 @@ void GlyphArrangement::draw (const Graphics& g) const const PositionedGlyph& pg = glyphs.getReference(i); if (pg.font.isUnderlined()) - { - 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; - - g.fillRect (pg.x, pg.y + lineThickness * 2.0f, - nextX - pg.x, lineThickness); - } + drawGlyphUnderline (g, pg, i, AffineTransform::identity); pg.draw (g); } @@ -748,19 +739,7 @@ void GlyphArrangement::draw (const Graphics& g, const AffineTransform& transform const PositionedGlyph& pg = glyphs.getReference(i); if (pg.font.isUnderlined()) - { - 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.addLineSegment (Line (pg.x, pg.y + lineThickness * 2.0f, - nextX, pg.y + lineThickness * 2.0f), lineThickness); - g.fillPath (p, transform); - } + drawGlyphUnderline (g, pg, i, transform); pg.draw (g, transform); } diff --git a/modules/juce_graphics/fonts/juce_GlyphArrangement.h b/modules/juce_graphics/fonts/juce_GlyphArrangement.h index 07868147eb..9e49d54fbd 100644 --- a/modules/juce_graphics/fonts/juce_GlyphArrangement.h +++ b/modules/juce_graphics/fonts/juce_GlyphArrangement.h @@ -308,6 +308,7 @@ private: int fitLineIntoSpace (int start, int numGlyphs, float x, float y, float w, float h, const Font&, const Justification&, float minimumHorizontalScale); void spreadOutLine (int start, int numGlyphs, float targetWidth); + void drawGlyphUnderline (const Graphics&, const PositionedGlyph&, int, const AffineTransform&) const; JUCE_LEAK_DETECTOR (GlyphArrangement) };