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 (C) 2021-2023 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, 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 OUTLINE_FONT_H_
28#define OUTLINE_FONT_H_
29
30#include <gal/gal.h>
32#ifdef _MSC_VER
33#include <ft2build.h>
34#else
35#include <freetype2/ft2build.h>
36#endif
37#include FT_FREETYPE_H
38#include FT_OUTLINE_H
39
40#include <font/font.h>
41#include <font/glyph.h>
43#include <embedded_files.h>
44
45#include <mutex>
46
47namespace KIFONT
48{
53{
54public:
55
57 {
58 INSTALLABLE,
59 EDITABLE,
60 PRINT_PREVIEW_ONLY,
61 RESTRICTED,
63 };
64
66
67 bool IsOutline() const override { return true; }
68
69 bool IsBold() const override
70 {
71 return m_face && ( m_fakeBold || ( m_face->style_flags & FT_STYLE_FLAG_BOLD ) );
72 }
73
74 bool IsItalic() const override
75 {
76 return m_face && ( m_fakeItal || ( m_face->style_flags & FT_STYLE_FLAG_ITALIC ) );
77 }
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
97 static OUTLINE_FONT* LoadFont( const wxString& aFontFileName, bool aBold, bool aItalic,
98 const std::vector<wxString>* aEmbeddedFiles,
99 bool aForDrawingSheet );
100
105 double GetInterline( double aGlyphHeight, const METRICS& aFontMetrics ) const override;
106
107 VECTOR2I GetTextAsGlyphs( BOX2I* aBoundingBox, std::vector<std::unique_ptr<GLYPH>>* aGlyphs,
108 const wxString& aText, const VECTOR2I& aSize,
109 const VECTOR2I& aPosition, const EDA_ANGLE& aAngle, bool aMirror,
110 const VECTOR2I& aOrigin, TEXT_STYLE_FLAGS aTextStyle ) const override;
111
112 void GetLinesAsGlyphs( std::vector<std::unique_ptr<GLYPH>>* aGlyphs, const wxString& aText,
113 const VECTOR2I& aPosition, const TEXT_ATTRIBUTES& aAttrs,
114 const METRICS& aFontMetrics ) const;
115
116 const FT_Face& GetFace() const { return m_face; }
117
118#if 0
119 void RenderToOpenGLCanvas( KIGFX::OPENGL_FREETYPE& aTarget, const wxString& aString,
120 const VECTOR2D& aSize, const wxPoint& aPosition,
121 const EDA_ANGLE& aAngle, bool aMirror ) const;
122#endif
123
124protected:
125 FT_Error loadFace( const wxString& aFontFileName, int aFaceIndex );
126
127 BOX2I getBoundingBox( const std::vector<std::unique_ptr<GLYPH>>& aGlyphs ) const;
128
129 VECTOR2I getTextAsGlyphs( BOX2I* aBoundingBox, std::vector<std::unique_ptr<GLYPH>>* aGlyphs,
130 const wxString& aText, const VECTOR2I& aSize,
131 const VECTOR2I& aPosition, const EDA_ANGLE& aAngle, bool aMirror,
132 const VECTOR2I& aOrigin, TEXT_STYLE_FLAGS aTextStyle ) const;
133
134private:
135 VECTOR2I getTextAsGlyphsUnlocked( BOX2I* aBoundingBox,
136 std::vector<std::unique_ptr<GLYPH>>* aGlyphs,
137 const wxString& aText, const VECTOR2I& aSize,
138 const VECTOR2I& aPosition, const EDA_ANGLE& aAngle,
139 bool aMirror, const VECTOR2I& aOrigin,
140 TEXT_STYLE_FLAGS aTextStyle ) const;
141
142private:
143 // FreeType variables
144
148 static std::mutex m_freeTypeMutex;
149 static FT_Library m_freeType;
150 FT_Face m_face;
151
152 const int m_faceSize;
155
157
158 // cache for glyphs converted to straight segments
159 // key is glyph index (FT_GlyphSlot field glyph_index)
160 std::map<unsigned int, std::vector<std::vector<VECTOR2D>>> m_contourCache;
161
162 // The height of the KiCad stroke font is the distance between stroke endpoints for a vertical
163 // line of cap-height. So the cap-height of the font is actually stroke-width taller than its
164 // height.
165 // Outline fonts are normally scaled on full-height (including ascenders and descenders), so we
166 // need to compensate to keep them from being much smaller than their stroked counterparts.
167 static constexpr double m_outlineFontSizeCompensation = 1.4;
168
169 // FT_Set_Char_Size() gets character width and height specified in 1/64ths of a point
170 static constexpr int m_charSizeScaler = 64;
171
172 // The KiCad stroke font uses a subscript/superscript size ratio of 0.7. This ratio is also
173 // commonly used in LaTeX, but fonts with designed-in subscript and superscript glyphs are more
174 // likely to use 0.58.
175 // For auto-generated subscript and superscript glyphs in outline fonts we split the difference
176 // with 0.64.
177 static constexpr double m_subscriptSuperscriptSize = 0.64;
178
179 static constexpr double m_underlineOffsetScaler = -0.16;
180
181 int faceSize( int aSize ) const
182 {
183 return aSize * m_charSizeScaler * m_outlineFontSizeCompensation;
184 };
185 int faceSize() const { return faceSize( m_faceSize ); }
186
187 // also for superscripts
188 int subscriptSize( int aSize ) const
189 {
190 return KiROUND( faceSize( aSize ) * m_subscriptSuperscriptSize );
191 }
192 int subscriptSize() const { return subscriptSize( m_faceSize ); }
193
194 static constexpr double m_subscriptVerticalOffset = -0.25;
195 static constexpr double m_superscriptVerticalOffset = 0.45;
196};
197
198} //namespace KIFONT
199
200#endif // OUTLINE_FONT_H_
constexpr BOX2I KiROUND(const BOX2D &aBoxD)
Definition: box2.h:990
FONT is an abstract base class for both outline and stroke fonts.
Definition: font.h:131
Class OUTLINE_FONT implements outline font drawing.
Definition: outline_font.h:53
static std::mutex m_freeTypeMutex
Mutex for freetype access, FT_Library and FT_Face are not thread safe.
Definition: outline_font.h:148
static FT_Library m_freeType
Definition: outline_font.h:149
const FT_Face & GetFace() const
Definition: outline_font.h:116
int subscriptSize(int aSize) const
Definition: outline_font.h:188
bool IsItalic() const override
Definition: outline_font.h:74
int faceSize(int aSize) const
Definition: outline_font.h:181
bool IsOutline() const override
Definition: outline_font.h:67
std::map< unsigned int, std::vector< std::vector< VECTOR2D > > > m_contourCache
Definition: outline_font.h:160
int faceSize() const
Definition: outline_font.h:185
const wxString & GetFileName() const
Definition: outline_font.h:89
int subscriptSize() const
Definition: outline_font.h:192
bool IsBold() const override
Definition: outline_font.h:69
unsigned int TEXT_STYLE_FLAGS
Definition: font.h:64
#define GAL_API
Definition: gal.h:28