-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathvariablefont.js
151 lines (151 loc) · 4.37 KB
/
variablefont.js
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
/**
* @class VariableFont
*/
var VariableFont = /** @class */ (function () {
/**
* @param openTypeFont the `Font` object of opentype.js
*/
function VariableFont(openTypeFont) {
this.openTypeFont = openTypeFont;
this.ascender = 0;
this.descender = 0;
this.encoding = {};
this.glyphNames = {};
this.glyphs = {};
this.kerningPairs = {};
this.numGlyphs = 0;
this.numberOfHMetrics = 0;
this.outlinesFormat = "truetype";
this.substitution = {};
this.unitsPerEm = 0;
this.names = {};
this.url = '';
console.log("Copying properties...");
Object.assign(this, openTypeFont);
this.prototype = openTypeFont;
if (this.tables) {
var fvar = this.tables["fvar"];
if (fvar && fvar.axes && (fvar.axes.length > 0)) {
var sortedInstances = fvar.instances.sort(function (a, b) {
var condition = 0;
var tags = Object.keys(a.coordinates);
var i = 0;
while (condition == 0 && i < tags.length) {
condition = ((a.coordinates[tags[i]] > b.coordinates[tags[i]]) ? 1 : ((b.coordinates[tags[i]] > a.coordinates[tags[i]]) ? -1 : 0));
i++;
}
return condition;
});
}
if (this.names.fontFamily == null) {
this.names.fontFamily = this.names.postScriptName;
}
}
}
/**
* Get fvar table.
*/
VariableFont.prototype.getFvarTable = function () {
return this.tables["fvar"];
};
/**
* Get Axes info. from fvar table.
*/
VariableFont.prototype.getAxes = function () {
var fvar = this.getFvarTable();
if (fvar) {
return fvar.axes;
}
return null;
};
/**
* Get number of axes in fvar table.
*/
VariableFont.prototype.getAxesCount = function () {
var fvar = this.getFvarTable();
if (fvar) {
if (fvar.axes && (fvar.axes.length > 0)) {
return fvar.axes.length;
}
}
return 0;
};
/**
* Get axis name string from fvar table.
*/
VariableFont.prototype.getAxis = function (i) {
var fvar = this.getFvarTable();
if (fvar) {
return fvar.axes[i];
}
return null;
};
/**
* Get axis name string from fvar table.
*/
VariableFont.prototype.getAxisName = function (i) {
var fvar = this.getFvarTable();
if (fvar) {
return fvar.axes[i].name.en;
}
return null;
};
/**
* Get instances from fvar table.
*/
VariableFont.prototype.getInstances = function () {
var fvar = this.getFvarTable();
if (fvar) {
return fvar.instances;
}
return null;
};
/**
* Get number of instances in fvar table.
*/
VariableFont.prototype.getInstancesCount = function () {
var fvar = this.getFvarTable();
if (fvar) {
if (fvar.instances && (fvar.instances.length > 0)) {
return fvar.instances.length;
}
}
return 0;
};
/**
* Get named instance string from fvar table.
*/
VariableFont.prototype.getInstanceName = function (i) {
var fvar = this.getFvarTable();
if (fvar) {
return fvar.instances[i].name.en;
}
return null;
};
/**
* Get named instance string from fvar table.
*/
VariableFont.prototype.getNamedInstance = function (i) {
var fvar = this.getFvarTable();
if (fvar) {
return fvar.instances[i];
}
return null;
};
/**
* Get the font-variation-settings string for a named instance string from fvar table.
*/
VariableFont.prototype.getNamedInstanceSetting = function (i) {
var fvar = this.getFvarTable();
if (fvar) {
var settings = [];
var values = fvar.instances[i].coordinates;
for (var i = 0; i < fvar.axes.length; i++) {
settings.push("'" + fvar.axes[i].tag + "' " + values[fvar.axes[i].tag].toString());
}
return settings.join();
}
return null;
};
return VariableFont;
}());