[barcode] Patches to barcode-0.97 to enable boxes around codes

Lars Fredriksen lars@odin-corporation.com
Mon, 04 Mar 2002 15:24:10 -0600


This is a multi-part message in MIME format.
--------------F4F1496FC9E80607CFF5D248
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit

Hi,

I had a need to put boxes around the bar codes in order to facilitate
easier cutting of the labels for use with DLT tapes. In addition I also
needed the barcodes to be of a particular size. If you just specify a
table today with barcode, it will autosize the barcodes to fit the space
available. Even though one could probably fix this using the margins,
I found it easier to be able to specify the geometry of the barcode and
make it count even inside a table entry.

So the attached patches, does the following:

Adds: -B geometry option, that works similar to the -g option (no
margins though). It will put a box around the barcode.

Changed barcode so that if you specified both -t and -g that it would
use the -g restrictions for the barcode inside the table.

These changes are not necessary the cleanest way to accomplish this, but
perhaps it might be  a starting point for a more robust implementation
of the features.

I am not a member of the mailling list, so please contact me directly if
you need to.

Lars

--------------F4F1496FC9E80607CFF5D248
Content-Type: text/plain; charset=us-ascii;
 name="barcode.h.diffs"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
 filename="barcode.h.diffs"

*** barcode.h.orig	Sun Mar  3 16:23:21 2002
--- barcode.h	Sun Mar  3 16:50:49 2002
***************
*** 37,42 ****
--- 37,43 ----
      char *textinfo;    /* information about text positioning */
      char *encoding;    /* code name, filled by encoding engine */
      int width, height; /* output units */
+     int bwidth, bheight; /* box output units */
      int xoff, yoff;    /* output units */
      int margin;        /* output units */
      double scalef;     /* requested scaling for barcode */
***************
*** 55,60 ****
--- 56,62 ----
  #define BARCODE_OUTPUT_MASK   0x000ff000   /* 256 output types */
  #define BARCODE_OUT_EPS       0x00001000
  #define BARCODE_OUT_PS        0x00002000
