From ebbd7c48d399cfa6e9505b87025f9f27663971cf Mon Sep 17 00:00:00 2001 From: Maciej Suminski Date: Thu, 11 Apr 2013 14:21:59 +0200 Subject: [PATCH] Added multiline strings and text overbars rendering using GAL. --- common/gal/stroke_font.cpp | 59 ++++++++++++++++++++++++++++---------- include/gal/stroke_font.h | 23 +++++++-------- 2 files changed, 55 insertions(+), 27 deletions(-) diff --git a/common/gal/stroke_font.cpp b/common/gal/stroke_font.cpp index 8c22dcdda4..a38966b2d1 100644 --- a/common/gal/stroke_font.cpp +++ b/common/gal/stroke_font.cpp @@ -125,7 +125,7 @@ void STROKE_FONT::LoadAttributes( const EDA_TEXT* aText ) } -BOX2D STROKE_FONT::computeBoundingBox( Glyph aGlyph, VECTOR2D aGlyphBoundingX ) +BOX2D STROKE_FONT::computeBoundingBox( const Glyph& aGlyph, const VECTOR2D& aGlyphBoundingX ) const { BOX2D boundingBox; @@ -134,9 +134,9 @@ BOX2D STROKE_FONT::computeBoundingBox( Glyph aGlyph, VECTOR2D aGlyphBoundingX ) boundingPoints.push_back( VECTOR2D( aGlyphBoundingX.x, 0 ) ); boundingPoints.push_back( VECTOR2D( aGlyphBoundingX.y, 0 ) ); - for( Glyph::iterator pointListIt = aGlyph.begin(); pointListIt != aGlyph.end(); ++pointListIt ) + for( Glyph::const_iterator pointListIt = aGlyph.begin(); pointListIt != aGlyph.end(); ++pointListIt ) { - for( std::deque::iterator pointIt = pointListIt->begin(); + for( std::deque::const_iterator pointIt = pointListIt->begin(); pointIt != pointListIt->end(); ++pointIt ) { boundingPoints.push_back( VECTOR2D( aGlyphBoundingX.x, pointIt->y ) ); @@ -149,11 +149,26 @@ BOX2D STROKE_FONT::computeBoundingBox( Glyph aGlyph, VECTOR2D aGlyphBoundingX ) } -void STROKE_FONT::Draw( std::string aText, VECTOR2D aPosition, double aRotationAngle ) +void STROKE_FONT::Draw( std::string aText, const VECTOR2D& aPosition, double aRotationAngle ) { + // Split multiline strings into separate ones and draw line by line + size_t newlinePos = aText.find( '\n' ); + + if( newlinePos != std::string::npos ) + { + VECTOR2D nextlinePosition( aPosition ); + nextlinePosition += VECTOR2D( 0.0, m_glyphSize.y * 1.6 ); // FIXME remove magic number + + Draw( aText.substr( newlinePos + 1 ), nextlinePosition, aRotationAngle ); + aText = aText.substr( 0, newlinePos ); + } + // Compute the text size VECTOR2D textsize = computeTextSize( aText ); + // By default overbar is turned off + m_overbar = false; + // Context needs to be saved before any transformations m_gal->Save(); @@ -221,8 +236,14 @@ void STROKE_FONT::Draw( std::string aText, VECTOR2D aPosition, double aRotationA m_gal->SetLineWidth( m_gal->GetLineWidth() * 1.3 ); } - for( std::string::iterator chIt = aText.begin(); chIt != aText.end(); chIt++ ) + for( std::string::const_iterator chIt = aText.begin(); chIt != aText.end(); chIt++ ) { + if( *chIt == '~' ) + { + m_overbar = !m_overbar; + continue; + } + GlyphList::iterator glyphIt = m_glyphs.begin(); std::deque::iterator bbIt = m_glyphBoundingBoxes.begin(); @@ -254,26 +275,34 @@ void STROKE_FONT::Draw( std::string aText, VECTOR2D aPosition, double aRotationA m_gal->DrawPolyline( pointListScaled ); } - xOffset += m_scaleFactor * glyphSizeX * - ( bbIt->GetEnd().x - bbIt->GetOrigin().x ); + if( m_overbar ) + { + VECTOR2D startOverbar( xOffset, -textsize.y * 1.2 ); + VECTOR2D endOverbar( xOffset + m_scaleFactor * glyphSizeX * bbIt->GetEnd().x, + -textsize.y * 1.2 ); + m_gal->DrawLine( startOverbar, endOverbar ); + } + + xOffset += m_scaleFactor * glyphSizeX * bbIt->GetEnd().x; } m_gal->Restore(); } -VECTOR2D STROKE_FONT::computeTextSize( std::string aText ) +VECTOR2D STROKE_FONT::computeTextSize( const std::string& aText ) const { - VECTOR2D result = VECTOR2D( 0.0, 0.0 ); + VECTOR2D result = VECTOR2D( 0.0, m_glyphSize.y ); - for( std::string::iterator chIt = aText.begin(); chIt != aText.end(); chIt++ ) + for( std::string::const_iterator chIt = aText.begin(); chIt != aText.end(); chIt++ ) { - std::deque::iterator bbIt = m_glyphBoundingBoxes.begin(); - advance( bbIt, (int) ( *chIt ) - (int) ' ' ); - result.x += m_scaleFactor * m_glyphSize.x * ( bbIt->GetEnd().x - bbIt->GetOrigin().x ); - } + if( *chIt == '~' ) + continue; - result.y = m_glyphSize.y; + std::deque::const_iterator bbIt = m_glyphBoundingBoxes.begin(); + advance( bbIt, (int) ( *chIt ) - (int) ' ' ); + result.x += m_scaleFactor * m_glyphSize.x * bbIt->GetEnd().x; + } return result; } diff --git a/include/gal/stroke_font.h b/include/gal/stroke_font.h index a94aace854..96aa5647b5 100644 --- a/include/gal/stroke_font.h +++ b/include/gal/stroke_font.h @@ -80,7 +80,7 @@ public: * @param aPosition is the text position in world coordinates. * @param aRotationAngle is the text rotation angle. */ - void Draw( std::string aText, VECTOR2D aPosition, double aRotationAngle ); + void Draw( std::string aText, const VECTOR2D& aPosition, double aRotationAngle ); /** * @brief Set the scale factor of the font for the glyph size. @@ -153,15 +153,14 @@ public: } private: - GAL* m_gal; ///< Pointer to the GAL - - GlyphList m_glyphs; ///< Glyph list - std::deque m_glyphBoundingBoxes; ///< Bounding boxes of the glyphs - double m_scaleFactor; ///< Scale factor for the glyph - VECTOR2D m_glyphSize; ///< Size of the glyphs - EDA_TEXT_HJUSTIFY_T m_horizontalJustify; ///< Horizontal justification - EDA_TEXT_VJUSTIFY_T m_verticalJustify; ///< Vertical justification - bool m_bold, m_italic, m_mirrored; ///< Properties of text + GAL* m_gal; ///< Pointer to the GAL + GlyphList m_glyphs; ///< Glyph list + std::deque m_glyphBoundingBoxes; ///< Bounding boxes of the glyphs + double m_scaleFactor; ///< Scale factor for the glyph + VECTOR2D m_glyphSize; ///< Size of the glyphs + EDA_TEXT_HJUSTIFY_T m_horizontalJustify; ///< Horizontal justification + EDA_TEXT_VJUSTIFY_T m_verticalJustify; ///< Vertical justification + bool m_bold, m_italic, m_mirrored, m_overbar; ///< Properties of text /** * @brief Compute the bounding box of a given glyph. @@ -170,7 +169,7 @@ private: * @param aGlyphBoundingX is the x-component of the bounding box size. * @return is the complete bounding box size. */ - BOX2D computeBoundingBox( Glyph aGlyph, VECTOR2D aGlyphBoundingX ); + BOX2D computeBoundingBox( const Glyph& aGlyph, const VECTOR2D& aGlyphBoundingX ) const; /** * @brief Compute the size of a given text. @@ -178,7 +177,7 @@ private: * @param aText is the text string. * @return is the text size. */ - VECTOR2D computeTextSize( const std::string aText ); + VECTOR2D computeTextSize( const std::string& aText ) const; }; } // namespace KiGfx