1 | 'use strict';
|
2 |
|
3 | var path = require('path');
|
4 | var Stream = require('readable-stream');
|
5 | var gutil = require('gulp-util');
|
6 | var BufferStreams = require('bufferstreams');
|
7 | var svg2ttf = require('svg2ttf');
|
8 |
|
9 | var PLUGIN_NAME = 'gulp-svg2ttf';
|
10 |
|
11 |
|
12 | function svg2ttfTransform(options) {
|
13 |
|
14 | return function svg2ttfTransformCb(err, buf, cb) {
|
15 |
|
16 |
|
17 | if(err) {
|
18 | return cb(new gutil.PluginError(PLUGIN_NAME, err, { showStack: true }));
|
19 | }
|
20 |
|
21 |
|
22 | try {
|
23 | buf = new Buffer(svg2ttf(String(buf), {
|
24 | ts: options.timestamp,
|
25 | copyright: options.copyright,
|
26 | }).buffer);
|
27 | return cb(null, buf);
|
28 | } catch(err2) {
|
29 | return cb(new gutil.PluginError(PLUGIN_NAME, err2, { showStack: true }));
|
30 | }
|
31 |
|
32 | };
|
33 | }
|
34 |
|
35 |
|
36 | function svg2ttfGulp(options) {
|
37 | var stream = new Stream.Transform({ objectMode: true });
|
38 |
|
39 | options = options || {};
|
40 | options.ignoreExt = options.ignoreExt || false;
|
41 | options.clone = options.clone || false;
|
42 | options.timestamp = 'number' === typeof options.timestamp ?
|
43 | options.timestamp :
|
44 | {}.undef;
|
45 | options.copyright = 'string' === typeof options.copyright ?
|
46 | options.copyright :
|
47 | {}.undef;
|
48 |
|
49 | stream._transform = function svg2ttfTransformStream(file, unused, done) {
|
50 | var cntStream;
|
51 | var newFile;
|
52 |
|
53 |
|
54 | if(file.isNull()) {
|
55 | stream.push(file); done();
|
56 | return;
|
57 | }
|
58 |
|
59 |
|
60 | if((!options.ignoreExt) && '.svg' !== path.extname(file.path)) {
|
61 | stream.push(file); done();
|
62 | return;
|
63 | }
|
64 |
|
65 |
|
66 |
|
67 | if(options.clone) {
|
68 | if(file.isBuffer()) {
|
69 | stream.push(file.clone());
|
70 | } else {
|
71 | cntStream = file.contents;
|
72 | file.contents = null;
|
73 | newFile = file.clone();
|
74 | file.contents = cntStream.pipe(new Stream.PassThrough());
|
75 | newFile.contents = cntStream.pipe(new Stream.PassThrough());
|
76 | stream.push(newFile);
|
77 | }
|
78 | }
|
79 |
|
80 | file.path = gutil.replaceExtension(file.path, '.ttf');
|
81 |
|
82 |
|
83 | if(file.isBuffer()) {
|
84 | try {
|
85 | file.contents = new Buffer(svg2ttf(String(file.contents), {
|
86 | ts: options.timestamp,
|
87 | copyright: options.copyright,
|
88 | }).buffer);
|
89 | } catch(err) {
|
90 | stream.emit('error',
|
91 | new gutil.PluginError(PLUGIN_NAME, err, { showStack: true }));
|
92 | }
|
93 |
|
94 |
|
95 | } else {
|
96 | file.contents = file.contents.pipe(new BufferStreams(svg2ttfTransform(options)));
|
97 | }
|
98 |
|
99 | stream.push(file);
|
100 | done();
|
101 | };
|
102 |
|
103 | return stream;
|
104 |
|
105 | }
|
106 |
|
107 |
|
108 | svg2ttfGulp.fileTransform = svg2ttfTransform;
|
109 |
|
110 |
|
111 | module.exports = svg2ttfGulp;
|