Barcode Writer in Pure PostScript

We recently took a look at KBarcode, GUI application for Linux desktops that lets users create and print a variety of barcode formats. But KBarcode isn’t the only option. Terry Burton’s Barcode Writer in Pure PostScript (BWIPP), which is one of the GUI app’s backends, is a fast and flexible barcode generating tool in its own right.

PostScript? Isn’t that a brand of printer?

Most of us probably think of PostScript as an output-only file format, because it is the lingua franca of so many computer printers. But PostScript is actually full procedural programming language — just one optimized for drawing on the page.

BWIPP is a collection of barcode generation functions written in PostScript itself, which can be directly embedded in final PostScript output, sent directly to a printer, or interpreted by a PostScript tool like Ghostscript and turned into an image. The entire collection is available for download in a single file, covering more than fifty 1-D, 2-D, and stacked barcode encoding options.

The barcode encoding routines follow a standardized syntax, so that you can invoke any one of them with a more-or-less standardized function invocation. BWIPP uses PostScript to draw the actual codes in vector format, which means that they can be scaled to any dimensions (although of course those codes that include alphabetic characters in or below the bars could be distorted if you scale them too much), and provides a standard set of options with which to specify output measurement. To handle the common need to scale a barcode without distorting the text that accompanies it, the BWIPP wiki has a brief guide on proper scaling calculations.

There are also general-purpose options for displaying human-readable text, specifying the text’s font and style, adjusting borders and whitespace, generating check-digits, printing symbols and text in color, and adjusting bar widths to account for ink bleeding. In addition to the global options, each barcode format has its own special rules, and thus its own unique set of options. The project wiki maintains a set of pages detailing each encoding standard’s available options, and how to invoke them.

Using it

BWIPP itself is not intended to be an end-user program, although you can use it as a command-line tool. To do so, you make a copy of the PostScript source code, and at the end, insert a barcode generation function call. You can then send the edited file directly to a PostScript printer, for example with lpr -Pdevice -o raw my_barcode_sample.ps, or convert it to another format with Ghostscript’s CLI utility — for example, gs -sDEVICE=tifflzw -dBATCH -dSAFER -dNOPAUSE -sOutputFile=my_rasterized_barcode.tiff my_barcode_sample.ps. You could also use ImageMagick’s convert command. In either case, you should familiarize yourself with the command-line tool’s rendering options before you use it for production work.

“BWIPP’s barcode_with_samples.ps file generates a test sheet of several different barcode types, and varies some of the options for effect.  Shown here with Ghostscript’s gv tool.”

If you want to try this approach, don’t start from scratch; take a look instead at the BWIPP sample document, which is found in the main distribution alongside the basic PostScript file. The file is named barcode_with_sample.ps, and just as in the preceding example, it is actually the full BWIPP, complete with barcode encoding instructions for every format, but with a set of example function calls at the end. You can look at the examples, alter them to get a feel for the options, and when you feel like generating your own codes in real life, modify the function calls at will. If you decide to do this repeatedly, you can save a lot of space by editing out the unused encoders for the format you do not intend to use.

“BWIPP’s barcode_with_samples.ps file generates a test sheet of several different barcode types, and varies some of the options for effect.  Shown here with Ghostscript’s gv tool.”

For most casual users, though, a front-end app like KBarcode is easier to work with. The powerful open source desktop publishing suite Scribus also uses BWIPP as its barcode-generating engine, and there is a web-based generator available for free use on Burton’s site.

BWIPP for programmers

Software developers that wish to incorporate BWIPP into their applications could extract the barcode encoders and write them directly into their own PostScript output chain, but they will probably be better off linking to them by name in PostScript and bundling the original BWIPP barcode.ps file as a template. That way, Burton says, they can easily update their project to the latest version of BWIPP simply by downloading the newest release and replacing barcode.ps.

The code is made available under the open source MIT/X-Consortium License, which permits its inclusion into any kind of downstream software project (free software or proprietary), so long as the copyright notice and license statement are included.

There is inline documentation in barcode.ps, but if for some reason you need to modify the code for your application, there are reference documents on the project’s Google Groups wiki site. They include a detailed look at the encoder routine, the renderer that converts the barcode data into vector shapes, the basic barcode data structure, and general style notes. In addition to that, the reference manual explaining all of the options for the various barcode formats supported will undoubtedly be helpful as well.

Considering its importance to KBarcode and Scribus, you have probably been using BWIPP whether you realize it or not. That alone makes it a project worth investigating, but for anyone who needs barcode printing support, BWIPP is an excellent jumping-off point. The code is easy-to-read and well-organized, and the documentation of the various formats supported is thorough. As a result, you can not only use BWIPP to create barcodes, but to better understand the underpinnings of the often quirky array of competing formats created over the years.

BY NATHAN WILLIS

Have your say: