Add barcode-detector-api-polyfill dependency and update environment variable declarations in SvelteKit project
This commit is contained in:
+47
@@ -0,0 +1,47 @@
|
||||
import { EncodeHintType } from '@zxing/library';
|
||||
declare abstract class BrowserCodeSvgWriter {
|
||||
/**
|
||||
* Default quiet zone in pixels.
|
||||
*/
|
||||
private static readonly QUIET_ZONE_SIZE;
|
||||
/**
|
||||
* SVG markup NameSpace
|
||||
*/
|
||||
private static readonly SVG_NS;
|
||||
/**
|
||||
* A HTML container element for the image.
|
||||
*/
|
||||
private containerElement;
|
||||
/**
|
||||
* Constructs. 😉
|
||||
*/
|
||||
constructor(containerElement: string | HTMLElement);
|
||||
/**
|
||||
* Writes the QR code to a SVG and renders it in the container.
|
||||
*/
|
||||
write(contents: string, width: number, height: number, hints?: Map<EncodeHintType, any>): SVGSVGElement;
|
||||
/**
|
||||
* Creates a SVG element.
|
||||
*/
|
||||
protected createSVGElement(w: number, h: number): SVGSVGElement;
|
||||
/**
|
||||
* Creates a SVG rect.
|
||||
*/
|
||||
protected createSvgPathPlaceholderElement(w: number, h: number): SVGPathElement;
|
||||
/**
|
||||
* Creates a SVG rect.
|
||||
*/
|
||||
protected createSvgRectElement(x: number, y: number, w: number, h: number): SVGRectElement;
|
||||
/**
|
||||
* Encodes the content to a Barcode type.
|
||||
*/
|
||||
private encode;
|
||||
/**
|
||||
* Renders the SVG in the container.
|
||||
*
|
||||
* @note the input matrix uses 0 == white, 1 == black. The output matrix
|
||||
* uses 0 == black, 255 == white (i.e. an 8 bit greyscale bitmap).
|
||||
*/
|
||||
private renderResult;
|
||||
}
|
||||
export { BrowserCodeSvgWriter };
|
||||
+132
@@ -0,0 +1,132 @@
|
||||
import { EncodeHintType, IllegalArgumentException, IllegalStateException, QRCodeDecoderErrorCorrectionLevel, QRCodeEncoder, } from '@zxing/library';
|
||||
var svgNs = 'http://www.w3.org/2000/svg';
|
||||
/**/
|
||||
var BrowserCodeSvgWriter = /** @class */ (function () {
|
||||
/**
|
||||
* Constructs. 😉
|
||||
*/
|
||||
function BrowserCodeSvgWriter(containerElement) {
|
||||
if (typeof containerElement === 'string') {
|
||||
var container = document.getElementById(containerElement);
|
||||
if (!container) {
|
||||
throw new Error("Could not find a Container element with '".concat(containerElement, "'."));
|
||||
}
|
||||
this.containerElement = container;
|
||||
}
|
||||
else {
|
||||
this.containerElement = containerElement;
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Writes the QR code to a SVG and renders it in the container.
|
||||
*/
|
||||
BrowserCodeSvgWriter.prototype.write = function (contents, width, height, hints) {
|
||||
if (contents.length === 0) {
|
||||
throw new IllegalArgumentException('Found empty contents');
|
||||
}
|
||||
if (width < 0 || height < 0) {
|
||||
throw new IllegalArgumentException('Requested dimensions are too small: ' + width + 'x' + height);
|
||||
}
|
||||
var quietZone = hints && hints.get(EncodeHintType.MARGIN) !== undefined
|
||||
? Number.parseInt(hints.get(EncodeHintType.MARGIN).toString(), 10)
|
||||
: BrowserCodeSvgWriter.QUIET_ZONE_SIZE;
|
||||
var code = this.encode(hints, contents);
|
||||
return this.renderResult(code, width, height, quietZone);
|
||||
};
|
||||
/**
|
||||
* Creates a SVG element.
|
||||
*/
|
||||
BrowserCodeSvgWriter.prototype.createSVGElement = function (w, h) {
|
||||
var el = document.createElementNS(BrowserCodeSvgWriter.SVG_NS, 'svg');
|
||||
el.setAttributeNS(svgNs, 'width', h.toString());
|
||||
el.setAttributeNS(svgNs, 'height', w.toString());
|
||||
return el;
|
||||
};
|
||||
/**
|
||||
* Creates a SVG rect.
|
||||
*/
|
||||
BrowserCodeSvgWriter.prototype.createSvgPathPlaceholderElement = function (w, h) {
|
||||
var el = document.createElementNS(BrowserCodeSvgWriter.SVG_NS, 'path');
|
||||
el.setAttributeNS(svgNs, 'd', "M0 0h".concat(w, "v").concat(h, "H0z"));
|
||||
el.setAttributeNS(svgNs, 'fill', 'none');
|
||||
return el;
|
||||
};
|
||||
/**
|
||||
* Creates a SVG rect.
|
||||
*/
|
||||
BrowserCodeSvgWriter.prototype.createSvgRectElement = function (x, y, w, h) {
|
||||
var el = document.createElementNS(BrowserCodeSvgWriter.SVG_NS, 'rect');
|
||||
el.setAttributeNS(svgNs, 'x', x.toString());
|
||||
el.setAttributeNS(svgNs, 'y', y.toString());
|
||||
el.setAttributeNS(svgNs, 'height', w.toString());
|
||||
el.setAttributeNS(svgNs, 'width', h.toString());
|
||||
el.setAttributeNS(svgNs, 'fill', '#000000');
|
||||
return el;
|
||||
};
|
||||
/**
|
||||
* Encodes the content to a Barcode type.
|
||||
*/
|
||||
BrowserCodeSvgWriter.prototype.encode = function (hints, contents) {
|
||||
var errorCorrectionLevel = QRCodeDecoderErrorCorrectionLevel.L;
|
||||
if (hints && hints.get(EncodeHintType.ERROR_CORRECTION) !== undefined) {
|
||||
var correctionStr = hints.get(EncodeHintType.ERROR_CORRECTION).toString();
|
||||
errorCorrectionLevel = QRCodeDecoderErrorCorrectionLevel.fromString(correctionStr);
|
||||
}
|
||||
var code = QRCodeEncoder.encode(contents, errorCorrectionLevel, hints);
|
||||
return code;
|
||||
};
|
||||
/**
|
||||
* Renders the SVG in the container.
|
||||
*
|
||||
* @note the input matrix uses 0 == white, 1 == black. The output matrix
|
||||
* uses 0 == black, 255 == white (i.e. an 8 bit greyscale bitmap).
|
||||
*/
|
||||
BrowserCodeSvgWriter.prototype.renderResult = function (code, width /*int*/, height /*int*/, quietZone /*int*/) {
|
||||
// if (this.format && format != this.format) {
|
||||
// throw new IllegalArgumentException("Can only encode QR_CODE, but got " + format)
|
||||
// }
|
||||
var input = code.getMatrix();
|
||||
if (input === null) {
|
||||
throw new IllegalStateException();
|
||||
}
|
||||
var inputWidth = input.getWidth();
|
||||
var inputHeight = input.getHeight();
|
||||
var qrWidth = inputWidth + (quietZone * 2);
|
||||
var qrHeight = inputHeight + (quietZone * 2);
|
||||
var outputWidth = Math.max(width, qrWidth);
|
||||
var outputHeight = Math.max(height, qrHeight);
|
||||
var multiple = Math.min(Math.floor(outputWidth / qrWidth), Math.floor(outputHeight / qrHeight));
|
||||
// Padding includes both the quiet zone and the extra white pixels to accommodate the requested
|
||||
// dimensions. For example, if input is 25x25 the QR will be 33x33 including the quiet zone.
|
||||
// If the requested size is 200x160, the multiple will be 4, for a QR of 132x132. These will
|
||||
// handle all the padding from 100x100 (the actual QR) up to 200x160.
|
||||
var leftPadding = Math.floor((outputWidth - (inputWidth * multiple)) / 2);
|
||||
var topPadding = Math.floor((outputHeight - (inputHeight * multiple)) / 2);
|
||||
var svgElement = this.createSVGElement(outputWidth, outputHeight);
|
||||
var placeholder = this.createSvgPathPlaceholderElement(width, height);
|
||||
svgElement.appendChild(placeholder);
|
||||
this.containerElement.appendChild(svgElement);
|
||||
// 2D loop
|
||||
for (var inputY = 0, outputY = topPadding; inputY < inputHeight; inputY++, outputY += multiple) {
|
||||
// Write the contents of this row of the barcode
|
||||
for (var inputX = 0, outputX = leftPadding; inputX < inputWidth; inputX++, outputX += multiple) {
|
||||
if (input.get(inputX, inputY) === 1) {
|
||||
var svgRectElement = this.createSvgRectElement(outputX, outputY, multiple, multiple);
|
||||
svgElement.appendChild(svgRectElement);
|
||||
}
|
||||
}
|
||||
}
|
||||
return svgElement;
|
||||
};
|
||||
/**
|
||||
* Default quiet zone in pixels.
|
||||
*/
|
||||
BrowserCodeSvgWriter.QUIET_ZONE_SIZE = 4;
|
||||
/**
|
||||
* SVG markup NameSpace
|
||||
*/
|
||||
BrowserCodeSvgWriter.SVG_NS = 'http://www.w3.org/2000/svg';
|
||||
return BrowserCodeSvgWriter;
|
||||
}());
|
||||
export { BrowserCodeSvgWriter };
|
||||
//# sourceMappingURL=BrowserCodeSvgWriter.js.map
|
||||
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"BrowserCodeSvgWriter.js","sourceRoot":"","sources":["../../../src/writers/BrowserCodeSvgWriter.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,cAAc,EACd,wBAAwB,EACxB,qBAAqB,EACrB,iCAAiC,EACjC,aAAa,GAEd,MAAM,gBAAgB,C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,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,KAAK,CAAC,EAAE;oBACnC,IAAM,cAAc,GAAG,IAAI,CAAC,oBAAoB,CAAC,OAAO,EAAE,OAAO,EAAE,QAAQ,EAAE,QAAQ,CAAC,CAAC;oBACvF,UAAU,CAAC,WAAW,CAAC,cAAc,CAAC,CAAC;iBACxC;aACF;SACF;QAED,OAAO,UAAU,CAAC;IACpB,CAAC;IA7KD;;OAEG;IACqB,oCAAe,GAAG,CAAC,CAAC;IAE5C;;OAEG;IACqB,2BAAM,GAAG,4BAA4B,CAAC;IAsKhE,2BAAC;CAAA,AAhLD,IAgLC;AAED,OAAO,EAAE,oBAAoB,EAAE,CAAC"}
|
||||
+39
@@ -0,0 +1,39 @@
|
||||
import { EncodeHintType } from '@zxing/library';
|
||||
declare class BrowserQRCodeSvgWriter {
|
||||
private static readonly QUIET_ZONE_SIZE;
|
||||
/**
|
||||
* Writes and renders a QRCode SVG element.
|
||||
*
|
||||
* @param contents
|
||||
* @param width
|
||||
* @param height
|
||||
* @param hints
|
||||
*/
|
||||
write(contents: string, width: number, height: number, hints?: Map<EncodeHintType, any>): SVGSVGElement;
|
||||
/**
|
||||
* Renders the result and then appends it to the DOM.
|
||||
*/
|
||||
writeToDom(containerElement: string | HTMLElement, contents: string, width: number, height: number, hints?: Map<EncodeHintType, any>): void;
|
||||
/**
|
||||
* Note that the input matrix uses 0 == white, 1 == black.
|
||||
* The output matrix uses 0 == black, 255 == white (i.e. an 8 bit greyscale bitmap).
|
||||
*/
|
||||
private renderResult;
|
||||
/**
|
||||
* Creates a SVG element.
|
||||
*
|
||||
* @param w SVG's width attribute
|
||||
* @param h SVG's height attribute
|
||||
*/
|
||||
private createSVGElement;
|
||||
/**
|
||||
* Creates a SVG rect element.
|
||||
*
|
||||
* @param x Element's x coordinate
|
||||
* @param y Element's y coordinate
|
||||
* @param w Element's width attribute
|
||||
* @param h Element's height attribute
|
||||
*/
|
||||
private createSvgRectElement;
|
||||
}
|
||||
export { BrowserQRCodeSvgWriter };
|
||||
+125
@@ -0,0 +1,125 @@
|
||||
import { EncodeHintType, IllegalArgumentException, IllegalStateException, QRCodeDecoderErrorCorrectionLevel, QRCodeEncoder, } from '@zxing/library';
|
||||
var svgNs = 'http://www.w3.org/2000/svg';
|
||||
/**/
|
||||
var BrowserQRCodeSvgWriter = /** @class */ (function () {
|
||||
function BrowserQRCodeSvgWriter() {
|
||||
}
|
||||
/**
|
||||
* Writes and renders a QRCode SVG element.
|
||||
*
|
||||
* @param contents
|
||||
* @param width
|
||||
* @param height
|
||||
* @param hints
|
||||
*/
|
||||
BrowserQRCodeSvgWriter.prototype.write = function (contents, width, height, hints) {
|
||||
if (contents.length === 0) {
|
||||
throw new IllegalArgumentException('Found empty contents');
|
||||
}
|
||||
// if (format != BarcodeFormat.QR_CODE) {
|
||||
// throw new IllegalArgumentException("Can only encode QR_CODE, but got " + format)
|
||||
// }
|
||||
if (width < 0 || height < 0) {
|
||||
throw new IllegalArgumentException('Requested dimensions are too small: ' + width + 'x' + height);
|
||||
}
|
||||
var errorCorrectionLevel = QRCodeDecoderErrorCorrectionLevel.L;
|
||||
var quietZone = BrowserQRCodeSvgWriter.QUIET_ZONE_SIZE;
|
||||
if (hints) {
|
||||
if (undefined !== hints.get(EncodeHintType.ERROR_CORRECTION)) {
|
||||
var correctionStr = hints.get(EncodeHintType.ERROR_CORRECTION).toString();
|
||||
errorCorrectionLevel = QRCodeDecoderErrorCorrectionLevel.fromString(correctionStr);
|
||||
}
|
||||
if (undefined !== hints.get(EncodeHintType.MARGIN)) {
|
||||
quietZone = Number.parseInt(hints.get(EncodeHintType.MARGIN).toString(), 10);
|
||||
}
|
||||
}
|
||||
var code = QRCodeEncoder.encode(contents, errorCorrectionLevel, hints);
|
||||
return this.renderResult(code, width, height, quietZone);
|
||||
};
|
||||
/**
|
||||
* Renders the result and then appends it to the DOM.
|
||||
*/
|
||||
BrowserQRCodeSvgWriter.prototype.writeToDom = function (containerElement, contents, width, height, hints) {
|
||||
if (typeof containerElement === 'string') {
|
||||
var targetEl = document.querySelector(containerElement);
|
||||
if (!targetEl) {
|
||||
throw new Error('Could no find the target HTML element.');
|
||||
}
|
||||
containerElement = targetEl;
|
||||
}
|
||||
var svgElement = this.write(contents, width, height, hints);
|
||||
if (containerElement instanceof HTMLElement) {
|
||||
containerElement.appendChild(svgElement);
|
||||
}
|
||||
};
|
||||
/**
|
||||
* Note that the input matrix uses 0 == white, 1 == black.
|
||||
* The output matrix uses 0 == black, 255 == white (i.e. an 8 bit greyscale bitmap).
|
||||
*/
|
||||
BrowserQRCodeSvgWriter.prototype.renderResult = function (code, width /*int*/, height /*int*/, quietZone /*int*/) {
|
||||
var input = code.getMatrix();
|
||||
if (input === null) {
|
||||
throw new IllegalStateException();
|
||||
}
|
||||
var inputWidth = input.getWidth();
|
||||
var inputHeight = input.getHeight();
|
||||
var qrWidth = inputWidth + (quietZone * 2);
|
||||
var qrHeight = inputHeight + (quietZone * 2);
|
||||
var outputWidth = Math.max(width, qrWidth);
|
||||
var outputHeight = Math.max(height, qrHeight);
|
||||
var multiple = Math.min(Math.floor(outputWidth / qrWidth), Math.floor(outputHeight / qrHeight));
|
||||
// Padding includes both the quiet zone and the extra white pixels to accommodate the requested
|
||||
// dimensions. For example, if input is 25x25 the QR will be 33x33 including the quiet zone.
|
||||
// If the requested size is 200x160, the multiple will be 4, for a QR of 132x132. These will
|
||||
// handle all the padding from 100x100 (the actual QR) up to 200x160.
|
||||
var leftPadding = Math.floor((outputWidth - (inputWidth * multiple)) / 2);
|
||||
var topPadding = Math.floor((outputHeight - (inputHeight * multiple)) / 2);
|
||||
var svgElement = this.createSVGElement(outputWidth, outputHeight);
|
||||
for (var inputY = 0, outputY = topPadding; inputY < inputHeight; inputY++, outputY += multiple) {
|
||||
// Write the contents of this row of the barcode
|
||||
for (var inputX = 0, outputX = leftPadding; inputX < inputWidth; inputX++, outputX += multiple) {
|
||||
if (input.get(inputX, inputY) === 1) {
|
||||
var svgRectElement = this.createSvgRectElement(outputX, outputY, multiple, multiple);
|
||||
svgElement.appendChild(svgRectElement);
|
||||
}
|
||||
}
|
||||
}
|
||||
return svgElement;
|
||||
};
|
||||
/**
|
||||
* Creates a SVG element.
|
||||
*
|
||||
* @param w SVG's width attribute
|
||||
* @param h SVG's height attribute
|
||||
*/
|
||||
BrowserQRCodeSvgWriter.prototype.createSVGElement = function (w, h) {
|
||||
var svgElement = document.createElementNS(svgNs, 'svg');
|
||||
var width = w.toString();
|
||||
var height = h.toString();
|
||||
svgElement.setAttribute('height', height);
|
||||
svgElement.setAttribute('width', width);
|
||||
svgElement.setAttribute('viewBox', "0 0 " + width + " " + height);
|
||||
return svgElement;
|
||||
};
|
||||
/**
|
||||
* Creates a SVG rect element.
|
||||
*
|
||||
* @param x Element's x coordinate
|
||||
* @param y Element's y coordinate
|
||||
* @param w Element's width attribute
|
||||
* @param h Element's height attribute
|
||||
*/
|
||||
BrowserQRCodeSvgWriter.prototype.createSvgRectElement = function (x, y, w, h) {
|
||||
var rect = document.createElementNS(svgNs, 'rect');
|
||||
rect.setAttribute('x', x.toString());
|
||||
rect.setAttribute('y', y.toString());
|
||||
rect.setAttribute('height', w.toString());
|
||||
rect.setAttribute('width', h.toString());
|
||||
rect.setAttribute('fill', '#000000');
|
||||
return rect;
|
||||
};
|
||||
BrowserQRCodeSvgWriter.QUIET_ZONE_SIZE = 4;
|
||||
return BrowserQRCodeSvgWriter;
|
||||
}());
|
||||
export { BrowserQRCodeSvgWriter };
|
||||
//# sourceMappingURL=BrowserQRCodeSvgWriter.js.map
|
||||
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"BrowserQRCodeSvgWriter.js","sourceRoot":"","sources":["../../../src/writers/BrowserQRCodeSvgWriter.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,cAAc,EACd,wBAAwB,EACxB,qBAAqB,EACrB,iCAAiC,EACjC,aAAa,GAEd,MAAM,gBAAgB,CAAC;AAExB,IAAM,KAAK,GAAG,4BAA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