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//#include <gal/opengl/opengl_freetype.h>
40#include <font/font.h>
41#include <font/glyph.h>
43
44#include <mutex>
45
46namespace KIFONT
47{
52{
53public:
55
56 bool IsOutline() const override { return true; }
57
58 bool IsBold() const override
59 {
60 return m_face && ( m_fakeBold || ( m_face->style_flags & FT_STYLE_FLAG_BOLD ) );
61 }
62
63 bool IsItalic() const override
64 {
65 return m_face && ( m_fakeItal || ( m_face->style_flags & FT_STYLE_FLAG_ITALIC ) );
66 }
67
69 {
70 m_fakeBold = true;
71 }
72
74 {
75 m_fakeItal = true;
76 }
77
82 static OUTLINE_FONT* LoadFont( const wxString& aFontFileName, bool aBold, bool aItalic );
83
88 double GetInterline( double aGlyphHeight, const METRICS& aFontMetrics ) const override;
89
90 VECTOR2I GetTextAsGlyphs( BOX2I* aBoundingBox, std::vector<std::unique_ptr<GLYPH>>* aGlyphs,
91 const wxString& aText, const VECTOR2I& aSize,
92 const VECTOR2I& aPosition, const EDA_ANGLE& aAngle, bool aMirror,
93 const VECTOR2I& aOrigin, TEXT_STYLE_FLAGS aTextStyle ) const override;
94
95 void GetLinesAsGlyphs( std::vector<std::unique_ptr<GLYPH>>* aGlyphs, const wxString& aText,
96 const VECTOR2I& aPosition, const TEXT_ATTRIBUTES& aAttrs,
97 const METRICS& aFontMetrics ) const;
98
99 const FT_Face& GetFace() const { return m_face; }
100
101#if 0
102 void RenderToOpenGLCanvas( KIGFX::OPENGL_FREETYPE& aTarget, const wxString& aString,
103 const VECTOR2D& aSize, const wxPoint& aPosition,
104 const EDA_ANGLE& aAngle, bool aMirror ) const;
105#endif
106
107protected:
108 FT_Error loadFace( const wxString& aFontFileName, int aFaceIndex );
109
110 BOX2I getBoundingBox( const std::vector<std::unique_ptr<GLYPH>>& aGlyphs ) const;
111
112 VECTOR2I getTextAsGlyphs( BOX2I* aBoundingBox, std::vector<std::unique_ptr<GLYPH>>* aGlyphs,
113 const wxString& aText, const VECTOR2I& aSize,
114 const VECTOR2I& aPosition, const EDA_ANGLE& aAngle, bool aMirror,
115 const VECTOR2I& aOrigin, TEXT_STYLE_FLAGS aTextStyle ) const;
116
117private:
118 VECTOR2I getTextAsGlyphsUnlocked( BOX2I* aBoundingBox,
119 std::vector<std::unique_ptr<GLYPH>>* aGlyphs,
120 const wxString& aText, const VECTOR2I& aSize,
121 const VECTOR2I& aPosition, const EDA_ANGLE& aAngle,
122 bool aMirror, const VECTOR2I& aOrigin,
123 TEXT_STYLE_FLAGS aTextStyle ) const;
124
125private:
126 // FreeType variables
127
131 static std::mutex m_freeTypeMutex;
132 static FT_Library m_freeType;
133 FT_Face m_face;
134
135 const int m_faceSize;
138
139 // cache for glyphs converted to straight segments
140 // key is glyph index (FT_GlyphSlot field glyph_index)
141 std::map<unsigned int, std::vector<std::vector<VECTOR2D>>> m_contourCache;
142
143 // The height of the KiCad stroke font is the distance between stroke endpoints for a vertical
144 // line of cap-height. So the cap-height of the font is actually stroke-width taller than its
145 // height.
146 // Outline fonts are normally scaled on full-height (including ascenders and descenders), so we
147 // need to compensate to keep them from being much smaller than their stroked counterparts.
148 static constexpr double m_outlineFontSizeCompensation = 1.4;
149
150 // FT_Set_Char_Size() gets character width and height specified in 1/64ths of a point
151 static constexpr int m_charSizeScaler = 64;
152
153 // The KiCad stroke font uses a subscript/superscript size ratio of 0.7. This ratio is also
154 // commonly used in LaTeX, but fonts with designed-in subscript and superscript glyphs are more
155 // likely to use 0.58.
156 // For auto-generated subscript and superscript glyphs in outline fonts we split the difference
157 // with 0.64.
158 static constexpr double m_subscriptSuperscriptSize = 0.64;
159
160 static constexpr double m_underlineOffsetScaler = -0.16;
161
162 int faceSize( int aSize ) const
163 {
164 return aSize * m_charSizeScaler * m_outlineFontSizeCompensation;
165 };
166 int faceSize() const { return faceSize( m_faceSize ); }
167
168 // also for superscripts
169 int subscriptSize( int aSize ) const
170 {
171 return KiROUND( faceSize( aSize ) * m_subscriptSuperscriptSize );
172 }
173 int subscriptSize() const { return subscriptSize( m_faceSize ); }
174
175 static constexpr double m_subscriptVerticalOffset = -0.25;
176 static constexpr double m_superscriptVerticalOffset = 0.45;
177};
178
179} //namespace KIFONT
180
181#endif // OUTLINE_FONT_H_
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:52
static std::mutex m_freeTypeMutex
Mutex for freetype access, FT_Library and FT_Face are not thread safe.
Definition: outline_font.h:131
static FT_Library m_freeType
Definition: outline_font.h:132
const FT_Face & GetFace() const
Definition: outline_font.h:99
int subscriptSize(int aSize) const
Definition: outline_font.h:169
bool IsItalic() const override
Definition: outline_font.h:63
int faceSize(int aSize) const
Definition: outline_font.h:162
bool IsOutline() const override
Definition: outline_font.h:56
std::map< unsigned int, std::vector< std::vector< VECTOR2D > > > m_contourCache
Definition: outline_font.h:141
int faceSize() const
Definition: outline_font.h:166
int subscriptSize() const
Definition: outline_font.h:173
bool IsBold() const override
Definition: outline_font.h:58
unsigned int TEXT_STYLE_FLAGS
Definition: font.h:64
#define GAL_API
Definition: gal.h:28
constexpr ret_type KiROUND(fp_type v)
Round a floating point number to an integer using "round halfway cases away from zero".
Definition: util.h:118