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.

345 lines
10 KiB

5 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( pcbIUScale )
  42. {
  43. SetMultilineAllowed( true );
  44. }
  45. PCB_TEXT::~PCB_TEXT()
  46. {
  47. }
  48. wxString PCB_TEXT::GetShownText( int aDepth, bool aAllowExtraText ) 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. // Don't use GetShownText() here; we want to show the user the variable references
  108. aList.emplace_back( _( "PCB Text" ), KIUI::EllipsizeStatusText( aFrame, GetText() ) );
  109. if( aFrame->GetName() == PCB_EDIT_FRAME_NAME && IsLocked() )
  110. aList.emplace_back( _( "Status" ), _( "Locked" ) );
  111. aList.emplace_back( _( "Layer" ), GetLayerName() );
  112. aList.emplace_back( _( "Mirror" ), IsMirrored() ? _( "Yes" ) : _( "No" ) );
  113. aList.emplace_back( _( "Angle" ), wxString::Format( wxT( "%g" ), GetTextAngle().AsDegrees() ) );
  114. aList.emplace_back( _( "Font" ), GetFont() ? GetFont()->GetName() : _( "Default" ) );
  115. aList.emplace_back( _( "Thickness" ), aFrame->MessageTextFromValue( GetTextThickness() ) );
  116. aList.emplace_back( _( "Width" ), aFrame->MessageTextFromValue( GetTextWidth() ) );
  117. aList.emplace_back( _( "Height" ), aFrame->MessageTextFromValue( GetTextHeight() ) );
  118. }
  119. int PCB_TEXT::getKnockoutMargin() const
  120. {
  121. VECTOR2I textSize( GetTextWidth(), GetTextHeight() );
  122. int thickness = GetTextThickness();
  123. return thickness * 1.5 + GetKnockoutTextMargin( textSize, thickness );
  124. }
  125. const BOX2I PCB_TEXT::GetBoundingBox() const
  126. {
  127. BOX2I rect = GetTextBox();
  128. if( IsKnockout() )
  129. rect.Inflate( getKnockoutMargin() );
  130. if( !GetTextAngle().IsZero() )
  131. rect = rect.GetBoundingBoxRotated( GetTextPos(), GetTextAngle() );
  132. return rect;
  133. }
  134. bool PCB_TEXT::TextHitTest( const VECTOR2I& aPoint, int aAccuracy ) const
  135. {
  136. if( IsKnockout() )
  137. {
  138. SHAPE_POLY_SET poly;
  139. TransformBoundingBoxToPolygon( &poly, getKnockoutMargin());
  140. return poly.Collide( aPoint, aAccuracy );
  141. }
  142. else
  143. {
  144. return EDA_TEXT::TextHitTest( aPoint, aAccuracy );
  145. }
  146. }
  147. bool PCB_TEXT::TextHitTest( const BOX2I& aRect, bool aContains, int aAccuracy ) const
  148. {
  149. BOX2I rect = aRect;
  150. rect.Inflate( aAccuracy );
  151. if( aContains )
  152. return rect.Contains( GetBoundingBox() );
  153. return rect.Intersects( GetBoundingBox() );
  154. }
  155. void PCB_TEXT::Rotate( const VECTOR2I& aRotCentre, const EDA_ANGLE& aAngle )
  156. {
  157. VECTOR2I pt = GetTextPos();
  158. RotatePoint( pt, aRotCentre, aAngle );
  159. SetTextPos( pt );
  160. EDA_ANGLE new_angle = GetTextAngle() + aAngle;
  161. new_angle.Normalize180();
  162. SetTextAngle( new_angle );
  163. }
  164. void PCB_TEXT::Mirror( const VECTOR2I& aCentre, bool aMirrorAroundXAxis )
  165. {
  166. // the position and justification are mirrored, but not the text itself
  167. if( aMirrorAroundXAxis )
  168. {
  169. if( GetTextAngle() == ANGLE_VERTICAL )
  170. SetHorizJustify( (GR_TEXT_H_ALIGN_T) -GetHorizJustify() );
  171. SetTextY( MIRRORVAL( GetTextPos().y, aCentre.y ) );
  172. }
  173. else
  174. {
  175. if( GetTextAngle() == ANGLE_HORIZONTAL )
  176. SetHorizJustify( (GR_TEXT_H_ALIGN_T) -GetHorizJustify() );
  177. SetTextX( MIRRORVAL( GetTextPos().x, aCentre.x ) );
  178. }
  179. }
  180. void PCB_TEXT::Flip( const VECTOR2I& aCentre, bool aFlipLeftRight )
  181. {
  182. if( aFlipLeftRight )
  183. {
  184. SetTextX( MIRRORVAL( GetTextPos().x, aCentre.x ) );
  185. SetTextAngle( -GetTextAngle() );
  186. }
  187. else
  188. {
  189. SetTextY( MIRRORVAL( GetTextPos().y, aCentre.y ) );
  190. SetTextAngle( ANGLE_180 - GetTextAngle() );
  191. }
  192. SetLayer( FlipLayer( GetLayer(), GetBoard()->GetCopperLayerCount() ) );
  193. if( ( GetLayerSet() & LSET::SideSpecificMask() ).any() )
  194. SetMirrored( !IsMirrored() );
  195. }
  196. wxString PCB_TEXT::GetSelectMenuText( UNITS_PROVIDER* aUnitsProvider ) const
  197. {
  198. return wxString::Format( _( "PCB Text '%s' on %s"),
  199. KIUI::EllipsizeMenuText( GetShownText() ),
  200. GetLayerName() );
  201. }
  202. BITMAPS PCB_TEXT::GetMenuImage() const
  203. {
  204. return BITMAPS::text;
  205. }
  206. EDA_ITEM* PCB_TEXT::Clone() const
  207. {
  208. return new PCB_TEXT( *this );
  209. }
  210. void PCB_TEXT::swapData( BOARD_ITEM* aImage )
  211. {
  212. assert( aImage->Type() == PCB_TEXT_T );
  213. std::swap( *((PCB_TEXT*) this), *((PCB_TEXT*) aImage) );
  214. }
  215. std::shared_ptr<SHAPE> PCB_TEXT::GetEffectiveShape( PCB_LAYER_ID aLayer, FLASHING aFlash ) const
  216. {
  217. return GetEffectiveTextShape();
  218. }
  219. void PCB_TEXT::TransformTextToPolySet( SHAPE_POLY_SET& aBuffer, PCB_LAYER_ID aLayer,
  220. int aClearance, int aError, ERROR_LOC aErrorLoc ) const
  221. {
  222. KIGFX::GAL_DISPLAY_OPTIONS empty_opts;
  223. KIFONT::FONT* font = getDrawFont();
  224. int penWidth = GetEffectiveTextPenWidth();
  225. // Note: this function is mainly used in 3D viewer.
  226. // the polygonal shape of a text can have many basic shapes,
  227. // so combining these shapes can be very useful to create a final shape
  228. // swith a lot less vertices to speedup calculations using this final shape
  229. // Simplify shapes is not usually always efficient, but in this case it is.
  230. SHAPE_POLY_SET buffer;
  231. CALLBACK_GAL callback_gal( empty_opts,
  232. // Stroke callback
  233. [&]( const VECTOR2I& aPt1, const VECTOR2I& aPt2 )
  234. {
  235. TransformOvalToPolygon( aBuffer, aPt1, aPt2, penWidth + ( 2 * aClearance ),
  236. aError, ERROR_INSIDE );
  237. },
  238. // Triangulation callback
  239. [&]( const VECTOR2I& aPt1, const VECTOR2I& aPt2, const VECTOR2I& aPt3 )
  240. {
  241. buffer.NewOutline();
  242. for( const VECTOR2I& point : { aPt1, aPt2, aPt3 } )
  243. buffer.Append( point.x, point.y );
  244. } );
  245. font->Draw( &callback_gal, GetShownText(), GetTextPos(), GetAttributes() );
  246. buffer.Simplify( SHAPE_POLY_SET::PM_FAST );
  247. aBuffer.Append( buffer );
  248. }
  249. void PCB_TEXT::TransformShapeToPolygon( SHAPE_POLY_SET& aBuffer, PCB_LAYER_ID aLayer,
  250. int aClearance, int aError, ERROR_LOC aErrorLoc,
  251. bool aIgnoreLineWidth ) const
  252. {
  253. EDA_TEXT::TransformBoundingBoxToPolygon( &aBuffer, aClearance );
  254. }
  255. static struct TEXTE_PCB_DESC
  256. {
  257. TEXTE_PCB_DESC()
  258. {
  259. PROPERTY_MANAGER& propMgr = PROPERTY_MANAGER::Instance();
  260. REGISTER_TYPE( PCB_TEXT );
  261. propMgr.AddTypeCast( new TYPE_CAST<PCB_TEXT, BOARD_ITEM> );
  262. propMgr.AddTypeCast( new TYPE_CAST<PCB_TEXT, EDA_TEXT> );
  263. propMgr.InheritsAfter( TYPE_HASH( PCB_TEXT ), TYPE_HASH( BOARD_ITEM ) );
  264. propMgr.InheritsAfter( TYPE_HASH( PCB_TEXT ), TYPE_HASH( EDA_TEXT ) );
  265. }
  266. } _TEXTE_PCB_DESC;