{"spec_id":"barcode-code128","library":"ggplot2","language":"r","code":"#' anyplot.ai\n#' barcode-code128: Code 128 Barcode\n#' Library: ggplot2 3.5.1 | R 4.4.1\n#' Quality: 86/100 | Created: 2026-05-21\n\nlibrary(ggplot2)\nlibrary(ragg)\n\n# --- Theme tokens -----------------------------------------------------------\nTHEME       <- Sys.getenv(\"ANYPLOT_THEME\", \"light\")\nPAGE_BG     <- if (THEME == \"light\") \"#FAF8F1\" else \"#1A1A17\"\nINK         <- if (THEME == \"light\") \"#1A1A17\" else \"#F0EFE8\"\nINK_SOFT    <- if (THEME == \"light\") \"#4A4A44\" else \"#B8B7B0\"\nINK_MUTED   <- if (THEME == \"light\") \"#6B6A63\" else \"#A8A79F\"\n\n# --- Code 128 Subset B patterns (values 0-106 in order, stop appended) -----\n# Each string: 6 digit widths (bar-space-bar-space-bar-space), 11 modules each\n# Stop pattern: 7 digit widths, 13 modules\nCODE128 <- c(\n  \"212222\",\"222122\",\"222221\",\"121223\",\"121322\",\"131222\",\"122213\",\"122312\",\n  \"132212\",\"221213\",\"221312\",\"231212\",\"112232\",\"122132\",\"122231\",\"113222\",\n  \"123122\",\"123221\",\"223211\",\"221132\",\"221231\",\"213212\",\"223112\",\"312131\",\n  \"311222\",\"321122\",\"321221\",\"312212\",\"322112\",\"322211\",\"212123\",\"212321\",\n  \"232121\",\"111323\",\"131123\",\"131321\",\"112313\",\"132113\",\"132311\",\"211313\",\n  \"231113\",\"231311\",\"112133\",\"112331\",\"132131\",\"113123\",\"113321\",\"133121\",\n  \"313121\",\"211331\",\"231131\",\"213113\",\"213311\",\"213131\",\"311123\",\"311321\",\n  \"331121\",\"312113\",\"312311\",\"332111\",\"314111\",\"221411\",\"431111\",\"111224\",\n  \"111422\",\"121124\",\"121421\",\"141122\",\"141221\",\"112214\",\"112412\",\"122114\",\n  \"122411\",\"142112\",\"142211\",\"241211\",\"221114\",\"413111\",\"241112\",\"134111\",\n  \"111242\",\"121142\",\"121241\",\"114212\",\"124112\",\"124211\",\"411212\",\"421112\",\n  \"421211\",\"212141\",\"214121\",\"412121\",\"111143\",\"111341\",\"131141\",\"114113\",\n  \"114311\",\"411113\",\"411311\",\"113141\",\"114131\",\"311141\",\"411131\",\"211412\",\n  \"211214\",\"211232\",\n  \"2331112\"  # stop symbol (index 108 in R / code value 106+stop)\n)\n\n# --- Encode a string using Code 128 Subset B --------------------------------\n# Returns vector of pattern strings (data + check + stop)\nencode_code128b <- function(text) {\n  chars     <- strsplit(text, \"\")[[1]]\n  code_vals <- vapply(chars, function(ch) utf8ToInt(ch) - 32L, integer(1L))\n  start_val <- 104L  # Start B\n  check_val <- (start_val + sum(seq_along(code_vals) * code_vals)) %% 103L\n  all_vals  <- c(start_val, code_vals, check_val)\n  c(CODE128[all_vals + 1L], CODE128[107L])  # patterns + stop (stop is at index 107)\n}\n\n# --- Build rectangle data frame from encoded patterns -----------------------\npatterns_to_df <- function(patterns, quiet_modules = 10L) {\n  x    <- quiet_modules\n  rows <- list()\n  for (pat in patterns) {\n    widths <- as.integer(strsplit(pat, \"\")[[1]])\n    for (i in seq_along(widths)) {\n      if (i %% 2L == 1L) {  # odd index = bar (black)\n        rows[[length(rows) + 1L]] <- c(x, x + widths[i])\n      }\n      x <- x + widths[i]\n    }\n  }\n  total_w <- x + quiet_modules\n  mat <- do.call(rbind, rows)\n  df  <- data.frame(xmin = mat[, 1], xmax = mat[, 2], ymin = 0, ymax = 1)\n  list(df = df, total_w = total_w)\n}\n\n# --- Data -------------------------------------------------------------------\nset.seed(42)\nlabel_text <- \"SPEC-LAB-2024-X789\"\npatterns   <- encode_code128b(label_text)\nbarcode    <- patterns_to_df(patterns)\nbar_df     <- barcode$df\ntotal_w    <- barcode$total_w\n\n# White barcode background (always white for scan reliability)\nbg_rect <- data.frame(xmin = 0, xmax = total_w, ymin = 0, ymax = 1)\n\nx_pad <- total_w * 0.05\n\n# --- Plot -------------------------------------------------------------------\np <- ggplot() +\n  geom_rect(\n    data = bg_rect,\n    aes(xmin = xmin, xmax = xmax, ymin = ymin, ymax = ymax),\n    fill = \"white\", color = NA\n  ) +\n  geom_rect(\n    data = bar_df,\n    aes(xmin = xmin, xmax = xmax, ymin = ymin, ymax = ymax),\n    fill = \"black\", color = NA\n  ) +\n  # Subtle frame around barcode to anchor it visually\n  geom_rect(\n    data = bg_rect,\n    aes(xmin = xmin, xmax = xmax, ymin = ymin, ymax = ymax),\n    fill = NA, color = INK_SOFT, linewidth = 0.5\n  ) +\n  annotate(\n    \"text\",\n    x = total_w / 2, y = -0.26,\n    label    = label_text,\n    size     = 6.5,\n    family   = \"mono\",\n    color    = INK,\n    fontface = \"plain\"\n  ) +\n  annotate(\n    \"text\",\n    x = total_w / 2, y = 1.30,\n    label    = \"Specimen Label — Healthcare Lab Tracking\",\n    size     = 4.5,\n    color    = INK_SOFT,\n    fontface = \"italic\"\n  ) +\n  labs(\n    title   = \"barcode-code128 · r · ggplot2 · anyplot.ai\",\n    caption = \"Code 128 Subset B · Mod-103 check digit · Quiet zones: 10× module width\"\n  ) +\n  coord_cartesian(\n    xlim   = c(-x_pad, total_w + x_pad),\n    ylim   = c(-0.55, 1.50),\n    expand = FALSE,\n    clip   = \"off\"\n  ) +\n  theme_void() +\n  theme(\n    plot.background  = element_rect(fill = PAGE_BG, color = PAGE_BG),\n    panel.background = element_rect(fill = PAGE_BG, color = NA),\n    plot.title       = element_text(\n      color  = INK,\n      size   = 12,\n      hjust  = 0.5,\n      face   = \"bold\",\n      margin = margin(b = 10, t = 8)\n    ),\n    plot.caption     = element_text(\n      color  = INK_MUTED,\n      size   = 7,\n      hjust  = 0.5,\n      margin = margin(t = 6, b = 4)\n    ),\n    plot.margin = margin(30, 80, 20, 80)\n  )\n\n# --- Save -------------------------------------------------------------------\nggsave(\n  filename = sprintf(\"plot-%s.png\", THEME),\n  plot     = p,\n  device   = ragg::agg_png,\n  width    = 8,\n  height   = 4.5,\n  units    = \"in\",\n  dpi      = 400\n)\n"}