Sign in

    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
    Download zip
    Author
    Version
    0.2.0
    License
    Apache-2.0
    Last updated
    2 months ago
    Downloads
    28

    #moonbarcode

    mooncakes.io 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)、ITF-14
    • 图书与出版物校验:ISBN-10 / ISBN-13、ISSN、GTIN-14

    #渲染

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

    #快速开始

    安装:

    moon add fan-ere/moonbarcode

    CLI 用法:

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

    CLI 包仅支持 native 目标;本地运行 CLI 需要 C 编译器。核心库仍可在 native / wasm / wasm-gc / js 多后端编译和测试。

    最小可运行示例:

    moon run examples/quickstart --target wasm-gc

    这个示例不依赖 C 编译器,适合先快速验证库的核心 API。

    库用法(开发中):

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

    #开发状态

    #质量与复现

    moon check --deny-warn moon test --deny-warn moon fmt --check . moon publish --dry-run

    CI 覆盖 native / wasm-gc / js 三个目标的检查、测试和构建,并包含 native CLI 冒烟测试。

    #来源与合规

    项目为原创 MoonBit 实现,未复制第三方条码库源码。标准、编码表和测试样例来源说明见 docs/SOURCES.md。

    #许可证

    Apache-2.0

    #参赛与维护

    OSC 2026 申请人与仓库维护者关系见 docs/PARTICIPATION.md。

    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

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

    DecodeError

    pub(all) enum DecodeError {
    InvalidModules(String)
    ChecksumMismatch(String)
    } derive(Eq,
    Debug
    )

    Decoding failures for module-level barcode readers.

    EncodeError

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

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

    Gs1Date

    pub struct Gs1Date {
    year : Int
    month : Int
    day : Int
    } derive(Eq,
    Debug
    )

    A YYMMDD date used by GS1 date Application Identifiers.

    Gs1Element

    pub struct Gs1Element {
    ai : String
    data : String
    title : String
    fixed_length : Bool
    } derive(Eq,
    Debug
    )

    A parsed GS1 Application Identifier element.

    GtinInfo

    pub struct GtinInfo {
    kind : GtinKind
    value : String
    canonical : String
    payload : String
    check_digit : Int
    } derive(Eq,
    Debug
    )

    Parsed and normalized GTIN information.

    GtinKind

    pub(all) enum GtinKind {
    GTIN8
    GTIN12
    GTIN13
    GTIN14
    } derive(Eq,
    Debug
    )

    Supported GS1 identification-key forms.

    ModuleRun

    pub struct ModuleRun {
    dark : Bool
    width : Int
    } derive(Eq,
    Debug
    )

    A contiguous run of light or dark modules.

    ModuleStats

    pub struct ModuleStats {
    module_count : Int
    dark_count : Int
    light_count : Int
    transitions : Int
    narrowest_run : Int
    widest_run : Int
    } derive(Eq,
    Debug
    )

    Basic structural statistics for a barcode module stream.

    Symbology

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

    支持的条码符号体系。

    add_quiet_zone

    fn add_quiet_zone(modules : Array[Bool], width : Int) -> Array[Bool]

    analyze_modules

    fn analyze_modules(modules : Array[Bool]) -> ModuleStats

    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。

    complete_gln

    fn complete_gln(payload : String) -> Result[String, String]

    Construct a 13-digit Global Location Number from its 12-digit payload.

    complete_gtin

    fn complete_gtin(payload : String) -> Result[String, String]

    Build a complete GTIN from a payload of 7, 11, 12 or 13 digits.

    complete_sscc

    fn complete_sscc(extension : String, company_prefix : String, serial_reference : String) -> Result[String, String]

    Construct an 18-digit Serial Shipping Container Code. The extension digit, company prefix and serial reference must together contain 17 digits.

    decode_code39

    fn decode_code39(modules : Array[Bool], verify_checksum? : Bool) -> Result[String, DecodeError]

    Decode a basic Code 39 symbol. When verify_checksum is true, the final data character is treated as mod-43 checksum and removed from the result.

    decode_ean13

    fn decode_ean13(modules : Array[Bool]) -> Result[String, DecodeError]

    Decode a canonical 95-module EAN-13 symbol and verify its check digit.

    decode_ean8

    fn decode_ean8(modules : Array[Bool]) -> Result[String, DecodeError]

    Decode a canonical 67-module EAN-8 symbol and verify its check digit.

    decode_error_message

    fn decode_error_message(error : DecodeError) -> String

    decode_itf14

    fn decode_itf14(modules : Array[Bool]) -> Result[String, DecodeError]

    Decode an ITF-14 module stream produced with the standard 3:1 wide ratio.

    decode_upca

    fn decode_upca(modules : Array[Bool]) -> Result[String, DecodeError]

    Decode UPC-A through its standards-compatible EAN-13 representation.

    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_itf14

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

    编码 ITF-14。

    输入为 13 位(自动追加 GTIN-14 校验位)或 14 位(校验校验位)。 输出包含标准 ITF 起始/终止模式的模块序列。

    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

    返回错误的可读描述。

    format_gs1

    fn format_gs1(elements : Array[Gs1Element]) -> String

    Format parsed elements back to parenthesized human-readable GS1 syntax.

    format_gs1_date

    fn format_gs1_date(date : Gs1Date) -> Result[String, String]

    Format a GS1 date after validating all fields.

    gs1_128_payload

    fn gs1_128_payload(elements : Array[Gs1Element]) -> Result[String, String]

    Build a GS1-128 payload. Variable-length fields are separated by FNC1 (\u{1d}) unless they are the final field.

    gtin14_checksum

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

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

    gtin14_indicator

    fn gtin14_indicator(value : String) -> Result[Int, String]

    Return the packaging indicator digit of a valid GTIN-14.

    gtin14_valid

    fn gtin14_valid(s : String) -> Bool

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

    hamming_distance

    fn hamming_distance(left : Array[Bool], right : Array[Bool]) -> Int?

    inspect_gtin

    fn inspect_gtin(value : String) -> Result[GtinInfo, String]

    Inspect a complete GTIN-8, GTIN-12 (UPC-A), GTIN-13 or GTIN-14.

    invert_modules

    fn invert_modules(modules : Array[Bool]) -> Array[Bool]

    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 校验族)。

    module_runs

    fn module_runs(modules : Array[Bool]) -> Array[ModuleRun]

    modules_from_binary

    fn modules_from_binary(value : String) -> Result[Array[Bool], String]

    modules_to_binary

    fn modules_to_binary(modules : Array[Bool]) -> String

    normalize_gtin14

    fn normalize_gtin14(value : String) -> Result[String, String]

    Normalize any valid GTIN form to the canonical 14-digit database form.

    parse_gs1

    fn parse_gs1(input : String) -> Result[Array[Gs1Element], String]

    Parse human-readable GS1 element syntax such as (01)09506000134352(17)270101(10)LOT42.

    parse_gs1_date

    fn parse_gs1_date(value : String) -> Result[Gs1Date, String]

    Parse a GS1 YYMMDD date. Day 00 is preserved as an unspecified day.

    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 体积。

    reverse_modules

    fn reverse_modules(modules : Array[Bool]) -> Array[Bool]

    strip_separators

    fn strip_separators(s : String) -> String

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

    trim_quiet_zone

    fn trim_quiet_zone(modules : Array[Bool]) -> Array[Bool]

    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 数据是否可编码。

    validate_gln

    fn validate_gln(value : String) -> Bool

    validate_gs1_date

    fn validate_gs1_date(value : String) -> Bool

    Return true when a six-digit GS1 date has a valid month/day shape. Day 00 is accepted because GS1 uses it for an unspecified day.

    validate_sscc

    fn validate_sscc(value : String) -> Bool