kicad/common/eda_text.cpp

811 lines
26 KiB
C++
Raw Normal View History

/*
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright (C) 2016 Jean-Pierre Charras, jp.charras at wanadoo.fr
* Copyright (C) 2004-2021 KiCad Developers, see change_log.txt for contributors.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program 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.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, you may find one here:
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
* or you may search the http://www.gnu.org website for the version 2 license,
* or you may write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
/**
* @file eda_text.cpp
* @brief Implementation of base KiCad text object.
*/
#include <algorithm> // for max
#include <stddef.h> // for NULL
#include <type_traits> // for swap
#include <vector> // for vector
#include <eda_item.h> // for EDA_ITEM
#include <base_units.h>
#include <basic_gal.h> // for BASIC_GAL, basic_gal
#include <convert_to_biu.h> // for Mils2iu
#include <convert_basic_shapes_to_polygon.h>
#include <eda_rect.h> // for EDA_RECT
#include <eda_text.h> // for EDA_TEXT, TEXT_EFFECTS, GR_TEXT_VJUSTIF...
#include <gal/color4d.h> // for COLOR4D, COLOR4D::BLACK
#include <gr_text.h> // for GRText
#include <string_utils.h> // for UnescapeString
#include <math/util.h> // for KiROUND
#include <math/vector2d.h> // for VECTOR2D
2020-10-24 12:44:03 +00:00
#include <richio.h>
2020-10-14 11:29:36 +00:00
#include <render_settings.h>
#include <trigo.h> // for RotatePoint
#include <i18n_utility.h>
#include <geometry/shape_segment.h>
#include <geometry/shape_compound.h>
#include <geometry/shape_simple.h>
#include <font/outline_font.h>
#include <geometry/shape_poly_set.h>
2020-11-23 10:43:22 +00:00
#include <wx/debug.h> // for wxASSERT
#include <wx/string.h> // wxString, wxArrayString
#include <wx/gdicmn.h> // for wxPoint,wxSize
class OUTPUTFORMATTER;
class wxFindReplaceData;
void addTextSegmToPoly( int x0, int y0, int xf, int yf, void* aData )
{
TSEGM_2_POLY_PRMS* prm = static_cast<TSEGM_2_POLY_PRMS*>( aData );
TransformOvalToPolygon( *prm->m_cornerBuffer, VECTOR2I( x0, y0 ), VECTOR2I( xf, yf ),
prm->m_textWidth, prm->m_error, ERROR_INSIDE );
}
GR_TEXT_H_ALIGN_T EDA_TEXT::MapHorizJustify( int aHorizJustify )
{
wxASSERT( aHorizJustify >= GR_TEXT_H_ALIGN_LEFT && aHorizJustify <= GR_TEXT_H_ALIGN_RIGHT );
if( aHorizJustify > GR_TEXT_H_ALIGN_RIGHT )
return GR_TEXT_H_ALIGN_RIGHT;
if( aHorizJustify < GR_TEXT_H_ALIGN_LEFT )
return GR_TEXT_H_ALIGN_LEFT;
return static_cast<GR_TEXT_H_ALIGN_T>( aHorizJustify );
}
GR_TEXT_V_ALIGN_T EDA_TEXT::MapVertJustify( int aVertJustify )
{
wxASSERT( aVertJustify >= GR_TEXT_V_ALIGN_TOP && aVertJustify <= GR_TEXT_V_ALIGN_BOTTOM );
if( aVertJustify > GR_TEXT_V_ALIGN_BOTTOM )
return GR_TEXT_V_ALIGN_BOTTOM;
if( aVertJustify < GR_TEXT_V_ALIGN_TOP )
return GR_TEXT_V_ALIGN_TOP;
return static_cast<GR_TEXT_V_ALIGN_T>( aVertJustify );
}
EDA_TEXT::EDA_TEXT( const wxString& text ) :
m_text( text )
{
int sz = Mils2iu( DEFAULT_SIZE_TEXT );
SetTextSize( wxSize( sz, sz ) );
cacheShownText();
}
2019-05-08 18:56:03 +00:00
EDA_TEXT::EDA_TEXT( const EDA_TEXT& aText ) :
m_text( aText.m_text ),
m_attributes( aText.m_attributes ),
m_pos( aText.m_pos )
2019-05-08 18:56:03 +00:00
{
cacheShownText();
2019-05-08 18:56:03 +00:00
}
EDA_TEXT::~EDA_TEXT()
{
}
void EDA_TEXT::SetText( const wxString& aText )
{
m_text = aText;
cacheShownText();
}
void EDA_TEXT::CopyText( const EDA_TEXT& aSrc )
{
m_text = aSrc.m_text;
m_shown_text = aSrc.m_shown_text;
2020-04-24 20:33:59 +00:00
m_shown_text_has_text_var_refs = aSrc.m_shown_text_has_text_var_refs;
}
void EDA_TEXT::SetAttributes( const EDA_TEXT& aSrc )
{
m_attributes = aSrc.m_attributes;
m_pos = aSrc.m_pos;
}
void EDA_TEXT::SwapText( EDA_TEXT& aTradingPartner )
{
std::swap( m_text, aTradingPartner.m_text );
std::swap( m_shown_text, aTradingPartner.m_shown_text );
2020-04-24 20:33:59 +00:00
std::swap( m_shown_text_has_text_var_refs, aTradingPartner.m_shown_text_has_text_var_refs );
}
void EDA_TEXT::SwapAttributes( EDA_TEXT& aTradingPartner )
{
std::swap( m_attributes, aTradingPartner.m_attributes );
std::swap( m_pos, aTradingPartner.m_pos );
}
int EDA_TEXT::GetEffectiveTextPenWidth( int aDefaultWidth ) const
{
2020-04-14 12:25:00 +00:00
int width = GetTextThickness();
if( width <= 1 )
2020-04-14 12:25:00 +00:00
{
width = aDefaultWidth;
2020-04-14 12:25:00 +00:00
if( IsBold() )
width = GetPenSizeForBold( GetTextWidth() );
else if( width <= 1 )
width = GetPenSizeForNormal( GetTextWidth() );
2020-04-14 12:25:00 +00:00
}
// Clip pen size for small texts:
width = Clamp_Text_PenSize( width, GetTextSize(), ALLOW_BOLD_THICKNESS );
return width;
}
2020-12-20 18:27:51 +00:00
bool EDA_TEXT::Replace( const wxFindReplaceData& aSearchData )
{
bool retval = EDA_ITEM::Replace( aSearchData, m_text );
cacheShownText();
return retval;
}
void EDA_TEXT::cacheShownText()
{
if( m_text.IsEmpty() || m_text == wxT( "~" ) ) // ~ is legacy empty-string token
{
m_shown_text = wxEmptyString;
m_shown_text_has_text_var_refs = false;
}
else
{
m_shown_text = UnescapeString( m_text );
m_shown_text_has_text_var_refs = m_shown_text.Contains( wxT( "${" ) );
}
}
wxString EDA_TEXT::ShortenedShownText() const
{
wxString tmp = GetShownText();
tmp.Replace( wxT( "\n" ), wxT( " " ) );
tmp.Replace( wxT( "\r" ), wxT( " " ) );
tmp.Replace( wxT( "\t" ), wxT( " " ) );
if( tmp.Length() > 36 )
tmp = tmp.Left( 34 ) + wxT( "..." );
return tmp;
}
int EDA_TEXT::GetInterline() const
{
2021-12-31 14:07:24 +00:00
KIFONT::FONT* font = GetFont();
if( !font )
font = KIFONT::FONT::GetFont( wxEmptyString, m_attributes.m_Bold, m_attributes.m_Italic );
return KiROUND( font->GetInterline( GetTextHeight() ) );
}
2020-04-14 12:25:00 +00:00
EDA_RECT EDA_TEXT::GetTextBox( int aLine, bool aInvertY ) const
{
EDA_RECT rect;
wxArrayString strings;
wxString text = GetShownText();
2020-04-14 12:25:00 +00:00
int thickness = GetEffectiveTextPenWidth();
int linecount = 1;
bool hasOverBar = false; // true if the first line of text as an overbar
if( IsMultilineAllowed() )
{
wxStringSplit( text, strings, '\n' );
if( strings.GetCount() ) // GetCount() == 0 for void strings
{
if( aLine >= 0 && ( aLine < static_cast<int>( strings.GetCount() ) ) )
text = strings.Item( aLine );
else
text = strings.Item( 0 );
linecount = strings.GetCount();
}
}
// Search for overbar symbol. Only text is scanned,
// because only this line can change the bounding box
for( unsigned ii = 1; ii < text.size(); ii++ )
{
if( text[ii-1] == '~' && text[ii] == '{' )
{
hasOverBar = true;
break;
}
}
// calculate the H and V size
2021-12-31 14:07:24 +00:00
KIFONT::FONT* font = KIFONT::FONT::GetFont();
VECTOR2D fontSize( GetTextSize() );
double penWidth( thickness );
int dx = KiROUND( font->StringBoundaryLimits( text, fontSize, penWidth ).x );
int dy = GetInterline();
2020-04-14 12:25:00 +00:00
// Creates bounding box (rectangle) for horizontal, left and top justified text. The
// bounding box will be moved later according to the actual text options
2021-12-31 14:07:24 +00:00
wxSize textsize = wxSize( dx, fontSize.y );
VECTOR2I pos = GetTextPos();
if( IsMultilineAllowed() && aLine > 0 && ( aLine < static_cast<int>( strings.GetCount() ) ) )
pos.y -= aLine * GetInterline();
if( aInvertY )
pos.y = -pos.y;
rect.SetOrigin( pos );
if( hasOverBar )
{ // A overbar adds an extra size to the text
// Height from the base line text of chars like [ or {
double curr_height = GetTextHeight() * 1.15;
2021-12-31 14:07:24 +00:00
double overbarPosition = font->ComputeOverbarVerticalPosition( fontSize.y );
2020-04-14 12:25:00 +00:00
int extra_height = KiROUND( overbarPosition - curr_height );
extra_height += thickness / 2;
textsize.y += extra_height;
rect.Move( VECTOR2I( 0, -extra_height ) );
}
// for multiline texts and aLine < 0, merge all rectangles
// ( if aLine < 0, we want the full text bounding box )
if( IsMultilineAllowed() && aLine < 0 )
{
for( unsigned ii = 1; ii < strings.GetCount(); ii++ )
{
text = strings.Item( ii );
2021-12-31 14:07:24 +00:00
dx = KiROUND( font->StringBoundaryLimits( text, fontSize, penWidth ).x );
textsize.x = std::max( textsize.x, dx );
textsize.y += dy;
}
}
rect.SetSize( textsize );
/* Now, calculate the rect origin, according to text justification
* At this point the rectangle origin is the text origin (m_Pos).
* This is true only for left and top text justified texts (using top to bottom Y axis
* orientation). and must be recalculated for others justifications
* also, note the V justification is relative to the first line
*/
switch( GetHorizJustify() )
{
case GR_TEXT_H_ALIGN_LEFT:
if( IsMirrored() )
rect.SetX( rect.GetX() - rect.GetWidth() );
break;
case GR_TEXT_H_ALIGN_CENTER:
2021-12-31 14:07:24 +00:00
rect.SetX( rect.GetX() - rect.GetWidth() / 2 );
break;
case GR_TEXT_H_ALIGN_RIGHT:
if( !IsMirrored() )
rect.SetX( rect.GetX() - rect.GetWidth() );
break;
}
switch( GetVertJustify() )
{
2021-12-31 14:07:24 +00:00
case GR_TEXT_V_ALIGN_TOP: break;
case GR_TEXT_V_ALIGN_CENTER: rect.SetY( rect.GetY() - rect.GetHeight() / 2 ); break;
case GR_TEXT_V_ALIGN_BOTTOM: rect.SetY( rect.GetY() - rect.GetHeight() ); break;
}
// Many fonts draw diacriticals, descenders, etc. outside the X-height of the font. This
// will cacth most (but probably not all) of them.
rect.Inflate( 0, thickness * 1.5 );
rect.Normalize(); // Make h and v sizes always >= 0
return rect;
}
bool EDA_TEXT::TextHitTest( const VECTOR2I& aPoint, int aAccuracy ) const
{
2020-04-14 12:25:00 +00:00
EDA_RECT rect = GetTextBox();
VECTOR2I location = aPoint;
rect.Inflate( aAccuracy );
RotatePoint( location, GetTextPos(), -GetTextAngle() );
return rect.Contains( location );
}
bool EDA_TEXT::TextHitTest( const EDA_RECT& aRect, bool aContains, int aAccuracy ) const
{
EDA_RECT rect = aRect;
rect.Inflate( aAccuracy );
if( aContains )
2020-04-14 12:25:00 +00:00
return rect.Contains( GetTextBox() );
return rect.Intersects( GetTextBox(), GetTextAngle().AsTenthsOfADegree() );
}
void EDA_TEXT::Print( const RENDER_SETTINGS* aSettings, const VECTOR2I& aOffset,
const COLOR4D& aColor, OUTLINE_MODE aFillMode )
{
if( IsMultilineAllowed() )
{
std::vector<VECTOR2I> positions;
wxArrayString strings;
wxStringSplit( GetShownText(), strings, '\n' );
positions.reserve( strings.Count() );
GetLinePositions( positions, strings.Count() );
for( unsigned ii = 0; ii < strings.Count(); ii++ )
2020-04-14 12:25:00 +00:00
printOneLineOfText( aSettings, aOffset, aColor, aFillMode, strings[ii], positions[ii] );
}
else
2020-04-14 12:25:00 +00:00
{
printOneLineOfText( aSettings, aOffset, aColor, aFillMode, GetShownText(), GetTextPos() );
}
}
void EDA_TEXT::GetLinePositions( std::vector<VECTOR2I>& aPositions, int aLineCount ) const
{
VECTOR2I pos = GetTextPos(); // Position of first line of the multiline text according
// to the center of the multiline text block
VECTOR2I offset; // Offset to next line.
offset.y = GetInterline();
if( aLineCount > 1 )
{
switch( GetVertJustify() )
{
case GR_TEXT_V_ALIGN_TOP:
break;
case GR_TEXT_V_ALIGN_CENTER:
pos.y -= ( aLineCount - 1 ) * offset.y / 2;
break;
case GR_TEXT_V_ALIGN_BOTTOM:
pos.y -= ( aLineCount - 1 ) * offset.y;
break;
}
}
// Rotate the position of the first line around the center of the multiline text block
RotatePoint( pos, GetTextPos(), GetTextAngle() );
// Rotate the offset lines to increase happened in the right direction
RotatePoint( offset, GetTextAngle() );
for( int ii = 0; ii < aLineCount; ii++ )
{
aPositions.push_back( (VECTOR2I) pos );
pos += offset;
}
}
void EDA_TEXT::printOneLineOfText( const RENDER_SETTINGS* aSettings, const VECTOR2I& aOffset,
const COLOR4D& aColor, OUTLINE_MODE aFillMode,
const wxString& aText, const VECTOR2I& aPos )
{
2020-04-14 12:25:00 +00:00
wxDC* DC = aSettings->GetPrintDC();
int penWidth = std::max( GetEffectiveTextPenWidth(), aSettings->GetDefaultPenWidth() );
if( aFillMode == SKETCH )
2020-04-14 12:25:00 +00:00
penWidth = -penWidth;
VECTOR2I size = GetTextSize();
if( IsMirrored() )
size.x = -size.x;
2020-04-14 12:25:00 +00:00
GRText( DC, aOffset + aPos, aColor, aText, GetTextAngle(), size, GetHorizJustify(),
GetVertJustify(), penWidth, IsItalic(), IsBold(), GetFont() );
}
2020-10-27 11:03:35 +00:00
wxString EDA_TEXT::GetTextStyleName() const
{
int style = 0;
if( IsItalic() )
style = 1;
if( IsBold() )
style += 2;
wxString stylemsg[4] = {
_("Normal"),
_("Italic"),
_("Bold"),
_("Bold+Italic")
};
return stylemsg[style];
}
wxString EDA_TEXT::GetFontName() const
{
if( GetFont() )
return GetFont()->Name();
else
return wxEmptyString;
}
bool EDA_TEXT::IsDefaultFormatting() const
{
2020-04-14 12:25:00 +00:00
return ( IsVisible()
&& !IsMirrored()
&& GetHorizJustify() == GR_TEXT_H_ALIGN_CENTER
&& GetVertJustify() == GR_TEXT_V_ALIGN_CENTER
2020-04-14 12:25:00 +00:00
&& GetTextThickness() == 0
&& !IsItalic()
&& !IsBold()
&& !IsMultilineAllowed()
&& GetFontName().IsEmpty()
);
}
void EDA_TEXT::Format( OUTPUTFORMATTER* aFormatter, int aNestLevel, int aControlBits ) const
{
#ifndef GERBVIEW // Gerbview does not use EDA_TEXT::Format
// and does not define FormatInternalUnits, used here
// however this function should exist
2020-12-20 18:50:45 +00:00
aFormatter->Print( aNestLevel + 1, "(effects" );
2020-12-20 18:50:45 +00:00
aFormatter->Print( 0, " (font" );
2022-01-03 01:17:42 +00:00
if( GetFont() && !GetFont()->Name().IsEmpty() )
aFormatter->Print( 0, " (face \"%s\")", GetFont()->NameAsToken() );
// Text size
2020-12-20 18:50:45 +00:00
aFormatter->Print( 0, " (size %s %s)",
FormatInternalUnits( GetTextHeight() ).c_str(),
FormatInternalUnits( GetTextWidth() ).c_str() );
2022-01-03 01:17:42 +00:00
if( GetLineSpacing() != 1.0 )
{
aFormatter->Print( 0, " (line_spacing %s)",
Double2Str( GetLineSpacing() ).c_str() );
}
2020-12-20 18:50:45 +00:00
if( GetTextThickness() )
2021-09-27 09:34:32 +00:00
{
aFormatter->Print( 0, " (thickness %s)",
FormatInternalUnits( GetTextThickness() ).c_str() );
2021-09-27 09:34:32 +00:00
}
2020-12-20 18:50:45 +00:00
if( IsBold() )
aFormatter->Print( 0, " bold" );
2020-12-20 18:50:45 +00:00
if( IsItalic() )
aFormatter->Print( 0, " italic" );
2020-12-20 18:50:45 +00:00
aFormatter->Print( 0, ")"); // (font
if( IsMirrored() || GetHorizJustify() != GR_TEXT_H_ALIGN_CENTER
|| GetVertJustify() != GR_TEXT_V_ALIGN_CENTER )
2020-12-20 18:50:45 +00:00
{
aFormatter->Print( 0, " (justify");
if( GetHorizJustify() != GR_TEXT_H_ALIGN_CENTER )
aFormatter->Print( 0, GetHorizJustify() == GR_TEXT_H_ALIGN_LEFT ? " left" : " right" );
if( GetVertJustify() != GR_TEXT_V_ALIGN_CENTER )
aFormatter->Print( 0, GetVertJustify() == GR_TEXT_V_ALIGN_TOP ? " top" : " bottom" );
2020-12-20 18:50:45 +00:00
if( IsMirrored() )
aFormatter->Print( 0, " mirror" );
2020-12-20 18:50:45 +00:00
aFormatter->Print( 0, ")" ); // (justify
}
2020-12-20 18:50:45 +00:00
if( !(aControlBits & CTL_OMIT_HIDE) && !IsVisible() )
aFormatter->Print( 0, " hide" );
2020-12-20 18:50:45 +00:00
aFormatter->Print( 0, ")\n" ); // (justify
#endif
}
// Convert the text shape to a list of segment
// each segment is stored as 2 VECTOR2Is: its starting point and its ending point
// we are using GRText to create the segments and therefore a call-back function is needed
// This is a call back function, used by GRText to put each segment in buffer
static void addTextSegmToBuffer( int x0, int y0, int xf, int yf, void* aData )
{
std::vector<VECTOR2I>* cornerBuffer = static_cast<std::vector<VECTOR2I>*>( aData );
cornerBuffer->push_back( VECTOR2I( x0, y0 ) );
cornerBuffer->push_back( VECTOR2I( xf, yf ) );
}
std::vector<VECTOR2I> EDA_TEXT::TransformToSegmentList() const
{
std::vector<VECTOR2I> cornerBuffer;
wxSize size = GetTextSize();
if( IsMirrored() )
size.x = -size.x;
bool forceBold = true;
int penWidth = 0; // use max-width for bold text
COLOR4D color = COLOR4D::BLACK; // not actually used, but needed by GRText
if( IsMultilineAllowed() )
{
wxArrayString strings_list;
wxStringSplit( GetShownText(), strings_list, wxChar('\n') );
std::vector<VECTOR2I> positions;
positions.reserve( strings_list.Count() );
GetLinePositions( positions, strings_list.Count() );
for( unsigned ii = 0; ii < strings_list.Count(); ii++ )
{
wxString txt = strings_list.Item( ii );
GRText( nullptr, positions[ii], color, txt, GetDrawRotation(), size,
GetDrawHorizJustify(), GetDrawVertJustify(), penWidth, IsItalic(), forceBold,
GetFont(), addTextSegmToBuffer, &cornerBuffer );
}
}
else
{
GRText( nullptr, GetDrawPos(), color, GetShownText(), GetDrawRotation(), size,
GetDrawHorizJustify(), GetDrawVertJustify(), penWidth, IsItalic(), forceBold,
GetFont(), addTextSegmToBuffer, &cornerBuffer );
}
return cornerBuffer;
}
std::shared_ptr<SHAPE_COMPOUND> EDA_TEXT::GetEffectiveTextShape( ) const
{
std::shared_ptr<SHAPE_COMPOUND> shape = std::make_shared<SHAPE_COMPOUND>();
if( GetFont() && GetFont()->IsOutline() )
{
// FONT TODO: Use the cached glyphs rather than rendering them
KIFONT::OUTLINE_FONT* font = static_cast<KIFONT::OUTLINE_FONT*>( GetFont() );
std::vector<std::unique_ptr<KIFONT::GLYPH>> glyphs;
font->GetLinesAsGlyphs( glyphs, this );
for( std::unique_ptr<KIFONT::GLYPH>& baseGlyph : glyphs )
{
KIFONT::OUTLINE_GLYPH* glyph = static_cast<KIFONT::OUTLINE_GLYPH*>( baseGlyph.get() );
if( IsMirrored() )
glyph->Mirror( GetTextPos() );
glyph->CacheTriangulation();
for( unsigned int ii = 0; ii < glyph->TriangulatedPolyCount(); ++ii )
{
const SHAPE_POLY_SET::TRIANGULATED_POLYGON* tri = glyph->TriangulatedPolygon( ii );
for( size_t jj = 0; jj < tri->GetTriangleCount(); ++jj )
{
VECTOR2I a, b, c;
tri->GetTriangle( jj, a, b, c );
SHAPE_SIMPLE* triShape = new SHAPE_SIMPLE;
triShape->Append( a );
triShape->Append( b );
triShape->Append( c );
shape->AddShape( triShape );
}
}
}
}
else
{
int penWidth = GetEffectiveTextPenWidth();
std::vector<VECTOR2I> pts = TransformToSegmentList();
for( unsigned jj = 0; jj < pts.size(); jj += 2 )
shape->AddShape( new SHAPE_SEGMENT( pts[jj], pts[jj+1], penWidth ) );
}
return shape;
}
int EDA_TEXT::Compare( const EDA_TEXT* aOther ) const
{
#define EPSILON 2 // Should be enough for rounding errors on calculated items
#define TEST( a, b ) { if( a != b ) return a - b; }
#define TEST_E( a, b ) { if( abs( a - b ) > EPSILON ) return a - b; }
#define TEST_PT( a, b ) { TEST_E( a.x, b.x ); TEST_E( a.y, b.y ); }
TEST_PT( m_pos, aOther->m_pos );
TEST_PT( m_attributes.m_Size, aOther->m_attributes.m_Size );
TEST_E( m_attributes.m_StrokeWidth, aOther->m_attributes.m_StrokeWidth );
TEST( m_attributes.m_Angle.AsTenthsOfADegree(), aOther->m_attributes.m_Angle.AsTenthsOfADegree() );
TEST( m_attributes.m_LineSpacing, aOther->m_attributes.m_LineSpacing );
TEST( m_attributes.m_Halign, aOther->m_attributes.m_Halign );
TEST( m_attributes.m_Valign, aOther->m_attributes.m_Valign );
TEST( m_attributes.m_Italic, aOther->m_attributes.m_Italic );
TEST( m_attributes.m_Bold, aOther->m_attributes.m_Bold );
TEST( m_attributes.m_Underlined, aOther->m_attributes.m_Underlined );
TEST( m_attributes.m_Visible, aOther->m_attributes.m_Visible );
TEST( m_attributes.m_Mirrored, aOther->m_attributes.m_Mirrored );
TEST( m_attributes.m_Multiline, aOther->m_attributes.m_Multiline );
TEST( m_attributes.m_KeepUpright, aOther->m_attributes.m_KeepUpright );
int val = GetFontName().Cmp( aOther->GetFontName() );
if( val != 0 )
return val;
return m_text.Cmp( aOther->m_text );
}
void EDA_TEXT::TransformBoundingBoxWithClearanceToPolygon( SHAPE_POLY_SET* aCornerBuffer,
int aClearanceValue ) const
{
if( GetText().Length() == 0 )
return;
VECTOR2I corners[4]; // Buffer of polygon corners
EDA_RECT rect = GetTextBox();
// This ugly hack is because this code used to be defined in the board polygon code
// file rather than in the EDA_TEXT source file where it belonged. Using the board
// default text width was dubious so this recreates the same code with the exception
// if for some reason a different default text width is require for some other object.
#if !defined( DEFAULT_TEXT_WIDTH )
#define LOCAL_DEFAULT_TEXT_WIDTH
#define DEFAULT_TEXT_WIDTH 0.15
#endif
rect.Inflate( aClearanceValue + Millimeter2iu( DEFAULT_TEXT_WIDTH ) );
#if defined( LOCAL_DEFAULT_TEXT_WIDTH )
#undef DEFAULT_TEXT_WIDTH
#undef LOCAL_DEFAULT_TEXT_WIDTH
#endif
corners[0].x = rect.GetOrigin().x;
corners[0].y = rect.GetOrigin().y;
corners[1].y = corners[0].y;
corners[1].x = rect.GetRight();
corners[2].x = corners[1].x;
corners[2].y = rect.GetBottom();
corners[3].y = corners[2].y;
corners[3].x = corners[0].x;
aCornerBuffer->NewOutline();
for( VECTOR2I& corner : corners )
{
// Rotate polygon
RotatePoint( corner, GetTextPos(), GetTextAngle() );
aCornerBuffer->Append( corner.x, corner.y );
}
}
static struct EDA_TEXT_DESC
{
EDA_TEXT_DESC()
{
ENUM_MAP<GR_TEXT_H_ALIGN_T>::Instance()
.Map( GR_TEXT_H_ALIGN_LEFT, _HKI( "Left" ) )
.Map( GR_TEXT_H_ALIGN_CENTER, _HKI( "Center" ) )
.Map( GR_TEXT_H_ALIGN_RIGHT, _HKI( "Right" ) );
ENUM_MAP<GR_TEXT_V_ALIGN_T>::Instance()
.Map( GR_TEXT_V_ALIGN_TOP, _HKI( "Top" ) )
.Map( GR_TEXT_V_ALIGN_CENTER, _HKI( "Center" ) )
.Map( GR_TEXT_V_ALIGN_BOTTOM, _HKI( "Bottom" ) );
PROPERTY_MANAGER& propMgr = PROPERTY_MANAGER::Instance();
REGISTER_TYPE( EDA_TEXT );
propMgr.AddProperty( new PROPERTY<EDA_TEXT, wxString>( _HKI( "Text" ),
2022-01-03 01:17:42 +00:00
&EDA_TEXT::SetText,
&EDA_TEXT::GetText ) );
propMgr.AddProperty( new PROPERTY<EDA_TEXT, int>( _HKI( "Thickness" ),
&EDA_TEXT::SetTextThickness,
&EDA_TEXT::GetTextThickness,
PROPERTY_DISPLAY::DISTANCE ) );
propMgr.AddProperty( new PROPERTY<EDA_TEXT, bool>( _HKI( "Italic" ),
&EDA_TEXT::SetItalic,
&EDA_TEXT::IsItalic ) );
propMgr.AddProperty( new PROPERTY<EDA_TEXT, bool>( _HKI( "Bold" ),
&EDA_TEXT::SetBold, &EDA_TEXT::IsBold ) );
propMgr.AddProperty( new PROPERTY<EDA_TEXT, bool>( _HKI( "Mirrored" ),
&EDA_TEXT::SetMirrored,
&EDA_TEXT::IsMirrored ) );
propMgr.AddProperty( new PROPERTY<EDA_TEXT, bool>( _HKI( "Visible" ),
&EDA_TEXT::SetVisible,
&EDA_TEXT::IsVisible ) );
propMgr.AddProperty( new PROPERTY<EDA_TEXT, int>( _HKI( "Width" ),
&EDA_TEXT::SetTextWidth,
&EDA_TEXT::GetTextWidth,
PROPERTY_DISPLAY::DISTANCE ) );
propMgr.AddProperty( new PROPERTY<EDA_TEXT, int>( _HKI( "Height" ),
&EDA_TEXT::SetTextHeight,
&EDA_TEXT::GetTextHeight,
PROPERTY_DISPLAY::DISTANCE ) );
propMgr.AddProperty( new PROPERTY_ENUM<EDA_TEXT,
GR_TEXT_H_ALIGN_T>( _HKI( "Horizontal Justification" ),
&EDA_TEXT::SetHorizJustify,
&EDA_TEXT::GetHorizJustify ) );
propMgr.AddProperty( new PROPERTY_ENUM<EDA_TEXT,
GR_TEXT_V_ALIGN_T>( _HKI( "Vertical Justification" ),
&EDA_TEXT::SetVertJustify,
&EDA_TEXT::GetVertJustify ) );
}
} _EDA_TEXT_DESC;
ENUM_TO_WXANY( GR_TEXT_H_ALIGN_T )
ENUM_TO_WXANY( GR_TEXT_V_ALIGN_T )