diff --git a/common/gal/stroke_font.cpp b/common/gal/stroke_font.cpp index 2ec1b43a0d..f9549ce070 100644 --- a/common/gal/stroke_font.cpp +++ b/common/gal/stroke_font.cpp @@ -3,6 +3,8 @@ * * Copyright (C) 2012 Torsten Hueter, torstenhtr gmx.de * Copyright (C) 2012 Kicad Developers, see change_log.txt for contributors. + * Copyright (C) 2013 CERN + * @author Maciej Suminski * * Stroke font class * @@ -26,12 +28,13 @@ #include #include +#include using namespace KIGFX; - -const double STROKE_FONT::LINE_HEIGHT_RATIO = 1.6; - +const double STROKE_FONT::OVERBAR_HEIGHT = 0.45; +const double STROKE_FONT::BOLD_FACTOR = 1.3; +const double STROKE_FONT::HERSHEY_SCALE = 1.0 / 21.0; STROKE_FONT::STROKE_FONT( GAL* aGal ) : m_gal( aGal ), @@ -40,22 +43,18 @@ STROKE_FONT::STROKE_FONT( GAL* aGal ) : m_mirrored( false ) { // Default values - m_scaleFactor = 1.0 / 21.0; m_glyphSize = VECTOR2D( 10.0, 10.0 ); m_verticalJustify = GR_TEXT_VJUSTIFY_BOTTOM; m_horizontalJustify = GR_TEXT_HJUSTIFY_LEFT; } -STROKE_FONT::~STROKE_FONT() -{ -} - - bool STROKE_FONT::LoadNewStrokeFont( const char* const aNewStrokeFont[], int aNewStrokeFontSize ) { m_glyphs.clear(); m_glyphBoundingBoxes.clear(); + m_glyphs.resize( aNewStrokeFontSize ); + m_glyphBoundingBoxes.resize( aNewStrokeFontSize ); for( int j = 0; j < aNewStrokeFontSize; j++ ) { @@ -81,8 +80,8 @@ bool STROKE_FONT::LoadNewStrokeFont( const char* const aNewStrokeFont[], int aNe if( i < 2 ) { // The first two values contain the width of the char - glyphStartX = coordinate[0] - 'R'; - glyphEndX = coordinate[1] - 'R'; + glyphStartX = ( coordinate[0] - 'R' ) * HERSHEY_SCALE; + glyphEndX = ( coordinate[1] - 'R' ) * HERSHEY_SCALE; glyphBoundingX = VECTOR2D( 0, glyphEndX - glyphStartX ); } else if( ( coordinate[0] == ' ' ) && ( coordinate[1] == 'R' ) ) @@ -97,8 +96,8 @@ bool STROKE_FONT::LoadNewStrokeFont( const char* const aNewStrokeFont[], int aNe { // Every coordinate description of the Hershey format has an offset, // it has to be subtracted - point.x = (double) ( coordinate[0] - 'R' ) - glyphStartX; - point.y = (double) ( coordinate[1] - 'R' ) - 11.0; + point.x = (double) ( coordinate[0] - 'R' ) * HERSHEY_SCALE - glyphStartX; + point.y = (double) ( coordinate[1] - 'R' ) * HERSHEY_SCALE; pointList.push_back( point ); } @@ -108,16 +107,22 @@ bool STROKE_FONT::LoadNewStrokeFont( const char* const aNewStrokeFont[], int aNe if( pointList.size() > 0 ) glyph.push_back( pointList ); - m_glyphs.push_back( glyph ); + m_glyphs[j] = glyph; // Compute the bounding box of the glyph - m_glyphBoundingBoxes.push_back( computeBoundingBox( glyph, glyphBoundingX ) ); + m_glyphBoundingBoxes[j] = computeBoundingBox( glyph, glyphBoundingX ); } return true; } +int STROKE_FONT::getInterline() const +{ + return ( m_glyphSize.y * 14 ) / 10 + m_gal->GetLineWidth(); +} + + BOX2D STROKE_FONT::computeBoundingBox( const GLYPH& aGLYPH, const VECTOR2D& aGLYPHBoundingX ) const { BOX2D boundingBox; @@ -142,133 +147,160 @@ BOX2D STROKE_FONT::computeBoundingBox( const GLYPH& aGLYPH, const VECTOR2D& aGLY } -void STROKE_FONT::Draw( std::string aText, const VECTOR2D& aPosition, double aRotationAngle ) +void STROKE_FONT::Draw( wxString aText, const VECTOR2D& aPosition, double aRotationAngle ) { - // By default overbar is turned off - m_overbar = false; - // Context needs to be saved before any transformations m_gal->Save(); m_gal->Translate( aPosition ); - m_gal->Rotate( -aRotationAngle ); - // Split multiline strings into separate ones and draw them line by line - size_t newlinePos = aText.find( '\n' ); + // Single line height + int lineHeight = getInterline(); - if( newlinePos != std::string::npos ) - { - VECTOR2D nextlinePosition = VECTOR2D( 0.0, m_glyphSize.y * LINE_HEIGHT_RATIO ); - - Draw( aText.substr( newlinePos + 1 ), nextlinePosition, 0.0 ); - aText = aText.substr( 0, newlinePos ); - } - - // Compute the text size - VECTOR2D textsize = computeTextSize( aText ); - - // Adjust the text position to the given alignment - switch( m_horizontalJustify ) - { - case GR_TEXT_HJUSTIFY_CENTER: - m_gal->Translate( VECTOR2D( -textsize.x / 2.0, 0 ) ); - break; - - case GR_TEXT_HJUSTIFY_RIGHT: - if( !m_mirrored ) - m_gal->Translate( VECTOR2D( -textsize.x, 0 ) ); - - break; - - case GR_TEXT_HJUSTIFY_LEFT: - if( m_mirrored ) - m_gal->Translate( VECTOR2D( -textsize.x, 0 ) ); - - break; - - default: - break; - } + // The overall height of all lines of text + double textBlockHeight = lineHeight * ( linesCount( aText ) - 1 ); switch( m_verticalJustify ) { case GR_TEXT_VJUSTIFY_CENTER: - m_gal->Translate( VECTOR2D( 0, textsize.y / 2.0 ) ); - break; - - case GR_TEXT_VJUSTIFY_TOP: - m_gal->Translate( VECTOR2D( 0, textsize.y ) ); + m_gal->Translate( VECTOR2D( 0, -textBlockHeight / 2.0 ) ); break; case GR_TEXT_VJUSTIFY_BOTTOM: + m_gal->Translate( VECTOR2D( 0, -textBlockHeight ) ); + break; + + case GR_TEXT_VJUSTIFY_TOP: break; default: break; } - double xOffset, glyphSizeX; - - if( m_mirrored ) - { - // In case of mirrored text invert the X scale of points and their X direction - // (m_glyphSize.x) and start drawing from the position where text normally should end - // (textsize.x) - xOffset = textsize.x; - glyphSizeX = -m_glyphSize.x; - } - else - { - xOffset = 0.0; - glyphSizeX = m_glyphSize.x; - } - double scaleY = m_scaleFactor * m_glyphSize.y; - double scaleX = m_scaleFactor * glyphSizeX; + m_gal->Rotate( -aRotationAngle ); m_gal->SetIsStroke( true ); m_gal->SetIsFill( false ); if( m_bold ) + m_gal->SetLineWidth( m_gal->GetLineWidth() * BOLD_FACTOR ); + + // Split multiline strings into separate ones and draw them line by line + int begin = 0; + int newlinePos = aText.Find( '\n' ); + + while( newlinePos != wxNOT_FOUND ) { - m_gal->SetLineWidth( m_gal->GetLineWidth() * 1.3 ); + size_t length = newlinePos - begin; + drawSingleLineText( aText.Mid( begin, length ) ); + m_gal->Translate( VECTOR2D( 0.0, lineHeight ) ); + + begin = newlinePos + 1; + newlinePos = aText.find( '\n', begin + 1 ); } - for( std::string::const_iterator chIt = aText.begin(); chIt != aText.end(); chIt++ ) + // Draw the last (or the only one) line + if( !aText.IsEmpty() ) + drawSingleLineText( aText.Mid( begin ) ); + + m_gal->Restore(); +} + + +void STROKE_FONT::drawSingleLineText( const wxString& aText ) +{ + // By default the overbar is turned off + m_overbar = false; + + double xOffset; + VECTOR2D glyphSize( m_glyphSize ); + + // Compute the text size + VECTOR2D textSize = computeTextSize( aText ); + + m_gal->Save(); + + // Adjust the text position to the given alignment + switch( m_horizontalJustify ) { + case GR_TEXT_HJUSTIFY_CENTER: + m_gal->Translate( VECTOR2D( -textSize.x / 2.0, 0 ) ); + break; + + case GR_TEXT_HJUSTIFY_RIGHT: + if( !m_mirrored ) + m_gal->Translate( VECTOR2D( -textSize.x, 0 ) ); + break; + + case GR_TEXT_HJUSTIFY_LEFT: + if( m_mirrored ) + m_gal->Translate( VECTOR2D( -textSize.x, 0 ) ); + break; + + default: + break; + } + + if( m_mirrored ) + { + // In case of mirrored text invert the X scale of points and their X direction + // (m_glyphSize.x) and start drawing from the position where text normally should end + // (textSize.x) + xOffset = textSize.x; + glyphSize.x = -m_glyphSize.x; + } else + { + xOffset = 0.0; + } + + for( wxString::const_iterator chIt = aText.begin(); chIt != aText.end(); ++chIt ) + { + // Toggle overbar if( *chIt == '~' ) { - m_overbar = !m_overbar; - continue; + if( ++chIt == aText.end() ) + break; + + if( *chIt != '~' ) // It was a single tilda, it toggles overbar + m_overbar = !m_overbar; + + // If it is a double tilda, just process the second one } - GLYPH_LIST::iterator glyphIt = m_glyphs.begin(); - std::deque::iterator bbIt = m_glyphBoundingBoxes.begin(); + unsigned dd = *chIt - ' '; - unsigned dd = (unsigned) ((unsigned char) *chIt ) - (unsigned) ' '; - - if( dd >= m_glyphBoundingBoxes.size() ) + if( dd >= m_glyphBoundingBoxes.size() || dd < 0 ) dd = '?' - ' '; - advance( glyphIt, dd ); - advance( bbIt, dd ); + GLYPH& glyph = m_glyphs[dd]; + BOX2D& bbox = m_glyphBoundingBoxes[dd]; - GLYPH glyph = *glyphIt; + if( m_overbar ) + { + VECTOR2D startOverbar( xOffset, -getInterline() * OVERBAR_HEIGHT ); + VECTOR2D endOverbar( xOffset + glyphSize.x * bbox.GetEnd().x, + -getInterline() * OVERBAR_HEIGHT ); + m_gal->DrawLine( startOverbar, endOverbar ); + } for( GLYPH::iterator pointListIt = glyph.begin(); pointListIt != glyph.end(); - pointListIt++ ) + ++pointListIt ) { std::deque pointListScaled; for( std::deque::iterator pointIt = pointListIt->begin(); - pointIt != pointListIt->end(); pointIt++ ) + pointIt != pointListIt->end(); ++pointIt ) { - VECTOR2D pointPos( pointIt->x * scaleX + xOffset, pointIt->y * scaleY ); + VECTOR2D pointPos( pointIt->x * glyphSize.x + xOffset, pointIt->y * glyphSize.y ); if( m_italic ) { // FIXME should be done other way - referring to the lowest Y value of point // because now italic fonts are translated a bit - pointPos.x += pointPos.y * 0.1; + if( m_mirrored ) + pointPos.x += pointPos.y * 0.1; + else + pointPos.x -= pointPos.y * 0.1; } pointListScaled.push_back( pointPos ); @@ -277,39 +309,37 @@ void STROKE_FONT::Draw( std::string aText, const VECTOR2D& aPosition, double aRo m_gal->DrawPolyline( pointListScaled ); } - 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; + xOffset += glyphSize.x * bbox.GetEnd().x; } m_gal->Restore(); } -VECTOR2D STROKE_FONT::computeTextSize( const std::string& aText ) const +VECTOR2D STROKE_FONT::computeTextSize( const wxString& aText ) const { VECTOR2D result = VECTOR2D( 0.0, m_glyphSize.y ); - for( std::string::const_iterator chIt = aText.begin(); chIt != aText.end(); chIt++ ) + for( wxString::const_iterator chIt = aText.begin(); chIt != aText.end(); ++chIt ) { + wxASSERT_MSG( *chIt != '\n', + wxT( "This function is intended to work with single line strings" ) ); + + // If it is double tilda, then it is displayed as a single tilda + // If it is single tilda, then it is toggling overbar, so we need to skip it if( *chIt == '~' ) - continue; + { + if( ++chIt == aText.end() ) + break; + } - std::deque::const_iterator bbIt = m_glyphBoundingBoxes.begin(); - unsigned dd = (unsigned) ((unsigned char)*chIt) - (unsigned) ' '; + // Index in the bounding boxes table + unsigned dd = *chIt - ' '; - if( dd >= m_glyphBoundingBoxes.size() ) + if( dd >= m_glyphBoundingBoxes.size() || dd < 0 ) dd = '?' - ' '; - advance( bbIt, dd ); - - result.x += m_scaleFactor * m_glyphSize.x * bbIt->GetEnd().x; + result.x += m_glyphSize.x * m_glyphBoundingBoxes[dd].GetEnd().x; } return result; diff --git a/common/worksheet_viewitem.cpp b/common/worksheet_viewitem.cpp index 5ad38bc46a..7ecf6de8d7 100644 --- a/common/worksheet_viewitem.cpp +++ b/common/worksheet_viewitem.cpp @@ -191,7 +191,7 @@ void WORKSHEET_VIEWITEM::draw( const WS_DRAW_ITEM_TEXT* aItem, GAL* aGal ) const aGal->SetStrokeColor( COLOR4D( aItem->GetColor() ) ); aGal->SetLineWidth( aItem->GetThickness() ); aGal->SetTextAttributes( aItem ); - aGal->StrokeText( std::string( aItem->GetText().mb_str() ), position, 0.0 ); + aGal->StrokeText( std::wstring( aItem->GetText().wc_str() ), position, 0.0 ); } diff --git a/include/gal/graphics_abstraction_layer.h b/include/gal/graphics_abstraction_layer.h index f374979531..7745dfa172 100644 --- a/include/gal/graphics_abstraction_layer.h +++ b/include/gal/graphics_abstraction_layer.h @@ -277,7 +277,7 @@ public: * @param aPosition is the text position in world coordinates. * @param aRotationAngle is the text rotation angle. */ - inline virtual void StrokeText( const std::string& aText, const VECTOR2D& aPosition, + inline virtual void StrokeText( const wxString& aText, const VECTOR2D& aPosition, double aRotationAngle ) { strokeFont.Draw( aText, aPosition, aRotationAngle ); diff --git a/include/gal/stroke_font.h b/include/gal/stroke_font.h index 50882ee142..a3619783f2 100644 --- a/include/gal/stroke_font.h +++ b/include/gal/stroke_font.h @@ -3,6 +3,8 @@ * * Copyright (C) 2012 Torsten Hueter, torstenhtr gmx.de * Copyright (C) 2012 Kicad Developers, see change_log.txt for contributors. + * Copyright (C) 2013 CERN + * @author Maciej Suminski * * Stroke font class * @@ -39,7 +41,7 @@ namespace KIGFX class GAL; typedef std::deque< std::deque > GLYPH; -typedef std::deque GLYPH_LIST; +typedef std::vector GLYPH_LIST; /** * @brief Class STROKE_FONT implements stroke font drawing. @@ -52,11 +54,6 @@ public: /// Constructor STROKE_FONT( GAL* aGal ); - /// Destructor - ~STROKE_FONT(); - - // TODO Load font from a text file - /** * @brief Load the new stroke font. * @@ -73,17 +70,7 @@ public: * @param aPosition is the text position in world coordinates. * @param aRotationAngle is the text rotation angle. */ - void Draw( std::string aText, const VECTOR2D& aPosition, double aRotationAngle ); - - /** - * @brief Set the scale factor of the font for the glyph size. - * - * @param aScaleFactor is the scale factor of the font. - */ - inline void SetScaleFactor( const double aScaleFactor ) - { - m_scaleFactor = aScaleFactor; - } + void Draw( wxString aText, const VECTOR2D& aPosition, double aRotationAngle ); /** * @brief Set the glyph size. @@ -158,13 +145,19 @@ public: private: GAL* m_gal; ///< Pointer to the GAL GLYPH_LIST m_glyphs; ///< Glyph list - std::deque m_glyphBoundingBoxes; ///< Bounding boxes of the glyphs - double m_scaleFactor; ///< Scale factor for the glyph + std::vector m_glyphBoundingBoxes; ///< Bounding boxes of the glyphs 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 Returns a single line height using current settings. + * + * @return The line height. + */ + int getInterline() const; + /** * @brief Compute the bounding box of a given glyph. * @@ -174,15 +167,50 @@ private: */ BOX2D computeBoundingBox( const GLYPH& aGlyph, const VECTOR2D& aGlyphBoundingX ) const; + /** + * @brief Draws a single line of text. Multiline texts should be split before using the + * function. + * + * @param aText is the text to be drawn. + */ + void drawSingleLineText( const wxString& aText ); + /** * @brief Compute the size of a given text. * * @param aText is the text string. * @return is the text size. */ - VECTOR2D computeTextSize( const std::string& aText ) const; + VECTOR2D computeTextSize( const wxString& aText ) const; - static const double LINE_HEIGHT_RATIO; + /** + * @brief Returns number of lines for a given text. + * + * @param aText is the text to be checked. + * @return Number of lines of aText. + */ + unsigned int linesCount( const wxString& aText ) const + { + wxString::const_iterator it, itEnd; + unsigned int lines = 1; + + for( it = aText.begin(), itEnd = aText.end(); it != itEnd; ++it ) + { + if( *it == '\n' ) + ++lines; + } + + return lines; + } + + ///> Factor that determines relative height of overbar. + static const double OVERBAR_HEIGHT; + + ///> Factor that determines relative line width for bold text. + static const double BOLD_FACTOR; + + ///> Scale factor for the glyph + static const double HERSHEY_SCALE; }; } // namespace KIGFX diff --git a/pcbnew/pcb_painter.cpp b/pcbnew/pcb_painter.cpp index 692f73f5e7..902c21f991 100644 --- a/pcbnew/pcb_painter.cpp +++ b/pcbnew/pcb_painter.cpp @@ -280,7 +280,7 @@ void PCB_PAINTER::draw( const TRACK* aTrack, int aLayer ) if( !net ) return; - std::string netName = std::string( net->GetShortNetname().mb_str() ); + std::wstring netName = std::wstring( net->GetShortNetname().wc_str() ); VECTOR2D textPosition = start + line / 2.0; // center of the track double textOrientation = -atan( line.y / line.x ); double textSize = std::min( static_cast( width ), length / netName.length() ); @@ -456,7 +456,7 @@ void PCB_PAINTER::draw( const D_PAD* aPad, int aLayer ) VECTOR2D namesize( tsize, tsize ); m_gal->SetGlyphSize( namesize ); m_gal->SetLineWidth( namesize.x / 12.0 ); - m_gal->StrokeText( std::string( aPad->GetShortNetname().mb_str() ), + m_gal->StrokeText( std::wstring( aPad->GetShortNetname().wc_str() ), textpos, 0.0 ); } @@ -474,8 +474,7 @@ void PCB_PAINTER::draw( const D_PAD* aPad, int aLayer ) m_gal->SetGlyphSize( numsize ); m_gal->SetLineWidth( numsize.x / 12.0 ); - m_gal->StrokeText( std::string( aPad->GetPadName().mb_str() ), - textpos, 0.0 ); + m_gal->StrokeText( std::wstring( aPad->GetPadName().wc_str() ), textpos, 0.0 ); } m_gal->Restore(); @@ -720,7 +719,7 @@ void PCB_PAINTER::draw( const TEXTE_PCB* aText, int aLayer ) m_gal->SetStrokeColor( strokeColor ); m_gal->SetLineWidth( aText->GetThickness() ); m_gal->SetTextAttributes( aText ); - m_gal->StrokeText( std::string( aText->GetText().mb_str() ), position, orientation ); + m_gal->StrokeText( aText->GetText(), position, orientation ); } @@ -736,7 +735,7 @@ void PCB_PAINTER::draw( const TEXTE_MODULE* aText, int aLayer ) m_gal->SetStrokeColor( strokeColor ); m_gal->SetLineWidth( aText->GetThickness() ); m_gal->SetTextAttributes( aText ); - m_gal->StrokeText( std::string( aText->GetText().mb_str() ), position, orientation ); + m_gal->StrokeText( aText->GetText(), position, orientation ); } @@ -836,7 +835,7 @@ void PCB_PAINTER::draw( const DIMENSION* aDimension, int aLayer ) m_gal->SetLineWidth( text.GetThickness() ); m_gal->SetTextAttributes( &text ); - m_gal->StrokeText( std::string( text.GetText().mb_str() ), position, orientation ); + m_gal->StrokeText( std::wstring( text.GetText().wc_str() ), position, orientation ); }