KiCad PCB EDA Suite
Loading...
Searching...
No Matches
font.h
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) 2021 Ola Rinta-Koski
5 * Copyright (C) 2021-2023 Kicad Developers, see AUTHORS.txt for contributors.
6 *
7 * Font abstract base class
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#ifndef FONT_H_
28#define FONT_H_
29
30#include <gal/gal.h>
31#include <iostream>
32#include <map>
33#include <algorithm>
34#include <wx/string.h>
35#include <font/glyph.h>
37
38namespace KIGFX
39{
40class GAL;
41}
42
43
45{
46 BOLD = 1,
47 ITALIC = 1 << 1,
48 SUBSCRIPT = 1 << 2,
49 SUPERSCRIPT = 1 << 3,
50 OVERBAR = 1 << 4,
51 UNDERLINE = 1 << 5
52};
53
54
61static constexpr double ITALIC_TILT = 1.0 / 8;
62
63
64using TEXT_STYLE_FLAGS = unsigned int;
65
66
67inline bool IsBold( TEXT_STYLE_FLAGS aFlags )
68{
69 return aFlags & TEXT_STYLE::BOLD;
70}
71
72
73inline bool IsItalic( TEXT_STYLE_FLAGS aFlags )
74{
75 return aFlags & TEXT_STYLE::ITALIC;
76}
77
78
79inline bool IsSuperscript( TEXT_STYLE_FLAGS aFlags )
80{
81 return aFlags & TEXT_STYLE::SUPERSCRIPT;
82}
83
84
85inline bool IsSubscript( TEXT_STYLE_FLAGS aFlags )
86{
87 return aFlags & TEXT_STYLE::SUBSCRIPT;
88}
89
90
91namespace KIFONT
92{
94{
95public:
100 double GetOverbarVerticalPosition( double aGlyphHeight ) const
101 {
102 return aGlyphHeight * m_OverbarHeight;
103 }
104
109 double GetUnderlineVerticalPosition( double aGlyphHeight ) const
110 {
111 return aGlyphHeight * m_UnderlineOffset;
112 }
113
114 double GetInterline( double aFontHeight ) const
115 {
116 return aFontHeight * m_InterlinePitch;
117 }
118
119 static const METRICS& Default();
120
121public:
122 double m_InterlinePitch = 1.68;
123 double m_OverbarHeight = 1.23;
124 double m_UnderlineOffset = -0.16;
125};
126
131{
132public:
133 explicit FONT();
134
135 virtual ~FONT()
136 { }
137
138 virtual bool IsStroke() const { return false; }
139 virtual bool IsOutline() const { return false; }
140 virtual bool IsBold() const { return false; }
141 virtual bool IsItalic() const { return false; }
142
143 static FONT* GetFont( const wxString& aFontName = wxEmptyString, bool aBold = false,
144 bool aItalic = false );
145 static bool IsStroke( const wxString& aFontName );
146
147 const wxString& GetName() const { return m_fontName; };
148 inline const char* NameAsToken() const { return GetName().utf8_str().data(); }
149
160 void Draw( KIGFX::GAL* aGal, const wxString& aText, const VECTOR2I& aPosition,
161 const VECTOR2I& aCursor, const TEXT_ATTRIBUTES& aAttributes,
162 const METRICS& aFontMetrics ) const;
163
164 void Draw( KIGFX::GAL* aGal, const wxString& aText, const VECTOR2I& aPosition,
165 const TEXT_ATTRIBUTES& aAttributes, const METRICS& aFontMetrics ) const
166 {
167 Draw( aGal, aText, aPosition, VECTOR2I( 0, 0 ), aAttributes, aFontMetrics );
168 }
169
175 VECTOR2I StringBoundaryLimits( const wxString& aText, const VECTOR2I& aSize, int aThickness,
176 bool aBold, bool aItalic, const METRICS& aFontMetrics ) const;
177
181 void LinebreakText( wxString& aText, int aColumnWidth, const VECTOR2I& aGlyphSize,
182 int aThickness, bool aBold, bool aItalic ) const;
183
188 virtual double GetInterline( double aGlyphHeight, const METRICS& aFontMetrics ) const = 0;
189
204 virtual VECTOR2I GetTextAsGlyphs( BOX2I* aBBox, std::vector<std::unique_ptr<GLYPH>>* aGlyphs,
205 const wxString& aText, const VECTOR2I& aSize,
206 const VECTOR2I& aPosition, const EDA_ANGLE& aAngle,
207 bool aMirror, const VECTOR2I& aOrigin,
208 TEXT_STYLE_FLAGS aTextStyle ) const = 0;
209
210protected:
217 inline unsigned linesCount( const wxString& aText ) const
218 {
219 if( aText.empty() )
220 return 0; // std::count does not work well with empty strings
221 else
222 // aText.end() - 1 is to skip a newline character that is potentially at the end
223 return std::count( aText.begin(), aText.end() - 1, '\n' ) + 1;
224 }
225
240 void drawSingleLineText( KIGFX::GAL* aGal, BOX2I* aBoundingBox, const wxString& aText,
241 const VECTOR2I& aPosition, const VECTOR2I& aSize,
242 const EDA_ANGLE& aAngle, bool aMirror, const VECTOR2I& aOrigin,
243 bool aItalic, bool aUnderline, const METRICS& aFontMetrics ) const;
244
255 VECTOR2I boundingBoxSingleLine( BOX2I* aBBox, const wxString& aText, const VECTOR2I& aPosition,
256 const VECTOR2I& aSize, bool aItalic, const METRICS& aFontMetrics ) const;
257
258 void getLinePositions( const wxString& aText, const VECTOR2I& aPosition,
259 wxArrayString& aTextLines, std::vector<VECTOR2I>& aPositions,
260 std::vector<VECTOR2I>& aExtents, const TEXT_ATTRIBUTES& aAttrs,
261 const METRICS& aFontMetrics ) const;
262
263 VECTOR2I drawMarkup( BOX2I* aBoundingBox, std::vector<std::unique_ptr<GLYPH>>* aGlyphs,
264 const wxString& aText, const VECTOR2I& aPosition,
265 const VECTOR2I& aSize, const EDA_ANGLE& aAngle, bool aMirror,
266 const VECTOR2I& aOrigin, TEXT_STYLE_FLAGS aTextStyle,
267 const METRICS& aFontMetrics ) const;
268
269 void wordbreakMarkup( std::vector<std::pair<wxString, int>>* aWords, const wxString& aText,
270 const VECTOR2I& aSize, TEXT_STYLE_FLAGS aTextStyle ) const;
271
272private:
273 static FONT* getDefaultFont();
274
275protected:
276 wxString m_fontName;
277 wxString m_fontFileName;
278
279private:
281
282 static std::map< std::tuple<wxString, bool, bool>, FONT* > s_fontMap;
283};
284
285} //namespace KIFONT
286
287
288inline std::ostream& operator<<(std::ostream& os, const KIFONT::FONT& aFont)
289{
290 os << "[Font \"" << aFont.GetName() << "\"" << ( aFont.IsStroke() ? " stroke" : "" )
291 << ( aFont.IsOutline() ? " outline" : "" ) << ( aFont.IsBold() ? " bold" : "" )
292 << ( aFont.IsItalic() ? " italic" : "" ) << "]";
293 return os;
294}
295
296
297inline std::ostream& operator<<(std::ostream& os, const KIFONT::FONT* aFont)
298{
299 os << *aFont;
300 return os;
301}
302
303#endif // FONT_H_
FONT is an abstract base class for both outline and stroke fonts.
Definition: font.h:131
wxString m_fontName
Font name.
Definition: font.h:276
virtual ~FONT()
Definition: font.h:135
wxString m_fontFileName
Font file name.
Definition: font.h:277
virtual bool IsStroke() const
Definition: font.h:138
static FONT * s_defaultFont
Definition: font.h:280
const char * NameAsToken() const
Definition: font.h:148
void Draw(KIGFX::GAL *aGal, const wxString &aText, const VECTOR2I &aPosition, const TEXT_ATTRIBUTES &aAttributes, const METRICS &aFontMetrics) const
Definition: font.h:164
virtual bool IsItalic() const
Definition: font.h:141
virtual bool IsBold() const
Definition: font.h:140
const wxString & GetName() const
Definition: font.h:147
virtual bool IsOutline() const
Definition: font.h:139
static std::map< std::tuple< wxString, bool, bool >, FONT * > s_fontMap
Definition: font.h:282
virtual double GetInterline(double aGlyphHeight, const METRICS &aFontMetrics) const =0
Compute the distance (interline) between 2 lines of text (for multiline texts).
unsigned linesCount(const wxString &aText) const
Returns number of lines for a given text.
Definition: font.h:217
virtual VECTOR2I GetTextAsGlyphs(BOX2I *aBBox, std::vector< std::unique_ptr< GLYPH > > *aGlyphs, const wxString &aText, const VECTOR2I &aSize, const VECTOR2I &aPosition, const EDA_ANGLE &aAngle, bool aMirror, const VECTOR2I &aOrigin, TEXT_STYLE_FLAGS aTextStyle) const =0
Convert text string to an array of GLYPHs.
double GetUnderlineVerticalPosition(double aGlyphHeight) const
Compute the vertical position of an underline.
Definition: font.h:109
double GetOverbarVerticalPosition(double aGlyphHeight) const
Compute the vertical position of an overbar.
Definition: font.h:100
double GetInterline(double aFontHeight) const
Definition: font.h:114
Abstract interface for drawing on a 2D-surface.
void wordbreakMarkup(std::vector< std::pair< wxString, int > > *aWords, const std::unique_ptr< MARKUP::NODE > &aNode, const KIFONT::FONT *aFont, const VECTOR2I &aSize, TEXT_STYLE_FLAGS aTextStyle)
Definition: font.cpp:484
VECTOR2I drawMarkup(BOX2I *aBoundingBox, std::vector< std::unique_ptr< GLYPH > > *aGlyphs, const MARKUP::NODE *aNode, const VECTOR2I &aPosition, const KIFONT::FONT *aFont, const VECTOR2I &aSize, const EDA_ANGLE &aAngle, bool aMirror, const VECTOR2I &aOrigin, TEXT_STYLE_FLAGS aTextStyle, const METRICS &aFontMetrics)
Definition: font.cpp:287
TEXT_STYLE
Definition: font.h:45
@ BOLD
Definition: font.h:46
@ SUBSCRIPT
Definition: font.h:48
@ OVERBAR
Definition: font.h:50
@ UNDERLINE
Definition: font.h:51
@ ITALIC
Definition: font.h:47
@ SUPERSCRIPT
Definition: font.h:49
unsigned int TEXT_STYLE_FLAGS
Definition: font.h:64
bool IsBold(TEXT_STYLE_FLAGS aFlags)
Definition: font.h:67
bool IsSuperscript(TEXT_STYLE_FLAGS aFlags)
Definition: font.h:79
bool IsItalic(TEXT_STYLE_FLAGS aFlags)
Definition: font.h:73
static constexpr double ITALIC_TILT
Tilt factor for italic style (this is the scaling factor on dY relative coordinates to give a tilted ...
Definition: font.h:61
bool IsSubscript(TEXT_STYLE_FLAGS aFlags)
Definition: font.h:85
std::ostream & operator<<(std::ostream &os, const KIFONT::FONT &aFont)
Definition: font.h:288
#define GAL_API
Definition: gal.h:28
The Cairo implementation of the graphics abstraction layer.
Definition: color4d.cpp:247
VECTOR2< int32_t > VECTOR2I
Definition: vector2d.h:638