From 172594198397b0d93f8ebcb75bef7157a7e170a7 Mon Sep 17 00:00:00 2001 From: Eric de Boer Date: Mon, 8 Apr 2024 21:40:53 +0200 Subject: [PATCH 01/10] Add EAN & UPC --- apps/cards/EAN.js | 77 +++++++++++++++++ apps/cards/EAN13.js | 92 ++++++++++++++++++++ apps/cards/EAN2.js | 30 +++++++ apps/cards/EAN5.js | 40 +++++++++ apps/cards/EAN8.js | 57 +++++++++++++ apps/cards/UPC.js | 132 +++++++++++++++++++++++++++++ apps/cards/UPCE.js | 177 +++++++++++++++++++++++++++++++++++++++ apps/cards/app.js | 54 ++++++++++++ apps/cards/constants.js | 42 ++++++++++ apps/cards/encode.js | 43 ++++++++++ apps/cards/metadata.json | 9 ++ 11 files changed, 753 insertions(+) create mode 100644 apps/cards/EAN.js create mode 100644 apps/cards/EAN13.js create mode 100644 apps/cards/EAN2.js create mode 100644 apps/cards/EAN5.js create mode 100644 apps/cards/EAN8.js create mode 100644 apps/cards/UPC.js create mode 100644 apps/cards/UPCE.js create mode 100644 apps/cards/constants.js create mode 100644 apps/cards/encode.js diff --git a/apps/cards/EAN.js b/apps/cards/EAN.js new file mode 100644 index 000000000..bb6a4d038 --- /dev/null +++ b/apps/cards/EAN.js @@ -0,0 +1,77 @@ +//import { SIDE_BIN, MIDDLE_BIN } from './constants'; + +const Barcode = require("cards.Barcode.js"); +const encode = require("cards.encode.js"); + +// Base class for EAN8 & EAN13 +class EAN extends Barcode { + + constructor(data, options) { + super(data, options); + + // Make sure the font is not bigger than the space between the guard bars + this.fontSize = !options.flat && options.fontSize > options.width * 10 + ? options.width * 10 + : options.fontSize; + + // Make the guard bars go down half the way of the text + this.guardHeight = options.height + this.fontSize / 2 + options.textMargin; + } + + encode() { + return this.options.flat + ? this.encodeFlat() + : this.encodeGuarded(); + } + + leftText(from, to) { + return this.text.substr(from, to); + } + + leftEncode(data, structure) { + return encode(data, structure); + } + + rightText(from, to) { + return this.text.substr(from, to); + } + + rightEncode(data, structure) { + return encode(data, structure); + } + + encodeGuarded() { + const textOptions = { fontSize: this.fontSize }; + const guardOptions = { height: this.guardHeight }; + const SIDE_BIN = '101'; + const MIDDLE_BIN = '01010'; + + return [ + { data: SIDE_BIN, options: guardOptions }, + { data: this.leftEncode(), text: this.leftText(), options: textOptions }, + { data: MIDDLE_BIN, options: guardOptions }, + { data: this.rightEncode(), text: this.rightText(), options: textOptions }, + { data: SIDE_BIN, options: guardOptions }, + ]; + } + + encodeFlat() { + const SIDE_BIN = '101'; + const MIDDLE_BIN = '01010'; + const data = [ + SIDE_BIN, + this.leftEncode(), + MIDDLE_BIN, + this.rightEncode(), + SIDE_BIN + ]; + + return { + data: data.join(''), + text: this.text + }; + } + +} + +module.exports = EAN; \ No newline at end of file diff --git a/apps/cards/EAN13.js b/apps/cards/EAN13.js new file mode 100644 index 000000000..3a914ebd0 --- /dev/null +++ b/apps/cards/EAN13.js @@ -0,0 +1,92 @@ +// Encoding documentation: +// https://en.wikipedia.org/wiki/International_Article_Number_(EAN)#Binary_encoding_of_data_digits_into_EAN-13_barcode + +import { EAN13_STRUCTURE } from './constants'; + +const EAN = require("cards.EAN.js"); + + +// Calculate the checksum digit +// https://en.wikipedia.org/wiki/International_Article_Number_(EAN)#Calculation_of_checksum_digit +const checksum = (number) => { + const res = number + .substr(0, 12) + .split('') + .map((n) => +n) + .reduce((sum, a, idx) => ( + idx % 2 ? sum + a * 3 : sum + a + ), 0); + + return (10 - (res % 10)) % 10; +}; + +class EAN13 extends EAN { + + constructor(data, options) { + // Add checksum if it does not exist + if (data.search(/^[0-9]{12}$/) !== -1) { + data += checksum(data); + } + + super(data, options); + + // Adds a last character to the end of the barcode + this.lastChar = options.lastChar; + } + + valid() { + return ( + this.data.search(/^[0-9]{13}$/) !== -1 && + +this.data[12] === checksum(this.data) + ); + } + + leftText() { + return super.leftText(1, 6); + } + + leftEncode() { + const data = this.data.substr(1, 6); + const structure = EAN13_STRUCTURE[this.data[0]]; + return super.leftEncode(data, structure); + } + + rightText() { + return super.rightText(7, 6); + } + + rightEncode() { + const data = this.data.substr(7, 6); + return super.rightEncode(data, 'RRRRRR'); + } + + // The "standard" way of printing EAN13 barcodes with guard bars + encodeGuarded() { + const data = super.encodeGuarded(); + + // Extend data with left digit & last character + if (this.options.displayValue) { + data.unshift({ + data: '000000000000', + text: this.text.substr(0, 1), + options: { textAlign: 'left', fontSize: this.fontSize } + }); + + if (this.options.lastChar) { + data.push({ + data: '00' + }); + data.push({ + data: '00000', + text: this.options.lastChar, + options: { fontSize: this.fontSize } + }); + } + } + + return data; + } + +} + +module.exports = EAN13; \ No newline at end of file diff --git a/apps/cards/EAN2.js b/apps/cards/EAN2.js new file mode 100644 index 000000000..c7ceb059b --- /dev/null +++ b/apps/cards/EAN2.js @@ -0,0 +1,30 @@ +// Encoding documentation: +// https://en.wikipedia.org/wiki/EAN_2#Encoding + +const constants = require("cards.constants.js"); +const encode = require("cards.encode.js"); +const Barcode = require("cards.Barcode.js"); + +class EAN2 extends Barcode { + + constructor(data, options) { + super(data, options); + } + + valid() { + return this.data.search(/^[0-9]{2}$/) !== -1; + } + + encode(){ + // Choose the structure based on the number mod 4 + const structure = constants.EAN2_STRUCTURE[parseInt(this.data) % 4]; + return { + // Start bits + Encode the two digits with 01 in between + data: '1011' + encode(this.data, structure, '01'), + text: this.text + }; + } + +} + +module.exports = EAN2; diff --git a/apps/cards/EAN5.js b/apps/cards/EAN5.js new file mode 100644 index 000000000..77217cd95 --- /dev/null +++ b/apps/cards/EAN5.js @@ -0,0 +1,40 @@ +// Encoding documentation: +// https://en.wikipedia.org/wiki/EAN_5#Encoding + +const constants = require("cards.constants.js"); +const encode = require("cards.encode.js"); +const Barcode = require("cards.Barcode.js"); + +const checksum = (data) => { + const result = data + .split('') + .map(n => +n) + .reduce((sum, a, idx) => { + return idx % 2 + ? sum + a * 9 + : sum + a * 3; + }, 0); + return result % 10; +}; + +class EAN5 extends Barcode { + + constructor(data, options) { + super(data, options); + } + + valid() { + return this.data.search(/^[0-9]{5}$/) !== -1; + } + + encode() { + const structure = constants.EAN5_STRUCTURE[checksum(this.data)]; + return { + data: '1011' + encode(this.data, structure, '01'), + text: this.text + }; + } + +} + +module.exports = EAN5; diff --git a/apps/cards/EAN8.js b/apps/cards/EAN8.js new file mode 100644 index 000000000..7ffb41c4c --- /dev/null +++ b/apps/cards/EAN8.js @@ -0,0 +1,57 @@ +// Encoding documentation: +// http://www.barcodeisland.com/ean8.phtml + +const EAN = require("cards.EAN.js"); + +// Calculate the checksum digit +const checksum = (number) => { + const res = number + .substr(0, 7) + .split('') + .map((n) => +n) + .reduce((sum, a, idx) => ( + idx % 2 ? sum + a : sum + a * 3 + ), 0); + + return (10 - (res % 10)) % 10; +}; + +class EAN8 extends EAN { + + constructor(data, options) { + // Add checksum if it does not exist + if (data.search(/^[0-9]{7}$/) !== -1) { + data += checksum(data); + } + + super(data, options); + } + + valid() { + return ( + this.data.search(/^[0-9]{8}$/) !== -1 && + +this.data[7] === checksum(this.data) + ); + } + + leftText() { + return super.leftText(0, 4); + } + + leftEncode() { + const data = this.data.substr(0, 4); + return super.leftEncode(data, 'LLLL'); + } + + rightText() { + return super.rightText(4, 4); + } + + rightEncode() { + const data = this.data.substr(4, 4); + return super.rightEncode(data, 'RRRR'); + } + +} + +module.exports = EAN8; diff --git a/apps/cards/UPC.js b/apps/cards/UPC.js new file mode 100644 index 000000000..3344a0785 --- /dev/null +++ b/apps/cards/UPC.js @@ -0,0 +1,132 @@ +// Encoding documentation: +// https://en.wikipedia.org/wiki/Universal_Product_Code#Encoding + +const encode = require("cards.encode.js"); +const Barcode = require("cards.Barcode.js"); + +class UPC extends Barcode{ + constructor(data, options){ + // Add checksum if it does not exist + if(data.search(/^[0-9]{11}$/) !== -1){ + data += checksum(data); + } + + super(data, options); + + this.displayValue = options.displayValue; + + // Make sure the font is not bigger than the space between the guard bars + if(options.fontSize > options.width * 10){ + this.fontSize = options.width * 10; + } + else{ + this.fontSize = options.fontSize; + } + + // Make the guard bars go down half the way of the text + this.guardHeight = options.height + this.fontSize / 2 + options.textMargin; + } + + valid(){ + return this.data.search(/^[0-9]{12}$/) !== -1 && + this.data[11] == checksum(this.data); + } + + encode(){ + if(this.options.flat){ + return this.flatEncoding(); + } + else{ + return this.guardedEncoding(); + } + } + + flatEncoding(){ + var result = ""; + + result += "101"; + result += encode(this.data.substr(0, 6), "LLLLLL"); + result += "01010"; + result += encode(this.data.substr(6, 6), "RRRRRR"); + result += "101"; + + return { + data: result, + text: this.text + }; + } + + guardedEncoding(){ + var result = []; + + // Add the first digit + if(this.displayValue){ + result.push({ + data: "00000000", + text: this.text.substr(0, 1), + options: {textAlign: "left", fontSize: this.fontSize} + }); + } + + // Add the guard bars + result.push({ + data: "101" + encode(this.data[0], "L"), + options: {height: this.guardHeight} + }); + + // Add the left side + result.push({ + data: encode(this.data.substr(1, 5), "LLLLL"), + text: this.text.substr(1, 5), + options: {fontSize: this.fontSize} + }); + + // Add the middle bits + result.push({ + data: "01010", + options: {height: this.guardHeight} + }); + + // Add the right side + result.push({ + data: encode(this.data.substr(6, 5), "RRRRR"), + text: this.text.substr(6, 5), + options: {fontSize: this.fontSize} + }); + + // Add the end bits + result.push({ + data: encode(this.data[11], "R") + "101", + options: {height: this.guardHeight} + }); + + // Add the last digit + if(this.displayValue){ + result.push({ + data: "00000000", + text: this.text.substr(11, 1), + options: {textAlign: "right", fontSize: this.fontSize} + }); + } + + return result; + } +} + +// Calulate the checksum digit +// https://en.wikipedia.org/wiki/International_Article_Number_(EAN)#Calculation_of_checksum_digit +function checksum(number){ + var result = 0; + + var i; + for(i = 1; i < 11; i += 2){ + result += parseInt(number[i]); + } + for(i = 0; i < 11; i += 2){ + result += parseInt(number[i]) * 3; + } + + return (10 - (result % 10)) % 10; +} + +module.exports = { UPC, checksum }; diff --git a/apps/cards/UPCE.js b/apps/cards/UPCE.js new file mode 100644 index 000000000..1b4015322 --- /dev/null +++ b/apps/cards/UPCE.js @@ -0,0 +1,177 @@ +// Encoding documentation: +// https://en.wikipedia.org/wiki/Universal_Product_Code#Encoding +// +// UPC-E documentation: +// https://en.wikipedia.org/wiki/Universal_Product_Code#UPC-E + +const encode = require("cards.encode.js"); +const Barcode = require("cards.Barcode.js"); +const upc = require("cards.UPC.js"); + +const EXPANSIONS = [ + "XX00000XXX", + "XX10000XXX", + "XX20000XXX", + "XXX00000XX", + "XXXX00000X", + "XXXXX00005", + "XXXXX00006", + "XXXXX00007", + "XXXXX00008", + "XXXXX00009" +]; + +const PARITIES = [ + ["EEEOOO", "OOOEEE"], + ["EEOEOO", "OOEOEE"], + ["EEOOEO", "OOEEOE"], + ["EEOOOE", "OOEEEO"], + ["EOEEOO", "OEOOEE"], + ["EOOEEO", "OEEOOE"], + ["EOOOEE", "OEEEOO"], + ["EOEOEO", "OEOEOE"], + ["EOEOOE", "OEOEEO"], + ["EOOEOE", "OEEOEO"] +]; + +class UPCE extends Barcode{ + constructor(data, options){ + // Code may be 6 or 8 digits; + // A 7 digit code is ambiguous as to whether the extra digit + // is a UPC-A check or number system digit. + super(data, options); + this.isValid = false; + if(data.search(/^[0-9]{6}$/) !== -1){ + this.middleDigits = data; + this.upcA = expandToUPCA(data, "0"); + this.text = options.text || + `${this.upcA[0]}${data}${this.upcA[this.upcA.length - 1]}`; + this.isValid = true; + } + else if(data.search(/^[01][0-9]{7}$/) !== -1){ + this.middleDigits = data.substring(1, data.length - 1); + this.upcA = expandToUPCA(this.middleDigits, data[0]); + + if(this.upcA[this.upcA.length - 1] === data[data.length - 1]){ + this.isValid = true; + } + else{ + // checksum mismatch + return; + } + } + else{ + return; + } + + this.displayValue = options.displayValue; + + // Make sure the font is not bigger than the space between the guard bars + if(options.fontSize > options.width * 10){ + this.fontSize = options.width * 10; + } + else{ + this.fontSize = options.fontSize; + } + + // Make the guard bars go down half the way of the text + this.guardHeight = options.height + this.fontSize / 2 + options.textMargin; + } + + valid(){ + return this.isValid; + } + + encode(){ + if(this.options.flat){ + return this.flatEncoding(); + } + else{ + return this.guardedEncoding(); + } + } + + flatEncoding(){ + var result = ""; + + result += "101"; + result += this.encodeMiddleDigits(); + result += "010101"; + + return { + data: result, + text: this.text + }; + } + + guardedEncoding(){ + var result = []; + + // Add the UPC-A number system digit beneath the quiet zone + if(this.displayValue){ + result.push({ + data: "00000000", + text: this.text[0], + options: {textAlign: "left", fontSize: this.fontSize} + }); + } + + // Add the guard bars + result.push({ + data: "101", + options: {height: this.guardHeight} + }); + + // Add the 6 UPC-E digits + result.push({ + data: this.encodeMiddleDigits(), + text: this.text.substring(1, 7), + options: {fontSize: this.fontSize} + }); + + // Add the end bits + result.push({ + data: "010101", + options: {height: this.guardHeight} + }); + + // Add the UPC-A check digit beneath the quiet zone + if(this.displayValue){ + result.push({ + data: "00000000", + text: this.text[7], + options: {textAlign: "right", fontSize: this.fontSize} + }); + } + + return result; + } + + encodeMiddleDigits() { + const numberSystem = this.upcA[0]; + const checkDigit = this.upcA[this.upcA.length - 1]; + const parity = PARITIES[parseInt(checkDigit)][parseInt(numberSystem)]; + return encode(this.middleDigits, parity); + } +} + +function expandToUPCA(middleDigits, numberSystem) { + const lastUpcE = parseInt(middleDigits[middleDigits.length - 1]); + const expansion = EXPANSIONS[lastUpcE]; + + let result = ""; + let digitIndex = 0; + for(let i = 0; i < expansion.length; i++) { + let c = expansion[i]; + if (c === 'X') { + result += middleDigits[digitIndex++]; + } else { + result += c; + } + } + + result = `${numberSystem}${result}`; + return `${result}${upc.checksum(result)}`; +} + +module.exports = UPCE; \ No newline at end of file diff --git a/apps/cards/app.js b/apps/cards/app.js index c0bacc918..25af66196 100644 --- a/apps/cards/app.js +++ b/apps/cards/app.js @@ -132,6 +132,60 @@ function showCode(card) { printLinearCode(code.encode().data); break; } + case "EAN_2": { + g.setFont("Vector:20"); + g.setFontAlign(0,1).setColor(BLACK); + g.drawString(card.value, g.getWidth()/2, g.getHeight()); + const EAN2 = require("cards.EAN2.js"); + let code = new EAN2(card.value, {}); + printLinearCode(code.encode().data); + break; + } + case "EAN_5": { + g.setFont("Vector:20"); + g.setFontAlign(0,1).setColor(BLACK); + g.drawString(card.value, g.getWidth()/2, g.getHeight()); + const EAN5 = require("cards.EAN5.js"); + let code = new EAN5(card.value, {}); + printLinearCode(code.encode().data); + break; + } + case "EAN_8": { + g.setFont("Vector:20"); + g.setFontAlign(0,1).setColor(BLACK); + g.drawString(card.value, g.getWidth()/2, g.getHeight()); + const EAN8 = require("cards.EAN8.js"); + let code = new EAN8(card.value, {}); + printLinearCode(code.encode().data); + break; + } + case "EAN_13": { + g.setFont("Vector:20"); + g.setFontAlign(0,1).setColor(BLACK); + g.drawString(card.value, g.getWidth()/2, g.getHeight()); + const EAN13 = require("cards.EAN13.js"); + let code = new EAN13(card.value, {}); + printLinearCode(code.encode().data); + break; + } + case "UPC_A": { + g.setFont("Vector:20"); + g.setFontAlign(0,1).setColor(BLACK); + g.drawString(card.value, g.getWidth()/2, g.getHeight()); + const UPC = require("cards.UPC.js"); + let code = new UPC.UPC(card.value, {}); + printLinearCode(code.encode().data); + break; + } + case "UPC_E": { + g.setFont("Vector:20"); + g.setFontAlign(0,1).setColor(BLACK); + g.drawString(card.value, g.getWidth()/2, g.getHeight()); + const UPCE = require("cards.UPCE.js"); + let code = new UPCE(card.value, {}); + printLinearCode(code.encode().data); + break; + } default: g.clear(true); g.setFont("Vector:30"); diff --git a/apps/cards/constants.js b/apps/cards/constants.js new file mode 100644 index 000000000..0d9f62415 --- /dev/null +++ b/apps/cards/constants.js @@ -0,0 +1,42 @@ +// Standard start end and middle bits + +export const SIDE_BIN = '101'; +export const MIDDLE_BIN = '01010'; + +export const BINARIES = { + 'L': [ // The L (left) type of encoding + '0001101', '0011001', '0010011', '0111101', '0100011', + '0110001', '0101111', '0111011', '0110111', '0001011' + ], + 'G': [ // The G type of encoding + '0100111', '0110011', '0011011', '0100001', '0011101', + '0111001', '0000101', '0010001', '0001001', '0010111' + ], + 'R': [ // The R (right) type of encoding + '1110010', '1100110', '1101100', '1000010', '1011100', + '1001110', '1010000', '1000100', '1001000', '1110100' + ], + 'O': [ // The O (odd) encoding for UPC-E + '0001101', '0011001', '0010011', '0111101', '0100011', + '0110001', '0101111', '0111011', '0110111', '0001011' + ], + 'E': [ // The E (even) encoding for UPC-E + '0100111', '0110011', '0011011', '0100001', '0011101', + '0111001', '0000101', '0010001', '0001001', '0010111' + ] +}; + +// Define the EAN-2 structure +export const EAN2_STRUCTURE = ['LL', 'LG', 'GL', 'GG']; + +// Define the EAN-5 structure +export const EAN5_STRUCTURE = [ + 'GGLLL', 'GLGLL', 'GLLGL', 'GLLLG', 'LGGLL', + 'LLGGL', 'LLLGG', 'LGLGL', 'LGLLG', 'LLGLG' +]; + +// Define the EAN-13 structure +export const EAN13_STRUCTURE = [ + 'LLLLLL', 'LLGLGG', 'LLGGLG', 'LLGGGL', 'LGLLGG', + 'LGGLLG', 'LGGGLL', 'LGLGLG', 'LGLGGL', 'LGGLGL' +]; \ No newline at end of file diff --git a/apps/cards/encode.js b/apps/cards/encode.js new file mode 100644 index 000000000..960bf4991 --- /dev/null +++ b/apps/cards/encode.js @@ -0,0 +1,43 @@ +//import { BINARIES } from './constants'; + +const BINARIES = { + 'L': [ // The L (left) type of encoding + '0001101', '0011001', '0010011', '0111101', '0100011', + '0110001', '0101111', '0111011', '0110111', '0001011' + ], + 'G': [ // The G type of encoding + '0100111', '0110011', '0011011', '0100001', '0011101', + '0111001', '0000101', '0010001', '0001001', '0010111' + ], + 'R': [ // The R (right) type of encoding + '1110010', '1100110', '1101100', '1000010', '1011100', + '1001110', '1010000', '1000100', '1001000', '1110100' + ], + 'O': [ // The O (odd) encoding for UPC-E + '0001101', '0011001', '0010011', '0111101', '0100011', + '0110001', '0101111', '0111011', '0110111', '0001011' + ], + 'E': [ // The E (even) encoding for UPC-E + '0100111', '0110011', '0011011', '0100001', '0011101', + '0111001', '0000101', '0010001', '0001001', '0010111' + ] +}; + +// Encode data string +const encode = (data, structure, separator) => { + let encoded = data + .split('') + .map((val, idx) => BINARIES[structure[idx]]) + .map((val, idx) => val ? val[data[idx]] : ''); + + if (separator) { + const last = data.length - 1; + encoded = encoded.map((val, idx) => ( + idx < last ? val + separator : val + )); + } + + return encoded.join(''); +}; + +module.exports = encode; \ No newline at end of file diff --git a/apps/cards/metadata.json b/apps/cards/metadata.json index 4bc3c9ae1..fe5630438 100644 --- a/apps/cards/metadata.json +++ b/apps/cards/metadata.json @@ -15,6 +15,15 @@ {"name":"cards.qrcode.js","url":"qrcode.js"}, {"name":"cards.codabar.js","url":"codabar.js"}, {"name":"cards.code39.js","url":"code39.js"}, + {"name":"cards.constants.js","url":"constants.js"}, + {"name":"cards.EAN.js","url":"EAN.js"}, + {"name":"cards.EAN2.js","url":"EAN2.js"}, + {"name":"cards.EAN5.js","url":"EAN5.js"}, + {"name":"cards.EAN8.js","url":"EAN8.js"}, + {"name":"cards.EAN13.js","url":"EAN13.js"}, + {"name":"cards.UPC.js","url":"UPC.js"}, + {"name":"cards.UPCE.js","url":"UPCE.js"}, + {"name":"cards.encode.js","url":"encode.js"}, {"name":"cards.img","url":"app-icon.js","evaluate":true} ], "data": [{"name":"cards.settings.json"}] From 1f596995e295031cb7fd49b3560dd98236ac7782 Mon Sep 17 00:00:00 2001 From: Eric de Boer Date: Mon, 8 Apr 2024 22:37:42 +0200 Subject: [PATCH 02/10] allow emulator --- apps/cards/metadata.json | 1 + 1 file changed, 1 insertion(+) diff --git a/apps/cards/metadata.json b/apps/cards/metadata.json index fe5630438..bc6bedce6 100644 --- a/apps/cards/metadata.json +++ b/apps/cards/metadata.json @@ -7,6 +7,7 @@ "screenshots": [{"url":"screenshot_cards_overview.png"}, {"url":"screenshot_cards_card1.png"}, {"url":"screenshot_cards_card2.png"}, {"url":"screenshot_cards_barcode.png"}, {"url":"screenshot_cards_qrcode.png"}], "tags": "cards", "supports": ["BANGLEJS","BANGLEJS2"], + "allow_emulator": true, "readme": "README.md", "storage": [ {"name":"cards.app.js","url":"app.js"}, From 50517444aa9bcdd1fb21f85ae65d21315c85e031 Mon Sep 17 00:00:00 2001 From: Eric de Boer Date: Tue, 9 Apr 2024 09:19:15 +0200 Subject: [PATCH 03/10] Only need flat encoding. --- apps/cards/EAN.js | 38 ++-------------------- apps/cards/EAN13.js | 27 ---------------- apps/cards/UPC.js | 78 --------------------------------------------- apps/cards/UPCE.js | 68 --------------------------------------- 4 files changed, 3 insertions(+), 208 deletions(-) diff --git a/apps/cards/EAN.js b/apps/cards/EAN.js index bb6a4d038..17b4cbaef 100644 --- a/apps/cards/EAN.js +++ b/apps/cards/EAN.js @@ -1,27 +1,12 @@ -//import { SIDE_BIN, MIDDLE_BIN } from './constants'; +import { SIDE_BIN, MIDDLE_BIN } from './constants'; -const Barcode = require("cards.Barcode.js"); const encode = require("cards.encode.js"); +const Barcode = require("cards.Barcode.js"); // Base class for EAN8 & EAN13 class EAN extends Barcode { - constructor(data, options) { super(data, options); - - // Make sure the font is not bigger than the space between the guard bars - this.fontSize = !options.flat && options.fontSize > options.width * 10 - ? options.width * 10 - : options.fontSize; - - // Make the guard bars go down half the way of the text - this.guardHeight = options.height + this.fontSize / 2 + options.textMargin; - } - - encode() { - return this.options.flat - ? this.encodeFlat() - : this.encodeGuarded(); } leftText(from, to) { @@ -40,24 +25,7 @@ class EAN extends Barcode { return encode(data, structure); } - encodeGuarded() { - const textOptions = { fontSize: this.fontSize }; - const guardOptions = { height: this.guardHeight }; - const SIDE_BIN = '101'; - const MIDDLE_BIN = '01010'; - - return [ - { data: SIDE_BIN, options: guardOptions }, - { data: this.leftEncode(), text: this.leftText(), options: textOptions }, - { data: MIDDLE_BIN, options: guardOptions }, - { data: this.rightEncode(), text: this.rightText(), options: textOptions }, - { data: SIDE_BIN, options: guardOptions }, - ]; - } - - encodeFlat() { - const SIDE_BIN = '101'; - const MIDDLE_BIN = '01010'; + encode() { const data = [ SIDE_BIN, this.leftEncode(), diff --git a/apps/cards/EAN13.js b/apps/cards/EAN13.js index 3a914ebd0..323daeef8 100644 --- a/apps/cards/EAN13.js +++ b/apps/cards/EAN13.js @@ -60,33 +60,6 @@ class EAN13 extends EAN { return super.rightEncode(data, 'RRRRRR'); } - // The "standard" way of printing EAN13 barcodes with guard bars - encodeGuarded() { - const data = super.encodeGuarded(); - - // Extend data with left digit & last character - if (this.options.displayValue) { - data.unshift({ - data: '000000000000', - text: this.text.substr(0, 1), - options: { textAlign: 'left', fontSize: this.fontSize } - }); - - if (this.options.lastChar) { - data.push({ - data: '00' - }); - data.push({ - data: '00000', - text: this.options.lastChar, - options: { fontSize: this.fontSize } - }); - } - } - - return data; - } - } module.exports = EAN13; \ No newline at end of file diff --git a/apps/cards/UPC.js b/apps/cards/UPC.js index 3344a0785..b7488b12c 100644 --- a/apps/cards/UPC.js +++ b/apps/cards/UPC.js @@ -12,19 +12,6 @@ class UPC extends Barcode{ } super(data, options); - - this.displayValue = options.displayValue; - - // Make sure the font is not bigger than the space between the guard bars - if(options.fontSize > options.width * 10){ - this.fontSize = options.width * 10; - } - else{ - this.fontSize = options.fontSize; - } - - // Make the guard bars go down half the way of the text - this.guardHeight = options.height + this.fontSize / 2 + options.textMargin; } valid(){ @@ -33,15 +20,6 @@ class UPC extends Barcode{ } encode(){ - if(this.options.flat){ - return this.flatEncoding(); - } - else{ - return this.guardedEncoding(); - } - } - - flatEncoding(){ var result = ""; result += "101"; @@ -55,62 +33,6 @@ class UPC extends Barcode{ text: this.text }; } - - guardedEncoding(){ - var result = []; - - // Add the first digit - if(this.displayValue){ - result.push({ - data: "00000000", - text: this.text.substr(0, 1), - options: {textAlign: "left", fontSize: this.fontSize} - }); - } - - // Add the guard bars - result.push({ - data: "101" + encode(this.data[0], "L"), - options: {height: this.guardHeight} - }); - - // Add the left side - result.push({ - data: encode(this.data.substr(1, 5), "LLLLL"), - text: this.text.substr(1, 5), - options: {fontSize: this.fontSize} - }); - - // Add the middle bits - result.push({ - data: "01010", - options: {height: this.guardHeight} - }); - - // Add the right side - result.push({ - data: encode(this.data.substr(6, 5), "RRRRR"), - text: this.text.substr(6, 5), - options: {fontSize: this.fontSize} - }); - - // Add the end bits - result.push({ - data: encode(this.data[11], "R") + "101", - options: {height: this.guardHeight} - }); - - // Add the last digit - if(this.displayValue){ - result.push({ - data: "00000000", - text: this.text.substr(11, 1), - options: {textAlign: "right", fontSize: this.fontSize} - }); - } - - return result; - } } // Calulate the checksum digit diff --git a/apps/cards/UPCE.js b/apps/cards/UPCE.js index 1b4015322..bd082dd4d 100644 --- a/apps/cards/UPCE.js +++ b/apps/cards/UPCE.js @@ -60,22 +60,6 @@ class UPCE extends Barcode{ return; } } - else{ - return; - } - - this.displayValue = options.displayValue; - - // Make sure the font is not bigger than the space between the guard bars - if(options.fontSize > options.width * 10){ - this.fontSize = options.width * 10; - } - else{ - this.fontSize = options.fontSize; - } - - // Make the guard bars go down half the way of the text - this.guardHeight = options.height + this.fontSize / 2 + options.textMargin; } valid(){ @@ -83,15 +67,6 @@ class UPCE extends Barcode{ } encode(){ - if(this.options.flat){ - return this.flatEncoding(); - } - else{ - return this.guardedEncoding(); - } - } - - flatEncoding(){ var result = ""; result += "101"; @@ -104,49 +79,6 @@ class UPCE extends Barcode{ }; } - guardedEncoding(){ - var result = []; - - // Add the UPC-A number system digit beneath the quiet zone - if(this.displayValue){ - result.push({ - data: "00000000", - text: this.text[0], - options: {textAlign: "left", fontSize: this.fontSize} - }); - } - - // Add the guard bars - result.push({ - data: "101", - options: {height: this.guardHeight} - }); - - // Add the 6 UPC-E digits - result.push({ - data: this.encodeMiddleDigits(), - text: this.text.substring(1, 7), - options: {fontSize: this.fontSize} - }); - - // Add the end bits - result.push({ - data: "010101", - options: {height: this.guardHeight} - }); - - // Add the UPC-A check digit beneath the quiet zone - if(this.displayValue){ - result.push({ - data: "00000000", - text: this.text[7], - options: {textAlign: "right", fontSize: this.fontSize} - }); - } - - return result; - } - encodeMiddleDigits() { const numberSystem = this.upcA[0]; const checkDigit = this.upcA[this.upcA.length - 1]; From 67e7e71df1a8765d63e4b09ddf98bc40ebf3bc5c Mon Sep 17 00:00:00 2001 From: Eric de Boer Date: Tue, 18 Jun 2024 14:37:07 +0200 Subject: [PATCH 04/10] Use require instead of unsupported import/export --- apps/cards/EAN.js | 8 ++--- apps/cards/EAN13.js | 4 +-- apps/cards/constants.js | 77 +++++++++++++++++++++-------------------- apps/cards/encode.js | 27 ++------------- 4 files changed, 47 insertions(+), 69 deletions(-) diff --git a/apps/cards/EAN.js b/apps/cards/EAN.js index 17b4cbaef..d9d857c0e 100644 --- a/apps/cards/EAN.js +++ b/apps/cards/EAN.js @@ -1,4 +1,4 @@ -import { SIDE_BIN, MIDDLE_BIN } from './constants'; +const constants = require("cards.constants.js"); const encode = require("cards.encode.js"); const Barcode = require("cards.Barcode.js"); @@ -27,11 +27,11 @@ class EAN extends Barcode { encode() { const data = [ - SIDE_BIN, + constants.SIDE_BIN, this.leftEncode(), - MIDDLE_BIN, + constants.MIDDLE_BIN, this.rightEncode(), - SIDE_BIN + constants.SIDE_BIN ]; return { diff --git a/apps/cards/EAN13.js b/apps/cards/EAN13.js index 323daeef8..01be1c8df 100644 --- a/apps/cards/EAN13.js +++ b/apps/cards/EAN13.js @@ -1,7 +1,7 @@ // Encoding documentation: // https://en.wikipedia.org/wiki/International_Article_Number_(EAN)#Binary_encoding_of_data_digits_into_EAN-13_barcode -import { EAN13_STRUCTURE } from './constants'; +const constants = require("cards.constants.js"); const EAN = require("cards.EAN.js"); @@ -47,7 +47,7 @@ class EAN13 extends EAN { leftEncode() { const data = this.data.substr(1, 6); - const structure = EAN13_STRUCTURE[this.data[0]]; + const structure = constants.EAN13_STRUCTURE[this.data[0]]; return super.leftEncode(data, structure); } diff --git a/apps/cards/constants.js b/apps/cards/constants.js index 0d9f62415..0f7244ce1 100644 --- a/apps/cards/constants.js +++ b/apps/cards/constants.js @@ -1,42 +1,43 @@ -// Standard start end and middle bits +module.exports = { + // Standard start end and middle bits + SIDE_BIN : '101', + MIDDLE_BIN : '01010', -export const SIDE_BIN = '101'; -export const MIDDLE_BIN = '01010'; + BINARIES : { + 'L': [ // The L (left) type of encoding + '0001101', '0011001', '0010011', '0111101', '0100011', + '0110001', '0101111', '0111011', '0110111', '0001011' + ], + 'G': [ // The G type of encoding + '0100111', '0110011', '0011011', '0100001', '0011101', + '0111001', '0000101', '0010001', '0001001', '0010111' + ], + 'R': [ // The R (right) type of encoding + '1110010', '1100110', '1101100', '1000010', '1011100', + '1001110', '1010000', '1000100', '1001000', '1110100' + ], + 'O': [ // The O (odd) encoding for UPC-E + '0001101', '0011001', '0010011', '0111101', '0100011', + '0110001', '0101111', '0111011', '0110111', '0001011' + ], + 'E': [ // The E (even) encoding for UPC-E + '0100111', '0110011', '0011011', '0100001', '0011101', + '0111001', '0000101', '0010001', '0001001', '0010111' + ] + }, -export const BINARIES = { - 'L': [ // The L (left) type of encoding - '0001101', '0011001', '0010011', '0111101', '0100011', - '0110001', '0101111', '0111011', '0110111', '0001011' + // Define the EAN-2 structure + EAN2_STRUCTURE : ['LL', 'LG', 'GL', 'GG'], + + // Define the EAN-5 structure + EAN5_STRUCTURE : [ + 'GGLLL', 'GLGLL', 'GLLGL', 'GLLLG', 'LGGLL', + 'LLGGL', 'LLLGG', 'LGLGL', 'LGLLG', 'LLGLG' ], - 'G': [ // The G type of encoding - '0100111', '0110011', '0011011', '0100001', '0011101', - '0111001', '0000101', '0010001', '0001001', '0010111' - ], - 'R': [ // The R (right) type of encoding - '1110010', '1100110', '1101100', '1000010', '1011100', - '1001110', '1010000', '1000100', '1001000', '1110100' - ], - 'O': [ // The O (odd) encoding for UPC-E - '0001101', '0011001', '0010011', '0111101', '0100011', - '0110001', '0101111', '0111011', '0110111', '0001011' - ], - 'E': [ // The E (even) encoding for UPC-E - '0100111', '0110011', '0011011', '0100001', '0011101', - '0111001', '0000101', '0010001', '0001001', '0010111' + + // Define the EAN-13 structure + EAN13_STRUCTURE : [ + 'LLLLLL', 'LLGLGG', 'LLGGLG', 'LLGGGL', 'LGLLGG', + 'LGGLLG', 'LGGGLL', 'LGLGLG', 'LGLGGL', 'LGGLGL' ] -}; - -// Define the EAN-2 structure -export const EAN2_STRUCTURE = ['LL', 'LG', 'GL', 'GG']; - -// Define the EAN-5 structure -export const EAN5_STRUCTURE = [ - 'GGLLL', 'GLGLL', 'GLLGL', 'GLLLG', 'LGGLL', - 'LLGGL', 'LLLGG', 'LGLGL', 'LGLLG', 'LLGLG' -]; - -// Define the EAN-13 structure -export const EAN13_STRUCTURE = [ - 'LLLLLL', 'LLGLGG', 'LLGGLG', 'LLGGGL', 'LGLLGG', - 'LGGLLG', 'LGGGLL', 'LGLGLG', 'LGLGGL', 'LGGLGL' -]; \ No newline at end of file +} \ No newline at end of file diff --git a/apps/cards/encode.js b/apps/cards/encode.js index 960bf4991..b484964c7 100644 --- a/apps/cards/encode.js +++ b/apps/cards/encode.js @@ -1,33 +1,10 @@ -//import { BINARIES } from './constants'; - -const BINARIES = { - 'L': [ // The L (left) type of encoding - '0001101', '0011001', '0010011', '0111101', '0100011', - '0110001', '0101111', '0111011', '0110111', '0001011' - ], - 'G': [ // The G type of encoding - '0100111', '0110011', '0011011', '0100001', '0011101', - '0111001', '0000101', '0010001', '0001001', '0010111' - ], - 'R': [ // The R (right) type of encoding - '1110010', '1100110', '1101100', '1000010', '1011100', - '1001110', '1010000', '1000100', '1001000', '1110100' - ], - 'O': [ // The O (odd) encoding for UPC-E - '0001101', '0011001', '0010011', '0111101', '0100011', - '0110001', '0101111', '0111011', '0110111', '0001011' - ], - 'E': [ // The E (even) encoding for UPC-E - '0100111', '0110011', '0011011', '0100001', '0011101', - '0111001', '0000101', '0010001', '0001001', '0010111' - ] -}; +const constants = require("cards.constants.js"); // Encode data string const encode = (data, structure, separator) => { let encoded = data .split('') - .map((val, idx) => BINARIES[structure[idx]]) + .map((val, idx) => constants.BINARIES[structure[idx]]) .map((val, idx) => val ? val[data[idx]] : ''); if (separator) { From fd2b3473492919ca6a22d2106fba63b6cdb54f66 Mon Sep 17 00:00:00 2001 From: Eric de Boer Date: Tue, 9 Apr 2024 09:17:10 +0200 Subject: [PATCH 05/10] Espruino doesn't support search or {} Regex quantifier, replace with test --- apps/cards/EAN13.js | 4 ++-- apps/cards/EAN2.js | 2 +- apps/cards/EAN5.js | 2 +- apps/cards/EAN8.js | 4 ++-- apps/cards/UPC.js | 4 ++-- apps/cards/UPCE.js | 4 ++-- 6 files changed, 10 insertions(+), 10 deletions(-) diff --git a/apps/cards/EAN13.js b/apps/cards/EAN13.js index 01be1c8df..f04417b6a 100644 --- a/apps/cards/EAN13.js +++ b/apps/cards/EAN13.js @@ -24,7 +24,7 @@ class EAN13 extends EAN { constructor(data, options) { // Add checksum if it does not exist - if (data.search(/^[0-9]{12}$/) !== -1) { + if (/^[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]$/.test(data)) { data += checksum(data); } @@ -36,7 +36,7 @@ class EAN13 extends EAN { valid() { return ( - this.data.search(/^[0-9]{13}$/) !== -1 && + /^[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]$/.test(this.data) && +this.data[12] === checksum(this.data) ); } diff --git a/apps/cards/EAN2.js b/apps/cards/EAN2.js index c7ceb059b..840a98ce0 100644 --- a/apps/cards/EAN2.js +++ b/apps/cards/EAN2.js @@ -12,7 +12,7 @@ class EAN2 extends Barcode { } valid() { - return this.data.search(/^[0-9]{2}$/) !== -1; + return /^[0-9][0-9]$/.test(this.data); } encode(){ diff --git a/apps/cards/EAN5.js b/apps/cards/EAN5.js index 77217cd95..241f422e1 100644 --- a/apps/cards/EAN5.js +++ b/apps/cards/EAN5.js @@ -24,7 +24,7 @@ class EAN5 extends Barcode { } valid() { - return this.data.search(/^[0-9]{5}$/) !== -1; + return /^[0-9][0-9][0-9][0-9][0-9]$/.test(this.data); } encode() { diff --git a/apps/cards/EAN8.js b/apps/cards/EAN8.js index 7ffb41c4c..abcf141fc 100644 --- a/apps/cards/EAN8.js +++ b/apps/cards/EAN8.js @@ -20,7 +20,7 @@ class EAN8 extends EAN { constructor(data, options) { // Add checksum if it does not exist - if (data.search(/^[0-9]{7}$/) !== -1) { + if (/^[0-9][0-9][0-9][0-9][0-9][0-9][0-9]$/.test(data)) { data += checksum(data); } @@ -29,7 +29,7 @@ class EAN8 extends EAN { valid() { return ( - this.data.search(/^[0-9]{8}$/) !== -1 && + /^[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]$/.test(this.data) && +this.data[7] === checksum(this.data) ); } diff --git a/apps/cards/UPC.js b/apps/cards/UPC.js index b7488b12c..dd18f25d9 100644 --- a/apps/cards/UPC.js +++ b/apps/cards/UPC.js @@ -7,7 +7,7 @@ const Barcode = require("cards.Barcode.js"); class UPC extends Barcode{ constructor(data, options){ // Add checksum if it does not exist - if(data.search(/^[0-9]{11}$/) !== -1){ + if(/^[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]$/.test(data)){ data += checksum(data); } @@ -15,7 +15,7 @@ class UPC extends Barcode{ } valid(){ - return this.data.search(/^[0-9]{12}$/) !== -1 && + return /^[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]$/.test(this.data) && this.data[11] == checksum(this.data); } diff --git a/apps/cards/UPCE.js b/apps/cards/UPCE.js index bd082dd4d..898abfad0 100644 --- a/apps/cards/UPCE.js +++ b/apps/cards/UPCE.js @@ -41,14 +41,14 @@ class UPCE extends Barcode{ // is a UPC-A check or number system digit. super(data, options); this.isValid = false; - if(data.search(/^[0-9]{6}$/) !== -1){ + if(/^[0-9][0-9][0-9][0-9][0-9][0-9]$/.test(data)){ this.middleDigits = data; this.upcA = expandToUPCA(data, "0"); this.text = options.text || `${this.upcA[0]}${data}${this.upcA[this.upcA.length - 1]}`; this.isValid = true; } - else if(data.search(/^[01][0-9]{7}$/) !== -1){ + else if(/^[01][0-9][0-9][0-9][0-9][0-9][0-9][0-9]$/.test(data)){ this.middleDigits = data.substring(1, data.length - 1); this.upcA = expandToUPCA(this.middleDigits, data[0]); From c2792f667a6b598176232c8de9399e356ea1a146 Mon Sep 17 00:00:00 2001 From: Eric de Boer Date: Tue, 9 Apr 2024 10:52:51 +0200 Subject: [PATCH 06/10] Also add padding for linear code, otherwise black watch border prevents scanning. --- apps/cards/app.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/apps/cards/app.js b/apps/cards/app.js index 25af66196..2e001760f 100644 --- a/apps/cards/app.js +++ b/apps/cards/app.js @@ -82,16 +82,17 @@ function printSquareCode(binary, size) { } } function printLinearCode(binary) { + var padding = 5; var yFrom = 15; var yTo = 28; - var width = g.getWidth()/binary.length; + var width = (g.getWidth()-(2*padding))/binary.length; for(var b = 0; b < binary.length; b++){ var x = b * width; if(binary[b] === "1"){ - g.setColor(BLACK).fillRect({x:x, y:yFrom, w:width, h:g.getHeight() - (yTo+yFrom)}); + g.setColor(BLACK).fillRect({x:x+padding, y:yFrom, w:width, h:g.getHeight() - (yTo+yFrom)}); } else if(binary[b]){ - g.setColor(WHITE).fillRect({x:x, y:yFrom, w:width, h:g.getHeight() - (yTo+yFrom)}); + g.setColor(WHITE).fillRect({x:x+padding, y:yFrom, w:width, h:g.getHeight() - (yTo+yFrom)}); } } } From dfa3417e682cfb43823ad112f4cdb17942bd28e3 Mon Sep 17 00:00:00 2001 From: Eric de Boer Date: Mon, 8 Apr 2024 22:41:53 +0200 Subject: [PATCH 07/10] update version --- apps/cards/ChangeLog | 1 + apps/cards/metadata.json | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/apps/cards/ChangeLog b/apps/cards/ChangeLog index e7e13b8e7..b35947cda 100644 --- a/apps/cards/ChangeLog +++ b/apps/cards/ChangeLog @@ -2,3 +2,4 @@ 0.02: Hiding widgets while showing the code 0.03: Added option to use max brightness when showing code 0.04: Minor code improvements +0.05: Add EAN & UPC codes diff --git a/apps/cards/metadata.json b/apps/cards/metadata.json index bc6bedce6..bdffeba71 100644 --- a/apps/cards/metadata.json +++ b/apps/cards/metadata.json @@ -1,7 +1,7 @@ { "id": "cards", "name": "Cards", - "version": "0.04", + "version": "0.05", "description": "Display loyalty cards", "icon": "app.png", "screenshots": [{"url":"screenshot_cards_overview.png"}, {"url":"screenshot_cards_card1.png"}, {"url":"screenshot_cards_card2.png"}, {"url":"screenshot_cards_barcode.png"}, {"url":"screenshot_cards_qrcode.png"}], From 5f18f47d8a1d30ad4dde2bd806c18800924e3e0a Mon Sep 17 00:00:00 2001 From: Eric de Boer Date: Thu, 20 Jun 2024 14:47:36 +0200 Subject: [PATCH 08/10] Add license --- apps/cards/EAN.js | 26 ++++++++++++++++++++++++++ apps/cards/EAN13.js | 25 +++++++++++++++++++++++++ apps/cards/EAN2.js | 25 +++++++++++++++++++++++++ apps/cards/EAN5.js | 25 +++++++++++++++++++++++++ apps/cards/EAN8.js | 25 +++++++++++++++++++++++++ apps/cards/UPC.js | 25 +++++++++++++++++++++++++ apps/cards/UPCE.js | 25 +++++++++++++++++++++++++ apps/cards/constants.js | 26 ++++++++++++++++++++++++++ apps/cards/encode.js | 26 ++++++++++++++++++++++++++ 9 files changed, 228 insertions(+) diff --git a/apps/cards/EAN.js b/apps/cards/EAN.js index d9d857c0e..8ac113292 100644 --- a/apps/cards/EAN.js +++ b/apps/cards/EAN.js @@ -1,3 +1,29 @@ +/* + * JS source adapted from https://github.com/lindell/JsBarcode + * + * The MIT License (MIT) + * + * Copyright (c) 2016 Johan Lindell (johan@lindell.me) + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + */ + const constants = require("cards.constants.js"); const encode = require("cards.encode.js"); diff --git a/apps/cards/EAN13.js b/apps/cards/EAN13.js index f04417b6a..9e36356b6 100644 --- a/apps/cards/EAN13.js +++ b/apps/cards/EAN13.js @@ -1,5 +1,30 @@ // Encoding documentation: // https://en.wikipedia.org/wiki/International_Article_Number_(EAN)#Binary_encoding_of_data_digits_into_EAN-13_barcode +/* + * JS source adapted from https://github.com/lindell/JsBarcode + * + * The MIT License (MIT) + * + * Copyright (c) 2016 Johan Lindell (johan@lindell.me) + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + */ const constants = require("cards.constants.js"); diff --git a/apps/cards/EAN2.js b/apps/cards/EAN2.js index 840a98ce0..323d6dc9a 100644 --- a/apps/cards/EAN2.js +++ b/apps/cards/EAN2.js @@ -1,5 +1,30 @@ // Encoding documentation: // https://en.wikipedia.org/wiki/EAN_2#Encoding +/* + * JS source adapted from https://github.com/lindell/JsBarcode + * + * The MIT License (MIT) + * + * Copyright (c) 2016 Johan Lindell (johan@lindell.me) + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + */ const constants = require("cards.constants.js"); const encode = require("cards.encode.js"); diff --git a/apps/cards/EAN5.js b/apps/cards/EAN5.js index 241f422e1..68545d360 100644 --- a/apps/cards/EAN5.js +++ b/apps/cards/EAN5.js @@ -1,5 +1,30 @@ // Encoding documentation: // https://en.wikipedia.org/wiki/EAN_5#Encoding +/* + * JS source adapted from https://github.com/lindell/JsBarcode + * + * The MIT License (MIT) + * + * Copyright (c) 2016 Johan Lindell (johan@lindell.me) + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + */ const constants = require("cards.constants.js"); const encode = require("cards.encode.js"); diff --git a/apps/cards/EAN8.js b/apps/cards/EAN8.js index abcf141fc..382ee647a 100644 --- a/apps/cards/EAN8.js +++ b/apps/cards/EAN8.js @@ -1,5 +1,30 @@ // Encoding documentation: // http://www.barcodeisland.com/ean8.phtml +/* + * JS source adapted from https://github.com/lindell/JsBarcode + * + * The MIT License (MIT) + * + * Copyright (c) 2016 Johan Lindell (johan@lindell.me) + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + */ const EAN = require("cards.EAN.js"); diff --git a/apps/cards/UPC.js b/apps/cards/UPC.js index dd18f25d9..6f581ca18 100644 --- a/apps/cards/UPC.js +++ b/apps/cards/UPC.js @@ -1,5 +1,30 @@ // Encoding documentation: // https://en.wikipedia.org/wiki/Universal_Product_Code#Encoding +/* + * JS source adapted from https://github.com/lindell/JsBarcode + * + * The MIT License (MIT) + * + * Copyright (c) 2016 Johan Lindell (johan@lindell.me) + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + */ const encode = require("cards.encode.js"); const Barcode = require("cards.Barcode.js"); diff --git a/apps/cards/UPCE.js b/apps/cards/UPCE.js index 898abfad0..9aaa464b7 100644 --- a/apps/cards/UPCE.js +++ b/apps/cards/UPCE.js @@ -3,6 +3,31 @@ // // UPC-E documentation: // https://en.wikipedia.org/wiki/Universal_Product_Code#UPC-E +/* + * JS source adapted from https://github.com/lindell/JsBarcode + * + * The MIT License (MIT) + * + * Copyright (c) 2016 Johan Lindell (johan@lindell.me) + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + */ const encode = require("cards.encode.js"); const Barcode = require("cards.Barcode.js"); diff --git a/apps/cards/constants.js b/apps/cards/constants.js index 0f7244ce1..86635691b 100644 --- a/apps/cards/constants.js +++ b/apps/cards/constants.js @@ -1,3 +1,29 @@ +/* + * JS source adapted from https://github.com/lindell/JsBarcode + * + * The MIT License (MIT) + * + * Copyright (c) 2016 Johan Lindell (johan@lindell.me) + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + */ + module.exports = { // Standard start end and middle bits SIDE_BIN : '101', diff --git a/apps/cards/encode.js b/apps/cards/encode.js index b484964c7..6390431f1 100644 --- a/apps/cards/encode.js +++ b/apps/cards/encode.js @@ -1,3 +1,29 @@ +/* + * JS source adapted from https://github.com/lindell/JsBarcode + * + * The MIT License (MIT) + * + * Copyright (c) 2016 Johan Lindell (johan@lindell.me) + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + */ + const constants = require("cards.constants.js"); // Encode data string From 39af2474d2694c9680089adf5a71ec0bdc0f4f25 Mon Sep 17 00:00:00 2001 From: Eric de Boer Date: Fri, 21 Jun 2024 09:34:28 +0200 Subject: [PATCH 09/10] Remove EAN 2 and 5 as they are not supported by the loyalty cards app --- apps/cards/EAN2.js | 55 ---------------------------------- apps/cards/EAN5.js | 65 ---------------------------------------- apps/cards/app.js | 18 ----------- apps/cards/constants.js | 9 ------ apps/cards/metadata.json | 2 -- 5 files changed, 149 deletions(-) delete mode 100644 apps/cards/EAN2.js delete mode 100644 apps/cards/EAN5.js diff --git a/apps/cards/EAN2.js b/apps/cards/EAN2.js deleted file mode 100644 index 323d6dc9a..000000000 --- a/apps/cards/EAN2.js +++ /dev/null @@ -1,55 +0,0 @@ -// Encoding documentation: -// https://en.wikipedia.org/wiki/EAN_2#Encoding -/* - * JS source adapted from https://github.com/lindell/JsBarcode - * - * The MIT License (MIT) - * - * Copyright (c) 2016 Johan Lindell (johan@lindell.me) - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to - * deal in the Software without restriction, including without limitation the - * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or - * sell copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING - * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS - * IN THE SOFTWARE. - */ - -const constants = require("cards.constants.js"); -const encode = require("cards.encode.js"); -const Barcode = require("cards.Barcode.js"); - -class EAN2 extends Barcode { - - constructor(data, options) { - super(data, options); - } - - valid() { - return /^[0-9][0-9]$/.test(this.data); - } - - encode(){ - // Choose the structure based on the number mod 4 - const structure = constants.EAN2_STRUCTURE[parseInt(this.data) % 4]; - return { - // Start bits + Encode the two digits with 01 in between - data: '1011' + encode(this.data, structure, '01'), - text: this.text - }; - } - -} - -module.exports = EAN2; diff --git a/apps/cards/EAN5.js b/apps/cards/EAN5.js deleted file mode 100644 index 68545d360..000000000 --- a/apps/cards/EAN5.js +++ /dev/null @@ -1,65 +0,0 @@ -// Encoding documentation: -// https://en.wikipedia.org/wiki/EAN_5#Encoding -/* - * JS source adapted from https://github.com/lindell/JsBarcode - * - * The MIT License (MIT) - * - * Copyright (c) 2016 Johan Lindell (johan@lindell.me) - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to - * deal in the Software without restriction, including without limitation the - * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or - * sell copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING - * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS - * IN THE SOFTWARE. - */ - -const constants = require("cards.constants.js"); -const encode = require("cards.encode.js"); -const Barcode = require("cards.Barcode.js"); - -const checksum = (data) => { - const result = data - .split('') - .map(n => +n) - .reduce((sum, a, idx) => { - return idx % 2 - ? sum + a * 9 - : sum + a * 3; - }, 0); - return result % 10; -}; - -class EAN5 extends Barcode { - - constructor(data, options) { - super(data, options); - } - - valid() { - return /^[0-9][0-9][0-9][0-9][0-9]$/.test(this.data); - } - - encode() { - const structure = constants.EAN5_STRUCTURE[checksum(this.data)]; - return { - data: '1011' + encode(this.data, structure, '01'), - text: this.text - }; - } - -} - -module.exports = EAN5; diff --git a/apps/cards/app.js b/apps/cards/app.js index 2e001760f..45e72831c 100644 --- a/apps/cards/app.js +++ b/apps/cards/app.js @@ -133,24 +133,6 @@ function showCode(card) { printLinearCode(code.encode().data); break; } - case "EAN_2": { - g.setFont("Vector:20"); - g.setFontAlign(0,1).setColor(BLACK); - g.drawString(card.value, g.getWidth()/2, g.getHeight()); - const EAN2 = require("cards.EAN2.js"); - let code = new EAN2(card.value, {}); - printLinearCode(code.encode().data); - break; - } - case "EAN_5": { - g.setFont("Vector:20"); - g.setFontAlign(0,1).setColor(BLACK); - g.drawString(card.value, g.getWidth()/2, g.getHeight()); - const EAN5 = require("cards.EAN5.js"); - let code = new EAN5(card.value, {}); - printLinearCode(code.encode().data); - break; - } case "EAN_8": { g.setFont("Vector:20"); g.setFontAlign(0,1).setColor(BLACK); diff --git a/apps/cards/constants.js b/apps/cards/constants.js index 86635691b..ead651fdd 100644 --- a/apps/cards/constants.js +++ b/apps/cards/constants.js @@ -52,15 +52,6 @@ module.exports = { ] }, - // Define the EAN-2 structure - EAN2_STRUCTURE : ['LL', 'LG', 'GL', 'GG'], - - // Define the EAN-5 structure - EAN5_STRUCTURE : [ - 'GGLLL', 'GLGLL', 'GLLGL', 'GLLLG', 'LGGLL', - 'LLGGL', 'LLLGG', 'LGLGL', 'LGLLG', 'LLGLG' - ], - // Define the EAN-13 structure EAN13_STRUCTURE : [ 'LLLLLL', 'LLGLGG', 'LLGGLG', 'LLGGGL', 'LGLLGG', diff --git a/apps/cards/metadata.json b/apps/cards/metadata.json index bdffeba71..d17ff4a51 100644 --- a/apps/cards/metadata.json +++ b/apps/cards/metadata.json @@ -18,8 +18,6 @@ {"name":"cards.code39.js","url":"code39.js"}, {"name":"cards.constants.js","url":"constants.js"}, {"name":"cards.EAN.js","url":"EAN.js"}, - {"name":"cards.EAN2.js","url":"EAN2.js"}, - {"name":"cards.EAN5.js","url":"EAN5.js"}, {"name":"cards.EAN8.js","url":"EAN8.js"}, {"name":"cards.EAN13.js","url":"EAN13.js"}, {"name":"cards.UPC.js","url":"UPC.js"}, From 333fd0562ae8f4dc1a8641cee8e3f27f9abf8d48 Mon Sep 17 00:00:00 2001 From: Eric de Boer Date: Fri, 21 Jun 2024 11:06:05 +0200 Subject: [PATCH 10/10] Move constants to files they are used. --- apps/cards/EAN.js | 12 ++++---- apps/cards/EAN13.js | 8 ++++-- apps/cards/constants.js | 60 ---------------------------------------- apps/cards/encode.js | 25 +++++++++++++++-- apps/cards/metadata.json | 1 - 5 files changed, 35 insertions(+), 71 deletions(-) delete mode 100644 apps/cards/constants.js diff --git a/apps/cards/EAN.js b/apps/cards/EAN.js index 8ac113292..177874494 100644 --- a/apps/cards/EAN.js +++ b/apps/cards/EAN.js @@ -24,11 +24,13 @@ * IN THE SOFTWARE. */ -const constants = require("cards.constants.js"); - const encode = require("cards.encode.js"); const Barcode = require("cards.Barcode.js"); +// Standard start end and middle bits +const SIDE_BIN = '101'; +const MIDDLE_BIN = '01010'; + // Base class for EAN8 & EAN13 class EAN extends Barcode { constructor(data, options) { @@ -53,11 +55,11 @@ class EAN extends Barcode { encode() { const data = [ - constants.SIDE_BIN, + SIDE_BIN, this.leftEncode(), - constants.MIDDLE_BIN, + MIDDLE_BIN, this.rightEncode(), - constants.SIDE_BIN + SIDE_BIN ]; return { diff --git a/apps/cards/EAN13.js b/apps/cards/EAN13.js index 9e36356b6..c91e385e3 100644 --- a/apps/cards/EAN13.js +++ b/apps/cards/EAN13.js @@ -26,10 +26,12 @@ * IN THE SOFTWARE. */ -const constants = require("cards.constants.js"); - const EAN = require("cards.EAN.js"); +const EAN13_STRUCTURE = [ + 'LLLLLL', 'LLGLGG', 'LLGGLG', 'LLGGGL', 'LGLLGG', + 'LGGLLG', 'LGGGLL', 'LGLGLG', 'LGLGGL', 'LGGLGL' +]; // Calculate the checksum digit // https://en.wikipedia.org/wiki/International_Article_Number_(EAN)#Calculation_of_checksum_digit @@ -72,7 +74,7 @@ class EAN13 extends EAN { leftEncode() { const data = this.data.substr(1, 6); - const structure = constants.EAN13_STRUCTURE[this.data[0]]; + const structure = EAN13_STRUCTURE[this.data[0]]; return super.leftEncode(data, structure); } diff --git a/apps/cards/constants.js b/apps/cards/constants.js deleted file mode 100644 index ead651fdd..000000000 --- a/apps/cards/constants.js +++ /dev/null @@ -1,60 +0,0 @@ -/* - * JS source adapted from https://github.com/lindell/JsBarcode - * - * The MIT License (MIT) - * - * Copyright (c) 2016 Johan Lindell (johan@lindell.me) - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to - * deal in the Software without restriction, including without limitation the - * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or - * sell copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING - * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS - * IN THE SOFTWARE. - */ - -module.exports = { - // Standard start end and middle bits - SIDE_BIN : '101', - MIDDLE_BIN : '01010', - - BINARIES : { - 'L': [ // The L (left) type of encoding - '0001101', '0011001', '0010011', '0111101', '0100011', - '0110001', '0101111', '0111011', '0110111', '0001011' - ], - 'G': [ // The G type of encoding - '0100111', '0110011', '0011011', '0100001', '0011101', - '0111001', '0000101', '0010001', '0001001', '0010111' - ], - 'R': [ // The R (right) type of encoding - '1110010', '1100110', '1101100', '1000010', '1011100', - '1001110', '1010000', '1000100', '1001000', '1110100' - ], - 'O': [ // The O (odd) encoding for UPC-E - '0001101', '0011001', '0010011', '0111101', '0100011', - '0110001', '0101111', '0111011', '0110111', '0001011' - ], - 'E': [ // The E (even) encoding for UPC-E - '0100111', '0110011', '0011011', '0100001', '0011101', - '0111001', '0000101', '0010001', '0001001', '0010111' - ] - }, - - // Define the EAN-13 structure - EAN13_STRUCTURE : [ - 'LLLLLL', 'LLGLGG', 'LLGGLG', 'LLGGGL', 'LGLLGG', - 'LGGLLG', 'LGGGLL', 'LGLGLG', 'LGLGGL', 'LGGLGL' - ] -} \ No newline at end of file diff --git a/apps/cards/encode.js b/apps/cards/encode.js index 6390431f1..7cb1fcc87 100644 --- a/apps/cards/encode.js +++ b/apps/cards/encode.js @@ -24,13 +24,34 @@ * IN THE SOFTWARE. */ -const constants = require("cards.constants.js"); +const BINARIES = { + 'L': [ // The L (left) type of encoding + '0001101', '0011001', '0010011', '0111101', '0100011', + '0110001', '0101111', '0111011', '0110111', '0001011' + ], + 'G': [ // The G type of encoding + '0100111', '0110011', '0011011', '0100001', '0011101', + '0111001', '0000101', '0010001', '0001001', '0010111' + ], + 'R': [ // The R (right) type of encoding + '1110010', '1100110', '1101100', '1000010', '1011100', + '1001110', '1010000', '1000100', '1001000', '1110100' + ], + 'O': [ // The O (odd) encoding for UPC-E + '0001101', '0011001', '0010011', '0111101', '0100011', + '0110001', '0101111', '0111011', '0110111', '0001011' + ], + 'E': [ // The E (even) encoding for UPC-E + '0100111', '0110011', '0011011', '0100001', '0011101', + '0111001', '0000101', '0010001', '0001001', '0010111' + ] +}; // Encode data string const encode = (data, structure, separator) => { let encoded = data .split('') - .map((val, idx) => constants.BINARIES[structure[idx]]) + .map((val, idx) => BINARIES[structure[idx]]) .map((val, idx) => val ? val[data[idx]] : ''); if (separator) { diff --git a/apps/cards/metadata.json b/apps/cards/metadata.json index d17ff4a51..c8d19a375 100644 --- a/apps/cards/metadata.json +++ b/apps/cards/metadata.json @@ -16,7 +16,6 @@ {"name":"cards.qrcode.js","url":"qrcode.js"}, {"name":"cards.codabar.js","url":"codabar.js"}, {"name":"cards.code39.js","url":"code39.js"}, - {"name":"cards.constants.js","url":"constants.js"}, {"name":"cards.EAN.js","url":"EAN.js"}, {"name":"cards.EAN8.js","url":"EAN8.js"}, {"name":"cards.EAN13.js","url":"EAN13.js"},