1
This commit is contained in:
@@ -0,0 +1,137 @@
|
||||
import merge from "../help/merge.js";
|
||||
import {calculateEncodingAttributes, getTotalWidthOfEncodings, getMaximumHeightOfEncodings} from "./shared.js";
|
||||
|
||||
class CanvasRenderer{
|
||||
constructor(canvas, encodings, options){
|
||||
this.canvas = canvas;
|
||||
this.encodings = encodings;
|
||||
this.options = options;
|
||||
}
|
||||
|
||||
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(let i = 0; i < this.encodings.length; i++){
|
||||
var encodingOptions = merge(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();
|
||||
}
|
||||
|
||||
prepareCanvas(){
|
||||
// Get the canvas context
|
||||
var ctx = this.canvas.getContext("2d");
|
||||
|
||||
ctx.save();
|
||||
|
||||
calculateEncodingAttributes(this.encodings, this.options, ctx);
|
||||
var totalWidth = getTotalWidthOfEncodings(this.encodings);
|
||||
var maxHeight = 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);
|
||||
}
|
||||
|
||||
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]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
moveCanvasDrawing(encoding){
|
||||
var ctx = this.canvas.getContext("2d");
|
||||
|
||||
ctx.translate(encoding.width, 0);
|
||||
}
|
||||
|
||||
restoreCanvas(){
|
||||
// Get the canvas context
|
||||
var ctx = this.canvas.getContext("2d");
|
||||
|
||||
ctx.restore();
|
||||
}
|
||||
}
|
||||
|
||||
export default CanvasRenderer;
|
||||
@@ -0,0 +1,5 @@
|
||||
import CanvasRenderer from './canvas.js';
|
||||
import SVGRenderer from './svg.js';
|
||||
import ObjectRenderer from './object.js';
|
||||
|
||||
export default {CanvasRenderer, SVGRenderer, ObjectRenderer};
|
||||
@@ -0,0 +1,14 @@
|
||||
class ObjectRenderer {
|
||||
constructor(object, encodings, options) {
|
||||
this.object = object;
|
||||
this.encodings = encodings;
|
||||
this.options = options;
|
||||
}
|
||||
|
||||
render() {
|
||||
this.object.encodings = this.encodings;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
export default ObjectRenderer;
|
||||
@@ -0,0 +1,94 @@
|
||||
import merge from "../help/merge.js";
|
||||
|
||||
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(let i = 0; i < encodings.length; i++){
|
||||
var encoding = encodings[i];
|
||||
var options = merge(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(let i = 0; i < encodings.length; i++){
|
||||
totalWidth += encodings[i].width;
|
||||
}
|
||||
return totalWidth;
|
||||
}
|
||||
|
||||
function getMaximumHeightOfEncodings(encodings){
|
||||
var maxHeight = 0;
|
||||
for(let 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;
|
||||
}
|
||||
|
||||
export {getMaximumHeightOfEncodings, getEncodingHeight, getBarcodePadding, calculateEncodingAttributes, getTotalWidthOfEncodings};
|
||||
@@ -0,0 +1,171 @@
|
||||
import merge from "../help/merge.js";
|
||||
import {calculateEncodingAttributes, getTotalWidthOfEncodings, getMaximumHeightOfEncodings} from "./shared.js";
|
||||
|
||||
var svgns = "http://www.w3.org/2000/svg";
|
||||
|
||||
class SVGRenderer{
|
||||
constructor(svg, encodings, options){
|
||||
this.svg = svg;
|
||||
this.encodings = encodings;
|
||||
this.options = options;
|
||||
this.document = options.xmlDocument || document;
|
||||
}
|
||||
|
||||
render(){
|
||||
var currentX = this.options.marginLeft;
|
||||
|
||||
this.prepareSVG();
|
||||
for(let i = 0; i < this.encodings.length; i++){
|
||||
var encoding = this.encodings[i];
|
||||
var encodingOptions = merge(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;
|
||||
}
|
||||
}
|
||||
|
||||
prepareSVG(){
|
||||
// Clear the SVG
|
||||
while (this.svg.firstChild) {
|
||||
this.svg.removeChild(this.svg.firstChild);
|
||||
}
|
||||
|
||||
calculateEncodingAttributes(this.encodings, this.options);
|
||||
var totalWidth = getTotalWidthOfEncodings(this.encodings);
|
||||
var maxHeight = 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 + ";"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
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)");
|
||||
}
|
||||
|
||||
createGroup(x, y, parent){
|
||||
var group = this.document.createElementNS(svgns, 'g');
|
||||
group.setAttribute("transform", "translate(" + x + ", " + y + ")");
|
||||
|
||||
parent.appendChild(group);
|
||||
|
||||
return group;
|
||||
}
|
||||
|
||||
setGroupOptions(group, options){
|
||||
group.setAttribute("style",
|
||||
"fill:" + options.lineColor + ";"
|
||||
);
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
export default SVGRenderer;
|
||||
Reference in New Issue
Block a user