-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFontParser.h
More file actions
151 lines (140 loc) · 4.03 KB
/
FontParser.h
File metadata and controls
151 lines (140 loc) · 4.03 KB
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#pragma once
#ifndef FONTPARSER_H
#define FONTPARSER_H
#include <fstream>
#include <map>
#include <unordered_map>
#include <glm/glm.hpp>
#include "Glyph.h"
#endif // FONTPARSER_H
struct Tag {
uint32_t checkSum;
uint32_t offset;
uint32_t length;
};
struct GlyphHeader {
int16_t numOfContours;
BoundingRect rect;
};
struct FontMetric {
int16_t ascent;
int16_t descent;
};
class FontParser {
public:
explicit FontParser(const std::string& path);
/**
* Get general metrics for font.
* @return FontMetric that has ascent and descent of font
*/
FontMetric getFontMetric();
/**
* Get glyphs and required rendering width from Unicode codepoints.
* @param cps Vector of Unicode codepoints
* @param scale Scaling value of glyph size
* @return Pair of Glyphs and necessary width for rendering
*/
std::pair<std::vector<Glyph>, int> getGlyphs(std::vector<uint32_t> cps,
float scale);
/**
* Get glyph by Unicode.
* @param cp Unicode codepoint
* @return Glyph
*/
Glyph getGlyph(uint32_t cp);
private:
std::ifstream ifs;
std::map<std::string, Tag> directory;
std::unordered_map<uint32_t, uint16_t> unicodeToGlyphCode;
std::unordered_map<uint16_t, uint32_t> glyphCodeToOffset;
std::unordered_map<uint16_t, Metric> glyphMetric;
// stream helper methods
void skipBytes(unsigned bytes);
void jumpTo(unsigned byteOffset);
/**
* Read data from stream.
* @tparam T Target type
* @return Read value
*/
template <class T>
T readBeOrThrow();
uint8_t readUint8();
uint16_t readUint16();
uint32_t readUint32();
int8_t readInt8();
int16_t readInt16();
int32_t readInt32();
float readF2Dot14();
// Initializer methods
/**
* Load the glyph code to offset mapping from `loca` table and
* store them in the glyphCodeToOffset hashmap.
*/
void loadGlyphOffsetsMap();
/**
* Load the glyph code to metrics from `hmtx` table and
* store them in the glyphMetric hashmap.
*/
void loadGlyphMetricsMap();
/**
* Load Unicode to Glyph code table. Glyph code is not the offset in ttf file,
* the glyphCodeToOffset is there to retrieve it.
* Only supports format 12 table for now.
* https://developer.apple.com/fonts/TrueType-Reference-Manual/RM06/Chap6cmap.html
*/
void loadUnicodeToGlyphCodeMap();
// Glyph related methods
/**
* Read glyph header data.
* @param glyphCode Glyph code
* @return GlyphHeader data
*/
GlyphHeader readGlyphHeader(uint16_t glyphCode);
/**
* Get glyph by glyph code.
*
* @param glyphCode Glyph code
* @return Glyph
*/
Glyph getGlyphByCode(uint16_t glyphCode);
/**
* Get glyph components for the compound glyph.
* A compound glyph can have multiple components,
* which can be either simple glyph structure or another compound glyph.
* @param glyphCode Glyph code
* @param affineMat 3x3 matrix for affine transformation
* @return Vector of the glyph components
*/
std::vector<GlyphComponent> getCompoundSubComponents(
uint16_t glyphCode,
const glm::mat3& affineMat);
/**
* Get a single glyph component.
* @param numOfContours number of contours of the target component
* @param boundingRect bounding rectangle of the target component
* @param affineMat 3x3 matrix for affine transformation
* @return Glyph component
*/
GlyphComponent getGlyphComponent(
short numOfContours,
::BoundingRect boundingRect,
const glm::mat3& affineMat = glm::mat3(
1.0f));
/**
* Get coordinates for a glyph.
* @param n Number of vertices
* @param flags Flags
* @param isX is for X coordinate
* @return vector of coordinates
*/
std::vector<int> getGlyphCoordinates(const uint16_t& n,
const std::vector<uint8_t>& flags,
bool isX);
/**
* Get compound glyph.
* ARGS_ARE_XY_VALUES, ROUND_XY_TO_GRID, WE_HAVE_INSTRUCTIONS, OVERLAP_COMPOUND
* are not supported.
* @return Glyph
*/
Glyph getCompoundGlyph();
};