90 |
91 |
92 | Introduction
93 |
94 | By default, bardcode renders in the top left corner of the canvas.
95 |
96 |
97 | {`
98 | var w = 420, h = 200;
99 | var canvas = document.getElementById("demo-1");
100 | var g = canvas.getContext("2d");
101 | g.fillStyle = "white";
102 | g.fillRect(0, 0, w, h);
103 | bardcode.drawBarcode(g, "test", { });
104 | `}
105 |
106 |
107 | So why is it not at the very top left? bardcode, by default, includes a quiet zone to the left and right of the barcode. Since it's left aligned, there is some space on the left. This can be configured:
108 |
109 |
110 | {`
111 | var w = 420, h = 200;
112 | var canvas = document.getElementById("demo-2");
113 | var g = canvas.getContext("2d");
114 | g.fillStyle = "white";
115 | g.fillRect(0, 0, w, h);
116 | bardcode.drawBarcode(g, "test", {
117 | quietZoneSize: 0
118 | });
119 | `}
120 |
121 |
122 | You can align and position the barcode within the canvas however you want:
123 |
124 |
125 | {`
126 | var w = 420, h = 230;
127 | var canvas = document.getElementById("demo-3");
128 | var g = canvas.getContext("2d");
129 | g.fillStyle = "white";
130 | g.fillRect(0, 0, w, h);
131 | bardcode.drawBarcode(g, "test", {
132 | x: w/2,
133 | y: h/2,
134 | horizontalAlign: "center",
135 | verticalAlign: "middle"
136 | });
137 | `}
138 |
139 |
140 | You might also find it useful to rotate the barcode. Use options.angle (degrees clockwise).
141 |
142 |
143 | {`
144 | var w = 420, h = 250;
145 | var canvas = document.getElementById("demo-4");
146 | var g = canvas.getContext("2d");
147 | g.fillStyle = "white";
148 | g.fillRect(0, 0, w, h);
149 | bardcode.drawBarcode(g, "test", {
150 | x: w/2,
151 | y: h/2,
152 | horizontalAlign: "center",
153 | verticalAlign: "middle",
154 | angle: 90
155 | });
156 | `}
157 |
158 |
159 |
160 |
161 |
162 | Width and height
163 |
164 | There are several options for controlling the width and height of rendered barcodes.
165 |
166 | Setting the moduleWidth sets the width of the thinnest bar. The default value is 2.892.
167 |
168 | That is, of course, unless you specify the maxWidth which will decrease the moduleWidth (if necessary) so that the barcode will fit within it (including the quiet zone unless quietZoneSize is set to 0).
169 |
170 | However, both are ignored if the width is specfied. The moduleWidth will be set to whatever value is necessary to make the barcode (plus quiet zone unless quietZoneSize is set to 0) have the given width.
171 |
172 |
173 | {`
174 | var w = 420, h = 290;
175 | var canvas = document.getElementById("demo-5");
176 | var g = canvas.getContext("2d");
177 | g.fillStyle = "white";
178 | g.fillRect(0, 0, w, h);
179 |
180 | drawGrid(g, w, h, 20);
181 |
182 | bardcode.drawBarcode(g, "test", {
183 | x: w/2,
184 | y: 0,
185 | moduleWidth: 2,
186 | quietZoneSize: 0,
187 | horizontalAlign: "center",
188 | verticalAlign: "top",
189 | height: 50
190 | });
191 | bardcode.drawBarcode(g, "test", {
192 | x: w/2,
193 | y: 60,
194 | maxWidth: 2*w/3,
195 | horizontalAlign: "center",
196 | verticalAlign: "top",
197 | height: 50
198 | });
199 | bardcode.drawBarcode(g, "test", {
200 | x: w/2,
201 | y: 120,
202 | maxWidth: 2*w/3,
203 | quietZoneSize: 0,
204 | horizontalAlign: "center",
205 | verticalAlign: "top",
206 | height: 50
207 | });
208 | bardcode.drawBarcode(g, "test", {
209 | x: w/2,
210 | y: 180,
211 | width: w,
212 | horizontalAlign: "center",
213 | verticalAlign: "top",
214 | height: 50
215 | });
216 | bardcode.drawBarcode(g, "test", {
217 | x: w/2,
218 | y: 240,
219 | width: w,
220 | quietZoneSize: 0,
221 | horizontalAlign: "center",
222 | verticalAlign: "top",
223 | height: 50
224 | });
225 | `}
226 |
227 |
228 |
229 |
230 |
231 | Symbologies
232 |
233 | So far all of the barcodes have been Code-128 encoded "test". Use the second parameter to `bardcode.drawBarcode` to change the barcode text, and set options.type to use a different symbology.
234 |
235 | The supported symbologies are:
236 |
237 | - Codabar
238 | - Code 128
239 | - Code 39
240 | - EAN-8
241 | - EAN-13
242 | - FIM
243 | - ITF (interleaved 2 of 5)
244 | - UPC-A
245 |
246 |
247 |
248 | {`
249 | var w = 420, h = 370;
250 | var canvas = document.getElementById("demo-6");
251 | var g = canvas.getContext("2d");
252 | g.fillStyle = "white";
253 | g.fillRect(0, 0, w, h);
254 |
255 | var barcodes = [
256 | { type: "Codabar", val: "31117013206375" },
257 | { type: "Code 128", val: "BarCode 1" },
258 | { type: "Code 39", val: "0123456789" },
259 | { type: "EAN-8", val: "9638507" },
260 | { type: "EAN-13", val: "590123412345" },
261 | { type: "FIM", val: "C" },
262 | { type: "ITF", val: "04004" },
263 | { type: "UPC-A", val: "90123412345" }
264 | ];
265 |
266 | var targetHeight = (h + 5)/barcodes.length;
267 |
268 | for (var i = 0; i < barcodes.length; i++) {
269 | var barcode = barcodes[i];
270 | bardcode.drawBarcode(g, barcode.val, {
271 | type: barcode.type,
272 | x: w/2,
273 | y: i*targetHeight,
274 | maxWidth: w,
275 | horizontalAlign: "center",
276 | verticalAlign: "top",
277 | height: targetHeight - 5
278 | });
279 | }
280 | `}
281 |
282 |
283 |
284 |
,
285 | document.getElementById("content")
286 | );
287 |
288 | })();
289 |
290 |
--------------------------------------------------------------------------------
/lib/Code128.js:
--------------------------------------------------------------------------------
1 | // At the moment Code 128 supports 128-B well, with basic support for 128-A
2 | // (shifting for each character which lives in A but not B). Most of the
3 | // building blocks for 128-C are included, but it's not implemented yet.
4 | //
5 | // There is also no ISO-8859-1 support (which would use FNC4).
6 |
7 | const CODE_128_VAL = 0;
8 | const CODE_128_CHAR_A = 1;
9 | const CODE_128_CHAR_B = 2;
10 | const CODE_128_CHAR_C = 3;
11 | const CODE_128_BITS = 4;
12 |
13 | // VALUE, CODE A CHAR, CODE B CHAR, CODE C CHARS, BITS
14 | const code128 = [
15 | [0, " ", " ", "00", "11011001100"],
16 | [1, "!", "!", "01", "11001101100"],
17 | [2, "\"", "\"", "02", "11001100110"],
18 | [3, "#", "#", "03", "10010011000"],
19 | [4, "$", "$", "04", "10010001100"],
20 | [5, " %", " %", "05", "10001001100"],
21 | [6, "&", "&", "06", "10011001000"],
22 | [7, "'", "'", "07", "10011000100"],
23 | [8, "(", "(", "08", "10001100100"],
24 | [9, ")", ")", "09", "11001001000"],
25 | [10, "*", "*", "10", "11001000100"],
26 | [11, "+", "+", "11", "11000100100"],
27 | [12, ", ", ", ", "12", "10110011100"],
28 | [13, "-", "-", "13", "10011011100"],
29 | [14, ".", ".", "14", "10011001110"],
30 | [15, "/", "/", "15", "10111001100"],
31 | [16, "0", "0", "16", "10011101100"],
32 | [17, "1", "1", "17", "10011100110"],
33 | [18, "2", "2", "18", "11001110010"],
34 | [19, "3", "3", "19", "11001011100"],
35 | [20, "4", "4", "20", "11001001110"],
36 | [21, "5", "5", "21", "11011100100"],
37 | [22, "6", "6", "22", "11001110100"],
38 | [23, "7", "7", "23", "11101101110"],
39 | [24, "8", "8", "24", "11101001100"],
40 | [25, "9", "9", "25", "11100101100"],
41 | [26, ":", ":", "26", "11100100110"],
42 | [27, ";", ";", "27", "11101100100"],
43 | [28, "<", "<", "28", "11100110100"],
44 | [29, "=", "=", "29", "11100110010"],
45 | [30, ">", ">", "30", "11011011000"],
46 | [31, "?", "?", "31", "11011000110"],
47 | [32, "@", "@", "32", "11000110110"],
48 | [33, "A", "A", "33", "10100011000"],
49 | [34, "B", "B", "34", "10001011000"],
50 | [35, "C", "C", "35", "10001000110"],
51 | [36, "D", "D", "36", "10110001000"],
52 | [37, "E", "E", "37", "10001101000"],
53 | [38, "F", "F", "38", "10001100010"],
54 | [39, "G", "G", "39", "11010001000"],
55 | [40, "H", "H", "40", "11000101000"],
56 | [41, "I", "I", "41", "11000100010"],
57 | [42, "J", "J", "42", "10110111000"],
58 | [43, "K", "K", "43", "10110001110"],
59 | [44, "L", "L", "44", "10001101110"],
60 | [45, "M", "M", "45", "10111011000"],
61 | [46, "N", "N", "46", "10111000110"],
62 | [47, "O", "O", "47", "10001110110"],
63 | [48, "P", "P", "48", "11101110110"],
64 | [49, "Q", "Q", "49", "11010001110"],
65 | [50, "R", "R", "50", "11000101110"],
66 | [51, "S", "S", "51", "11011101000"],
67 | [52, "T", "T", "52", "11011100010"],
68 | [53, "U", "U", "53", "11011101110"],
69 | [54, "V", "V", "54", "11101011000"],
70 | [55, "W", "W", "55", "11101000110"],
71 | [56, "X", "X", "56", "11100010110"],
72 | [57, "Y", "Y", "57", "11101101000"],
73 | [58, "Z", "Z", "58", "11101100010"],
74 | [59, "[", "[", "59", "11100011010"],
75 | [60, "\\", "\\", "60", "11101111010"],
76 | [61, "]", "]", "61", "11001000010"],
77 | [62, "^", "^", "62", "11110001010"],
78 | [63, "_", "_", "63", "10100110000"],
79 | [64, "\0", "`", "64", "10100001100"],
80 | [65, "\x01", "a", "65", "10010110000"],
81 | [66, "\x02", "b", "66", "10010000110"],
82 | [67, "\x03", "c", "67", "10000101100"],
83 | [68, "\x04", "d", "68", "10000100110"],
84 | [69, "\x05", "e", "69", "10110010000"],
85 | [70, "\x06", "f", "70", "10110000100"],
86 | [71, "\x07", "g", "71", "10011010000"],
87 | [72, "\b", "h", "72", "10011000010"],
88 | [73, "\x01", "i", "73", "10000110100"],
89 | [74, "\n", "j", "74", "10000110010"],
90 | [75, "\v", "k", "75", "11000010010"],
91 | [76, "\f", "l", "76", "11001010000"],
92 | [77, "\r", "m", "77", "11110111010"],
93 | [78, "\x08", "n", "78", "11000010100"],
94 | [79, "\x09", "o", "79", "10001111010"],
95 | [80, "\x10", "p", "80", "10100111100"],
96 | [81, "\x11", "q", "81", "10010111100"],
97 | [82, "\x12", "r", "82", "10010011110"],
98 | [83, "\x13", "s", "83", "10111100100"],
99 | [84, "\x14", "t", "84", "10011110100"],
100 | [85, "\x15", "u", "85", "10011110010"],
101 | [86, "\x16", "v", "86", "11110100100"],
102 | [87, "\x17", "w", "87", "11110010100"],
103 | [88, "\x18", "x", "88", "11110010010"],
104 | [89, "\x19", "y", "89", "11011011110"],
105 | [90, "\x1a", "z", "90", "11011110110"],
106 | [91, "\x1b", "{", "91", "11110110110"],
107 | [92, "\x1c", "|", "92", "10101111000"],
108 | [93, "\x1d", "}", "93", "10100011110"],
109 | [94, "\x1e", "~", "94", "10001011110"],
110 | [95, "\x1f", "\x7f", "95", "10111101000"],
111 | [96, "FNC 3", "FNC 3", "96", "10111100010"],
112 | [97, "FNC 2", "FNC 2", "97", "11110101000"],
113 | [98, "SHIFT B", "SHIFT A", "98", "11110100010"],
114 | [99, "CODE C", "CODE C", "99", "10111011110"],
115 | [100, "CODE B", "FNC 4", "CODE B", "10111101110"],
116 | [101, "FNC 4", "CODE A", "CODE A", "11101011110"],
117 | [102, "FNC 1", "FNC 1", "FNC 1", "11110101110"],
118 | [103, "A0", "A0", "A0", "11010000100"],
119 | [104, "B0", "B0", "B0", "11010010000"],
120 | [105, "C0", "C0", "C0", "11010011100"],
121 | [106, "STOP", "STOP", "STOP", "1100011101011"],
122 | ];
123 |
124 | // Reverse lookups from first 4 columns. Created on first use.
125 | let code128ValLookup = null;
126 | let code128ALookup = null;
127 | let code128BLookup = null;
128 | let code128CLookup = null;
129 |
130 | const makeCode128Lookups = function() {
131 | if (code128ValLookup) {
132 | return;
133 | }
134 |
135 | code128ValLookup = { };
136 | code128ALookup = { };
137 | code128BLookup = { };
138 | code128CLookup = { };
139 |
140 | for (let i = 0; i < code128.length; i++) {
141 | const data = code128[i];
142 |
143 | const val = data[CODE_128_VAL];
144 | const charA = data[CODE_128_CHAR_A];
145 | const charB = data[CODE_128_CHAR_B];
146 | const charC = data[CODE_128_CHAR_C];
147 |
148 | code128ValLookup[val] = data;
149 | code128ALookup[charA] = data;
150 | code128BLookup[charB] = data;
151 | code128CLookup[charC] = data;
152 | }
153 | };
154 |
155 | export default function encodeCode128(text) {
156 | makeCode128Lookups();
157 |
158 | const chars = new Array(1 + text.length);
159 | chars[0] = "B0";
160 | for (let i = 0, len = text.length; i < len; i++) {
161 | chars[1 + i] = text[i];
162 | }
163 |
164 | // Basic support for Code 128-A: do shift A before characters which live in
165 | // A but not in B.
166 | for (let i = 0; i < chars.length; i++) {
167 | const ch = chars[i];
168 | if (code128ALookup[ch] && !code128BLookup[ch]) {
169 | chars.splice(i, 0, "SHIFT A");
170 | i++;
171 | }
172 | }
173 |
174 | // Main thing we return is a list of characters.
175 | // [{ bits: "1011011", mode: "A", value: 44, char: "L", humanReadable: true }, ...]
176 | const outlist = [];
177 |
178 | let mode;
179 | switch (chars[0]) {
180 | case "A0": mode = "A"; break;
181 | case "B0": mode = "B"; break;
182 | case "C0": mode = "C"; break;
183 | default: throw new Error("Expected a starting character");
184 | }
185 |
186 | // If "SHIFT A" is a character in chars, then shift to mode A for one
187 | // character, and then switch back to returnMode.
188 | let returnMode;
189 |
190 | let sum = 0;
191 | for (let i = 0; i < chars.length; i++) {
192 | const ch = chars[i];
193 |
194 | // The weight value depends on what mode we're in.
195 | let data;
196 | switch (mode) {
197 | case "A":
198 | data = code128ALookup[ch];
199 | break;
200 | case "B":
201 | data = code128BLookup[ch];
202 | break;
203 | case "C":
204 | data = code128CLookup[ch];
205 | break;
206 | }
207 |
208 | // Throw an error if the character does not exist in this mode.
209 | if (!data) {
210 | throw new Error(`Invalid input (no such char '${ch}' in mode ${mode})`);
211 | }
212 |
213 | const val = data[CODE_128_VAL];
214 | const bits = data[CODE_128_BITS];
215 |
216 | // Contribute to sum.
217 | const n = i || 1; // both start code and first text char have position 1.
218 | sum += n * val;
219 |
220 | outlist.push({
221 | bits: bits,
222 | char: ch,
223 | humanReadable: null,
224 | _mode: mode,
225 | _val: val,
226 | });
227 |
228 | // Return to previous mode after a shift.
229 | if (returnMode) {
230 | mode = returnMode;
231 | returnMode = null;
232 | }
233 |
234 | // Handle mode switches.
235 | switch (ch) {
236 | case "CODE A": mode = "A"; break;
237 | case "CODE B": mode = "B"; break;
238 | case "CODE C": mode = "C"; break;
239 | case "SHIFT A": returnMode = mode; mode = "A"; break;
240 | case "SHIFT B": returnMode = mode; mode = "B"; break;
241 |
242 | default:
243 | // Do nothing for non-mode switching characters.
244 | break;
245 | }
246 | }
247 |
248 | const checksum = sum % 103;
249 |
250 | // Append the checksum.
251 | const checksumData = code128ValLookup[checksum];
252 | outlist.push({
253 | bits: checksumData[CODE_128_BITS],
254 | char: "CHECKSUM",
255 | humanReadable: false,
256 | _val: checksum,
257 | });
258 |
259 | // Append the stop char.
260 | const stopData = code128ALookup.STOP;
261 | outlist.push({
262 | bits: stopData[CODE_128_BITS],
263 | char: "STOP",
264 | humanReadable: false,
265 | _val: stopData[CODE_128_VAL],
266 | });
267 |
268 | return {
269 | type: "bits",
270 | checksum: checksum,
271 | data: outlist,
272 | };
273 | }
274 |
275 |
--------------------------------------------------------------------------------
/demos/barcode-generator/src/FileSaver.js:
--------------------------------------------------------------------------------
1 | /* FileSaver.js
2 | * A saveAs() FileSaver implementation.
3 | * 1.1.20160328
4 | *
5 | * By Eli Grey, http://eligrey.com
6 | * License: MIT
7 | * See https://github.com/eligrey/FileSaver.js/blob/master/LICENSE.md
8 | */
9 |
10 | /*global self */
11 | /*jslint bitwise: true, indent: 4, laxbreak: true, laxcomma: true, smarttabs: true, plusplus: true */
12 |
13 | /*! @source http://purl.eligrey.com/github/FileSaver.js/blob/master/FileSaver.js */
14 |
15 | var saveAs = saveAs || (function(view) {
16 | "use strict";
17 | // IE <10 is explicitly unsupported
18 | if (typeof navigator !== "undefined" && /MSIE [1-9]\./.test(navigator.userAgent)) {
19 | return;
20 | }
21 | var
22 | doc = view.document
23 | // only get URL when necessary in case Blob.js hasn't overridden it yet
24 | , get_URL = function() {
25 | return view.URL || view.webkitURL || view;
26 | }
27 | , save_link = doc.createElementNS("http://www.w3.org/1999/xhtml", "a")
28 | , can_use_save_link = "download" in save_link
29 | , click = function(node) {
30 | var event = new MouseEvent("click");
31 | node.dispatchEvent(event);
32 | }
33 | , is_safari = /Version\/[\d\.]+.*Safari/.test(navigator.userAgent)
34 | , webkit_req_fs = view.webkitRequestFileSystem
35 | , req_fs = view.requestFileSystem || webkit_req_fs || view.mozRequestFileSystem
36 | , throw_outside = function(ex) {
37 | (view.setImmediate || view.setTimeout)(function() {
38 | throw ex;
39 | }, 0);
40 | }
41 | , force_saveable_type = "application/octet-stream"
42 | , fs_min_size = 0
43 | // the Blob API is fundamentally broken as there is no "downloadfinished" event to subscribe to
44 | , arbitrary_revoke_timeout = 1000 * 40 // in ms
45 | , revoke = function(file) {
46 | var revoker = function() {
47 | if (typeof file === "string") { // file is an object URL
48 | get_URL().revokeObjectURL(file);
49 | } else { // file is a File
50 | file.remove();
51 | }
52 | };
53 | /* // Take note W3C:
54 | var
55 | uri = typeof file === "string" ? file : file.toURL()
56 | , revoker = function(evt) {
57 | // idealy DownloadFinishedEvent.data would be the URL requested
58 | if (evt.data === uri) {
59 | if (typeof file === "string") { // file is an object URL
60 | get_URL().revokeObjectURL(file);
61 | } else { // file is a File
62 | file.remove();
63 | }
64 | }
65 | }
66 | ;
67 | view.addEventListener("downloadfinished", revoker);
68 | */
69 | setTimeout(revoker, arbitrary_revoke_timeout);
70 | }
71 | , dispatch = function(filesaver, event_types, event) {
72 | event_types = [].concat(event_types);
73 | var i = event_types.length;
74 | while (i--) {
75 | var listener = filesaver["on" + event_types[i]];
76 | if (typeof listener === "function") {
77 | try {
78 | listener.call(filesaver, event || filesaver);
79 | } catch (ex) {
80 | throw_outside(ex);
81 | }
82 | }
83 | }
84 | }
85 | , auto_bom = function(blob) {
86 | // prepend BOM for UTF-8 XML and text/* types (including HTML)
87 | if (/^\s*(?:text\/\S*|application\/xml|\S*\/\S*\+xml)\s*;.*charset\s*=\s*utf-8/i.test(blob.type)) {
88 | return new Blob(["\ufeff", blob], {type: blob.type});
89 | }
90 | return blob;
91 | }
92 | , FileSaver = function(blob, name, no_auto_bom) {
93 | if (!no_auto_bom) {
94 | blob = auto_bom(blob);
95 | }
96 | // First try a.download, then web filesystem, then object URLs
97 | var
98 | filesaver = this
99 | , type = blob.type
100 | , blob_changed = false
101 | , object_url
102 | , target_view
103 | , dispatch_all = function() {
104 | dispatch(filesaver, "writestart progress write writeend".split(" "));
105 | }
106 | // on any filesys errors revert to saving with object URLs
107 | , fs_error = function() {
108 | if (target_view && is_safari && typeof FileReader !== "undefined") {
109 | // Safari doesn't allow downloading of blob urls
110 | var reader = new FileReader();
111 | reader.onloadend = function() {
112 | var base64Data = reader.result;
113 | target_view.location.href = "data:attachment/file" + base64Data.slice(base64Data.search(/[,;]/));
114 | filesaver.readyState = filesaver.DONE;
115 | dispatch_all();
116 | };
117 | reader.readAsDataURL(blob);
118 | filesaver.readyState = filesaver.INIT;
119 | return;
120 | }
121 | // don't create more object URLs than needed
122 | if (blob_changed || !object_url) {
123 | object_url = get_URL().createObjectURL(blob);
124 | }
125 | if (target_view) {
126 | target_view.location.href = object_url;
127 | } else {
128 | var new_tab = view.open(object_url, "_blank");
129 | if (new_tab === undefined && is_safari) {
130 | //Apple do not allow window.open, see http://bit.ly/1kZffRI
131 | view.location.href = object_url
132 | }
133 | }
134 | filesaver.readyState = filesaver.DONE;
135 | dispatch_all();
136 | revoke(object_url);
137 | }
138 | , abortable = function(func) {
139 | return function() {
140 | if (filesaver.readyState !== filesaver.DONE) {
141 | return func.apply(this, arguments);
142 | }
143 | };
144 | }
145 | , create_if_not_found = {create: true, exclusive: false}
146 | , slice
147 | ;
148 | filesaver.readyState = filesaver.INIT;
149 | if (!name) {
150 | name = "download";
151 | }
152 | if (can_use_save_link) {
153 | object_url = get_URL().createObjectURL(blob);
154 | setTimeout(function() {
155 | save_link.href = object_url;
156 | save_link.download = name;
157 | click(save_link);
158 | dispatch_all();
159 | revoke(object_url);
160 | filesaver.readyState = filesaver.DONE;
161 | });
162 | return;
163 | }
164 | // Object and web filesystem URLs have a problem saving in Google Chrome when
165 | // viewed in a tab, so I force save with application/octet-stream
166 | // http://code.google.com/p/chromium/issues/detail?id=91158
167 | // Update: Google errantly closed 91158, I submitted it again:
168 | // https://code.google.com/p/chromium/issues/detail?id=389642
169 | if (view.chrome && type && type !== force_saveable_type) {
170 | slice = blob.slice || blob.webkitSlice;
171 | blob = slice.call(blob, 0, blob.size, force_saveable_type);
172 | blob_changed = true;
173 | }
174 | // Since I can't be sure that the guessed media type will trigger a download
175 | // in WebKit, I append .download to the filename.
176 | // https://bugs.webkit.org/show_bug.cgi?id=65440
177 | if (webkit_req_fs && name !== "download") {
178 | name += ".download";
179 | }
180 | if (type === force_saveable_type || webkit_req_fs) {
181 | target_view = view;
182 | }
183 | if (!req_fs) {
184 | fs_error();
185 | return;
186 | }
187 | fs_min_size += blob.size;
188 | req_fs(view.TEMPORARY, fs_min_size, abortable(function(fs) {
189 | fs.root.getDirectory("saved", create_if_not_found, abortable(function(dir) {
190 | var save = function() {
191 | dir.getFile(name, create_if_not_found, abortable(function(file) {
192 | file.createWriter(abortable(function(writer) {
193 | writer.onwriteend = function(event) {
194 | target_view.location.href = file.toURL();
195 | filesaver.readyState = filesaver.DONE;
196 | dispatch(filesaver, "writeend", event);
197 | revoke(file);
198 | };
199 | writer.onerror = function() {
200 | var error = writer.error;
201 | if (error.code !== error.ABORT_ERR) {
202 | fs_error();
203 | }
204 | };
205 | "writestart progress write abort".split(" ").forEach(function(event) {
206 | writer["on" + event] = filesaver["on" + event];
207 | });
208 | writer.write(blob);
209 | filesaver.abort = function() {
210 | writer.abort();
211 | filesaver.readyState = filesaver.DONE;
212 | };
213 | filesaver.readyState = filesaver.WRITING;
214 | }), fs_error);
215 | }), fs_error);
216 | };
217 | dir.getFile(name, {create: false}, abortable(function(file) {
218 | // delete file if it already exists
219 | file.remove();
220 | save();
221 | }), abortable(function(ex) {
222 | if (ex.code === ex.NOT_FOUND_ERR) {
223 | save();
224 | } else {
225 | fs_error();
226 | }
227 | }));
228 | }), fs_error);
229 | }), fs_error);
230 | }
231 | , FS_proto = FileSaver.prototype
232 | , saveAs = function(blob, name, no_auto_bom) {
233 | return new FileSaver(blob, name, no_auto_bom);
234 | }
235 | ;
236 | // IE 10+ (native saveAs)
237 | if (typeof navigator !== "undefined" && navigator.msSaveOrOpenBlob) {
238 | return function(blob, name, no_auto_bom) {
239 | if (!no_auto_bom) {
240 | blob = auto_bom(blob);
241 | }
242 | return navigator.msSaveOrOpenBlob(blob, name || "download");
243 | };
244 | }
245 |
246 | FS_proto.abort = function() {
247 | var filesaver = this;
248 | filesaver.readyState = filesaver.DONE;
249 | dispatch(filesaver, "abort");
250 | };
251 | FS_proto.readyState = FS_proto.INIT = 0;
252 | FS_proto.WRITING = 1;
253 | FS_proto.DONE = 2;
254 |
255 | FS_proto.error =
256 | FS_proto.onwritestart =
257 | FS_proto.onprogress =
258 | FS_proto.onwrite =
259 | FS_proto.onabort =
260 | FS_proto.onerror =
261 | FS_proto.onwriteend =
262 | null;
263 |
264 | return saveAs;
265 | }(
266 | typeof self !== "undefined" && self
267 | || typeof window !== "undefined" && window
268 | || this.content
269 | ));
270 | // `self` is undefined in Firefox for Android content script context
271 | // while `this` is nsIContentFrameMessageManager
272 | // with an attribute `content` that corresponds to the window
273 |
274 | if (typeof module !== "undefined" && module.exports) {
275 | module.exports.saveAs = saveAs;
276 | } else if ((typeof define !== "undefined" && define !== null) && (define.amd !== null)) {
277 | define([], function() {
278 | return saveAs;
279 | });
280 | }
--------------------------------------------------------------------------------
/dist/bardcode.min.js:
--------------------------------------------------------------------------------
1 | !function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t(e.bardcode={})}(this,function(e){"use strict";function t(e,t){if("number"!=typeof e)throw new Error("Expected "+t+" to be a number, got "+e)}function r(e,t){if("number"!=typeof e)throw new Error("Expected "+t+" to be a number, got "+e);if(e<=0)throw new Error("Expected "+t+" to be positive, got "+e)}function a(e){b();var t=new Array(1+e.length);t[0]="B0";for(var r=0,a=e.length;r