{"spec_id":"barcode-ean13","library":"matplotlib","language":"python","code":"\"\"\" anyplot.ai\nbarcode-ean13: EAN-13 Barcode\nLibrary: matplotlib 3.10.9 | Python 3.13.13\nQuality: 87/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.patches as patches\nimport matplotlib.pyplot as plt\n\n\n# Theme\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\"\n\n# EAN-13 encoding tables\nL_CODES = {\n    \"0\": \"0001101\",\n    \"1\": \"0011001\",\n    \"2\": \"0010011\",\n    \"3\": \"0111101\",\n    \"4\": \"0100011\",\n    \"5\": \"0110001\",\n    \"6\": \"0101111\",\n    \"7\": \"0111011\",\n    \"8\": \"0110111\",\n    \"9\": \"0001011\",\n}\nG_CODES = {\n    \"0\": \"0100111\",\n    \"1\": \"0110011\",\n    \"2\": \"0011011\",\n    \"3\": \"0100001\",\n    \"4\": \"0011101\",\n    \"5\": \"0111001\",\n    \"6\": \"0000101\",\n    \"7\": \"0010001\",\n    \"8\": \"0001001\",\n    \"9\": \"0010111\",\n}\nR_CODES = {\n    \"0\": \"1110010\",\n    \"1\": \"1100110\",\n    \"2\": \"1101100\",\n    \"3\": \"1000010\",\n    \"4\": \"1011100\",\n    \"5\": \"1001110\",\n    \"6\": \"1010000\",\n    \"7\": \"1000100\",\n    \"8\": \"1001000\",\n    \"9\": \"1110100\",\n}\nFIRST_DIGIT_PATTERN = {\n    \"0\": \"LLLLLL\",\n    \"1\": \"LLGLGG\",\n    \"2\": \"LLGGLG\",\n    \"3\": \"LLGGGL\",\n    \"4\": \"LGLLGG\",\n    \"5\": \"LGGLLG\",\n    \"6\": \"LGGGLL\",\n    \"7\": \"LGLGLG\",\n    \"8\": \"LGLGGL\",\n    \"9\": \"LGGLGL\",\n}\n\n# Data — 12-digit input; check digit auto-calculated\ninput_code = \"400638133393\"\ntotal = sum(int(d) * (1 if i % 2 == 0 else 3) for i, d in enumerate(input_code))\ncheck_digit = str((10 - (total % 10)) % 10)\nfull_code = input_code + check_digit  # → \"4006381333931\"\nean_formatted = f\"{full_code[0]} {full_code[1:7]} {full_code[7:]}\"  # \"4 006381 333931\"\n\n# Encode EAN-13 bit pattern\nlg_pattern = FIRST_DIGIT_PATTERN[full_code[0]]\nbarcode_pattern = \"101\"  # start guard\nfor i, digit in enumerate(full_code[1:7]):\n    barcode_pattern += L_CODES[digit] if lg_pattern[i] == \"L\" else G_CODES[digit]\nbarcode_pattern += \"01010\"  # center guard\nfor digit in full_code[7:13]:\n    barcode_pattern += R_CODES[digit]\nbarcode_pattern += \"101\"  # end guard\n\n# Guard bar bit indices: start (0–2), center (45–49), end (92–94)\nguard_positions = set(range(3)) | set(range(45, 50)) | set(range(92, 95))\n\n# Layout (data units — 1 unit = 1 module width)\nquiet_zone = 9\nstart_x = quiet_zone\nstart_y = 7.0\nbar_height = 14.0\nguard_height = 17.0\ntotal_modules = len(barcode_pattern)  # 95\n\n# Plot\nfig, ax = plt.subplots(figsize=(8, 4.5), dpi=400, facecolor=PAGE_BG)\nax.set_facecolor(PAGE_BG)\n\n# Draw barcode bars\nfor i, bit in enumerate(barcode_pattern):\n    if bit == \"1\":\n        is_guard = i in guard_positions\n        h = guard_height if is_guard else bar_height\n        y0 = start_y if is_guard else start_y + (guard_height - bar_height)\n        ax.add_patch(patches.Rectangle((start_x + i, y0), 1, h, linewidth=0, edgecolor=\"none\", facecolor=INK))\n\n# Style — human-readable digits below bars\ndigit_y = start_y - 0.5\ndigit_fs = 10\n\n# First digit — sits outside the left quiet zone\nax.text(\n    start_x - 3.5,\n    digit_y,\n    full_code[0],\n    fontsize=digit_fs,\n    ha=\"center\",\n    va=\"top\",\n    fontfamily=\"monospace\",\n    fontweight=\"bold\",\n    color=INK,\n)\n\n# Left-side digits (positions 1–6); each digit spans 7 modules, after 3-bit start guard\nfor i, digit in enumerate(full_code[1:7]):\n    dx = start_x + 3 + (i + 0.5) * 7\n    ax.text(\n        dx,\n        digit_y,\n        digit,\n        fontsize=digit_fs,\n        ha=\"center\",\n        va=\"top\",\n        fontfamily=\"monospace\",\n        fontweight=\"bold\",\n        color=INK,\n    )\n\n# Right-side digits (positions 7–12); start after start(3)+left(42)+center(5)=50 modules\nfor i, digit in enumerate(full_code[7:13]):\n    dx = start_x + 50 + (i + 0.5) * 7\n    ax.text(\n        dx,\n        digit_y,\n        digit,\n        fontsize=digit_fs,\n        ha=\"center\",\n        va=\"top\",\n        fontfamily=\"monospace\",\n        fontweight=\"bold\",\n        color=INK,\n    )\n\n# Section labels — barcode anatomy guide below digit row\n# Country (1–3): first digit at x=5.5, digits 2–3 at x=15.5, 22.5 → center=14.0\n# Manufacturer (4–8): left digits 4–7 at x=29.5..50.5, right digit 8 at x=62.5 → center=46.0\n# Product (9–12): right digits 9–12 at x=69.5..90.5 → center=80.0\n# Check (13): right digit 13 at x=97.5\nsections = [(\"Country (1–3)\", 14.0), (\"Manufacturer (4–8)\", 46.0), (\"Product (9–12)\", 80.0), (\"Check (13)\", 97.5)]\nfor label, cx in sections:\n    ax.text(cx, digit_y - 2.0, label, fontsize=7, ha=\"center\", va=\"top\", color=INK_SOFT)\n\n# Axis bounds and clean presentation\nx_max = start_x + total_modules + quiet_zone  # 9 + 95 + 9 = 113\ny_max = start_y + guard_height + 5.0\nax.set_xlim(0, x_max)\nax.set_ylim(3.5, y_max)\nax.axis(\"off\")\n\n# Title\nax.text(\n    start_x + total_modules / 2,\n    start_y + guard_height + 2.0,\n    \"barcode-ean13 · python · matplotlib · anyplot.ai\",\n    fontsize=12,\n    ha=\"center\",\n    va=\"bottom\",\n    fontweight=\"medium\",\n    color=INK,\n)\n\n# EAN-13 grouping format caption\nax.text(\n    start_x + total_modules / 2,\n    start_y + guard_height + 0.5,\n    ean_formatted,\n    fontsize=9,\n    ha=\"center\",\n    va=\"bottom\",\n    fontfamily=\"monospace\",\n    color=INK_SOFT,\n)\n\n# Save\nfig.subplots_adjust(left=0.03, right=0.97, top=0.97, bottom=0.03)\nplt.savefig(f\"plot-{THEME}.png\", dpi=400, facecolor=PAGE_BG)\n"}