[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>
---
 main.c |   78 +++++++++++++++++++++++++++++++++++++---------------------------
 1 files changed, 45 insertions(+), 33 deletions(-)

diff --git a/main.c b/main.c
index 7c0c949..c29faf1 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,29 @@ int main(int argc, char **argv)
 		exit(1);
 	    }
 	    bc->margin = 0;
-	    if ( (Barcode_Position(bc, code_width, code_height,
-				   xmargin0 + ximargin + x * xstep,
-				   ymargin0 + yimargin + y * ystep, 0.0) < 0)
+	    if ( (Barcode_Position(bc, code_width - xpadding*2, code_height - ypadding*2,
+				   xbl+xpadding, ybl+ypadding, 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


--=-7TwHRYJziiB8fzm1s0ab--



More information about the barcode mailing list