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.

515 lines
15 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2022 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 <base_units.h>
  25. #include <bitmaps.h>
  26. #include <board.h>
  27. #include <board_design_settings.h>
  28. #include <core/mirror.h>
  29. #include <footprint.h>
  30. #include <fp_textbox.h>
  31. #include <settings/settings_manager.h>
  32. #include <trigo.h>
  33. #include <string_utils.h>
  34. #include <painter.h>
  35. #include <geometry/shape_compound.h>
  36. #include <callback_gal.h>
  37. #include <convert_basic_shapes_to_polygon.h>
  38. FP_TEXTBOX::FP_TEXTBOX( FOOTPRINT* aParentFootprint ) :
  39. FP_SHAPE( aParentFootprint, SHAPE_T::RECT, PCB_FP_TEXTBOX_T ),
  40. EDA_TEXT()
  41. {
  42. SetHorizJustify( GR_TEXT_H_ALIGN_LEFT );
  43. SetVertJustify( GR_TEXT_V_ALIGN_TOP );
  44. SetMultilineAllowed( true );
  45. SetDrawCoord();
  46. }
  47. FP_TEXTBOX::~FP_TEXTBOX()
  48. {
  49. }
  50. int FP_TEXTBOX::GetTextMargin() const
  51. {
  52. return KiROUND( GetTextSize().y * 0.8 );
  53. }
  54. VECTOR2I FP_TEXTBOX::GetTopLeft() const
  55. {
  56. EDA_ANGLE rotation = GetDrawRotation();
  57. if( rotation == ANGLE_90 )
  58. return VECTOR2I( GetStartX(), GetEndY() );
  59. else if( rotation == ANGLE_180 )
  60. return GetEnd();
  61. else if( rotation == ANGLE_270 )
  62. return VECTOR2I( GetEndX(), GetStartY() );
  63. else
  64. return GetStart();
  65. }
  66. VECTOR2I FP_TEXTBOX::GetBotRight() const
  67. {
  68. EDA_ANGLE rotation = GetDrawRotation();
  69. if( rotation == ANGLE_90 )
  70. return VECTOR2I( GetEndX(), GetStartY() );
  71. else if( rotation == ANGLE_180 )
  72. return GetStart();
  73. else if( rotation == ANGLE_270 )
  74. return VECTOR2I( GetStartX(), GetEndY() );
  75. else
  76. return GetEnd();
  77. }
  78. void FP_TEXTBOX::SetTop( int aVal )
  79. {
  80. EDA_ANGLE rotation = GetDrawRotation();
  81. if( rotation == ANGLE_90 || rotation == ANGLE_180 )
  82. SetEndY( aVal );
  83. else
  84. SetStartY( aVal );
  85. }
  86. void FP_TEXTBOX::SetBottom( int aVal )
  87. {
  88. EDA_ANGLE rotation = GetDrawRotation();
  89. if( rotation == ANGLE_90 || rotation == ANGLE_180 )
  90. SetStartY( aVal );
  91. else
  92. SetEndY( aVal );
  93. }
  94. void FP_TEXTBOX::SetLeft( int aVal )
  95. {
  96. EDA_ANGLE rotation = GetDrawRotation();
  97. if( rotation == ANGLE_180 || rotation == ANGLE_270 )
  98. SetEndX( aVal );
  99. else
  100. SetStartX( aVal );
  101. }
  102. void FP_TEXTBOX::SetRight( int aVal )
  103. {
  104. EDA_ANGLE rotation = GetDrawRotation();
  105. if( rotation == ANGLE_180 || rotation == ANGLE_270 )
  106. SetStartX( aVal );
  107. else
  108. SetEndX( aVal );
  109. }
  110. EDA_ANGLE FP_TEXTBOX::GetDrawRotation() const
  111. {
  112. FOOTPRINT* parentFootprint = static_cast<FOOTPRINT*>( m_parent );
  113. EDA_ANGLE rotation = GetTextAngle();
  114. if( parentFootprint )
  115. rotation += parentFootprint->GetOrientation();
  116. rotation.Normalize();
  117. return rotation;
  118. }
  119. std::vector<VECTOR2I> FP_TEXTBOX::GetAnchorAndOppositeCorner() const
  120. {
  121. std::vector<VECTOR2I> pts;
  122. std::vector<VECTOR2I> corners = GetCorners();
  123. EDA_ANGLE textAngle( GetDrawRotation() );
  124. textAngle.Normalize();
  125. pts.emplace_back( corners[0] );
  126. if( textAngle < ANGLE_90 )
  127. {
  128. if( corners[1].y <= corners[0].y )
  129. pts.emplace_back( corners[1] );
  130. else
  131. pts.emplace_back( corners[3] );
  132. }
  133. else if( textAngle < ANGLE_180 )
  134. {
  135. if( corners[1].x <= corners[0].x )
  136. pts.emplace_back( corners[1] );
  137. else
  138. pts.emplace_back( corners[3] );
  139. }
  140. else if( textAngle < ANGLE_270 )
  141. {
  142. if( corners[1].y >= corners[0].y )
  143. pts.emplace_back( corners[1] );
  144. else
  145. pts.emplace_back( corners[3] );
  146. }
  147. else
  148. {
  149. if( corners[1].x >= corners[0].x )
  150. pts.emplace_back( corners[1] );
  151. else
  152. pts.emplace_back( corners[3] );
  153. }
  154. return pts;
  155. }
  156. VECTOR2I FP_TEXTBOX::GetDrawPos() const
  157. {
  158. std::vector<VECTOR2I> corners = GetAnchorAndOppositeCorner();
  159. GR_TEXT_H_ALIGN_T effectiveAlignment = GetHorizJustify();
  160. VECTOR2I textAnchor;
  161. VECTOR2I offset;
  162. if( IsMirrored() )
  163. {
  164. switch( GetHorizJustify() )
  165. {
  166. case GR_TEXT_H_ALIGN_LEFT: effectiveAlignment = GR_TEXT_H_ALIGN_RIGHT; break;
  167. case GR_TEXT_H_ALIGN_CENTER: effectiveAlignment = GR_TEXT_H_ALIGN_CENTER; break;
  168. case GR_TEXT_H_ALIGN_RIGHT: effectiveAlignment = GR_TEXT_H_ALIGN_LEFT; break;
  169. }
  170. }
  171. switch( effectiveAlignment )
  172. {
  173. case GR_TEXT_H_ALIGN_LEFT:
  174. textAnchor = corners[0];
  175. offset = VECTOR2I( GetTextMargin(), GetTextMargin() );
  176. break;
  177. case GR_TEXT_H_ALIGN_CENTER:
  178. textAnchor = ( corners[0] + corners[1] ) / 2;
  179. offset = VECTOR2I( 0, GetTextMargin() );
  180. break;
  181. case GR_TEXT_H_ALIGN_RIGHT:
  182. textAnchor = corners[1];
  183. offset = VECTOR2I( -GetTextMargin(), GetTextMargin() );
  184. break;
  185. }
  186. RotatePoint( offset, GetDrawRotation() );
  187. return textAnchor + offset;
  188. }
  189. bool FP_TEXTBOX::HitTest( const VECTOR2I& aPosition, int aAccuracy ) const
  190. {
  191. EDA_RECT rect = GetBoundingBox();
  192. rect.Inflate( aAccuracy );
  193. return rect.Contains( aPosition );
  194. }
  195. bool FP_TEXTBOX::HitTest( const EDA_RECT& aRect, bool aContained, int aAccuracy ) const
  196. {
  197. EDA_RECT rect = aRect;
  198. rect.Inflate( aAccuracy );
  199. if( aContained )
  200. return rect.Contains( GetBoundingBox() );
  201. return rect.Intersects( GetBoundingBox() );
  202. }
  203. void FP_TEXTBOX::Move( const VECTOR2I& aMoveVector )
  204. {
  205. FP_SHAPE::Move( aMoveVector );
  206. EDA_TEXT::Offset( aMoveVector );
  207. }
  208. void FP_TEXTBOX::Rotate( const VECTOR2I& aRotCentre, const EDA_ANGLE& aAngle )
  209. {
  210. FP_SHAPE::Rotate( aRotCentre, aAngle );
  211. SetTextAngle( GetTextAngle() + aAngle );
  212. }
  213. void FP_TEXTBOX::Flip( const VECTOR2I& aCentre, bool aFlipLeftRight )
  214. {
  215. // flipping the footprint is relative to the X axis
  216. if( aFlipLeftRight )
  217. {
  218. SetTextX( MIRRORVAL( GetTextPos().x, aCentre.x ) );
  219. SetTextAngle( -GetTextAngle() );
  220. }
  221. else
  222. {
  223. SetTextY( MIRRORVAL( GetTextPos().y, aCentre.y ) );
  224. SetTextAngle( ANGLE_180 - GetTextAngle() );
  225. }
  226. SetLayer( FlipLayer( GetLayer(), GetBoard()->GetCopperLayerCount() ) );
  227. SetMirrored( IsBackLayer( GetLayer() ) );
  228. SetLocalCoord();
  229. }
  230. void FP_TEXTBOX::Mirror( const VECTOR2I& aCentre, bool aMirrorAroundXAxis )
  231. {
  232. // the position is mirrored, but the text itself is not mirrored
  233. if( aMirrorAroundXAxis )
  234. SetTextY( ::MIRRORVAL( GetTextPos().y, aCentre.y ) );
  235. else
  236. SetTextX( ::MIRRORVAL( GetTextPos().x, aCentre.x ) );
  237. SetLocalCoord();
  238. }
  239. void FP_TEXTBOX::GetMsgPanelInfo( EDA_DRAW_FRAME* aFrame, std::vector<MSG_PANEL_ITEM>& aList )
  240. {
  241. EDA_UNITS units = aFrame->GetUserUnits();
  242. // Don't use GetShownText() here; we want to show the user the variable references
  243. aList.emplace_back( _( "Text Box" ), UnescapeString( GetText() ) );
  244. if( aFrame->GetName() == PCB_EDIT_FRAME_NAME && IsLocked() )
  245. aList.emplace_back( _( "Status" ), _( "Locked" ) );
  246. aList.emplace_back( _( "Layer" ), GetLayerName() );
  247. aList.emplace_back( _( "Mirror" ), IsMirrored() ? _( "Yes" ) : _( "No" ) );
  248. aList.emplace_back( _( "Angle" ), wxString::Format( "%g", GetTextAngle().AsDegrees() ) );
  249. aList.emplace_back( _( "Font" ), GetDrawFont()->GetName() );
  250. aList.emplace_back( _( "Thickness" ), MessageTextFromValue( units, GetTextThickness() ) );
  251. aList.emplace_back( _( "Text Width" ), MessageTextFromValue( units, GetTextWidth() ) );
  252. aList.emplace_back( _( "Text Height" ), MessageTextFromValue( units, GetTextHeight() ) );
  253. wxString msg = MessageTextFromValue( units, std::abs( GetEnd().x - GetStart().x ) );
  254. aList.emplace_back( _( "Box Width" ), msg );
  255. msg = MessageTextFromValue( units, std::abs( GetEnd().y - GetStart().y ) );
  256. aList.emplace_back( _( "Box Height" ), msg );
  257. m_stroke.GetMsgPanelInfo( units, aList );
  258. }
  259. wxString FP_TEXTBOX::GetSelectMenuText( EDA_UNITS aUnits ) const
  260. {
  261. return wxString::Format( _( "Footprint Text Box of %s" ),
  262. static_cast<FOOTPRINT*>( GetParent() )->GetReference() );
  263. }
  264. BITMAPS FP_TEXTBOX::GetMenuImage() const
  265. {
  266. return BITMAPS::add_textbox;
  267. }
  268. EDA_ITEM* FP_TEXTBOX::Clone() const
  269. {
  270. return new FP_TEXTBOX( *this );
  271. }
  272. void FP_TEXTBOX::ViewGetLayers( int aLayers[], int& aCount ) const
  273. {
  274. if( IsVisible() )
  275. aLayers[0] = GetLayer();
  276. else
  277. aLayers[0] = LAYER_MOD_TEXT_INVISIBLE;
  278. aCount = 1;
  279. }
  280. double FP_TEXTBOX::ViewGetLOD( int aLayer, KIGFX::VIEW* aView ) const
  281. {
  282. constexpr double HIDE = (double)std::numeric_limits<double>::max();
  283. if( !aView )
  284. return 0.0;
  285. // Hidden text gets put on the LAYER_MOD_TEXT_INVISIBLE for rendering, but
  286. // should only render if its native layer is visible.
  287. if( !aView->IsLayerVisible( GetLayer() ) )
  288. return HIDE;
  289. RENDER_SETTINGS* renderSettings = aView->GetPainter()->GetSettings();
  290. COLOR4D backgroundColor = renderSettings->GetLayerColor( LAYER_PCB_BACKGROUND );
  291. // Handle Render tab switches
  292. if( renderSettings->GetLayerColor( LAYER_MOD_TEXT ) == backgroundColor )
  293. return HIDE;
  294. if( !IsParentFlipped() && !aView->IsLayerVisible( LAYER_MOD_FR ) )
  295. return HIDE;
  296. if( IsParentFlipped() && !aView->IsLayerVisible( LAYER_MOD_BK ) )
  297. return HIDE;
  298. if( !aView->IsLayerVisible( LAYER_MOD_TEXT ) )
  299. return HIDE;
  300. // Other layers are shown without any conditions
  301. return 0.0;
  302. }
  303. wxString FP_TEXTBOX::GetShownText( int aDepth ) const
  304. {
  305. const FOOTPRINT* parentFootprint = static_cast<FOOTPRINT*>( GetParent() );
  306. wxASSERT( parentFootprint );
  307. const BOARD* board = parentFootprint->GetBoard();
  308. std::function<bool( wxString* )> footprintResolver =
  309. [&]( wxString* token ) -> bool
  310. {
  311. return parentFootprint && parentFootprint->ResolveTextVar( token, aDepth );
  312. };
  313. std::function<bool( wxString* )> boardTextResolver =
  314. [&]( wxString* token ) -> bool
  315. {
  316. return board->ResolveTextVar( token, aDepth + 1 );
  317. };
  318. wxString text = EDA_TEXT::GetShownText();
  319. if( HasTextVars() )
  320. {
  321. PROJECT* project = nullptr;
  322. if( parentFootprint && parentFootprint->GetParent() )
  323. project = static_cast<BOARD*>( parentFootprint->GetParent() )->GetProject();
  324. if( aDepth < 10 )
  325. text = ExpandTextVars( text, &footprintResolver, &boardTextResolver, project );
  326. }
  327. KIFONT::FONT* font = GetDrawFont();
  328. std::vector<VECTOR2I> corners = GetAnchorAndOppositeCorner();
  329. int colWidth = ( corners[1] - corners[0] ).EuclideanNorm();
  330. colWidth -= GetTextMargin() * 2;
  331. font->LinebreakText( text, colWidth, GetTextSize(), GetTextThickness(), IsBold(), IsItalic() );
  332. return text;
  333. }
  334. std::shared_ptr<SHAPE> FP_TEXTBOX::GetEffectiveShape( PCB_LAYER_ID aLayer, FLASHING aFlash ) const
  335. {
  336. return GetEffectiveTextShape();
  337. }
  338. void FP_TEXTBOX::TransformTextShapeWithClearanceToPolygon( SHAPE_POLY_SET& aCornerBuffer,
  339. PCB_LAYER_ID aLayer, int aClearance,
  340. int aError, ERROR_LOC aErrorLoc ) const
  341. {
  342. KIGFX::GAL_DISPLAY_OPTIONS empty_opts;
  343. KIFONT::FONT* font = GetDrawFont();
  344. int penWidth = GetEffectiveTextPenWidth();
  345. CALLBACK_GAL callback_gal( empty_opts,
  346. // Stroke callback
  347. [&]( const VECTOR2I& aPt1, const VECTOR2I& aPt2 )
  348. {
  349. TransformOvalToPolygon( aCornerBuffer, aPt1, aPt2, penWidth + ( 2 * aClearance ),
  350. aError, ERROR_INSIDE );
  351. },
  352. // Triangulation callback
  353. [&]( const VECTOR2I& aPt1, const VECTOR2I& aPt2, const VECTOR2I& aPt3 )
  354. {
  355. aCornerBuffer.NewOutline();
  356. for( const VECTOR2I& point : { aPt1, aPt2, aPt3 } )
  357. aCornerBuffer.Append( point.x, point.y );
  358. } );
  359. TEXT_ATTRIBUTES attrs = GetAttributes();
  360. attrs.m_Angle = GetDrawRotation();
  361. font->Draw( &callback_gal, GetShownText(), GetDrawPos(), attrs );
  362. }
  363. void FP_TEXTBOX::TransformShapeWithClearanceToPolygon( SHAPE_POLY_SET& aCornerBuffer,
  364. PCB_LAYER_ID aLayer, int aClearance,
  365. int aError, ERROR_LOC aErrorLoc,
  366. bool aIgnoreLineWidth ) const
  367. {
  368. // Don't use FP_SHAPE::TransformShapeWithClearanceToPolygon. We want to treat the
  369. // textbox as filled even if there's no background colour.
  370. std::vector<VECTOR2I> pts = GetRectCorners();
  371. aCornerBuffer.NewOutline();
  372. for( const VECTOR2I& pt : pts )
  373. aCornerBuffer.Append( pt );
  374. int width = GetWidth() + ( 2 * aClearance );
  375. if( width > 0 )
  376. {
  377. // Add in segments
  378. TransformOvalToPolygon( aCornerBuffer, pts[0], pts[1], width, aError, aErrorLoc );
  379. TransformOvalToPolygon( aCornerBuffer, pts[1], pts[2], width, aError, aErrorLoc );
  380. TransformOvalToPolygon( aCornerBuffer, pts[2], pts[3], width, aError, aErrorLoc );
  381. TransformOvalToPolygon( aCornerBuffer, pts[3], pts[0], width, aError, aErrorLoc );
  382. }
  383. }
  384. static struct FP_TEXTBOX_DESC
  385. {
  386. FP_TEXTBOX_DESC()
  387. {
  388. PROPERTY_MANAGER& propMgr = PROPERTY_MANAGER::Instance();
  389. REGISTER_TYPE( FP_TEXTBOX );
  390. propMgr.AddTypeCast( new TYPE_CAST<FP_TEXTBOX, FP_SHAPE> );
  391. propMgr.AddTypeCast( new TYPE_CAST<FP_TEXTBOX, EDA_TEXT> );
  392. propMgr.InheritsAfter( TYPE_HASH( FP_TEXTBOX ), TYPE_HASH( FP_SHAPE ) );
  393. propMgr.InheritsAfter( TYPE_HASH( FP_TEXTBOX ), TYPE_HASH( EDA_TEXT ) );
  394. propMgr.AddProperty( new PROPERTY<FP_TEXTBOX, wxString>( _HKI( "Parent" ),
  395. NO_SETTER( FP_TEXTBOX, wxString ), &FP_TEXTBOX::GetParentAsString ) );
  396. }
  397. } _FP_TEXTBOX_DESC;