moonbarcode

纯 MoonBit 原生的一维条码(1D barcode)生成、校验与渲染工具库:EAN-13 / EAN-8 / UPC-A / Code 39 / Code 93 / Code 128 / ITF-14 编码、ISBN / ISSN / GTIN 校验、SVG / ASCII / ANSI 渲染,可编译为 native / wasm / wasm-gc / js 多后端。

barcode
ean
upc
code128
code39
code93
isbn
itf
gs1
svg
wasm
moon add fan-ere/moonbarcode@0.1.0
Download zip
Author
Version
0.1.0
License
Apache-2.0
Last updated
7 hours ago
Downloads
2

Dependencies

README

#moonbarcode

CI

纯 MoonBit 原生的一维条码(1D barcode)生成、校验与渲染工具库。

目标:补足 MoonBit 生态中一维条码能力的空白(MoonBit 生态目前只有二维 QR 码 实现 qrc),为零售、物流、 图书、库存、电商标签等场景提供可复用、可编译到多后端的条码基础组件。

#支持的符号体系(Symbologies)

  • 零售:EAN-13、EAN-8、UPC-A
  • 物流与工业:Code 39(含 mod-43 校验)、Code 93(C/K 双校验 + Full ASCII)、Code 128(A/B/C 子集自动优化 + GS1-128)
  • 图书与出版物校验:ISBN-10 / ISBN-13、ISSN、GTIN-14

#渲染

  • SVG:适合打印与网页
  • ASCII / ANSI:适合终端调试与测试

#快速开始

# 编码为 SVG 文件 moon run cmd/main -- encode --type ean13 --data 6901234567892 --out out.svg # 终端 ASCII 预览 moon run cmd/main -- encode --type ean13 --data 6901234567892 --format ascii # 校验 ISBN-13 moon run cmd/main -- validate --type isbn13 --data 978-0-306-40615-7

库用法(开发中):

let barcode = @moonbarcode.encode(@moonbarcode.EAN13, "6901234567892")

#开发状态

#许可证

Apache-2.0

#
Barcode

pub struct Barcode {
symbology : Symbology
data : String
modules : Array[Bool]
}

条码的规范表示:一系列明/暗模块(暗=条,明=空)加可读文本。

#
Barcode::bar_count

fn Barcode::bar_count(self : Barcode) -> Int

条码中暗模块(条)的数量。

#
Barcode::module_count

fn Barcode::module_count(self : Barcode) -> Int

条码的模块总数(暗 + 明)。

#
BarcodeOptions

pub struct BarcodeOptions {
module_width : Int
height : Int
quiet_zone : Int
show_text : Bool
text_margin : Int
}

渲染选项,供 SVG / ASCII / ANSI 渲染器共用。

#
BarcodeOptions::default

#
BarcodeOptions::new

fn BarcodeOptions::new(module_width? : Int, height? : Int, quiet_zone? : Int, show_text? : Bool, text_margin? : Int) -> BarcodeOptions

用可选参数构造渲染选项。

#
EncodeError

pub(all) enum EncodeError {
InvalidData(String)
Unsupported(String)
} derive(Eq,
Debug
)

编码或校验过程中产生的错误。

#
Symbology

pub(all) enum Symbology {
EAN13
EAN8
UPCA
Code39
Code93
Code128
ITF14
} derive(Eq,
Debug
)

支持的条码符号体系。

#
checksum

fn checksum(symbology : Symbology, data : String) -> Result[Int, EncodeError]

计算给定负载的校验位(不含校验位的输入)。

  • EAN-13:12 位负载,校验位权重奇 1 偶 3
  • EAN-8:7 位负载,校验位权重奇 3 偶 1
  • UPC-A:11 位负载,校验位权重奇 3 偶 1
  • ITF-14:13 位负载,校验位权重奇 3 偶 1

#
code128_checksum

fn code128_checksum(data : String, gs1? : Bool) -> Result[Int, String]

计算 Code 128 校验值(mod-103,含起始码,权重从 1 递增)。

#
code39_checksum

fn code39_checksum(data : String) -> Result[Int, String]

计算 Code 39 mod-43 校验值(数据不含起始/终止符)。

#
code93_checksums

fn code93_checksums(data : String, full_ascii? : Bool) -> Result[(Int, Int), String]

计算 Code 93 的 C 与 K 两个 mod-47 校验值。

权重从最右侧字符为 1 向左递增;C 的最大权重为 20,K 的最大权重为 15 且计算时包含 C。

#
encode

fn encode(symbology : Symbology, data : String) -> Result[Barcode, EncodeError]

按符号体系编码条码数据。

对 EAN-13 / EAN-8 / UPC-A,输入可以是不含校验位的负载(自动追加), 也可以是含校验位的完整数字串(校验是否匹配)。

#
encode_code128

fn encode_code128(data : String, gs1? : Bool) -> Result[Barcode, EncodeError]

编码 Code 128(含子集 A/B/C 自动选择)。

gs1 为 true 时生成 GS1-128:起始码后紧跟 FNC1。 输出结构:起始码 + 数据(含子集切换)+ [FNC1] + 校验码 + 停止符(13 模块)。

