forked from jrnewell/goog-webfont-dl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcssGenerator.js
136 lines (114 loc) · 3.6 KB
/
cssGenerator.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
var Promise = require("bluebird");
var fs = require("fs");
var path = require("path");
function FakeFile() {
this.s = "";
}
FakeFile.prototype.write = function(s) {
this.s += s;
};
FakeFile.prototype.toString = function() {
return this.s;
};
function getFile(outFile) {
if (outFile === "-") {
return process.stdout;
}
else if (outFile == null) {
return new FakeFile();
}
else if (outFile instanceof Buffer) {
return outFile;
}
else if (typeof outFile === "string") {
return fs.createWriteStream(path.normalize(outFile));
}
else {
throw new Error("options.out is of unknown type");
}
}
function generateCSS(options, parsingResults) {
return new Promise(function(resolve, reject) {
var usingStdout = options.out === "-";
var cssOutFile = options.out;
var cssObj = parsingResults.cssObj;
// defer this until here so no CSS is created if an error occurred.
var cssOut = getFile(cssOutFile);
// write out css with new file names
for(var subset in cssObj) {
var subsetObj = cssObj[subset];
for(var family in subsetObj) {
var familyObj = subsetObj[family];
for (var style in familyObj) {
var styleObj = familyObj[style];
for (var weight in styleObj) {
var weightObj = styleObj[weight];
if (subset !== "default") {
cssOut.write("/* " + subset + " */\n");
}
cssOut.write("@font-face {\n");
cssOut.write(" font-family: '" + family + "';\n");
cssOut.write(" font-style: " + style + ";\n");
cssOut.write(" font-weight: " + weight + ";\n");
// special handling for eot (ie 9)
if (weightObj.urls["embedded-opentype"]) {
cssOut.write(" src: url(" + weightObj.urls["embedded-opentype"] + ");\n");
}
cssOut.write(" src: ");
var addComma = false;
// local names
for (var i = 0; i < weightObj.localNames.length; i++) {
var localName = weightObj.localNames[i];
if (addComma) {
cssOut.write(", ");
}
else {
addComma = true;
}
cssOut.write("local('" + localName + "')");
}
// url and formats
for (var format in weightObj.urls) {
var url = weightObj.urls[format];
if (format === "embedded-opentype") {
url += "?#iefix";
}
else if (format === "svg") {
url += "#" + family.replace(/\s+/g, "");
}
if (addComma) {
cssOut.write(", ");
}
else {
addComma = true;
}
cssOut.write("url(" + url + ") format('" + format + "')");
}
cssOut.write(";\n");
if (weightObj.unicodeRange) {
cssOut.write(" unicode-range: " + weightObj.unicodeRange + ";\n");
}
cssOut.write("}\n\n");
}
}
}
}
if (!usingStdout && typeof cssOutFile === "string") {
if (options.verbose) {
console.log("CSS output was successfully written to “" + cssOutFile + "”");
}
cssOut.on("error", function(err) {
reject(new Error("write error: " + err));
});
cssOut.on("finish", function() {
resolve();
});
cssOut.end();
}
else {
resolve((cssOutFile == null) ? cssOut.toString() : null);
}
});
}
module.exports = generateCSS;
module.exports.default = generateCSS;