File: barcode.html

package info (click to toggle)
barcode 0.94-1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 612 kB
  • ctags: 370
  • sloc: ansic: 2,205; perl: 1,676; python: 416; sh: 187; makefile: 170; awk: 74; sed: 5
file content (610 lines) | stat: -rw-r--r-- 21,482 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
<HTML>
<HEAD>
<!-- This HTML file has been created by texi2html 1.51
     from barcode.texinfo on 24 October 1999 -->

<TITLE>Barcode </TITLE>
</HEAD>
<BODY>
<H1>barcode 0.94</H1>
<H2>A library for drawing bar codes</H2>
<H2>October 1999</H2>
<ADDRESS>by Alessandro Rubini (<CODE>rubini@{gnu.org,prosa.it}</CODE>)</ADDRESS>
<P>
<P><HR><P>



<H1><A NAME="SEC1" HREF="barcode_toc.html#TOC1">Overview</A></H1>

<P>
The <STRONG>barcode</STRONG> package is mainly a C library for creating bar-code
output files. It also includes a command line front-end and (in a
foreseeable future) a graphic frontend.

</P>
<P>
The package is designed as a library because we think the main use for
barcode-generation tools is inside more featured applications. The
library addresses bar code printing as two distinct problems: creation
of bar information and actual conversion to an output format. To this
aim we use an intermediate representation for bar codes, which is
currently documented in the <TT>`ps.c'</TT> source file (not in this
document).

</P>
<P>
Note that the library and the accompanying material is released
according to the GPL license, not the LGPL one. A copy of the GPL is
included in the distribution tarball.

</P>



<H1><A NAME="SEC2" HREF="barcode_toc.html#TOC2">The Underlying Data Structure</A></H1>

<P>
Every barcode-related function acts on a data structure defined in the
<TT>`barcode.h'</TT> header, which must be included by any C source file
that uses the library. The header is installed by <TT>make install</TT>.

</P>
<P>
The definition of the data structure is included here for reference:

</P>

<PRE>
struct Barcode_Item {
    int flags;         /* type of encoding and other flags */
    char *ascii;       /* malloced */
    char *partial;     /* malloced too */
    char *textinfo;    /* information about text positioning */
    char *encoding;    /* code name, filled by encoding engine */
    int width, height; /* output units */
    int xoff, yoff;    /* output units */
    int margin;        /* output units */
    double scalef;     /* requested scaling for barcode */
    int error;         /* an errno-like value, in case of failure */
};
</PRE>

<P>
The exact meaning of each field and the various flags implemented are
described in the following sections.

</P>
<P>
Even though you won't usually need to act on the contents of this
structure, some of the functions in the library receive arguments that
are directly related to one or more of these fields.

</P>



<H2><A NAME="SEC3" HREF="barcode_toc.html#TOC3">The Fields</A></H2>

<DL COMPACT>

<DT><CODE>int flags;</CODE>
<DD>
The flags are, as you may suspect, meant to specify the exact
behaviour of the library. They are often passed as an argument
to <I>barcode</I> functions and are discussed in the next section.

<DT><CODE>char *ascii;</CODE>
<DD>
<DT><CODE>char *partial;</CODE>
<DD>
<DT><CODE>char *textinfo;</CODE>
<DD>
<DT><CODE>char *encoding;</CODE>
<DD>
These fields are internally managed by the library, and you are
not expected to touch them if you use the provided API. All
of them are allocated with <I>malloc</I>.

<DT><CODE>int width;</CODE>
<DD>
<DT><CODE>int height;</CODE>
<DD>
They specify the width and height of the <I>active</I> barcode
region (i.e., excluding the white margin), in the units used
to create output data (for postscript they are points, 1/72th
of an inch, 0.0352 mm). The fields can be either assigned to
in the structure or via <I>Barcode_Position()</I>, at your
choice.  If either value or both are left to their default
value of zero, the output engine will assign default values
according to the specified scaling factor. If the specified
width is bigger than needed (according to the scaling factor),
the output barcode will be centered in its requested
region. If either the width of the height are too small for
the specified scale factor, the output bar code will expand
symmetrically around the requested region.

<DT><CODE>int xoff;</CODE>
<DD>
<DT><CODE>int yoff;</CODE>
<DD>
The fields specify offset from the coordinate origin of the
output engine (for postscript, position 0,0 is the lower left
corner of the page).  The fields can be either assigned to in
the structure or via <I>Barcode_Position()</I>, at your choice.
The offset specifies where the white margin begins, not where
the first bar will be printed. To print real ink to the
specified position you should set <I>margin</I> to 0.

