UNPKG

4.14 kBJavaScriptView Raw
1/// <reference path="../typings/globals/node/index.d.ts" />
2class Font2{
3 constructor(projectPath,options){
4 const _ts = this;
5 _ts.m = {
6 fs:require('fs-extra'),
7 path:require('path'),
8 tip:require('../lib/tip'),
9 Fontmin:require('fontmin'),
10 Font:require('fonteditor-core').Font,
11 pathInfo:require('../lib/getPathInfo')
12 };
13
14 //_ts.init();
15 _ts.test();
16 }
17 test(){
18 const _ts = this;
19
20 let dir = fws.cmdPath,
21 fileNames = _ts.m.fs.readdirSync(dir),
22 fontPaths = {
23 'otf':[],
24 'svg':[],
25 'svg2':[]
26 };
27
28 fileNames.forEach((item,index)=>{
29 let filePath = _ts.m.path.join(dir,item),
30 fileInfo = _ts.m.pathInfo(filePath);
31 if(fileInfo.extension === '.otf'){
32 fontPaths.otf.push(filePath);
33 }else if(fileInfo.extension === '.svg'){
34 fontPaths.svg.push(filePath);
35 }else if(fileInfo.extension === '.svg2'){
36 fontPaths.svg2.push(filePath);
37 };
38 });
39
40 for(let i in fontPaths){
41 if(fontPaths[i].length){
42 fontPaths[i].forEach(item => {
43 let dir = _ts.m.path.resolve(item,'..'),
44 itemInfo = _ts.m.pathInfo(item);
45
46 let inBuffer = _ts.m.fs.readFileSync(item),
47 font = _ts.m.Font.create(inBuffer,{
48 type:i,
49 hinting:true
50 }),
51 outBuffer = font.write({
52 type:'ttf',
53 hinting:true
54 });
55
56 _ts.m.fs.writeFileSync(_ts.m.path.join(dir,itemInfo.name+'.ttf'),outBuffer);
57 })
58
59 };
60 };
61 }
62
63 //初始化
64 init(){
65 const _ts = this,
66 fontmin = new _ts.m.Fontmin;
67
68 let dir = fws.cmdPath,
69 fileNames = _ts.m.fs.readdirSync(dir),
70 fontPaths = {
71 'otf':[],
72 'svg':[],
73 'svg2':[]
74 };
75
76 fileNames.forEach((item,index)=>{
77 let filePath = _ts.m.path.join(dir,item),
78 fileInfo = _ts.m.pathInfo(filePath);
79 if(fileInfo.extension === '.otf'){
80 fontPaths.otf.push(filePath);
81 }else if(fileInfo.extension === '.svg'){
82 fontPaths.svg.push(filePath);
83 }else if(fileInfo.extension === '.svg2'){
84 fontPaths.svg2.push(filePath);
85 };
86 });
87
88 for(let i in fontPaths){
89 if(i==='otf' && fontPaths[i].length){
90 fontPaths[i].forEach(item => {
91 let dir = _ts.m.path.resolve(item,'..'),
92 itemInfo = _ts.m.pathInfo(item);
93 fontmin
94 .src(item)
95 .use(_ts.m.Fontmin.otf2ttf())
96 //.dest(_ts.m.path.join(dir,itemInfo.name+'.ttf'))
97 .run((err,files)=>{
98 if(err){
99 _ts.m.tip.error(err)
100 }else{
101 _ts.m.fs.writeFileSync(_ts.m.path.join(dir,itemInfo.name+'.ttf'),files[0].contents);
102 };
103 });
104
105
106 })
107
108 };
109 };
110
111
112
113
114
115 }
116};
117
118
119module.exports = {
120 regTask:{
121 command:'[name]',
122 description:'otf、svg、svgs字体转换为ttf',
123 option:[
124 ['-s, --server','开启http server']
125 ],
126 help:()=>{
127 console.log(' 补充说明:');
128 console.log(' ------------------------------------------------------------');
129 console.log(' 暂无');
130 },
131 action:Font2
132 }
133};
\No newline at end of file