UNPKG

@adrianso/react-native-barcode-builder

Version:
133 lines (132 loc) 5.99 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __rest = (this && this.__rest) || function (s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.barcodeToSvg = void 0; // @flow var react_1 = __importStar(require("react")); var react_native_1 = require("react-native"); var react_native_svg_1 = __importStar(require("react-native-svg")); var barcodes_1 = __importDefault(require("jsbarcode/src/barcodes")); // encode() handles the Encoder call and builds the binary string to be rendered var encode = function (text, Encoder, options) { // If text is not a non-empty string, throw error. if (typeof text !== 'string' || text.length === 0) { throw new Error('Barcode value must be a non-empty string'); } var encoder; try { encoder = new Encoder(text, options); } catch (error) { // If the encoder could not be instantiated, throw error. throw new Error('Invalid barcode format.'); } // If the input is not valid for the encoder, throw error. if (!encoder.valid()) { throw new Error('Invalid barcode for selected format.'); } // Make a request for the binary data (and other infromation) that should be rendered // encoded stucture is { // text: 'xxxxx', // data: '110100100001....' // } var encoded = encoder.encode(); return encoded; }; var drawSvgBarCode = function (encoding) { var rects = []; // binary data of barcode var binary = encoding.data; var barWidth = 0; var x = 0; var yFrom = 0; for (var b = 0; b < binary.length; b++) { x = b; if (binary[b] === '1') { barWidth++; } else if (barWidth > 0) { rects[rects.length] = drawRect(x - barWidth, yFrom, barWidth, 100); barWidth = 0; } } // Last draw is needed since the barcode ends with 1 if (barWidth > 0) { rects[rects.length] = drawRect(x - barWidth + 1, yFrom, barWidth, 100); } return rects; }; var drawRect = function (x, y, width, height) { return "M".concat(x, ",").concat(y, "h").concat(width, "v").concat(height, "h-").concat(width, "z"); }; var barcodeToSvg = function (options) { var _a = options.format, format = _a === void 0 ? 'CODE128' : _a, _b = options.lineColor, lineColor = _b === void 0 ? '#000000' : _b, value = options.value, _c = options.width, width = _c === void 0 ? 100 : _c, _d = options.height, height = _d === void 0 ? 100 : _d; var encoder = barcodes_1.default[format]; var encoded = encode(value, encoder, options); var bars = drawSvgBarCode(encoded); var barCodeWidth = encoded.data.length; return "\n <svg xmlns=\"http://www.w3.org/2000/svg\" width=\"".concat(width, "\" height=\"").concat(height, "\" viewBox=\"0 0 ").concat(barCodeWidth, " 100\" preserveAspectRatio=\"xMinYMin slice\">\n <path d=\"").concat(bars.join(' '), "\" fill=\"").concat(lineColor, "\"/>\n </svg>\n "); }; exports.barcodeToSvg = barcodeToSvg; var Barcode = function (props) { var _a = props.format, format = _a === void 0 ? 'CODE128' : _a, _b = props.lineColor, lineColor = _b === void 0 ? '#000000' : _b, _c = props.flat, flat = _c === void 0 ? false : _c, value = props.value, width = props.width, height = props.height, viewProps = __rest(props, ["format", "lineColor", "flat", "value", "width", "height"]); var _d = (0, react_1.useState)([]), bars = _d[0], setBars = _d[1]; var _e = (0, react_1.useState)(0), barCodeWidth = _e[0], setBarCodeWidth = _e[1]; (0, react_1.useEffect)(function () { var encoder = barcodes_1.default[format]; var encoded = encode(value, encoder, { format: format, lineColor: lineColor, flat: flat, value: value, width: width, height: height, }); if (encoded) { setBars(drawSvgBarCode(encoded)); setBarCodeWidth(encoded.data.length); } }, [format, value, lineColor, flat, width, height]); return (<react_native_1.View {...viewProps}> <react_native_svg_1.default height="100%" width="100%" viewBox={"0 0 ".concat(barCodeWidth, " 100")} preserveAspectRatio="xMinYMin slice" fill={lineColor}> <react_native_svg_1.Path d={bars.join(' ')}/> </react_native_svg_1.default> </react_native_1.View>); }; exports.default = Barcode;