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 The 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/font_metrics.h>
36#include <font/glyph.h>
38
39namespace KIGFX
40{
41class GAL;
42}
43
44
46{
47 BOLD = 1,
48 ITALIC = 1 << 1,
49 SUBSCRIPT = 1 << 2,
50 SUPERSCRIPT = 1 << 3,
51 OVERBAR = 1 << 4,
52 UNDERLINE = 1 << 5
53};
54
55
62static constexpr double ITALIC_TILT = 1.0 / 8;
63
64
65using TEXT_STYLE_FLAGS = unsigned int;
66
67
68inline bool IsBold( TEXT_STYLE_FLAGS aFlags )
69{
70 return aFlags & TEXT_STYLE::BOLD;
71}
72
73
74inline bool IsItalic( TEXT_STYLE_FLAGS aFlags )
75{
76 return aFlags & TEXT_STYLE::ITALIC;
77}
78
79
80inline bool IsSuperscript( TEXT_STYLE_FLAGS aFlags )
81{
82 return aFlags & TEXT_STYLE::SUPERSCRIPT;
83}
84
85
86inline bool IsSubscript( TEXT_STYLE_FLAGS aFlags )
87{
88 return aFlags & TEXT_STYLE::SUBSCRIPT;
89}
90
91
92namespace KIFONT
93{
98{
99public:
100 explicit FONT();
101
102 virtual ~FONT()
103 { }
104
105 virtual bool IsStroke() const { return false; }
106 virtual bool IsOutline() const { return false; }
107 virtual bool IsBold() const { return false; }
108 virtual bool IsItalic() const { return false; }
109
110 static FONT* GetFont( const wxString& aFontName = wxEmptyString, bool aBold = false,
111 bool aItalic = false,
112 const std::vector<wxString>* aEmbeddedFiles = nullptr,
113 bool aForDrawingSheet = false );
114 static bool IsStroke( const wxString& aFontName );
115
116 const wxString& GetName() const { return m_fontName; };
117 inline const char* NameAsToken() const { return GetName().utf8_str().data(); }
118
131 void Draw( KIGFX::GAL* aGal, const wxString& aText, const VECTOR2I& aPosition,
132 const VECTOR2I& aCursor, const TEXT_ATTRIBUTES& aAttributes,
133 const METRICS& aFontMetrics, std::optional<VECTOR2I> aMousePos = std::nullopt,
134 wxString* aActiveUrl = nullptr ) const;
135
136 void Draw( KIGFX::GAL* aGal, const wxString& aText, const VECTOR2I& aPosition,
137 const TEXT_ATTRIBUTES& aAttributes, const METRICS& aFontMetrics,
138 std::optional<VECTOR2I> aMousePos = std::nullopt, wxString* aActiveUrl = nullptr ) const
139 {
140 Draw( aGal, aText, aPosition, VECTOR2I( 0, 0 ), aAttributes, aFontMetrics, aMousePos, aActiveUrl );
141 }
142
148 VECTOR2I StringBoundaryLimits( const wxString& aText, const VECTOR2I& aSize, int aThickness,
149 bool aBold, bool aItalic, const METRICS& aFontMetrics ) const;
150
164 void LinebreakText( wxString& aText, int aColumnWidth, const VECTOR2I& aGlyphSize,
165 int aThickness, bool aBold, bool aItalic ) const;
166
171 virtual double GetInterline( double aGlyphHeight, const METRICS& aFontMetrics ) const = 0;
172
187 virtual VECTOR2I GetTextAsGlyphs( BOX2I* aBBox, std::vector<std::unique_ptr<GLYPH>>* aGlyphs,
188 const wxString& aText, const VECTOR2I& aSize,
189 const VECTOR2I& aPosition, const EDA_ANGLE& aAngle,
190 bool aMirror, const VECTOR2I& aOrigin,
191 TEXT_STYLE_FLAGS aTextStyle ) const = 0;
192
193protected:
200 inline unsigned linesCount( const wxString& aText ) const
201 {
202 if( aText.empty() )
203 return 0; // std::count does not work well with empty strings
204 else
205 // aText.end() - 1 is to skip a newline character that is potentially at the end
206 return std::count( aText.begin(), aText.end() - 1, '\n' ) + 1;
207 }
208
226 void drawSingleLineText( KIGFX::GAL* aGal, BOX2I* aBoundingBox, const wxString& aText,
227 const VECTOR2I& aPosition, const VECTOR2I& aSize,
228 const EDA_ANGLE& aAngle, bool aMirror, const VECTOR2I& aOrigin,
229 bool aItalic, bool aUnderline, bool aHover, const METRICS& aFontMetrics,
230 std::optional<VECTOR2I> aMousePos, wxString* aActiveUrl ) const;
231
243 VECTOR2I boundingBoxSingleLine( BOX2I* aBBox, const wxString& aText, const VECTOR2I& aPosition,
244 const VECTOR2I& aSize, bool aItalic,
245 const METRICS& aFontMetrics ) const;
246
247 void getLinePositions( const wxString& aText, const VECTOR2I& aPosition,
248 wxArrayString& aTextLines, std::vector<VECTOR2I>& aPositions,
249 std::vector<VECTOR2I>& aExtents, const TEXT_ATTRIBUTES& aAttrs,
250 const METRICS& aFontMetrics ) const;
251
252 VECTOR2I drawMarkup( BOX2I* aBoundingBox, std::vector<std::unique_ptr<GLYPH>>* aGlyphs,
253 const wxString& aText, const VECTOR2I& aPosition,
254 const VECTOR2I& aSize, const EDA_ANGLE& aAngle, bool aMirror,
255 const VECTOR2I& aOrigin, TEXT_STYLE_FLAGS aTextStyle,
256 const METRICS& aFontMetrics, std::optional<VECTOR2I> aMousePos = std::nullopt,
257 wxString* aActiveUrl = nullptr ) const;
258
259 void wordbreakMarkup( std::vector<std::pair<wxString, int>>* aWords, const wxString& aText,
260 const VECTOR2I& aSize, TEXT_STYLE_FLAGS aTextStyle ) const;
261
262private:
263 static FONT* getDefaultFont();
264
265protected:
266 wxString m_fontName;
267 wxString m_fontFileName;
268
269private:
271
272 static std::map< std::tuple<wxString, bool, bool, bool>, FONT* > s_fontMap;
273};
274
275} //namespace KIFONT
276
277
278inline std::ostream& operator<<(std::ostream& os, const KIFONT::FONT& aFont)
279{
280 os << "[Font \"" << aFont.GetName() << "\"" << ( aFont.IsStroke() ? " stroke" : "" )
281 << ( aFont.IsOutline() ? " outline" : "" ) << ( aFont.IsBold() ? " bold" : "" )
282 << ( aFont.IsItalic() ? " italic" : "" ) << "]";
283 return os;
284}
285
286
287inline std::ostream& operator<<(std::ostream& os, const KIFONT::FONT* aFont)
288{
289 os << *aFont;
290 return os;
291}
292
293#endif // FONT_H_
BOX2< VECTOR2I > BOX2I
Definition box2.h:922
FONT is an abstract base class for both outline and stroke fonts.
Definition font.h:98
wxString m_fontName
Font name.
Definition font.h:266
virtual ~FONT()
Definition font.h:102
wxString m_fontFileName
Font file name.
Definition font.h:267
virtual bool IsStroke() const
Definition font.h:105
static FONT * s_defaultFont
Definition font.h:270
const char * NameAsToken() const
Definition font.h:117
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:250
virtual bool IsItalic() const
Definition font.h:108
virtual bool IsBold() const
Definition font.h:107
const wxString & GetName() const
Definition font.h:116
virtual bool IsOutline() const
Definition font.h:106
void Draw(KIGFX::GAL *aGal, const wxString &aText, const VECTOR2I &aPosition, const TEXT_ATTRIBUTES &aAttributes, const METRICS &aFontMetrics, std::optional< VECTOR2I > aMousePos=std::nullopt, wxString *aActiveUrl=nullptr) const
Definition font.h:136
static std::map< std::tuple< wxString, bool, bool, bool >, FONT * > s_fontMap
Definition font.h:272
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
Return number of lines for a given text.
Definition font.h:200
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.
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, bool aInsideMarkup=false)
Break marked-up text into "words".
Definition font.cpp:503
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, std::optional< VECTOR2I > aMousePos, wxString *aActiveUrl)
Definition font.cpp:280
TEXT_STYLE
Definition font.h:46
@ BOLD
Definition font.h:47
@ SUBSCRIPT
Definition font.h:49
@ OVERBAR
Definition font.h:51
@ UNDERLINE
Definition font.h:52
@ ITALIC
Definition font.h:48
@ SUPERSCRIPT
Definition font.h:50
unsigned int TEXT_STYLE_FLAGS
Definition font.h:65
bool IsBold(TEXT_STYLE_FLAGS aFlags)
Definition font.h:68
bool IsSuperscript(TEXT_STYLE_FLAGS aFlags)
Definition font.h:80
bool IsItalic(TEXT_STYLE_FLAGS aFlags)
Definition font.h:74
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:62
bool IsSubscript(TEXT_STYLE_FLAGS aFlags)
Definition font.h:86
std::ostream & operator<<(std::ostream &os, const KIFONT::FONT &aFont)
Definition font.h:278
#define GAL_API
Definition gal.h:28
The Cairo implementation of the graphics abstraction layer.
Definition eda_group.h:33
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:695