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.

523 lines
15 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2022-2023 KiCad Developers, see AUTHORS.txt for contributors.
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version 2
  9. * of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, you may find one here:
  18. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  19. * or you may search the http://www.gnu.org website for the version 2 license,
  20. * or you may write to the Free Software Foundation, Inc.,
  21. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  22. */
  23. #include <pcb_edit_frame.h>
  24. #include <board.h>
  25. #include <board_design_settings.h>
  26. #include <core/mirror.h>
  27. #include <footprint.h>
  28. #include <fp_textbox.h>
  29. #include <settings/settings_manager.h>
  30. #include <string_utils.h>
  31. #include <painter.h>
  32. #include <geometry/shape_compound.h>
  33. #include <callback_gal.h>
  34. #include <convert_basic_shapes_to_polygon.h>
  35. FP_TEXTBOX::FP_TEXTBOX( FOOTPRINT* aParentFootprint ) :
  36. FP_SHAPE( aParentFootprint, SHAPE_T::RECT, PCB_FP_TEXTBOX_T ),
  37. EDA_TEXT( pcbIUScale )
  38. {
  39. SetHorizJustify( GR_TEXT_H_ALIGN_LEFT );
  40. SetVertJustify( GR_TEXT_V_ALIGN_TOP );
  41. SetMultilineAllowed( true );
  42. SetDrawCoord();
  43. }
  44. FP_TEXTBOX::~FP_TEXTBOX()
  45. {
  46. }
  47. int FP_TEXTBOX::GetTextMargin() const
  48. {
  49. return KiROUND( GetStroke().GetWidth() / 2.0 ) + KiROUND( GetTextSize().y * 0.75 );
  50. }
  51. VECTOR2I FP_TEXTBOX::GetTopLeft() const
  52. {
  53. EDA_ANGLE rotation = GetDrawRotation();
  54. if( rotation == ANGLE_90 )
  55. return VECTOR2I( GetStartX(), GetEndY() );
  56. else if( rotation == ANGLE_180 )
  57. return GetEnd();
  58. else if( rotation == ANGLE_270 )
  59. return VECTOR2I( GetEndX(), GetStartY() );
  60. else
  61. return GetStart();
  62. }
  63. VECTOR2I FP_TEXTBOX::GetBotRight() const
  64. {
  65. EDA_ANGLE rotation = GetDrawRotation();
  66. if( rotation == ANGLE_90 )
  67. return VECTOR2I( GetEndX(), GetStartY() );
  68. else if( rotation == ANGLE_180 )
  69. return GetStart();
  70. else if( rotation == ANGLE_270 )
  71. return VECTOR2I( GetStartX(), GetEndY() );
  72. else
  73. return GetEnd();
  74. }
  75. void FP_TEXTBOX::SetTop( int aVal )
  76. {
  77. EDA_ANGLE rotation = GetDrawRotation();
  78. if( rotation == ANGLE_90 || rotation == ANGLE_180 )
  79. SetEndY( aVal );
  80. else
  81. SetStartY( aVal );
  82. }
  83. void FP_TEXTBOX::SetBottom( int aVal )
  84. {
  85. EDA_ANGLE rotation = GetDrawRotation();
  86. if( rotation == ANGLE_90 || rotation == ANGLE_180 )
  87. SetStartY( aVal );
  88. else
  89. SetEndY( aVal );
  90. }
  91. void FP_TEXTBOX::SetLeft( int aVal )
  92. {
  93. EDA_ANGLE rotation = GetDrawRotation();
  94. if( rotation == ANGLE_180 || rotation == ANGLE_270 )
  95. SetEndX( aVal );
  96. else
  97. SetStartX( aVal );
  98. }
  99. void FP_TEXTBOX::SetRight( int aVal )
  100. {
  101. EDA_ANGLE rotation = GetDrawRotation();
  102. if( rotation == ANGLE_180 || rotation == ANGLE_270 )
  103. SetStartX( aVal );
  104. else
  105. SetEndX( aVal );
  106. }
  107. EDA_ANGLE FP_TEXTBOX::GetDrawRotation() const
  108. {
  109. FOOTPRINT* parentFootprint = static_cast<FOOTPRINT*>( m_parent );
  110. EDA_ANGLE rotation = GetTextAngle();
  111. if( parentFootprint )
  112. rotation += parentFootprint->GetOrientation();
  113. rotation.Normalize();
  114. return rotation;
  115. }
  116. std::vector<VECTOR2I> FP_TEXTBOX::GetAnchorAndOppositeCorner() const
  117. {
  118. std::vector<VECTOR2I> pts;
  119. std::vector<VECTOR2I> corners = GetCorners();
  120. EDA_ANGLE textAngle( GetDrawRotation() );
  121. textAngle.Normalize();
  122. pts.emplace_back( corners[0] );
  123. if( textAngle < ANGLE_90 )
  124. {
  125. if( corners[1].y <= corners[0].y )
  126. pts.emplace_back( corners[1] );
  127. else
  128. pts.emplace_back( corners[3] );
  129. }
  130. else if( textAngle < ANGLE_180 )
  131. {
  132. if( corners[1].x <= corners[0].x )
  133. pts.emplace_back( corners[1] );
  134. else
  135. pts.emplace_back( corners[3] );
  136. }
  137. else if( textAngle < ANGLE_270 )
  138. {
  139. if( corners[1].y >= corners[0].y )
  140. pts.emplace_back( corners[1] );
  141. else
  142. pts.emplace_back( corners[3] );
  143. }
  144. else
  145. {
  146. if( corners[1].x >= corners[0].x )
  147. pts.emplace_back( corners[1] );
  148. else
  149. pts.emplace_back( corners[3] );
  150. }
  151. return pts;
  152. }
  153. VECTOR2I FP_TEXTBOX::GetDrawPos() const
  154. {
  155. std::vector<VECTOR2I> corners = GetAnchorAndOppositeCorner();
  156. GR_TEXT_H_ALIGN_T effectiveAlignment = GetHorizJustify();
  157. VECTOR2I textAnchor;
  158. VECTOR2I offset;
  159. if( IsMirrored() )
  160. {
  161. switch( GetHorizJustify() )
  162. {
  163. case GR_TEXT_H_ALIGN_LEFT: effectiveAlignment = GR_TEXT_H_ALIGN_RIGHT; break;
  164. case GR_TEXT_H_ALIGN_CENTER: effectiveAlignment = GR_TEXT_H_ALIGN_CENTER; break;
  165. case GR_TEXT_H_ALIGN_RIGHT: effectiveAlignment = GR_TEXT_H_ALIGN_LEFT; break;
  166. }
  167. }
  168. switch( effectiveAlignment )
  169. {
  170. case GR_TEXT_H_ALIGN_LEFT:
  171. textAnchor = corners[0];
  172. offset = VECTOR2I( GetTextMargin(), GetTextMargin() );
  173. break;
  174. case GR_TEXT_H_ALIGN_CENTER:
  175. textAnchor = ( corners[0] + corners[1] ) / 2;
  176. offset = VECTOR2I( 0, GetTextMargin() );
  177. break;
  178. case GR_TEXT_H_ALIGN_RIGHT:
  179. textAnchor = corners[1];
  180. offset = VECTOR2I( -GetTextMargin(), GetTextMargin() );
  181. break;
  182. }
  183. RotatePoint( offset, GetDrawRotation() );
  184. return textAnchor + offset;
  185. }
  186. bool FP_TEXTBOX::HitTest( const VECTOR2I& aPosition, int aAccuracy ) const
  187. {
  188. BOX2I rect = GetBoundingBox();
  189. rect.Inflate( aAccuracy );
  190. return rect.Contains( aPosition );
  191. }
  192. bool FP_TEXTBOX::HitTest( const BOX2I& aRect, bool aContained, int aAccuracy ) const
  193. {
  194. BOX2I rect = aRect;
  195. rect.Inflate( aAccuracy );
  196. if( aContained )
  197. return rect.Contains( GetBoundingBox() );
  198. return rect.Intersects( GetBoundingBox() );
  199. }
  200. void FP_TEXTBOX::Move( const VECTOR2I& aMoveVector )
  201. {
  202. FP_SHAPE::Move( aMoveVector );
  203. EDA_TEXT::Offset( aMoveVector );
  204. }
  205. void FP_TEXTBOX::Rotate( const VECTOR2I& aRotCentre, const EDA_ANGLE& aAngle )
  206. {
  207. FP_SHAPE::Rotate( aRotCentre, aAngle );
  208. SetTextAngle( GetTextAngle() + aAngle );
  209. }
  210. void FP_TEXTBOX::Flip( const VECTOR2I& aCentre, bool aFlipLeftRight )
  211. {
  212. // flipping the footprint is relative to the X axis
  213. if( aFlipLeftRight )
  214. {
  215. SetTextX( MIRRORVAL( GetTextPos().x, aCentre.x ) );
  216. SetTextAngle( -GetTextAngle() );
  217. }
  218. else
  219. {
  220. SetTextY( MIRRORVAL( GetTextPos().y, aCentre.y ) );
  221. SetTextAngle( ANGLE_180 - GetTextAngle() );
  222. }
  223. SetLayer( FlipLayer( GetLayer(), GetBoard()->GetCopperLayerCount() ) );
  224. if( ( GetLayerSet() & LSET::SideSpecificMask() ).any() )
  225. SetMirrored( !IsMirrored() );
  226. SetLocalCoord();
  227. }
  228. void FP_TEXTBOX::Mirror( const VECTOR2I& aCentre, bool aMirrorAroundXAxis )
  229. {
  230. // the position is mirrored, but not the text (or its justification)
  231. FP_SHAPE::Mirror( aCentre, aMirrorAroundXAxis );
  232. BOX2I rect( m_start0, m_end0 - m_start0 );
  233. rect.Normalize();
  234. m_start0 = VECTOR2I( rect.GetLeft(), rect.GetTop() );
  235. m_end0 = VECTOR2I( rect.GetRight(), rect.GetBottom() );
  236. SetDrawCoord();
  237. }
  238. void FP_TEXTBOX::GetMsgPanelInfo( EDA_DRAW_FRAME* aFrame, std::vector<MSG_PANEL_ITEM>& aList )
  239. {
  240. // Don't use GetShownText() here; we want to show the user the variable references
  241. aList.emplace_back( _( "Text Box" ), UnescapeString( GetText() ) );
  242. if( aFrame->GetName() == PCB_EDIT_FRAME_NAME && IsLocked() )
  243. aList.emplace_back( _( "Status" ), _( "Locked" ) );
  244. aList.emplace_back( _( "Layer" ), GetLayerName() );
  245. aList.emplace_back( _( "Mirror" ), IsMirrored() ? _( "Yes" ) : _( "No" ) );
  246. aList.emplace_back( _( "Angle" ), wxString::Format( "%g", GetTextAngle().AsDegrees() ) );
  247. aList.emplace_back( _( "Font" ), GetFont() ? GetFont()->GetName() : _( "Default" ) );
  248. aList.emplace_back( _( "Thickness" ), aFrame->MessageTextFromValue( GetTextThickness() ) );
  249. aList.emplace_back( _( "Text Width" ), aFrame->MessageTextFromValue( GetTextWidth() ) );
  250. aList.emplace_back( _( "Text Height" ), aFrame->MessageTextFromValue( GetTextHeight() ) );
  251. wxString msg = aFrame->MessageTextFromValue( std::abs( GetEnd().x - GetStart().x ) );
  252. aList.emplace_back( _( "Box Width" ), msg );
  253. msg = aFrame->MessageTextFromValue( std::abs( GetEnd().y - GetStart().y ) );
  254. aList.emplace_back( _( "Box Height" ), msg );
  255. m_stroke.GetMsgPanelInfo( aFrame, aList );
  256. }
  257. wxString FP_TEXTBOX::GetItemDescription( UNITS_PROVIDER* aUnitsProvider ) const
  258. {
  259. return wxString::Format( _( "Footprint Text Box of %s" ),
  260. static_cast<FOOTPRINT*>( GetParent() )->GetReference() );
  261. }
  262. BITMAPS FP_TEXTBOX::GetMenuImage() const
  263. {
  264. return BITMAPS::add_textbox;
  265. }
  266. EDA_ITEM* FP_TEXTBOX::Clone() const
  267. {
  268. return new FP_TEXTBOX( *this );
  269. }
  270. void FP_TEXTBOX::ViewGetLayers( int aLayers[], int& aCount ) const
  271. {
  272. if( IsVisible() )
  273. aLayers[0] = GetLayer();
  274. else
  275. aLayers[0] = LAYER_MOD_TEXT_INVISIBLE;
  276. aCount = 1;
  277. }
  278. double FP_TEXTBOX::ViewGetLOD( int aLayer, KIGFX::VIEW* aView ) const
  279. {
  280. constexpr double HIDE = (double)std::numeric_limits<double>::max();
  281. if( !aView )
  282. return 0.0;
  283. // Hidden text gets put on the LAYER_MOD_TEXT_INVISIBLE for rendering, but
  284. // should only render if its native layer is visible.
  285. if( !aView->IsLayerVisible( GetLayer() ) )
  286. return HIDE;
  287. RENDER_SETTINGS* renderSettings = aView->GetPainter()->GetSettings();
  288. COLOR4D backgroundColor = renderSettings->GetLayerColor( LAYER_PCB_BACKGROUND );
  289. // Handle Render tab switches
  290. if( renderSettings->GetLayerColor( LAYER_MOD_TEXT ) == backgroundColor )
  291. return HIDE;
  292. if( !IsParentFlipped() && !aView->IsLayerVisible( LAYER_MOD_FR ) )
  293. return HIDE;
  294. if( IsParentFlipped() && !aView->IsLayerVisible( LAYER_MOD_BK ) )
  295. return HIDE;
  296. if( !aView->IsLayerVisible( LAYER_MOD_TEXT ) )
  297. return HIDE;
  298. // Other layers are shown without any conditions
  299. return 0.0;
  300. }
  301. wxString FP_TEXTBOX::GetShownText( int aDepth, bool aAllowExtraText ) const
  302. {
  303. const FOOTPRINT* parentFootprint = static_cast<FOOTPRINT*>( GetParent() );
  304. std::function<bool( wxString* )> footprintResolver =
  305. [&]( wxString* token ) -> bool
  306. {
  307. return parentFootprint && parentFootprint->ResolveTextVar( token, aDepth );
  308. };
  309. wxString text = EDA_TEXT::GetShownText();
  310. if( HasTextVars() )
  311. {
  312. if( aDepth < 10 )
  313. text = ExpandTextVars( text, &footprintResolver );
  314. }
  315. KIFONT::FONT* font = getDrawFont();
  316. std::vector<VECTOR2I> corners = GetAnchorAndOppositeCorner();
  317. int colWidth = ( corners[1] - corners[0] ).EuclideanNorm();
  318. colWidth -= GetTextMargin() * 2;
  319. font->LinebreakText( text, colWidth, GetTextSize(), GetTextThickness(), IsBold(), IsItalic() );
  320. return text;
  321. }
  322. std::shared_ptr<SHAPE> FP_TEXTBOX::GetEffectiveShape( PCB_LAYER_ID aLayer, FLASHING aFlash ) const
  323. {
  324. std::shared_ptr<SHAPE_COMPOUND> shape = GetEffectiveTextShape();
  325. if( PCB_SHAPE::GetStroke().GetWidth() >= 0 )
  326. shape->AddShape( PCB_SHAPE::GetEffectiveShape( aLayer, aFlash ) );
  327. return shape;
  328. }
  329. void FP_TEXTBOX::TransformTextToPolySet( SHAPE_POLY_SET& aBuffer, PCB_LAYER_ID aLayer,
  330. int aClearance, int aError, ERROR_LOC aErrorLoc ) const
  331. {
  332. KIGFX::GAL_DISPLAY_OPTIONS empty_opts;
  333. KIFONT::FONT* font = getDrawFont();
  334. int penWidth = GetEffectiveTextPenWidth();
  335. // Note: this function is mainly used in 3D viewer.
  336. // the polygonal shape of a text can have many basic shapes,
  337. // so combining these shapes can be very useful to create a final shape
  338. // swith a lot less vertices to speedup calculations using this final shape
  339. // Simplify shapes is not usually always efficient, but in this case it is.
  340. SHAPE_POLY_SET buffer;
  341. CALLBACK_GAL callback_gal( empty_opts,
  342. // Stroke callback
  343. [&]( const VECTOR2I& aPt1, const VECTOR2I& aPt2 )
  344. {
  345. TransformOvalToPolygon( buffer, aPt1, aPt2, penWidth + ( 2 * aClearance ), aError,
  346. ERROR_INSIDE );
  347. },
  348. // Triangulation callback
  349. [&]( const VECTOR2I& aPt1, const VECTOR2I& aPt2, const VECTOR2I& aPt3 )
  350. {
  351. buffer.NewOutline();
  352. for( const VECTOR2I& point : { aPt1, aPt2, aPt3 } )
  353. buffer.Append( point.x, point.y );
  354. } );
  355. TEXT_ATTRIBUTES attrs = GetAttributes();
  356. attrs.m_Angle = GetDrawRotation();
  357. font->Draw( &callback_gal, GetShownText(), GetDrawPos(), attrs );
  358. buffer.Simplify( SHAPE_POLY_SET::PM_FAST );
  359. aBuffer.Append( buffer );
  360. }
  361. void FP_TEXTBOX::TransformShapeToPolygon( SHAPE_POLY_SET& aBuffer, PCB_LAYER_ID aLayer,
  362. int aClearance, int aError, ERROR_LOC aErrorLoc,
  363. bool aIgnoreLineWidth ) const
  364. {
  365. // Don't use FP_SHAPE::TransformShapeToPolygon. We want to treat the textbox as filled even
  366. // if there's no background colour.
  367. std::vector<VECTOR2I> pts = GetRectCorners();
  368. aBuffer.NewOutline();
  369. for( const VECTOR2I& pt : pts )
  370. aBuffer.Append( pt );
  371. int width = GetWidth() + ( 2 * aClearance );
  372. if( width > 0 )
  373. {
  374. // Add in segments
  375. TransformOvalToPolygon( aBuffer, pts[0], pts[1], width, aError, aErrorLoc );
  376. TransformOvalToPolygon( aBuffer, pts[1], pts[2], width, aError, aErrorLoc );
  377. TransformOvalToPolygon( aBuffer, pts[2], pts[3], width, aError, aErrorLoc );
  378. TransformOvalToPolygon( aBuffer, pts[3], pts[0], width, aError, aErrorLoc );
  379. }
  380. }
  381. wxString FP_TEXTBOX::GetParentAsString() const
  382. {
  383. if( FOOTPRINT* fp = dynamic_cast<FOOTPRINT*>( m_parent ) )
  384. return fp->GetReference();
  385. return m_parent->m_Uuid.AsString();
  386. }
  387. static struct FP_TEXTBOX_DESC
  388. {
  389. FP_TEXTBOX_DESC()
  390. {
  391. PROPERTY_MANAGER& propMgr = PROPERTY_MANAGER::Instance();
  392. REGISTER_TYPE( FP_TEXTBOX );
  393. propMgr.AddTypeCast( new TYPE_CAST<FP_TEXTBOX, FP_SHAPE> );
  394. propMgr.AddTypeCast( new TYPE_CAST<FP_TEXTBOX, EDA_TEXT> );
  395. propMgr.InheritsAfter( TYPE_HASH( FP_TEXTBOX ), TYPE_HASH( FP_SHAPE ) );
  396. propMgr.InheritsAfter( TYPE_HASH( FP_TEXTBOX ), TYPE_HASH( EDA_TEXT ) );
  397. propMgr.AddProperty( new PROPERTY<FP_TEXTBOX, wxString>( _HKI( "Parent" ),
  398. NO_SETTER( FP_TEXTBOX, wxString ), &FP_TEXTBOX::GetParentAsString ) );
  399. }
  400. } _FP_TEXTBOX_DESC;