1 |
|
2 |
|
3 |
|
4 |
|
5 | 'use strict';
|
6 | var fs = require('fs'),
|
7 | path = require('path'),
|
8 | fontCarrier = require('font-carrier');
|
9 |
|
10 |
|
11 |
|
12 | function decimal2Hex(n) {
|
13 | var hex = n.toString(16);
|
14 | hex = '000'.substr(0, 3 - hex.length) + hex;
|
15 | return hex;
|
16 | }
|
17 |
|
18 |
|
19 | function generateIconContent(n) {
|
20 | return '' + decimal2Hex(n);
|
21 | }
|
22 |
|
23 | function mkdir(dir) {
|
24 | if (!fs.existsSync(dir)) {
|
25 | mkdir(path.dirname(dir));
|
26 | fs.mkdirSync(dir);
|
27 | }
|
28 | }
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 | exports.genarateFonts = function(opt, icons) {
|
35 | var svgPath = opt._svgPath,
|
36 |
|
37 | font = fontCarrier.create(),
|
38 | svgsObj = {},
|
39 | filePath,
|
40 | iconContent;
|
41 | icons.forEach(function(icon, index) {
|
42 | filePath = path.join(svgPath, icon + '.svg');
|
43 | if (fs.existsSync(filePath)) {
|
44 | iconContent = generateIconContent(index);
|
45 | svgsObj[iconContent] = fs.readFileSync(filePath).toString();
|
46 | } else {
|
47 | fis.log.warning(filePath + ' ------ svg file does not exist!');
|
48 | }
|
49 |
|
50 | });
|
51 |
|
52 | font.setSvg(svgsObj);
|
53 |
|
54 | var outFontsContent = font.output({}),
|
55 | outFileName = 'iconfont';
|
56 |
|
57 | return {
|
58 | content: outFontsContent,
|
59 | subpath: opt.output + '/' + outFileName
|
60 | };
|
61 | };
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 | exports.generateCss = function(opt, iconNames, start, step) {
|
68 | var self = this,
|
69 | pseudoClass = ~['after', 'before'].indexOf(opt.pseClass) ? opt.pseClass : 'after',
|
70 | start = start || 0,
|
71 | step = step || 1;
|
72 |
|
73 | var content = [],
|
74 | iconContent;
|
75 |
|
76 |
|
77 | content.push('@font-face { ');
|
78 | content.push('font-family: "mfont";');
|
79 | content.push('src: url("' + opt._fontCdn.eot + '");');
|
80 | content.push('src: url("' + opt._fontCdn.eot + '?#iefix") format("embedded-opentype"),');
|
81 | content.push('url("' + opt._fontCdn.woff + '") format("woff"),');
|
82 | content.push('url("' + opt._fontCdn.ttf + '") format("truetype");}');
|
83 |
|
84 | if(!opt.nonBaseCss) {
|
85 | content.push('.icon-font{font-family:"mfont";font-size:16px;font-style:normal;font-weight: normal;font-variant: normal;text-transform: none;line-height: 1;position: relative;-webkit-font-smoothing: antialiased;}');
|
86 | }
|
87 |
|
88 | iconNames.forEach(function(iconName) {
|
89 | iconContent = generateIconContent(start++);
|
90 | if (typeof iconContent !== 'undefined' && fs.existsSync(path.join(opt._svgPath, iconName + '.svg'))) {
|
91 | iconContent = iconContent.replace('', '\\f');
|
92 | content.push('.i-' + iconName + ':' + pseudoClass + '{content: "' + iconContent + '";}');
|
93 | }
|
94 | });
|
95 | return content.join('\r\n');
|
96 | };
|
97 |
|
98 |
|
99 | function file2Base64(str) {
|
100 | return new Buffer(str).toString('base64');
|
101 | }
|
102 |
|
103 |
|
104 | exports.generateBase64Css = function(opt, iconNames, ttf, start, step) {
|
105 | var fontBase64 = file2Base64(ttf);
|
106 |
|
107 | var self = this,
|
108 | pseudoClass = ~['after', 'before'].indexOf(opt.pseClass) ? opt.pseClass : 'after',
|
109 | start = start || 0,
|
110 | step = step || 1;
|
111 |
|
112 | var content = [],
|
113 | iconContent;
|
114 | content.push('@font-face { ');
|
115 | content.push('font-family: "mfont";');
|
116 | content.push('src: url("data:application/octet-stream;base64,' + fontBase64 + '") format("truetype");}');
|
117 |
|
118 | if(!opt.nonBaseCss) {
|
119 | content.push('.icon-font{font-family:"mfont";font-size:16px;font-style:normal;font-weight: normal;font-variant: normal;text-transform: none;line-height: 1;position: relative;-webkit-font-smoothing: antialiased;}');
|
120 | }
|
121 |
|
122 | iconNames.forEach(function(iconName) {
|
123 | iconContent = generateIconContent(start++);
|
124 | if (typeof iconContent !== 'undefined' && fs.existsSync(path.join(opt._svgPath, iconName + '.svg'))) {
|
125 | iconContent = iconContent.replace('', '\\f');
|
126 | content.push('.i-' + iconName + ':' + pseudoClass + '{content: "' + iconContent + '";}');
|
127 | }
|
128 | });
|
129 | return content.join('\r\n');
|
130 | }
|
131 |
|
132 |
|
133 | exports.exportCssFile = function(iconNames, pseClass, path) {
|
134 | var content = this.generateCss(iconNames, pseClass);
|
135 | fs.writeFileSync(path, content, 'utf-8');
|
136 | }
|