forked from metafloor/bwip-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
browser-bitmap.js
executable file
·109 lines (96 loc) · 2.77 KB
/
browser-bitmap.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
// file : bwip-js/browser-bitmap.js
//
// bwip-js bitmap interface for an HTML canvas.
//
// Copyright (c) 2011-2018 Mark Warren
//
// Licensed MIT. See the LICENSE file in the bwip-js root directory
// for the extended copyright notice.
"use strict";
module.exports = Bitmap;
// bgcolor is optional, defaults to #fff
function Bitmap(cvsid, rot, bgcolor) {
var rgb = [0, 0, 0];
var padx = 0; // padding-x
var pady = 0; // padding-y
// Initialized in imageinfo()
var imgwidth, imgheight, cvs, ctx, idata, bytes;
this.pad = function(x, y) {
if (rot == 'R' || rot == 'L') {
padx = y;
pady = x;
} else {
padx = x;
pady = y;
}
}
// Sets the final size for the drawing surface and maximum number of
// colors. If fonts are lazy-loaded and not available when the cross-compiled
// code is executing, the number of colors can be wrong.
this.imageinfo = function(width, height, ncolors) {
if (rot == 'R' || rot == 'L') {
imgwidth = height;
imgheight = width;
} else {
imgwidth = width;
imgheight = height;
}
cvs = cvsid instanceof window.HTMLCanvasElement
? cvsid : document.getElementById(cvsid);
cvs.width = imgwidth + 2*padx;
cvs.height = imgheight + 2*pady;
// Convert from cmyk?
if (bgcolor && bgcolor.length == 8) {
var c = parseInt(bgcolor.substr(0,2), 16) / 255;
var m = parseInt(bgcolor.substr(2,2), 16) / 255;
var y = parseInt(bgcolor.substr(4,2), 16) / 255;
var k = parseInt(bgcolor.substr(6,2), 16) / 255;
var r = Math.floor((1-c) * (1-k) * 255);
var g = Math.floor((1-m) * (1-k) * 255);
var b = Math.floor((1-y) * (1-k) * 255);
bgcolor = 'rgb(' + r + ',' + g + ',' + b + ')';
} else if (bgcolor) {
bgcolor = '#' + bgcolor;
}
ctx = cvs.getContext('2d');
ctx.fillStyle = bgcolor || '#fff';
ctx.fillRect(0, 0, cvs.width, cvs.height);
ctx.fillStyle = '#000';
idata = ctx.getImageData(padx, pady, imgwidth, imgheight);
bytes = idata.data;
}
this.color = function(r, g, b) {
rgb = [r, g, b];
}
// a is the alpha-level of the pixel [0 .. 255]
this.set = function(x, y, a) {
// PostScript builds bottom-up, we build top-down.
if (rot == 'N') {
y = imgheight - y - 1;
} else if (rot == 'I') {
x = imgwidth - x - 1;
} else {
y = imgwidth - y;
if (rot == 'L') {
var t = y;
y = imgheight - x - 1;
x = t - 1;
} else {
var t = x;
x = imgwidth - y;
y = t;
}
}
// alpha-blend with the existing pixel
var idx = (y * imgwidth + x) * 4
a = a / 255;
bytes[idx+0] = (bytes[idx+0] * (1 - a) + rgb[0] * a)|0;
bytes[idx+1] = (bytes[idx+1] * (1 - a) + rgb[1] * a)|0;
bytes[idx+2] = (bytes[idx+2] * (1 - a) + rgb[2] * a)|0;
bytes[idx+3] = 255;
}
this.finalize = function(callback) {
ctx.putImageData(idata, padx, pady);
callback(null, cvsid);
}
}