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.

1267 lines
36 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
2 years ago
2 years ago
2 years ago
4 years ago
4 years ago
2 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-2023 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. #include <algorithm> // for max
  25. #include <stddef.h> // for NULL
  26. #include <type_traits> // for swap
  27. #include <vector>
  28. #include <eda_item.h>
  29. #include <base_units.h>
  30. #include <callback_gal.h>
  31. #include <eda_text.h> // for EDA_TEXT, TEXT_EFFECTS, GR_TEXT_VJUSTIF...
  32. #include <gal/color4d.h> // for COLOR4D, COLOR4D::BLACK
  33. #include <font/glyph.h>
  34. #include <gr_text.h>
  35. #include <string_utils.h> // for UnescapeString
  36. #include <math/util.h> // for KiROUND
  37. #include <math/vector2d.h>
  38. #include <core/kicad_algo.h>
  39. #include <richio.h>
  40. #include <render_settings.h>
  41. #include <trigo.h> // for RotatePoint
  42. #include <i18n_utility.h>
  43. #include <geometry/shape_segment.h>
  44. #include <geometry/shape_compound.h>
  45. #include <geometry/shape_simple.h>
  46. #include <font/outline_font.h>
  47. #include <geometry/shape_poly_set.h>
  48. #include <properties/property_validators.h>
  49. #include <wx/debug.h> // for wxASSERT
  50. #include <wx/string.h>
  51. #include <wx/url.h> // for wxURL
  52. #include "font/kicad_font_name.h"
  53. #include "font/fontconfig.h"
  54. #include "pgm_base.h"
  55. class OUTPUTFORMATTER;
  56. class wxFindReplaceData;
  57. GR_TEXT_H_ALIGN_T EDA_TEXT::MapHorizJustify( int aHorizJustify )
  58. {
  59. wxASSERT( aHorizJustify >= GR_TEXT_H_ALIGN_LEFT && aHorizJustify <= GR_TEXT_H_ALIGN_RIGHT );
  60. if( aHorizJustify > GR_TEXT_H_ALIGN_RIGHT )
  61. return GR_TEXT_H_ALIGN_RIGHT;
  62. if( aHorizJustify < GR_TEXT_H_ALIGN_LEFT )
  63. return GR_TEXT_H_ALIGN_LEFT;
  64. return static_cast<GR_TEXT_H_ALIGN_T>( aHorizJustify );
  65. }
  66. GR_TEXT_V_ALIGN_T EDA_TEXT::MapVertJustify( int aVertJustify )
  67. {
  68. wxASSERT( aVertJustify >= GR_TEXT_V_ALIGN_TOP && aVertJustify <= GR_TEXT_V_ALIGN_BOTTOM );
  69. if( aVertJustify > GR_TEXT_V_ALIGN_BOTTOM )
  70. return GR_TEXT_V_ALIGN_BOTTOM;
  71. if( aVertJustify < GR_TEXT_V_ALIGN_TOP )
  72. return GR_TEXT_V_ALIGN_TOP;
  73. return static_cast<GR_TEXT_V_ALIGN_T>( aVertJustify );
  74. }
  75. EDA_TEXT::EDA_TEXT( const EDA_IU_SCALE& aIuScale, const wxString& aText ) :
  76. m_text( aText ),
  77. m_IuScale( aIuScale ),
  78. m_render_cache_font( nullptr ),
  79. m_bounding_box_cache_valid( false ),
  80. m_bounding_box_cache_line( -1 ),
  81. m_bounding_box_cache_inverted( false )
  82. {
  83. SetTextSize( VECTOR2I( EDA_UNIT_UTILS::Mils2IU( m_IuScale, DEFAULT_SIZE_TEXT ),
  84. EDA_UNIT_UTILS::Mils2IU( m_IuScale, DEFAULT_SIZE_TEXT ) ) );
  85. if( m_text.IsEmpty() )
  86. {
  87. m_shown_text = wxEmptyString;
  88. m_shown_text_has_text_var_refs = false;
  89. }
  90. else
  91. {
  92. m_shown_text = UnescapeString( m_text );
  93. m_shown_text_has_text_var_refs = m_shown_text.Contains( wxT( "${" ) );
  94. }
  95. }
  96. EDA_TEXT::EDA_TEXT( const EDA_TEXT& aText ) :
  97. m_IuScale( aText.m_IuScale )
  98. {
  99. m_text = aText.m_text;
  100. m_shown_text = aText.m_shown_text;
  101. m_shown_text_has_text_var_refs = aText.m_shown_text_has_text_var_refs;
  102. m_attributes = aText.m_attributes;
  103. m_pos = aText.m_pos;
  104. m_render_cache_font = aText.m_render_cache_font;
  105. m_render_cache_text = aText.m_render_cache_text;
  106. m_render_cache_angle = aText.m_render_cache_angle;
  107. m_render_cache_offset = aText.m_render_cache_offset;
  108. m_render_cache.clear();
  109. for( const std::unique_ptr<KIFONT::GLYPH>& glyph : aText.m_render_cache )
  110. {
  111. if( KIFONT::OUTLINE_GLYPH* outline = dynamic_cast<KIFONT::OUTLINE_GLYPH*>( glyph.get() ) )
  112. m_render_cache.emplace_back( std::make_unique<KIFONT::OUTLINE_GLYPH>( *outline ) );
  113. else if( KIFONT::STROKE_GLYPH* stroke = dynamic_cast<KIFONT::STROKE_GLYPH*>( glyph.get() ) )
  114. m_render_cache.emplace_back( std::make_unique<KIFONT::STROKE_GLYPH>( *stroke ) );
  115. }
  116. m_bounding_box_cache_valid = aText.m_bounding_box_cache_valid;
  117. m_bounding_box_cache = aText.m_bounding_box_cache;
  118. m_bounding_box_cache_line = aText.m_bounding_box_cache_line;
  119. m_bounding_box_cache_inverted = aText.m_bounding_box_cache_inverted;
  120. }
  121. EDA_TEXT::~EDA_TEXT()
  122. {
  123. }
  124. EDA_TEXT& EDA_TEXT::operator=( const EDA_TEXT& aText )
  125. {
  126. m_text = aText.m_text;
  127. m_shown_text = aText.m_shown_text;
  128. m_shown_text_has_text_var_refs = aText.m_shown_text_has_text_var_refs;
  129. m_attributes = aText.m_attributes;
  130. m_pos = aText.m_pos;
  131. m_render_cache_font = aText.m_render_cache_font;
  132. m_render_cache_text = aText.m_render_cache_text;
  133. m_render_cache_angle = aText.m_render_cache_angle;
  134. m_render_cache_offset = aText.m_render_cache_offset;
  135. m_render_cache.clear();
  136. for( const std::unique_ptr<KIFONT::GLYPH>& glyph : aText.m_render_cache )
  137. {
  138. if( KIFONT::OUTLINE_GLYPH* outline = dynamic_cast<KIFONT::OUTLINE_GLYPH*>( glyph.get() ) )
  139. m_render_cache.emplace_back( std::make_unique<KIFONT::OUTLINE_GLYPH>( *outline ) );
  140. else if( KIFONT::STROKE_GLYPH* stroke = dynamic_cast<KIFONT::STROKE_GLYPH*>( glyph.get() ) )
  141. m_render_cache.emplace_back( std::make_unique<KIFONT::STROKE_GLYPH>( *stroke ) );
  142. }
  143. m_bounding_box_cache_valid = aText.m_bounding_box_cache_valid;
  144. m_bounding_box_cache = aText.m_bounding_box_cache;
  145. return *this;
  146. }
  147. void EDA_TEXT::SetText( const wxString& aText )
  148. {
  149. m_text = aText;
  150. cacheShownText();
  151. }
  152. void EDA_TEXT::CopyText( const EDA_TEXT& aSrc )
  153. {
  154. m_text = aSrc.m_text;
  155. cacheShownText();
  156. }
  157. void EDA_TEXT::SetTextThickness( int aWidth )
  158. {
  159. m_attributes.m_StrokeWidth = aWidth;
  160. ClearRenderCache();
  161. m_bounding_box_cache_valid = false;
  162. }
  163. void EDA_TEXT::SetTextAngle( const EDA_ANGLE& aAngle )
  164. {
  165. m_attributes.m_Angle = aAngle;
  166. ClearRenderCache();
  167. m_bounding_box_cache_valid = false;
  168. }
  169. void EDA_TEXT::SetItalic( bool aItalic )
  170. {
  171. m_attributes.m_Italic = aItalic;
  172. ClearRenderCache();
  173. m_bounding_box_cache_valid = false;
  174. }
  175. void EDA_TEXT::SetBold( bool aBold )
  176. {
  177. if( m_attributes.m_Bold != aBold )
  178. {
  179. int size = std::min( m_attributes.m_Size.x, m_attributes.m_Size.y );
  180. if( aBold )
  181. m_attributes.m_StrokeWidth = GetPenSizeForBold( size );
  182. else
  183. m_attributes.m_StrokeWidth = GetPenSizeForNormal( size );
  184. }
  185. SetBoldFlag( aBold );
  186. }
  187. void EDA_TEXT::SetBoldFlag( bool aBold )
  188. {
  189. m_attributes.m_Bold = aBold;
  190. ClearRenderCache();
  191. m_bounding_box_cache_valid = false;
  192. }
  193. void EDA_TEXT::SetVisible( bool aVisible )
  194. {
  195. m_attributes.m_Visible = aVisible;
  196. ClearRenderCache();
  197. }
  198. void EDA_TEXT::SetMirrored( bool isMirrored )
  199. {
  200. m_attributes.m_Mirrored = isMirrored;
  201. ClearRenderCache();
  202. m_bounding_box_cache_valid = false;
  203. }
  204. void EDA_TEXT::SetMultilineAllowed( bool aAllow )
  205. {
  206. m_attributes.m_Multiline = aAllow;
  207. ClearRenderCache();
  208. m_bounding_box_cache_valid = false;
  209. }
  210. void EDA_TEXT::SetHorizJustify( GR_TEXT_H_ALIGN_T aType )
  211. {
  212. m_attributes.m_Halign = aType;
  213. ClearRenderCache();
  214. m_bounding_box_cache_valid = false;
  215. }
  216. void EDA_TEXT::SetVertJustify( GR_TEXT_V_ALIGN_T aType )
  217. {
  218. m_attributes.m_Valign = aType;
  219. ClearRenderCache();
  220. m_bounding_box_cache_valid = false;
  221. }
  222. void EDA_TEXT::SetKeepUpright( bool aKeepUpright )
  223. {
  224. m_attributes.m_KeepUpright = aKeepUpright;
  225. ClearRenderCache();
  226. m_bounding_box_cache_valid = false;
  227. }
  228. void EDA_TEXT::SetAttributes( const EDA_TEXT& aSrc, bool aSetPosition )
  229. {
  230. m_attributes = aSrc.m_attributes;
  231. if( aSetPosition )
  232. m_pos = aSrc.m_pos;
  233. ClearRenderCache();
  234. m_bounding_box_cache_valid = false;
  235. }
  236. void EDA_TEXT::SwapText( EDA_TEXT& aTradingPartner )
  237. {
  238. std::swap( m_text, aTradingPartner.m_text );
  239. cacheShownText();
  240. }
  241. void EDA_TEXT::SwapAttributes( EDA_TEXT& aTradingPartner )
  242. {
  243. std::swap( m_attributes, aTradingPartner.m_attributes );
  244. std::swap( m_pos, aTradingPartner.m_pos );
  245. ClearRenderCache();
  246. aTradingPartner.ClearRenderCache();
  247. m_bounding_box_cache_valid = false;
  248. aTradingPartner.m_bounding_box_cache_valid = false;
  249. }
  250. int EDA_TEXT::GetEffectiveTextPenWidth( int aDefaultPenWidth ) const
  251. {
  252. int penWidth = GetTextThickness();
  253. if( penWidth <= 1 )
  254. {
  255. penWidth = aDefaultPenWidth;
  256. if( IsBold() )
  257. penWidth = GetPenSizeForBold( GetTextWidth() );
  258. else if( penWidth <= 1 )
  259. penWidth = GetPenSizeForNormal( GetTextWidth() );
  260. }
  261. // Clip pen size for small texts:
  262. penWidth = Clamp_Text_PenSize( penWidth, GetTextSize() );
  263. return penWidth;
  264. }
  265. bool EDA_TEXT::Replace( const EDA_SEARCH_DATA& aSearchData )
  266. {
  267. bool retval = EDA_ITEM::Replace( aSearchData, m_text );
  268. cacheShownText();
  269. ClearRenderCache();
  270. m_bounding_box_cache_valid = false;
  271. return retval;
  272. }
  273. void EDA_TEXT::SetFont( KIFONT::FONT* aFont )
  274. {
  275. m_attributes.m_Font = aFont;
  276. ClearRenderCache();
  277. m_bounding_box_cache_valid = false;
  278. }
  279. void EDA_TEXT::SetLineSpacing( double aLineSpacing )
  280. {
  281. m_attributes.m_LineSpacing = aLineSpacing;
  282. ClearRenderCache();
  283. m_bounding_box_cache_valid = false;
  284. }
  285. void EDA_TEXT::SetTextSize( VECTOR2I aNewSize, bool aEnforceMinTextSize )
  286. {
  287. // Plotting uses unityScale and independently scales the text. If we clamp here we'll
  288. // clamp to *really* small values.
  289. if( m_IuScale.get().IU_PER_MM == unityScale.IU_PER_MM )
  290. aEnforceMinTextSize = false;
  291. if( aEnforceMinTextSize )
  292. {
  293. int min = m_IuScale.get().mmToIU( TEXT_MIN_SIZE_MM );
  294. int max = m_IuScale.get().mmToIU( TEXT_MAX_SIZE_MM );
  295. aNewSize = VECTOR2I( alg::clamp( min, aNewSize.x, max ),
  296. alg::clamp( min, aNewSize.y, max ) );
  297. }
  298. m_attributes.m_Size = aNewSize;
  299. ClearRenderCache();
  300. m_bounding_box_cache_valid = false;
  301. }
  302. void EDA_TEXT::SetTextWidth( int aWidth )
  303. {
  304. int min = m_IuScale.get().mmToIU( TEXT_MIN_SIZE_MM );
  305. int max = m_IuScale.get().mmToIU( TEXT_MAX_SIZE_MM );
  306. m_attributes.m_Size.x = alg::clamp( min, aWidth, max );
  307. ClearRenderCache();
  308. m_bounding_box_cache_valid = false;
  309. }
  310. void EDA_TEXT::SetTextHeight( int aHeight )
  311. {
  312. int min = m_IuScale.get().mmToIU( TEXT_MIN_SIZE_MM );
  313. int max = m_IuScale.get().mmToIU( TEXT_MAX_SIZE_MM );
  314. m_attributes.m_Size.y = alg::clamp( min, aHeight, max );
  315. ClearRenderCache();
  316. m_bounding_box_cache_valid = false;
  317. }
  318. void EDA_TEXT::SetTextPos( const VECTOR2I& aPoint )
  319. {
  320. Offset( VECTOR2I( aPoint.x - m_pos.x, aPoint.y - m_pos.y ) );
  321. }
  322. void EDA_TEXT::SetTextX( int aX )
  323. {
  324. Offset( VECTOR2I( aX - m_pos.x, 0 ) );
  325. }
  326. void EDA_TEXT::SetTextY( int aY )
  327. {
  328. Offset( VECTOR2I( 0, aY - m_pos.y ) );
  329. }
  330. void EDA_TEXT::Offset( const VECTOR2I& aOffset )
  331. {
  332. if( aOffset.x == 0 && aOffset.y == 0 )
  333. return;
  334. m_pos += aOffset;
  335. for( std::unique_ptr<KIFONT::GLYPH>& glyph : m_render_cache )
  336. {
  337. if( KIFONT::OUTLINE_GLYPH* outline = dynamic_cast<KIFONT::OUTLINE_GLYPH*>( glyph.get() ) )
  338. outline->Move( aOffset );
  339. else if( KIFONT::STROKE_GLYPH* stroke = dynamic_cast<KIFONT::STROKE_GLYPH*>( glyph.get() ) )
  340. glyph = stroke->Transform( { 1.0, 1.0 }, aOffset, 0, ANGLE_0, false, { 0, 0 } );
  341. }
  342. m_bounding_box_cache_valid = false;
  343. }
  344. void EDA_TEXT::Empty()
  345. {
  346. m_text.Empty();
  347. ClearRenderCache();
  348. m_bounding_box_cache_valid = false;
  349. }
  350. void EDA_TEXT::cacheShownText()
  351. {
  352. if( m_text.IsEmpty() )
  353. {
  354. m_shown_text = wxEmptyString;
  355. m_shown_text_has_text_var_refs = false;
  356. }
  357. else
  358. {
  359. m_shown_text = UnescapeString( m_text );
  360. m_shown_text_has_text_var_refs = m_shown_text.Contains( wxT( "${" ) );
  361. }
  362. ClearRenderCache();
  363. m_bounding_box_cache_valid = false;
  364. }
  365. KIFONT::FONT* EDA_TEXT::getDrawFont() const
  366. {
  367. KIFONT::FONT* font = GetFont();
  368. if( !font )
  369. font = KIFONT::FONT::GetFont( wxEmptyString, IsBold(), IsItalic() );
  370. return font;
  371. }
  372. const KIFONT::METRICS& EDA_TEXT::getFontMetrics() const
  373. {
  374. return KIFONT::METRICS::Default();
  375. }
  376. void EDA_TEXT::ClearRenderCache()
  377. {
  378. m_render_cache.clear();
  379. }
  380. void EDA_TEXT::ClearBoundingBoxCache()
  381. {
  382. m_bounding_box_cache_valid = false;
  383. }
  384. std::vector<std::unique_ptr<KIFONT::GLYPH>>*
  385. EDA_TEXT::GetRenderCache( const KIFONT::FONT* aFont, const wxString& forResolvedText,
  386. const VECTOR2I& aOffset ) const
  387. {
  388. if( aFont->IsOutline() )
  389. {
  390. EDA_ANGLE resolvedAngle = GetDrawRotation();
  391. if( m_render_cache.empty()
  392. || m_render_cache_font != aFont
  393. || m_render_cache_text != forResolvedText
  394. || m_render_cache_angle != resolvedAngle
  395. || m_render_cache_offset != aOffset )
  396. {
  397. m_render_cache.clear();
  398. const KIFONT::OUTLINE_FONT* font = static_cast<const KIFONT::OUTLINE_FONT*>( aFont );
  399. TEXT_ATTRIBUTES attrs = GetAttributes();
  400. attrs.m_Angle = resolvedAngle;
  401. font->GetLinesAsGlyphs( &m_render_cache, forResolvedText, GetDrawPos() + aOffset,
  402. attrs, getFontMetrics() );
  403. m_render_cache_font = aFont;
  404. m_render_cache_angle = resolvedAngle;
  405. m_render_cache_text = forResolvedText;
  406. m_render_cache_offset = aOffset;
  407. }
  408. return &m_render_cache;
  409. }
  410. return nullptr;
  411. }
  412. void EDA_TEXT::SetupRenderCache( const wxString& aResolvedText, const EDA_ANGLE& aAngle )
  413. {
  414. m_render_cache_text = aResolvedText;
  415. m_render_cache_angle = aAngle;
  416. m_render_cache.clear();
  417. }
  418. void EDA_TEXT::AddRenderCacheGlyph( const SHAPE_POLY_SET& aPoly )
  419. {
  420. m_render_cache.emplace_back( std::make_unique<KIFONT::OUTLINE_GLYPH>( aPoly ) );
  421. }
  422. int EDA_TEXT::GetInterline() const
  423. {
  424. return KiROUND( getDrawFont()->GetInterline( GetTextHeight(), getFontMetrics() ) );
  425. }
  426. BOX2I EDA_TEXT::GetTextBox( int aLine, bool aInvertY ) const
  427. {
  428. VECTOR2I drawPos = GetDrawPos();
  429. if( m_bounding_box_cache_valid
  430. && m_bounding_box_cache_pos == drawPos
  431. && m_bounding_box_cache_line == aLine
  432. && m_bounding_box_cache_inverted == aInvertY )
  433. {
  434. return m_bounding_box_cache;
  435. }
  436. BOX2I bbox;
  437. wxArrayString strings;
  438. wxString text = GetShownText( true );
  439. int thickness = GetEffectiveTextPenWidth();
  440. if( IsMultilineAllowed() )
  441. {
  442. wxStringSplit( text, strings, '\n' );
  443. if( strings.GetCount() ) // GetCount() == 0 for void strings with multilines allowed
  444. {
  445. if( aLine >= 0 && ( aLine < static_cast<int>( strings.GetCount() ) ) )
  446. text = strings.Item( aLine );
  447. else
  448. text = strings.Item( 0 );
  449. }
  450. }
  451. // calculate the H and V size
  452. KIFONT::FONT* font = getDrawFont();
  453. VECTOR2D fontSize( GetTextSize() );
  454. bool bold = IsBold();
  455. bool italic = IsItalic();
  456. VECTOR2I extents = font->StringBoundaryLimits( text, fontSize, thickness, bold, italic,
  457. getFontMetrics() );
  458. int overbarOffset = 0;
  459. // Creates bounding box (rectangle) for horizontal, left and top justified text. The
  460. // bounding box will be moved later according to the actual text options
  461. VECTOR2I textsize = VECTOR2I( extents.x, extents.y );
  462. VECTOR2I pos = drawPos;
  463. int fudgeFactor = KiROUND( extents.y * 0.17 );
  464. if( font->IsStroke() )
  465. textsize.y += fudgeFactor;
  466. if( IsMultilineAllowed() && aLine > 0 && aLine < (int) strings.GetCount() )
  467. pos.y -= KiROUND( aLine * font->GetInterline( fontSize.y, getFontMetrics() ) );
  468. if( text.Contains( wxT( "~{" ) ) )
  469. overbarOffset = extents.y / 6;
  470. if( aInvertY )
  471. pos.y = -pos.y;
  472. bbox.SetOrigin( pos );
  473. // for multiline texts and aLine < 0, merge all rectangles (aLine == -1 signals all lines)
  474. if( IsMultilineAllowed() && aLine < 0 && strings.GetCount() > 1 )
  475. {
  476. for( unsigned ii = 1; ii < strings.GetCount(); ii++ )
  477. {
  478. text = strings.Item( ii );
  479. extents = font->StringBoundaryLimits( text, fontSize, thickness, bold, italic,
  480. getFontMetrics() );
  481. textsize.x = std::max( textsize.x, extents.x );
  482. }
  483. // interline spacing is only *between* lines, so total height is the height of the first
  484. // line plus the interline distance (with interline spacing) for all subsequent lines
  485. textsize.y += KiROUND( ( strings.GetCount() - 1 ) * font->GetInterline( fontSize.y,
  486. getFontMetrics() ) );
  487. }
  488. textsize.y += overbarOffset;
  489. bbox.SetSize( textsize );
  490. /*
  491. * At this point the rectangle origin is the text origin (m_Pos). This is correct only for
  492. * left and top justified, non-mirrored, non-overbarred texts. Recalculate for all others.
  493. */
  494. int italicOffset = IsItalic() ? KiROUND( fontSize.y * ITALIC_TILT ) : 0;
  495. switch( GetHorizJustify() )
  496. {
  497. case GR_TEXT_H_ALIGN_LEFT:
  498. if( IsMirrored() )
  499. bbox.SetX( bbox.GetX() - ( bbox.GetWidth() - italicOffset ) );
  500. break;
  501. case GR_TEXT_H_ALIGN_CENTER:
  502. bbox.SetX( bbox.GetX() - ( bbox.GetWidth() - italicOffset ) / 2 );
  503. break;
  504. case GR_TEXT_H_ALIGN_RIGHT:
  505. if( !IsMirrored() )
  506. bbox.SetX( bbox.GetX() - ( bbox.GetWidth() - italicOffset ) );
  507. break;
  508. case GR_TEXT_H_ALIGN_INDETERMINATE:
  509. wxFAIL_MSG( wxT( "Indeterminate state legal only in dialogs." ) );
  510. break;
  511. }
  512. switch( GetVertJustify() )
  513. {
  514. case GR_TEXT_V_ALIGN_TOP:
  515. bbox.Offset( 0, -fudgeFactor );
  516. break;
  517. case GR_TEXT_V_ALIGN_CENTER:
  518. bbox.SetY( bbox.GetY() - bbox.GetHeight() / 2 );
  519. break;
  520. case GR_TEXT_V_ALIGN_BOTTOM:
  521. bbox.SetY( bbox.GetY() - bbox.GetHeight() );
  522. bbox.Offset( 0, fudgeFactor );
  523. break;
  524. case GR_TEXT_V_ALIGN_INDETERMINATE:
  525. wxFAIL_MSG( wxT( "Indeterminate state legal only in dialogs." ) );
  526. break;
  527. }
  528. bbox.Normalize(); // Make h and v sizes always >= 0
  529. m_bounding_box_cache_valid = true;
  530. m_bounding_box_cache_pos = drawPos;
  531. m_bounding_box_cache_line = aLine;
  532. m_bounding_box_cache_inverted = aInvertY;
  533. m_bounding_box_cache = bbox;
  534. return bbox;
  535. }
  536. bool EDA_TEXT::TextHitTest( const VECTOR2I& aPoint, int aAccuracy ) const
  537. {
  538. BOX2I rect = GetTextBox();
  539. VECTOR2I location = aPoint;
  540. rect.Inflate( aAccuracy );
  541. RotatePoint( location, GetDrawPos(), -GetDrawRotation() );
  542. return rect.Contains( location );
  543. }
  544. bool EDA_TEXT::TextHitTest( const BOX2I& aRect, bool aContains, int aAccuracy ) const
  545. {
  546. BOX2I rect = aRect;
  547. rect.Inflate( aAccuracy );
  548. if( aContains )
  549. return rect.Contains( GetTextBox() );
  550. return rect.Intersects( GetTextBox(), GetDrawRotation() );
  551. }
  552. void EDA_TEXT::Print( const RENDER_SETTINGS* aSettings, const VECTOR2I& aOffset,
  553. const COLOR4D& aColor, OUTLINE_MODE aFillMode )
  554. {
  555. if( IsMultilineAllowed() )
  556. {
  557. std::vector<VECTOR2I> positions;
  558. wxArrayString strings;
  559. wxStringSplit( GetShownText( true ), strings, '\n' );
  560. positions.reserve( strings.Count() );
  561. GetLinePositions( positions, (int) strings.Count() );
  562. for( unsigned ii = 0; ii < strings.Count(); ii++ )
  563. printOneLineOfText( aSettings, aOffset, aColor, aFillMode, strings[ii], positions[ii] );
  564. }
  565. else
  566. {
  567. printOneLineOfText( aSettings, aOffset, aColor, aFillMode, GetShownText( true ),
  568. GetDrawPos() );
  569. }
  570. }
  571. void EDA_TEXT::GetLinePositions( std::vector<VECTOR2I>& aPositions, int aLineCount ) const
  572. {
  573. VECTOR2I pos = GetDrawPos(); // Position of first line of the multiline text according
  574. // to the center of the multiline text block
  575. VECTOR2I offset; // Offset to next line.
  576. offset.y = GetInterline();
  577. if( aLineCount > 1 )
  578. {
  579. switch( GetVertJustify() )
  580. {
  581. case GR_TEXT_V_ALIGN_TOP:
  582. break;
  583. case GR_TEXT_V_ALIGN_CENTER:
  584. pos.y -= ( aLineCount - 1 ) * offset.y / 2;
  585. break;
  586. case GR_TEXT_V_ALIGN_BOTTOM:
  587. pos.y -= ( aLineCount - 1 ) * offset.y;
  588. break;
  589. case GR_TEXT_V_ALIGN_INDETERMINATE:
  590. wxFAIL_MSG( wxT( "Indeterminate state legal only in dialogs." ) );
  591. break;
  592. }
  593. }
  594. // Rotate the position of the first line around the center of the multiline text block
  595. RotatePoint( pos, GetDrawPos(), GetDrawRotation() );
  596. // Rotate the offset lines to increase happened in the right direction
  597. RotatePoint( offset, GetDrawRotation() );
  598. for( int ii = 0; ii < aLineCount; ii++ )
  599. {
  600. aPositions.push_back( (VECTOR2I) pos );
  601. pos += offset;
  602. }
  603. }
  604. void EDA_TEXT::printOneLineOfText( const RENDER_SETTINGS* aSettings, const VECTOR2I& aOffset,
  605. const COLOR4D& aColor, OUTLINE_MODE aFillMode,
  606. const wxString& aText, const VECTOR2I& aPos )
  607. {
  608. wxDC* DC = aSettings->GetPrintDC();
  609. int penWidth = GetEffectiveTextPenWidth( aSettings->GetDefaultPenWidth() );
  610. if( aFillMode == SKETCH )
  611. penWidth = -penWidth;
  612. VECTOR2I size = GetTextSize();
  613. if( IsMirrored() )
  614. size.x = -size.x;
  615. KIFONT::FONT* font = GetFont();
  616. if( !font )
  617. font = KIFONT::FONT::GetFont( aSettings->GetDefaultFont(), IsBold(), IsItalic() );
  618. GRPrintText( DC, aOffset + aPos, aColor, aText, GetDrawRotation(), size, GetHorizJustify(),
  619. GetVertJustify(), penWidth, IsItalic(), IsBold(), font, getFontMetrics() );
  620. }
  621. wxString EDA_TEXT::GetTextStyleName() const
  622. {
  623. int style = 0;
  624. if( IsItalic() )
  625. style = 1;
  626. if( IsBold() )
  627. style += 2;
  628. wxString stylemsg[4] = {
  629. _("Normal"),
  630. _("Italic"),
  631. _("Bold"),
  632. _("Bold+Italic")
  633. };
  634. return stylemsg[style];
  635. }
  636. wxString EDA_TEXT::GetFontName() const
  637. {
  638. if( GetFont() )
  639. return GetFont()->GetName();
  640. else
  641. return wxEmptyString;
  642. }
  643. int EDA_TEXT::GetFontIndex() const
  644. {
  645. if( !GetFont() )
  646. return -1;
  647. if( GetFont()->GetName() == KICAD_FONT_NAME )
  648. return -2;
  649. std::vector<std::string> fontNames;
  650. Fontconfig()->ListFonts( fontNames, std::string( Pgm().GetLanguageTag().utf8_str() ) );
  651. for( int ii = 0; ii < (int) fontNames.size(); ++ii )
  652. {
  653. if( fontNames[ii] == GetFont()->GetName() )
  654. return ii;
  655. }
  656. return 0;
  657. }
  658. void EDA_TEXT::SetFontIndex( int aIdx )
  659. {
  660. if( aIdx == -1 )
  661. {
  662. SetFont( nullptr );
  663. }
  664. else if( aIdx == -2 )
  665. {
  666. SetFont( KIFONT::FONT::GetFont( wxEmptyString, IsBold(), IsItalic() ) );
  667. }
  668. else
  669. {
  670. std::vector<std::string> fontNames;
  671. Fontconfig()->ListFonts( fontNames, std::string( Pgm().GetLanguageTag().utf8_str() ) );
  672. if( aIdx >= 0 && aIdx < static_cast<int>( fontNames.size() ) )
  673. SetFont( KIFONT::FONT::GetFont( fontNames[ aIdx ], IsBold(), IsItalic() ) );
  674. else
  675. SetFont( nullptr );
  676. }
  677. }
  678. bool EDA_TEXT::IsDefaultFormatting() const
  679. {
  680. return ( IsVisible()
  681. && !IsMirrored()
  682. && GetHorizJustify() == GR_TEXT_H_ALIGN_CENTER
  683. && GetVertJustify() == GR_TEXT_V_ALIGN_CENTER
  684. && GetTextThickness() == 0
  685. && !IsItalic()
  686. && !IsBold()
  687. && !IsMultilineAllowed()
  688. && GetFontName().IsEmpty()
  689. );
  690. }
  691. void EDA_TEXT::Format( OUTPUTFORMATTER* aFormatter, int aNestLevel, int aControlBits ) const
  692. {
  693. aFormatter->Print( aNestLevel + 1, "(effects" );
  694. aFormatter->Print( 0, " (font" );
  695. if( GetFont() && !GetFont()->GetName().IsEmpty() )
  696. aFormatter->Print( 0, " (face \"%s\")", GetFont()->NameAsToken() );
  697. // Text size
  698. aFormatter->Print( 0, " (size %s %s)",
  699. EDA_UNIT_UTILS::FormatInternalUnits( m_IuScale, GetTextHeight() ).c_str(),
  700. EDA_UNIT_UTILS::FormatInternalUnits( m_IuScale, GetTextWidth() ).c_str() );
  701. if( GetLineSpacing() != 1.0 )
  702. {
  703. aFormatter->Print( 0, " (line_spacing %s)",
  704. FormatDouble2Str( GetLineSpacing() ).c_str() );
  705. }
  706. if( GetTextThickness() )
  707. {
  708. aFormatter->Print( 0, " (thickness %s)",
  709. EDA_UNIT_UTILS::FormatInternalUnits( m_IuScale, GetTextThickness() ).c_str() );
  710. }
  711. if( IsBold() )
  712. aFormatter->Print( 0, " (bold yes)" );
  713. if( IsItalic() )
  714. aFormatter->Print( 0, " (italic yes)" );
  715. if( GetTextColor() != COLOR4D::UNSPECIFIED )
  716. {
  717. aFormatter->Print( 0, " (color %d %d %d %s)",
  718. KiROUND( GetTextColor().r * 255.0 ),
  719. KiROUND( GetTextColor().g * 255.0 ),
  720. KiROUND( GetTextColor().b * 255.0 ),
  721. FormatDouble2Str( GetTextColor().a ).c_str() );
  722. }
  723. aFormatter->Print( 0, ")"); // (font
  724. if( IsMirrored() || GetHorizJustify() != GR_TEXT_H_ALIGN_CENTER
  725. || GetVertJustify() != GR_TEXT_V_ALIGN_CENTER )
  726. {
  727. aFormatter->Print( 0, " (justify");
  728. if( GetHorizJustify() != GR_TEXT_H_ALIGN_CENTER )
  729. aFormatter->Print( 0, GetHorizJustify() == GR_TEXT_H_ALIGN_LEFT ? " left" : " right" );
  730. if( GetVertJustify() != GR_TEXT_V_ALIGN_CENTER )
  731. aFormatter->Print( 0, GetVertJustify() == GR_TEXT_V_ALIGN_TOP ? " top" : " bottom" );
  732. if( IsMirrored() )
  733. aFormatter->Print( 0, " mirror" );
  734. aFormatter->Print( 0, ")" ); // (justify
  735. }
  736. if( !( aControlBits & CTL_OMIT_HIDE ) && !IsVisible() )
  737. aFormatter->Print( 0, " (hide yes)" );
  738. if( HasHyperlink() )
  739. {
  740. aFormatter->Print( 0, " (href %s)", aFormatter->Quotew( GetHyperlink() ).c_str() );
  741. }
  742. aFormatter->Print( 0, ")\n" ); // (effects
  743. }
  744. std::shared_ptr<SHAPE_COMPOUND> EDA_TEXT::GetEffectiveTextShape( bool aTriangulate,
  745. const BOX2I& aBBox,
  746. const EDA_ANGLE& aAngle ) const
  747. {
  748. std::shared_ptr<SHAPE_COMPOUND> shape = std::make_shared<SHAPE_COMPOUND>();
  749. KIGFX::GAL_DISPLAY_OPTIONS empty_opts;
  750. KIFONT::FONT* font = getDrawFont();
  751. int penWidth = GetEffectiveTextPenWidth();
  752. wxString shownText( GetShownText( true ) );
  753. VECTOR2I drawPos = GetDrawPos();
  754. TEXT_ATTRIBUTES attrs = GetAttributes();
  755. std::vector<std::unique_ptr<KIFONT::GLYPH>>* cache = nullptr;
  756. if( aBBox.GetWidth() )
  757. {
  758. drawPos = aBBox.GetCenter();
  759. attrs.m_Halign = GR_TEXT_H_ALIGN_CENTER;
  760. attrs.m_Valign = GR_TEXT_V_ALIGN_CENTER;
  761. attrs.m_Angle = aAngle;
  762. }
  763. else
  764. {
  765. attrs.m_Angle = GetDrawRotation();
  766. if( font->IsOutline() )
  767. cache = GetRenderCache( font, shownText, VECTOR2I() );
  768. }
  769. if( aTriangulate )
  770. {
  771. CALLBACK_GAL callback_gal(
  772. empty_opts,
  773. // Stroke callback
  774. [&]( const VECTOR2I& aPt1, const VECTOR2I& aPt2 )
  775. {
  776. shape->AddShape( new SHAPE_SEGMENT( aPt1, aPt2, penWidth ) );
  777. },
  778. // Triangulation callback
  779. [&]( const VECTOR2I& aPt1, const VECTOR2I& aPt2, const VECTOR2I& aPt3 )
  780. {
  781. SHAPE_SIMPLE* triShape = new SHAPE_SIMPLE;
  782. for( const VECTOR2I& point : { aPt1, aPt2, aPt3 } )
  783. triShape->Append( point.x, point.y );
  784. shape->AddShape( triShape );
  785. } );
  786. if( cache )
  787. callback_gal.DrawGlyphs( *cache );
  788. else
  789. font->Draw( &callback_gal, shownText, drawPos, attrs, getFontMetrics() );
  790. }
  791. else
  792. {
  793. CALLBACK_GAL callback_gal(
  794. empty_opts,
  795. // Stroke callback
  796. [&]( const VECTOR2I& aPt1, const VECTOR2I& aPt2 )
  797. {
  798. shape->AddShape( new SHAPE_SEGMENT( aPt1, aPt2, penWidth ) );
  799. },
  800. // Outline callback
  801. [&]( const SHAPE_LINE_CHAIN& aPoly )
  802. {
  803. shape->AddShape( aPoly.Clone() );
  804. } );
  805. if( cache )
  806. callback_gal.DrawGlyphs( *cache );
  807. else
  808. font->Draw( &callback_gal, shownText, drawPos, attrs, getFontMetrics() );
  809. }
  810. return shape;
  811. }
  812. int EDA_TEXT::Compare( const EDA_TEXT* aOther ) const
  813. {
  814. wxCHECK( aOther, 1 );
  815. int val = m_attributes.Compare( aOther->m_attributes );
  816. if( val != 0 )
  817. return val;
  818. if( m_pos.x != aOther->m_pos.x )
  819. return m_pos.x - aOther->m_pos.x;
  820. if( m_pos.y != aOther->m_pos.y )
  821. return m_pos.y - aOther->m_pos.y;
  822. val = GetFontName().Cmp( aOther->GetFontName() );
  823. if( val != 0 )
  824. return val;
  825. return m_text.Cmp( aOther->m_text );
  826. }
  827. bool EDA_TEXT::ValidateHyperlink( const wxString& aURL )
  828. {
  829. if( aURL.IsEmpty() || IsGotoPageHref( aURL ) )
  830. return true;
  831. // Limit valid urls to file, http and https for now. Note wxURL doesn't support https
  832. wxURI uri;
  833. if( uri.Create( aURL ) && uri.HasScheme() )
  834. {
  835. const wxString& scheme = uri.GetScheme();
  836. return scheme == wxT( "file" ) || scheme == wxT( "http" ) || scheme == wxT( "https" );
  837. }
  838. return false;
  839. }
  840. double EDA_TEXT::Levenshtein( const EDA_TEXT& aOther ) const
  841. {
  842. // Compute the Levenshtein distance between the two strings
  843. const wxString& str1 = GetText();
  844. const wxString& str2 = aOther.GetText();
  845. int m = str1.length();
  846. int n = str2.length();
  847. if( n == 0 || m == 0 )
  848. return 0.0;
  849. // Create a matrix to store the distance values
  850. std::vector<std::vector<int>> distance(m + 1, std::vector<int>(n + 1));
  851. // Initialize the matrix
  852. for( int i = 0; i <= m; i++ )
  853. distance[i][0] = i;
  854. for( int j = 0; j <= n; j++ )
  855. distance[0][j] = j;
  856. // Calculate the distance
  857. for( int i = 1; i <= m; i++ )
  858. {
  859. for( int j = 1; j <= n; j++ )
  860. {
  861. if( str1[i - 1] == str2[j - 1] )
  862. {
  863. distance[i][j] = distance[i - 1][j - 1];
  864. }
  865. else
  866. {
  867. distance[i][j] = std::min( { distance[i - 1][j], distance[i][j - 1],
  868. distance[i - 1][j - 1] } ) + 1;
  869. }
  870. }
  871. }
  872. // Calculate similarity score
  873. int maxLen = std::max( m, n );
  874. double similarity = 1.0 - ( static_cast<double>( distance[m][n] ) / maxLen );
  875. return similarity;
  876. }
  877. double EDA_TEXT::Similarity( const EDA_TEXT& aOther ) const
  878. {
  879. double retval = 1.0;
  880. if( !( m_attributes == aOther.m_attributes ) )
  881. retval *= 0.9;
  882. if( m_pos != aOther.m_pos )
  883. retval *= 0.9;
  884. retval *= Levenshtein( aOther );
  885. return retval;
  886. }
  887. bool EDA_TEXT::IsGotoPageHref( const wxString& aHref, wxString* aDestination )
  888. {
  889. return aHref.StartsWith( wxT( "#" ), aDestination );
  890. }
  891. wxString EDA_TEXT::GotoPageHref( const wxString& aDestination )
  892. {
  893. return wxT( "#" ) + aDestination;
  894. }
  895. std::ostream& operator<<( std::ostream& aStream, const EDA_TEXT& aText )
  896. {
  897. aStream << aText.GetText();
  898. return aStream;
  899. }
  900. static struct EDA_TEXT_DESC
  901. {
  902. EDA_TEXT_DESC()
  903. {
  904. ENUM_MAP<GR_TEXT_H_ALIGN_T>::Instance()
  905. .Map( GR_TEXT_H_ALIGN_LEFT, _HKI( "Left" ) )
  906. .Map( GR_TEXT_H_ALIGN_CENTER, _HKI( "Center" ) )
  907. .Map( GR_TEXT_H_ALIGN_RIGHT, _HKI( "Right" ) );
  908. ENUM_MAP<GR_TEXT_V_ALIGN_T>::Instance()
  909. .Map( GR_TEXT_V_ALIGN_TOP, _HKI( "Top" ) )
  910. .Map( GR_TEXT_V_ALIGN_CENTER, _HKI( "Center" ) )
  911. .Map( GR_TEXT_V_ALIGN_BOTTOM, _HKI( "Bottom" ) );
  912. PROPERTY_MANAGER& propMgr = PROPERTY_MANAGER::Instance();
  913. REGISTER_TYPE( EDA_TEXT );
  914. propMgr.AddProperty( new PROPERTY<EDA_TEXT, double>( _HKI( "Orientation" ),
  915. &EDA_TEXT::SetTextAngleDegrees, &EDA_TEXT::GetTextAngleDegrees,
  916. PROPERTY_DISPLAY::PT_DEGREE ) );
  917. const wxString textProps = _HKI( "Text Properties" );
  918. propMgr.AddProperty( new PROPERTY<EDA_TEXT, wxString>( _HKI( "Text" ),
  919. &EDA_TEXT::SetText, &EDA_TEXT::GetText ),
  920. textProps );
  921. // This must be a PROPERTY_ENUM to get a choice list.
  922. // SCH_ and PCB_PROPERTIES_PANEL::updateFontList() fill in the enum values.
  923. propMgr.AddProperty( new PROPERTY_ENUM<EDA_TEXT, int>( _HKI( "Font" ),
  924. &EDA_TEXT::SetFontIndex, &EDA_TEXT::GetFontIndex ),
  925. textProps )
  926. .SetIsHiddenFromRulesEditor();
  927. propMgr.AddProperty( new PROPERTY<EDA_TEXT, int>( _HKI( "Thickness" ),
  928. &EDA_TEXT::SetTextThickness, &EDA_TEXT::GetTextThickness,
  929. PROPERTY_DISPLAY::PT_SIZE ),
  930. textProps );
  931. propMgr.AddProperty( new PROPERTY<EDA_TEXT, bool>( _HKI( "Italic" ),
  932. &EDA_TEXT::SetItalic,
  933. &EDA_TEXT::IsItalic ),
  934. textProps );
  935. propMgr.AddProperty( new PROPERTY<EDA_TEXT, bool>( _HKI( "Bold" ),
  936. &EDA_TEXT::SetBold, &EDA_TEXT::IsBold ),
  937. textProps );
  938. propMgr.AddProperty( new PROPERTY<EDA_TEXT, bool>( _HKI( "Mirrored" ),
  939. &EDA_TEXT::SetMirrored, &EDA_TEXT::IsMirrored ),
  940. textProps );
  941. propMgr.AddProperty( new PROPERTY<EDA_TEXT, bool>( _HKI( "Visible" ),
  942. &EDA_TEXT::SetVisible, &EDA_TEXT::IsVisible ),
  943. textProps );
  944. propMgr.AddProperty( new PROPERTY<EDA_TEXT, int>( _HKI( "Width" ),
  945. &EDA_TEXT::SetTextWidth, &EDA_TEXT::GetTextWidth,
  946. PROPERTY_DISPLAY::PT_SIZE ),
  947. textProps );
  948. propMgr.AddProperty( new PROPERTY<EDA_TEXT, int>( _HKI( "Height" ),
  949. &EDA_TEXT::SetTextHeight, &EDA_TEXT::GetTextHeight,
  950. PROPERTY_DISPLAY::PT_SIZE ),
  951. textProps );
  952. propMgr.AddProperty( new PROPERTY_ENUM<EDA_TEXT, GR_TEXT_H_ALIGN_T>(
  953. _HKI( "Horizontal Justification" ),
  954. &EDA_TEXT::SetHorizJustify, &EDA_TEXT::GetHorizJustify ),
  955. textProps );
  956. propMgr.AddProperty( new PROPERTY_ENUM<EDA_TEXT, GR_TEXT_V_ALIGN_T>(
  957. _HKI( "Vertical Justification" ),
  958. &EDA_TEXT::SetVertJustify, &EDA_TEXT::GetVertJustify ),
  959. textProps );
  960. propMgr.AddProperty( new PROPERTY<EDA_TEXT, COLOR4D>( _HKI( "Color" ),
  961. &EDA_TEXT::SetTextColor, &EDA_TEXT::GetTextColor ),
  962. textProps );
  963. propMgr.AddProperty( new PROPERTY<EDA_TEXT, wxString>( _HKI( "Hyperlink" ),
  964. &EDA_TEXT::SetHyperlink, &EDA_TEXT::GetHyperlink ),
  965. textProps );
  966. }
  967. } _EDA_TEXT_DESC;
  968. ENUM_TO_WXANY( GR_TEXT_H_ALIGN_T )
  969. ENUM_TO_WXANY( GR_TEXT_V_ALIGN_T )