KiCad PCB EDA Suite
Loading...
Searching...
No Matches
outline_decomposer.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-2024 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_DECOMPOSER_H
28#define OUTLINE_DECOMPOSER_H
29
30#include <vector>
31#ifdef _MSC_VER
32#include <ft2build.h>
33#else
34#include <freetype2/ft2build.h>
35#endif
36#include FT_FREETYPE_H
37#include FT_OUTLINE_H
38#include <math/box2.h>
39#include <math/vector2d.h>
40#include <font/glyph.h>
41
42namespace KIFONT
43{
44
45constexpr int GLYPH_DEFAULT_DPI = 72;
46// The FreeType default of 72 DPI is not enough for outline decomposition;
47// so we'll use something larger than that.
48constexpr int GLYPH_RESOLUTION = 1152;
50
51struct CONTOUR
52{
53 std::vector<VECTOR2D> m_Points;
54 int m_Winding = 0;
55 FT_Orientation m_Orientation;
56};
57
59{
60 std::vector<CONTOUR> m_Contours;
61
62 // Cache of the triangulation data. We'll use this as a hint for triangulating the actual
63 // OUTLINE_GLYPHs.
64 std::vector<std::unique_ptr<SHAPE_POLY_SET::TRIANGULATED_POLYGON>> m_TriangulationData;
65};
66
67
69{
70public:
71 OUTLINE_DECOMPOSER( FT_Outline& aOutline );
72
73 bool OutlineToSegments( std::vector<CONTOUR>* aContours );
74
75private:
76 void newContour();
77
78 void addContourPoint( const VECTOR2D& p );
79
84 int winding( const std::vector<VECTOR2D>& aContour ) const;
85
86 inline static unsigned int onCurve( char aTags )
87 {
88 return aTags & 0x1;
89 }
90
91 inline static unsigned int thirdOrderBezierPoint( char aTags )
92 {
93 return onCurve( aTags ) ? 0 : aTags & 0x2;
94 }
95
96 inline static unsigned int secondOrderBezierPoint( char aTags )
97 {
98 return onCurve( aTags ) ? 0 : !thirdOrderBezierPoint( aTags );
99 }
100
101 inline static unsigned int hasDropout( char aTags )
102 {
103 return aTags & 0x4;
104 }
105
106 inline static unsigned int dropoutMode( char aTags )
107 {
108 return hasDropout( aTags ) ? ( aTags & 0x38 ) : 0;
109 }
110
111 // FT_Outline_Decompose callbacks
112 static int moveTo( const FT_Vector* aEndPoint, void* aCallbackData );
113
114 static int lineTo( const FT_Vector* aEndPoint, void* aCallbackData );
115
116 static int quadraticTo( const FT_Vector* aControlPoint, const FT_Vector* aEndPoint,
117 void* aCallbackData );
118
119 static int cubicTo( const FT_Vector* aFirstControlPoint, const FT_Vector* aSecondControlPoint,
120 const FT_Vector* aEndPoint, void* aCallbackData );
121
122private:
123 FT_Outline& m_outline;
124 std::vector<CONTOUR>* m_contours;
125
127};
128
129} //namespace KIFONT
130
131#endif // OUTLINE_DECOMPOSER_H
static unsigned int hasDropout(char aTags)
static unsigned int onCurve(char aTags)
void addContourPoint(const VECTOR2D &p)
std::vector< CONTOUR > * m_contours
static int cubicTo(const FT_Vector *aFirstControlPoint, const FT_Vector *aSecondControlPoint, const FT_Vector *aEndPoint, void *aCallbackData)
static int moveTo(const FT_Vector *aEndPoint, void *aCallbackData)
bool OutlineToSegments(std::vector< CONTOUR > *aContours)
static unsigned int thirdOrderBezierPoint(char aTags)
static int lineTo(const FT_Vector *aEndPoint, void *aCallbackData)
static unsigned int secondOrderBezierPoint(char aTags)
static int quadraticTo(const FT_Vector *aControlPoint, const FT_Vector *aEndPoint, void *aCallbackData)
static unsigned int dropoutMode(char aTags)
int winding(const std::vector< VECTOR2D > &aContour) const
constexpr int GLYPH_RESOLUTION
constexpr double GLYPH_SIZE_SCALER
constexpr int GLYPH_DEFAULT_DPI
FreeType default.
std::vector< VECTOR2D > m_Points
FT_Orientation m_Orientation
std::vector< CONTOUR > m_Contours
std::vector< std::unique_ptr< SHAPE_POLY_SET::TRIANGULATED_POLYGON > > m_TriangulationData