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, you may find one at
18 * http://www.gnu.org/licenses/
19 */
20
21
22#include "multiline_pin_text.h"
23
24#include <wx/tokenzr.h>
25
27 const VECTOR2D& aAnchorPos, const TEXT_ATTRIBUTES& aAttrs )
28{
30 layout.m_StartPos = aAnchorPos;
31
32 if( !( aText.StartsWith( "[" ) && aText.EndsWith( "]" ) && aText.Contains( "\n" ) ) )
33 return layout; // not multi-line stacked
34
35 wxString content = aText.Mid( 1, aText.Length() - 2 );
36 wxArrayString lines; wxStringSplit( content, lines, '\n' );
37 if( lines.size() <= 1 )
38 return layout;
39
40 layout.m_IsMultiLine = true;
41
42 layout.m_Lines = lines;
43 for( size_t i = 0; i < layout.m_Lines.size(); ++i )
44 layout.m_Lines[i].Trim( true ).Trim( false );
45
46 layout.m_LineSpacing = KiROUND( aAttrs.m_Size.y * 1.3 );
47
48 // Apply alignment-dependent origin shift identical to sch_painter logic
49 if( aAttrs.m_Angle == ANGLE_VERTICAL )
50 {
51 int totalWidth = ( (int) layout.m_Lines.size() - 1 ) * layout.m_LineSpacing;
52 if( aAttrs.m_Halign == GR_TEXT_H_ALIGN_RIGHT )
53 layout.m_StartPos.x -= totalWidth;
54 else if( aAttrs.m_Halign == GR_TEXT_H_ALIGN_CENTER )
55 layout.m_StartPos.x -= totalWidth / 2;
56 }
57 else
58 {
59 int totalHeight = ( (int) layout.m_Lines.size() - 1 ) * layout.m_LineSpacing;
60 if( aAttrs.m_Valign == GR_TEXT_V_ALIGN_BOTTOM )
61 layout.m_StartPos.y -= totalHeight;
62 else if( aAttrs.m_Valign == GR_TEXT_V_ALIGN_CENTER )
63 layout.m_StartPos.y -= totalHeight / 2;
64 }
65
66 return layout;
67}
constexpr BOX2I KiROUND(const BOX2D &aBoxD)
Definition box2.h:990
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:694