{"spec_id":"barcode-code128","library":"muix","language":"javascript","code":"// anyplot.ai\n// barcode-code128: Code 128 Barcode\n// Library: muix 9.17.0 | JavaScript 22.23.4\n// Quality: 87/100 | Updated: 2026-11-08\nimport { BarChart } from \"@mui/x-charts/BarChart\";\n\nconst t = window.ANYPLOT_TOKENS;\n\n// Constants\n// Code 128 symbol patterns (ISO/IEC 15417): PATTERNS[v] is the 11-module bar (1) /\n// space (0) pattern of symbol value v; 103-105 are Start A/B/C, 106 is the 13-module Stop.\nconst PATTERNS = [\n  11011001100, 11001101100, 11001100110, 10010011000, 10010001100, 10001001100, 10011001000, 10011000100,\n  10001100100, 11001001000, 11001000100, 11000100100, 10110011100, 10011011100, 10011001110, 10111001100,\n  10011101100, 10011100110, 11001110010, 11001011100, 11001001110, 11011100100, 11001110100, 11101101110,\n  11101001100, 11100101100, 11100100110, 11101100100, 11100110100, 11100110010, 11011011000, 11011000110,\n  11000110110, 10100011000, 10001011000, 10001000110, 10110001000, 10001101000, 10001100010, 11010001000,\n  11000101000, 11000100010, 10110111000, 10110001110, 10001101110, 10111011000, 10111000110, 10001110110,\n  11101110110, 11010001110, 11000101110, 11011101000, 11011100010, 11011101110, 11101011000, 11101000110,\n  11100010110, 11101101000, 11101100010, 11100011010, 11101111010, 11001000010, 11110001010, 10100110000,\n  10100001100, 10010110000, 10010000110, 10000101100, 10000100110, 10110010000, 10110000100, 10011010000,\n  10011000010, 10000110100, 10000110010, 11000010010, 11001010000, 11110111010, 11000010100, 10001111010,\n  10100111100, 10010111100, 10010011110, 10111100100, 10011110100, 10011110010, 11110100100, 11110010100,\n  11110010010, 11011011110, 11011110110, 11110110110, 10101111000, 10100011110, 10001011110, 10111101000,\n  10111100010, 11110101000, 11110100010, 10111011110, 10111101110, 11101011110, 11110101110, 11010000100,\n  11010010000, 11010011100, 1100011101011,\n];\nconst START_B = 104;\nconst STOP = 106;\nconst CHECK_MODULUS = 103;\nconst SUBSET_B_OFFSET = 32; // a Subset B symbol value is the ASCII code minus 32\nconst QUIET_ZONE_MODULES = 10; // ISO/IEC 15417 minimum quiet zone on each side\n// A barcode keeps dark bars on a light ground in both themes so it still scans\nconst LABEL_BG = \"#FAF8F1\";\nconst BAR_INK = \"#1A1A17\";\nconst MAX_SYMBOL_SHARE = 0.86; // widest share of the canvas the symbol may take\nconst BAR_HEIGHT_SHARE = 0.5; // bar height as a share of the canvas height\n\n// Data: replace this block with your own parameters\nconst CONTENT = \"SHIP-2024-ABC123\"; // role: content\n\n// Plot: reads only the parameters and the constants above\nconst dataValues = Array.from(CONTENT, (ch) => ch.charCodeAt(0) - SUBSET_B_OFFSET);\nconst checkValue = dataValues.reduce((sum, v, i) => sum + v * (i + 1), START_B) % CHECK_MODULUS;\nconst symbolValues = [START_B, ...dataValues, checkValue, STOP];\nconst quietZone = \"0\".repeat(QUIET_ZONE_MODULES);\nconst bits = quietZone + symbolValues.map((v) => String(PATTERNS[v])).join(\"\") + quietZone;\nconst totalModules = bits.length;\n\n// Run-length segments: each bar or space becomes one stacked series, so every\n// width is a whole number of modules exactly as the encoding gives it\nconst segments: { modules: number; isBar: boolean }[] = [];\nfor (let i = 0; i < bits.length; ) {\n  let j = i;\n  while (j < bits.length && bits[j] === bits[i]) j += 1;\n  segments.push({ modules: j - i, isBar: bits[i] === \"1\" });\n  i = j;\n}\n\nconst { width, height } = window.ANYPLOT_SIZE;\n// Whole pixels per module keep every bar edge sharp\nconst modulePx = Math.floor((width * MAX_SYMBOL_SHARE) / totalModules);\nconst symbolWidth = modulePx * totalModules;\nconst barHeight = Math.round(height * BAR_HEIGHT_SHARE);\n\nconst TITLE = \"barcode-code128 · javascript · muix · anyplot.ai\";\nconst caption = `Code 128 Subset B · ${CONTENT.length} data characters · check value ${checkValue} · ${totalModules} modules incl. ${QUIET_ZONE_MODULES}-module quiet zones`;\n\nexport default function Chart() {\n  return (\n    <div\n      style={{\n        width,\n        height,\n        display: \"flex\",\n        flexDirection: \"column\",\n        alignItems: \"center\",\n        justifyContent: \"center\",\n        gap: 28,\n        background: t.pageBg,\n      }}\n    >\n      <div style={{ fontSize: 22, fontWeight: 600, color: t.ink }}>{TITLE}</div>\n      <div\n        style={{\n          background: LABEL_BG,\n          border: `1px solid ${t.grid}`,\n          borderRadius: 6,\n          padding: \"40px 0 28px\",\n          display: \"flex\",\n          flexDirection: \"column\",\n          alignItems: \"center\",\n        }}\n      >\n        <BarChart\n          width={symbolWidth}\n          height={barHeight}\n          layout=\"horizontal\"\n          skipAnimation\n          hideLegend\n          margin={{ top: 0, bottom: 0, left: 0, right: 0 }}\n          xAxis={[{ scaleType: \"linear\", min: 0, max: totalModules, position: \"none\" }]}\n          yAxis={[{ scaleType: \"band\", data: [CONTENT], categoryGapRatio: 0, position: \"none\" }]}\n          series={segments.map((seg, i) => ({\n            id: `segment-${i}`,\n            data: [seg.modules],\n            stack: \"symbol\",\n            color: seg.isBar ? BAR_INK : \"transparent\",\n          }))}\n          slotProps={{ tooltip: { trigger: \"none\" } }}\n        />\n        <div\n          style={{\n            marginTop: 22,\n            fontFamily: \"'JetBrains Mono', 'Courier New', monospace\",\n            fontSize: 30,\n            letterSpacing: \"0.35em\",\n            paddingLeft: \"0.35em\", // balances the spacing after the last character\n            color: BAR_INK,\n          }}\n        >\n          {CONTENT}\n        </div>\n      </div>\n      <div style={{ fontSize: 16, color: t.inkSoft }}>{caption}</div>\n    </div>\n  );\n}\n"}