KiCad PCB EDA Suite
Loading...
Searching...
No Matches
multiline_pin_text.cpp
Go to the documentation of this file.
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, see <https://www.gnu.org/licenses/>.
18 */
19
20
21#include "multiline_pin_text.h"
22
23#include <wx/tokenzr.h>
24
26 const VECTOR2D& aAnchorPos, const TEXT_ATTRIBUTES& aAttrs )
27{
29 layout.m_StartPos = aAnchorPos;
30
31 if( !( aText.StartsWith( "[" ) && aText.EndsWith( "]" ) && aText.Contains( "\n" ) ) )
32 return layout; // not multi-line stacked
33
34 wxString content = aText.Mid( 1, aText.Length() - 2 );
35 wxArrayString lines; wxStringSplit( content, lines, '\n' );
36 if( lines.size() <= 1 )
37 return layout;
38
39 layout.m_IsMultiLine = true;
40
41 layout.m_Lines = lines;
42 for( size_t i = 0; i < layout.m_Lines.size(); ++i )
43 layout.m_Lines[i].Trim( true ).Trim( false );
44
45 layout.m_LineSpacing = KiROUND( aAttrs.m_Size.y * 1.3 );
46
47 // Apply alignment-dependent origin shift identical to sch_painter logic
48 if( aAttrs.m_Angle == ANGLE_VERTICAL )
49 {
50 int totalWidth = ( (int) layout.m_Lines.size() - 1 ) * layout.m_LineSpacing;
51 if( aAttrs.m_Halign == GR_TEXT_H_ALIGN_RIGHT )
52 layout.m_StartPos.x -= totalWidth;
53 else if( aAttrs.m_Halign == GR_TEXT_H_ALIGN_CENTER )
54 layout.m_StartPos.x -= totalWidth / 2;
55 }
56 else
57 {
58 int totalHeight = ( (int) layout.m_Lines.size() - 1 ) * layout.m_LineSpacing;
59 if( aAttrs.m_Valign == GR_TEXT_V_ALIGN_BOTTOM )
60 layout.m_StartPos.y -= totalHeight;
61 else if( aAttrs.m_Valign == GR_TEXT_V_ALIGN_CENTER )
62 layout.m_StartPos.y -= totalHeight / 2;
63 }
64
65 return layout;
66}
constexpr BOX2I KiROUND(const BOX2D &aBoxD)
Definition box2.h:986
GR_TEXT_H_ALIGN_T m_Halign
GR_TEXT_V_ALIGN_T m_Valign
static constexpr EDA_ANGLE ANGLE_VERTICAL
Definition eda_angle.h:408
MULTILINE_PIN_TEXT_LAYOUT ComputeMultiLinePinNumberLayout(const wxString &aText, const VECTOR2D &aAnchorPos, const TEXT_ATTRIBUTES &aAttrs)
void wxStringSplit(const wxString &aText, wxArrayString &aStrings, wxChar aSplitter)
Split aString to a string list separated at aSplitter.
@ GR_TEXT_H_ALIGN_CENTER
@ GR_TEXT_H_ALIGN_RIGHT
@ GR_TEXT_V_ALIGN_BOTTOM
@ GR_TEXT_V_ALIGN_CENTER
VECTOR2< double > VECTOR2D
Definition vector2d.h:682