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 const std::vector<wxString>* aEmbeddedFiles = nullptr,
146 bool aForDrawingSheet = false );
147 static bool IsStroke( const wxString& aFontName );
148
149 const wxString& GetName() const { return m_fontName; };
150 inline const char* NameAsToken() const { return GetName().utf8_str().data(); }
151
162 void Draw( KIGFX::GAL* aGal, const wxString& aText, const VECTOR2I& aPosition,
163 const VECTOR2I& aCursor, const TEXT_ATTRIBUTES& aAttributes,
164 const METRICS& aFontMetrics ) const;
165
166 void Draw( KIGFX::GAL* aGal, const wxString& aText, const VECTOR2I& aPosition,
167 const TEXT_ATTRIBUTES& aAttributes, const METRICS& aFontMetrics ) const
168 {
169 Draw( aGal, aText, aPosition, VECTOR2I( 0, 0 ), aAttributes, aFontMetrics );
170 }
171
177 VECTOR2I StringBoundaryLimits( const wxString& aText, const VECTOR2I& aSize, int aThickness,
178 bool aBold, bool aItalic, const METRICS& aFontMetrics ) const;
179
183 void LinebreakText( wxString& aText, int aColumnWidth, const VECTOR2I& aGlyphSize,
184 int aThickness, bool aBold, bool aItalic ) const;
185
190 virtual double GetInterline( double aGlyphHeight, const METRICS& aFontMetrics ) const = 0;
191
206 virtual VECTOR2I GetTextAsGlyphs( BOX2I* aBBox, std::vector<std::unique_ptr<GLYPH>>* aGlyphs,
207 const wxString& aText, const VECTOR2I& aSize,
208 const VECTOR2I& aPosition, const EDA_ANGLE& aAngle,
209 bool aMirror, const VECTOR2I& aOrigin,
210 TEXT_STYLE_FLAGS aTextStyle ) const = 0;
211
212protected:
219 inline unsigned linesCount( const wxString& aText ) const
220 {
221 if( aText.empty() )
222 return 0; // std::count does not work well with empty strings
223 else
224 // aText.end() - 1 is to skip a newline character that is potentially at the end
225 return std::count( aText.begin(), aText.end() - 1, '\n' ) + 1;
226 }
227
242 void drawSingleLineText( KIGFX::GAL* aGal, BOX2I* aBoundingBox, const wxString& aText,
243 const VECTOR2I& aPosition, const VECTOR2I& aSize,
244 const EDA_ANGLE& aAngle, bool aMirror, const VECTOR2I& aOrigin,
245 bool aItalic, bool aUnderline, const METRICS& aFontMetrics ) const;
246
257 VECTOR2I boundingBoxSingleLine( BOX2I* aBBox, const wxString& aText, const VECTOR2I& aPosition,
258 const VECTOR2I& aSize, bool aItalic, const METRICS& aFontMetrics ) const;
259
260 void getLinePositions( const wxString& aText, const VECTOR2I& aPosition,
261 wxArrayString& aTextLines, std::vector<VECTOR2I>& aPositions,
262 std::vector<VECTOR2I>& aExtents, const TEXT_ATTRIBUTES& aAttrs,
263 const METRICS& aFontMetrics ) const;
264
265 VECTOR2I drawMarkup( BOX2I* aBoundingBox, std::vector<std::unique_ptr<GLYPH>>* aGlyphs,
266 const wxString& aText, const VECTOR2I& aPosition,
267 const VECTOR2I& aSize, const EDA_ANGLE& aAngle, bool aMirror,
268 const VECTOR2I& aOrigin, TEXT_STYLE_FLAGS aTextStyle,
269 const METRICS& aFontMetrics ) const;
270
271 void wordbreakMarkup( std::vector<std::pair<wxString, int>>* aWords, const wxString& aText,
272 const VECTOR2I& aSize, TEXT_STYLE_FLAGS aTextStyle ) const;
273
274private:
275 static FONT* getDefaultFont();
276
277protected:
278 wxString m_fontName;
279 wxString m_fontFileName;
280
281private:
283
284 static std::map< std::tuple<wxString, bool, bool, bool>, FONT* > s_fontMap;
285};
286
287} //namespace KIFONT
288
289
290inline std::ostream& operator<<(std::ostream& os, const KIFONT::FONT& aFont)
291{
292 os << "[Font \"" << aFont.GetName() << "\"" << ( aFont.IsStroke() ? " stroke" : "" )
293 << ( aFont.IsOutline() ? " outline" : "" ) << ( aFont.IsBold() ? " bold" : "" )
294 << ( aFont.IsItalic() ? " italic" : "" ) << "]";
295 return os;
296}
297
298
299inline std::ostream& operator<<(std::ostream& os, const KIFONT::FONT* aFont)
300{
301 os << *aFont;
302 return os;
303}
304
305#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:278
virtual ~FONT()
Definition: font.h:135
wxString m_fontFileName
Font file name.
Definition: font.h:279
virtual bool IsStroke() const
Definition: font.h:138
static FONT * s_defaultFont
Definition: font.h:282
const char * NameAsToken() const
Definition: font.h:150
void Draw(KIGFX::GAL *aGal, const wxString &aText, const VECTOR2I &aPosition, const TEXT_ATTRIBUTES &aAttributes, const METRICS &aFontMetrics) const
Definition: font.h:166
virtual bool IsItalic() const
Definition: font.h:141
virtual bool IsBold() const
Definition: font.h:140
const wxString & GetName() const
Definition: font.h:149
virtual bool IsOutline() const
Definition: font.h:139
static std::map< std::tuple< wxString, bool, bool, bool >, FONT * > s_fontMap
Definition: font.h:284
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:219
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.
std::ostream & operator<<(std::ostream &aStream, const EDA_TEXT &aText)
Definition: eda_text.cpp:1210
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:485
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:288
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
#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:691