KiCad PCB EDA Suite
gr_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 (C) 2018 Jean-Pierre Charras, jp.charras at wanadoo.fr
5 * Copyright (C) 2012 SoftPLC Corporation, Dick Hollenbeck <[email protected]>
6 * Copyright (C) 2012 Wayne Stambaugh <[email protected]>
7 * Copyright (C) 1992-2022 KiCad Developers, see AUTHORS.txt for contributors.
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version 2
12 * of the License, or (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, you may find one here:
21 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
22 * or you may search the http://www.gnu.org website for the version 2 license,
23 * or you may write to the Free Software Foundation, Inc.,
24 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
25 */
26
27#include <gr_basic.h>
28#include <plotters/plotter.h>
29#include <trigo.h>
30#include <math/util.h> // for KiROUND
31#include <font/font.h>
32
33#include <callback_gal.h>
34
35
40int GetPenSizeForBold( int aTextSize )
41{
42 return KiROUND( aTextSize / 5.0 );
43}
44
45
46int GetPenSizeForBold( const wxSize& aTextSize )
47{
48 return GetPenSizeForBold( std::min( aTextSize.x, aTextSize.y ) );
49}
50
51
52int GetPenSizeForNormal( int aTextSize )
53{
54 return KiROUND( aTextSize / 8.0 );
55}
56
57
58int GetPenSizeForNormal( const wxSize& aTextSize )
59{
60 return GetPenSizeForNormal( std::min( aTextSize.x, aTextSize.y ) );
61}
62
63
75int Clamp_Text_PenSize( int aPenSize, int aSize, bool aStrict )
76{
77 double scale = aStrict ? 0.18 : 0.25;
78 int maxWidth = KiROUND( (double) aSize * scale );
79
80 return std::min( aPenSize, maxWidth );
81}
82
83
84float Clamp_Text_PenSize( float aPenSize, int aSize, bool aStrict )
85{
86 double scale = aStrict ? 0.18 : 0.25;
87 float maxWidth = (float) aSize * scale;
88
89 return std::min( aPenSize, maxWidth );
90}
91
92
93int Clamp_Text_PenSize( int aPenSize, const VECTOR2I& aSize, bool aStrict )
94{
95 int size = std::min( std::abs( aSize.x ), std::abs( aSize.y ) );
96
97 return Clamp_Text_PenSize( aPenSize, size, aStrict );
98}
99
100
101int GraphicTextWidth( const wxString& aText, KIFONT::FONT* aFont, const VECTOR2I& aSize,
102 int aThickness, bool aBold, bool aItalic )
103{
104 if( !aFont )
105 aFont = KIFONT::FONT::GetFont();
106
107 return KiROUND( aFont->StringBoundaryLimits( aText, aSize, aThickness, aBold, aItalic ).x );
108}
109
110
129void GRPrintText( wxDC* aDC, const VECTOR2I& aPos, const COLOR4D& aColor, const wxString& aText,
130 const EDA_ANGLE& aOrient, const VECTOR2I& aSize,
131 enum GR_TEXT_H_ALIGN_T aH_justify, enum GR_TEXT_V_ALIGN_T aV_justify,
132 int aWidth, bool aItalic, bool aBold, KIFONT::FONT* aFont )
133{
135 bool fill_mode = true;
136
137 if( !aFont )
138 aFont = KIFONT::FONT::GetFont();
139
140 if( aWidth == 0 && aBold ) // Use default values if aWidth == 0
141 aWidth = GetPenSizeForBold( std::min( aSize.x, aSize.y ) );
142
143 if( aWidth < 0 )
144 {
145 aWidth = -aWidth;
146 fill_mode = false;
147 }
148
149 CALLBACK_GAL callback_gal( empty_opts,
150 // Stroke callback
151 [&]( const VECTOR2I& aPt1, const VECTOR2I& aPt2 )
152 {
153 if( fill_mode )
154 GRLine( aDC, aPt1, aPt2, aWidth, aColor );
155 else
156 GRCSegm( aDC, aPt1, aPt2, aWidth, aColor );
157 },
158 // Polygon callback
159 [&]( const SHAPE_LINE_CHAIN& aPoly )
160 {
161 GRClosedPoly( aDC, aPoly.PointCount(), aPoly.CPoints().data(), true, aColor );
162 } );
163
164 TEXT_ATTRIBUTES attributes;
165 attributes.m_Angle = aOrient;
166 attributes.m_StrokeWidth = aWidth;
167 attributes.m_Italic = aItalic;
168 attributes.m_Bold = aBold;
169 attributes.m_Halign = aH_justify;
170 attributes.m_Valign = aV_justify;
171 attributes.m_Size = aSize;
172
173 aFont->Draw( &callback_gal, aText, aPos, attributes );
174}
175
176
FONT is an abstract base class for both outline and stroke fonts.
Definition: font.h:105
static FONT * GetFont(const wxString &aFontName=wxEmptyString, bool aBold=false, bool aItalic=false)
Definition: font.cpp:65
void Draw(KIGFX::GAL *aGal, const wxString &aText, const VECTOR2I &aPosition, const VECTOR2I &aCursor, const TEXT_ATTRIBUTES &aAttrs) const
Draw a string.
Definition: font.cpp:159
VECTOR2I StringBoundaryLimits(const wxString &aText, const VECTOR2I &aSize, int aThickness, bool aBold, bool aItalic) const
Compute the boundary limits of aText (the bounding box of all shapes).
Definition: font.cpp:271
A color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:104
Represent a polyline containing arcs as well as line segments: A chain of connected line and/or arc s...
void GRCSegm(wxDC *DC, const VECTOR2I &A, const VECTOR2I &B, int width, const COLOR4D &Color)
Definition: gr_basic.cpp:192
void GRLine(wxDC *DC, int x1, int y1, int x2, int y2, int width, const COLOR4D &Color, wxPenStyle aStyle)
Definition: gr_basic.cpp:162
void GRClosedPoly(wxDC *DC, int n, const VECTOR2I *Points, bool Fill, const COLOR4D &Color)
Draw a closed polyline and fill it if Fill, in object space.
Definition: gr_basic.cpp:342
int GetPenSizeForBold(int aTextSize)
Definition: gr_text.cpp:40
void GRPrintText(wxDC *aDC, const VECTOR2I &aPos, const COLOR4D &aColor, const wxString &aText, const EDA_ANGLE &aOrient, const VECTOR2I &aSize, enum GR_TEXT_H_ALIGN_T aH_justify, enum GR_TEXT_V_ALIGN_T aV_justify, int aWidth, bool aItalic, bool aBold, KIFONT::FONT *aFont)
Print a graphic text through wxDC.
Definition: gr_text.cpp:129
int GraphicTextWidth(const wxString &aText, KIFONT::FONT *aFont, const VECTOR2I &aSize, int aThickness, bool aBold, bool aItalic)
The full X size is GraphicTextWidth + the thickness of graphic lines.
Definition: gr_text.cpp:101
int GetPenSizeForNormal(int aTextSize)
Definition: gr_text.cpp:52
int Clamp_Text_PenSize(int aPenSize, int aSize, bool aStrict)
Pen width should not allow characters to become cluttered up in their own fatness.
Definition: gr_text.cpp:75
EDA_ANGLE abs(const EDA_ANGLE &aAngle)
Definition: eda_angle.h:401
Plot settings, and plotting engines (PostScript, Gerber, HPGL and DXF)
const int scale
GR_TEXT_H_ALIGN_T
GR_TEXT_V_ALIGN_T
constexpr ret_type KiROUND(fp_type v)
Round a floating point number to an integer using "round halfway cases away from zero".
Definition: util.h:85