UNPKG

red-agate-barcode

Version:

red-agate barcode tag library.

68 lines 2.59 kB
// Copyright (c) 2017, Shellyl_N and Authors // license: ISC // https://github.com/shellyln import { barcodeBasePropsDefault, BarcodeBase } from './BarcodeBase'; import { charactersMap, reverseMap, fullAsciiMap } from './data/Code39.data'; export const code39PropsDefault = Object.assign({}, barcodeBasePropsDefault, { addCheckDigit: true, fullAscii: false, narrowWidth: 0.33, wideWidth: 0.66, charGapWidth: void 0 }); export class Code39 extends BarcodeBase { constructor(props) { super(Object.assign({}, code39PropsDefault, props), charactersMap); } calcSymbolSize(data, startChar, stopChar, cdChar) { const props = this.props; const gw = props.charGapWidth || props.narrowWidth; // module width (bar + space + gap) const mw = props.narrowWidth * 6 + props.wideWidth * 3 + gw; return { // total width (quiet + data + start + stop + cd) tw: props.quietWidth * 2 + mw * (data.length + 2 + (props.addCheckDigit ? 1 : 0)) - gw, // total height (quiet + bar + text) th: props.quietHeight * 2 + props.height + (props.drawText ? props.textHeight : 0) }; } calcCheckDigit(data) { let cdChar = ""; if (this.props.addCheckDigit) { let cd = 0; for (let i = 0; i < data.length; i++) { const v = charactersMap.get(data[i]); if (v === void 0) { throw new Error("code39: character is out of range."); } cd = (cd + v.index) % 43; } const cdch = reverseMap.get(cd); if (cdch === void 0) { throw new Error("code39 (trace): checkdigit is out of range."); } cdChar = cdch; } return cdChar; } encodeData(data) { let d = data; if (this.props.fullAscii) { d = ""; for (let i = 0; i < data.length; i++) { const c = fullAsciiMap.get(data.charCodeAt(i)); if (c === void 0) { throw new Error("code39 fullascii: character is out of range."); } d += c; } } return { data: d, startChar: "*", stopChar: "*" }; } getBarSpaceWidth() { const props = this.props; const gw = props.charGapWidth || props.narrowWidth; return [gw, props.narrowWidth, props.wideWidth]; } } //# sourceMappingURL=Code39.js.map