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>
38
39namespace KIFONT
40{
41
42constexpr int GLYPH_DEFAULT_DPI = 72;
43
44// The FreeType default of 72 DPI is not enough for outline decomposition;
45// so we'll use something larger than that.
46constexpr int GLYPH_RESOLUTION = 1152;
48
49struct CONTOUR
50{
51 std::vector<VECTOR2D> m_Points;
52 int m_Winding = 0;
53 FT_Orientation m_Orientation;
54};
55
57{
58 std::vector<CONTOUR> m_Contours;
59
60 // Cache of the triangulation data. We'll use this as a hint for triangulating the actual
61 // OUTLINE_GLYPHs.
62 std::vector<std::unique_ptr<SHAPE_POLY_SET::TRIANGULATED_POLYGON>> m_TriangulationData;
63
64 // Glyphs with no outline (e.g. a space) legitimately decompose to zero contours, so cache
65 // occupancy can't key off m_Contours alone.
66 bool m_Loaded = false;
67};
68
69
71{
72public:
73 OUTLINE_DECOMPOSER( FT_Outline& aOutline );
74
75 bool OutlineToSegments( std::vector<CONTOUR>* aContours );
76
77private:
78 void newContour();
79
80 void addContourPoint( const VECTOR2D& p );
81
86 int winding( const std::vector<VECTOR2D>& aContour ) const;
87
88 inline static unsigned int onCurve( char aTags )
89 {
90 return aTags & 0x1;
91 }
92
93 inline static unsigned int thirdOrderBezierPoint( char aTags )
94 {
95 return onCurve( aTags ) ? 0 : aTags & 0x2;
96 }
97
98 inline static unsigned int secondOrderBezierPoint( char aTags )
99 {
100 return onCurve( aTags ) ? 0 : !thirdOrderBezierPoint( aTags );
101 }
102
103 inline static unsigned int hasDropout( char aTags )
104 {
105 return aTags & 0x4;
106 }
107
108 inline static unsigned int dropoutMode( char aTags )
109 {
110 return hasDropout( aTags ) ? ( aTags & 0x38 ) : 0;
111 }
112
113 // FT_Outline_Decompose callbacks
114 static int moveTo( const FT_Vector* aEndPoint, void* aCallbackData );
115
116 static int lineTo( const FT_Vector* aEndPoint, void* aCallbackData );
117
118 static int quadraticTo( const FT_Vector* aControlPoint, const FT_Vector* aEndPoint,
119 void* aCallbackData );
120
121 static int cubicTo( const FT_Vector* aFirstControlPoint, const FT_Vector* aSecondControlPoint,
122 const FT_Vector* aEndPoint, void* aCallbackData );
123
124private:
125 FT_Outline& m_outline;
126 std::vector<CONTOUR>* m_contours;
127
129};
130
131} //namespace KIFONT
132
133#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:707