forked from Automattic/node-canvas
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFontParser.h
More file actions
115 lines (95 loc) · 2.46 KB
/
FontParser.h
File metadata and controls
115 lines (95 loc) · 2.46 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
#pragma once
#include <string>
#include <vector>
#include <optional>
#include <memory>
#include <variant>
#include <unordered_map>
#include "CharData.h"
enum class FontStyle {
Normal,
Italic,
Oblique
};
enum class FontVariant {
Normal,
SmallCaps
};
struct FontProperties {
double fontSize{16.0f};
std::vector<std::string> fontFamily;
uint16_t fontWeight{400};
FontVariant fontVariant{FontVariant::Normal};
FontStyle fontStyle{FontStyle::Normal};
};
class Token {
public:
enum class Type {
Invalid,
Number,
Percent,
Identifier,
Slash,
Comma,
QuotedString,
Whitespace,
EndOfInput
};
Token(Type type, std::string value);
Token(Type type, double value);
Token(Type type);
Type type() const { return type_; }
const std::string& getString() const;
double getNumber() const;
private:
Type type_;
std::variant<std::string, double> value_;
};
class Tokenizer {
public:
Tokenizer(std::string_view input);
Token nextToken();
private:
std::string_view input_;
size_t position_{0};
// Util
std::string utf8Encode(uint32_t codepoint);
inline bool isWhitespace(char c) const {
return charData[static_cast<uint8_t>(c)] & CharData::Whitespace;
}
inline bool isNewline(char c) const {
return charData[static_cast<uint8_t>(c)] & CharData::Newline;
}
// Moving through the string
char peek() const;
char advance();
// Tokenize
Token parseNumber();
Token parseIdentifier();
uint32_t parseUnicode();
bool parseEscape(std::string& str);
Token parseString(char quote);
};
class FontParser {
public:
static FontProperties parse(const std::string& fontString, bool* success = nullptr);
private:
static const std::unordered_map<std::string, uint16_t> weightMap;
static const std::unordered_map<std::string, double> unitMap;
FontParser(std::string_view input);
void advance();
void skipWs();
bool check(Token::Type type) const;
bool checkWs() const;
bool parseFontStyle(FontProperties& props);
bool parseFontVariant(FontProperties& props);
bool parseFontWeight(FontProperties& props);
bool parseFontSize(FontProperties& props);
bool parseLineHeight(FontProperties& props);
bool parseFontFamily(FontProperties& props);
FontProperties parseFont();
Tokenizer tokenizer_;
Token currentToken_;
Token nextToken_;
bool hasError_{false};
};