#
encode_code39

fn encode_code39(data : String, with_checksum? : Bool) -> Result[Barcode, EncodeError]

编码 Code 39。

data 使用 43 字符基本字符集(* 除外);with_checksum 为 true 时自动 追加 mod-43 校验字符。输出模块数 = 16 * N + 31(N 为数据字符数)。

#
encode_code93

fn encode_code93(data : String, full_ascii? : Bool) -> Result[Barcode, EncodeError]

编码 Code 93。

full_ascii 为 true(默认)时支持全部 128 个 ASCII 字符(小写、标点、 控制字符经移位符扩展);为 false 时仅支持 43 个基本字符。 输出结构:起始 * + 数据 + C + K + 终止 * + 终止条。

#
encode_ean13

fn encode_ean13(data : String) -> Result[Barcode, EncodeError]

编码 EAN-13。

输入为 12 位(自动追加校验位)或 13 位(校验校验位是否匹配)。 输出 95 个模块。

#
encode_ean8

fn encode_ean8(data : String) -> Result[Barcode, EncodeError]

编码 EAN-8。

输入为 7 位(自动追加校验位)或 8 位(校验校验位)。输出 67 个模块。

#
encode_upca

fn encode_upca(data : String) -> Result[Barcode, EncodeError]

编码 UPC-A。

输入为 11 位(自动追加校验位)或 12 位(校验校验位)。 UPC-A 等价于首位为 0 的 EAN-13,输出 95 个模块。

#
error_message

fn error_message(err : EncodeError) -> String

返回错误的可读描述。

#
gtin14_checksum

fn gtin14_checksum(data : String) -> Result[Int, String]

计算 GTIN-14(14 位,ITF-14 使用的数据结构)的校验位。 输入为 13 位负载,权重从左起奇位为 3、偶位为 1。

#
gtin14_valid

fn gtin14_valid(s : String) -> Bool

校验完整的 GTIN-14(含校验位)。

#
is_all_digits

fn is_all_digits(s : String) -> Bool

判断字符串是否只包含十进制数字。

#
is_valid

fn is_valid(symbology : Symbology, data : String) -> Bool

便捷的布尔校验入口。

#
isbn10_valid

fn isbn10_valid(s : String) -> Bool

校验 ISBN-10(允许连字符与空格,校验位可为 X)。

校验规则:第 i 位(1-based)乘以权重 11 - i 求和,和须能被 11 整除; 校验位 X 表示 10。

#
isbn13_valid

fn isbn13_valid(s : String) -> Bool

校验 ISBN-13(允许连字符与空格),规则同 EAN-13。

#
issn_valid

fn issn_valid(s : String) -> Bool

校验 ISSN(允许连字符与空格,校验位可为 X)。

校验规则:前 7 位乘以权重 8..2 求和,校验位 = (11 - sum % 11) % 11 10 记作 X,11 记作 0。

#
mod10_check

fn mod10_check(digits : String, odd_weight : Int, even_weight : Int) -> Result[Int, String]

计算 mod-10 校验位。

对从左到右的第奇数位乘以 odd_weight、偶数位乘以 even_weight 求和, 校验位 = (10 - sum % 10) % 10。适用于 EAN-13 / EAN-8 / UPC-A / GTIN-14 等以 10 为模的校验族(权重按各符号体系的规范传入)。

#
mod10_verify

fn mod10_verify(digits : String, odd_weight : Int, even_weight : Int) -> Bool

验证包含校验位的完整数字串(mod-10 校验族)。

#
render_ansi

fn render_ansi(barcode : Barcode, options : BarcodeOptions) -> String

渲染为 ANSI 反白预览(暗模块为反白块,适配多数终端)。

#
render_ascii

fn render_ascii(barcode : Barcode, options : BarcodeOptions) -> String

渲染为单行 ASCII 预览(暗模块为 #,明模块为空格)。 每个模块按 module_width 重复,便于肉眼检查。

#
render_svg

fn render_svg(barcode : Barcode, options : BarcodeOptions) -> String

将一段明/暗模块序列渲染为 SVG。

静区(quiet zone)按模块数计,左右各留 quiet_zone 个模块; 相邻的暗模块会被合并为一个矩形以减小 SVG 体积。

#
strip_separators

fn strip_separators(s : String) -> String

去掉输入中的空白与常见分隔符(-、空格等),只保留数字和字母 X。

#
validate

fn validate(symbology : Symbology, data : String) -> Result[Unit, EncodeError]

严格校验条码数据是否合法(格式 + 校验位)。

encode 不同,validate 要求输入是包含校验位的完整形式: EAN-13 13 位、EAN-8 8 位、UPC-A 12 位、ITF-14 14 位。

#
validate_code128

fn validate_code128(data : String, gs1? : Bool) -> Result[Unit, EncodeError]

校验 Code 128 数据是否可编码。

#
validate_code39

fn validate_code39(data : String) -> Result[Unit, EncodeError]

校验 Code 39 数据是否可编码(字符集检查,不含校验位要求)。

#
validate_code93

fn validate_code93(data : String, full_ascii? : Bool) -> Result[Unit, EncodeError]

校验 Code 93 数据是否可编码。