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.

612 lines
18 KiB

  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-2017 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 <base_struct.h> // for EDA_ITEM
  33. #include <base_units.h>
  34. #include <basic_gal.h> // for BASIC_GAL, basic_gal
  35. #include <common.h> // for wxStringSplit
  36. #include <convert_to_biu.h> // for Mils2iu
  37. #include <core/typeinfo.h> // for KICAD_T, SCH_LABEL_T, SCH_TEXT_T, SCH_G...
  38. #include <eda_rect.h> // for EDA_RECT
  39. #include <eda_text.h> // for EDA_TEXT, TEXT_EFFECTS, GR_TEXT_VJUSTIF...
  40. #include <gal/color4d.h> // for COLOR4D, COLOR4D::BLACK
  41. #include <gal/stroke_font.h> // for STROKE_FONT
  42. #include <gr_text.h> // for GRText
  43. #include <kicad_string.h> // for UnescapeString
  44. #include <math/util.h> // for KiROUND
  45. #include <math/vector2d.h> // for VECTOR2D
  46. #include <trigo.h> // for RotatePoint
  47. #include <wx/debug.h> // for wxASSERT
  48. #include <wx/wx.h> // for wxPoint, wxString, wxArrayString, wxSize
  49. class OUTPUTFORMATTER;
  50. class wxFindReplaceData;
  51. EDA_TEXT_HJUSTIFY_T EDA_TEXT::MapHorizJustify( int aHorizJustify )
  52. {
  53. wxASSERT( aHorizJustify >= GR_TEXT_HJUSTIFY_LEFT && aHorizJustify <= GR_TEXT_HJUSTIFY_RIGHT );
  54. if( aHorizJustify > GR_TEXT_HJUSTIFY_RIGHT )
  55. return GR_TEXT_HJUSTIFY_RIGHT;
  56. if( aHorizJustify < GR_TEXT_HJUSTIFY_LEFT )
  57. return GR_TEXT_HJUSTIFY_LEFT;
  58. return (EDA_TEXT_HJUSTIFY_T) aHorizJustify;
  59. }
  60. EDA_TEXT_VJUSTIFY_T EDA_TEXT::MapVertJustify( int aVertJustify )
  61. {
  62. wxASSERT( aVertJustify >= GR_TEXT_VJUSTIFY_TOP && aVertJustify <= GR_TEXT_VJUSTIFY_BOTTOM );
  63. if( aVertJustify > GR_TEXT_VJUSTIFY_BOTTOM )
  64. return GR_TEXT_VJUSTIFY_BOTTOM;
  65. if( aVertJustify < GR_TEXT_VJUSTIFY_TOP )
  66. return GR_TEXT_VJUSTIFY_TOP;
  67. return (EDA_TEXT_VJUSTIFY_T) aVertJustify;
  68. }
  69. EDA_TEXT::EDA_TEXT( const wxString& text ) :
  70. m_text( text ),
  71. m_e( 1<<TE_VISIBLE )
  72. {
  73. int sz = Mils2iu( DEFAULT_SIZE_TEXT );
  74. SetTextSize( wxSize( sz, sz ) );
  75. m_shown_text_has_text_var_refs = false;
  76. if( !text.IsEmpty() )
  77. {
  78. m_shown_text = UnescapeString( text );
  79. m_shown_text_has_text_var_refs = m_shown_text.Contains( wxT( "${" ) );
  80. }
  81. }
  82. EDA_TEXT::EDA_TEXT( const EDA_TEXT& aText ) :
  83. m_text( aText.m_text ),
  84. m_e( aText.m_e )
  85. {
  86. m_shown_text = UnescapeString( m_text );
  87. m_shown_text_has_text_var_refs = m_shown_text.Contains( wxT( "${" ) );
  88. }
  89. EDA_TEXT::~EDA_TEXT()
  90. {
  91. }
  92. void EDA_TEXT::SetText( const wxString& aText )
  93. {
  94. m_text = aText;
  95. m_shown_text = UnescapeString( aText );
  96. m_shown_text_has_text_var_refs = m_shown_text.Contains( wxT( "${" ) );
  97. }
  98. void EDA_TEXT::CopyText( const EDA_TEXT& aSrc )
  99. {
  100. m_text = aSrc.m_text;
  101. m_shown_text = aSrc.m_shown_text;
  102. m_shown_text_has_text_var_refs = aSrc.m_shown_text_has_text_var_refs;
  103. }
  104. void EDA_TEXT::SetEffects( const EDA_TEXT& aSrc )
  105. {
  106. m_e = aSrc.m_e;
  107. }
  108. void EDA_TEXT::SwapText( EDA_TEXT& aTradingPartner )
  109. {
  110. std::swap( m_text, aTradingPartner.m_text );
  111. std::swap( m_shown_text, aTradingPartner.m_shown_text );
  112. std::swap( m_shown_text_has_text_var_refs, aTradingPartner.m_shown_text_has_text_var_refs );
  113. }
  114. void EDA_TEXT::SwapEffects( EDA_TEXT& aTradingPartner )
  115. {
  116. std::swap( m_e, aTradingPartner.m_e );
  117. }
  118. int EDA_TEXT::GetEffectiveTextPenWidth( int aDefaultWidth ) const
  119. {
  120. int width = GetTextThickness();
  121. if( width <= 1 )
  122. {
  123. width = aDefaultWidth;
  124. if( IsBold() )
  125. width = GetPenSizeForBold( GetTextWidth() );
  126. else if( width <= 1 )
  127. width = GetPenSizeForNormal( GetTextWidth() );
  128. }
  129. // Clip pen size for small texts:
  130. width = Clamp_Text_PenSize( width, GetTextSize(), ALLOW_BOLD_THICKNESS );
  131. return width;
  132. }
  133. bool EDA_TEXT::Replace( wxFindReplaceData& aSearchData )
  134. {
  135. bool retval = EDA_ITEM::Replace( aSearchData, m_text );
  136. m_shown_text = UnescapeString( m_text );
  137. m_shown_text_has_text_var_refs = m_shown_text.Contains( wxT( "${" ) );
  138. return retval;
  139. }
  140. int EDA_TEXT::LenSize( const wxString& aLine, int aThickness ) const
  141. {
  142. basic_gal.SetFontItalic( IsItalic() );
  143. basic_gal.SetFontBold( IsBold() );
  144. basic_gal.SetLineWidth( (float) aThickness );
  145. basic_gal.SetGlyphSize( VECTOR2D( GetTextSize() ) );
  146. VECTOR2D tsize = basic_gal.GetTextLineSize( aLine );
  147. return KiROUND( tsize.x );
  148. }
  149. wxString EDA_TEXT::ShortenedShownText() const
  150. {
  151. wxString tmp = GetShownText();
  152. tmp.Replace( wxT( "\n" ), wxT( " " ) );
  153. tmp.Replace( wxT( "\r" ), wxT( " " ) );
  154. tmp.Replace( wxT( "\t" ), wxT( " " ) );
  155. if( tmp.Length() > 36 )
  156. tmp = tmp.Left( 34 ) + wxT( "..." );
  157. return tmp;
  158. }
  159. int EDA_TEXT::GetInterline() const
  160. {
  161. return KiROUND( KIGFX::STROKE_FONT::GetInterline( GetTextHeight() ) );
  162. }
  163. EDA_RECT EDA_TEXT::GetTextBox( int aLine, bool aInvertY ) const
  164. {
  165. EDA_RECT rect;
  166. wxArrayString strings;
  167. wxString text = GetShownText();
  168. int thickness = GetEffectiveTextPenWidth();
  169. int linecount = 1;
  170. bool hasOverBar = false; // true if the first line of text as an overbar
  171. if( IsMultilineAllowed() )
  172. {
  173. wxStringSplit( text, strings, '\n' );
  174. if( strings.GetCount() ) // GetCount() == 0 for void strings
  175. {
  176. if( aLine >= 0 && (aLine < (int)strings.GetCount()) )
  177. text = strings.Item( aLine );
  178. else
  179. text = strings.Item( 0 );
  180. linecount = strings.GetCount();
  181. }
  182. }
  183. // Search for overbar symbol. Only text is scanned,
  184. // because only this line can change the bounding box
  185. for( unsigned ii = 1; ii < text.size(); ii++ )
  186. {
  187. if( text[ii-1] == '~' && text[ii] != '~' )
  188. {
  189. hasOverBar = true;
  190. break;
  191. }
  192. }
  193. // calculate the H and V size
  194. const auto& font = basic_gal.GetStrokeFont();
  195. VECTOR2D fontSize( GetTextSize() );
  196. double penWidth( thickness );
  197. int dx = KiROUND( font.ComputeStringBoundaryLimits( text, fontSize, penWidth ).x );
  198. int dy = GetInterline();
  199. // Creates bounding box (rectangle) for horizontal, left and top justified text. The
  200. // bounding box will be moved later according to the actual text options
  201. wxSize textsize = wxSize( dx, dy );
  202. wxPoint pos = GetTextPos();
  203. if( aInvertY )
  204. pos.y = -pos.y;
  205. rect.SetOrigin( pos );
  206. // The bbox vertical size returned by GetInterline( aThickness )
  207. // includes letters like j and y and ] + interval between lines.
  208. // The interval below the last line is not usefull, and we can use its half value
  209. // as vertical margin above the text
  210. // the full interval is roughly GetTextHeight() * 0.4 - aThickness/2
  211. rect.Move( wxPoint( 0, thickness/4 - KiROUND( GetTextHeight() * 0.22 ) ) );
  212. if( hasOverBar )
  213. { // A overbar adds an extra size to the text
  214. // Height from the base line text of chars like [ or {
  215. double curr_height = GetTextHeight() * 1.15;
  216. double overbarPosition = font.ComputeOverbarVerticalPosition( fontSize.y, penWidth );
  217. int extra_height = KiROUND( overbarPosition - curr_height );
  218. extra_height += thickness / 2;
  219. textsize.y += extra_height;
  220. rect.Move( wxPoint( 0, -extra_height ) );
  221. }
  222. // for multiline texts and aLine < 0, merge all rectangles
  223. // ( if aLine < 0, we want the full text bounding box )
  224. if( IsMultilineAllowed() && aLine < 0 )
  225. {
  226. for( unsigned ii = 1; ii < strings.GetCount(); ii++ )
  227. {
  228. text = strings.Item( ii );
  229. dx = KiROUND( font.ComputeStringBoundaryLimits( text, fontSize, penWidth ).x );
  230. textsize.x = std::max( textsize.x, dx );
  231. textsize.y += dy;
  232. }
  233. }
  234. rect.SetSize( textsize );
  235. /* Now, calculate the rect origin, according to text justification
  236. * At this point the rectangle origin is the text origin (m_Pos).
  237. * This is true only for left and top text justified texts (using top to bottom Y axis
  238. * orientation). and must be recalculated for others justifications
  239. * also, note the V justification is relative to the first line
  240. */
  241. switch( GetHorizJustify() )
  242. {
  243. case GR_TEXT_HJUSTIFY_LEFT:
  244. if( IsMirrored() )
  245. rect.SetX( rect.GetX() - rect.GetWidth() );
  246. break;
  247. case GR_TEXT_HJUSTIFY_CENTER:
  248. rect.SetX( rect.GetX() - (rect.GetWidth() / 2) );
  249. break;
  250. case GR_TEXT_HJUSTIFY_RIGHT:
  251. if( !IsMirrored() )
  252. rect.SetX( rect.GetX() - rect.GetWidth() );
  253. break;
  254. }
  255. dy = GetTextHeight() + thickness;
  256. switch( GetVertJustify() )
  257. {
  258. case GR_TEXT_VJUSTIFY_TOP:
  259. break;
  260. case GR_TEXT_VJUSTIFY_CENTER:
  261. rect.SetY( rect.GetY() - ( dy / 2) );
  262. break;
  263. case GR_TEXT_VJUSTIFY_BOTTOM:
  264. rect.SetY( rect.GetY() - dy );
  265. break;
  266. }
  267. if( linecount > 1 )
  268. {
  269. int yoffset;
  270. linecount -= 1;
  271. switch( GetVertJustify() )
  272. {
  273. case GR_TEXT_VJUSTIFY_TOP:
  274. break;
  275. case GR_TEXT_VJUSTIFY_CENTER:
  276. yoffset = linecount * GetInterline() / 2;
  277. rect.SetY( rect.GetY() - yoffset );
  278. break;
  279. case GR_TEXT_VJUSTIFY_BOTTOM:
  280. yoffset = linecount * GetInterline();
  281. rect.SetY( rect.GetY() - yoffset );
  282. break;
  283. }
  284. }
  285. rect.Normalize(); // Make h and v sizes always >= 0
  286. return rect;
  287. }
  288. bool EDA_TEXT::TextHitTest( const wxPoint& aPoint, int aAccuracy ) const
  289. {
  290. EDA_RECT rect = GetTextBox();
  291. wxPoint location = aPoint;
  292. rect.Inflate( aAccuracy );
  293. RotatePoint( &location, GetTextPos(), -GetTextAngle() );
  294. return rect.Contains( location );
  295. }
  296. bool EDA_TEXT::TextHitTest( const EDA_RECT& aRect, bool aContains, int aAccuracy ) const
  297. {
  298. EDA_RECT rect = aRect;
  299. rect.Inflate( aAccuracy );
  300. if( aContains )
  301. return rect.Contains( GetTextBox() );
  302. return rect.Intersects( GetTextBox(), GetTextAngle() );
  303. }
  304. void EDA_TEXT::Print( RENDER_SETTINGS* aSettings, const wxPoint& aOffset, COLOR4D aColor,
  305. EDA_DRAW_MODE_T aFillMode )
  306. {
  307. if( IsMultilineAllowed() )
  308. {
  309. std::vector<wxPoint> positions;
  310. wxArrayString strings;
  311. wxStringSplit( GetShownText(), strings, '\n' );
  312. positions.reserve( strings.Count() );
  313. GetLinePositions( positions, strings.Count());
  314. for( unsigned ii = 0; ii < strings.Count(); ii++ )
  315. printOneLineOfText( aSettings, aOffset, aColor, aFillMode, strings[ii], positions[ii] );
  316. }
  317. else
  318. {
  319. printOneLineOfText( aSettings, aOffset, aColor, aFillMode, GetShownText(), GetTextPos() );
  320. }
  321. }
  322. void EDA_TEXT::GetLinePositions( std::vector<wxPoint>& aPositions, int aLineCount ) const
  323. {
  324. wxPoint pos = GetTextPos(); // Position of first line of the
  325. // multiline text according to
  326. // the center of the multiline text block
  327. wxPoint offset; // Offset to next line.
  328. offset.y = GetInterline();
  329. if( aLineCount > 1 )
  330. {
  331. switch( GetVertJustify() )
  332. {
  333. case GR_TEXT_VJUSTIFY_TOP:
  334. break;
  335. case GR_TEXT_VJUSTIFY_CENTER:
  336. pos.y -= ( aLineCount - 1 ) * offset.y / 2;
  337. break;
  338. case GR_TEXT_VJUSTIFY_BOTTOM:
  339. pos.y -= ( aLineCount - 1 ) * offset.y;
  340. break;
  341. }
  342. }
  343. // Rotate the position of the first line
  344. // around the center of the multiline text block
  345. RotatePoint( &pos, GetTextPos(), GetTextAngle() );
  346. // Rotate the offset lines to increase happened in the right direction
  347. RotatePoint( &offset, GetTextAngle() );
  348. for( int ii = 0; ii < aLineCount; ii++ )
  349. {
  350. aPositions.push_back( pos );
  351. pos += offset;
  352. }
  353. }
  354. void EDA_TEXT::printOneLineOfText( RENDER_SETTINGS* aSettings, const wxPoint& aOffset,
  355. COLOR4D aColor, EDA_DRAW_MODE_T aFillMode,
  356. const wxString& aText, const wxPoint &aPos )
  357. {
  358. wxDC* DC = aSettings->GetPrintDC();
  359. int penWidth = std::max( GetEffectiveTextPenWidth(), aSettings->GetDefaultPenWidth() );
  360. if( aFillMode == SKETCH )
  361. penWidth = -penWidth;
  362. wxSize size = GetTextSize();
  363. if( IsMirrored() )
  364. size.x = -size.x;
  365. GRText( DC, aOffset + aPos, aColor, aText, GetTextAngle(), size, GetHorizJustify(),
  366. GetVertJustify(), penWidth, IsItalic(), IsBold() );
  367. }
  368. wxString EDA_TEXT::GetTextStyleName()
  369. {
  370. int style = 0;
  371. if( IsItalic() )
  372. style = 1;
  373. if( IsBold() )
  374. style += 2;
  375. wxString stylemsg[4] = {
  376. _("Normal"),
  377. _("Italic"),
  378. _("Bold"),
  379. _("Bold+Italic")
  380. };
  381. return stylemsg[style];
  382. }
  383. bool EDA_TEXT::IsDefaultFormatting() const
  384. {
  385. return ( IsVisible()
  386. && !IsMirrored()
  387. && GetHorizJustify() == GR_TEXT_HJUSTIFY_CENTER
  388. && GetVertJustify() == GR_TEXT_VJUSTIFY_CENTER
  389. && GetTextThickness() == 0
  390. && !IsItalic()
  391. && !IsBold()
  392. && !IsMultilineAllowed()
  393. );
  394. }
  395. void EDA_TEXT::Format( OUTPUTFORMATTER* aFormatter, int aNestLevel, int aControlBits ) const
  396. {
  397. #ifndef GERBVIEW // Gerbview does not use EDA_TEXT::Format
  398. // and does not define FormatInternalUnits, used here
  399. // however this function should exist
  400. aFormatter->Print( aNestLevel + 1, "(effects" );
  401. // Text size
  402. aFormatter->Print( 0, " (font" );
  403. aFormatter->Print( 0, " (size %s %s)",
  404. FormatInternalUnits( GetTextHeight() ).c_str(),
  405. FormatInternalUnits( GetTextWidth() ).c_str() );
  406. if( GetTextThickness() )
  407. aFormatter->Print( 0, " (thickness %s)", FormatInternalUnits( GetTextThickness() ).c_str() );
  408. if( IsBold() )
  409. aFormatter->Print( 0, " bold" );
  410. if( IsItalic() )
  411. aFormatter->Print( 0, " italic" );
  412. aFormatter->Print( 0, ")"); // (font
  413. if( IsMirrored() ||
  414. GetHorizJustify() != GR_TEXT_HJUSTIFY_CENTER ||
  415. GetVertJustify() != GR_TEXT_VJUSTIFY_CENTER )
  416. {
  417. aFormatter->Print( 0, " (justify");
  418. if( GetHorizJustify() != GR_TEXT_HJUSTIFY_CENTER )
  419. aFormatter->Print( 0, (GetHorizJustify() == GR_TEXT_HJUSTIFY_LEFT) ? " left" : " right" );
  420. if( GetVertJustify() != GR_TEXT_VJUSTIFY_CENTER )
  421. aFormatter->Print( 0, (GetVertJustify() == GR_TEXT_VJUSTIFY_TOP) ? " top" : " bottom" );
  422. if( IsMirrored() )
  423. aFormatter->Print( 0, " mirror" );
  424. aFormatter->Print( 0, ")" ); // (justify
  425. }
  426. if( !(aControlBits & CTL_OMIT_HIDE) && !IsVisible() )
  427. aFormatter->Print( 0, " hide" );
  428. aFormatter->Print( 0, ")\n" ); // (justify
  429. #endif
  430. }
  431. // Convert the text shape to a list of segment
  432. // each segment is stored as 2 wxPoints: its starting point and its ending point
  433. // we are using GRText to create the segments and therefore a call-back function is needed
  434. // This is a call back function, used by GRText to put each segment in buffer
  435. static void addTextSegmToBuffer( int x0, int y0, int xf, int yf, void* aData )
  436. {
  437. std::vector<wxPoint>* cornerBuffer = static_cast<std::vector<wxPoint>*>( aData );
  438. cornerBuffer->push_back( wxPoint( x0, y0 ) );
  439. cornerBuffer->push_back( wxPoint( xf, yf ) );
  440. }
  441. void EDA_TEXT::TransformTextShapeToSegmentList( std::vector<wxPoint>& aCornerBuffer ) const
  442. {
  443. wxSize size = GetTextSize();
  444. if( IsMirrored() )
  445. size.x = -size.x;
  446. bool forceBold = true;
  447. int penWidth = 0; // use max-width for bold text
  448. COLOR4D color = COLOR4D::BLACK; // not actually used, but needed by GRText
  449. if( IsMultilineAllowed() )
  450. {
  451. wxArrayString strings_list;
  452. wxStringSplit( GetShownText(), strings_list, wxChar('\n') );
  453. std::vector<wxPoint> positions;
  454. positions.reserve( strings_list.Count() );
  455. GetLinePositions( positions, strings_list.Count());
  456. for( unsigned ii = 0; ii < strings_list.Count(); ii++ )
  457. {
  458. wxString txt = strings_list.Item( ii );
  459. GRText( NULL, positions[ii], color, txt, GetTextAngle(), size, GetHorizJustify(),
  460. GetVertJustify(), penWidth, IsItalic(), forceBold, addTextSegmToBuffer,
  461. &aCornerBuffer );
  462. }
  463. }
  464. else
  465. {
  466. GRText( NULL, GetTextPos(), color, GetText(), GetTextAngle(), size, GetHorizJustify(),
  467. GetVertJustify(), penWidth, IsItalic(), forceBold, addTextSegmToBuffer,
  468. &aCornerBuffer );
  469. }
  470. }