From 6717882e759c320e468683f8c69cf8af1281d91b Mon Sep 17 00:00:00 2001 From: Jim Evins Date: Sun, 11 Jul 2010 00:49:15 -0400 Subject: [PATCH] Refactoring of barcode structure. Minor refactoring of API to barcode structure. --- src/bc-gnubarcode.c | 21 ++--- src/bc-iec16022.c | 8 +- src/bc-iec18004.c | 8 +- src/bc-postnet.c | 18 ++--- src/bc-zint.c | 23 ++---- src/bc.c | 190 +++++++++++++++++++++++++++----------------- src/bc.h | 50 ++++++++---- src/label-barcode.c | 8 +- 8 files changed, 172 insertions(+), 154 deletions(-) diff --git a/src/bc-gnubarcode.c b/src/bc-gnubarcode.c index 11f132ae..d8d096d9 100644 --- a/src/bc-gnubarcode.c +++ b/src/bc-gnubarcode.c @@ -217,8 +217,6 @@ render_pass1 (struct Barcode_Item *bci, { gint validbits = BARCODE_NO_ASCII; glBarcode *gbc; - glBarcodeShapeLine *line; - glBarcodeShapeAlpha *bchar; gdouble scalef = 1.0; gdouble x; gint i, j, barlen; @@ -315,12 +313,7 @@ render_pass1 (struct Barcode_Item *bci, yr -= (isdigit (*p) ? 20 : 10) * scalef; } } - line = gl_barcode_shape_line_new (); - line->x = x0; - line->y = y0; - line->length = yr; - line->width = (j * scalef) - SHRINK_AMOUNT; - gl_barcode_add_shape (gbc, (glBarcodeShape *)line); + gl_barcode_add_line (gbc, x0, y0, yr, (j * scalef) - SHRINK_AMOUNT); } x += j * scalef; @@ -342,17 +335,13 @@ render_pass1 (struct Barcode_Item *bci, g_message ("impossible data: %s", p); continue; } - bchar = gl_barcode_shape_alpha_new (); - bchar->x = f1 * scalef + bci->margin; + x0 = f1 * scalef + bci->margin; if (mode == '-') { - bchar->y = - bci->margin + bci->height - 8 * scalef; + y0 = bci->margin + bci->height - 8 * scalef; } else { - bchar->y = bci->margin; + y0 = bci->margin; } - bchar->fsize = f2 * FONT_SCALE * scalef; - bchar->c = c; - gl_barcode_add_shape (gbc, (glBarcodeShape *)bchar); + gl_barcode_add_char (gbc, x0, y0, (f2 * FONT_SCALE * scalef), c); } } diff --git a/src/bc-iec16022.c b/src/bc-iec16022.c index 834e9f92..e56990d4 100644 --- a/src/bc-iec16022.c +++ b/src/bc-iec16022.c @@ -96,7 +96,6 @@ render_iec16022 (const gchar *grid, gdouble h) { glBarcode *gbc; - glBarcodeShapeLine *line; gint x, y; gdouble aspect_ratio, pixel_size; @@ -127,12 +126,7 @@ render_iec16022 (const gchar *grid, if (*grid++) { - line = gl_barcode_shape_line_new (); - line->x = x*pixel_size + pixel_size/2.0; - line->y = y*pixel_size; - line->length = pixel_size; - line->width = pixel_size; - gl_barcode_add_shape (gbc, (glBarcodeShape *)line); + gl_barcode_add_box (gbc, x*pixel_size, y*pixel_size, pixel_size, pixel_size); } } diff --git a/src/bc-iec18004.c b/src/bc-iec18004.c index f5f1adaf..33f27891 100644 --- a/src/bc-iec18004.c +++ b/src/bc-iec18004.c @@ -101,7 +101,6 @@ render_iec18004 (const gchar *grid, gdouble h) { glBarcode *gbc; - glBarcodeShapeLine *line; gint x, y; gdouble aspect_ratio, pixel_size; @@ -136,12 +135,7 @@ render_iec18004 (const gchar *grid, * bits are meaningless for us. */ if ((*grid++) & 1) { - line = gl_barcode_shape_line_new (); - line->x = x*pixel_size + pixel_size/2.0; - line->y = y*pixel_size; - line->length = pixel_size; - line->width = pixel_size; - gl_barcode_add_shape (gbc, (glBarcodeShape *)line); + gl_barcode_add_box (gbc, x*pixel_size, y*pixel_size, pixel_size, pixel_size); } } diff --git a/src/bc-postnet.c b/src/bc-postnet.c index 9a3ba322..3aa17651 100644 --- a/src/bc-postnet.c +++ b/src/bc-postnet.c @@ -85,8 +85,7 @@ gl_barcode_postnet_new (const gchar *id, { gchar *code, *p; glBarcode *gbc; - glBarcodeShapeLine *line; - gdouble x; + gdouble x, y, length, width; /* Validate code length for all subtypes. */ if ( (g_ascii_strcasecmp (id, "POSTNET") == 0) ) { @@ -128,19 +127,16 @@ gl_barcode_postnet_new (const gchar *id, /* Now traverse the code string and create a list of lines */ x = POSTNET_HORIZ_MARGIN; for (p = code; *p != 0; p++) { - line = gl_barcode_shape_line_new (); - line->x = x; - line->y = POSTNET_VERT_MARGIN; + y = POSTNET_VERT_MARGIN; if (*p == '0') { - line->y += - POSTNET_FULLBAR_HEIGHT - POSTNET_HALFBAR_HEIGHT; - line->length = POSTNET_HALFBAR_HEIGHT; + y += POSTNET_FULLBAR_HEIGHT - POSTNET_HALFBAR_HEIGHT; + length = POSTNET_HALFBAR_HEIGHT; } else { - line->length = POSTNET_FULLBAR_HEIGHT; + length = POSTNET_FULLBAR_HEIGHT; } - line->width = POSTNET_BAR_WIDTH; + width = POSTNET_BAR_WIDTH; - gl_barcode_add_shape (gbc, (glBarcodeShape *)line); + gl_barcode_add_line (gbc, x, y, length, width); x += POSTNET_BAR_PITCH; } diff --git a/src/bc-zint.c b/src/bc-zint.c index ca83f619..c8d6d079 100644 --- a/src/bc-zint.c +++ b/src/bc-zint.c @@ -59,7 +59,6 @@ gl_barcode_zint_new (const gchar *id, { glBarcode *gbc; struct zint_symbol *symbol; - gint type; gint result; symbol = ZBarcode_Create(); @@ -182,9 +181,7 @@ gl_barcode_zint_new (const gchar *id, static glBarcode *render_zint(struct zint_symbol *symbol, gboolean text_flag) { glBarcode *gbc; - glBarcodeShapeBox *box; - glBarcodeShapeString *bstring; - + struct zint_render *render; struct zint_render_line *zline; struct zint_render_string *zstring; @@ -195,14 +192,7 @@ static glBarcode *render_zint(struct zint_symbol *symbol, gboolean text_flag) for ( zline = render->lines; zline != NULL; zline = zline->next ) { - box = gl_barcode_shape_box_new (); - - box->x = (gdouble) zline->x; - box->y = (gdouble) zline->y; - box->width = (gdouble) zline->width; - box->height = (gdouble) zline->length; - - gl_barcode_add_shape (gbc, (glBarcodeShape *)box); + gl_barcode_add_box (gbc, zline->x, zline->y, zline->width, zline->length); } /* @@ -212,12 +202,9 @@ static glBarcode *render_zint(struct zint_symbol *symbol, gboolean text_flag) { for ( zstring = render->strings; zstring != NULL; zstring = zstring->next ) { - bstring = gl_barcode_shape_string_new(); - bstring->x = (double) zstring->x; - bstring->y = (double) zstring->y; - bstring->fsize = (double) zstring->fsize; - bstring->str = g_strndup (zstring->text, zstring->length); - gl_barcode_add_shape (gbc, (glBarcodeShape *)bstring); + gl_barcode_add_string (gbc, + zstring->x, zstring->y, + zstring->fsize, (gchar *)zstring->text, zstring->length); } } diff --git a/src/bc.c b/src/bc.c index 247b5065..ca3d42f7 100644 --- a/src/bc.c +++ b/src/bc.c @@ -382,6 +382,11 @@ static const Backend backends[] = { /* Private function prototypes. */ /*========================================================*/ +static void gl_barcode_add_shape (glBarcode *bc, + glBarcodeShape *shape); + +static void gl_barcode_shape_free (glBarcodeShape *shape); + /*---------------------------------------------------------------------------*/ /* Convert id to index into above table. */ @@ -427,79 +432,6 @@ name_to_index (const gchar *name) } -/*****************************************************************************/ -/* Allocate new Line shape. */ -/*****************************************************************************/ -glBarcodeShapeLine * -gl_barcode_shape_line_new (void) -{ - glBarcodeShapeLine *line_shape = g_new0 (glBarcodeShapeLine, 1); - line_shape->type = GL_BARCODE_SHAPE_LINE; - - return line_shape; -} - - -/*****************************************************************************/ -/* Allocate new Box shape. */ -/*****************************************************************************/ -glBarcodeShapeBox * -gl_barcode_shape_box_new (void) -{ - glBarcodeShapeBox *box_shape = g_new0 (glBarcodeShapeBox, 1); - box_shape->type = GL_BARCODE_SHAPE_BOX; - - return box_shape; -} - - -/*****************************************************************************/ -/* Allocate new Alpha shape. */ -/*****************************************************************************/ -glBarcodeShapeAlpha * -gl_barcode_shape_alpha_new (void) -{ - glBarcodeShapeAlpha *alpha_shape = g_new0 (glBarcodeShapeAlpha, 1); - alpha_shape->type = GL_BARCODE_SHAPE_ALPHA; - - return alpha_shape; -} - - -/*****************************************************************************/ -/* Allocate new String shape. */ -/*****************************************************************************/ -glBarcodeShapeString * -gl_barcode_shape_string_new (void) -{ - glBarcodeShapeString *string_shape = g_new0 (glBarcodeShapeString, 1); - string_shape->type = GL_BARCODE_SHAPE_STRING; - - return string_shape; -} - - -/*****************************************************************************/ -/* Free a shape primitive. */ -/*****************************************************************************/ -void -gl_barcode_shape_free (glBarcodeShape *shape) -{ - switch (shape->type) - { - - case GL_BARCODE_SHAPE_STRING: - g_free (shape->string.str); - break; - - default: - break; - } - - g_free (shape); -} - - /*****************************************************************************/ /* Call appropriate barcode backend to create barcode in intermediate format.*/ /*****************************************************************************/ @@ -552,9 +484,98 @@ gl_barcode_free (glBarcode **gbc) /*****************************************************************************/ -/* Add shape to barcode. */ +/* Add a line. */ +/*****************************************************************************/ +void +gl_barcode_add_line (glBarcode *bc, + gdouble x, + gdouble y, + gdouble length, + gdouble width) +{ + glBarcodeShapeLine *line_shape = g_new0 (glBarcodeShapeLine, 1); + line_shape->type = GL_BARCODE_SHAPE_LINE; + + line_shape->x = x; + line_shape->y = y; + line_shape->length = length; + line_shape->width = width; + + gl_barcode_add_shape (bc, (glBarcodeShape *)line_shape); +} + + +/*****************************************************************************/ +/* Add box. */ +/*****************************************************************************/ +void +gl_barcode_add_box (glBarcode *bc, + gdouble x, + gdouble y, + gdouble width, + gdouble height) +{ + glBarcodeShapeBox *box_shape = g_new0 (glBarcodeShapeBox, 1); + box_shape->type = GL_BARCODE_SHAPE_BOX; + + box_shape->x = x; + box_shape->y = y; + box_shape->width = width; + box_shape->height = height; + + gl_barcode_add_shape (bc, (glBarcodeShape *)box_shape); +} + + +/*****************************************************************************/ +/* Add character. */ /*****************************************************************************/ void +gl_barcode_add_char (glBarcode *bc, + gdouble x, + gdouble y, + gdouble fsize, + gchar c) +{ + glBarcodeShapeChar *char_shape = g_new0 (glBarcodeShapeChar, 1); + char_shape->type = GL_BARCODE_SHAPE_CHAR; + + char_shape->x = x; + char_shape->y = y; + char_shape->fsize = fsize; + char_shape->c = c; + + gl_barcode_add_shape (bc, (glBarcodeShape *)char_shape); +} + + +/*****************************************************************************/ +/* Add string. */ +/*****************************************************************************/ +void +gl_barcode_add_string (glBarcode *bc, + gdouble x, + gdouble y, + gdouble fsize, + gchar *string, + gsize length) +{ + glBarcodeShapeString *string_shape = g_new0 (glBarcodeShapeString, 1); + string_shape->type = GL_BARCODE_SHAPE_STRING; + + string_shape->x = x; + string_shape->y = y; + string_shape->fsize = fsize; + string_shape->string = g_strndup(string, length); + + gl_barcode_add_shape (bc, (glBarcodeShape *)string_shape); +} + + +/*****************************************************************************/ +/* Add shape to barcode. */ +/*****************************************************************************/ +static void gl_barcode_add_shape (glBarcode *bc, glBarcodeShape *shape) { @@ -565,6 +586,27 @@ gl_barcode_add_shape (glBarcode *bc, } +/*****************************************************************************/ +/* Free a shape primitive. */ +/*****************************************************************************/ +static void +gl_barcode_shape_free (glBarcodeShape *shape) +{ + switch (shape->type) + { + + case GL_BARCODE_SHAPE_STRING: + g_free (shape->string.string); + break; + + default: + break; + } + + g_free (shape); +} + + /*****************************************************************************/ /* Get a list of names for valid barcode styles. */ /*****************************************************************************/ diff --git a/src/bc.h b/src/bc.h index 6c7cbcb5..c32fbd36 100644 --- a/src/bc.h +++ b/src/bc.h @@ -38,7 +38,7 @@ G_BEGIN_DECLS typedef enum { GL_BARCODE_SHAPE_LINE, GL_BARCODE_SHAPE_BOX, - GL_BARCODE_SHAPE_ALPHA, + GL_BARCODE_SHAPE_CHAR, GL_BARCODE_SHAPE_STRING, } glBarcodeShapeType; @@ -100,7 +100,7 @@ typedef struct { typedef struct { /* Begin Common Fields */ - glBarcodeShapeType type; /* Always GL_BARCODE_SHAPE_LINE. */ + glBarcodeShapeType type; /* Always GL_BARCODE_SHAPE_BOX. */ gdouble x; gdouble y; /* End Common Fields */ @@ -111,7 +111,7 @@ typedef struct { } glBarcodeShapeBox; /* - * glBarcodeShapeAlpha: + * glBarcodeShapeChar: * * @ = origin (x,y) from top left corner of barcode * @@ -128,7 +128,7 @@ typedef struct { typedef struct { /* Begin Common Fields */ - glBarcodeShapeType type; /* Always GL_BARCODE_SHAPE_ALPHA. */ + glBarcodeShapeType type; /* Always GL_BARCODE_SHAPE_CHAR. */ gdouble x; gdouble y; /* End Common Fields */ @@ -136,7 +136,7 @@ typedef struct { gdouble fsize; gchar c; -} glBarcodeShapeAlpha; +} glBarcodeShapeChar; /* * glBarcodeShapeString: @@ -157,13 +157,13 @@ typedef struct { typedef struct { /* Begin Common Fields */ - glBarcodeShapeType type; /* Always GL_BARCODE_SHAPE_ALPHA. */ + glBarcodeShapeType type; /* Always GL_BARCODE_SHAPE_STRING. */ gdouble x; gdouble y; /* End Common Fields */ gdouble fsize; - gchar *str; + gchar *string; } glBarcodeShapeString; @@ -174,18 +174,11 @@ typedef union { glBarcodeShapeLine line; glBarcodeShapeBox box; - glBarcodeShapeAlpha alpha; + glBarcodeShapeChar bchar; glBarcodeShapeString string; } glBarcodeShape; -glBarcodeShapeLine *gl_barcode_shape_line_new (void); -glBarcodeShapeBox *gl_barcode_shape_box_new (void); -glBarcodeShapeAlpha *gl_barcode_shape_alpha_new (void); -glBarcodeShapeString *gl_barcode_shape_string_new (void); - -void gl_barcode_shape_free (glBarcodeShape *shape); - /********************************/ /* Barcode Intermediate Format. */ @@ -213,8 +206,31 @@ glBarcode *gl_barcode_new (const gchar *id, void gl_barcode_free (glBarcode **bc); -void gl_barcode_add_shape (glBarcode *bc, - glBarcodeShape *shape); +void gl_barcode_add_line (glBarcode *bc, + gdouble x, + gdouble y, + gdouble length, + gdouble width); + +void gl_barcode_add_box (glBarcode *bc, + gdouble x, + gdouble y, + gdouble width, + gdouble height); + +void gl_barcode_add_char (glBarcode *bc, + gdouble x, + gdouble y, + gdouble fsize, + gchar c); + +void gl_barcode_add_string (glBarcode *bc, + gdouble x, + gdouble y, + gdouble fsize, + gchar *string, + gsize length); + GList *gl_barcode_get_styles_list (void); void gl_barcode_free_styles_list (GList *styles_list); diff --git a/src/label-barcode.c b/src/label-barcode.c index 7f3cf8f3..5367261b 100644 --- a/src/label-barcode.c +++ b/src/label-barcode.c @@ -425,7 +425,7 @@ draw_object (glLabelObject *object, glBarcodeShape *shape; glBarcodeShapeLine *line; glBarcodeShapeBox *box; - glBarcodeShapeAlpha *bchar; + glBarcodeShapeChar *bchar; glBarcodeShapeString *bstring; GList *p; gdouble x_offset, y_offset; @@ -521,8 +521,8 @@ draw_object (glLabelObject *object, break; - case GL_BARCODE_SHAPE_ALPHA: - bchar = (glBarcodeShapeAlpha *) shape; + case GL_BARCODE_SHAPE_CHAR: + bchar = (glBarcodeShapeChar *) shape; layout = pango_cairo_create_layout (cr); @@ -556,7 +556,7 @@ draw_object (glLabelObject *object, pango_layout_set_font_description (layout, desc); pango_font_description_free (desc); - pango_layout_set_text (layout, bstring->str, -1); + pango_layout_set_text (layout, bstring->string, -1); pango_layout_get_size (layout, &iw, &ih); layout_width = (gdouble)iw / (gdouble)PANGO_SCALE; -- 2.39.2