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, see <https://www.gnu.org/licenses/>.
21 */
22
23#ifndef FONT_H_
24#define FONT_H_
25
26#include <gal/gal.h>
27#include <iostream>
28#include <map>
29#include <algorithm>
30#include <wx/string.h>
31#include <font/font_metrics.h>
32#include <font/glyph.h>
34
35namespace KIGFX
36{
37class GAL;
38}
39
40
42{
43 BOLD = 1,
44 ITALIC = 1 << 1,
45 SUBSCRIPT = 1 << 2,
46 SUPERSCRIPT = 1 << 3,
47 OVERBAR = 1 << 4,
48 UNDERLINE = 1 << 5
49};
50
51
58static constexpr double ITALIC_TILT = 1.0 / 8;
59
60
61using TEXT_STYLE_FLAGS = unsigned int;
62
63
64inline bool IsBold( TEXT_STYLE_FLAGS aFlags )
65{
66 return aFlags & TEXT_STYLE::BOLD;
67}
68
69
70inline bool IsItalic( TEXT_STYLE_FLAGS aFlags )
71{
72 return aFlags & TEXT_STYLE::ITALIC;
73}
74
75
76inline bool IsSuperscript( TEXT_STYLE_FLAGS aFlags )
77{
78 return aFlags & TEXT_STYLE::SUPERSCRIPT;
79}
80
81
82inline bool IsSubscript( TEXT_STYLE_FLAGS aFlags )
83{
84 return aFlags & TEXT_STYLE::SUBSCRIPT;
85}
86
87
88namespace KIFONT
89{
94{
95public:
96 explicit FONT();
97
98 virtual ~FONT()
99 { }
100
101 virtual bool IsStroke() const { return false; }
102 virtual bool IsOutline() const { return false; }
103 virtual bool IsBold() const { return false; }
104 virtual bool IsItalic() const { return false; }
105
106 static FONT* GetFont( const wxString& aFontName = wxEmptyString, bool aBold = false,
107 bool aItalic = false,
108 const std::vector<wxString>* aEmbeddedFiles = nullptr,
109 bool aForDrawingSheet = false );
110 static bool IsStroke( const wxString& aFontName );
111
112 const wxString& GetName() const { return m_fontName; };
113 inline const char* NameAsToken() const { return GetName().utf8_str().data(); }
114
127 void Draw( KIGFX::GAL* aGal, const wxString& aText, const VECTOR2I& aPosition,
128 const VECTOR2I& aCursor, const TEXT_ATTRIBUTES& aAttributes,
129 const METRICS& aFontMetrics, std::optional<VECTOR2I> aMousePos = std::nullopt,
130 wxString* aActiveUrl = nullptr ) const;
131
132 void Draw( KIGFX::GAL* aGal, const wxString& aText, const VECTOR2I& aPosition,
133 const TEXT_ATTRIBUTES& aAttributes, const METRICS& aFontMetrics,
134 std::optional<VECTOR2I> aMousePos = std::nullopt, wxString* aActiveUrl = nullptr ) const
135 {
136 Draw( aGal, aText, aPosition, VECTOR2I( 0, 0 ), aAttributes, aFontMetrics, aMousePos, aActiveUrl );
137 }
138
144 VECTOR2I StringBoundaryLimits( const wxString& aText, const VECTOR2I& aSize, int aThickness,
145 bool aBold, bool aItalic, const METRICS& aFontMetrics ) const;
146
151 VECTOR2I GetAlignedDrawPosition( const wxString& aText, const VECTOR2I& aAnchor, const TEXT_ATTRIBUTES& aAttributes,
152 const METRICS& aFontMetrics ) const;
153
167 void LinebreakText( wxString& aText, int aColumnWidth, const VECTOR2I& aGlyphSize,
168 int aThickness, bool aBold, bool aItalic ) const;
169
174 virtual double GetInterline( double aGlyphHeight, const METRICS& aFontMetrics ) const = 0;
175
190 virtual VECTOR2I GetTextAsGlyphs( BOX2I* aBBox, std::vector<std::unique_ptr<GLYPH>>* aGlyphs,
191 const wxString& aText, const VECTOR2I& aSize,
192 const VECTOR2I& aPosition, const EDA_ANGLE& aAngle,
193 bool aMirror, const VECTOR2I& aOrigin,
194 TEXT_STYLE_FLAGS aTextStyle ) const = 0;
195
196protected:
203 inline unsigned linesCount( const wxString& aText ) const
204 {
205 if( aText.empty() )
206 return 0; // std::count does not work well with empty strings
207 else
208 // aText.end() - 1 is to skip a newline character that is potentially at the end
209 return std::count( aText.begin(), aText.end() - 1, '\n' ) + 1;
210 }
211
229 void drawSingleLineText( KIGFX::GAL* aGal, BOX2I* aBoundingBox, const wxString& aText,
230 const VECTOR2I& aPosition, const VECTOR2I& aSize,
231 const EDA_ANGLE& aAngle, bool aMirror, const VECTOR2I& aOrigin,
232 bool aItalic, bool aUnderline, bool aHover, const METRICS& aFontMetrics,
233 std::optional<VECTOR2I> aMousePos, wxString* aActiveUrl ) const;
234
246 VECTOR2I boundingBoxSingleLine( BOX2I* aBBox, const wxString& aText, const VECTOR2I& aPosition,
247 const VECTOR2I& aSize, bool aItalic,
248 const METRICS& aFontMetrics ) const;
249
250 void getLinePositions( const wxString& aText, const VECTOR2I& aPosition,
251 wxArrayString& aTextLines, std::vector<VECTOR2I>& aPositions,
252 std::vector<VECTOR2I>& aExtents, const TEXT_ATTRIBUTES& aAttrs,
253 const METRICS& aFontMetrics ) const;
254
255 VECTOR2I drawMarkup( BOX2I* aBoundingBox, std::vector<std::unique_ptr<GLYPH>>* aGlyphs,
256 const wxString& aText, const VECTOR2I& aPosition,
257 const VECTOR2I& aSize, const EDA_ANGLE& aAngle, bool aMirror,
258 const VECTOR2I& aOrigin, TEXT_STYLE_FLAGS aTextStyle,
259 const METRICS& aFontMetrics, std::optional<VECTOR2I> aMousePos = std::nullopt,
260 wxString* aActiveUrl = nullptr ) const;
261
262 void wordbreakMarkup( std::vector<std::pair<wxString, int>>* aWords, const wxString& aText,
263 const VECTOR2I& aSize, TEXT_STYLE_FLAGS aTextStyle ) const;
264
265private:
266 static FONT* getDefaultFont();
267
268protected:
269 wxString m_fontName;
270 wxString m_fontFileName;
271
272private:
274
275 static std::map< std::tuple<wxString, bool, bool, bool>, FONT* > s_fontMap;
276};
277
278} //namespace KIFONT
279
280
281inline std::ostream& operator<<(std::ostream& os, const KIFONT::FONT& aFont)
282{
283 os << "[Font \"" << aFont.GetName() << "\"" << ( aFont.IsStroke() ? " stroke" : "" )
284 << ( aFont.IsOutline() ? " outline" : "" ) << ( aFont.IsBold() ? " bold" : "" )
285 << ( aFont.IsItalic() ? " italic" : "" ) << "]";
286 return os;
287}
288
289
290inline std::ostream& operator<<(std::ostream& os, const KIFONT::FONT* aFont)
291{
292 os << *aFont;
293 return os;
294}
295
296#endif // FONT_H_
BOX2< VECTOR2I > BOX2I
Definition box2.h:918
FONT is an abstract base class for both outline and stroke fonts.
Definition font.h:94
wxString m_fontName
Font name.
Definition font.h:269
virtual ~FONT()
Definition font.h:98
wxString m_fontFileName
Font file name.
Definition font.h:270
virtual bool IsStroke() const
Definition font.h:101
static FONT * s_defaultFont
Definition font.h:273
const char * NameAsToken() const
Definition font.h:113
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
virtual bool IsItalic() const
Definition font.h:104
virtual bool IsBold() const
Definition font.h:103
const wxString & GetName() const
Definition font.h:112
virtual bool IsOutline() const
Definition font.h:102
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:132
static std::map< std::tuple< wxString, bool, bool, bool >, FONT * > s_fontMap
Definition font.h:275
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:203
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:515
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:276
TEXT_STYLE
Definition font.h:42
@ BOLD
Definition font.h:43
@ SUBSCRIPT
Definition font.h:45
@ OVERBAR
Definition font.h:47
@ UNDERLINE
Definition font.h:48
@ ITALIC
Definition font.h:44
@ SUPERSCRIPT
Definition font.h:46
unsigned int TEXT_STYLE_FLAGS
Definition font.h:61
bool IsBold(TEXT_STYLE_FLAGS aFlags)
Definition font.h:64
bool IsSuperscript(TEXT_STYLE_FLAGS aFlags)
Definition font.h:76
bool IsItalic(TEXT_STYLE_FLAGS aFlags)
Definition font.h:70
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:58
bool IsSubscript(TEXT_STYLE_FLAGS aFlags)
Definition font.h:82
std::ostream & operator<<(std::ostream &os, const KIFONT::FONT &aFont)
Definition font.h:281
#define GAL_API
Definition gal.h:27
The Cairo implementation of the graphics abstraction layer.
Definition eda_group.h:29
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:683