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.

404 lines
11 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2004-2020 KiCad Developers, see AUTHORS.txt for contributors.
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version 2
  9. * of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, you may find one here:
  18. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  19. * or you may search the http://www.gnu.org website for the version 2 license,
  20. * or you may write to the Free Software Foundation, Inc.,
  21. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  22. */
  23. /**
  24. * @file lib_text.cpp
  25. */
  26. #include <common.h>
  27. #include <sch_draw_panel.h>
  28. #include <plotter.h>
  29. #include <gr_text.h>
  30. #include <trigo.h>
  31. #include <base_units.h>
  32. #include <widgets/msgpanel.h>
  33. #include <bitmaps.h>
  34. #include <eda_draw_frame.h>
  35. #include <lib_item.h>
  36. #include <general.h>
  37. #include <transform.h>
  38. #include <settings/color_settings.h>
  39. #include <lib_text.h>
  40. #include <default_values.h> // For some default values
  41. LIB_TEXT::LIB_TEXT( LIB_PART* aParent ) :
  42. LIB_ITEM( LIB_TEXT_T, aParent ),
  43. EDA_TEXT( wxEmptyString )
  44. {
  45. SetTextSize( wxSize( Mils2iu( DEFAULT_TEXT_SIZE ), Mils2iu( DEFAULT_TEXT_SIZE ) ) );
  46. }
  47. void LIB_TEXT::ViewGetLayers( int aLayers[], int& aCount ) const
  48. {
  49. aCount = 2;
  50. aLayers[0] = LAYER_DEVICE;
  51. aLayers[1] = LAYER_SELECTION_SHADOWS;
  52. }
  53. bool LIB_TEXT::HitTest( const wxPoint& aPosition, int aAccuracy ) const
  54. {
  55. EDA_TEXT tmp_text( *this );
  56. tmp_text.SetTextPos( DefaultTransform.TransformCoordinate( GetTextPos() ) );
  57. /* The text orientation may need to be flipped if the
  58. * transformation matrix causes xy axes to be flipped.
  59. * this simple algo works only for schematic matrix (rot 90 or/and mirror)
  60. */
  61. bool t1 = ( DefaultTransform.x1 != 0 ) ^ ( GetTextAngle() != 0 );
  62. tmp_text.SetTextAngle( t1 ? TEXT_ANGLE_HORIZ : TEXT_ANGLE_VERT );
  63. return tmp_text.TextHitTest( aPosition, aAccuracy );
  64. }
  65. EDA_ITEM* LIB_TEXT::Clone() const
  66. {
  67. LIB_TEXT* newitem = new LIB_TEXT( nullptr );
  68. newitem->m_Unit = m_Unit;
  69. newitem->m_Convert = m_Convert;
  70. newitem->m_Flags = m_Flags;
  71. newitem->SetText( GetText() );
  72. newitem->SetEffects( *this );
  73. return newitem;
  74. }
  75. int LIB_TEXT::compare( const LIB_ITEM& aOther, LIB_ITEM::COMPARE_FLAGS aCompareFlags ) const
  76. {
  77. wxASSERT( aOther.Type() == LIB_TEXT_T );
  78. int retv = LIB_ITEM::compare( aOther, aCompareFlags );
  79. if( retv )
  80. return retv;
  81. const LIB_TEXT* tmp = ( LIB_TEXT* ) &aOther;
  82. int result = GetText().CmpNoCase( tmp->GetText() );
  83. if( result != 0 )
  84. return result;
  85. if( GetTextPos().x != tmp->GetTextPos().x )
  86. return GetTextPos().x - tmp->GetTextPos().x;
  87. if( GetTextPos().y != tmp->GetTextPos().y )
  88. return GetTextPos().y - tmp->GetTextPos().y;
  89. if( GetTextWidth() != tmp->GetTextWidth() )
  90. return GetTextWidth() - tmp->GetTextWidth();
  91. if( GetTextHeight() != tmp->GetTextHeight() )
  92. return GetTextHeight() - tmp->GetTextHeight();
  93. return 0;
  94. }
  95. void LIB_TEXT::Offset( const wxPoint& aOffset )
  96. {
  97. EDA_TEXT::Offset( aOffset );
  98. }
  99. void LIB_TEXT::MoveTo( const wxPoint& newPosition )
  100. {
  101. SetTextPos( newPosition );
  102. }
  103. void LIB_TEXT::NormalizeJustification( bool inverse )
  104. {
  105. wxPoint delta( 0, 0 );
  106. EDA_RECT bbox = GetTextBox();
  107. if( GetTextAngle() == 0.0 )
  108. {
  109. if( GetHorizJustify() == GR_TEXT_HJUSTIFY_LEFT )
  110. delta.x = bbox.GetWidth() / 2;
  111. else if( GetHorizJustify() == GR_TEXT_HJUSTIFY_RIGHT )
  112. delta.x = - bbox.GetWidth() / 2;
  113. if( GetVertJustify() == GR_TEXT_VJUSTIFY_TOP )
  114. delta.y = - bbox.GetHeight() / 2;
  115. else if( GetVertJustify() == GR_TEXT_VJUSTIFY_BOTTOM )
  116. delta.y = bbox.GetHeight() / 2;
  117. }
  118. else
  119. {
  120. if( GetHorizJustify() == GR_TEXT_HJUSTIFY_LEFT )
  121. delta.y = bbox.GetWidth() / 2;
  122. else if( GetHorizJustify() == GR_TEXT_HJUSTIFY_RIGHT )
  123. delta.y = - bbox.GetWidth() / 2;
  124. if( GetVertJustify() == GR_TEXT_VJUSTIFY_TOP )
  125. delta.x = + bbox.GetHeight() / 2;
  126. else if( GetVertJustify() == GR_TEXT_VJUSTIFY_BOTTOM )
  127. delta.x = - bbox.GetHeight() / 2;
  128. }
  129. if( inverse )
  130. SetTextPos( GetTextPos() - delta );
  131. else
  132. SetTextPos( GetTextPos() + delta );
  133. }
  134. void LIB_TEXT::MirrorHorizontal( const wxPoint& center )
  135. {
  136. NormalizeJustification( false );
  137. int x = GetTextPos().x;
  138. x -= center.x;
  139. x *= -1;
  140. x += center.x;
  141. if( GetTextAngle() == 0.0 )
  142. {
  143. if( GetHorizJustify() == GR_TEXT_HJUSTIFY_LEFT )
  144. SetHorizJustify( GR_TEXT_HJUSTIFY_RIGHT );
  145. else if( GetHorizJustify() == GR_TEXT_HJUSTIFY_RIGHT )
  146. SetHorizJustify( GR_TEXT_HJUSTIFY_LEFT );
  147. }
  148. else
  149. {
  150. if( GetVertJustify() == GR_TEXT_VJUSTIFY_TOP )
  151. SetVertJustify( GR_TEXT_VJUSTIFY_BOTTOM );
  152. else if( GetVertJustify() == GR_TEXT_VJUSTIFY_BOTTOM )
  153. SetVertJustify( GR_TEXT_VJUSTIFY_TOP );
  154. }
  155. SetTextX( x );
  156. NormalizeJustification( true );
  157. }
  158. void LIB_TEXT::MirrorVertical( const wxPoint& center )
  159. {
  160. NormalizeJustification( false );
  161. int y = GetTextPos().y;
  162. y -= center.y;
  163. y *= -1;
  164. y += center.y;
  165. if( GetTextAngle() == 0.0 )
  166. {
  167. if( GetVertJustify() == GR_TEXT_VJUSTIFY_TOP )
  168. SetVertJustify( GR_TEXT_VJUSTIFY_BOTTOM );
  169. else if( GetVertJustify() == GR_TEXT_VJUSTIFY_BOTTOM )
  170. SetVertJustify( GR_TEXT_VJUSTIFY_TOP );
  171. }
  172. else
  173. {
  174. if( GetHorizJustify() == GR_TEXT_HJUSTIFY_LEFT )
  175. SetHorizJustify( GR_TEXT_HJUSTIFY_RIGHT );
  176. else if( GetHorizJustify() == GR_TEXT_HJUSTIFY_RIGHT )
  177. SetHorizJustify( GR_TEXT_HJUSTIFY_LEFT );
  178. }
  179. SetTextY( y );
  180. NormalizeJustification( true );
  181. }
  182. void LIB_TEXT::Rotate( const wxPoint& center, bool aRotateCCW )
  183. {
  184. NormalizeJustification( false );
  185. int rot_angle = aRotateCCW ? -900 : 900;
  186. wxPoint pt = GetTextPos();
  187. RotatePoint( &pt, center, rot_angle );
  188. SetTextPos( pt );
  189. if( GetTextAngle() == 0.0 )
  190. {
  191. SetTextAngle( 900 );
  192. }
  193. else
  194. {
  195. // 180º of rotation is a mirror
  196. if( GetHorizJustify() == GR_TEXT_HJUSTIFY_LEFT )
  197. SetHorizJustify( GR_TEXT_HJUSTIFY_RIGHT );
  198. else if( GetHorizJustify() == GR_TEXT_HJUSTIFY_RIGHT )
  199. SetHorizJustify( GR_TEXT_HJUSTIFY_LEFT );
  200. if( GetVertJustify() == GR_TEXT_VJUSTIFY_TOP )
  201. SetVertJustify( GR_TEXT_VJUSTIFY_BOTTOM );
  202. else if( GetVertJustify() == GR_TEXT_VJUSTIFY_BOTTOM )
  203. SetVertJustify( GR_TEXT_VJUSTIFY_TOP );
  204. SetTextAngle( 0 );
  205. }
  206. NormalizeJustification( true );
  207. }
  208. void LIB_TEXT::Plot( PLOTTER* plotter, const wxPoint& offset, bool fill,
  209. const TRANSFORM& aTransform )
  210. {
  211. wxASSERT( plotter != NULL );
  212. EDA_RECT bBox = GetBoundingBox();
  213. // convert coordinates from draw Y axis to symbol_editor Y axis
  214. bBox.RevertYAxis();
  215. wxPoint txtpos = bBox.Centre();
  216. /* The text orientation may need to be flipped if the
  217. * transformation matrix causes xy axes to be flipped. */
  218. int t1 = ( aTransform.x1 != 0 ) ^ ( GetTextAngle() != 0 );
  219. wxPoint pos = aTransform.TransformCoordinate( txtpos ) + offset;
  220. // Get color
  221. COLOR4D color;
  222. if( plotter->GetColorMode() ) // Used normal color or selected color
  223. color = plotter->RenderSettings()->GetLayerColor( LAYER_DEVICE );
  224. else
  225. color = COLOR4D::BLACK;
  226. RENDER_SETTINGS* settings = plotter->RenderSettings();
  227. int penWidth = std::max( GetEffectiveTextPenWidth(), settings->GetMinPenWidth() );
  228. plotter->Text( pos, color, GetText(), t1 ? TEXT_ANGLE_HORIZ : TEXT_ANGLE_VERT, GetTextSize(),
  229. GR_TEXT_HJUSTIFY_CENTER, GR_TEXT_VJUSTIFY_CENTER, penWidth, IsItalic(),
  230. IsBold() );
  231. }
  232. int LIB_TEXT::GetPenWidth() const
  233. {
  234. return GetEffectiveTextPenWidth();
  235. }
  236. void LIB_TEXT::print( RENDER_SETTINGS* aSettings, const wxPoint& aOffset, void* aData,
  237. const TRANSFORM& aTransform )
  238. {
  239. wxDC* DC = aSettings->GetPrintDC();
  240. COLOR4D color = aSettings->GetLayerColor( LAYER_DEVICE );
  241. int penWidth = std::max( GetEffectiveTextPenWidth(), aSettings->GetDefaultPenWidth() );
  242. /* Calculate the text orientation, according to the component
  243. * orientation/mirror (needed when draw text in schematic)
  244. */
  245. int orient = (int) GetTextAngle();
  246. if( aTransform.y1 ) // Rotate component 90 degrees.
  247. {
  248. if( orient == TEXT_ANGLE_HORIZ )
  249. orient = TEXT_ANGLE_VERT;
  250. else
  251. orient = TEXT_ANGLE_HORIZ;
  252. }
  253. /* Calculate the text justification, according to the component
  254. * orientation/mirror this is a bit complicated due to cumulative
  255. * calculations:
  256. * - numerous cases (mirrored or not, rotation)
  257. * - the DrawGraphicText function recalculate also H and H justifications
  258. * according to the text orientation.
  259. * - When a component is mirrored, the text is not mirrored and
  260. * justifications are complicated to calculate
  261. * so the more easily way is to use no justifications ( Centered text )
  262. * and use GetBoundaryBox to know the text coordinate considered as centered
  263. */
  264. EDA_RECT bBox = GetBoundingBox();
  265. // convert coordinates from draw Y axis to symbol_editor Y axis:
  266. bBox.RevertYAxis();
  267. wxPoint txtpos = bBox.Centre();
  268. // Calculate pos according to mirror/rotation.
  269. txtpos = aTransform.TransformCoordinate( txtpos ) + aOffset;
  270. GRText( DC, txtpos, color, GetShownText(), orient, GetTextSize(), GR_TEXT_HJUSTIFY_CENTER,
  271. GR_TEXT_VJUSTIFY_CENTER, penWidth, IsItalic(), IsBold() );
  272. }
  273. void LIB_TEXT::GetMsgPanelInfo( EDA_DRAW_FRAME* aFrame, MSG_PANEL_ITEMS& aList )
  274. {
  275. LIB_ITEM::GetMsgPanelInfo( aFrame, aList );
  276. wxString msg = MessageTextFromValue( aFrame->GetUserUnits(), GetTextThickness() );
  277. aList.push_back( MSG_PANEL_ITEM( _( "Line Width" ), msg, BLUE ) );
  278. }
  279. const EDA_RECT LIB_TEXT::GetBoundingBox() const
  280. {
  281. /* Y coordinates for LIB_ITEMS are bottom to top, so we must invert the Y position when
  282. * calling GetTextBox() that works using top to bottom Y axis orientation.
  283. */
  284. EDA_RECT rect = GetTextBox( -1, true );
  285. rect.RevertYAxis();
  286. // We are using now a bottom to top Y axis.
  287. wxPoint orig = rect.GetOrigin();
  288. wxPoint end = rect.GetEnd();
  289. RotatePoint( &orig, GetTextPos(), -GetTextAngle() );
  290. RotatePoint( &end, GetTextPos(), -GetTextAngle() );
  291. rect.SetOrigin( orig );
  292. rect.SetEnd( end );
  293. // We are using now a top to bottom Y axis:
  294. rect.RevertYAxis();
  295. return rect;
  296. }
  297. wxString LIB_TEXT::GetSelectMenuText( EDA_UNITS aUnits ) const
  298. {
  299. return wxString::Format( _( "Graphic Text '%s'" ), ShortenedShownText() );
  300. }
  301. BITMAP_DEF LIB_TEXT::GetMenuImage() const
  302. {
  303. return text_xpm;
  304. }
  305. void LIB_TEXT::BeginEdit( const wxPoint aPosition )
  306. {
  307. SetTextPos( aPosition );
  308. }
  309. void LIB_TEXT::CalcEdit( const wxPoint& aPosition )
  310. {
  311. SetTextPos( aPosition );
  312. }