|
33 | 33 | import { bwipp_auspost,bwipp_azteccode,bwipp_azteccodecompact,bwipp_aztecrune,bwipp_bc412,bwipp_channelcode,bwipp_codablockf,bwipp_code11,bwipp_code128,bwipp_code16k,bwipp_code2of5,bwipp_code32,bwipp_code39,bwipp_code39ext,bwipp_code49,bwipp_code93,bwipp_code93ext,bwipp_codeone,bwipp_coop2of5,bwipp_daft,bwipp_databarexpanded,bwipp_databarexpandedcomposite,bwipp_databarexpandedstacked,bwipp_databarexpandedstackedcomposite,bwipp_databarlimited,bwipp_databarlimitedcomposite,bwipp_databaromni,bwipp_databaromnicomposite,bwipp_databarstacked,bwipp_databarstackedcomposite,bwipp_databarstackedomni,bwipp_databarstackedomnicomposite,bwipp_databartruncated,bwipp_databartruncatedcomposite,bwipp_datalogic2of5,bwipp_datamatrix,bwipp_datamatrixrectangular,bwipp_datamatrixrectangularextension,bwipp_dotcode,bwipp_ean13,bwipp_ean13composite,bwipp_ean14,bwipp_ean2,bwipp_ean5,bwipp_ean8,bwipp_ean8composite,bwipp_flattermarken,bwipp_gs1_128,bwipp_gs1_128composite,bwipp_gs1_cc,bwipp_gs1datamatrix,bwipp_gs1datamatrixrectangular,bwipp_gs1dldatamatrix,bwipp_gs1dlqrcode,bwipp_gs1dotcode,bwipp_gs1northamericancoupon,bwipp_gs1qrcode,bwipp_hanxin,bwipp_hibcazteccode,bwipp_hibccodablockf,bwipp_hibccode128,bwipp_hibccode39,bwipp_hibcdatamatrix,bwipp_hibcdatamatrixrectangular,bwipp_hibcmicropdf417,bwipp_hibcpdf417,bwipp_hibcqrcode,bwipp_iata2of5,bwipp_identcode,bwipp_industrial2of5,bwipp_interleaved2of5,bwipp_isbn,bwipp_ismn,bwipp_issn,bwipp_itf14,bwipp_jabcode,bwipp_japanpost,bwipp_kix,bwipp_leitcode,bwipp_mailmark,bwipp_mands,bwipp_matrix2of5,bwipp_maxicode,bwipp_micropdf417,bwipp_microqrcode,bwipp_msi,bwipp_onecode,bwipp_pdf417,bwipp_pdf417compact,bwipp_pharmacode,bwipp_pharmacode2,bwipp_planet,bwipp_plessey,bwipp_posicode,bwipp_postnet,bwipp_pzn,bwipp_qrcode,bwipp_rationalizedCodabar,bwipp_raw,bwipp_rectangularmicroqrcode,bwipp_royalmail,bwipp_sscc18,bwipp_swissqrcode,bwipp_symbol,bwipp_telepen,bwipp_telepennumeric,bwipp_ultracode,bwipp_upca,bwipp_upcacomposite,bwipp_upce,bwipp_upcecomposite,bwipp_lookup,bwipp_encode,BWIPP_VERSION } from './bwipp.mjs'; |
34 | 34 |
|
35 | 35 | // exports.js |
36 | | -const BWIPJS_VERSION = '4.6.0 (2025-04-20)'; |
| 36 | +const BWIPJS_VERSION = '4.7.0 (2025-07-01)'; |
37 | 37 |
|
38 | 38 |
|
39 | 39 | // bwipjs.toSVG(options) |
@@ -788,6 +788,24 @@ BWIPJS.prototype.clip = function() { |
788 | 788 | }); |
789 | 789 | }; |
790 | 790 |
|
| 791 | +// Our replacement for the renmatrix drawlayer functionality. |
| 792 | +BWIPJS.prototype.drawlayer = function(pix, width, height) { |
| 793 | + // The pix array is in y-inverted postscript orientation. |
| 794 | + let paths = tracepaths(pix, width, height); |
| 795 | + |
| 796 | + this.newpath(); |
| 797 | + for (let i = 0, il = paths.length; i < il; i++) { |
| 798 | + let path = paths[i]; |
| 799 | + this.moveto(path[0][0], path[0][1]); |
| 800 | + for (let j = 1, jl = path.length; j < jl; j++) { |
| 801 | + let pt = path[j]; |
| 802 | + this.lineto(pt[0], pt[1]); |
| 803 | + } |
| 804 | + this.closepath(); |
| 805 | + } |
| 806 | + this.fill(); |
| 807 | +}; |
| 808 | + |
791 | 809 | // The pix array is in standard (not y-inverted postscript) orientation. |
792 | 810 | BWIPJS.prototype.showmaxicode = function(pix) { |
793 | 811 | var tsx = this.g_tsx; |
@@ -967,6 +985,177 @@ BWIPJS.prototype.render = function() { |
967 | 985 |
|
968 | 986 | return BWIPJS; |
969 | 987 | })(); // BWIPJS closure |
| 988 | +// 4-connected path tracing |
| 989 | + |
| 990 | +function tracepaths(pixs, width, height, inkspreadh, inkspreadv) { |
| 991 | + let dx = inkspreadh||0; |
| 992 | + let dy = inkspreadv||0; |
| 993 | + |
| 994 | + // The pixs array is in y-inverted postscript orientation. |
| 995 | + // Convert to an array of arrays. |
| 996 | + let grid = new Array(height+1); |
| 997 | + let yoff = 0; |
| 998 | + for (let y = height-1; y >= 0; y--) { |
| 999 | + let row = new Uint8Array(width); |
| 1000 | + for (let x = 0; x < width; x++) { |
| 1001 | + row[x] = pixs[yoff + x] ? 1 : 0; |
| 1002 | + } |
| 1003 | + grid[y] = row; |
| 1004 | + yoff += width; |
| 1005 | + } |
| 1006 | + // Add a row before/after so we can blindly access the grid. |
| 1007 | + // We don't need actual zero-padding around the pixels. |
| 1008 | + // All accesses are "cast to integer" with bit-&, so undefined |
| 1009 | + // get converted to zero. |
| 1010 | + grid[-1] = []; |
| 1011 | + grid[height] = []; |
| 1012 | + |
| 1013 | + let paths = []; |
| 1014 | + for (let y = 0; y < height; y++) { |
| 1015 | + let last = 0; |
| 1016 | + for (let x = 0; x < width; x++) { |
| 1017 | + // The last&9 verifies the edge was not traced ccw (dir == 8). |
| 1018 | + if ((last&9) == 0 && grid[y][x] == 1) { |
| 1019 | + paths.push(tracecw(x, y, [])); // clockwise for outside |
| 1020 | + // The last&5 verifies the edge was not traced cw (dir == 4). |
| 1021 | + } else if ((last&5) == 1 && grid[y][x] == 0) { |
| 1022 | + paths.push(traceccw(x, y, [])); // counter-clockwise for inside |
| 1023 | + } |
| 1024 | + last = grid[y][x]; |
| 1025 | + } |
| 1026 | + } |
| 1027 | + return paths; |
| 1028 | + |
| 1029 | + // Trace outside edges clockwise |
| 1030 | + function tracecw(x, y, path) { |
| 1031 | + path.push([ x+dx, y+dy ]); |
| 1032 | + |
| 1033 | + // 2 == top edge |
| 1034 | + // 4 == right edge |
| 1035 | + // 2 == top edge |
| 1036 | + // 4 == right edge |
| 1037 | + // 8 == bottom edge |
| 1038 | + // 16 == left edge |
| 1039 | + let dir = 2; |
| 1040 | + for (;;) { |
| 1041 | + if (grid[y][x] & dir) { |
| 1042 | + path.pop(); |
| 1043 | + return path; |
| 1044 | + } |
| 1045 | + grid[y][x] |= dir; |
| 1046 | + |
| 1047 | + if (dir == 2) { // top edge rightward |
| 1048 | + if (grid[y][x+1] & 1) { |
| 1049 | + if (grid[y-1][x+1] & 1) { |
| 1050 | + path.push([ x+1-dx, y+dy, 'H' ]); |
| 1051 | + dir = 16; |
| 1052 | + } |
| 1053 | + x++; |
| 1054 | + } else { |
| 1055 | + path.push([ x+1-dx, y+dy, 'H' ]); |
| 1056 | + dir = 4; |
| 1057 | + } |
| 1058 | + } else if (dir == 4) { // right edge downward |
| 1059 | + if (grid[y+1][x] & 1) { |
| 1060 | + if (grid[y+1][x+1] & 1) { |
| 1061 | + path.push([ x+1-dx, y+1-dy, 'V' ]); |
| 1062 | + dir = 2; |
| 1063 | + } |
| 1064 | + y++; |
| 1065 | + } else { |
| 1066 | + path.push([ x+1-dx, y+1-dy, 'V' ]); |
| 1067 | + dir = 8; |
| 1068 | + } |
| 1069 | + } else if (dir == 8) { // bottom edge leftward |
| 1070 | + if (grid[y][x-1] & 1) { |
| 1071 | + if (grid[y+1][x-1] & 1) { |
| 1072 | + path.push([ x+dx, y+1-dy, 'H' ]); |
| 1073 | + dir = 4; |
| 1074 | + } |
| 1075 | + x--; |
| 1076 | + } else { |
| 1077 | + path.push([ x+dx, y+1-dy, 'H' ]); |
| 1078 | + dir = 16; |
| 1079 | + } |
| 1080 | + } else { // left edge upward |
| 1081 | + if (grid[y-1][x] & 1) { |
| 1082 | + if (grid[y-1][x-1] & 1) { |
| 1083 | + path.push([ x+dx, y+dy, 'V' ]); |
| 1084 | + dir = 8; |
| 1085 | + } |
| 1086 | + y--; |
| 1087 | + } else { |
| 1088 | + path.push([ x+dx, y+dy, 'V' ]); |
| 1089 | + dir = 2; |
| 1090 | + } |
| 1091 | + } |
| 1092 | + } |
| 1093 | + } |
| 1094 | + |
| 1095 | + // Trace inside edges counter clockwise |
| 1096 | + function traceccw(x, y, path) { |
| 1097 | + path.push([ x-dx, y-dy ]); |
| 1098 | + |
| 1099 | + // 2 == left edge |
| 1100 | + // 4 == bottom edge |
| 1101 | + // 8 == right edge |
| 1102 | + // 16 == top edge |
| 1103 | + let dir = 2; |
| 1104 | + for (;;) { |
| 1105 | + if (grid[y][x] & dir) { |
| 1106 | + path.pop(); |
| 1107 | + return path; |
| 1108 | + } |
| 1109 | + grid[y][x] |= dir; |
| 1110 | + |
| 1111 | + if (dir == 2) { // left edge downward |
| 1112 | + if ((grid[y+1][x] & 1) == 0) { |
| 1113 | + if ((grid[y+1][x-1] & 1) == 0) { |
| 1114 | + path.push([ x-dx, y+1+dy ]); |
| 1115 | + dir = 16; |
| 1116 | + } |
| 1117 | + y++; |
| 1118 | + } else { |
| 1119 | + path.push([ x-dx, y+1+dy ]); |
| 1120 | + dir = 4; |
| 1121 | + } |
| 1122 | + } else if (dir == 4) { // bottom edge rightward |
| 1123 | + if ((grid[y][x+1] & 1) == 0) { |
| 1124 | + if ((grid[y+1][x+1] & 1) == 0) { |
| 1125 | + path.push([ x+1+dx, y+1+dy ]); |
| 1126 | + dir = 2; |
| 1127 | + } |
| 1128 | + x++; |
| 1129 | + } else { |
| 1130 | + path.push([ x+1+dx, y+1+dy ]); |
| 1131 | + dir = 8; |
| 1132 | + } |
| 1133 | + } else if (dir == 8) { // right edge upward |
| 1134 | + if ((grid[y-1][x] & 1) == 0) { |
| 1135 | + if ((grid[y-1][x+1] & 1) == 0) { |
| 1136 | + path.push([ x+1+dx, y-dy ]); |
| 1137 | + dir = 4; |
| 1138 | + } |
| 1139 | + y--; |
| 1140 | + } else { |
| 1141 | + path.push([ x+1+dx, y-dy ]); |
| 1142 | + dir = 16; |
| 1143 | + } |
| 1144 | + } else { // top edge leftward |
| 1145 | + if ((grid[y][x-1] & 1) == 0) { |
| 1146 | + if ((grid[y-1][x-1] & 1) == 0) { |
| 1147 | + path.push([ x-dx, y-dy ]); |
| 1148 | + dir = 8; |
| 1149 | + } |
| 1150 | + x--; |
| 1151 | + } else { |
| 1152 | + path.push([ x-dx, y-dy ]); |
| 1153 | + dir = 2; |
| 1154 | + } |
| 1155 | + } |
| 1156 | + } |
| 1157 | + } |
| 1158 | +} |
970 | 1159 | // drawing-builtin.js |
971 | 1160 | // |
972 | 1161 | // The aliased (except the fonts) graphics used by drawing-canvas.js and |
|
0 commit comments