<DT><CODE>int margin;</CODE>
<DD>
The white margin that will be left around the printed area of
the bar code. The same margin is applied to all sides of the
printed area. The default value for the margin is defined in
<TT>`barcode.h'</TT> as <TT>BARCODE_DEFAULT_MARGIN</TT> (10).

<DT><CODE>double scalef;</CODE>
<DD>
The enlarge or shrink value for the bar code over its default
dimension. The <I>width</I> and <I>scalef</I> fields interact deeply
in the creation of the output, and a complete description of
the issues appears later in this section.

<DT><CODE>int error;</CODE>
<DD>
The field is used when a <I>barcode</I> function fails to host
an <TT>errno</TT>-like integer value.

</DL>



<H3><A NAME="SEC4" HREF="barcode_toc.html#TOC4">Use of the <I>width</I> and <I>scalef</I> fields.</A></H3>

<P>
A width unit is the width of the thinnest bar and/or space in the
chosen code; it defaults to 1 point if the output is postscript or
encapsulated postscript.

</P>
<P>
Either or both the code width and the scale factor can be left
unspecified (i.e., zero). The library deals with defaults in the
following way:

</P>
<DL COMPACT>

<DT><I>Both unspecified</I>
<DD>
If both the width and the scale factor are unspecified, the
scale factor will default to 1.0 and the width is calculated
according to the actual width of the bar code being printed.

<DT><I>Width unspecified</I>
<DD>
If the width is not specified, it is calculated according to
the values of <I>scalef</I>. 

<DT><I>Scale factor unspecified</I>
<DD>
If the scale factor is not specified, it will be chosen so
that the generated bar code exactly fits the specified width.

<DT><I>Both specified</I>
<DD>
The code will be printed inside the specified region according
to the specified scale factor. It will be aligned to the left.
If, however, the chosen width is too small for the specific
bar code and scaling factor, then the code will extend
symmetrically to the left and to the right of the chosen
region.

</DL>



<H1><A NAME="SEC5" HREF="barcode_toc.html#TOC5">The Flags</A></H1>

<P>
The following flags are supported by version 0.94 of the
library:

</P>
<DL COMPACT>

<DT><CODE>BARCODE_ENCODING_MASK</CODE>
<DD>
The mask is used to extract the encoding-type identifier from
the <I>flags</I> field.

<DT><CODE>BARCODE_EAN</CODE>
<DD>
<DT><CODE>BARCODE_UPC</CODE>
<DD>
<DT><CODE>BARCODE_ISBN</CODE>
<DD>
<DT><CODE>BARCODE_128B</CODE>
<DD>
<DT><CODE>BARCODE_128C</CODE>
<DD>
<DT><CODE>BARCODE_39</CODE>
<DD>
<DT><CODE>BARCODE_I25</CODE>
<DD>
The currently supported encoding types: EAN (13 digits, 8
digits, 13 + 2 addon and 13 + 5 addon), UPC (UPC-A, UPC-E,
UPC-A with 2 or 5 digit addon), ISBN (with or without the
5-digit addon), CODE128-B (the whole set of printable
ASCII characters), CODE128-C (two digits encoded by each barcode
symbol), CODE39 (alphanumeric) and "interleaved 2 of 5" (numeric).
See section <A HREF="barcode.html#SEC9">Supported Encodings</A>

<DT><CODE>BARCODE_ANY</CODE>
<DD>
This special encoding type (represented by a value of zero, so
it will be the default) tells the encoding procedure to look
for the first encoding type that can deal with a textual
string.  Therefore, a 11-digit code will be printed as UPC (as
well as 6-digit, 11+2 and 11+5), a 12-digit (or 7-digit, or
12+2 or 12+5) as EAN13, an ISBN code (with or without hyphens,
with or without add-5) will be encoded in its EAN13
representation, an even number of digits is encoded using
CODE128C and a generic string is encoded using CODE128B. Since
code-39 offers a much larger representation for the same
text string, code128-b is preferred over code39 for
alphanumeric strings.

<DT><CODE>BARCODE_NO_ASCII</CODE>
<DD>
Instructs the engine not to print the ascii string on
output. By default the bar code is accompanied with an ascii
version of the text it encodes.

<DT><CODE>BARCODE_NO_CHECKSUM</CODE>
<DD>
Instructs the engine not to add the checksum character to the
output. Not all the encoding types can drop the checksum;
those where the checksum is mandatory (like EAN and UPC)
just ignore the flag.

<DT><CODE>BARCODE_OUTPUT_MASK</CODE>
<DD>
The mask is used to extract the output-type identifier from
the <I>flags</I> field.

<DT><CODE>BARCODE_OUT_PS</CODE>
<DD>
<DT><CODE>BARCODE_OUT_EPS</CODE>
<DD>
The currently supported encoding types: full-page postscript
and encapsulated postscript.

<DT><CODE>BARCODE_OUT_NOHEADERS</CODE>
<DD>
The flag instructs the printing engine not to print the header
and footer part of the file. This makes sense for the
postscript engine but might not make sense for other engines;
such other engines will silently ignore the flag.

</DL>



<H1><A NAME="SEC6" HREF="barcode_toc.html#TOC6">Functions Exported by the Library</A></H1>

<P>
The functions included in the barcode library are declared in the
header file <TT>barcode.h</TT>.  They perform the following tasks:

</P>
<DL COMPACT>

<DT><CODE>struct Barcode_Item *Barcode_Create(char *text);</CODE>
<DD>
The function creates a new barcode object to deal with a
specified text string.  It returns NULL in case of failure and
a pointer to a barcode data structure in case of success.

<DT><CODE>int Barcode_Delete(struct Barcode_Item *bc);</CODE>
<DD>
Destroy a barcode object. Always returns 0 (success)

<DT><CODE>int Barcode_Encode(struct Barcode_Item *bc, int flags);</CODE>
<DD>
Encode the text included in the <I>bc</I> object. Valid flags are
the encoding type (other flags are ignored) and
BARCODE_NO_CHECKSUM (other flags are silently ignored); if the
flag argument is zero, <TT>bc-&#62;flags</TT> will apply. The function
returns 0 on success and -1 in case of error. After
successful termination the data structure will host the
description of the bar code and its textual representation,
after a failure the <TT>error</TT> field will include the reason of
the failure.

<DT><CODE>int Barcode_Print(struct Barcode_Item *bc, FILE *f, int flags);</CODE>
<DD>
Print the bar code described by <TT>bc</TT> to the specified file.
Valid flags are the output type, <TT>BARCODE_NO_ASCII</TT> and
<TT>BARCODE_OUT_NOHEADERS</TT>, other flags are ignored. If any of
these flags is zero, it will be inherited from <TT>bc-&#62;flags</TT>
which therefore takes precedence. The function returns 0 on
success and -1 in case of error (with <TT>bc-&#62;error</TT> set
accordingly). In case of success, the bar code is printed to
the specified file, which won't be closed after use.

<DT><CODE>int Barcode_Position(struct Barcode_Item *bc, int wid, int hei, int xoff, int yoff, double scalef);</CODE>
<DD>
The function is a shortcut to assign values to the data
structure.

<DT><CODE>int Barcode_Encode_and_Print(char *text, FILE *f, int wid, int hei, int xoff, int yoff, double scalef, int flags);</CODE>
<DD>
The function deals with the whole life of the barcode
object by calling the other functions; it uses all the specified
flags.

<DT><CODE>int Barcode_Version(char *versionname);</CODE>
<DD>
Returns the current version as an integer number of the form
major * 10000 + minor * 100 + release. Therefore, version
1.03.5 will be returned as 10305 and version 0.53 as 5300.  If
the argument is non-null, it will be used to return the version
number as a string. Note that the same information is available from
two preprocessor macros: <TT>BARCODE_VERSION</TT> (the string) and
<TT>BARCODE_VERSION_INT</TT> (the integer number).

</DL>



<H1><A NAME="SEC7" HREF="barcode_toc.html#TOC7">The <I>barcode</I> frontend program</A></H1>

<P>
The <B>barcode</B> program is a front-end to access some features of the
library from the command line.  It is able to read user supplied
strings from the command line or a data file (standard input by default)
and encode all of them.

</P>



<H2><A NAME="SEC8" HREF="barcode_toc.html#TOC8">The Command Line</A></H2>

<P>
<B>barcode</B> accepts the following options:

</P>
<DL COMPACT>

<DT><CODE>--help or -h</CODE>
<DD>
Print a usage summary and exit.

<DT><CODE>-i filename</CODE>
<DD>
Identify a file where strings to be encoded are read from. If
missing (and if <TT>-b</TT> is not used) it defaults to standard
input. Each data line of the input file will be used to create
one barcode output.

<DT><CODE>-o filename</CODE>
<DD>
Output file. It defaults to standard output.

<DT><CODE>-b string</CODE>
<DD>
Specify a single "barcode" string to be encoded.
The option can be used multiple times in order to encode
multiple strings (this will result in multi-page postscript
output or a table of barcodes if <TT>-t</TT> is specified).  The
strings must match the encoding chosen; if it doesn't
match the program will print a warning to <TT>stderr</TT> and
generate "blank" output (although not zero-lenght).
Please note that a string including spaces or
other special characters must be properly quoted.

<DT><CODE>-e encoding</CODE>
<DD>
<B>encoding</B> is the name of the chosen encoding format being
used. It defaults to the value of the environment variable
<TT>BARCODE_ENCODING</TT> or to auto detection if the environment is
also unset.

<DT><CODE>-g geometry</CODE>
<DD>
The geometry argument is of the form "<I>&#60;width&#62;</I> <TT>x</TT>
<I>&#60;height&#62;</I> [<TT>+</TT> <I>&#60;xmargin&#62;</I> <TT>+</TT> <I>&#60;ymargin&#62;</I>]" (with
no intervening spaces). The margin values, if missing, default
to 0).  The specified values must be integer numbers and they
represent print points (I plan to add support for different
units, like inches and millimeters, but this is currently not
supported).  The argument is used to place the printout code
on the page. Note that an additional white margin of 10 points
is added to the printout. If the option is unspecified,
<TT>BARCODE_GEOMETRY</TT> is looked up in the environment, if
missing a default size and no margin (but the default 10
points) are used.

<DT><CODE>-t table-geometry</CODE>
<DD>
Used to print several barcodes to a single page, this option
is meant to be used to print stickers. The argument is of the
form "<I>&#60;columns&#62;</I> <TT>x</TT> <I>&#60;lines&#62;</I> [<TT>+</TT> <I>&#60;leftmargin&#62;</I>
<TT>+</TT> <I>&#60;bottommargin&#62;</I> [<TT>-</TT> <I>&#60;rightmargin&#62;</I> [<TT>-</TT>
<I>&#60;topmargin&#62;</I>]]]" (with no intervening spaces); if missing,
the top and right margin will default to be the same as the
bottom and left margin. The margins are specified in print
points or in the chosen unit (see <TT>-u</TT> below).  If the
option is not specified, <TT>BARCODE_TABLE</TT> is looked up in the
environment, otherwise no table is printed and each barcode
will get its own page.

<DT><CODE>-m margin(s)</CODE>
<DD>
Specifies an internal margin for each sticker in the
table. The argument is of the form
"<I>&#60;xmargin&#62;</I><TT>,</TT><I>&#60;ymargin&#62;</I>" and the margin is applied
symmetrically to the sticker. If unspecified, the environment
variable <TT>BARCODE_MARGIN</TT> is used or a default internal
margin of 10 points is used.

<DT><CODE>-n</CODE>
<DD>
"Numeric" output: don't print the ASCII form of the code,
only the bars.

<DT><CODE>-c</CODE>
<DD>
No checksum character (for encodings that allow it, like code 39,
other codes, like UPC or EAN, ignore this option).

<DT><CODE>-E</CODE>
<DD>
Encapsulated postscript (default is normal postscript. When
the output is generated a EPS only one text string is encoded.

<DT><CODE>-p pagesize</CODE>
<DD>
Specify a non-default page size. The page size can be specified
in millimeters, inches or plain numbers (for example: "<TT>210x297mm</TT>",
"<TT>8.5x11in</TT>", "<TT>595x842</TT>"). A page specification as numbers
will be interpreted according to the current unit specification
(see <TT>-u</TT> below). If libpaper is available,
you can also specify the page size with its name, like "<TT>A3</TT>"
or "<TT>letter</TT>" (libpaper is a standard component of Debian
GNU/Linux, but may be missing elsewhere). The default page
size is your system-wide default if libpaper is there, A4 otherwise.

<DT><CODE>-u unit</CODE>
<DD>
Choose the unit used in size specifications. Accepted values
are "mm", "cm", "in" and "pt". By default, the program
will check <TT>BARCODE_UNIT</TT> in the environment, and assume
points otherwise (this behaviour is compatible with 0.92 and
previous versions. If <TT>-u</TT> appears more than once, each
instance will modified the behaviour for the arguments at its
right, as the command line is processes left to right. The
program internally works with points, and any size is
approximated to the nearest multiple of one point. The <TT>-u</TT>
option affect <TT>-g</TT> (geometry), <TT>-t</TT> (table) and <TT>-p</TT>
(page size).

</DL>



<H2><A NAME="SEC9" HREF="barcode_toc.html#TOC9">Supported Encodings</A></H2>

<P>
The program encodes text strings passed either on the command line
(with -b) or retrieved from standard input. The text representation is
interpreted according to the following rules. When auto-detection
of the encoding is enabled (i.e, no explicit encoding type is specified),
the encoding types are scanned to find one that can digest the text string.

</P>
<DL COMPACT>

<DT><VAR>UPC</VAR>
<DD>
The UPC frontend accepts only strings made up of digits (and,
if a supplemental encoding is used, a blank to separate it).
It accepts strings of 11 digits (UPC-A) or 6 digits (UPC-E).
The 12th digit of UPC-A is the checksum and is added by the
library, if you pass a 12-digit string it will be rejected as
invalid.  For UPC-A, a trailing string of 2 digits or 5 digits
is accepted as well. Therefore, valid strings look like one of
the following: "<TT>01234567890</TT>" (UPC-A), "<TT>012345</TT>"
(UPC-E), "<TT>01234567890 12</TT>" (UPC-A, add-2) and
"<TT>01234567890 12345</TT>" (UPC-A add-5).

<DT><VAR>EAN</VAR>
<DD>
The EAN frontend is similar to UPC; it accepts strings of
digits, 12 or 7 characters long, the checksum digit is added
by the library and a string of 13 or 8 characters is
rejected. The add-2 and add-5 extension are accepted for the
EAN13 encoding. Valid strings look like one of the following:
"<TT>123456789012</TT>" (EAN-13), "<TT>1234567</TT>" (EAN-8),
"<TT>123456789012 12</TT>" (EAN-13 with add-2) and
"<TT>123456789012 12345</TT>" (EAN-13 with add-5).

<DT><VAR>ISBN</VAR>
<DD>
ISBN numbers are encoded as EAN-13 symbols, with an optional
add-5 trailer. The ISBN frontend of the library accepts real
ISBN numbers and deals with any hyphen and, if present, the
ISBN checksum character before encoding data. Valid
representations for ISBN strings are for example:
"<TT>1-56592-292-1</TT>", "3-89721-122-X" and "3-89721-122-X
06900".

<DT><VAR>code 128-B</VAR>
<DD>
This encoding can represent all of the printing ASCII
characters, from the space (32) to DEL (127). The checksum
digit is mandatory in this encoding.

<DT><VAR>code 128-C</VAR>
<DD>
The "C" variation of Code-128 uses Code-128 symbols to
represent two digits at a time (Code-128 is made up of 104
symbols whose interpretation is controlled by the start symbol
being used). Code 128-C is thus the most compact way to
represent any even number of digits. The encoder refuses to
deal with an odd number of digits because the caller is
expected to provide proper padding to an even number of
digits. (Since Code-128 includes control symbols to switch
charset, it is theoretically possible to represent the odd
digit as a Code 128-A or 128-B symbol, but this tool doesn't
currently implement this option).

<DT><VAR>code 39</VAR>
<DD>
The code-39 standard can encode uppercase letters, digits, the
blank space, plus, minus, dot, star, dollar, slash, percent.
Any string that is only composed of such characters is
accepted by the code-39 encoder. To avoid loosing information,
the encoder refuses to encode mixed-case strings (a lowercase
string is nonetheless accepted as a shortcut, but is encoded
as uppercase).

<DT><VAR>interleaved 2 of 5</VAR>
<DD>
This encoding can only represent an even number of digits
(odd digits are represented by bars, and even digits by the
interleaving spaces). The name stresses the fact that two
of the five items (bars or spaces) allocated to each symbol
are wide, while the rest are narrow. The checksum digit is
optional (can be disabled via <TT>BARCODE_NO_CHECKSUM</TT>).
Since the number of digits, including the checksum, must be even,
a leading zero is inserted in the string being encoded if needed
(this is specifically stated in the specs I have access to).

</DL>



<H2><A NAME="SEC10" HREF="barcode_toc.html#TOC10">Bugs and Pending Issues.</A></H2>

<P>
The current management of borders/margins is far from optimal. The
"default" margin applied by the library interferes with the external
representation, but I feel it is mandatory to avoid creating barcode
output with no surrounding white space (the problem is especially
relevant for EPS output).

</P>
<P>
Code128-A should be supported.

</P>

<P><HR><P>
This document was generated on 24 October 1999 using the
<A HREF="http://wwwcn.cern.ch/dci/texi2html/">texi2html</A>
translator version 1.51.</P>
</BODY>
</HTML>