You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

291 lines
8.9 KiB

6 years ago
  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2012 Jean-Pierre Charras, jean-pierre.charras@ujf-grenoble.fr
  5. * Copyright (C) 2012 SoftPLC Corporation, Dick Hollenbeck <dick@softplc.com>
  6. * Copyright (C) 1992-2022 KiCad Developers, see AUTHORS.txt for contributors.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version 2
  11. * of the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, you may find one here:
  20. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  21. * or you may search the http://www.gnu.org website for the version 2 license,
  22. * or you may write to the Free Software Foundation, Inc.,
  23. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  24. */
  25. #include <pcb_edit_frame.h>
  26. #include <base_units.h>
  27. #include <bitmaps.h>
  28. #include <board.h>
  29. #include <board_design_settings.h>
  30. #include <core/mirror.h>
  31. #include <footprint.h>
  32. #include <pcb_text.h>
  33. #include <pcb_painter.h>
  34. #include <trigo.h>
  35. #include <string_utils.h>
  36. #include <geometry/shape_compound.h>
  37. #include <callback_gal.h>
  38. #include <convert_basic_shapes_to_polygon.h>
  39. PCB_TEXT::PCB_TEXT( BOARD_ITEM* parent ) :
  40. BOARD_ITEM( parent, PCB_TEXT_T ),
  41. EDA_TEXT()
  42. {
  43. SetMultilineAllowed( true );
  44. }
  45. PCB_TEXT::~PCB_TEXT()
  46. {
  47. }
  48. wxString PCB_TEXT::GetShownText( int aDepth ) const
  49. {
  50. BOARD* board = dynamic_cast<BOARD*>( GetParent() );
  51. std::function<bool( wxString* )> pcbTextResolver =
  52. [&]( wxString* token ) -> bool
  53. {
  54. if( token->IsSameAs( wxT( "LAYER" ) ) )
  55. {
  56. *token = GetLayerName();
  57. return true;
  58. }
  59. if( token->Contains( ':' ) )
  60. {
  61. wxString remainder;
  62. wxString ref = token->BeforeFirst( ':', &remainder );
  63. BOARD_ITEM* refItem = board->GetItem( KIID( ref ) );
  64. if( refItem && refItem->Type() == PCB_FOOTPRINT_T )
  65. {
  66. FOOTPRINT* refFP = static_cast<FOOTPRINT*>( refItem );
  67. if( refFP->ResolveTextVar( &remainder, aDepth + 1 ) )
  68. {
  69. *token = remainder;
  70. return true;
  71. }
  72. }
  73. }
  74. return false;
  75. };
  76. std::function<bool( wxString* )> boardTextResolver =
  77. [&]( wxString* token ) -> bool
  78. {
  79. return board->ResolveTextVar( token, aDepth + 1 );
  80. };
  81. wxString text = EDA_TEXT::GetShownText();
  82. if( board && HasTextVars() && aDepth < 10 )
  83. text = ExpandTextVars( text, &pcbTextResolver, &boardTextResolver, board->GetProject() );
  84. return text;
  85. }
  86. double PCB_TEXT::ViewGetLOD( int aLayer, KIGFX::VIEW* aView ) const
  87. {
  88. constexpr double HIDE = std::numeric_limits<double>::max();
  89. KIGFX::PCB_PAINTER* painter = static_cast<KIGFX::PCB_PAINTER*>( aView->GetPainter() );
  90. KIGFX::PCB_RENDER_SETTINGS* renderSettings = painter->GetSettings();
  91. if( aLayer == LAYER_LOCKED_ITEM_SHADOW )
  92. {
  93. // Hide shadow if the main layer is not shown
  94. if( !aView->IsLayerVisible( m_layer ) )
  95. return HIDE;
  96. // Hide shadow on dimmed tracks
  97. if( renderSettings->GetHighContrast() )
  98. {
  99. if( m_layer != renderSettings->GetPrimaryHighContrastLayer() )
  100. return HIDE;
  101. }
  102. }
  103. return 0.0;
  104. }
  105. void PCB_TEXT::GetMsgPanelInfo( EDA_DRAW_FRAME* aFrame, std::vector<MSG_PANEL_ITEM>& aList )
  106. {
  107. EDA_UNITS units = aFrame->GetUserUnits();
  108. // Don't use GetShownText() here; we want to show the user the variable references
  109. aList.emplace_back( _( "PCB Text" ), UnescapeString( GetText() ) );
  110. if( aFrame->GetName() == PCB_EDIT_FRAME_NAME && IsLocked() )
  111. aList.emplace_back( _( "Status" ), _( "Locked" ) );
  112. aList.emplace_back( _( "Layer" ), GetLayerName() );
  113. aList.emplace_back( _( "Mirror" ), IsMirrored() ? _( "Yes" ) : _( "No" ) );
  114. aList.emplace_back( _( "Angle" ), wxString::Format( wxT( "%g" ), GetTextAngle().AsDegrees() ) );
  115. aList.emplace_back( _( "Font" ), GetDrawFont()->GetName() );
  116. aList.emplace_back( _( "Thickness" ), MessageTextFromValue( units, GetTextThickness() ) );
  117. aList.emplace_back( _( "Width" ), MessageTextFromValue( units, GetTextWidth() ) );
  118. aList.emplace_back( _( "Height" ), MessageTextFromValue( units, GetTextHeight() ) );
  119. }
  120. const EDA_RECT PCB_TEXT::GetBoundingBox() const
  121. {
  122. EDA_RECT rect = GetTextBox();
  123. if( !GetTextAngle().IsZero() )
  124. rect = rect.GetBoundingBoxRotated( GetTextPos(), GetTextAngle() );
  125. return rect;
  126. }
  127. bool PCB_TEXT::TextHitTest( const VECTOR2I& aPoint, int aAccuracy ) const
  128. {
  129. return EDA_TEXT::TextHitTest( aPoint, aAccuracy );
  130. }
  131. bool PCB_TEXT::TextHitTest( const EDA_RECT& aRect, bool aContains, int aAccuracy ) const
  132. {
  133. EDA_RECT rect = aRect;
  134. rect.Inflate( aAccuracy );
  135. if( aContains )
  136. return rect.Contains( GetBoundingBox() );
  137. return rect.Intersects( GetBoundingBox() );
  138. }
  139. void PCB_TEXT::Rotate( const VECTOR2I& aRotCentre, const EDA_ANGLE& aAngle )
  140. {
  141. VECTOR2I pt = GetTextPos();
  142. RotatePoint( pt, aRotCentre, aAngle );
  143. SetTextPos( pt );
  144. EDA_ANGLE new_angle = GetTextAngle() + aAngle;
  145. new_angle.Normalize180();
  146. SetTextAngle( new_angle );
  147. }
  148. void PCB_TEXT::Flip( const VECTOR2I& aCentre, bool aFlipLeftRight )
  149. {
  150. if( aFlipLeftRight )
  151. {
  152. SetTextX( MIRRORVAL( GetTextPos().x, aCentre.x ) );
  153. SetTextAngle( -GetTextAngle() );
  154. }
  155. else
  156. {
  157. SetTextY( MIRRORVAL( GetTextPos().y, aCentre.y ) );
  158. SetTextAngle( ANGLE_180 - GetTextAngle() );
  159. }
  160. SetLayer( FlipLayer( GetLayer(), GetBoard()->GetCopperLayerCount() ) );
  161. SetMirrored( !IsMirrored() );
  162. }
  163. wxString PCB_TEXT::GetSelectMenuText( EDA_UNITS aUnits ) const
  164. {
  165. return wxString::Format( _( "PCB Text '%s' on %s"), ShortenedShownText(), GetLayerName() );
  166. }
  167. BITMAPS PCB_TEXT::GetMenuImage() const
  168. {
  169. return BITMAPS::text;
  170. }
  171. EDA_ITEM* PCB_TEXT::Clone() const
  172. {
  173. return new PCB_TEXT( *this );
  174. }
  175. void PCB_TEXT::SwapData( BOARD_ITEM* aImage )
  176. {
  177. assert( aImage->Type() == PCB_TEXT_T );
  178. std::swap( *((PCB_TEXT*) this), *((PCB_TEXT*) aImage) );
  179. }
  180. std::shared_ptr<SHAPE> PCB_TEXT::GetEffectiveShape( PCB_LAYER_ID aLayer, FLASHING aFlash ) const
  181. {
  182. return GetEffectiveTextShape();
  183. }
  184. void PCB_TEXT::TransformTextShapeWithClearanceToPolygon( SHAPE_POLY_SET& aCornerBuffer,
  185. PCB_LAYER_ID aLayer, int aClearance,
  186. int aError, ERROR_LOC aErrorLoc ) const
  187. {
  188. KIGFX::GAL_DISPLAY_OPTIONS empty_opts;
  189. KIFONT::FONT* font = GetDrawFont();
  190. int penWidth = GetEffectiveTextPenWidth();
  191. CALLBACK_GAL callback_gal( empty_opts,
  192. // Stroke callback
  193. [&]( const VECTOR2I& aPt1, const VECTOR2I& aPt2 )
  194. {
  195. TransformOvalToPolygon( aCornerBuffer, aPt1, aPt2, penWidth+ ( 2 * aClearance ),
  196. aError, ERROR_INSIDE );
  197. },
  198. // Triangulation callback
  199. [&]( const VECTOR2I& aPt1, const VECTOR2I& aPt2, const VECTOR2I& aPt3 )
  200. {
  201. aCornerBuffer.NewOutline();
  202. for( const VECTOR2I& point : { aPt1, aPt2, aPt3 } )
  203. aCornerBuffer.Append( point.x, point.y );
  204. } );
  205. font->Draw( &callback_gal, GetShownText(), GetTextPos(), GetAttributes() );
  206. }
  207. void PCB_TEXT::TransformShapeWithClearanceToPolygon( SHAPE_POLY_SET& aCornerBuffer,
  208. PCB_LAYER_ID aLayer, int aClearance,
  209. int aError, ERROR_LOC aErrorLoc,
  210. bool aIgnoreLineWidth ) const
  211. {
  212. EDA_TEXT::TransformBoundingBoxWithClearanceToPolygon( &aCornerBuffer, aClearance );
  213. }
  214. static struct TEXTE_PCB_DESC
  215. {
  216. TEXTE_PCB_DESC()
  217. {
  218. PROPERTY_MANAGER& propMgr = PROPERTY_MANAGER::Instance();
  219. REGISTER_TYPE( PCB_TEXT );
  220. propMgr.AddTypeCast( new TYPE_CAST<PCB_TEXT, BOARD_ITEM> );
  221. propMgr.AddTypeCast( new TYPE_CAST<PCB_TEXT, EDA_TEXT> );
  222. propMgr.InheritsAfter( TYPE_HASH( PCB_TEXT ), TYPE_HASH( BOARD_ITEM ) );
  223. propMgr.InheritsAfter( TYPE_HASH( PCB_TEXT ), TYPE_HASH( EDA_TEXT ) );
  224. }
  225. } _TEXTE_PCB_DESC;