[PATCH 1/2] Improved layout control when printing tables

Markus Berg markus@kelvin.nu
Sat Apr 9 15:36:40 CEST 2011


Signed-off-by: Markus Berg <markus@kelvin.nu>
---
 barcode.h |    1 +
 library.c |    8 ++++-
 main.c    |   78 ++++++++++++++++++++++++++++++++++++-------------------------
 3 files changed, 53 insertions(+), 34 deletions(-)

diff --git a/barcode.h b/barcode.h
index e4dd04b..19be940 100644
--- a/barcode.h
+++ b/barcode.h
@@ -39,6 +39,7 @@ struct Barcode_Item {
     int width, height; /* output units */
     int xoff, yoff;    /* output units */
     int margin;        /* output units */
+    int xpadding, ypadding; /* output units */
     double scalef;     /* requested scaling for barcode */
     int error;         /* an errno-like value, in case of failure */
 };
diff --git a/library.c b/library.c
index 5d08f49..96070c4 100644
--- a/library.c
+++ b/library.c
@@ -44,6 +44,8 @@ struct Barcode_Item *Barcode_Create(char *text)
     memset(bc, 0, sizeof(*bc));
     bc->ascii = strdup(text);
     bc->margin = BARCODE_DEFAULT_MARGIN; /* default margin */
+    bc->xpadding = 0;
+    bc->ypadding = 0;
     return bc;
 }
 
@@ -209,8 +211,10 @@ int Barcode_Print(struct Barcode_Item *bc, FILE *f, int flags)
 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->width = wid - 2 * bc->xpadding;
+    bc->height = hei - 2 * bc->ypadding;
+    bc->xoff = xoff + bc->xpadding;
+    bc->yoff = yoff + bc->ypadding;
     bc->scalef = scalef;
     return 0;
 }
diff --git a/main.c b/main.c
index 7c0c949..cf94b55 100644
--- a/main.c
+++ b/main.c
@@ -123,9 +123,10 @@ char *ifilename, *ofilename;
 int encoding_type;                    /* filled by get_encoding() */
 int code_width, code_height;          /* "-g" for standalone codes */
 int lines, columns;                   /* "-t" for tables */
-int xmargin0, ymargin0;               /* both for "-g" and "-t" */
+int xpadding, ypadding;               /* internal padding for "-g" */
+int xmargin0, ymargin0;               /* left bottom page margins */
 int xmargin1, ymargin1;               /* same, but right and top */
-int ximargin, yimargin;               /* "-m": internal margins */
+int ximargin, yimargin;               /* "-m": internal margins of table */
 int eps, pcl, ps, noascii, nochecksum; /* boolean flags */
 int page_wid, page_hei;               /* page size in points */
 char *page_name;                      /* name of the media */
@@ -242,8 +243,8 @@ int get_geometry(void *arg)
     /* convert to points */
     code_width  = w * unit;
     code_height = h * unit;
-    xmargin0 = x * unit;
-    ymargin0 = y * unit;
+    xpadding = x * unit;
+    ypadding = y * unit;
     return 0;
 }
 
@@ -385,7 +386,7 @@ struct commandline option_table[] = {
     {'u', CMDLINE_S, NULL, get_unit, "BARCODE_UNIT", NULL,
                     "unit (\"mm\", \"in\", ...) used to decode -g, -t, -p"},
     {'g', CMDLINE_S, NULL, get_geometry, "BARCODE_GEOMETRY", NULL,
-                    "geometry on the page: [<wid>x<hei>][+<margin>+<margin>]"},
+                    "geometry on the page: [<wid>x<hei>][+<padding>+<padding>]"},
     {'t', CMDLINE_S, NULL, get_table, "BARCODE_TABLE", NULL,
                     "table geometry: <cols>x<lines>[+<margin>+<margin>]"},
     {'m', CMDLINE_S, NULL, get_margin, "BARCODE_MARGIN", "10",
@@ -544,7 +545,7 @@ int main(int argc, char **argv)
 		fprintf(ofile, "%%%%Page: %i %i\n\n",page,page);
 	    }
 	    if (Barcode_Encode_and_Print(line, ofile, code_width, code_height,
-					 xmargin0, ymargin0, flags) < 0) {
+					 xpadding, ypadding, flags) < 0) {
 		fprintf(stderr, "%s: can't encode \"%s\"\n", argv[0], line);
 		errors++;
 	    }
@@ -560,33 +561,30 @@ int main(int argc, char **argv)
 
 	/* table mode, the header has been already printed */
 	
-	int xstep = (page_wid - xmargin0 - xmargin1)/columns;
-	int ystep = (page_hei - ymargin0 - ymargin1)/lines;
-	int x = columns, y = -1; /* position in the table, start off-page */
-
-	if (!ximargin) ximargin = BARCODE_DEFAULT_MARGIN;
-	if (!yimargin) yimargin = BARCODE_DEFAULT_MARGIN;
-	/* Assign default size unless -g did it (Joachim Reichelt) */
-	if ( !code_width && !code_height) {
-	    code_width = xstep - 2*ximargin;
-	    code_height = ystep - 2*yimargin;
+	/* Calculate size unless -g did it */
+	int xstep, ystep;
+	if (!code_width && !code_height) {
+	    xstep = (page_wid - xmargin0 - xmargin1 + ximargin)/columns;
+	    ystep = (page_hei - ymargin0 - ymargin1 + yimargin)/lines;
+	    code_width = xstep - ximargin;
+	    code_height = ystep - yimargin;
+	} else {
+	    xstep = code_width + ximargin;
+	    ystep = code_height + yimargin;
 	}
 
-	page=0;
-	while ( (line = retrieve_input_string(ifile)) ) {
-	    x++;  /* fit x and y */
-	    if (x >= columns) {
-		x=0; y--;
-		if (y<0) {
-		    y = lines-1; page++;
-		    /* flush page */
-		    if (ps && page > 1) fprintf(ofile, "showpage\n");
-		    if (pcl && page > 1) fprintf(ofile, "\f");
-		    /* new page */
-		    if (ps) fprintf(ofile, "%%%%Page: %i %i\n\n",page,page);
-		}
-	    }
+	/* position in the table, start off-page */
+	int x = 0;
+	int y = 0;
+	page = 1;
+	if (ps) fprintf(ofile, "%%%%Page: %i %i\n\n",page,page);
+
+	/* Bottom left corner of the barcode */
+	int xbl, ybl;
 
+	while ( (line = retrieve_input_string(ifile)) ) {
+	    xbl = xmargin0 + x * xstep;
+	    ybl = page_hei - ymargin0 - code_height - y * ystep;
 	    /*
 	     * Create a barcode item. This allows to set the margin to 0, as
 	     * we have [xy]imargin to use. But don't use Encode_and_Print(),
@@ -599,15 +597,31 @@ int main(int argc, char **argv)
 		exit(1);
 	    }
 	    bc->margin = 0;
+	    bc->xpadding = xpadding;
+	    bc->ypadding = ypadding;
 	    if ( (Barcode_Position(bc, code_width, code_height,
-				   xmargin0 + ximargin + x * xstep,
-				   ymargin0 + yimargin + y * ystep, 0.0) < 0)
+				   xbl, ybl, 0.0) < 0)
 		 || (Barcode_Encode(bc, flags) < 0)
 		 || (Barcode_Print(bc, ofile, flags) < 0) ) {
 		fprintf(stderr, "%s: can't encode \"%s\": %s\n", argv[0],
 			line, strerror(bc->error));
 	    }
 	    Barcode_Delete(bc);
+
+	    x++;
+	    if (x == columns) {
+		x=0;
+		y++;
+		if (y == lines || (ybl - ystep - ymargin0 < 0)) {
+		    y=0;
+		    page++;
+		    /* flush page */
+		    if (ps) fprintf(ofile, "showpage\n");
+		    if (pcl) fprintf(ofile, "\f");
+		    /* new page */
+		    if (ps) fprintf(ofile, "%%%%Page: %i %i\n\n",page,page);
+		}
+	    }
 	}
 	if (ps) fprintf(ofile, "showpage\n\n%%%%Trailer\n\n");
 	if (pcl) fprintf(ofile, "\f");
-- 
1.7.4


--=-dyM6JsBZRutZe/oegFtt
Content-Disposition: attachment; filename="0002-Add-optional-border-around-barcodes-in-table-mode.patch"
Content-Type: text/x-patch; name="0002-Add-optional-border-around-barcodes-in-table-mode.patch"; charset="ISO-8859-1"
Content-Transfer-Encoding: 7bit



More information about the barcode mailing list