forked from NativeScript/NativeScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfont-common.ts
More file actions
187 lines (158 loc) · 5.01 KB
/
font-common.ts
File metadata and controls
187 lines (158 loc) · 5.01 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import enums = require("ui/enums");
import definitios = require("ui/styling/font");
import * as converters from "./converters";
export class Font implements definitios.Font {
public static default = undefined;
private _fontFamily: string;
private _fontStyle: string;
private _fontWeight: string;
private _fontSize: number;
get fontFamily(): string {
return this._fontFamily;
}
set fontFamily(value: string) {
throw new Error("fontFamily is read-only");
}
get fontStyle(): string {
return this._fontStyle;
}
set fontStyle(value: string) {
throw new Error("fontStyle is read-only");
}
get fontWeight(): string {
return this._fontWeight;
}
set fontWeight(value: string) {
throw new Error("fontWeight is read-only");
}
get fontSize(): number {
return this._fontSize;
}
set fontSize(value: number) {
throw new Error("fontSize is read-only");
}
get isBold(): boolean {
return this._fontWeight.toLowerCase() === enums.FontWeight.bold;;
}
set isBold(value: boolean) {
throw new Error("isBold is read-only");
}
get isItalic(): boolean {
return this._fontStyle.toLowerCase() === enums.FontStyle.italic;;
}
set isItalic(value: boolean) {
throw new Error("isItalic is read-only");
}
constructor(family: string, size: number, style: string, weight: string) {
this._fontFamily = family;
this._fontSize = size;
this._fontStyle = style;
this._fontWeight = weight;
}
public getAndroidTypeface(): android.graphics.Typeface {
return undefined;
}
public getUIFont(defaultFont: UIFont): UIFont {
return undefined;
}
public withFontFamily(family: string): Font {
throw new Error("This should be called on the derived class");
}
public withFontStyle(style: string): Font {
throw new Error("This should be called on the derived class");
}
public withFontWeight(weight: string): Font {
throw new Error("This should be called on the derived class");
}
public withFontSize(size: number): Font {
throw new Error("This should be called on the derived class");
}
public static equals(value1: Font, value2: Font): boolean {
// both values are falsy
if (!value1 && !value2) {
return true;
}
// only one is falsy
if (!value1 || !value2) {
return false;
}
return value1.fontFamily === value2.fontFamily &&
value1.fontSize === value2.fontSize &&
value1.fontStyle === value2.fontStyle &&
value1.fontWeight === value2.fontWeight;
}
public static parse(cssValue: string): Font {
var parsed = parseFont(cssValue);
var size = converters.fontSizeConverter(parsed.fontSize);
size = !!size ? size : undefined;
return new Font(parsed.fontFamily, size, parsed.fontStyle, parsed.fontWeight);
}
}
export function parseFontFamily(value: string): Array<string> {
var result = new Array<string>();
if (!value) {
return result;
}
var split = value.split(",");
for (var i = 0; i < split.length; i++) {
var str = split[i].trim().replace(/['"]+/g, '');
if (str) {
result.push(str);
}
}
return result;
}
export module genericFontFamilies {
export var serif = "serif";
export var sansSerif = "sans-serif";
export var monospace = "monospace";
}
var styles = new Set();
["italic", "oblique"].forEach((val, i, a) => styles.add(val));
var weights = new Set();
["bold", "bolder", "lighter", "100", "200", "300", "400", "500", "600", "700", "800", "900"].forEach((val, i, a) => weights.add(val));
interface ParsedFont {
fontStyle?: string;
fontVariant?: string;
fontWeight?: string,
lineHeight?: string,
fontSize?: string,
fontFamily?: string
}
function parseFont(fontValue: string): ParsedFont {
var result: ParsedFont = {
fontStyle: "normal",
fontVariant: "normal",
fontWeight: "normal",
}
var parts = fontValue.split(/\s+/);
var part: string;
while (part = parts.shift()) {
if (part === "normal") {
// nothing to do here
}
else if (part === "small-caps") {
// The only supported font variant in shorthand font
result.fontVariant = part;
}
else if (styles.has(part)) {
result.fontStyle = part
}
else if (weights.has(part)) {
result.fontWeight = part;
}
else if (!result.fontSize) {
var sizes = part.split("/");
result.fontSize = sizes[0];
result.lineHeight = sizes.length > 1 ? sizes[1] : undefined;
}
else {
result.fontFamily = part;
if (parts.length) {
result.fontFamily += " " + parts.join(" ");
}
break;
}
}
return result;
}