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.

424 lines
16 KiB

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) 2013 Jean-Pierre Charras, jp.charras at wanadoo.fr
  5. * Copyright (C) 2004-2023 KiCad Developers, see AUTHORS.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. #ifndef EDA_TEXT_H_
  25. #define EDA_TEXT_H_
  26. #include <memory>
  27. #include <vector>
  28. #include <outline_mode.h>
  29. #include <eda_search_data.h>
  30. #include <font/glyph.h>
  31. #include <font/text_attributes.h>
  32. class OUTPUTFORMATTER;
  33. class SHAPE_COMPOUND;
  34. class SHAPE_POLY_SET;
  35. // These are only here for algorithmic safety, not to tell the user what to do
  36. #define TEXT_MIN_SIZE_MILS 1 ///< Minimum text size in mils
  37. #define TEXT_MAX_SIZE_MILS 10000 ///< Maximum text size in mils (10 inches)
  38. namespace KIGFX
  39. {
  40. class RENDER_SETTINGS;
  41. class COLOR4D;
  42. }
  43. using KIGFX::RENDER_SETTINGS;
  44. using KIGFX::COLOR4D;
  45. // part of the kicad_plugin.h family of defines.
  46. // See kicad_plugin.h for the choice of the value
  47. // When set when calling EDA_TEXT::Format, disable writing the "hide" keyword in save file
  48. #define CTL_OMIT_HIDE (1 << 6)
  49. /**
  50. * This is the "default-of-the-default" hardcoded text size; individual
  51. * application define their own default policy starting with this
  52. * (usually with a user option or project).
  53. */
  54. #define DEFAULT_SIZE_TEXT 50 // default text height (in mils, i.e. 1/1000")
  55. #define DIM_ANCRE_TEXTE 2 // Anchor size for text
  56. /**
  57. * A mix-in class (via multiple inheritance) that handles texts such as labels, parts,
  58. * components, or footprints. Because it's a mix-in class, care is used to provide
  59. * function names (accessors) that to not collide with function names likely to be seen
  60. * in the combined derived classes.
  61. */
  62. class EDA_TEXT
  63. {
  64. public:
  65. EDA_TEXT( const EDA_IU_SCALE& aIuScale, const wxString& aText = wxEmptyString );
  66. EDA_TEXT( const EDA_TEXT& aText );
  67. virtual ~EDA_TEXT();
  68. EDA_TEXT& operator=( const EDA_TEXT& aItem );
  69. /**
  70. * Return the string associated with the text object.
  71. *
  72. * @return a const wxString reference containing the string of the item.
  73. */
  74. virtual const wxString& GetText() const { return m_text; }
  75. /**
  76. * Return the string actually shown after processing of the base text.
  77. *
  78. * @param aAllowExtraText is true to allow adding more text than the initial expanded text,
  79. * for intance a title, a prefix for texts in display functions.
  80. * False to disable any added text (for instance when writing the shown text in netlists).
  81. * @param aDepth is used to prevent infinite recursions and loops when expanding
  82. * text variables.
  83. */
  84. virtual wxString GetShownText( bool aAllowExtraText, int aDepth = 0 ) const
  85. {
  86. return m_shown_text;
  87. }
  88. /**
  89. * Indicates the ShownText has text var references which need to be processed.
  90. */
  91. bool HasTextVars() const { return m_shown_text_has_text_var_refs; }
  92. virtual void SetText( const wxString& aText );
  93. /**
  94. * The TextThickness is that set by the user. The EffectiveTextPenWidth also factors
  95. * in bold text and thickness clamping.
  96. */
  97. void SetTextThickness( int aWidth );
  98. int GetTextThickness() const { return m_attributes.m_StrokeWidth; };
  99. /**
  100. * The EffectiveTextPenWidth uses the text thickness if > 1 or aDefaultPenWidth.
  101. */
  102. int GetEffectiveTextPenWidth( int aDefaultPenWidth = 0 ) const;
  103. virtual void SetTextAngle( const EDA_ANGLE& aAngle );
  104. const EDA_ANGLE& GetTextAngle() const { return m_attributes.m_Angle; }
  105. // For property system:
  106. void SetTextAngleDegrees( double aOrientation )
  107. {
  108. SetTextAngle( EDA_ANGLE( aOrientation, DEGREES_T ) );
  109. }
  110. double GetTextAngleDegrees() const { return m_attributes.m_Angle.AsDegrees(); }
  111. void SetItalic( bool aItalic );
  112. bool IsItalic() const { return m_attributes.m_Italic; }
  113. void SetBold( bool aBold );
  114. bool IsBold() const { return m_attributes.m_Bold; }
  115. virtual void SetVisible( bool aVisible );
  116. virtual bool IsVisible() const { return m_attributes.m_Visible; }
  117. void SetMirrored( bool isMirrored );
  118. bool IsMirrored() const { return m_attributes.m_Mirrored; }
  119. /**
  120. * @param aAllow true if ok to use multiline option, false if ok to use only single line
  121. * text. (Single line is faster in calculations than multiline.)
  122. */
  123. void SetMultilineAllowed( bool aAllow );
  124. bool IsMultilineAllowed() const { return m_attributes.m_Multiline; }
  125. void SetHorizJustify( GR_TEXT_H_ALIGN_T aType );
  126. GR_TEXT_H_ALIGN_T GetHorizJustify() const { return m_attributes.m_Halign; };
  127. void SetVertJustify( GR_TEXT_V_ALIGN_T aType );
  128. GR_TEXT_V_ALIGN_T GetVertJustify() const { return m_attributes.m_Valign; };
  129. void SetKeepUpright( bool aKeepUpright );
  130. bool IsKeepUpright() const { return m_attributes.m_KeepUpright; }
  131. /**
  132. * Set the text attributes from another instance.
  133. */
  134. void SetAttributes( const EDA_TEXT& aSrc );
  135. /**
  136. * Swap the text attributes of the two involved instances.
  137. */
  138. void SwapAttributes( EDA_TEXT& aTradingPartner );
  139. void SwapText( EDA_TEXT& aTradingPartner );
  140. void CopyText( const EDA_TEXT& aSrc );
  141. void SetAttributes( const TEXT_ATTRIBUTES& aTextAttrs ) { m_attributes = aTextAttrs; }
  142. const TEXT_ATTRIBUTES& GetAttributes() const { return m_attributes; }
  143. /**
  144. * Helper function used in search and replace dialog.
  145. *
  146. * Perform a text replace using the find and replace criteria in \a aSearchData.
  147. *
  148. * @param aSearchData A reference to a EDA_SEARCH_DATA object containing the
  149. * search and replace criteria.
  150. * @return True if the text item was modified, otherwise false.
  151. */
  152. bool Replace( const EDA_SEARCH_DATA& aSearchData );
  153. bool IsDefaultFormatting() const;
  154. void SetFont( KIFONT::FONT* aFont );
  155. KIFONT::FONT* GetFont() const { return m_attributes.m_Font; }
  156. wxString GetFontName() const;
  157. void SetLineSpacing( double aLineSpacing );
  158. double GetLineSpacing() const { return m_attributes.m_LineSpacing; }
  159. void SetTextSize( const VECTOR2I& aNewSize );
  160. VECTOR2I GetTextSize() const { return m_attributes.m_Size; }
  161. void SetTextWidth( int aWidth );
  162. int GetTextWidth() const { return m_attributes.m_Size.x; }
  163. void SetTextHeight( int aHeight );
  164. int GetTextHeight() const { return m_attributes.m_Size.y; }
  165. void SetTextColor( const COLOR4D& aColor ) { m_attributes.m_Color = aColor; }
  166. COLOR4D GetTextColor() const { return m_attributes.m_Color; }
  167. void SetTextPos( const VECTOR2I& aPoint );
  168. const VECTOR2I& GetTextPos() const { return m_pos; }
  169. void SetTextX( int aX );
  170. void SetTextY( int aY );
  171. void Offset( const VECTOR2I& aOffset );
  172. void Empty();
  173. static GR_TEXT_H_ALIGN_T MapHorizJustify( int aHorizJustify );
  174. static GR_TEXT_V_ALIGN_T MapVertJustify( int aVertJustify );
  175. /**
  176. * Print this text object to the device context \a aDC.
  177. *
  178. * @param aDC the current Device Context.
  179. * @param aOffset draw offset (usually (0,0)).
  180. * @param aColor text color.
  181. * @param aDisplay_mode #FILLED or #SKETCH.
  182. */
  183. void Print( const RENDER_SETTINGS* aSettings, const VECTOR2I& aOffset,
  184. const COLOR4D& aColor, OUTLINE_MODE aDisplay_mode = FILLED );
  185. /**
  186. * build a list of segments (SHAPE_SEGMENT) to describe a text shape.
  187. * @param aTriangulate: true to build also the triangulation of each shape
  188. * @param aUseTextRotation: true to use the actual text draw rotation.
  189. * false to build a list of shape for a not rotated text ("native" shapes).
  190. */
  191. std::shared_ptr<SHAPE_COMPOUND> GetEffectiveTextShape( bool aTriangulate = true,
  192. bool aUseTextRotation = true ) const;
  193. /**
  194. * Test if \a aPoint is within the bounds of this object.
  195. *
  196. * @param aPoint A VECTOR2I to test.
  197. * @param aAccuracy Amount to inflate the bounding box.
  198. * @return true if a hit, else false.
  199. */
  200. virtual bool TextHitTest( const VECTOR2I& aPoint, int aAccuracy = 0 ) const;
  201. /**
  202. * Test if object bounding box is contained within or intersects \a aRect.
  203. *
  204. * @param aRect Rect to test against.
  205. * @param aContains Test for containment instead of intersection if true.
  206. * @param aAccuracy Amount to inflate the bounding box.
  207. * @return true if a hit, else false.
  208. */
  209. virtual bool TextHitTest( const BOX2I& aRect, bool aContains, int aAccuracy = 0 ) const;
  210. /**
  211. * Useful in multiline texts to calculate the full text or a line area (for zones filling,
  212. * locate functions....)
  213. *
  214. * @param aLine The line of text to consider. Pass -1 for all lines.
  215. * @param aInvertY Invert the Y axis when calculating bounding box.
  216. * @return the rect containing the line of text (i.e. the position and the size of one line)
  217. * this rectangle is calculated for 0 orient text.
  218. * If orientation is not 0 the rect must be rotated to match the physical area
  219. */
  220. BOX2I GetTextBox( int aLine = -1, bool aInvertY = false ) const;
  221. /**
  222. * Return the distance between two lines of text.
  223. *
  224. * Calculates the distance (pitch) between two lines of text. This distance includes the
  225. * interline distance plus room for characters like j, {, and [. It also used for single
  226. * line text, to calculate the text bounding box.
  227. */
  228. int GetInterline() const;
  229. /**
  230. * @return a wxString with the style name( Normal, Italic, Bold, Bold+Italic).
  231. */
  232. wxString GetTextStyleName() const;
  233. /**
  234. * Populate \a aPositions with the position of each line of a multiline text, according
  235. * to the vertical justification and the rotation of the whole text.
  236. *
  237. * @param aPositions is the list to populate by the VECTOR2I positions.
  238. * @param aLineCount is the number of lines (not recalculated here for efficiency reasons.
  239. */
  240. void GetLinePositions( std::vector<VECTOR2I>& aPositions, int aLineCount ) const;
  241. /**
  242. * Output the object to \a aFormatter in s-expression form.
  243. *
  244. * @param aFormatter The #OUTPUTFORMATTER object to write to.
  245. * @param aNestLevel The indentation next level.
  246. * @param aControlBits The control bit definition for object specific formatting.
  247. * @throw IO_ERROR on write error.
  248. */
  249. virtual void Format( OUTPUTFORMATTER* aFormatter, int aNestLevel, int aControlBits ) const;
  250. virtual EDA_ANGLE GetDrawRotation() const { return GetTextAngle(); }
  251. virtual VECTOR2I GetDrawPos() const { return GetTextPos(); }
  252. virtual void ClearRenderCache();
  253. virtual void ClearBoundingBoxCache();
  254. std::vector<std::unique_ptr<KIFONT::GLYPH>>*
  255. GetRenderCache( const KIFONT::FONT* aFont, const wxString& forResolvedText,
  256. const VECTOR2I& aOffset = { 0, 0 } ) const;
  257. // Support for reading the cache from disk.
  258. void SetupRenderCache( const wxString& aResolvedText, const EDA_ANGLE& aAngle );
  259. void AddRenderCacheGlyph( const SHAPE_POLY_SET& aPoly );
  260. int Compare( const EDA_TEXT* aOther ) const;
  261. bool operator==( const EDA_TEXT& aRhs ) const { return Compare( &aRhs ) == 0; }
  262. bool operator<( const EDA_TEXT& aRhs ) const { return Compare( &aRhs ) < 0; }
  263. bool operator>( const EDA_TEXT& aRhs ) const { return Compare( &aRhs ) > 0; }
  264. virtual bool HasHyperlink() const { return !m_hyperlink.IsEmpty(); }
  265. wxString GetHyperlink() const { return m_hyperlink; }
  266. void SetHyperlink( wxString aLink ) { m_hyperlink = aLink; }
  267. void RemoveHyperlink() { m_hyperlink = wxEmptyString; }
  268. /**
  269. * Check if aURL is a valid hyperlink.
  270. *
  271. * @param aURL String to validate
  272. * @return true if aURL is a valid hyperlink
  273. */
  274. static bool ValidateHyperlink( const wxString& aURL );
  275. /**
  276. * Check if aHref is a valid internal hyperlink.
  277. *
  278. * @param aHref String to validate
  279. * @param aDestination [optional] pointer to populate with the destination page
  280. * @return true if aHref is a valid internal hyperlink. Does *not* check if the destination
  281. * page actually exists.
  282. */
  283. static bool IsGotoPageHref( const wxString& aHref, wxString* aDestination = nullptr );
  284. /**
  285. * Generate a href to a page in the current schematic.
  286. *
  287. * @param aDestination Destination sheet's page number.
  288. * @return A hyperlink href string that goes to the specified page.
  289. */
  290. static wxString GotoPageHref( const wxString& aDestination );
  291. protected:
  292. virtual KIFONT::FONT* getDrawFont() const;
  293. void cacheShownText();
  294. /**
  295. * Print each line of this EDA_TEXT.
  296. *
  297. * @param aOffset draw offset (usually (0,0)).
  298. * @param aColor text color.
  299. * @param aFillMode FILLED or SKETCH
  300. * @param aText the single line of text to draw.
  301. * @param aPos the position of this line ).
  302. */
  303. void printOneLineOfText( const RENDER_SETTINGS* aSettings, const VECTOR2I& aOffset,
  304. const COLOR4D& aColor, OUTLINE_MODE aFillMode, const wxString& aText,
  305. const VECTOR2I& aPos );
  306. protected:
  307. /**
  308. * A hyperlink URL. If empty, this text object is not a hyperlink.
  309. */
  310. wxString m_hyperlink;
  311. private:
  312. wxString m_text;
  313. wxString m_shown_text; // Cache of unescaped text for efficient access
  314. bool m_shown_text_has_text_var_refs;
  315. std::reference_wrapper<const EDA_IU_SCALE> m_IuScale;
  316. mutable wxString m_render_cache_text;
  317. mutable const KIFONT::FONT* m_render_cache_font;
  318. mutable EDA_ANGLE m_render_cache_angle;
  319. mutable VECTOR2I m_render_cache_offset;
  320. mutable std::vector<std::unique_ptr<KIFONT::GLYPH>> m_render_cache;
  321. mutable bool m_bounding_box_cache_valid;
  322. mutable VECTOR2I m_bounding_box_cache_pos;
  323. mutable int m_bounding_box_cache_line;
  324. mutable bool m_bounding_box_cache_inverted;
  325. mutable BOX2I m_bounding_box_cache;
  326. TEXT_ATTRIBUTES m_attributes;
  327. VECTOR2I m_pos;
  328. };
  329. extern std::ostream& operator<<( std::ostream& aStream, const EDA_TEXT& aAttributes );
  330. template<>
  331. struct std::hash<EDA_TEXT>
  332. {
  333. std::size_t operator()( const EDA_TEXT& aText ) const
  334. {
  335. return hash_val( aText.GetText(), aText.GetAttributes(), aText.GetTextPos().x,
  336. aText.GetTextPos().y );
  337. }
  338. };
  339. #endif // EDA_TEXT_H_