GS1 keys, Application Identifiers, Digital Link and EAN/UPC and GS1-128 barcodes
README.md

gs1 #

The GS1 identification keys, Application Identifiers and barcodes of the GS1
General Specifications: GTIN, GLN and SSCC with their check digits, element
strings with their FNC1 separator, GS1 Digital Link URIs, and the module
sequences of EAN-13, EAN-8 and UPC-A (ISO/IEC 15420) and of Code 128 and
GS1-128 (ISO/IEC 15417).

A point of sale reads a GTIN from an EAN-13 barcode and a lot number and an
expiry date from a GS1-128 one; a stock module prints them on labels. This
library checks the keys, reads and writes the data, and turns them into the
bars and spaces a renderer draws as SVG, PDF or a printer's raster.

Contents #

Installation #

$ opam install gs1

The library is gs1 and its module is Gs1; the command is gs1.

Usage #

A key is checked for its length and its check digit:

# let g = Result.get_ok (Gs1.Gtin.of_string "4006381333931");;
val g : Gs1.Gtin.t = <abstr>
# Gs1.Gtin.to_gtin14 g;;
- : string = "04006381333931"
# Gs1.Gtin.of_string "4006381333932"
  |> Result.iter_error (Fmt.pr "%a@." Gs1.Check_digit.pp_error);;
check digit 2 where 1 is expected
- : unit = ()

Element strings read and print in their human-readable form, in the form a
scanner transmits, with the ASCII group separator standing for FNC1, and as a
GS1 Digital Link URI:

# let e =
    Result.get_ok
      (Gs1.Element_string.of_hri "(01)09520123456788(10)ABC123(17)271231");;
val e : Gs1.Element_string.t = <abstr>
# Gs1.Element_string.find "17" e;;
- : string option = Some "271231"
# Gs1.Element_string.to_string e;;
- : string = "010952012345678810ABC123\02917271231"
# Gs1.Digital_link.to_uri e;;
- : (string, Gs1.Digital_link.error) result =
Ok "https://id.gs1.org/01/09520123456788/10/ABC123?17=271231"

A symbol is a sequence of modules, dark or light, with the quiet zones that
must surround it:

# let s = Result.get_ok (Gs1.Ean_upc.encode Ean_8 (Result.get_ok (Gs1.Gtin.of_string "96385074")));;
val s : Gs1.Symbol.t = <abstr>
# Gs1.Symbol.to_string s;;
- : string =
"1010001011010111101111010110111010101001110111001010001001011100101"
# Gs1.Symbol.quiet_zone s;;
- : int * int = (7, 7)
# let l = Result.get_ok (Gs1.Code128.encode (Gs1 e));;
val l : Gs1.Symbol.t = <abstr>
# Gs1.Symbol.width l;;
- : int = 277

The command line #

$ gs1 check gtin 036000291452
GTIN-12 036000291452
$ gs1 parse 'https://id.gs1.org/01/09520123456788/10/ABC123?17=271231'
(01) GTIN: 09520123456788
(10) BATCH/LOT: ABC123
(17) USE BY or EXPIRY: 271231
$ gs1 link '(01)09520123456788(21)12345'
https://id.gs1.org/01/09520123456788/21/12345
$ gs1 encode ean8 96385074
1010001011010111101111010110111010101001110111001010001001011100101

gs1 encode SYMBOLOGY DATA --format pgm writes a binary PGM image of the
symbol with its quiet zones.

What it covers #

  • The GS1 check digit of section 7.9.1, and GTIN-8, -12, -13 and -14, GLN and
    SSCC. Two GTINs compare by their 14-digit form.
  • The Application Identifiers of section 3.2 that identify trade items,
    logistic units, locations, assets and services, and those that qualify them
    with dates, measures, amounts, origins and references, each with its
    format: the characters of each part (digits, CSET 82, CSET 39 or CSET 64),
    its length, its check digit and its dates. The rules of section 4.14 that
    require or exclude AIs in combination are not checked, except that an AI
    repeated with different data is refused.
  • Element strings in the form a scanner transmits, separated by FNC1 after
    every AI of no predefined length (section 7.8.5), after a symbology
    identifier or none, and in their human-readable form.
  • GS1 Digital Link URIs in their uncompressed form: a primary key, its key
    qualifiers in the standard's order, and the other AIs as query
    parameters, each datum percent-encoded as RFC 3986 says.
  • EAN-13, EAN-8 and UPC-A of ISO/IEC 15420, without the two- and five-digit
    add-ons and UPC-E.
  • Code 128 of ISO/IEC 15417 over ASCII 0 to 127, in the fewest symbol
    characters the code sets A, B and C and the shift allow, and GS1-128 with
    its FNC1 characters and its limit of 48 data characters.

Rendering is the caller's: Gs1.Symbol.row is one line of a raster at a
given module width, and Gs1.Ean_upc.guards the guard bars a renderer
extends below the human-readable digits.

License #

ISC. See LICENSE.md.