+ #define BARCODE_BOX           0x00004000
  #define BARCODE_OUT_NOHEADERS 0x00100000   /* no header nor footer */
  
  enum {
***************
*** 93,105 ****
  /*
   * Choose the position
   */
! extern int Barcode_Position(struct Barcode_Item *bc, int wid, int hei,
  			    int xoff, int yoff, double scalef);
  
  /*
   * Do it all in one step
   */
! extern int Barcode_Encode_and_Print(char *text, FILE *f, int wid, int hei,
  				    int xoff, int yoff, int flags);
  
  
--- 95,109 ----
  /*
   * Choose the position
   */
! extern int Barcode_Position(struct Barcode_Item *bc, int bwid, int bhei,
! 			    int wid, int hei,
  			    int xoff, int yoff, double scalef);
  
  /*
   * Do it all in one step
   */
! extern int Barcode_Encode_and_Print(char *text, FILE *f, int bwid, int bhei,
! 				    int wid, int hei,
  				    int xoff, int yoff, int flags);
  
  

--------------F4F1496FC9E80607CFF5D248
Content-Type: text/plain; charset=us-ascii;
 name="library.c.diffs"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
 filename="library.c.diffs"

*** library.c.orig	Sun Mar  3 16:22:59 2002
--- library.c	Sun Mar  3 16:23:00 2002
***************
*** 192,200 ****
  /*
   * Choose the position
   */
! int Barcode_Position(struct Barcode_Item *bc, int wid, int hei,
  		     int xoff, int yoff, double scalef)
  {
      bc->width = wid; bc->height = hei;
      bc->xoff = xoff; bc->yoff = yoff;
      bc->scalef = scalef;
--- 192,202 ----
  /*
   * Choose the position
   */
! int Barcode_Position(struct Barcode_Item *bc, int bwid, int bhei, 
! 		     int wid, int hei,
  		     int xoff, int yoff, double scalef)
  {
+     bc->bwidth = bwid; bc->bheight = bhei;
      bc->width = wid; bc->height = hei;
      bc->xoff = xoff; bc->yoff = yoff;
      bc->scalef = scalef;
***************
*** 204,210 ****
  /*
   * Do it all in one step
   */
! int Barcode_Encode_and_Print(char *text, FILE *f, int wid, int hei,
  				    int xoff, int yoff, int flags)
  {
      struct Barcode_Item * bc;
--- 206,213 ----
  /*
   * Do it all in one step
   */
! int Barcode_Encode_and_Print(char *text, FILE *f, int bwid, int bhei, 
! 				    int wid, int hei,
  				    int xoff, int yoff, int flags)
  {
      struct Barcode_Item * bc;
***************
*** 213,219 ****
  	errno = -ENOMEM;
  	return -1;
      }
!     if (     Barcode_Position(bc, wid, hei, xoff, yoff, 0.0) < 0
  	  || Barcode_Encode(bc, flags) < 0
  	  || Barcode_Print(bc, f, flags) < 0) {
  	errno = bc->error;
--- 216,222 ----
  	errno = -ENOMEM;
  	return -1;
      }
!     if (     Barcode_Position(bc, bwid, bhei, wid, hei, xoff, yoff, 0.0) < 0
  	  || Barcode_Encode(bc, flags) < 0
  	  || Barcode_Print(bc, f, flags) < 0) {
  	errno = bc->error;

--------------F4F1496FC9E80607CFF5D248
Content-Type: text/plain; charset=us-ascii;
 name="main.c.diffs"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
 filename="main.c.diffs"

*** main.c.orig	Fri Feb  1 21:31:29 2002
--- main.c	Sun Mar  3 17:01:27 2002
***************
*** 117,122 ****
--- 117,123 ----
  char *ifilename, *ofilename;
  int encoding_type;                    /* filled by get_encoding() */
  int code_width, code_height;          /* "-g" for standalone codes */
+ int box_width, box_height;            /* "-B" for bounding boxes */
  int lines, columns;                   /* "-t" for tables */
  int xmargin0, ymargin0;               /* both for "-g" and "-t" */
  int xmargin1, ymargin1;               /* same, but right and top */
***************
*** 217,222 ****
--- 218,241 ----
      return -2; /* error, no help */
  }
  
+ /* convert a bounding box geometry specification */
+ int get_box(void *arg)
+ {
+     double w = 0.0, h = 0.0;
+     double x = 0.0, y = 0.0;
+     int n;
+ 
+     n = sscanf((char *)arg, "%lfx%lf", &w, &h, (char *)arg);
+     if (n!=2) {
+ 	fprintf(stderr, "%s: wrong geometry \"%s\"\n", prgname, (char *)arg);
+ 	return -2;
+     }
+     /* convert to points */
+     box_width  = w * unit;
+     box_height = h * unit;
+     return 0;
+ }
+ 
  /* convert a geometry specification */
  int get_geometry(void *arg)
  {
***************
*** 375,380 ****
--- 394,401 ----
                      "output file, default is stdout"},
      {'b', CMDLINE_S, NULL, get_input_string, NULL, NULL,
                     "string to encode (use input file if missing)"},
+     {'B', CMDLINE_S, NULL, get_box, NULL, NULL,
+                    "geometry of barcode bouding box: [<wid>x<hei>]"},
      {'e', CMDLINE_S, NULL, get_encoding, "BARCODE_ENCODING", NULL,
                     "encoding type (default is best fit for first string)"},
      {'u', CMDLINE_S, NULL, get_unit, "BARCODE_UNIT", NULL,
***************
*** 520,526 ****
  	    if (ps) {
  		fprintf(ofile, "%%%%Page: %i %i\n\n",page,page);
  	    }
! 	    if (Barcode_Encode_and_Print(line, ofile, code_width, code_height,
  					 xmargin0, ymargin0, flags) < 0) {
  		fprintf(stderr, "%s: can't encode \"%s\"\n", argv[0], line);
  	    }
--- 541,548 ----
  	    if (ps) {
  		fprintf(ofile, "%%%%Page: %i %i\n\n",page,page);
  	    }
! 	    if (Barcode_Encode_and_Print(line, ofile, box_width, box_height,
! 					code_width, code_height,
  					 xmargin0, ymargin0, flags) < 0) {
  		fprintf(stderr, "%s: can't encode \"%s\"\n", argv[0], line);
  	    }
***************
*** 558,569 ****
  	     * In order to remove the extra (default) margin, subtract it
  	     * in advance (dirty)
  	     */
! 	    if (Barcode_Encode_and_Print(line, ofile,
! 		    xstep - 2*ximargin, ystep - 2*yimargin,
! 		    xmargin0 + ximargin + x * xstep - BARCODE_DEFAULT_MARGIN,
! 		    ymargin0 + yimargin + y * ystep - BARCODE_DEFAULT_MARGIN,
! 		    flags)<0) {
! 		fprintf(stderr, "%s: can't encode \"%s\"\n", argv[0], line);
  	    }
  	}
  	fprintf(ofile, "showpage\n\n%%%%Trailer\n\n");
--- 580,603 ----
  	     * In order to remove the extra (default) margin, subtract it
  	     * in advance (dirty)
  	     */
! 	    if (code_width) {
! 		    if (Barcode_Encode_and_Print(line, ofile,
! 			    box_width, box_height,    
! 			    code_width, code_height,    
! 			    xmargin0 + ximargin + x * xstep - BARCODE_DEFAULT_MARGIN,
! 			    ymargin0 + yimargin + y * ystep - BARCODE_DEFAULT_MARGIN,
! 			    flags)<0) {
! 			fprintf(stderr, "%s: can't encode \"%s\"\n", argv[0], line);
! 		    }
! 	    } else {
! 		    if (Barcode_Encode_and_Print(line, ofile,
! 			    box_width, box_height,
! 			    xstep - 2*ximargin, ystep - 2*yimargin, 
! 			    xmargin0 + ximargin + x * xstep - BARCODE_DEFAULT_MARGIN,
! 			    ymargin0 + yimargin + y * ystep - BARCODE_DEFAULT_MARGIN,
! 			    flags)<0) {
! 			fprintf(stderr, "%s: can't encode \"%s\"\n", argv[0], line);
! 		    }
  	    }
  	}
  	fprintf(ofile, "showpage\n\n%%%%Trailer\n\n");

--------------F4F1496FC9E80607CFF5D248
Content-Type: text/plain; charset=us-ascii;
 name="ps.c.diffs"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
 filename="ps.c.diffs"

*** ps.c.orig	Sun Mar  3 16:49:16 2002
--- ps.c	Sun Mar  3 20:54:55 2002
***************
*** 64,70 ****
      int i, j, k, barlen, printable=1;
      double f1, f2, fsav=0;
      int mode = '-'; /* text below bars */
!     double scalef=1, xpos, x0, y0, yr;
      unsigned char *ptr;
      unsigned char c;
  
--- 64,71 ----
      int i, j, k, barlen, printable=1;
      double f1, f2, fsav=0;
      int mode = '-'; /* text below bars */
!     double scalef=1, xpos, x0, y0, yr; /*  bx0, by0, bxd, byd; */
!     int bx0, by0, bxd, byd;
      unsigned char *ptr;
      unsigned char c;
  
***************
*** 135,140 ****
--- 136,148 ----
      }
  #endif
  
+     if (bc->bwidth) {
+ 		bxd = (bc->bwidth - bc->width)/2;
+ 		byd = (bc->bheight - bc->height)/2;
+     } else {
+ 		bxd = byd = 0;
+     }
+ 
      /*
       * Ok, then deal with actual ps (eps) output
       */
***************
*** 146,156 ****
  	    fprintf(f, "%%!PS-Adobe-2.0\n");
  	fprintf(f, "%%%%Creator: libbarcode\n");
  	if (bc->flags & BARCODE_OUT_EPS)  {
! 	    fprintf(f, "%%%%BoundingBox: %i %i %i %i\n",
! 		    bc->xoff,
! 		    bc->yoff,
! 		    bc->xoff + bc->width + 2* bc->margin,
! 		    bc->yoff + bc->height + 2* bc->margin);
  	}
  	fprintf(f, "%%%%EndComments\n");
  	if (bc->flags & BARCODE_OUT_PS)  {
--- 154,172 ----
  	    fprintf(f, "%%!PS-Adobe-2.0\n");
  	fprintf(f, "%%%%Creator: libbarcode\n");
  	if (bc->flags & BARCODE_OUT_EPS)  {
!  		if (bc->flags & BARCODE_BOX) {
! 		    fprintf(f, "%%%%BoundingBox: %i %i %i %i\n",
! 			    bc->xoff,
! 			    bc->yoff,
! 			    bc->xoff + bc->width + 2*bxd + 2* bc->margin,
! 			    bc->yoff + bc->height + 2*byd + 2* bc->margin);
! 		} else {
! 		    fprintf(f, "%%%%BoundingBox: %i %i %i %i\n",
! 			    bc->xoff,
! 			    bc->yoff,
! 			    bc->xoff + bc->width + 2* bc->margin,
! 			    bc->yoff + bc->height + 2* bc->margin);
! 		}
  	}
  	fprintf(f, "%%%%EndComments\n");
  	if (bc->flags & BARCODE_OUT_PS)  {
***************
*** 182,188 ****
      fprintf(f, "\n[\n%%  height  xpos   ypos  width"
  	         "       height  xpos   ypos  width\n");
  
!     xpos = bc->margin + (bc->partial[0]-'0') * scalef;
      for (ptr = bc->partial+1, i=1; *ptr; ptr++, i++) {
  	/* special cases: '+' and '-' */
  	if (*ptr == '+' || *ptr == '-') {
--- 198,204 ----
      fprintf(f, "\n[\n%%  height  xpos   ypos  width"
  	         "       height  xpos   ypos  width\n");
  
!     bx0 = xpos = bc->margin + bxd + (bc->partial[0]-'0') * scalef;
      for (ptr = bc->partial+1, i=1; *ptr; ptr++, i++) {
  	/* special cases: '+' and '-' */
  	if (*ptr == '+' || *ptr == '-') {
***************
*** 193,199 ****
  	else                  j = *ptr-'a'+1;
  	if (i%2) { /* bar */
  	    x0 = bc->xoff + xpos + (j*scalef)/2;
!             y0 = bc->yoff + bc->margin;
              yr = bc->height;
              if (!(bc->flags & BARCODE_NO_ASCII)) { /* leave space for text */
  		if (mode == '-') {
--- 209,215 ----
  	else                  j = *ptr-'a'+1;
  	if (i%2) { /* bar */
  	    x0 = bc->xoff + xpos + (j*scalef)/2;
!             y0 = bc->yoff + byd + bc->margin;
              yr = bc->height;
              if (!(bc->flags & BARCODE_NO_ASCII)) { /* leave space for text */
  		if (mode == '-') {
***************
*** 240,249 ****
  	    else
  		fprintf(f, "%c)  ", c);
              fprintf(f, "%6.2f %6.2f %5.2f]\n", 
!                     bc->xoff + f1 * scalef + bc->margin,
  		    mode == '-'
!                        ? (double)bc->yoff + bc->margin
! 		       : (double)bc->yoff + bc->margin+bc->height - 8*scalef,
  		    fsav == f2 ? 0.0 : f2 * scalef);
  	    fsav = f2;
  	}
--- 256,265 ----
  	    else
  		fprintf(f, "%c)  ", c);
              fprintf(f, "%6.2f %6.2f %5.2f]\n", 
!                     bc->xoff + bxd + f1 * scalef + bc->margin,
  		    mode == '-'
!                        ? (double)bc->yoff + byd + bc->margin
! 		       : (double)bc->yoff + byd + bc->margin+bc->height - 8*scalef,
  		    fsav == f2 ? 0.0 : f2 * scalef);
  	    fsav = f2;
  	}
***************
*** 255,260 ****
--- 271,295 ----
  
      }
  
+     if (bc->bwidth) {
+ 	    fprintf(f,"%% Box around Barcode \"%s\"\n\n",
+ 	    printable ? bc->ascii : "<unprintable string>");
+ 	    fprintf(f,"newpath %d %d moveto %d %d lineto %d %d lineto %d %d lineto closepath 1 setlinewidth stroke\n",
+ 			bc->xoff+bxd, bc->yoff+2*byd, 
+ 			bc->xoff+bxd + bc->width + 2*bxd, bc->yoff+2*byd,
+ 			bc->xoff+bxd + bc->width + 2*bxd, 
+ 			bc->yoff+byd + bc->height + 3*byd,
+ 			bc->xoff+bxd, bc->yoff+byd + bc->height + 3*byd); 
+ /*
+ 	    fprintf(f,"newpath %d %d moveto %d %d lineto %d %d lineto %d %d lineto closepath 1 setlinewidth stroke\n",
+ 			bc->xoff, bc->yoff, 
+ 			bc->xoff + bc->width + 2*bxd, bc->yoff,
+ 			bc->xoff + bc->width + 2*bxd, 
+ 			bc->yoff + bc->height + 2*byd,
+ 			bc->xoff, bc->yoff + bc->height + 2*byd); 
+ */
+     }
+ 	
      fprintf(f,"%% End barcode for \"%s\"\n\n",
  	    printable ? bc->ascii : "<unprintable string>");
  

--------------F4F1496FC9E80607CFF5D248--