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.

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