forked from briantbutton/meshwriter-font
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
96 lines (89 loc) · 4.21 KB
/
index.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
/*!
* Babylon MeshWriter-Font
* https://github.com/BabylonJS/Babylon.js
* (c) 2018-2019 Brian Todd Button
* Released under the MIT license
*/
(function(){
const opentype = require("./opentype/opentype");
const PiP = require("./js/pip");
const MeshWriter = require("./js/meshwriter");
const Glyphin = require("./js/glyphin");
const config = require("./config");
const fs = require("fs");
const root = this;
global.PiP = PiP;
global.config_ = config;
// *-*=* *=*-* *-*=* *=*-* *-*=* *=*-* *-*=* *=*-* *-*=* *=*-* *-*=* *=*-*
// The command function
global.convertFontFile = function(opt){
const options = opt && typeof opt === "object" ? opt : {};
const suffix = opt.suffix;
const name = opt.name;
const dd = opt.dir;
const sd = opt.subdir;
const compress = opt.compress === true;
const subdir = typeof sd === "string" && sd.length>1 ? sd+"/" : "";
const dir = typeof dd === "string" && dd.length>1 ? dd+"/" : "";
const coverage = typeof name === "string" ?
typeof config[name.toLowerCase()] === "object" ?
config[name.toLowerCase()]
: typeof config["default-coverage"] === "object" ?
config["default-coverage"]
: ["0","1","2","3","4","5","6","7","8","9"]
: typeof config["default-coverage"] === "object" ?
config["default-coverage"]
: ["0","1","2","3","4","5","6","7","8","9"];
console.log(coverage);
if(!arguments.length){
return "arguments: suffix , filename [ directory [ subdirectory ] ] "
}else{
fs.readFile(config.relPathFrom+name+"."+suffix,onReadFile)
}
function onReadFile(err,data){
if(err){
console.log("Could not read from '"+config.relPathFrom+name+"."+suffix+"'");
console.log(err)
}else{
let fontName;
const fontArrayBuffer = convertBuff2AB(data);
const nativeFont = opentype.parse(fontArrayBuffer,{lowMemory:false});
const glyphin = new Glyphin(nativeFont,coverage);
let fileText = glyphin.getMeshWriterSeries(compress);
try{
fontName = nativeFont.names.fontFamily.en;
}catch(e){
fontName = "font"
}
fileText = filePre(fontName,nativeFont.outlinesFormat)+fileText+filePost(fontName);
const fileBuffer = Buffer.from(fileText);
fs.writeFile(config.relPathTo+name.toLowerCase()+"."+"js",fileBuffer,onWriteFile)
}
};
function onWriteFile(err){
if(err){
console.log("Could not write to '"+config.relPathTo+name+"."+"js"+"'");
console.log(err)
}else{
console.log("Wrote MeshWriter font file to '"+config.relPathTo+name.toLowerCase()+"."+"js"+"'")
}
}
};
function convertBuff2AB(buff){
var ab = new ArrayBuffer(buff.length),
vw = new Uint8Array(ab);
for ( var i = 0; i < buff.length; i++ ) { vw[i] = buff[i] }
return ab
};
function filePre(fontName,format){
let line1 = "// "+fontName.toUpperCase()+" "+fontName.toUpperCase()+" "+fontName.toUpperCase()+"\n// \n\n";
if(format==="cff"){
return line1+"define(\n [],\n function(){\n\n return function(codeList){\n\n var font={reverseHoles:true,reverseShapes:false},nbsp='\u00A0';\n\n"
}else{
return line1+"define(\n [],\n function(){\n\n return function(codeList){\n\n var font={reverseHoles:false,reverseShapes:true},nbsp='\u00A0';\n\n"
}
};
function filePost(){
return "\n return font;\n }\n }\n);\n"
}
}).call(this);