npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@juit/librebarcode

v2.0.2

Published

Encoders and fonts for EAN13 and Code128

Downloads

551

Readme

LibreBarcode Fonts and Encoders

This project provides two encoders for the Code128 and EAN fonts developed by the LibreBarcode project.

Look here for a simple demo.

Code128

The code128() encoder takes a string (the barcode to be encoded) and returns a string that can be fed to the Code128 font to correctly display the barcode.

It attempts to calculate the shortest possible barcode given the input, and supports encoding of all 128 characters in the US-ASCII range, plus support for the Code128 specific FNC1, FNC2, FNC3 and FNC4 characters.

Control characters can be entered directly in the string to encode using their JavaScript equivalent (e.g. \n for newline, \x1D for the group separator) and the four speial characters \uf001, \uf002, \uf003, \uf004, can be used to encode FNC1, FNC2, FNC3 and FNC4 respectively.

Those are also exported by the library as FNC1, FNC2, FNC3 and FNC4.

import { FNC1, code128 } from '@juit/librebarcode'

const gs1 =
  FNC1 + // FNC1 indicates this is a standard "GS1" barcode
  '02' + // application identifier "02": Identification of trade items
  '05411361083574' + // Ardo Spring Onions, 250g
  '15' + // application identifier "15": Best before date (YYMMDD)
  '250929' + // Expires 29.09.2025
  '37' + // application identifier "37": Count of trade items
  '144' // Contains 144 packages

const encoded = code128(gs1)
// now you can feed the "encoded" string to the font!

Encoding with a specific code set

In certain cases, where a specific code set of Code128 is required for a barcode (for example labeling products for the Amazon Marketplace, requiring codeset "A"), there might be the need to forego the "shortest path" algorithm provided by code128() and specify the code set directly.

In this case, an extra parameter to code128() can be added to the call:

import { code128 } from '@juit/librebarcode'

const a = code128('ABCD', 'A') // this will be constrained to code set "A"
const b = code128('abcd', 'B') // this will be constrained to code set "B"
const b = code128('1234', 'C') // this will be constrained to code set "C"

EAN-13, UPC/A, EAN-8

Similarly to Code128, but for EAN the ean() encoder takes a string of digits and produces a a string that can be fed to the EAN font to correctly display the barcode.

The string to encode should have one of the following lengths:

  • 13 digits: standard EAN-13 barcode
  • 12 digits: standard UPC/A barcode
  • 8 digits: standard EAN-8 barcode

The encoder also supports the standard 2-digits and 5-digits supplemental barcodes and the lengths will then become:

  • 18 digits: standard EAN-13 barcode with 5-digits supplemental barcode
  • 17 digits: standard UPC/A barcode with 5-digits supplemental barcode
  • 15 digits: standard EAN-13 barcode with 2-digits supplemental barcode
  • 12 digits: standard UPC/A barcode with 2-digits supplemental barcode
  • 5 digits: only the 5-digits supplemental barcode
  • 2 digits: only the 2-digits supplemental barcode
import { ean } from '@juit/librebarcode'

const encoded = ean('5411361083574') // EAN-13 barcode for Ardo Spring Onions
// now you can feed the "encoded" string to the font!

EAN/GS-1 Checksums

Unlike the "compatible" EAN-13 encoder developed and provided by the LibreBarcode project, this library doesn't try to calculate the EAN-13 checksum while encoding strings.

A separate function checksum() can be used to calculate the checksum for an arbitrary sequence of digits according to the EAN/GS-1 calculation rules:

import { checksum } from '@juit/librebarcode'

const check = checksum('541136108357')
// here checksum will be the number 4

Font Assets and CSS

Embedded with this library, you can find three fonts for your encoding needs:

  • ./assets/LibreBarcode128-Regular....: the regular Code128 font without any underlying text. Use this one for GS1 barcodes, as encoding of the FNC1 character messes up the text, and anyhow the text should be represented as (xx)yyyyyyyyyy(zz)wwwwwwww... (application identifiers with parentheses).
  • ./assets/LibreBarcode128Text-Regular....: the Code128 font with underlying text. Use this one for standard Code128 barcodes.
  • ./assets/LibreBarcodeEAN13Text-Regular....: the EAN font.

All fonts are available in TrueType (.ttf), OpenType (.otf), WOFF (.woff) and WOFF2 (.woff2) formats and are declared as exports of this package.

To know the path of those assets at runtime, you can use require.resolve(...) on CommonJS or import.meta.resolve(...) for EcmaScript modules.

// in CommonJS
const path = require.resolve('@juit/librebarcode/assets/LibreBarcode128-Regular.ttf')

// in EcmaScript modules
const url = import.meta.resolve('@juit/librebarcode/assets/LibreBarcodeEAN13Text-Regular.woff2')

Similarly, a number of CSS stylesheets are included in the package to easily embed these fonts onto web pages and similar. These stylesheets define the families LibreBarcode128, LibreBarcode128Text, and LibreBarcodeEAN13Text (all with normal font style and 400 as weight).

  • ./assets/fonts-otf.css: Only import OpenType fonts.
  • ./assets/fonts-ttf.css: Only import TrueType fonts.
  • ./assets/fonts-woff.css: Only import WOFF fonts.
  • ./assets/fonts-woff2.css: Only import WOFF2 fonts.
  • ./assets/fonts.css: Import all font types, in the following preference order: WOFF2, WOFF, OpenType, TrueType.