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.

67 lines
2.3 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright The KiCad Developers
  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 3
  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 at
  18. * http://www.gnu.org/licenses/
  19. */
  20. #include "multiline_pin_text.h"
  21. #include <wx/tokenzr.h>
  22. MULTILINE_PIN_TEXT_LAYOUT ComputeMultiLinePinNumberLayout( const wxString& aText,
  23. const VECTOR2D& aAnchorPos, const TEXT_ATTRIBUTES& aAttrs )
  24. {
  25. MULTILINE_PIN_TEXT_LAYOUT layout;
  26. layout.m_StartPos = aAnchorPos;
  27. if( !( aText.StartsWith( "[" ) && aText.EndsWith( "]" ) && aText.Contains( "\n" ) ) )
  28. return layout; // not multi-line stacked
  29. wxString content = aText.Mid( 1, aText.Length() - 2 );
  30. wxArrayString lines; wxStringSplit( content, lines, '\n' );
  31. if( lines.size() <= 1 )
  32. return layout;
  33. layout.m_IsMultiLine = true;
  34. layout.m_Lines = lines;
  35. for( size_t i = 0; i < layout.m_Lines.size(); ++i )
  36. layout.m_Lines[i].Trim( true ).Trim( false );
  37. layout.m_LineSpacing = KiROUND( aAttrs.m_Size.y * 1.3 );
  38. // Apply alignment-dependent origin shift identical to sch_painter logic
  39. if( aAttrs.m_Angle == ANGLE_VERTICAL )
  40. {
  41. int totalWidth = ( (int) layout.m_Lines.size() - 1 ) * layout.m_LineSpacing;
  42. if( aAttrs.m_Halign == GR_TEXT_H_ALIGN_RIGHT )
  43. layout.m_StartPos.x -= totalWidth;
  44. else if( aAttrs.m_Halign == GR_TEXT_H_ALIGN_CENTER )
  45. layout.m_StartPos.x -= totalWidth / 2;
  46. }
  47. else
  48. {
  49. int totalHeight = ( (int) layout.m_Lines.size() - 1 ) * layout.m_LineSpacing;
  50. if( aAttrs.m_Valign == GR_TEXT_V_ALIGN_BOTTOM )
  51. layout.m_StartPos.y -= totalHeight;
  52. else if( aAttrs.m_Valign == GR_TEXT_V_ALIGN_CENTER )
  53. layout.m_StartPos.y -= totalHeight / 2;
  54. }
  55. return layout;
  56. }