forked from modesty/pdf2json
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpdfunit.js
116 lines (97 loc) · 2.93 KB
/
pdfunit.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
'use strict';
let nodeUtil = require("util");
let PDFUnit = (function PFPUnitClosure() {
'use strict';
// private static
let _nextId = 1;
let _name = 'PDFUnit';
let dpi = 96.0;
let gridXPerInch = 4.0;
let gridYPerInch = 4.0;
let _pixelXPerGrid = dpi/gridXPerInch;
let _pixelYPerGrid = dpi/gridYPerInch;
let _pixelPerPoint = dpi/72;
let kColors = [
'#000000', // 0
'#ffffff', // 1
'#4c4c4c', // 2
'#808080', // 3
'#999999', // 4
'#c0c0c0', // 5
'#cccccc', // 6
'#e5e5e5', // 7
'#f2f2f2', // 8
'#008000', // 9
'#00ff00', // 10
'#bfffa0', // 11
'#ffd629', // 12
'#ff99cc', // 13
'#004080', // 14
'#9fc0e1', // 15
'#5580ff', // 16
'#a9c9fa', // 17
'#ff0080', // 18
'#800080', // 19
'#ffbfff', // 20
'#e45b21', // 21
'#ffbfaa', // 22
'#008080', // 23
'#ff0000', // 24
'#fdc59f', // 25
'#808000', // 26
'#bfbf00', // 27
'#824100', // 28
'#007256', // 29
'#008000', // 30
'#000080', // Last + 1
'#008080', // Last + 2
'#800080', // Last + 3
'#ff0000', // Last + 4
'#0000ff', // Last + 5
'#008000', // Last + 6
'#000000' // Last + 7
];
// constructor
let cls = function () {
// private
let _id = _nextId++;
// public (every instance will have their own copy of these methods, needs to be lightweight)
this.get_id = function() { return _id; };
this.get_name = function() { return _name + _id; };
};
cls.toFixedFloat = function(fNum) {
return parseFloat(fNum.toFixed(3));
};
cls.colorCount = function() {
return kColors.length;
};
cls.toPixelX = function(formX) {
return Math.round(formX * _pixelXPerGrid);
};
cls.toPixelY = function(formY) {
return Math.round(formY * _pixelYPerGrid);
};
cls.pointToPixel = function(point) {// Point unit (1/72 an inch) to pixel units
return point * _pixelPerPoint;
};
cls.getColorByIndex = function(clrId) {
return kColors[clrId];
};
cls.toFormPoint = function(viewportX, viewportY) {
return [(viewportX / _pixelXPerGrid), (viewportY / _pixelYPerGrid)];
};
cls.toFormX = function(viewportX) {
return cls.toFixedFloat(viewportX / _pixelXPerGrid);
};
cls.toFormY = function(viewportY) {
return cls.toFixedFloat(viewportY / _pixelYPerGrid);
};
cls.findColorIndex = function(color) {
if (color.length === 4)
color += "000";
//MQZ. 07/29/2013: if color is not in dictionary, just return -1. The caller (pdffont, pdffill) will set the actual color
return kColors.indexOf(color);
};
return cls;
})();
module.exports = PDFUnit;