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.

300 lines
12 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2004 Jean-Pierre Charras, jean-pierre.charras@gipsa-lab.inpg.com
  5. * Copyright (C) 2004-2011 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.h
  26. * @brief Definition of base KiCad text object.
  27. */
  28. #ifndef EDA_TEXT_H_
  29. #define EDA_TEXT_H_
  30. #include <macros.h> // NORMALIZE_ANGLE_POS( angle );
  31. #include <common.h> // wxStringSplit
  32. #include <gr_basic.h> // EDA_DRAW_MODE_T
  33. #include <base_struct.h> // EDA_RECT
  34. // Graphic Text justify:
  35. // Values -1,0,1 are used in computations, do not change them
  36. enum EDA_TEXT_HJUSTIFY_T {
  37. GR_TEXT_HJUSTIFY_LEFT = -1,
  38. GR_TEXT_HJUSTIFY_CENTER = 0,
  39. GR_TEXT_HJUSTIFY_RIGHT = 1
  40. };
  41. enum EDA_TEXT_VJUSTIFY_T {
  42. GR_TEXT_VJUSTIFY_TOP = -1,
  43. GR_TEXT_VJUSTIFY_CENTER = 0,
  44. GR_TEXT_VJUSTIFY_BOTTOM = 1
  45. };
  46. /* Options to show solid segments (segments, texts...) */
  47. enum EDA_DRAW_MODE_T {
  48. LINE = 0, // segments are drawn as lines
  49. FILLED, // normal mode: segments have thickness
  50. SKETCH // sketch mode: segments have thickness, but are not filled
  51. };
  52. #define DEFAULT_SIZE_TEXT 60 /* default text height (in mils or 1/1000") */
  53. #define TEXT_NO_VISIBLE 1 //< EDA_TEXT::m_Attribut(e?) visibility flag.
  54. /**
  55. * Class EDA_TEXT
  56. * is a basic class to handle texts (labels, texts on components or footprints
  57. * ..) not used directly. The "used" text classes are derived from EDA_ITEM and
  58. * EDA_TEXT using multiple inheritance.
  59. */
  60. class EDA_TEXT
  61. {
  62. protected:
  63. wxString m_Text;
  64. int m_Thickness; ///< pen size used to draw this text
  65. double m_Orient; ///< Orient in 0.1 degrees
  66. wxPoint m_Pos; ///< XY position of anchor text.
  67. wxSize m_Size; ///< XY size of text
  68. bool m_Mirror; ///< true if mirrored
  69. int m_Attributs; ///< bit flags such as visible, etc.
  70. bool m_Italic; ///< should be italic font (if available)
  71. bool m_Bold; ///< should be bold font (if available)
  72. EDA_TEXT_HJUSTIFY_T m_HJustify; ///< horizontal justification
  73. EDA_TEXT_VJUSTIFY_T m_VJustify; ///< vertical justification
  74. bool m_MultilineAllowed; /**< true to use multiline option, false
  75. * to use only single line text
  76. * Single line is faster in
  77. * calculations than multiline */
  78. public:
  79. EDA_TEXT( const wxString& text = wxEmptyString );
  80. EDA_TEXT( const EDA_TEXT& aText );
  81. virtual ~EDA_TEXT();
  82. /**
  83. * Function GetText
  84. * returns the string associated with the text object.
  85. * <p>
  86. * This function is virtual to allow derived classes to override getting the
  87. * string to provide a way for modifying the base string by adding a suffix or
  88. * prefix to the base string.
  89. * </p>
  90. * @return a const wxString object containing the string of the item.
  91. */
  92. virtual const wxString GetText() const { return m_Text; }
  93. virtual void SetText( const wxString& aText ) { m_Text = aText; }
  94. /**
  95. * Function SetThickness
  96. * sets text thickness.
  97. * @param aNewThickness is the new text thickness.
  98. */
  99. void SetThickness( int aNewThickness ) { m_Thickness = aNewThickness; };
  100. /**
  101. * Function GetThickness
  102. * returns text thickness.
  103. * @return int - text thickness.
  104. */
  105. int GetThickness() const { return m_Thickness; };
  106. void SetOrientation( double aOrientation )
  107. {
  108. NORMALIZE_ANGLE_POS( aOrientation );
  109. m_Orient = aOrientation;
  110. }
  111. double GetOrientation() const { return m_Orient; }
  112. void SetItalic( bool isItalic ) { m_Italic = isItalic; }
  113. bool IsItalic() const { return m_Italic; }
  114. void SetBold( bool aBold ) { m_Bold = aBold; }
  115. bool IsBold() const { return m_Bold; }
  116. void SetVisible( bool aVisible )
  117. {
  118. ( aVisible ) ? m_Attributs &= ~TEXT_NO_VISIBLE : m_Attributs |= TEXT_NO_VISIBLE;
  119. }
  120. bool IsVisible() const { return !( m_Attributs & TEXT_NO_VISIBLE ); }
  121. void SetMirrored( bool isMirrored ) { m_Mirror = isMirrored; }
  122. bool IsMirrored() const { return m_Mirror; }
  123. void SetAttributes( int aAttributes ) { m_Attributs = aAttributes; }
  124. int GetAttributes() const { return m_Attributs; }
  125. bool IsDefaultFormatting() const;
  126. /**
  127. * Function SetSize
  128. * sets text size.
  129. * @param aNewSize is the new text size.
  130. */
  131. void SetSize( const wxSize& aNewSize ) { m_Size = aNewSize; };
  132. /**
  133. * Function GetSize
  134. * returns text size.
  135. * @return wxSize - text size.
  136. */
  137. const wxSize& GetSize() const { return m_Size; };
  138. void SetWidth( int aWidth ) { m_Size.x = aWidth; }
  139. int GetWidth() const { return m_Size.x; }
  140. void SetHeight( int aHeight ) { m_Size.y = aHeight; }
  141. int GetHeight() const { return m_Size.y; }
  142. /// named differently than the ones using multiple inheritance and including this class
  143. void SetTextPosition( const wxPoint& aPoint ) { m_Pos = aPoint; }
  144. const wxPoint& GetTextPosition() const { return m_Pos; }
  145. void SetMultilineAllowed( bool aAllow ) { m_MultilineAllowed = aAllow; }
  146. bool IsMultilineAllowed() const { return m_MultilineAllowed; }
  147. void Offset( const wxPoint& aOffset ) { m_Pos += aOffset; }
  148. void Empty() { m_Text.Empty(); }
  149. /**
  150. * Function Draw
  151. * @param aPanel = the current DrawPanel
  152. * @param aDC = the current Device Context
  153. * @param aOffset = draw offset (usually (0,0))
  154. * @param aColor = text color
  155. * @param aDrawMode = GR_OR, GR_XOR.., -1 to use the current mode.
  156. * @param aDisplay_mode = LINE, FILLED or SKETCH
  157. * @param aAnchor_color = anchor color ( UNSPECIFIED = do not draw anchor ).
  158. */
  159. void Draw( EDA_DRAW_PANEL* aPanel, wxDC* aDC,
  160. const wxPoint& aOffset, EDA_COLOR_T aColor,
  161. GR_DRAWMODE aDrawMode, EDA_DRAW_MODE_T aDisplay_mode = LINE,
  162. EDA_COLOR_T aAnchor_color = UNSPECIFIED_COLOR );
  163. /**
  164. * Function TextHitTest
  165. * Test if \a aPoint is within the bounds of this object.
  166. * @param aPoint- A wxPoint to test
  167. * @param aAccuracy - Amount to inflate the bounding box.
  168. * @return bool - true if a hit, else false
  169. */
  170. bool TextHitTest( const wxPoint& aPoint, int aAccuracy = 0 ) const;
  171. /**
  172. * Function TextHitTest (overloaded)
  173. * Tests if object bounding box is contained within or intersects \a aRect.
  174. *
  175. * @param aRect - Rect to test against.
  176. * @param aContains - Test for containment instead of intersection if true.
  177. * @param aAccuracy - Amount to inflate the bounding box.
  178. * @return bool - true if a hit, else false
  179. */
  180. bool TextHitTest( const EDA_RECT& aRect, bool aContains = false, int aAccuracy = 0 ) const;
  181. /**
  182. * Function LenSize
  183. * @return the text length in internal units
  184. * @param aLine : the line of text to consider.
  185. * For single line text, this parameter is always m_Text
  186. */
  187. int LenSize( const wxString& aLine ) const;
  188. /**
  189. * Function GetTextBox
  190. * useful in multiline texts to calculate the full text or a line area (for
  191. * zones filling, locate functions....)
  192. * @return the rect containing the line of text (i.e. the position and the
  193. * size of one line) this rectangle is calculated for 0 orient text.
  194. * If orientation is not 0 the rect must be rotated to match the
  195. * physical area
  196. * @param aLine The line of text to consider.
  197. * for single line text, aLine is unused
  198. * If aLine == -1, the full area (considering all lines) is returned
  199. * @param aThickness Overrides the current thickness when greater than 0.
  200. * @param aInvertY Invert the Y axis when calculating bounding box.
  201. */
  202. EDA_RECT GetTextBox( int aLine = -1, int aThickness = -1, bool aInvertY = false ) const;
  203. /**
  204. * Function GetInterline
  205. * return the distance between 2 text lines
  206. * has meaning only for multiline texts
  207. */
  208. int GetInterline() const
  209. {
  210. return (( m_Size.y * 14 ) / 10) + m_Thickness;
  211. }
  212. /**
  213. * Function GetTextStyleName
  214. * @return a wxString with the style name( Normal, Italic, Bold, Bold+Italic)
  215. */
  216. wxString GetTextStyleName();
  217. EDA_TEXT_HJUSTIFY_T GetHorizJustify() const { return m_HJustify; };
  218. EDA_TEXT_VJUSTIFY_T GetVertJustify() const { return m_VJustify; };
  219. void SetHorizJustify( EDA_TEXT_HJUSTIFY_T aType ) { m_HJustify = aType; };
  220. void SetVertJustify( EDA_TEXT_VJUSTIFY_T aType ) { m_VJustify = aType; };
  221. /**
  222. * Function Format
  223. * outputs the object to \a aFormatter in s-expression form.
  224. *
  225. * @param aFormatter The #OUTPUTFORMATTER object to write to.
  226. * @param aNestLevel The indentation next level.
  227. * @param aControlBits The control bit definition for object specific formatting.
  228. * @throw IO_ERROR on write error.
  229. */
  230. virtual void Format( OUTPUTFORMATTER* aFormatter, int aNestLevel, int aControlBits ) const
  231. throw( IO_ERROR );
  232. private:
  233. /**
  234. * Function DrawOneLineOfText
  235. * Draw a single text line.
  236. * Used to draw each line of this EDA_TEXT, that can be multiline
  237. * @param aPanel = the current DrawPanel
  238. * @param aDC = the current Device Context
  239. * @param aOffset = draw offset (usually (0,0))
  240. * @param aColor = text color
  241. * @param aDrawMode = GR_OR, GR_XOR.., -1 to use the current mode.
  242. * @param aFillMode = LINE, FILLED or SKETCH
  243. * @param aAnchor_color = anchor color ( UNSPECIFIED_COLOR = do not draw anchor ).
  244. * @param aText = the single line of text to draw.
  245. * @param aPos = the position of this line ).
  246. */
  247. void DrawOneLineOfText( EDA_DRAW_PANEL* aPanel, wxDC* aDC,
  248. const wxPoint& aOffset, EDA_COLOR_T aColor,
  249. GR_DRAWMODE aDrawMode, EDA_DRAW_MODE_T aFillMode,
  250. EDA_COLOR_T aAnchor_color, wxString& aText,
  251. wxPoint aPos );
  252. };
  253. #endif // EDA_TEXT_H_