KiCad PCB EDA Suite
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-2022 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 <iostream>
31#include <map>
32#include <algorithm>
33#include <wx/string.h>
34#include <font/glyph.h>
36
37namespace KIGFX
38{
39class GAL;
40}
41
42
44{
45 BOLD = 1,
46 ITALIC = 1 << 1,
47 SUBSCRIPT = 1 << 2,
48 SUPERSCRIPT = 1 << 3,
49 OVERBAR = 1 << 4,
50 UNDERLINE = 1 << 5
51};
52
53
60static constexpr double ITALIC_TILT = 1.0 / 8;
61
62
63using TEXT_STYLE_FLAGS = unsigned int;
64
65
66inline bool IsBold( TEXT_STYLE_FLAGS aFlags )
67{
68 return aFlags & TEXT_STYLE::BOLD;
69}
70
71
72inline bool IsItalic( TEXT_STYLE_FLAGS aFlags )
73{
74 return aFlags & TEXT_STYLE::ITALIC;
75}
76
77
78inline bool IsSuperscript( TEXT_STYLE_FLAGS aFlags )
79{
80 return aFlags & TEXT_STYLE::SUPERSCRIPT;
81}
82
83
84inline bool IsSubscript( TEXT_STYLE_FLAGS aFlags )
85{
86 return aFlags & TEXT_STYLE::SUBSCRIPT;
87}
88
89
90inline bool IsOverbar( TEXT_STYLE_FLAGS aFlags )
91{
92 return aFlags & TEXT_STYLE::OVERBAR;
93}
94
95
96std::string TextStyleAsString( TEXT_STYLE_FLAGS aFlags );
97
98
99namespace KIFONT
100{
104class FONT
105{
106public:
107 explicit FONT();
108
109 virtual ~FONT()
110 { }
111
112 virtual bool IsStroke() const { return false; }
113 virtual bool IsOutline() const { return false; }
114 virtual bool IsBold() const { return false; }
115 virtual bool IsItalic() const { return false; }
116
117 static FONT* GetFont( const wxString& aFontName = wxEmptyString, bool aBold = false,
118 bool aItalic = false );
119 static bool IsStroke( const wxString& aFontName );
120
121 const wxString& GetName() const { return m_fontName; };
122 inline const char* NameAsToken() const { return GetName().utf8_str().data(); }
123
134 void Draw( KIGFX::GAL* aGal, const wxString& aText, const VECTOR2I& aPosition,
135 const VECTOR2I& aCursor, const TEXT_ATTRIBUTES& aAttrs ) const;
136
137 void Draw( KIGFX::GAL* aGal, const wxString& aText, const VECTOR2I& aPosition,
138 const TEXT_ATTRIBUTES& aAttributes ) const
139 {
140 Draw( aGal, aText, aPosition, VECTOR2I( 0, 0 ), aAttributes );
141 }
142
148 VECTOR2I StringBoundaryLimits( const wxString& aText, const VECTOR2I& aSize, int aThickness,
149 bool aBold, bool aItalic ) const;
150
154 void LinebreakText( wxString& aText, int aColumnWidth, const VECTOR2I& aGlyphSize,
155 int aThickness, bool aBold, bool aItalic ) const;
156
161 virtual double ComputeOverbarVerticalPosition( double aGlyphHeight ) const = 0;
162
167 virtual double ComputeUnderlineVerticalPosition( double aGlyphHeight ) const = 0;
168
173 virtual double GetInterline( double aGlyphHeight, double aLineSpacing = 1.0 ) const = 0;
174
189 virtual VECTOR2I GetTextAsGlyphs( BOX2I* aBBox, std::vector<std::unique_ptr<GLYPH>>* aGlyphs,
190 const wxString& aText, const VECTOR2I& aSize,
191 const VECTOR2I& aPosition, const EDA_ANGLE& aAngle,
192 bool aMirror, const VECTOR2I& aOrigin,
193 TEXT_STYLE_FLAGS aTextStyle ) const = 0;
194
195protected:
202 inline unsigned linesCount( const wxString& aText ) const
203 {
204 if( aText.empty() )
205 return 0; // std::count does not work well with empty strings
206 else
207 // aText.end() - 1 is to skip a newline character that is potentially at the end
208 return std::count( aText.begin(), aText.end() - 1, '\n' ) + 1;
209 }
210
225 void drawSingleLineText( KIGFX::GAL* aGal, BOX2I* aBoundingBox, const wxString& aText,
226 const VECTOR2I& aPosition, const VECTOR2I& aSize,
227 const EDA_ANGLE& aAngle, bool aMirror, const VECTOR2I& aOrigin,
228 bool aItalic, bool aUnderline ) const;
229
240 VECTOR2I boundingBoxSingleLine( BOX2I* aBBox, const wxString& aText, const VECTOR2I& aPosition,
241 const VECTOR2I& aSize, bool aItalic ) 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 ) const;
246
247 VECTOR2I drawMarkup( BOX2I* aBoundingBox, std::vector<std::unique_ptr<GLYPH>>* aGlyphs,
248 const wxString& aText, const VECTOR2I& aPosition, const VECTOR2I& aSize,
249 const EDA_ANGLE& aAngle, bool aMirror, const VECTOR2I& aOrigin,
250 TEXT_STYLE_FLAGS aTextStyle ) const;
251
252 void wordbreakMarkup( std::vector<std::pair<wxString, int>>* aWords, const wxString& aText,
253 const VECTOR2I& aSize, TEXT_STYLE_FLAGS aTextStyle ) const;
254
256 static constexpr double INTERLINE_PITCH_RATIO = 1.62; // The golden mean
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>, 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_
FONT is an abstract base class for both outline and stroke fonts.
Definition: font.h:105
virtual double ComputeUnderlineVerticalPosition(double aGlyphHeight) const =0
Compute the vertical position of an underline.
wxString m_fontName
Font name.
Definition: font.h:262
virtual double GetInterline(double aGlyphHeight, double aLineSpacing=1.0) const =0
Compute the distance (interline) between 2 lines of text (for multiline texts).
static FONT * GetFont(const wxString &aFontName=wxEmptyString, bool aBold=false, bool aItalic=false)
Definition: font.cpp:138
void Draw(KIGFX::GAL *aGal, const wxString &aText, const VECTOR2I &aPosition, const VECTOR2I &aCursor, const TEXT_ATTRIBUTES &aAttrs) const
Draw a string.
Definition: font.cpp:232
virtual ~FONT()
Definition: font.h:109
virtual double ComputeOverbarVerticalPosition(double aGlyphHeight) const =0
Compute the vertical position of an overbar.
wxString m_fontFileName
Font file name.
Definition: font.h:263
void drawSingleLineText(KIGFX::GAL *aGal, BOX2I *aBoundingBox, const wxString &aText, const VECTOR2I &aPosition, const VECTOR2I &aSize, const EDA_ANGLE &aAngle, bool aMirror, const VECTOR2I &aOrigin, bool aItalic, bool aUnderline) const
Draws a single line of text.
Definition: font.cpp:332
virtual bool IsStroke() const
Definition: font.h:112
static FONT * s_defaultFont
Definition: font.h:266
void getLinePositions(const wxString &aText, const VECTOR2I &aPosition, wxArrayString &aTextLines, std::vector< VECTOR2I > &aPositions, std::vector< VECTOR2I > &aExtents, const TEXT_ATTRIBUTES &aAttrs) const
Definition: font.cpp:167
VECTOR2I boundingBoxSingleLine(BOX2I *aBBox, const wxString &aText, const VECTOR2I &aPosition, const VECTOR2I &aSize, bool aItalic) const
Computes the bounding box for a single line of text.
Definition: font.cpp:387
const char * NameAsToken() const
Definition: font.h:122
void wordbreakMarkup(std::vector< std::pair< wxString, int > > *aWords, const wxString &aText, const VECTOR2I &aSize, TEXT_STYLE_FLAGS aTextStyle) const
Factor that determines the pitch between 2 lines.
Definition: font.cpp:488
virtual bool IsItalic() const
Definition: font.h:115
virtual bool IsBold() const
Definition: font.h:114
const wxString & GetName() const
Definition: font.h:121
virtual bool IsOutline() const
Definition: font.h:113
VECTOR2I drawMarkup(BOX2I *aBoundingBox, std::vector< std::unique_ptr< GLYPH > > *aGlyphs, const wxString &aText, const VECTOR2I &aPosition, const VECTOR2I &aSize, const EDA_ANGLE &aAngle, bool aMirror, const VECTOR2I &aOrigin, TEXT_STYLE_FLAGS aTextStyle) const
Definition: font.cpp:306
static std::map< std::tuple< wxString, bool, bool >, FONT * > s_fontMap
Definition: font.h:268
VECTOR2I StringBoundaryLimits(const wxString &aText, const VECTOR2I &aSize, int aThickness, bool aBold, bool aItalic) const
Compute the boundary limits of aText (the bounding box of all shapes).
Definition: font.cpp:357
unsigned linesCount(const wxString &aText) const
Returns number of lines for a given text.
Definition: font.h:202
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.
static constexpr double INTERLINE_PITCH_RATIO
Definition: font.h:256
void Draw(KIGFX::GAL *aGal, const wxString &aText, const VECTOR2I &aPosition, const TEXT_ATTRIBUTES &aAttributes) const
Definition: font.h:137
static FONT * getDefaultFont()
Definition: font.cpp:129
void LinebreakText(wxString &aText, int aColumnWidth, const VECTOR2I &aGlyphSize, int aThickness, bool aBold, bool aItalic) const
Insert characters into text to ensure that no lines are wider than aColumnWidth.
Definition: font.cpp:509
Abstract interface for drawing on a 2D-surface.
TEXT_STYLE
Definition: font.h:44
@ BOLD
Definition: font.h:45
@ SUBSCRIPT
Definition: font.h:47
@ OVERBAR
Definition: font.h:49
@ UNDERLINE
Definition: font.h:50
@ ITALIC
Definition: font.h:46
@ SUPERSCRIPT
Definition: font.h:48
unsigned int TEXT_STYLE_FLAGS
Definition: font.h:63
bool IsOverbar(TEXT_STYLE_FLAGS aFlags)
Definition: font.h:90
bool IsBold(TEXT_STYLE_FLAGS aFlags)
Definition: font.h:66
bool IsSuperscript(TEXT_STYLE_FLAGS aFlags)
Definition: font.h:78
std::string TextStyleAsString(TEXT_STYLE_FLAGS aFlags)
bool IsItalic(TEXT_STYLE_FLAGS aFlags)
Definition: font.h:72
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:60
bool IsSubscript(TEXT_STYLE_FLAGS aFlags)
Definition: font.h:84
std::ostream & operator<<(std::ostream &os, const KIFONT::FONT &aFont)
Definition: font.h:274
Definition: font.h:100
The Cairo implementation of the graphics abstraction layer.
Definition: color4d.cpp:246
VECTOR2< int > VECTOR2I
Definition: vector2d.h:590