Barcode Generation
This commit is contained in:
158
node_modules/jsbarcode/bin/renderers/canvas.js
generated
vendored
Executable file
158
node_modules/jsbarcode/bin/renderers/canvas.js
generated
vendored
Executable file
@@ -0,0 +1,158 @@
|
||||
"use strict";
|
||||
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
|
||||
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
|
||||
|
||||
var _merge = require("../help/merge.js");
|
||||
|
||||
var _merge2 = _interopRequireDefault(_merge);
|
||||
|
||||
var _shared = require("./shared.js");
|
||||
|
||||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
||||
|
||||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
||||
|
||||
var CanvasRenderer = function () {
|
||||
function CanvasRenderer(canvas, encodings, options) {
|
||||
_classCallCheck(this, CanvasRenderer);
|
||||
|
||||
this.canvas = canvas;
|
||||
this.encodings = encodings;
|
||||
this.options = options;
|
||||
}
|
||||
|
||||
_createClass(CanvasRenderer, [{
|
||||
key: "render",
|
||||
value: function render() {
|
||||
// Abort if the browser does not support HTML5 canvas
|
||||
if (!this.canvas.getContext) {
|
||||
throw new Error('The browser does not support canvas.');
|
||||
}
|
||||
|
||||
this.prepareCanvas();
|
||||
for (var i = 0; i < this.encodings.length; i++) {
|
||||
var encodingOptions = (0, _merge2.default)(this.options, this.encodings[i].options);
|
||||
|
||||
this.drawCanvasBarcode(encodingOptions, this.encodings[i]);
|
||||
this.drawCanvasText(encodingOptions, this.encodings[i]);
|
||||
|
||||
this.moveCanvasDrawing(this.encodings[i]);
|
||||
}
|
||||
|
||||
this.restoreCanvas();
|
||||
}
|
||||
}, {
|
||||
key: "prepareCanvas",
|
||||
value: function prepareCanvas() {
|
||||
// Get the canvas context
|
||||
var ctx = this.canvas.getContext("2d");
|
||||
|
||||
ctx.save();
|
||||
|
||||
(0, _shared.calculateEncodingAttributes)(this.encodings, this.options, ctx);
|
||||
var totalWidth = (0, _shared.getTotalWidthOfEncodings)(this.encodings);
|
||||
var maxHeight = (0, _shared.getMaximumHeightOfEncodings)(this.encodings);
|
||||
|
||||
this.canvas.width = totalWidth + this.options.marginLeft + this.options.marginRight;
|
||||
|
||||
this.canvas.height = maxHeight;
|
||||
|
||||
// Paint the canvas
|
||||
ctx.clearRect(0, 0, this.canvas.width, this.canvas.height);
|
||||
if (this.options.background) {
|
||||
ctx.fillStyle = this.options.background;
|
||||
ctx.fillRect(0, 0, this.canvas.width, this.canvas.height);
|
||||
}
|
||||
|
||||
ctx.translate(this.options.marginLeft, 0);
|
||||
}
|
||||
}, {
|
||||
key: "drawCanvasBarcode",
|
||||
value: function drawCanvasBarcode(options, encoding) {
|
||||
// Get the canvas context
|
||||
var ctx = this.canvas.getContext("2d");
|
||||
|
||||
var binary = encoding.data;
|
||||
|
||||
// Creates the barcode out of the encoded binary
|
||||
var yFrom;
|
||||
if (options.textPosition == "top") {
|
||||
yFrom = options.marginTop + options.fontSize + options.textMargin;
|
||||
} else {
|
||||
yFrom = options.marginTop;
|
||||
}
|
||||
|
||||
ctx.fillStyle = options.lineColor;
|
||||
|
||||
for (var b = 0; b < binary.length; b++) {
|
||||
var x = b * options.width + encoding.barcodePadding;
|
||||
|
||||
if (binary[b] === "1") {
|
||||
ctx.fillRect(x, yFrom, options.width, options.height);
|
||||
} else if (binary[b]) {
|
||||
ctx.fillRect(x, yFrom, options.width, options.height * binary[b]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}, {
|
||||
key: "drawCanvasText",
|
||||
value: function drawCanvasText(options, encoding) {
|
||||
// Get the canvas context
|
||||
var ctx = this.canvas.getContext("2d");
|
||||
|
||||
var font = options.fontOptions + " " + options.fontSize + "px " + options.font;
|
||||
|
||||
// Draw the text if displayValue is set
|
||||
if (options.displayValue) {
|
||||
var x, y;
|
||||
|
||||
if (options.textPosition == "top") {
|
||||
y = options.marginTop + options.fontSize - options.textMargin;
|
||||
} else {
|
||||
y = options.height + options.textMargin + options.marginTop + options.fontSize;
|
||||
}
|
||||
|
||||
ctx.font = font;
|
||||
|
||||
// Draw the text in the correct X depending on the textAlign option
|
||||
if (options.textAlign == "left" || encoding.barcodePadding > 0) {
|
||||
x = 0;
|
||||
ctx.textAlign = 'left';
|
||||
} else if (options.textAlign == "right") {
|
||||
x = encoding.width - 1;
|
||||
ctx.textAlign = 'right';
|
||||
}
|
||||
// In all other cases, center the text
|
||||
else {
|
||||
x = encoding.width / 2;
|
||||
ctx.textAlign = 'center';
|
||||
}
|
||||
|
||||
ctx.fillText(encoding.text, x, y);
|
||||
}
|
||||
}
|
||||
}, {
|
||||
key: "moveCanvasDrawing",
|
||||
value: function moveCanvasDrawing(encoding) {
|
||||
var ctx = this.canvas.getContext("2d");
|
||||
|
||||
ctx.translate(encoding.width, 0);
|
||||
}
|
||||
}, {
|
||||
key: "restoreCanvas",
|
||||
value: function restoreCanvas() {
|
||||
// Get the canvas context
|
||||
var ctx = this.canvas.getContext("2d");
|
||||
|
||||
ctx.restore();
|
||||
}
|
||||
}]);
|
||||
|
||||
return CanvasRenderer;
|
||||
}();
|
||||
|
||||
exports.default = CanvasRenderer;
|
||||
21
node_modules/jsbarcode/bin/renderers/index.js
generated
vendored
Executable file
21
node_modules/jsbarcode/bin/renderers/index.js
generated
vendored
Executable file
@@ -0,0 +1,21 @@
|
||||
'use strict';
|
||||
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
|
||||
var _canvas = require('./canvas.js');
|
||||
|
||||
var _canvas2 = _interopRequireDefault(_canvas);
|
||||
|
||||
var _svg = require('./svg.js');
|
||||
|
||||
var _svg2 = _interopRequireDefault(_svg);
|
||||
|
||||
var _object = require('./object.js');
|
||||
|
||||
var _object2 = _interopRequireDefault(_object);
|
||||
|
||||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
||||
|
||||
exports.default = { CanvasRenderer: _canvas2.default, SVGRenderer: _svg2.default, ObjectRenderer: _object2.default };
|
||||
30
node_modules/jsbarcode/bin/renderers/object.js
generated
vendored
Executable file
30
node_modules/jsbarcode/bin/renderers/object.js
generated
vendored
Executable file
@@ -0,0 +1,30 @@
|
||||
"use strict";
|
||||
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
|
||||
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
|
||||
|
||||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
||||
|
||||
var ObjectRenderer = function () {
|
||||
function ObjectRenderer(object, encodings, options) {
|
||||
_classCallCheck(this, ObjectRenderer);
|
||||
|
||||
this.object = object;
|
||||
this.encodings = encodings;
|
||||
this.options = options;
|
||||
}
|
||||
|
||||
_createClass(ObjectRenderer, [{
|
||||
key: "render",
|
||||
value: function render() {
|
||||
this.object.encodings = this.encodings;
|
||||
}
|
||||
}]);
|
||||
|
||||
return ObjectRenderer;
|
||||
}();
|
||||
|
||||
exports.default = ObjectRenderer;
|
||||
101
node_modules/jsbarcode/bin/renderers/shared.js
generated
vendored
Executable file
101
node_modules/jsbarcode/bin/renderers/shared.js
generated
vendored
Executable file
@@ -0,0 +1,101 @@
|
||||
"use strict";
|
||||
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
exports.getTotalWidthOfEncodings = exports.calculateEncodingAttributes = exports.getBarcodePadding = exports.getEncodingHeight = exports.getMaximumHeightOfEncodings = undefined;
|
||||
|
||||
var _merge = require("../help/merge.js");
|
||||
|
||||
var _merge2 = _interopRequireDefault(_merge);
|
||||
|
||||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
||||
|
||||
function getEncodingHeight(encoding, options) {
|
||||
return options.height + (options.displayValue && encoding.text.length > 0 ? options.fontSize + options.textMargin : 0) + options.marginTop + options.marginBottom;
|
||||
}
|
||||
|
||||
function getBarcodePadding(textWidth, barcodeWidth, options) {
|
||||
if (options.displayValue && barcodeWidth < textWidth) {
|
||||
if (options.textAlign == "center") {
|
||||
return Math.floor((textWidth - barcodeWidth) / 2);
|
||||
} else if (options.textAlign == "left") {
|
||||
return 0;
|
||||
} else if (options.textAlign == "right") {
|
||||
return Math.floor(textWidth - barcodeWidth);
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
function calculateEncodingAttributes(encodings, barcodeOptions, context) {
|
||||
for (var i = 0; i < encodings.length; i++) {
|
||||
var encoding = encodings[i];
|
||||
var options = (0, _merge2.default)(barcodeOptions, encoding.options);
|
||||
|
||||
// Calculate the width of the encoding
|
||||
var textWidth;
|
||||
if (options.displayValue) {
|
||||
textWidth = messureText(encoding.text, options, context);
|
||||
} else {
|
||||
textWidth = 0;
|
||||
}
|
||||
|
||||
var barcodeWidth = encoding.data.length * options.width;
|
||||
encoding.width = Math.ceil(Math.max(textWidth, barcodeWidth));
|
||||
|
||||
encoding.height = getEncodingHeight(encoding, options);
|
||||
|
||||
encoding.barcodePadding = getBarcodePadding(textWidth, barcodeWidth, options);
|
||||
}
|
||||
}
|
||||
|
||||
function getTotalWidthOfEncodings(encodings) {
|
||||
var totalWidth = 0;
|
||||
for (var i = 0; i < encodings.length; i++) {
|
||||
totalWidth += encodings[i].width;
|
||||
}
|
||||
return totalWidth;
|
||||
}
|
||||
|
||||
function getMaximumHeightOfEncodings(encodings) {
|
||||
var maxHeight = 0;
|
||||
for (var i = 0; i < encodings.length; i++) {
|
||||
if (encodings[i].height > maxHeight) {
|
||||
maxHeight = encodings[i].height;
|
||||
}
|
||||
}
|
||||
return maxHeight;
|
||||
}
|
||||
|
||||
function messureText(string, options, context) {
|
||||
var ctx;
|
||||
|
||||
if (context) {
|
||||
ctx = context;
|
||||
} else if (typeof document !== "undefined") {
|
||||
ctx = document.createElement("canvas").getContext("2d");
|
||||
} else {
|
||||
// If the text cannot be messured we will return 0.
|
||||
// This will make some barcode with big text render incorrectly
|
||||
return 0;
|
||||
}
|
||||
ctx.font = options.fontOptions + " " + options.fontSize + "px " + options.font;
|
||||
|
||||
// Calculate the width of the encoding
|
||||
var measureTextResult = ctx.measureText(string);
|
||||
if (!measureTextResult) {
|
||||
// Some implementations don't implement measureText and return undefined.
|
||||
// If the text cannot be measured we will return 0.
|
||||
// This will make some barcode with big text render incorrectly
|
||||
return 0;
|
||||
}
|
||||
var size = measureTextResult.width;
|
||||
return size;
|
||||
}
|
||||
|
||||
exports.getMaximumHeightOfEncodings = getMaximumHeightOfEncodings;
|
||||
exports.getEncodingHeight = getEncodingHeight;
|
||||
exports.getBarcodePadding = getBarcodePadding;
|
||||
exports.calculateEncodingAttributes = calculateEncodingAttributes;
|
||||
exports.getTotalWidthOfEncodings = getTotalWidthOfEncodings;
|
||||
189
node_modules/jsbarcode/bin/renderers/svg.js
generated
vendored
Executable file
189
node_modules/jsbarcode/bin/renderers/svg.js
generated
vendored
Executable file
@@ -0,0 +1,189 @@
|
||||
"use strict";
|
||||
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
|
||||
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
|
||||
|
||||
var _merge = require("../help/merge.js");
|
||||
|
||||
var _merge2 = _interopRequireDefault(_merge);
|
||||
|
||||
var _shared = require("./shared.js");
|
||||
|
||||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
||||
|
||||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
||||
|
||||
var svgns = "http://www.w3.org/2000/svg";
|
||||
|
||||
var SVGRenderer = function () {
|
||||
function SVGRenderer(svg, encodings, options) {
|
||||
_classCallCheck(this, SVGRenderer);
|
||||
|
||||
this.svg = svg;
|
||||
this.encodings = encodings;
|
||||
this.options = options;
|
||||
this.document = options.xmlDocument || document;
|
||||
}
|
||||
|
||||
_createClass(SVGRenderer, [{
|
||||
key: "render",
|
||||
value: function render() {
|
||||
var currentX = this.options.marginLeft;
|
||||
|
||||
this.prepareSVG();
|
||||
for (var i = 0; i < this.encodings.length; i++) {
|
||||
var encoding = this.encodings[i];
|
||||
var encodingOptions = (0, _merge2.default)(this.options, encoding.options);
|
||||
|
||||
var group = this.createGroup(currentX, encodingOptions.marginTop, this.svg);
|
||||
|
||||
this.setGroupOptions(group, encodingOptions);
|
||||
|
||||
this.drawSvgBarcode(group, encodingOptions, encoding);
|
||||
this.drawSVGText(group, encodingOptions, encoding);
|
||||
|
||||
currentX += encoding.width;
|
||||
}
|
||||
}
|
||||
}, {
|
||||
key: "prepareSVG",
|
||||
value: function prepareSVG() {
|
||||
// Clear the SVG
|
||||
while (this.svg.firstChild) {
|
||||
this.svg.removeChild(this.svg.firstChild);
|
||||
}
|
||||
|
||||
(0, _shared.calculateEncodingAttributes)(this.encodings, this.options);
|
||||
var totalWidth = (0, _shared.getTotalWidthOfEncodings)(this.encodings);
|
||||
var maxHeight = (0, _shared.getMaximumHeightOfEncodings)(this.encodings);
|
||||
|
||||
var width = totalWidth + this.options.marginLeft + this.options.marginRight;
|
||||
this.setSvgAttributes(width, maxHeight);
|
||||
|
||||
if (this.options.background) {
|
||||
this.drawRect(0, 0, width, maxHeight, this.svg).setAttribute("style", "fill:" + this.options.background + ";");
|
||||
}
|
||||
}
|
||||
}, {
|
||||
key: "drawSvgBarcode",
|
||||
value: function drawSvgBarcode(parent, options, encoding) {
|
||||
var binary = encoding.data;
|
||||
|
||||
// Creates the barcode out of the encoded binary
|
||||
var yFrom;
|
||||
if (options.textPosition == "top") {
|
||||
yFrom = options.fontSize + options.textMargin;
|
||||
} else {
|
||||
yFrom = 0;
|
||||
}
|
||||
|
||||
var barWidth = 0;
|
||||
var x = 0;
|
||||
for (var b = 0; b < binary.length; b++) {
|
||||
x = b * options.width + encoding.barcodePadding;
|
||||
|
||||
if (binary[b] === "1") {
|
||||
barWidth++;
|
||||
} else if (barWidth > 0) {
|
||||
this.drawRect(x - options.width * barWidth, yFrom, options.width * barWidth, options.height, parent);
|
||||
barWidth = 0;
|
||||
}
|
||||
}
|
||||
|
||||
// Last draw is needed since the barcode ends with 1
|
||||
if (barWidth > 0) {
|
||||
this.drawRect(x - options.width * (barWidth - 1), yFrom, options.width * barWidth, options.height, parent);
|
||||
}
|
||||
}
|
||||
}, {
|
||||
key: "drawSVGText",
|
||||
value: function drawSVGText(parent, options, encoding) {
|
||||
var textElem = this.document.createElementNS(svgns, 'text');
|
||||
|
||||
// Draw the text if displayValue is set
|
||||
if (options.displayValue) {
|
||||
var x, y;
|
||||
|
||||
textElem.setAttribute("style", "font:" + options.fontOptions + " " + options.fontSize + "px " + options.font);
|
||||
|
||||
if (options.textPosition == "top") {
|
||||
y = options.fontSize - options.textMargin;
|
||||
} else {
|
||||
y = options.height + options.textMargin + options.fontSize;
|
||||
}
|
||||
|
||||
// Draw the text in the correct X depending on the textAlign option
|
||||
if (options.textAlign == "left" || encoding.barcodePadding > 0) {
|
||||
x = 0;
|
||||
textElem.setAttribute("text-anchor", "start");
|
||||
} else if (options.textAlign == "right") {
|
||||
x = encoding.width - 1;
|
||||
textElem.setAttribute("text-anchor", "end");
|
||||
}
|
||||
// In all other cases, center the text
|
||||
else {
|
||||
x = encoding.width / 2;
|
||||
textElem.setAttribute("text-anchor", "middle");
|
||||
}
|
||||
|
||||
textElem.setAttribute("x", x);
|
||||
textElem.setAttribute("y", y);
|
||||
|
||||
textElem.appendChild(this.document.createTextNode(encoding.text));
|
||||
|
||||
parent.appendChild(textElem);
|
||||
}
|
||||
}
|
||||
}, {
|
||||
key: "setSvgAttributes",
|
||||
value: function setSvgAttributes(width, height) {
|
||||
var svg = this.svg;
|
||||
svg.setAttribute("width", width + "px");
|
||||
svg.setAttribute("height", height + "px");
|
||||
svg.setAttribute("x", "0px");
|
||||
svg.setAttribute("y", "0px");
|
||||
svg.setAttribute("viewBox", "0 0 " + width + " " + height);
|
||||
|
||||
svg.setAttribute("xmlns", svgns);
|
||||
svg.setAttribute("version", "1.1");
|
||||
|
||||
svg.setAttribute("style", "transform: translate(0,0)");
|
||||
}
|
||||
}, {
|
||||
key: "createGroup",
|
||||
value: function createGroup(x, y, parent) {
|
||||
var group = this.document.createElementNS(svgns, 'g');
|
||||
group.setAttribute("transform", "translate(" + x + ", " + y + ")");
|
||||
|
||||
parent.appendChild(group);
|
||||
|
||||
return group;
|
||||
}
|
||||
}, {
|
||||
key: "setGroupOptions",
|
||||
value: function setGroupOptions(group, options) {
|
||||
group.setAttribute("style", "fill:" + options.lineColor + ";");
|
||||
}
|
||||
}, {
|
||||
key: "drawRect",
|
||||
value: function drawRect(x, y, width, height, parent) {
|
||||
var rect = this.document.createElementNS(svgns, 'rect');
|
||||
|
||||
rect.setAttribute("x", x);
|
||||
rect.setAttribute("y", y);
|
||||
rect.setAttribute("width", width);
|
||||
rect.setAttribute("height", height);
|
||||
|
||||
parent.appendChild(rect);
|
||||
|
||||
return rect;
|
||||
}
|
||||
}]);
|
||||
|
||||
return SVGRenderer;
|
||||
}();
|
||||
|
||||
exports.default = SVGRenderer;
|
||||
Reference in New Issue
Block a user