KiCad PCB EDA Suite
Loading...
Searching...
No Matches
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 The 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, see <https://www.gnu.org/licenses/>.
21 */
22
23#include <gr_basic.h>
24#include <plotters/plotter.h>
25#include <trigo.h>
26#include <math/util.h> // for KiROUND
27#include <font/font.h>
29
30#include <callback_gal.h>
31
32
33int GetPenSizeForBold( int aTextSize )
34{
35 return KiROUND( aTextSize / 5.0 );
36}
37
38
39int GetPenSizeForDemiBold( int aTextSize )
40{
41 return KiROUND( aTextSize / 6.0 );
42}
43
44
45int GetPenSizeForBold( const wxSize& aTextSize )
46{
47 return GetPenSizeForBold( std::min( aTextSize.x, aTextSize.y ) );
48}
49
50
51int GetPenSizeForDemiBold( const wxSize& aTextSize )
52{
53 return GetPenSizeForDemiBold( std::min( aTextSize.x, aTextSize.y ) );
54}
55
56
57int GetPenSizeForNormal( int aTextSize )
58{
59 return KiROUND( aTextSize / 8.0 );
60}
61
62
63int GetPenSizeForNormal( const wxSize& aTextSize )
64{
65 return GetPenSizeForNormal( std::min( aTextSize.x, aTextSize.y ) );
66}
67
68
69int ClampTextPenSize( int aPenSize, int aSize, bool aStrict )
70{
71 double scale = aStrict ? 0.18 : 0.25;
72 int maxWidth = KiROUND( (double) aSize * scale );
73
74 return std::min( aPenSize, maxWidth );
75}
76
77
78float ClampTextPenSize( float aPenSize, int aSize, bool aStrict )
79{
80 double scale = aStrict ? 0.18 : 0.25;
81 float maxWidth = (float) aSize * scale;
82
83 return std::min( aPenSize, maxWidth );
84}
85
86
87int ClampTextPenSize( int aPenSize, const VECTOR2I& aSize, bool aStrict )
88{
89 int size = std::min( std::abs( aSize.x ), std::abs( aSize.y ) );
90
91 return ClampTextPenSize( aPenSize, size, aStrict );
92}
93
94
95int GRTextWidth( const wxString& aText, KIFONT::FONT* aFont, const VECTOR2I& aSize,
96 int aThickness, bool aBold, bool aItalic, const KIFONT::METRICS& aFontMetrics )
97{
98 if( !aFont )
99 aFont = KIFONT::FONT::GetFont();
100 wxString evaluated( aText );
101
102 if( evaluated.Contains( wxS( "@{" ) ) )
103 {
104 EXPRESSION_EVALUATOR evaluator;
105 evaluated = evaluator.Evaluate( evaluated );
106 }
107
108 return KiROUND( aFont->StringBoundaryLimits( evaluated, aSize, aThickness, aBold, aItalic,
109 aFontMetrics ).x );
110}
111
112
113void GRPrintText( wxDC* aDC, const VECTOR2I& aPos, const COLOR4D& aColor, const wxString& aText,
114 const EDA_ANGLE& aOrient, const VECTOR2I& aSize,
115 enum GR_TEXT_H_ALIGN_T aH_justify, enum GR_TEXT_V_ALIGN_T aV_justify,
116 int aWidth, bool aItalic, bool aBold, KIFONT::FONT* aFont,
117 const KIFONT::METRICS& aFontMetrics )
118{
120 bool fill_mode = true;
121 wxString evaluatedText( aText );
122
123 if( evaluatedText.Contains( wxS( "@{" ) ) )
124 {
125 EXPRESSION_EVALUATOR evaluator;
126 evaluatedText = evaluator.Evaluate( evaluatedText );
127 }
128
129 if( !aFont )
130 aFont = KIFONT::FONT::GetFont();
131
132 if( aWidth == 0 ) // Use default values if aWidth == 0
133 {
134 if( aBold )
135 aWidth = GetPenSizeForBold( std::min( aSize.x, aSize.y ) );
136 else
137 aWidth = GetPenSizeForNormal( std::min( aSize.x, aSize.y ) );
138 }
139
140 if( aWidth < 0 )
141 {
142 aWidth = -aWidth;
143 fill_mode = false;
144 }
145
146 CALLBACK_GAL callback_gal( empty_opts,
147 // Stroke callback
148 [&]( const VECTOR2I& aPt1, const VECTOR2I& aPt2 )
149 {
150 if( fill_mode )
151 GRLine( aDC, aPt1, aPt2, aWidth, aColor );
152 else
153 GRCSegm( aDC, aPt1, aPt2, aWidth, aColor );
154 },
155 // Polygon callback
156 [&]( const SHAPE_LINE_CHAIN& aPoly )
157 {
158 GRClosedPoly( aDC, aPoly.PointCount(), aPoly.CPoints().data(), true, aColor );
159 } );
160
161 TEXT_ATTRIBUTES attributes;
162 attributes.m_Angle = aOrient;
163 attributes.m_StrokeWidth = aWidth;
164 attributes.m_Italic = aItalic;
165 attributes.m_Bold = aBold;
166 attributes.m_Halign = aH_justify;
167 attributes.m_Valign = aV_justify;
168 attributes.m_Size = aSize;
169
170 aFont->Draw( &callback_gal, evaluatedText, aPos, attributes, aFontMetrics );
171}
172
173
constexpr BOX2I KiROUND(const BOX2D &aBoxD)
Definition box2.h:986
High-level wrapper for evaluating mathematical and string expressions in wxString format.
wxString Evaluate(const wxString &aInput)
Main evaluation function - processes input string and evaluates all} expressions.
FONT is an abstract base class for both outline and stroke fonts.
Definition font.h:94
static FONT * GetFont(const wxString &aFontName=wxEmptyString, bool aBold=false, bool aItalic=false, const std::vector< wxString > *aEmbeddedFiles=nullptr, bool aForDrawingSheet=false)
Definition font.cpp:143
void Draw(KIGFX::GAL *aGal, const wxString &aText, const VECTOR2I &aPosition, const VECTOR2I &aCursor, const TEXT_ATTRIBUTES &aAttributes, const METRICS &aFontMetrics, std::optional< VECTOR2I > aMousePos=std::nullopt, wxString *aActiveUrl=nullptr) const
Draw a string.
Definition font.cpp:246
VECTOR2I StringBoundaryLimits(const wxString &aText, const VECTOR2I &aSize, int aThickness, bool aBold, bool aItalic, const METRICS &aFontMetrics) const
Compute the boundary limits of aText (the bounding box of all shapes).
Definition font.cpp:447
A color representation with 4 components: red, green, blue, alpha.
Definition color4d.h:101
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:201
void GRLine(wxDC *DC, int x1, int y1, int x2, int y2, int width, const COLOR4D &Color, wxPenStyle aStyle)
Definition gr_basic.cpp:171
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:352
int GetPenSizeForBold(int aTextSize)
Definition gr_text.cpp:33
int GetPenSizeForDemiBold(int aTextSize)
Definition gr_text.cpp:39
int GetPenSizeForNormal(int aTextSize)
Definition gr_text.cpp:57
int GRTextWidth(const wxString &aText, KIFONT::FONT *aFont, const VECTOR2I &aSize, int aThickness, bool aBold, bool aItalic, const KIFONT::METRICS &aFontMetrics)
Definition gr_text.cpp:95
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, const KIFONT::METRICS &aFontMetrics)
Print a graphic text through wxDC.
Definition gr_text.cpp:113
int ClampTextPenSize(int aPenSize, int aSize, bool aStrict)
Pen width should not allow characters to become cluttered up in their own fatness.
Definition gr_text.cpp:69
EDA_ANGLE abs(const EDA_ANGLE &aAngle)
Definition eda_angle.h:400
const int scale
GR_TEXT_H_ALIGN_T
This is API surface mapped to common.types.HorizontalAlignment.
GR_TEXT_V_ALIGN_T
This is API surface mapped to common.types.VertialAlignment.
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:683