{"spec_id":"barcode-code128","library":"matplotlib","language":"python","code":"\"\"\" anyplot.ai\nbarcode-code128: Code 128 Barcode\nLibrary: matplotlib 3.10.9 | Python 3.13.13\nQuality: 86/100 | Updated: 2026-05-21\n\"\"\"\n\nimport os\nimport sys\n\n\nsys.path = sys.path[1:]  # prevent this file from shadowing the installed matplotlib package\n\nimport matplotlib.pyplot as plt\nimport numpy as np\nfrom matplotlib.colors import LinearSegmentedColormap\n\n\n# Theme tokens\nTHEME = os.getenv(\"ANYPLOT_THEME\", \"light\")\nPAGE_BG = \"#FAF8F1\" if THEME == \"light\" else \"#1A1A17\"\nINK = \"#1A1A17\" if THEME == \"light\" else \"#F0EFE8\"\nINK_SOFT = \"#4A4A44\" if THEME == \"light\" else \"#B8B7B0\"\nINK_MUTED = \"#6B6A63\" if THEME == \"light\" else \"#A8A79F\"\n\n# Code 128B encoding: each character → 6 bar/space widths (alternating)\nCODE128_B = {\n    \" \": [2, 1, 2, 2, 2, 2],\n    \"!\": [2, 2, 2, 1, 2, 2],\n    '\"': [2, 2, 2, 2, 2, 1],\n    \"#\": [1, 2, 1, 2, 2, 3],\n    \"$\": [1, 2, 1, 3, 2, 2],\n    \"%\": [1, 3, 1, 2, 2, 2],\n    \"&\": [1, 2, 2, 2, 1, 3],\n    \"'\": [1, 2, 2, 3, 1, 2],\n    \"(\": [1, 3, 2, 2, 1, 2],\n    \")\": [2, 2, 1, 2, 1, 3],\n    \"*\": [2, 2, 1, 3, 1, 2],\n    \"+\": [2, 3, 1, 2, 1, 2],\n    \",\": [1, 1, 2, 2, 3, 2],\n    \"-\": [1, 2, 2, 1, 3, 2],\n    \".\": [1, 2, 2, 2, 3, 1],\n    \"/\": [1, 1, 3, 2, 2, 2],\n    \"0\": [1, 2, 3, 1, 2, 2],\n    \"1\": [1, 2, 3, 2, 2, 1],\n    \"2\": [2, 2, 3, 2, 1, 1],\n    \"3\": [2, 2, 1, 1, 3, 2],\n    \"4\": [2, 2, 1, 2, 3, 1],\n    \"5\": [2, 1, 3, 2, 1, 2],\n    \"6\": [2, 2, 3, 1, 1, 2],\n    \"7\": [3, 1, 2, 1, 3, 1],\n    \"8\": [3, 1, 1, 2, 2, 2],\n    \"9\": [3, 2, 1, 1, 2, 2],\n    \":\": [3, 2, 1, 2, 2, 1],\n    \";\": [3, 1, 2, 2, 1, 2],\n    \"<\": [3, 2, 2, 1, 1, 2],\n    \"=\": [3, 2, 2, 2, 1, 1],\n    \">\": [2, 1, 2, 1, 2, 3],\n    \"?\": [2, 1, 2, 3, 2, 1],\n    \"@\": [2, 3, 2, 1, 2, 1],\n    \"A\": [1, 1, 1, 3, 2, 3],\n    \"B\": [1, 3, 1, 1, 2, 3],\n    \"C\": [1, 3, 1, 3, 2, 1],\n    \"D\": [1, 1, 2, 3, 1, 3],\n    \"E\": [1, 3, 2, 1, 1, 3],\n    \"F\": [1, 3, 2, 3, 1, 1],\n    \"G\": [2, 1, 1, 3, 1, 3],\n    \"H\": [2, 3, 1, 1, 1, 3],\n    \"I\": [2, 3, 1, 3, 1, 1],\n    \"J\": [1, 1, 2, 1, 3, 3],\n    \"K\": [1, 1, 2, 3, 3, 1],\n    \"L\": [1, 3, 2, 1, 3, 1],\n    \"M\": [1, 1, 3, 1, 2, 3],\n    \"N\": [1, 1, 3, 3, 2, 1],\n    \"O\": [1, 3, 3, 1, 2, 1],\n    \"P\": [3, 1, 3, 1, 2, 1],\n    \"Q\": [2, 1, 1, 3, 3, 1],\n    \"R\": [2, 3, 1, 1, 3, 1],\n    \"S\": [2, 1, 3, 1, 1, 3],\n    \"T\": [2, 1, 3, 3, 1, 1],\n    \"U\": [2, 1, 3, 1, 3, 1],\n    \"V\": [3, 1, 1, 1, 2, 3],\n    \"W\": [3, 1, 1, 3, 2, 1],\n    \"X\": [3, 3, 1, 1, 2, 1],\n    \"Y\": [3, 1, 2, 1, 1, 3],\n    \"Z\": [3, 1, 2, 3, 1, 1],\n    \"[\": [3, 3, 2, 1, 1, 1],\n    \"\\\\\": [3, 1, 4, 1, 1, 1],\n    \"]\": [2, 2, 1, 4, 1, 1],\n    \"^\": [4, 3, 1, 1, 1, 1],\n    \"_\": [1, 1, 1, 2, 2, 4],\n    \"`\": [1, 1, 1, 4, 2, 2],\n    \"a\": [1, 2, 1, 1, 2, 4],\n    \"b\": [1, 2, 1, 4, 2, 1],\n    \"c\": [1, 4, 1, 1, 2, 2],\n    \"d\": [1, 4, 1, 2, 2, 1],\n    \"e\": [1, 1, 2, 2, 1, 4],\n    \"f\": [1, 1, 2, 4, 1, 2],\n    \"g\": [1, 2, 2, 1, 1, 4],\n    \"h\": [1, 2, 2, 4, 1, 1],\n    \"i\": [1, 4, 2, 1, 1, 2],\n    \"j\": [1, 4, 2, 2, 1, 1],\n    \"k\": [2, 4, 1, 2, 1, 1],\n    \"l\": [2, 2, 1, 1, 1, 4],\n    \"m\": [4, 1, 3, 1, 1, 1],\n    \"n\": [2, 4, 1, 1, 1, 2],\n    \"o\": [1, 3, 4, 1, 1, 1],\n    \"p\": [1, 1, 1, 2, 4, 2],\n    \"q\": [1, 2, 1, 1, 4, 2],\n    \"r\": [1, 2, 1, 2, 4, 1],\n    \"s\": [1, 1, 4, 2, 1, 2],\n    \"t\": [1, 2, 4, 1, 1, 2],\n    \"u\": [1, 2, 4, 2, 1, 1],\n    \"v\": [4, 1, 1, 2, 1, 2],\n    \"w\": [4, 2, 1, 1, 1, 2],\n    \"x\": [4, 2, 1, 2, 1, 1],\n    \"y\": [2, 1, 2, 1, 4, 1],\n    \"z\": [2, 1, 4, 1, 2, 1],\n    \"{\": [4, 1, 2, 1, 2, 1],\n    \"|\": [1, 1, 1, 1, 4, 3],\n    \"}\": [1, 1, 1, 3, 4, 1],\n    \"~\": [1, 3, 1, 1, 4, 1],\n}\n\nCODE128_B_VALUES = {\n    char: i\n    for i, char in enumerate(\n        \" !\\\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\\\]^_`abcdefghijklmnopqrstuvwxyz{|}~\"\n    )\n}\n\nSTART_B = [2, 1, 1, 4, 1, 2]\nSTART_B_VALUE = 104\nSTOP = [2, 3, 3, 1, 1, 1, 2]\n\n# Checksum symbol patterns (indices 0–102 for modulo-103 calculation)\nCHECKSUM_PATTERNS = [\n    [2, 1, 2, 2, 2, 2],\n    [2, 2, 2, 1, 2, 2],\n    [2, 2, 2, 2, 2, 1],\n    [1, 2, 1, 2, 2, 3],\n    [1, 2, 1, 3, 2, 2],\n    [1, 3, 1, 2, 2, 2],\n    [1, 2, 2, 2, 1, 3],\n    [1, 2, 2, 3, 1, 2],\n    [1, 3, 2, 2, 1, 2],\n    [2, 2, 1, 2, 1, 3],\n    [2, 2, 1, 3, 1, 2],\n    [2, 3, 1, 2, 1, 2],\n    [1, 1, 2, 2, 3, 2],\n    [1, 2, 2, 1, 3, 2],\n    [1, 2, 2, 2, 3, 1],\n    [1, 1, 3, 2, 2, 2],\n    [1, 2, 3, 1, 2, 2],\n    [1, 2, 3, 2, 2, 1],\n    [2, 2, 3, 2, 1, 1],\n    [2, 2, 1, 1, 3, 2],\n    [2, 2, 1, 2, 3, 1],\n    [2, 1, 3, 2, 1, 2],\n    [2, 2, 3, 1, 1, 2],\n    [3, 1, 2, 1, 3, 1],\n    [3, 1, 1, 2, 2, 2],\n    [3, 2, 1, 1, 2, 2],\n    [3, 2, 1, 2, 2, 1],\n    [3, 1, 2, 2, 1, 2],\n    [3, 2, 2, 1, 1, 2],\n    [3, 2, 2, 2, 1, 1],\n    [2, 1, 2, 1, 2, 3],\n    [2, 1, 2, 3, 2, 1],\n    [2, 3, 2, 1, 2, 1],\n    [1, 1, 1, 3, 2, 3],\n    [1, 3, 1, 1, 2, 3],\n    [1, 3, 1, 3, 2, 1],\n    [1, 1, 2, 3, 1, 3],\n    [1, 3, 2, 1, 1, 3],\n    [1, 3, 2, 3, 1, 1],\n    [2, 1, 1, 3, 1, 3],\n    [2, 3, 1, 1, 1, 3],\n    [2, 3, 1, 3, 1, 1],\n    [1, 1, 2, 1, 3, 3],\n    [1, 1, 2, 3, 3, 1],\n    [1, 3, 2, 1, 3, 1],\n    [1, 1, 3, 1, 2, 3],\n    [1, 1, 3, 3, 2, 1],\n    [1, 3, 3, 1, 2, 1],\n    [3, 1, 3, 1, 2, 1],\n    [2, 1, 1, 3, 3, 1],\n    [2, 3, 1, 1, 3, 1],\n    [2, 1, 3, 1, 1, 3],\n    [2, 1, 3, 3, 1, 1],\n    [2, 1, 3, 1, 3, 1],\n    [3, 1, 1, 1, 2, 3],\n    [3, 1, 1, 3, 2, 1],\n    [3, 3, 1, 1, 2, 1],\n    [3, 1, 2, 1, 1, 3],\n    [3, 1, 2, 3, 1, 1],\n    [3, 3, 2, 1, 1, 1],\n    [3, 1, 4, 1, 1, 1],\n    [2, 2, 1, 4, 1, 1],\n    [4, 3, 1, 1, 1, 1],\n    [1, 1, 1, 2, 2, 4],\n    [1, 1, 1, 4, 2, 2],\n    [1, 2, 1, 1, 2, 4],\n    [1, 2, 1, 4, 2, 1],\n    [1, 4, 1, 1, 2, 2],\n    [1, 4, 1, 2, 2, 1],\n    [1, 1, 2, 2, 1, 4],\n    [1, 1, 2, 4, 1, 2],\n    [1, 2, 2, 1, 1, 4],\n    [1, 2, 2, 4, 1, 1],\n    [1, 4, 2, 1, 1, 2],\n    [1, 4, 2, 2, 1, 1],\n    [2, 4, 1, 2, 1, 1],\n    [2, 2, 1, 1, 1, 4],\n    [4, 1, 3, 1, 1, 1],\n    [2, 4, 1, 1, 1, 2],\n    [1, 3, 4, 1, 1, 1],\n    [1, 1, 1, 2, 4, 2],\n    [1, 2, 1, 1, 4, 2],\n    [1, 2, 1, 2, 4, 1],\n    [1, 1, 4, 2, 1, 2],\n    [1, 2, 4, 1, 1, 2],\n    [1, 2, 4, 2, 1, 1],\n    [4, 1, 1, 2, 1, 2],\n    [4, 2, 1, 1, 1, 2],\n    [4, 2, 1, 2, 1, 1],\n    [2, 1, 2, 1, 4, 1],\n    [2, 1, 4, 1, 2, 1],\n    [4, 1, 2, 1, 2, 1],\n    [1, 1, 1, 1, 4, 3],\n    [1, 1, 1, 3, 4, 1],\n    [1, 3, 1, 1, 4, 1],\n    [1, 1, 4, 1, 1, 3],\n    [1, 1, 4, 3, 1, 1],\n    [4, 1, 1, 1, 1, 3],\n    [4, 1, 1, 3, 1, 1],\n    [1, 1, 3, 1, 4, 1],\n    [1, 1, 4, 1, 3, 1],\n    [3, 1, 1, 1, 4, 1],\n    [4, 1, 1, 1, 3, 1],\n    [2, 1, 1, 4, 1, 2],\n    [2, 1, 1, 2, 1, 4],\n    [2, 1, 1, 2, 3, 2],\n]\n\n# Data\ncontent = \"SHIP-2024-ABC123\"\n\n# Encode: start + data + checksum + stop\nbars = list(START_B)\nchecksum = START_B_VALUE\nfor i, char in enumerate(content):\n    pattern = CODE128_B.get(char, CODE128_B[\" \"])\n    bars.extend(pattern)\n    checksum += CODE128_B_VALUES.get(char, 0) * (i + 1)\n\nbars.extend(CHECKSUM_PATTERNS[checksum % 103])\nbars.extend(STOP)\n\n# Convert bar widths to binary (1=bar, 0=space)\nbinary_pattern = []\nis_bar = True\nfor width in bars:\n    binary_pattern.extend([1 if is_bar else 0] * width)\n    is_bar = not is_bar\n\n# Layout\nbarcode_height = 200\nquiet_zone = 30\nfull_pattern = [0] * quiet_zone + binary_pattern + [0] * quiet_zone\nbarcode_width = len(full_pattern)\nbarcode_array = np.array([full_pattern] * barcode_height)\n\n# Zone boundaries for structural annotations\n# Code 128 standard: every symbol (except STOP) = 11 modules; STOP = 13 modules\n_sw = 11\n_stop_w = sum(STOP)  # 13\n_qz0_end = quiet_zone\n_start_end = _qz0_end + _sw\n_data_end = _start_end + len(content) * _sw\n_check_end = _data_end + _sw\n_stop_end = _check_end + _stop_w\n\nzones = [\n    (_qz0_end - quiet_zone, _qz0_end, \"Quiet Zone\"),\n    (_qz0_end, _start_end, \"Start\"),\n    (_start_end, _data_end, f\"Data · Code B ({len(content)} chars)\"),\n    (_data_end, _check_end, \"Check\"),\n    (_check_end, _stop_end, \"Stop\"),\n    (_stop_end, barcode_width, \"Quiet Zone\"),\n]\n\n# Plot — landscape canvas: figsize=(8, 4.5) × dpi=400 → 3200×1800 px\nfig, ax = plt.subplots(figsize=(8, 4.5), dpi=400, facecolor=PAGE_BG)\nax.set_facecolor(PAGE_BG)\n\n# Theme-adaptive colormap: 0 (space) → PAGE_BG, 1 (bar) → INK\nbar_cmap = LinearSegmentedColormap.from_list(\"barcode_cmap\", [PAGE_BG, INK], N=2)\nax.imshow(barcode_array, cmap=bar_cmap, aspect=\"auto\", interpolation=\"nearest\", vmin=0, vmax=1)\n\nax.set_xticks([])\nax.set_yticks([])\n\n# Structural zone annotations above the barcode\ny_tick_top = 0  # tick mark at barcode top edge\ny_bracket = -9  # horizontal bracket line\ny_label = -20  # zone label center\n\nfor x0, x1, label in zones:\n    cx = (x0 + x1) / 2\n    ax.plot([x0, x1], [y_bracket, y_bracket], color=INK_MUTED, lw=0.6, solid_capstyle=\"butt\")\n    ax.plot([x0, x0], [y_tick_top, y_bracket], color=INK_MUTED, lw=0.6)\n    ax.plot([x1, x1], [y_tick_top, y_bracket], color=INK_MUTED, lw=0.6)\n    ax.text(cx, y_label, label, fontsize=6, ha=\"center\", va=\"center\", color=INK_MUTED, fontfamily=\"monospace\")\n\n# Human-readable text below barcode\nax.text(\n    barcode_width / 2,\n    barcode_height + 28,\n    content,\n    fontsize=16,\n    ha=\"center\",\n    va=\"top\",\n    fontfamily=\"monospace\",\n    fontweight=\"bold\",\n    color=INK,\n)\n\nax.set_xlim(-10, barcode_width + 10)\nax.set_ylim(barcode_height + 75, -38)\n\nfor spine in ax.spines.values():\n    spine.set_visible(False)\n\n# Style\nax.set_title(\"barcode-code128 · python · matplotlib · anyplot.ai\", fontsize=12, fontweight=\"medium\", color=INK, pad=8)\nax.tick_params(colors=INK_SOFT)\n\nfig.subplots_adjust(left=0.02, right=0.98, top=0.93, bottom=0.04)\n\n# Save\nplt.savefig(f\"plot-{THEME}.png\", dpi=400, facecolor=PAGE_BG)\n"}