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
160 void LinebreakText( wxString& aText, int aColumnWidth, const VECTOR2I& aGlyphSize,
161 int aThickness, bool aBold, bool aItalic ) const;
162
167 virtual double GetInterline( double aGlyphHeight, const METRICS& aFontMetrics ) const = 0;
168
183 virtual VECTOR2I GetTextAsGlyphs( BOX2I* aBBox, std::vector<std::unique_ptr<GLYPH>>* aGlyphs,
184 const wxString& aText, const VECTOR2I& aSize,
185 const VECTOR2I& aPosition, const EDA_ANGLE& aAngle,
186 bool aMirror, const VECTOR2I& aOrigin,
187 TEXT_STYLE_FLAGS aTextStyle ) const = 0;
188
189protected:
196 inline unsigned linesCount( const wxString& aText ) const
197 {
198 if( aText.empty() )
199 return 0; // std::count does not work well with empty strings
200 else
201 // aText.end() - 1 is to skip a newline character that is potentially at the end
202 return std::count( aText.begin(), aText.end() - 1, '\n' ) + 1;
203 }
204
222 void drawSingleLineText( KIGFX::GAL* aGal, BOX2I* aBoundingBox, const wxString& aText,
223 const VECTOR2I& aPosition, const VECTOR2I& aSize,
224 const EDA_ANGLE& aAngle, bool aMirror, const VECTOR2I& aOrigin,
225 bool aItalic, bool aUnderline, bool aHover, const METRICS& aFontMetrics,
226 std::optional<VECTOR2I> aMousePos, wxString* aActiveUrl ) const;
227
239 VECTOR2I boundingBoxSingleLine( BOX2I* aBBox, const wxString& aText, const VECTOR2I& aPosition,
240 const VECTOR2I& aSize, bool aItalic,
241 const METRICS& aFontMetrics ) const;
242
243 void getLinePositions( const wxString& aText, const VECTOR2I& aPosition,
244 wxArrayString& aTextLines, std::vector<VECTOR2I>& aPositions,
245 std::vector<VECTOR2I>& aExtents, const TEXT_ATTRIBUTES& aAttrs,
246 const METRICS& aFontMetrics ) const;
247
248 VECTOR2I drawMarkup( BOX2I* aBoundingBox, std::vector<std::unique_ptr<GLYPH>>* aGlyphs,
249 const wxString& aText, const VECTOR2I& aPosition,
250 const VECTOR2I& aSize, const EDA_ANGLE& aAngle, bool aMirror,
251 const VECTOR2I& aOrigin, TEXT_STYLE_FLAGS aTextStyle,
252 const METRICS& aFontMetrics, std::optional<VECTOR2I> aMousePos = std::nullopt,
253 wxString* aActiveUrl = nullptr ) const;
254
255 void wordbreakMarkup( std::vector<std::pair<wxString, int>>* aWords, const wxString& aText,
256 const VECTOR2I& aSize, TEXT_STYLE_FLAGS aTextStyle ) const;
257
258private:
259 static FONT* getDefaultFont();
260
261protected:
262 wxString m_fontName;
263 wxString m_fontFileName;
264
265private:
267
268 static std::map< std::tuple<wxString, bool, bool, bool>, FONT* > s_fontMap;
269};
270
271} //namespace KIFONT
272
273
274inline std::ostream& operator<<(std::ostream& os, const KIFONT::FONT& aFont)
275{
276 os << "[Font \"" << aFont.GetName() << "\"" << ( aFont.IsStroke() ? " stroke" : "" )
277 << ( aFont.IsOutline() ? " outline" : "" ) << ( aFont.IsBold() ? " bold" : "" )
278 << ( aFont.IsItalic() ? " italic" : "" ) << "]";
279 return os;
280}
281
282
283inline std::ostream& operator<<(std::ostream& os, const KIFONT::FONT* aFont)
284{
285 os << *aFont;
286 return os;
287}
288
289#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:262
virtual ~FONT()
Definition font.h:98
wxString m_fontFileName
Font file name.
Definition font.h:263
virtual bool IsStroke() const
Definition font.h:101
static FONT * s_defaultFont
Definition font.h:266
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:268
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:196
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:499
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:274
#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