KiCad PCB EDA Suite
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-2022 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
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
44namespace KIFONT
45{
49class OUTLINE_FONT : public FONT
50{
51public:
53
54 static wxString FontConfigVersion();
55
56 static wxString FreeTypeVersion();
57
58 static wxString HarfBuzzVersion();
59
60 static wxString FontLibraryVersion();
61
62 bool IsOutline() const override { return true; }
63
64 bool IsBold() const override
65 {
66 return m_face && ( m_fakeBold || ( m_face->style_flags & FT_STYLE_FLAG_BOLD ) );
67 }
68
69 bool IsItalic() const override
70 {
71 return m_face && ( m_fakeItal || ( m_face->style_flags & FT_STYLE_FLAG_ITALIC ) );
72 }
73
75 {
76 m_fakeBold = true;
77 }
78
80 {
81 m_fakeItal = true;
82 }
83
88 static OUTLINE_FONT* LoadFont( const wxString& aFontFileName, bool aBold, bool aItalic );
89
94 double ComputeOverbarVerticalPosition( double aGlyphHeight ) const override;
95
100 double ComputeUnderlineVerticalPosition( double aGlyphHeight ) const override;
101
106 double GetInterline( double aGlyphHeight = 0.0, double aLineSpacing = 1.0 ) const override;
107
108 VECTOR2I GetTextAsGlyphs( BOX2I* aBoundingBox, std::vector<std::unique_ptr<GLYPH>>* aGlyphs,
109 const wxString& aText, const VECTOR2I& aSize,
110 const VECTOR2I& aPosition, const EDA_ANGLE& aAngle, bool aMirror,
111 const VECTOR2I& aOrigin, TEXT_STYLE_FLAGS aTextStyle ) const override;
112
113 void GetLinesAsGlyphs( std::vector<std::unique_ptr<GLYPH>>* aGlyphs, const wxString& aText,
114 const VECTOR2I& aPosition, const TEXT_ATTRIBUTES& aAttrs ) 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 );
126
127 double getOverbarOffset( int ascender, int height, int thickness ) const;
128
129 BOX2I getBoundingBox( const std::vector<std::unique_ptr<GLYPH>>& aGlyphs ) const;
130
131 VECTOR2I getTextAsGlyphs( BOX2I* aBoundingBox, std::vector<std::unique_ptr<GLYPH>>* aGlyphs,
132 const wxString& aText, const VECTOR2I& aSize,
133 const VECTOR2I& aPosition, const EDA_ANGLE& aAngle, bool aMirror,
134 const VECTOR2I& aOrigin, TEXT_STYLE_FLAGS aTextStyle ) const;
135
136private:
137 // FreeType variables
138 static FT_Library m_freeType;
139 FT_Face m_face;
140 const int m_faceSize;
143
144 // cache for glyphs converted to straight segments
145 // key is glyph index (FT_GlyphSlot field glyph_index)
146 std::map<unsigned int, GLYPH_POINTS_LIST> m_contourCache;
147
148 // The height of the KiCad stroke font is the distance between stroke endpoints for a vertical
149 // line of cap-height. So the cap-height of the font is actually stroke-width taller than its
150 // height.
151 // Outline fonts are normally scaled on full-height (including ascenders and descenders), so we
152 // need to compensate to keep them from being much smaller than their stroked counterparts.
153 static constexpr double m_outlineFontSizeCompensation = 1.4;
154
155 // FT_Set_Char_Size() gets character width and height specified in 1/64ths of a point
156 static constexpr int m_charSizeScaler = 64;
157
158 // The KiCad stroke font uses a subscript/superscript size ratio of 0.7. This ratio is also
159 // commonly used in LaTeX, but fonts with designed-in subscript and superscript glyphs are more
160 // likely to use 0.58.
161 // For auto-generated subscript and superscript glyphs in outline fonts we split the difference
162 // with 0.64.
163 static constexpr double m_subscriptSuperscriptSize = 0.64;
164
165 static constexpr double m_underlineOffsetScaler = -0.16;
166
167 int faceSize( int aSize ) const
168 {
170 };
171 int faceSize() const { return faceSize( m_faceSize ); }
172
173 // also for superscripts
174 int subscriptSize( int aSize ) const
175 {
176 return KiROUND( faceSize( aSize ) * m_subscriptSuperscriptSize );
177 }
178 int subscriptSize() const { return subscriptSize( m_faceSize ); }
179
180 static constexpr double m_subscriptVerticalOffset = -0.25;
181 static constexpr double m_superscriptVerticalOffset = 0.45;
182};
183
184} //namespace KIFONT
185
186#endif // OUTLINE_FONT_H_
FONT is an abstract base class for both outline and stroke fonts.
Definition: font.h:105
Class OUTLINE_FONT implements outline font drawing.
Definition: outline_font.h:50
FT_Error loadFace(const wxString &aFontFileName)
static FT_Library m_freeType
Definition: outline_font.h:138
BOX2I getBoundingBox(const std::vector< std::unique_ptr< GLYPH > > &aGlyphs) const
static constexpr double m_superscriptVerticalOffset
Definition: outline_font.h:181
static wxString FreeTypeVersion()
VECTOR2I getTextAsGlyphs(BOX2I *aBoundingBox, 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
std::map< unsigned int, GLYPH_POINTS_LIST > m_contourCache
Definition: outline_font.h:146
VECTOR2I GetTextAsGlyphs(BOX2I *aBoundingBox, 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 override
Convert text string to an array of GLYPHs.
const FT_Face & GetFace() const
Definition: outline_font.h:116
double GetInterline(double aGlyphHeight=0.0, double aLineSpacing=1.0) const override
Compute the distance (interline) between 2 lines of text (for multiline texts).
int subscriptSize(int aSize) const
Definition: outline_font.h:174
static wxString FontConfigVersion()
bool IsItalic() const override
Definition: outline_font.h:69
static constexpr int m_charSizeScaler
Definition: outline_font.h:156
int faceSize(int aSize) const
Definition: outline_font.h:167
void GetLinesAsGlyphs(std::vector< std::unique_ptr< GLYPH > > *aGlyphs, const wxString &aText, const VECTOR2I &aPosition, const TEXT_ATTRIBUTES &aAttrs) const
bool IsOutline() const override
Definition: outline_font.h:62
double ComputeUnderlineVerticalPosition(double aGlyphHeight) const override
Compute the vertical position of an underline.
double getOverbarOffset(int ascender, int height, int thickness) const
int faceSize() const
Definition: outline_font.h:171
static constexpr double m_subscriptSuperscriptSize
Definition: outline_font.h:163
static constexpr double m_outlineFontSizeCompensation
Definition: outline_font.h:153
static constexpr double m_underlineOffsetScaler
Definition: outline_font.h:165
double ComputeOverbarVerticalPosition(double aGlyphHeight) const override
Compute the vertical position of an overbar.
int subscriptSize() const
Definition: outline_font.h:178
static wxString FontLibraryVersion()
bool IsBold() const override
Definition: outline_font.h:64
static wxString HarfBuzzVersion()
static OUTLINE_FONT * LoadFont(const wxString &aFontFileName, bool aBold, bool aItalic)
Load an outline font.
static constexpr double m_subscriptVerticalOffset
Definition: outline_font.h:180
unsigned int TEXT_STYLE_FLAGS
Definition: font.h:63
Definition: font.h:100
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:85