This project has moved and is read-only. For the latest updates, please go here.

Code128 barcode longer than needed


I ran across two issues with the Code128 code that decides which code set to use.

1) It would be nice to have some way of asking the library to tell you how many barcode characters it will use for a given barcode. This value is needed for things like GetPrintMetrict. For most of the other symboligies this is trivial and is directly related to the length (and independent of the content of) the encoded text. Because of the different character sets in code 128, it can be shorter or longer than the encoded text.

ABCD = 4 characters
1234 = 2 characters
^Gg (meaning ctrl-g) = 3 characters

For my purposes I just generated a throwaway barcode with 1 pixel features, and calculated the number of characters as (width-24)/11, but this is obviously somewhat wasteful.

2) In the course of testing this I realized that the library is generating barcodes that are longer than needed in some cases. Below I'm using {A}{B}{C} to mean switches to the respective set.

A123 is optimally expressed by just staying in set A or B.
It looks like the library does A{C}12{A}3 (5 characters) instead of A123 (4 characters)
This occurs any time an alphanumeric barcode ends in an odd number of digits greater than 1.
for longer runs like A12345 it does make sense to switch to set C, but the switch should wait until there is an even number of digits left. A1{C}2345 would take 5 characters, but the library does A{C}1234{A}5 which takes 6, and is at least the same as staying in set A (but not any savings in space).

This also occurs with internal digit sequences:
A12A should be A12A with no set changes, instead it is A{C}12{A}A, taking an extra character. As long as the run of digits is at least 4 there is no harm in changing to set C, but there's no actual benefit until it's at least 6 digits long.

And while these simple cases only add an extra character to the barcode, given certain formats it could make the barcode significantly longer than it needs to be.
as a "worst-case" example, take this list of comma separated numbers
95,96,97,98,99,100 it's 18 characters long (and would take that many characters in set A), but the library does 95{A},{C}96{A},{C}97{A},{C}98{A},{C}99{A},{C}10{A}0 adding 5 extra characters that don't need to be there and making it over 1/4 longer than it needs to be.