kicad/pcbnew/pcb_text.cpp

348 lines
10 KiB
C++
Raw Normal View History

/*
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright (C) 2012 Jean-Pierre Charras, jean-pierre.charras@ujf-grenoble.fr
* Copyright (C) 2012 SoftPLC Corporation, Dick Hollenbeck <dick@softplc.com>
* Copyright (C) 1992-2023 KiCad Developers, see AUTHORS.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
*/
2018-01-29 20:58:58 +00:00
#include <pcb_edit_frame.h>
#include <base_units.h>
#include <bitmaps.h>
#include <board.h>
#include <board_design_settings.h>
#include <core/mirror.h>
#include <footprint.h>
#include <pcb_text.h>
2020-06-27 18:01:00 +00:00
#include <pcb_painter.h>
2020-10-14 03:37:48 +00:00
#include <trigo.h>
#include <string_utils.h>
#include <geometry/shape_compound.h>
#include <callback_gal.h>
#include <convert_basic_shapes_to_polygon.h>
2020-04-14 12:25:00 +00:00
PCB_TEXT::PCB_TEXT( BOARD_ITEM* parent ) :
BOARD_ITEM( parent, PCB_TEXT_T ),
2022-09-16 04:38:10 +00:00
EDA_TEXT( pcbIUScale )
{
SetMultilineAllowed( true );
}
PCB_TEXT::~PCB_TEXT()
{
}
wxString PCB_TEXT::GetShownText( int aDepth, bool aAllowExtraText ) const
{
2020-09-12 14:23:07 +00:00
BOARD* board = dynamic_cast<BOARD*>( GetParent() );
std::function<bool( wxString* )> pcbTextResolver =
[&]( wxString* token ) -> bool
{
if( token->IsSameAs( wxT( "LAYER" ) ) )
{
*token = GetLayerName();
return true;
}
if( board->ResolveTextVar( token, aDepth + 1 ) )
{
return true;
}
return false;
};
wxString text = EDA_TEXT::GetShownText();
if( board && HasTextVars() && aDepth < 10 )
text = ExpandTextVars( text, &pcbTextResolver );
return text;
}
double PCB_TEXT::ViewGetLOD( int aLayer, KIGFX::VIEW* aView ) const
{
constexpr double HIDE = std::numeric_limits<double>::max();
KIGFX::PCB_PAINTER* painter = static_cast<KIGFX::PCB_PAINTER*>( aView->GetPainter() );
KIGFX::PCB_RENDER_SETTINGS* renderSettings = painter->GetSettings();
if( aLayer == LAYER_LOCKED_ITEM_SHADOW )
{
// Hide shadow if the main layer is not shown
if( !aView->IsLayerVisible( m_layer ) )
return HIDE;
// Hide shadow on dimmed tracks
if( renderSettings->GetHighContrast() )
{
if( m_layer != renderSettings->GetPrimaryHighContrastLayer() )
return HIDE;
}
}
return 0.0;
}
void PCB_TEXT::GetMsgPanelInfo( EDA_DRAW_FRAME* aFrame, std::vector<MSG_PANEL_ITEM>& aList )
{
// Don't use GetShownText() here; we want to show the user the variable references
aList.emplace_back( _( "PCB Text" ), KIUI::EllipsizeStatusText( aFrame, GetText() ) );
2008-03-04 04:22:27 +00:00
if( aFrame->GetName() == PCB_EDIT_FRAME_NAME && IsLocked() )
aList.emplace_back( _( "Status" ), _( "Locked" ) );
aList.emplace_back( _( "Layer" ), GetLayerName() );
aList.emplace_back( _( "Mirror" ), IsMirrored() ? _( "Yes" ) : _( "No" ) );
2022-02-04 22:44:59 +00:00
aList.emplace_back( _( "Angle" ), wxString::Format( wxT( "%g" ), GetTextAngle().AsDegrees() ) );
aList.emplace_back( _( "Font" ), GetFont() ? GetFont()->GetName() : _( "Default" ) );
2022-09-19 09:25:20 +00:00
aList.emplace_back( _( "Thickness" ), aFrame->MessageTextFromValue( GetTextThickness() ) );
aList.emplace_back( _( "Width" ), aFrame->MessageTextFromValue( GetTextWidth() ) );
aList.emplace_back( _( "Height" ), aFrame->MessageTextFromValue( GetTextHeight() ) );
}
int PCB_TEXT::getKnockoutMargin() const
{
VECTOR2I textSize( GetTextWidth(), GetTextHeight() );
int thickness = GetTextThickness();
return thickness * 1.5 + GetKnockoutTextMargin( textSize, thickness );
}
2022-08-31 09:15:42 +00:00
const BOX2I PCB_TEXT::GetBoundingBox() const
{
2022-08-30 23:28:18 +00:00
BOX2I rect = GetTextBox();
if( IsKnockout() )
rect.Inflate( getKnockoutMargin() );
2022-01-13 12:29:46 +00:00
if( !GetTextAngle().IsZero() )
2022-01-16 21:15:20 +00:00
rect = rect.GetBoundingBoxRotated( GetTextPos(), GetTextAngle() );
return rect;
}
bool PCB_TEXT::TextHitTest( const VECTOR2I& aPoint, int aAccuracy ) const
{
if( IsKnockout() )
{
SHAPE_POLY_SET poly;
TransformBoundingBoxToPolygon( &poly, getKnockoutMargin());
return poly.Collide( aPoint, aAccuracy );
}
else
{
return EDA_TEXT::TextHitTest( aPoint, aAccuracy );
}
}
2022-08-31 09:33:46 +00:00
bool PCB_TEXT::TextHitTest( const BOX2I& aRect, bool aContains, int aAccuracy ) const
{
2022-08-31 09:33:46 +00:00
BOX2I rect = aRect;
rect.Inflate( aAccuracy );
if( aContains )
return rect.Contains( GetBoundingBox() );
return rect.Intersects( GetBoundingBox() );
}
void PCB_TEXT::Rotate( const VECTOR2I& aRotCentre, const EDA_ANGLE& aAngle )
2009-08-01 19:26:05 +00:00
{
VECTOR2I pt = GetTextPos();
RotatePoint( pt, aRotCentre, aAngle );
SetTextPos( pt );
EDA_ANGLE new_angle = GetTextAngle() + aAngle;
new_angle.Normalize180();
SetTextAngle( new_angle );
2009-08-01 19:26:05 +00:00
}
void PCB_TEXT::Mirror( const VECTOR2I& aCentre, bool aMirrorAroundXAxis )
{
// the position and justification are mirrored, but not the text itself
if( aMirrorAroundXAxis )
{
if( GetTextAngle() == ANGLE_VERTICAL )
SetHorizJustify( (GR_TEXT_H_ALIGN_T) -GetHorizJustify() );
SetTextY( MIRRORVAL( GetTextPos().y, aCentre.y ) );
}
else
{
if( GetTextAngle() == ANGLE_HORIZONTAL )
SetHorizJustify( (GR_TEXT_H_ALIGN_T) -GetHorizJustify() );
SetTextX( MIRRORVAL( GetTextPos().x, aCentre.x ) );
}
}
void PCB_TEXT::Flip( const VECTOR2I& aCentre, bool aFlipLeftRight )
2009-08-01 19:26:05 +00:00
{
if( aFlipLeftRight )
{
SetTextX( MIRRORVAL( GetTextPos().x, aCentre.x ) );
SetTextAngle( -GetTextAngle() );
}
else
{
SetTextY( MIRRORVAL( GetTextPos().y, aCentre.y ) );
2022-01-13 23:30:52 +00:00
SetTextAngle( ANGLE_180 - GetTextAngle() );
}
SetLayer( FlipLayer( GetLayer(), GetBoard()->GetCopperLayerCount() ) );
if( ( GetLayerSet() & LSET::SideSpecificMask() ).any() )
SetMirrored( !IsMirrored() );
2009-08-01 19:26:05 +00:00
}
wxString PCB_TEXT::GetItemDescription( UNITS_PROVIDER* aUnitsProvider ) const
{
return wxString::Format( _( "PCB Text '%s' on %s"),
KIUI::EllipsizeMenuText( GetShownText() ),
GetLayerName() );
}
BITMAPS PCB_TEXT::GetMenuImage() const
{
return BITMAPS::text;
}
EDA_ITEM* PCB_TEXT::Clone() const
{
return new PCB_TEXT( *this );
}
void PCB_TEXT::swapData( BOARD_ITEM* aImage )
{
assert( aImage->Type() == PCB_TEXT_T );
std::swap( *((PCB_TEXT*) this), *((PCB_TEXT*) aImage) );
}
std::shared_ptr<SHAPE> PCB_TEXT::GetEffectiveShape( PCB_LAYER_ID aLayer, FLASHING aFlash ) const
{
return GetEffectiveTextShape();
}
void PCB_TEXT::TransformTextToPolySet( SHAPE_POLY_SET& aBuffer, PCB_LAYER_ID aLayer,
int aClearance, int aError, ERROR_LOC aErrorLoc ) const
{
KIGFX::GAL_DISPLAY_OPTIONS empty_opts;
KIFONT::FONT* font = getDrawFont();
int penWidth = GetEffectiveTextPenWidth();
// Note: this function is mainly used in 3D viewer.
// the polygonal shape of a text can have many basic shapes,
// so combining these shapes can be very useful to create a final shape
// swith a lot less vertices to speedup calculations using this final shape
// Simplify shapes is not usually always efficient, but in this case it is.
SHAPE_POLY_SET buffer;
CALLBACK_GAL callback_gal( empty_opts,
// Stroke callback
[&]( const VECTOR2I& aPt1, const VECTOR2I& aPt2 )
{
TransformOvalToPolygon( aBuffer, aPt1, aPt2, penWidth + ( 2 * aClearance ),
aError, ERROR_INSIDE );
},
// Triangulation callback
[&]( const VECTOR2I& aPt1, const VECTOR2I& aPt2, const VECTOR2I& aPt3 )
{
buffer.NewOutline();
for( const VECTOR2I& point : { aPt1, aPt2, aPt3 } )
buffer.Append( point.x, point.y );
} );
font->Draw( &callback_gal, GetShownText(), GetTextPos(), GetAttributes() );
buffer.Simplify( SHAPE_POLY_SET::PM_FAST );
aBuffer.Append( buffer );
}
void PCB_TEXT::TransformShapeToPolygon( SHAPE_POLY_SET& aBuffer, PCB_LAYER_ID aLayer,
int aClearance, int aError, ERROR_LOC aErrorLoc,
bool aIgnoreLineWidth ) const
{
EDA_TEXT::TransformBoundingBoxToPolygon( &aBuffer, aClearance );
}
static struct PCB_TEXT_DESC
{
PCB_TEXT_DESC()
{
PROPERTY_MANAGER& propMgr = PROPERTY_MANAGER::Instance();
REGISTER_TYPE( PCB_TEXT );
propMgr.AddTypeCast( new TYPE_CAST<PCB_TEXT, BOARD_ITEM> );
propMgr.AddTypeCast( new TYPE_CAST<PCB_TEXT, EDA_TEXT> );
propMgr.InheritsAfter( TYPE_HASH( PCB_TEXT ), TYPE_HASH( BOARD_ITEM ) );
propMgr.InheritsAfter( TYPE_HASH( PCB_TEXT ), TYPE_HASH( EDA_TEXT ) );
2022-12-03 02:46:03 +00:00
propMgr.AddProperty( new PROPERTY<PCB_TEXT, bool, BOARD_ITEM>( _HKI( "Knockout" ),
&BOARD_ITEM::SetIsKnockout, &BOARD_ITEM::IsKnockout ),
_HKI( "Text Properties" ) );
auto isFootprintText =
[]( INSPECTABLE* aItem ) -> bool
{
if( PCB_TEXT* text = dynamic_cast<PCB_TEXT*>( aItem ) )
return text->GetParentFootprint();
return false;
};
propMgr.OverrideAvailability( TYPE_HASH( PCB_TEXT ), TYPE_HASH( EDA_TEXT ),
_HKI( "Visible" ), isFootprintText );
propMgr.OverrideAvailability( TYPE_HASH( PCB_TEXT ), TYPE_HASH( EDA_TEXT ),
_HKI( "Hyperlink" ),
[]( INSPECTABLE* aItem ) { return false; } );
}
} _PCB_TEXT_DESC;