/
svgop-pkg.js
145 lines (136 loc) · 5.36 KB
/
svgop-pkg.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
137
138
139
140
141
142
143
144
145
#!/usr/bin/env node
// This version is for running on node and compiling via pkg
'use strict';
const util = require('util');
console.log = function() {
this._stderr.write('');
};
console.error = function() {
this._stdout.write(util.format.apply(null, arguments));
};
const getStdin = require('get-stdin');
var SVGO = require('../lib/svgo');
require('./domstubs.js').setStubs(global);
// var pdfjsLib = require('pdfjs-dist'); //require('../lib/pdf.combined.js');
// pdfjsLib.PDFJS.disableWorker = true;
// pdfjsLib.PDFJS.disableFontFace = true;
// pdfjsLib.PDFJS.disableStream = true;
// pdfjsLib.PDFJS.disableWebGL = true;
// pdfjsLib.PDFJS.verbosity = pdfjsLib.PDFJS.VERBOSITY_LEVELS.errors;
// pdfjsLib.verbosity = pdfjsLib.PDFJS.VERBOSITY_LEVELS.errors;
//console.log(pdfjsLib.PDFJS);
var svgo = new SVGO({
floatPrecision: 8,
plugins: [
'addAttributesToSVGElement',
'addClassesToSVGElement',
'cleanupAttrs',
'cleanupEnableBackground',
'cleanupIDs',
'cleanupListOfValues',
'cleanupNumericValues',
'collapseGroups',
'convertColors',
'convertPathData',
'convertShapeToPath',
'convertStyleToAttrs',
'convertTransform',
'inlineStyles',
'mergePaths',
'minifyStyles',
'moveElemsAttrsToGroup',
'moveGroupAttrsToElems',
'prefixIds',
'removeAttrs',
'removeComments',
'removeDesc',
'removeDimensions',
'removeDoctype',
'removeEditorsNSData',
'removeElementsByAttr',
'removeEmptyAttrs',
'removeEmptyContainers',
'removeEmptyText',
'removeHiddenElems',
'removeMetadata',
'removeNonInheritableGroupAttrs',
'removeRasterImages',
'removeScriptElement',
'removeStyleElement',
'removeTitle',
'removeUnknownsAndDefaults',
'removeUnusedNS',
'removeUselessDefs',
'removeUselessStrokeAndFill',
'removeViewBox',
'removeXMLNS',
'removeXMLProcInst',
'sortAttrs'
]
}
);
getStdin.buffer().then(data => {
// var doctype = data.toString('utf8').substring(0,5);
// if (doctype == "%PDF-") {
// pdfjsLib.getDocument({
// data: data.buffer,
// nativeImageDecoderSupport: pdfjsLib.NativeImageDecoding.DISPLAY
// }).then(function (pdfDoc) {
// var lastPromise = Promise.resolve();
// var loadPage = function (pageNum) {
// return pdfDoc.getPage(pageNum).then(function (page) {
// var viewport = page.getViewport(1.0);
// var svgGfx = new PDFJS.SVGGraphics(page.commonObjs, page.objs);
// svgGfx.embedFonts = true;
// return page.getOperatorList().then(function (opList) {
// return svgGfx.getSVG(opList, viewport).then(function (svg) {
// var svgstr = svg.toString().replace(/svg:/g, '');
// svgo.optimize(svgstr).then(function(result) {
// console.error(result.data.replace('<svg width','<svg xmlns="http://www.w3.org/2000/svg" width'));
// });
// });
// });
// });
// };
// lastPromise = lastPromise.then(loadPage.bind(null, 1));
// });
// } else {
svgo.optimize(data).then(function(result) {
console.error(result.data);
});
// };
});
/**
var stdin = process.openStdin();
var data = "";
stdin.on('data', function(chunk) {
data += chunk;
});
stdin.on('end', function() {
if (data.substring(0,4) == "%PDF") {
PDFJS.getDocument({
data: data,
nativeImageDecoderSupport: PDFJS.NativeImageDecoding.DISPLAY
}).then(function (pdfDocument) {
pdfDocument.getPage(1).then(function (page) {
var viewport = page.getViewport(1.0);
console.log('Size: ' + viewport.width + 'x' + viewport.height);
console.log();
page.getOperatorList().then(function (opList) {
var svgGfx = new PDFJS.SVGGraphics(page.commonObjs, page.objs);
svgGfx.embedFonts = true;
svgGfx.getSVG(opList, viewport).then(function (svg) {
console.log(svg);
});
});
});
});
console.log(data.substring(0,4));
} else {
svgo.optimize(data).then(function(result) {
console.log(result.data);
//console.log(JSON.stringify(svgo.config, null, 2));
});
}
});
**/