1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
|
/*************************************************************************
** GlyfTable.hpp **
** **
** This file is part of dvisvgm -- a fast DVI to SVG converter **
** Copyright (C) 2005-2024 Martin Gieseking <martin.gieseking@uos.de> **
** **
** This program is free software; you can redistribute it and/or **
** modify it under the terms of the GNU General Public License as **
** published by the Free Software Foundation; either version 3 of **
** the License, or (at your option) any later version. **
** **
** This program is distributed in the hope that it will be useful, but **
** WITHOUT ANY WARRANTY; without even the implied warranty of **
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the **
** GNU General Public License for more details. **
** **
** You should have received a copy of the GNU General Public License **
** along with this program; if not, see <http://www.gnu.org/licenses/>. **
*************************************************************************/
#pragma once
#include <cmath>
#include <list>
#include <ostream>
#include <vector>
#include "TTFTable.hpp"
#include "../Pair.hpp"
namespace ttf {
struct PointInfo {
enum State {OFFCURVE, ONCURVE, REMOVED};
PointInfo (double x, double y, bool oncurve) : coords(std::round(x), std::round(y)), state(oncurve ? ONCURVE : OFFCURVE) {}
PointInfo (int x, int y) : coords(x, y), state(ONCURVE) {}
bool oncurve () const {return state == ONCURVE;}
bool offcurve () const {return state == OFFCURVE;}
bool removed () const {return state == REMOVED;}
void markForRemoval () {state = REMOVED;}
Pair<int> coords; ///< relative coordinates to previous one in contour
State state;
};
class Contour {
friend class GlyfTable;
using Iterator = std::vector<PointInfo>::iterator;
using ConstIterator = std::vector<PointInfo>::const_iterator;
public:
Contour () =default;
Contour (Contour &&contour) =default;
Contour (std::list<Contour> &&contours);
Contour (const Contour &contour) =delete;
Iterator begin () {return _pointInfos.begin();}
Iterator end () {return _pointInfos.end();}
ConstIterator begin () const {return _pointInfos.begin();}
ConstIterator end () const {return _pointInfos.end();}
size_t numPoints () const {return _pointInfos.size();}
const PointInfo& operator [] (size_t pos) const {return _pointInfos[pos];}
bool empty () const {return _pointInfos.empty();}
std::vector<uint8_t> glyphFlags () const;
void reverse ();
void reduceNumberOfPoints ();
void append (PointInfo &&pinfo) {_pointInfos.emplace_back(std::move(pinfo));}
void reserve (size_t n) {_pointInfos.reserve(n);}
protected:
void convertAbsoluteToRelative ();
#ifdef TTFDEBUG
void writePS (std::ostream &os) const;
#endif
private:
std::vector<PointInfo> _pointInfos;
};
/** This class provides the functions required to write the glyf data table of a TTF/OTF font.
* https://www.microsoft.com/typography/otspec/glyf.htm */
class GlyfTable : public TTFTable {
public:
uint32_t tag () const override {return name2id("glyf");}
void write (std::ostream &os) const override;
protected:
std::vector<Contour> computeContours () const;
std::list<Contour> computeContours (uint32_t charcode) const;
static void computeBbox (const std::list<Contour> &contours, int &xmin, int &ymin, int &xmax, int &ymax);
size_t writeGlyphContours (std::ostream &os, uint32_t charcode) const;
// Contour getNotDefContour () const;
#ifdef TTFDEBUG
void writePS (std::ostream &os, const std::list<Contour> &contours, uint32_t charcode) const;
#endif
};
} // namespace ttf
|