KiCad PCB EDA Suite
Loading...
Searching...
No Matches
outline_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 * Outline font 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 OUTLINE_FONT_H_
24#define OUTLINE_FONT_H_
25
26#include <gal/gal.h>
28#ifdef _MSC_VER
29#include <ft2build.h>
30#else
31#include <freetype2/ft2build.h>
32#endif
33#include FT_FREETYPE_H
34#include FT_OUTLINE_H
35
36#include <font/font.h>
37#include <font/glyph.h>
39#include <embedded_files.h>
40
41#include <mutex>
42
43namespace KIFONT
44{
49{
50public:
51
60
62
63 bool IsOutline() const override { return true; }
64
65 bool IsBold() const override
66 {
67 return m_face && ( m_fakeBold || ( m_face->style_flags & FT_STYLE_FLAG_BOLD ) );
68 }
69
70 bool IsItalic() const override
71 {
72 return m_face && ( m_fakeItal || ( m_face->style_flags & FT_STYLE_FLAG_ITALIC ) );
73 }
74
75 // Accessors to distinguish fake vs real style for diagnostics and rendering decisions
76 bool IsFakeItalic() const { return m_fakeItal; }
77 bool IsFakeBold() const { return m_fakeBold; }
78
80 {
81 m_fakeBold = true;
82 }
83
85 {
86 m_fakeItal = true;
87 }
88
89 const wxString& GetFileName() const { return m_fontFileName; }
90
91 EMBEDDING_PERMISSION GetEmbeddingPermission() const;
92
102 static OUTLINE_FONT* LoadFont( const wxString& aFontFileName, bool aBold, bool aItalic,
103 const std::vector<wxString>* aEmbeddedFiles,
104 bool aForDrawingSheet );
105
111 double GetInterline( double aGlyphHeight, const METRICS& aFontMetrics ) const override;
112
113 double GetSuperSubSizeMultiplier() const override { return m_subscriptSuperscriptSize; }
114
115 double GetSuperSubBaselineOffset( TEXT_STYLE_FLAGS aTextStyle ) const override
116 {
117 double offset = 0.0;
118
119 if( IsSubscript( aTextStyle ) )
121 else if( IsSuperscript( aTextStyle ) )
123
124 // The stored offsets are y-up face units carrying the size compensation.
125 return -offset * m_outlineFontSizeCompensation;
126 }
127
128 VECTOR2I GetTextAsGlyphs( BOX2I* aBoundingBox, std::vector<std::unique_ptr<GLYPH>>* aGlyphs,
129 const wxString& aText, const VECTOR2I& aSize,
130 const VECTOR2I& aPosition, const EDA_ANGLE& aAngle, bool aMirror,
131 const VECTOR2I& aOrigin, TEXT_STYLE_FLAGS aTextStyle ) const override;
132
133 void GetLinesAsGlyphs( std::vector<std::unique_ptr<GLYPH>>* aGlyphs, const wxString& aText,
134 const VECTOR2I& aPosition, const TEXT_ATTRIBUTES& aAttrs,
135 const METRICS& aFontMetrics ) const;
136
137 const FT_Face& GetFace() const { return m_face; }
138
156 bool LoadGlyphContours( unsigned int aGlyphIndex, std::vector<CONTOUR>& aContours ) const;
157
166 static void SelectCharmap( FT_Face aFace );
167
168protected:
169 FT_Error loadFace( const wxString& aFontFileName, int aFaceIndex );
170
171 BOX2I getBoundingBox( const std::vector<std::unique_ptr<GLYPH>>& aGlyphs ) const;
172
173 VECTOR2I getTextAsGlyphs( BOX2I* aBoundingBox, std::vector<std::unique_ptr<GLYPH>>* aGlyphs,
174 const wxString& aText, const VECTOR2I& aSize,
175 const VECTOR2I& aPosition, const EDA_ANGLE& aAngle, bool aMirror,
176 const VECTOR2I& aOrigin, TEXT_STYLE_FLAGS aTextStyle ) const;
177
178private:
179 VECTOR2I getTextAsGlyphsUnlocked( BOX2I* aBoundingBox,
180 std::vector<std::unique_ptr<GLYPH>>* aGlyphs,
181 const wxString& aText, const VECTOR2I& aSize,
182 const VECTOR2I& aPosition, const EDA_ANGLE& aAngle,
183 bool aMirror, const VECTOR2I& aOrigin,
184 TEXT_STYLE_FLAGS aTextStyle ) const;
185
186private:
187 // FreeType variables
188
192 static std::mutex m_freeTypeMutex;
193 static FT_Library m_freeType;
194 FT_Face m_face;
195
196 const int m_faceSize;
199
201
202 // cache for glyphs converted to straight segments
203 // key is glyph index (FT_GlyphSlot field glyph_index)
204 std::map<unsigned int, std::vector<std::vector<VECTOR2D>>> m_contourCache;
205
206 // The height of the KiCad stroke font is the distance between stroke endpoints for a vertical
207 // line of cap-height. So the cap-height of the font is actually stroke-width taller than its
208 // height.
209 // Outline fonts are normally scaled on full-height (including ascenders and descenders), so we
210 // need to compensate to keep them from being much smaller than their stroked counterparts.
211 static constexpr double m_outlineFontSizeCompensation = 1.4;
212
213 // FT_Set_Char_Size() gets character width and height specified in 1/64ths of a point
214 static constexpr int m_charSizeScaler = 64;
215
216 // The KiCad stroke font uses a subscript/superscript size ratio of 0.7. This ratio is also
217 // commonly used in LaTeX, but fonts with designed-in subscript and superscript glyphs are more
218 // likely to use 0.58.
219 // For auto-generated subscript and superscript glyphs in outline fonts we split the difference
220 // with 0.64.
221 static constexpr double m_subscriptSuperscriptSize = 0.64;
222
223 static constexpr double m_underlineOffsetScaler = -0.16;
224
225 int faceSize( int aSize ) const
226 {
228 };
229
230 int faceSize() const { return faceSize( m_faceSize ); }
231
232 // also for superscripts
233 int subscriptSize( int aSize ) const
234 {
235 return KiROUND( faceSize( aSize ) * m_subscriptSuperscriptSize );
236 }
237 int subscriptSize() const { return subscriptSize( m_faceSize ); }
238
239 static constexpr double m_subscriptVerticalOffset = -0.25;
240 static constexpr double m_superscriptVerticalOffset = 0.45;
241};
242
243} //namespace KIFONT
244
245#endif // OUTLINE_FONT_H_
BOX2I getBoundingBox(BOARD_ITEM *aItem)
BOX2< VECTOR2I > BOX2I
Definition box2.h:914
constexpr BOX2I KiROUND(const BOX2D &aBoxD)
Definition box2.h:982
wxString m_fontFileName
Font file name.
Definition font.h:286
Implement outline font drawing.
static std::mutex m_freeTypeMutex
Mutex for freetype access, FT_Library and FT_Face are not thread safe.
double GetSuperSubBaselineOffset(TEXT_STYLE_FLAGS aTextStyle) const override
Return the baseline shift of a superscript or subscript run, as a downwards fraction of the reduced g...
static FT_Library m_freeType
static constexpr double m_superscriptVerticalOffset
bool IsFakeItalic() const
bool IsFakeBold() const
const FT_Face & GetFace() const
int subscriptSize(int aSize) const
bool IsItalic() const override
static constexpr int m_charSizeScaler
int faceSize(int aSize) const
bool IsOutline() const override
std::map< unsigned int, std::vector< std::vector< VECTOR2D > > > m_contourCache
static constexpr double m_subscriptSuperscriptSize
const wxString & GetFileName() const
static constexpr double m_outlineFontSizeCompensation
static constexpr double m_underlineOffsetScaler
double GetSuperSubSizeMultiplier() const override
Return the factor the text size is multiplied by for a superscript or subscript run.
int subscriptSize() const
bool IsBold() const override
static constexpr double m_subscriptVerticalOffset
unsigned int TEXT_STYLE_FLAGS
Definition font.h:61
bool IsSuperscript(TEXT_STYLE_FLAGS aFlags)
Definition font.h:76
bool IsSubscript(TEXT_STYLE_FLAGS aFlags)
Definition font.h:82
#define GAL_API
Definition gal.h:27
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:708