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