{"spec_id":"barcode-code128","library":"highcharts","language":"javascript","code":"// anyplot.ai\n// barcode-code128: Code 128 Barcode\n// Library: highcharts 12.6.0 | JavaScript 22.23.2\n// Quality: 87/100 | Created: 2026-09-01\n\nconst t = window.ANYPLOT_TOKENS;\n\n// --- Code 128 encoding (Subset B, modulo-103 check digit) ------------------\n// Element-width table: 6 digits (bar,space,bar,space,bar,space) per symbol\n// value 0-102, plus Start A/B/C (103/104/105) and Stop (106, 7-digit\n// bar-space-bar-space-bar-space-bar). Digits are module counts (1-4 units).\nconst CODE128_WIDTHS = [\n  \"212222\", \"222122\", \"222221\", \"121223\", \"121322\", \"131222\", \"122213\",\n  \"122312\", \"132212\", \"221213\", \"221312\", \"231212\", \"112232\", \"122132\",\n  \"122231\", \"113222\", \"123122\", \"123221\", \"223211\", \"221132\", \"221231\",\n  \"213212\", \"223112\", \"312131\", \"311222\", \"321122\", \"321221\", \"312212\",\n  \"322112\", \"322211\", \"212123\", \"212321\", \"232121\", \"111323\", \"131123\",\n  \"131321\", \"112313\", \"132113\", \"132311\", \"211313\", \"231113\", \"231311\",\n  \"112133\", \"112331\", \"132131\", \"113123\", \"113321\", \"133121\", \"313121\",\n  \"211331\", \"231131\", \"213113\", \"213311\", \"213131\", \"311123\", \"311321\",\n  \"331121\", \"312113\", \"312311\", \"332111\", \"314111\", \"221411\", \"431111\",\n  \"111224\", \"111422\", \"121124\", \"121421\", \"141122\", \"141221\", \"112214\",\n  \"112412\", \"122114\", \"122411\", \"142112\", \"142211\", \"241211\", \"221114\",\n  \"413111\", \"241112\", \"134111\", \"111242\", \"121142\", \"121241\", \"114212\",\n  \"124112\", \"124211\", \"411212\", \"421112\", \"421211\", \"212141\", \"214121\",\n  \"412121\", \"111143\", \"111341\", \"131141\", \"114113\", \"114311\", \"411113\",\n  \"411311\", \"113141\", \"114131\", \"311141\", \"411131\", \"211412\", \"211214\",\n  \"211232\", \"2331112\",\n];\nconst START_B = 104;\nconst STOP = 106;\n\n// The barcode itself is encoded *data*, not chrome: it must stay high-contrast\n// black-on-white in both themes for scan reliability, so these are fixed\n// literals rather than theme tokens (only the title/label chrome uses t.ink).\nconst BARCODE_INK = \"#1A1A17\";\nconst BARCODE_BG = \"#FAF8F1\";\n\nconst content = \"SHIP-2024-ABC123\";\n\n// Subset B maps printable ASCII 32-126 to symbol values 0-94.\nconst values = content.split(\"\").map((c) => c.charCodeAt(0) - 32);\nconst checkDigit =\n  (START_B + values.reduce((sum, v, i) => sum + v * (i + 1), 0)) % 103;\nconst symbolValues = [START_B, ...values, checkDigit, STOP];\n\n// Expand each symbol's width digits into 1-unit modules (1 = bar, 0 = space).\n// Every pattern both starts and ends its digit count so that the alternation\n// carries over cleanly into the next symbol's bar.\nconst modules = [];\nsymbolValues.forEach((value) => {\n  let isBar = true;\n  for (const digit of CODE128_WIDTHS[value]) {\n    for (let i = 0; i < Number(digit); i++) modules.push(isBar ? 1 : 0);\n    isBar = !isBar;\n  }\n});\n\n// --- Chart -------------------------------------------------------------\nconst QUIET_ZONE = 10; // blank modules either side — Code 128 requires >=10x\nconst barData = modules.map((bit, i) => ({\n  x: i,\n  y: 1,\n  color: bit ? BARCODE_INK : \"transparent\",\n}));\n\nHighcharts.chart(\"container\", {\n  chart: {\n    type: \"column\",\n    backgroundColor: \"transparent\",\n    plotBackgroundColor: BARCODE_BG,\n    animation: false,\n    style: { fontFamily: \"inherit\" },\n  },\n  credits: { enabled: false },\n  title: {\n    text: \"barcode-code128 · javascript · highcharts · anyplot.ai\",\n    style: { color: t.ink, fontSize: \"22px\", fontWeight: \"600\" },\n  },\n  xAxis: {\n    min: -QUIET_ZONE,\n    max: modules.length - 1 + QUIET_ZONE,\n    lineWidth: 0,\n    tickLength: 0,\n    labels: { enabled: false },\n    title: {\n      text: content,\n      style: {\n        color: t.ink,\n        fontSize: \"20px\",\n        fontFamily: \"monospace\",\n        letterSpacing: \"4px\",\n      },\n      margin: 30,\n    },\n  },\n  yAxis: {\n    min: 0,\n    max: 1,\n    gridLineWidth: 0,\n    lineWidth: 0,\n    tickLength: 0,\n    labels: { enabled: false },\n    title: { text: null },\n  },\n  legend: { enabled: false },\n  tooltip: { enabled: false },\n  plotOptions: {\n    column: {\n      pointPadding: 0,\n      groupPadding: 0,\n      borderWidth: 0,\n      borderRadius: 0,\n      pointRange: 1,\n    },\n    series: { animation: false },\n  },\n  series: [{ name: \"Code 128 modules\", data: barData }],\n});\n"}