UNPKG

red-agate-barcode

Version:

red-agate barcode tag library.

60 lines 2.27 kB
// Copyright (c) 2017, Shellyl_N and Authors // license: ISC // https://github.com/shellyln import { barcodeBasePropsDefault, BarcodeBase } from './BarcodeBase'; import { charactersMap } from './data/Itf.data'; export const itfPropsDefault = Object.assign({}, barcodeBasePropsDefault, { addCheckDigit: true, narrowWidth: 0.33, wideWidth: 0.66 }); export class Itf extends BarcodeBase { constructor(props) { super(Object.assign({}, itfPropsDefault, props), charactersMap); } calcSymbolSize(data, startChar, stopChar, cdChar) { const props = this.props; // module width (bar + space + gap) const mw = props.narrowWidth * 3 + props.wideWidth * 2; return { // total width (quiet + data + start + stop + cd) tw: props.quietWidth * 2 + (props.narrowWidth * 8) + mw * data.length * 2, // total height (quiet + bar + text) th: props.quietHeight * 2 + props.height + (props.drawText ? props.textHeight : 0) }; } calcItfMod10W3Checksum(data) { let odd = 0, even = 0; for (let i = 0; i < data.length; i++) { // most right -> odd if ((i + 1) % 2) { odd = (odd + Number.parseInt(data[data.length - 1 - i], 10)) % 10; } else { even = (even + Number.parseInt(data[data.length - 1 - i], 10)) % 10; } } return String((10 - ((odd * 3 + even) % 10)) % 10); } encodeData(data) { let labelText = data; if (this.props.addCheckDigit) { const cd = this.calcItfMod10W3Checksum(data); labelText += cd; } let d = ""; if (labelText.length % 2) { throw new Error("bad data length"); } for (let i = 0; i < labelText.length; i += 2) { const c = Number.parseInt(labelText.slice(i, i + 2), 10); d += String.fromCharCode(c); } return { data: d, labelText, startChar: "\x64", stopChar: "\x65" }; } getBarSpaceWidth() { const props = this.props; return [0, props.narrowWidth, props.wideWidth]; } } //# sourceMappingURL=Itf.js.map