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.

773 lines
24 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2016 Jean-Pierre Charras, jp.charras at wanadoo.fr
  5. * Copyright (C) 2004-2021 KiCad Developers, see change_log.txt for contributors.
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version 2
  10. * of the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, you may find one here:
  19. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  20. * or you may search the http://www.gnu.org website for the version 2 license,
  21. * or you may write to the Free Software Foundation, Inc.,
  22. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  23. */
  24. /**
  25. * @file eda_text.cpp
  26. * @brief Implementation of base KiCad text object.
  27. */
  28. #include <algorithm> // for max
  29. #include <stddef.h> // for NULL
  30. #include <type_traits> // for swap
  31. #include <vector> // for vector
  32. #include <eda_item.h> // for EDA_ITEM
  33. #include <base_units.h>
  34. #include <basic_gal.h> // for BASIC_GAL, basic_gal
  35. #include <convert_to_biu.h> // for Mils2iu
  36. #include <convert_basic_shapes_to_polygon.h>
  37. #include <eda_rect.h> // for EDA_RECT
  38. #include <eda_text.h> // for EDA_TEXT, TEXT_EFFECTS, GR_TEXT_VJUSTIF...
  39. #include <gal/color4d.h> // for COLOR4D, COLOR4D::BLACK
  40. #include <gal/stroke_font.h> // for STROKE_FONT
  41. #include <gr_text.h> // for GRText
  42. #include <string_utils.h> // for UnescapeString
  43. #include <math/util.h> // for KiROUND
  44. #include <math/vector2d.h> // for VECTOR2D
  45. #include <richio.h>
  46. #include <render_settings.h>
  47. #include <trigo.h> // for RotatePoint
  48. #include <i18n_utility.h>
  49. #include <geometry/shape_segment.h>
  50. #include <geometry/shape_compound.h>
  51. #include <geometry/shape_poly_set.h>
  52. #include <wx/debug.h> // for wxASSERT
  53. #include <wx/string.h> // wxString, wxArrayString
  54. #include <wx/gdicmn.h> // for wxPoint,wxSize
  55. class OUTPUTFORMATTER;
  56. class wxFindReplaceData;
  57. void addTextSegmToPoly( int x0, int y0, int xf, int yf, void* aData )
  58. {
  59. TSEGM_2_POLY_PRMS* prm = static_cast<TSEGM_2_POLY_PRMS*>( aData );
  60. TransformOvalToPolygon( *prm->m_cornerBuffer, wxPoint( x0, y0 ), wxPoint( xf, yf ),
  61. prm->m_textWidth, prm->m_error, ERROR_INSIDE );
  62. }
  63. EDA_TEXT_HJUSTIFY_T EDA_TEXT::MapHorizJustify( int aHorizJustify )
  64. {
  65. wxASSERT( aHorizJustify >= GR_TEXT_HJUSTIFY_LEFT && aHorizJustify <= GR_TEXT_HJUSTIFY_RIGHT );
  66. if( aHorizJustify > GR_TEXT_HJUSTIFY_RIGHT )
  67. return GR_TEXT_HJUSTIFY_RIGHT;
  68. if( aHorizJustify < GR_TEXT_HJUSTIFY_LEFT )
  69. return GR_TEXT_HJUSTIFY_LEFT;
  70. return static_cast<EDA_TEXT_HJUSTIFY_T>( aHorizJustify );
  71. }
  72. EDA_TEXT_VJUSTIFY_T EDA_TEXT::MapVertJustify( int aVertJustify )
  73. {
  74. wxASSERT( aVertJustify >= GR_TEXT_VJUSTIFY_TOP && aVertJustify <= GR_TEXT_VJUSTIFY_BOTTOM );
  75. if( aVertJustify > GR_TEXT_VJUSTIFY_BOTTOM )
  76. return GR_TEXT_VJUSTIFY_BOTTOM;
  77. if( aVertJustify < GR_TEXT_VJUSTIFY_TOP )
  78. return GR_TEXT_VJUSTIFY_TOP;
  79. return static_cast<EDA_TEXT_VJUSTIFY_T>( aVertJustify );
  80. }
  81. EDA_TEXT::EDA_TEXT( const wxString& text ) :
  82. m_text( text ),
  83. m_e( 1 << TE_VISIBLE )
  84. {
  85. int sz = Mils2iu( DEFAULT_SIZE_TEXT );
  86. SetTextSize( wxSize( sz, sz ) );
  87. cacheShownText();
  88. }
  89. EDA_TEXT::EDA_TEXT( const EDA_TEXT& aText ) :
  90. m_text( aText.m_text ),
  91. m_e( aText.m_e )
  92. {
  93. cacheShownText();
  94. }
  95. EDA_TEXT::~EDA_TEXT()
  96. {
  97. }
  98. void EDA_TEXT::SetText( const wxString& aText )
  99. {
  100. m_text = aText;
  101. cacheShownText();
  102. }
  103. void EDA_TEXT::CopyText( const EDA_TEXT& aSrc )
  104. {
  105. m_text = aSrc.m_text;
  106. m_shown_text = aSrc.m_shown_text;
  107. m_shown_text_has_text_var_refs = aSrc.m_shown_text_has_text_var_refs;
  108. }
  109. void EDA_TEXT::SetEffects( const EDA_TEXT& aSrc )
  110. {
  111. m_e = aSrc.m_e;
  112. }
  113. void EDA_TEXT::SwapText( EDA_TEXT& aTradingPartner )
  114. {
  115. std::swap( m_text, aTradingPartner.m_text );
  116. std::swap( m_shown_text, aTradingPartner.m_shown_text );
  117. std::swap( m_shown_text_has_text_var_refs, aTradingPartner.m_shown_text_has_text_var_refs );
  118. }
  119. void EDA_TEXT::SwapEffects( EDA_TEXT& aTradingPartner )
  120. {
  121. std::swap( m_e, aTradingPartner.m_e );
  122. }
  123. int EDA_TEXT::GetEffectiveTextPenWidth( int aDefaultWidth ) const
  124. {
  125. int width = GetTextThickness();
  126. if( width <= 1 )
  127. {
  128. width = aDefaultWidth;
  129. if( IsBold() )
  130. width = GetPenSizeForBold( GetTextWidth() );
  131. else if( width <= 1 )
  132. width = GetPenSizeForNormal( GetTextWidth() );
  133. }
  134. // Clip pen size for small texts:
  135. width = Clamp_Text_PenSize( width, GetTextSize(), ALLOW_BOLD_THICKNESS );
  136. return width;
  137. }
  138. bool EDA_TEXT::Replace( const wxFindReplaceData& aSearchData )
  139. {
  140. bool retval = EDA_ITEM::Replace( aSearchData, m_text );
  141. cacheShownText();
  142. return retval;
  143. }
  144. void EDA_TEXT::cacheShownText()
  145. {
  146. if( m_text.IsEmpty() || m_text == wxT( "~" ) ) // ~ is legacy empty-string token
  147. {
  148. m_shown_text = wxEmptyString;
  149. m_shown_text_has_text_var_refs = false;
  150. }
  151. else
  152. {
  153. m_shown_text = UnescapeString( m_text );
  154. m_shown_text_has_text_var_refs = m_shown_text.Contains( wxT( "${" ) );
  155. }
  156. }
  157. int EDA_TEXT::LenSize( const wxString& aLine, int aThickness ) const
  158. {
  159. basic_gal.SetFontItalic( IsItalic() );
  160. basic_gal.SetFontBold( IsBold() );
  161. basic_gal.SetFontUnderlined( false );
  162. basic_gal.SetLineWidth( (float) aThickness );
  163. basic_gal.SetGlyphSize( VECTOR2D( GetTextSize() ) );
  164. VECTOR2D tsize = basic_gal.GetTextLineSize( aLine );
  165. return KiROUND( tsize.x );
  166. }
  167. wxString EDA_TEXT::ShortenedShownText() const
  168. {
  169. wxString tmp = GetShownText();
  170. tmp.Replace( wxT( "\n" ), wxT( " " ) );
  171. tmp.Replace( wxT( "\r" ), wxT( " " ) );
  172. tmp.Replace( wxT( "\t" ), wxT( " " ) );
  173. if( tmp.Length() > 36 )
  174. tmp = tmp.Left( 34 ) + wxT( "..." );
  175. return tmp;
  176. }
  177. int EDA_TEXT::GetInterline() const
  178. {
  179. return KiROUND( KIGFX::STROKE_FONT::GetInterline( GetTextHeight() ) );
  180. }
  181. EDA_RECT EDA_TEXT::GetTextBox( int aLine, bool aInvertY ) const
  182. {
  183. EDA_RECT rect;
  184. wxArrayString strings;
  185. wxString text = GetShownText();
  186. int thickness = GetEffectiveTextPenWidth();
  187. int linecount = 1;
  188. bool hasOverBar = false; // true if the first line of text as an overbar
  189. if( IsMultilineAllowed() )
  190. {
  191. wxStringSplit( text, strings, '\n' );
  192. if( strings.GetCount() ) // GetCount() == 0 for void strings
  193. {
  194. if( aLine >= 0 && ( aLine < static_cast<int>( strings.GetCount() ) ) )
  195. text = strings.Item( aLine );
  196. else
  197. text = strings.Item( 0 );
  198. linecount = strings.GetCount();
  199. }
  200. }
  201. // Search for overbar symbol. Only text is scanned,
  202. // because only this line can change the bounding box
  203. for( unsigned ii = 1; ii < text.size(); ii++ )
  204. {
  205. if( text[ii-1] == '~' && text[ii] == '{' )
  206. {
  207. hasOverBar = true;
  208. break;
  209. }
  210. }
  211. // calculate the H and V size
  212. const auto& font = basic_gal.GetStrokeFont();
  213. VECTOR2D fontSize( GetTextSize() );
  214. double penWidth( thickness );
  215. int dx = KiROUND( font.ComputeStringBoundaryLimits( text, fontSize, penWidth ).x );
  216. int dy = GetInterline();
  217. // Creates bounding box (rectangle) for horizontal, left and top justified text. The
  218. // bounding box will be moved later according to the actual text options
  219. wxSize textsize = wxSize( dx, dy );
  220. wxPoint pos = GetTextPos();
  221. if( IsMultilineAllowed() && aLine > 0 && ( aLine < static_cast<int>( strings.GetCount() ) ) )
  222. {
  223. pos.y -= aLine * GetInterline();
  224. }
  225. if( aInvertY )
  226. pos.y = -pos.y;
  227. rect.SetOrigin( pos );
  228. if( hasOverBar )
  229. { // A overbar adds an extra size to the text
  230. // Height from the base line text of chars like [ or {
  231. double curr_height = GetTextHeight() * 1.15;
  232. double overbarPosition = font.ComputeOverbarVerticalPosition( fontSize.y );
  233. int extra_height = KiROUND( overbarPosition - curr_height );
  234. extra_height += thickness / 2;
  235. textsize.y += extra_height;
  236. rect.Move( wxPoint( 0, -extra_height ) );
  237. }
  238. // for multiline texts and aLine < 0, merge all rectangles
  239. // ( if aLine < 0, we want the full text bounding box )
  240. if( IsMultilineAllowed() && aLine < 0 )
  241. {
  242. for( unsigned ii = 1; ii < strings.GetCount(); ii++ )
  243. {
  244. text = strings.Item( ii );
  245. dx = KiROUND( font.ComputeStringBoundaryLimits( text, fontSize, penWidth ).x );
  246. textsize.x = std::max( textsize.x, dx );
  247. textsize.y += dy;
  248. }
  249. }
  250. rect.SetSize( textsize );
  251. /* Now, calculate the rect origin, according to text justification
  252. * At this point the rectangle origin is the text origin (m_Pos).
  253. * This is true only for left and top text justified texts (using top to bottom Y axis
  254. * orientation). and must be recalculated for others justifications
  255. * also, note the V justification is relative to the first line
  256. */
  257. switch( GetHorizJustify() )
  258. {
  259. case GR_TEXT_HJUSTIFY_LEFT:
  260. if( IsMirrored() )
  261. rect.SetX( rect.GetX() - rect.GetWidth() );
  262. break;
  263. case GR_TEXT_HJUSTIFY_CENTER:
  264. rect.SetX( rect.GetX() - (rect.GetWidth() / 2) );
  265. break;
  266. case GR_TEXT_HJUSTIFY_RIGHT:
  267. if( !IsMirrored() )
  268. rect.SetX( rect.GetX() - rect.GetWidth() );
  269. break;
  270. }
  271. switch( GetVertJustify() )
  272. {
  273. case GR_TEXT_VJUSTIFY_TOP:
  274. break;
  275. case GR_TEXT_VJUSTIFY_CENTER:
  276. rect.SetY( rect.GetY() - ( dy / 2) );
  277. break;
  278. case GR_TEXT_VJUSTIFY_BOTTOM:
  279. rect.SetY( rect.GetY() - dy );
  280. break;
  281. }
  282. if( linecount > 1 )
  283. {
  284. int yoffset;
  285. linecount -= 1;
  286. switch( GetVertJustify() )
  287. {
  288. case GR_TEXT_VJUSTIFY_TOP:
  289. break;
  290. case GR_TEXT_VJUSTIFY_CENTER:
  291. yoffset = linecount * GetInterline() / 2;
  292. rect.SetY( rect.GetY() - yoffset );
  293. break;
  294. case GR_TEXT_VJUSTIFY_BOTTOM:
  295. yoffset = linecount * GetInterline();
  296. rect.SetY( rect.GetY() - yoffset );
  297. break;
  298. }
  299. }
  300. // Many fonts draw diacriticals, descenders, etc. outside the X-height of the font. This
  301. // will cacth most (but probably not all) of them.
  302. rect.Inflate( 0, thickness * 1.5 );
  303. rect.Normalize(); // Make h and v sizes always >= 0
  304. return rect;
  305. }
  306. bool EDA_TEXT::TextHitTest( const wxPoint& aPoint, int aAccuracy ) const
  307. {
  308. EDA_RECT rect = GetTextBox();
  309. wxPoint location = aPoint;
  310. rect.Inflate( aAccuracy );
  311. RotatePoint( &location, GetTextPos(), -GetTextAngle() );
  312. return rect.Contains( location );
  313. }
  314. bool EDA_TEXT::TextHitTest( const EDA_RECT& aRect, bool aContains, int aAccuracy ) const
  315. {
  316. EDA_RECT rect = aRect;
  317. rect.Inflate( aAccuracy );
  318. if( aContains )
  319. return rect.Contains( GetTextBox() );
  320. return rect.Intersects( GetTextBox(), GetTextAngle() );
  321. }
  322. void EDA_TEXT::Print( const RENDER_SETTINGS* aSettings, const wxPoint& aOffset,
  323. const COLOR4D& aColor, OUTLINE_MODE aFillMode )
  324. {
  325. if( IsMultilineAllowed() )
  326. {
  327. std::vector<wxPoint> positions;
  328. wxArrayString strings;
  329. wxStringSplit( GetShownText(), strings, '\n' );
  330. positions.reserve( strings.Count() );
  331. GetLinePositions( positions, strings.Count() );
  332. for( unsigned ii = 0; ii < strings.Count(); ii++ )
  333. printOneLineOfText( aSettings, aOffset, aColor, aFillMode, strings[ii], positions[ii] );
  334. }
  335. else
  336. {
  337. printOneLineOfText( aSettings, aOffset, aColor, aFillMode, GetShownText(), GetTextPos() );
  338. }
  339. }
  340. void EDA_TEXT::GetLinePositions( std::vector<wxPoint>& aPositions, int aLineCount ) const
  341. {
  342. wxPoint pos = GetTextPos(); // Position of first line of the
  343. // multiline text according to
  344. // the center of the multiline text block
  345. wxPoint offset; // Offset to next line.
  346. offset.y = GetInterline();
  347. if( aLineCount > 1 )
  348. {
  349. switch( GetVertJustify() )
  350. {
  351. case GR_TEXT_VJUSTIFY_TOP:
  352. break;
  353. case GR_TEXT_VJUSTIFY_CENTER:
  354. pos.y -= ( aLineCount - 1 ) * offset.y / 2;
  355. break;
  356. case GR_TEXT_VJUSTIFY_BOTTOM:
  357. pos.y -= ( aLineCount - 1 ) * offset.y;
  358. break;
  359. }
  360. }
  361. // Rotate the position of the first line
  362. // around the center of the multiline text block
  363. RotatePoint( &pos, GetTextPos(), GetTextAngle() );
  364. // Rotate the offset lines to increase happened in the right direction
  365. RotatePoint( &offset, GetTextAngle() );
  366. for( int ii = 0; ii < aLineCount; ii++ )
  367. {
  368. aPositions.push_back( pos );
  369. pos += offset;
  370. }
  371. }
  372. void EDA_TEXT::printOneLineOfText( const RENDER_SETTINGS* aSettings, const wxPoint& aOffset,
  373. const COLOR4D& aColor, OUTLINE_MODE aFillMode,
  374. const wxString& aText, const wxPoint &aPos )
  375. {
  376. wxDC* DC = aSettings->GetPrintDC();
  377. int penWidth = std::max( GetEffectiveTextPenWidth(), aSettings->GetDefaultPenWidth() );
  378. if( aFillMode == SKETCH )
  379. penWidth = -penWidth;
  380. wxSize size = GetTextSize();
  381. if( IsMirrored() )
  382. size.x = -size.x;
  383. GRText( DC, aOffset + aPos, aColor, aText, GetTextAngle(), size, GetHorizJustify(),
  384. GetVertJustify(), penWidth, IsItalic(), IsBold() );
  385. }
  386. wxString EDA_TEXT::GetTextStyleName() const
  387. {
  388. int style = 0;
  389. if( IsItalic() )
  390. style = 1;
  391. if( IsBold() )
  392. style += 2;
  393. wxString stylemsg[4] = {
  394. _("Normal"),
  395. _("Italic"),
  396. _("Bold"),
  397. _("Bold+Italic")
  398. };
  399. return stylemsg[style];
  400. }
  401. bool EDA_TEXT::IsDefaultFormatting() const
  402. {
  403. return ( IsVisible()
  404. && !IsMirrored()
  405. && GetHorizJustify() == GR_TEXT_HJUSTIFY_CENTER
  406. && GetVertJustify() == GR_TEXT_VJUSTIFY_CENTER
  407. && GetTextThickness() == 0
  408. && !IsItalic()
  409. && !IsBold()
  410. && !IsMultilineAllowed()
  411. );
  412. }
  413. void EDA_TEXT::Format( OUTPUTFORMATTER* aFormatter, int aNestLevel, int aControlBits ) const
  414. {
  415. #ifndef GERBVIEW // Gerbview does not use EDA_TEXT::Format
  416. // and does not define FormatInternalUnits, used here
  417. // however this function should exist
  418. aFormatter->Print( aNestLevel + 1, "(effects" );
  419. // Text size
  420. aFormatter->Print( 0, " (font" );
  421. aFormatter->Print( 0, " (size %s %s)",
  422. FormatInternalUnits( GetTextHeight() ).c_str(),
  423. FormatInternalUnits( GetTextWidth() ).c_str() );
  424. if( GetTextThickness() )
  425. {
  426. aFormatter->Print( 0, " (thickness %s)",
  427. FormatInternalUnits( GetTextThickness() ).c_str() );
  428. }
  429. if( IsBold() )
  430. aFormatter->Print( 0, " bold" );
  431. if( IsItalic() )
  432. aFormatter->Print( 0, " italic" );
  433. aFormatter->Print( 0, ")"); // (font
  434. if( IsMirrored() || GetHorizJustify() != GR_TEXT_HJUSTIFY_CENTER
  435. || GetVertJustify() != GR_TEXT_VJUSTIFY_CENTER )
  436. {
  437. aFormatter->Print( 0, " (justify");
  438. if( GetHorizJustify() != GR_TEXT_HJUSTIFY_CENTER )
  439. aFormatter->Print( 0, GetHorizJustify() == GR_TEXT_HJUSTIFY_LEFT ? " left" : " right" );
  440. if( GetVertJustify() != GR_TEXT_VJUSTIFY_CENTER )
  441. aFormatter->Print( 0, GetVertJustify() == GR_TEXT_VJUSTIFY_TOP ? " top" : " bottom" );
  442. if( IsMirrored() )
  443. aFormatter->Print( 0, " mirror" );
  444. aFormatter->Print( 0, ")" ); // (justify
  445. }
  446. if( !(aControlBits & CTL_OMIT_HIDE) && !IsVisible() )
  447. aFormatter->Print( 0, " hide" );
  448. aFormatter->Print( 0, ")\n" ); // (justify
  449. #endif
  450. }
  451. // Convert the text shape to a list of segment
  452. // each segment is stored as 2 wxPoints: its starting point and its ending point
  453. // we are using GRText to create the segments and therefore a call-back function is needed
  454. // This is a call back function, used by GRText to put each segment in buffer
  455. static void addTextSegmToBuffer( int x0, int y0, int xf, int yf, void* aData )
  456. {
  457. std::vector<wxPoint>* cornerBuffer = static_cast<std::vector<wxPoint>*>( aData );
  458. cornerBuffer->push_back( wxPoint( x0, y0 ) );
  459. cornerBuffer->push_back( wxPoint( xf, yf ) );
  460. }
  461. std::vector<wxPoint> EDA_TEXT::TransformToSegmentList() const
  462. {
  463. std::vector<wxPoint> cornerBuffer;
  464. wxSize size = GetTextSize();
  465. if( IsMirrored() )
  466. size.x = -size.x;
  467. bool forceBold = true;
  468. int penWidth = 0; // use max-width for bold text
  469. COLOR4D color = COLOR4D::BLACK; // not actually used, but needed by GRText
  470. if( IsMultilineAllowed() )
  471. {
  472. wxArrayString strings_list;
  473. wxStringSplit( GetShownText(), strings_list, wxChar('\n') );
  474. std::vector<wxPoint> positions;
  475. positions.reserve( strings_list.Count() );
  476. GetLinePositions( positions, strings_list.Count() );
  477. for( unsigned ii = 0; ii < strings_list.Count(); ii++ )
  478. {
  479. wxString txt = strings_list.Item( ii );
  480. GRText( nullptr, positions[ii], color, txt, GetDrawRotation(), size,
  481. GetDrawHorizJustify(), GetDrawVertJustify(), penWidth, IsItalic(), forceBold,
  482. addTextSegmToBuffer, &cornerBuffer );
  483. }
  484. }
  485. else
  486. {
  487. GRText( nullptr, GetDrawPos(), color, GetShownText(), GetDrawRotation(), size,
  488. GetDrawHorizJustify(), GetDrawVertJustify(), penWidth, IsItalic(), forceBold,
  489. addTextSegmToBuffer, &cornerBuffer );
  490. }
  491. return cornerBuffer;
  492. }
  493. std::shared_ptr<SHAPE_COMPOUND> EDA_TEXT::GetEffectiveTextShape( ) const
  494. {
  495. std::shared_ptr<SHAPE_COMPOUND> shape = std::make_shared<SHAPE_COMPOUND>();
  496. int penWidth = GetEffectiveTextPenWidth();
  497. std::vector<wxPoint> pts = TransformToSegmentList();
  498. for( unsigned jj = 0; jj < pts.size(); jj += 2 )
  499. shape->AddShape( new SHAPE_SEGMENT( pts[jj], pts[jj+1], penWidth ) );
  500. return shape;
  501. }
  502. int EDA_TEXT::Compare( const EDA_TEXT* aOther ) const
  503. {
  504. #define TEST( a, b ) { if( a != b ) return a - b; }
  505. #define TEST_PT( a, b ) { TEST( a.x, b.x ); TEST( a.y, b.y ); }
  506. TEST_PT( m_e.pos, aOther->m_e.pos );
  507. TEST_PT( m_e.size, aOther->m_e.size );
  508. TEST( m_e.penwidth, aOther->m_e.penwidth );
  509. TEST( m_e.angle, aOther->m_e.angle );
  510. TEST( m_e.hjustify, aOther->m_e.hjustify );
  511. TEST( m_e.vjustify, aOther->m_e.vjustify );
  512. TEST( m_e.bits, aOther->m_e.bits );
  513. return m_text.Cmp( aOther->m_text );
  514. }
  515. void EDA_TEXT::TransformBoundingBoxWithClearanceToPolygon( SHAPE_POLY_SET* aCornerBuffer,
  516. int aClearanceValue ) const
  517. {
  518. if( GetText().Length() == 0 )
  519. return;
  520. wxPoint corners[4]; // Buffer of polygon corners
  521. EDA_RECT rect = GetTextBox();
  522. // This ugly hack is because this code used to be defined in the board polygon code
  523. // file rather than in the EDA_TEXT source file where it belonged. Using the board
  524. // default text width was dubious so this recreates the same code with the exception
  525. // if for some reason a different default text width is require for some other object.
  526. #if !defined( DEFAULT_TEXT_WIDTH )
  527. #define LOCAL_DEFAULT_TEXT_WIDTH
  528. #define DEFAULT_TEXT_WIDTH 0.15
  529. #endif
  530. rect.Inflate( aClearanceValue + Millimeter2iu( DEFAULT_TEXT_WIDTH ) );
  531. #if defined( LOCAL_DEFAULT_TEXT_WIDTH )
  532. #undef DEFAULT_TEXT_WIDTH
  533. #undef LOCAL_DEFAULT_TEXT_WIDTH
  534. #endif
  535. corners[0].x = rect.GetOrigin().x;
  536. corners[0].y = rect.GetOrigin().y;
  537. corners[1].y = corners[0].y;
  538. corners[1].x = rect.GetRight();
  539. corners[2].x = corners[1].x;
  540. corners[2].y = rect.GetBottom();
  541. corners[3].y = corners[2].y;
  542. corners[3].x = corners[0].x;
  543. aCornerBuffer->NewOutline();
  544. for( wxPoint& corner : corners )
  545. {
  546. // Rotate polygon
  547. RotatePoint( &corner.x, &corner.y, GetTextPos().x, GetTextPos().y, GetTextAngle() );
  548. aCornerBuffer->Append( corner.x, corner.y );
  549. }
  550. }
  551. static struct EDA_TEXT_DESC
  552. {
  553. EDA_TEXT_DESC()
  554. {
  555. ENUM_MAP<EDA_TEXT_HJUSTIFY_T>::Instance()
  556. .Map( GR_TEXT_HJUSTIFY_LEFT, _HKI( "Left" ) )
  557. .Map( GR_TEXT_HJUSTIFY_CENTER, _HKI( "Center" ) )
  558. .Map( GR_TEXT_HJUSTIFY_RIGHT, _HKI( "Right" ) );
  559. ENUM_MAP<EDA_TEXT_VJUSTIFY_T>::Instance()
  560. .Map( GR_TEXT_VJUSTIFY_TOP, _HKI( "Top" ) )
  561. .Map( GR_TEXT_VJUSTIFY_CENTER, _HKI( "Center" ) )
  562. .Map( GR_TEXT_VJUSTIFY_BOTTOM, _HKI( "Bottom" ) );
  563. PROPERTY_MANAGER& propMgr = PROPERTY_MANAGER::Instance();
  564. REGISTER_TYPE( EDA_TEXT );
  565. propMgr.AddProperty( new PROPERTY<EDA_TEXT, wxString>( _HKI( "Text" ),
  566. &EDA_TEXT::SetText, &EDA_TEXT::GetText ) );
  567. propMgr.AddProperty( new PROPERTY<EDA_TEXT, int>( _HKI( "Thickness" ),
  568. &EDA_TEXT::SetTextThickness,
  569. &EDA_TEXT::GetTextThickness,
  570. PROPERTY_DISPLAY::DISTANCE ) );
  571. propMgr.AddProperty( new PROPERTY<EDA_TEXT, bool>( _HKI( "Italic" ),
  572. &EDA_TEXT::SetItalic,
  573. &EDA_TEXT::IsItalic ) );
  574. propMgr.AddProperty( new PROPERTY<EDA_TEXT, bool>( _HKI( "Bold" ),
  575. &EDA_TEXT::SetBold, &EDA_TEXT::IsBold ) );
  576. propMgr.AddProperty( new PROPERTY<EDA_TEXT, bool>( _HKI( "Mirrored" ),
  577. &EDA_TEXT::SetMirrored,
  578. &EDA_TEXT::IsMirrored ) );
  579. propMgr.AddProperty( new PROPERTY<EDA_TEXT, bool>( _HKI( "Visible" ),
  580. &EDA_TEXT::SetVisible,
  581. &EDA_TEXT::IsVisible ) );
  582. propMgr.AddProperty( new PROPERTY<EDA_TEXT, int>( _HKI( "Width" ),
  583. &EDA_TEXT::SetTextWidth,
  584. &EDA_TEXT::GetTextWidth,
  585. PROPERTY_DISPLAY::DISTANCE ) );
  586. propMgr.AddProperty( new PROPERTY<EDA_TEXT, int>( _HKI( "Height" ),
  587. &EDA_TEXT::SetTextHeight,
  588. &EDA_TEXT::GetTextHeight,
  589. PROPERTY_DISPLAY::DISTANCE ) );
  590. propMgr.AddProperty( new PROPERTY_ENUM<EDA_TEXT,
  591. EDA_TEXT_HJUSTIFY_T>( _HKI( "Horizontal Justification" ),
  592. &EDA_TEXT::SetHorizJustify,
  593. &EDA_TEXT::GetHorizJustify ) );
  594. propMgr.AddProperty( new PROPERTY_ENUM<EDA_TEXT,
  595. EDA_TEXT_VJUSTIFY_T>( _HKI( "Vertical Justification" ),
  596. &EDA_TEXT::SetVertJustify,
  597. &EDA_TEXT::GetVertJustify ) );
  598. }
  599. } _EDA_TEXT_DESC;
  600. ENUM_TO_WXANY( EDA_TEXT_HJUSTIFY_T )
  601. ENUM_TO_WXANY( EDA_TEXT_VJUSTIFY_T )