123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395 |
- !(function () {
- var CHAR_TILDE = 126
- var CODE_FNC1 = 102
- var SET_STARTA = 103
- var SET_STARTB = 104
- var SET_STARTC = 105
- var SET_SHIFT = 98
- var SET_CODEA = 101
- var SET_CODEB = 100
- var SET_STOP = 106
- var REPLACE_CODES = {
- CHAR_TILDE: CODE_FNC1
- }
- var CODESET = {
- ANY: 1,
- AB: 2,
- A: 3,
- B: 4,
- C: 5
- }
- function getBytes(str) {
- var bytes = []
- for (var i = 0; i < str.length; i++) {
- bytes.push(str.charCodeAt(i))
- }
- return bytes
- }
- exports.code128 = function (ctx, text, width, height) {
- width = parseInt(width)
- height = parseInt(height)
- var codes = stringToCode128(text)
- var g = new Graphics(ctx, width, height)
- var barWeight = g.area.width / ((codes.length - 3) * 11 + 35)
- var x = g.area.left
- var y = g.area.top
- for (var i = 0; i < codes.length; i++) {
- var c = codes[i]
-
- for (var bar = 0; bar < 8; bar += 2) {
- var barW = PATTERNS[c][bar] * barWeight
-
- var barH = height - y
- var spcW = PATTERNS[c][bar + 1] * barWeight
-
- if (barW > 0) {
- g.fillFgRect(x, y, barW, barH)
- }
- x += barW + spcW
- }
- }
- ctx.draw()
- }
- function stringToCode128(text) {
- var barc = {
- currcs: CODESET.C
- }
- var bytes = getBytes(text)
-
- var index = bytes[0] == CHAR_TILDE ? 1 : 0
- var csa1 = bytes.length > 0 ? codeSetAllowedFor(bytes[index++]) : CODESET.AB
- var csa2 = bytes.length > 0 ? codeSetAllowedFor(bytes[index++]) : CODESET.AB
- barc.currcs = getBestStartSet(csa1, csa2)
- barc.currcs = perhapsCodeC(bytes, barc.currcs)
-
-
- var codes = new Array()
- switch (barc.currcs) {
- case CODESET.A:
- codes.push(SET_STARTA)
- break
- case CODESET.B:
- codes.push(SET_STARTB)
- break
- default:
- codes.push(SET_STARTC)
- break
- }
- for (var i = 0; i < bytes.length; i++) {
- var b1 = bytes[i]
-
- if (b1 in REPLACE_CODES) {
- codes.push(REPLACE_CODES[b1])
- i++
- b1 = bytes[i]
- }
-
- var b2 = bytes.length > i + 1 ? bytes[i + 1] : -1
- codes = codes.concat(codesForChar(b1, b2, barc.currcs))
-
- if (barc.currcs == CODESET.C) i++
- }
-
- var checksum = codes[0]
- for (var weight = 1; weight < codes.length; weight++) {
- checksum += weight * codes[weight]
- }
- codes.push(checksum % 103)
- codes.push(SET_STOP)
-
- return codes
- function getBestStartSet(csa1, csa2) {
-
-
- var vote = 0
- vote += csa1 == CODESET.A ? 1 : 0
- vote += csa1 == CODESET.B ? -1 : 0
- vote += csa2 == CODESET.A ? 1 : 0
- vote += csa2 == CODESET.B ? -1 : 0
-
- return vote > 0 ? CODESET.A : CODESET.B
- }
- function perhapsCodeC(bytes, codeset) {
- for (var i = 0; i < bytes.length; i++) {
- var b = bytes[i]
- if ((b < 48 || b > 57) && b != CHAR_TILDE) return codeset
- }
- return CODESET.C
- }
-
-
- function codesForChar(chr1, chr2, currcs) {
- var result = []
- var shifter = -1
- if (charCompatible(chr1, currcs)) {
- if (currcs == CODESET.C) {
- if (chr2 == -1) {
- shifter = SET_CODEB
- currcs = CODESET.B
- } else if (chr2 != -1 && !charCompatible(chr2, currcs)) {
-
- if (charCompatible(chr2, CODESET.A)) {
- shifter = SET_CODEA
- currcs = CODESET.A
- } else {
- shifter = SET_CODEB
- currcs = CODESET.B
- }
- }
- }
- } else {
-
- if (chr2 != -1 && !charCompatible(chr2, currcs)) {
-
- switch (currcs) {
- case CODESET.A:
- shifter = SET_CODEB
- currcs = CODESET.B
- break
- case CODESET.B:
- shifter = SET_CODEA
- currcs = CODESET.A
- break
- }
- } else {
-
- shifter = SET_SHIFT
- }
- }
-
- if (shifter != -1) {
- result.push(shifter)
- result.push(codeValue(chr1))
- } else {
- if (currcs == CODESET.C) {
-
- result.push(codeValue(chr1, chr2))
- } else {
- result.push(codeValue(chr1))
- }
- }
- barc.currcs = currcs
- return result
- }
- }
-
- function codeValue(chr1, chr2) {
- if (typeof chr2 == 'undefined') {
- return chr1 >= 32 ? chr1 - 32 : chr1 + 64
- } else {
- return parseInt(String.fromCharCode(chr1) + String.fromCharCode(chr2))
- }
- }
- function charCompatible(chr, codeset) {
- var csa = codeSetAllowedFor(chr)
- if (csa == CODESET.ANY) return true
-
- if (csa == CODESET.AB) return true
- if (csa == CODESET.A && codeset == CODESET.A) return true
- if (csa == CODESET.B && codeset == CODESET.B) return true
- return false
- }
- function codeSetAllowedFor(chr) {
- if (chr >= 48 && chr <= 57) {
-
- return CODESET.ANY
- } else if (chr >= 32 && chr <= 95) {
-
- return CODESET.AB
- } else {
-
- return chr < 32 ? CODESET.A : CODESET.B
- }
- }
- var Graphics = function (ctx, width, height) {
- this.width = width
- this.height = height
- this.quiet = Math.round(this.width / 40)
- this.border_size = 0
- this.padding_width = 0
- this.area = {
- width: width - this.padding_width * 2 - this.quiet * 2,
- height: height - this.border_size * 2,
- top: this.border_size - 4,
- left: this.padding_width + this.quiet
- }
- this.ctx = ctx
- this.fg = '#000000'
- this.bg = '#ffffff'
-
- this.fillBgRect(0, 0, width, height)
-
- this.fillBgRect(0, this.border_size, width, height - this.border_size * 2)
- }
-
- Graphics.prototype._fillRect = function (x, y, width, height, color) {
- this.ctx.setFillStyle(color)
- this.ctx.fillRect(x, y, width, height)
- }
- Graphics.prototype.fillFgRect = function (x, y, width, height) {
- this._fillRect(x, y, width, height, this.fg)
- }
- Graphics.prototype.fillBgRect = function (x, y, width, height) {
- this._fillRect(x, y, width, height, this.bg)
- }
- var PATTERNS = [
- [2, 1, 2, 2, 2, 2, 0, 0],
- [2, 2, 2, 1, 2, 2, 0, 0],
- [2, 2, 2, 2, 2, 1, 0, 0],
- [1, 2, 1, 2, 2, 3, 0, 0],
- [1, 2, 1, 3, 2, 2, 0, 0],
- [1, 3, 1, 2, 2, 2, 0, 0],
- [1, 2, 2, 2, 1, 3, 0, 0],
- [1, 2, 2, 3, 1, 2, 0, 0],
- [1, 3, 2, 2, 1, 2, 0, 0],
- [2, 2, 1, 2, 1, 3, 0, 0],
- [2, 2, 1, 3, 1, 2, 0, 0],
- [2, 3, 1, 2, 1, 2, 0, 0],
- [1, 1, 2, 2, 3, 2, 0, 0],
- [1, 2, 2, 1, 3, 2, 0, 0],
- [1, 2, 2, 2, 3, 1, 0, 0],
- [1, 1, 3, 2, 2, 2, 0, 0],
- [1, 2, 3, 1, 2, 2, 0, 0],
- [1, 2, 3, 2, 2, 1, 0, 0],
- [2, 2, 3, 2, 1, 1, 0, 0],
- [2, 2, 1, 1, 3, 2, 0, 0],
- [2, 2, 1, 2, 3, 1, 0, 0],
- [2, 1, 3, 2, 1, 2, 0, 0],
- [2, 2, 3, 1, 1, 2, 0, 0],
- [3, 1, 2, 1, 3, 1, 0, 0],
- [3, 1, 1, 2, 2, 2, 0, 0],
- [3, 2, 1, 1, 2, 2, 0, 0],
- [3, 2, 1, 2, 2, 1, 0, 0],
- [3, 1, 2, 2, 1, 2, 0, 0],
- [3, 2, 2, 1, 1, 2, 0, 0],
- [3, 2, 2, 2, 1, 1, 0, 0],
- [2, 1, 2, 1, 2, 3, 0, 0],
- [2, 1, 2, 3, 2, 1, 0, 0],
- [2, 3, 2, 1, 2, 1, 0, 0],
- [1, 1, 1, 3, 2, 3, 0, 0],
- [1, 3, 1, 1, 2, 3, 0, 0],
- [1, 3, 1, 3, 2, 1, 0, 0],
-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