From 30d4df7681459d895ef5c09e2e43ba88c0e72785 Mon Sep 17 00:00:00 2001 From: Jim Evins Date: Sat, 22 May 2010 19:02:23 -0400 Subject: [PATCH] Minor refactoring of glBarcode structure. Minor refactoring of glBarcode structure in preparation of support for more exotic barcode shapes. --- src/bc-gnubarcode.c | 30 +++++++-------- src/bc-iec16022.c | 12 +++--- src/bc-iec18004.c | 12 +++--- src/bc-postnet.c | 14 +++---- src/bc-zint.c | 14 +++---- src/bc.c | 53 +++++++++++++++++++++----- src/bc.h | 92 ++++++++++++++++++++++++++++++++++++++++---- src/label-barcode.c | 93 ++++++++++++++++++++++++++------------------- 8 files changed, 222 insertions(+), 98 deletions(-) diff --git a/src/bc-gnubarcode.c b/src/bc-gnubarcode.c index 3e0ba437..11f132ae 100644 --- a/src/bc-gnubarcode.c +++ b/src/bc-gnubarcode.c @@ -215,17 +215,17 @@ static glBarcode * render_pass1 (struct Barcode_Item *bci, gint flags) { - gint validbits = BARCODE_NO_ASCII; - glBarcode *gbc; - glBarcodeLine *line; - glBarcodeChar *bchar; - gdouble scalef = 1.0; - gdouble x; - gint i, j, barlen; - gdouble f1, f2; - gint mode = '-'; /* text below bars */ - gdouble x0, y0, yr; - gchar *p, c; + gint validbits = BARCODE_NO_ASCII; + glBarcode *gbc; + glBarcodeShapeLine *line; + glBarcodeShapeAlpha *bchar; + gdouble scalef = 1.0; + gdouble x; + gint i, j, barlen; + gdouble f1, f2; + gint mode = '-'; /* text below bars */ + gdouble x0, y0, yr; + gchar *p, c; if (bci->width > (2*bci->margin)) { bci->width -= 2*bci->margin; @@ -315,12 +315,12 @@ render_pass1 (struct Barcode_Item *bci, yr -= (isdigit (*p) ? 20 : 10) * scalef; } } - line = g_new0 (glBarcodeLine, 1); + line = gl_barcode_shape_line_new (); line->x = x0; line->y = y0; line->length = yr; line->width = (j * scalef) - SHRINK_AMOUNT; - gbc->lines = g_list_append (gbc->lines, line); + gl_barcode_add_shape (gbc, (glBarcodeShape *)line); } x += j * scalef; @@ -342,7 +342,7 @@ render_pass1 (struct Barcode_Item *bci, g_message ("impossible data: %s", p); continue; } - bchar = g_new0 (glBarcodeChar, 1); + bchar = gl_barcode_shape_alpha_new (); bchar->x = f1 * scalef + bci->margin; if (mode == '-') { bchar->y = @@ -352,7 +352,7 @@ render_pass1 (struct Barcode_Item *bci, } bchar->fsize = f2 * FONT_SCALE * scalef; bchar->c = c; - gbc->chars = g_list_append (gbc->chars, bchar); + gl_barcode_add_shape (gbc, (glBarcodeShape *)bchar); } } diff --git a/src/bc-iec16022.c b/src/bc-iec16022.c index ebaa4e6b..834e9f92 100644 --- a/src/bc-iec16022.c +++ b/src/bc-iec16022.c @@ -95,10 +95,10 @@ render_iec16022 (const gchar *grid, gdouble w, gdouble h) { - glBarcode *gbc; - glBarcodeLine *line; - gint x, y; - gdouble aspect_ratio, pixel_size; + glBarcode *gbc; + glBarcodeShapeLine *line; + gint x, y; + gdouble aspect_ratio, pixel_size; /* Treat requested size as a bounding box, scale to maintain aspect * ratio while fitting it in this bounding box. */ @@ -127,12 +127,12 @@ render_iec16022 (const gchar *grid, if (*grid++) { - line = g_new0 (glBarcodeLine, 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; - gbc->lines = g_list_append (gbc->lines, line); + gl_barcode_add_shape (gbc, (glBarcodeShape *)line); } } diff --git a/src/bc-iec18004.c b/src/bc-iec18004.c index 7070fcab..f5f1adaf 100644 --- a/src/bc-iec18004.c +++ b/src/bc-iec18004.c @@ -100,10 +100,10 @@ render_iec18004 (const gchar *grid, gdouble w, gdouble h) { - glBarcode *gbc; - glBarcodeLine *line; - gint x, y; - gdouble aspect_ratio, pixel_size; + glBarcode *gbc; + glBarcodeShapeLine *line; + gint x, y; + gdouble aspect_ratio, pixel_size; /* Treat requested size as a bounding box, scale to maintain aspect * ratio while fitting it in this bounding box. */ @@ -136,12 +136,12 @@ render_iec18004 (const gchar *grid, * bits are meaningless for us. */ if ((*grid++) & 1) { - line = g_new0 (glBarcodeLine, 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; - gbc->lines = g_list_append (gbc->lines, line); + gl_barcode_add_shape (gbc, (glBarcodeShape *)line); } } diff --git a/src/bc-postnet.c b/src/bc-postnet.c index 52c07dba..9a3ba322 100644 --- a/src/bc-postnet.c +++ b/src/bc-postnet.c @@ -83,10 +83,10 @@ gl_barcode_postnet_new (const gchar *id, gdouble h, const gchar *digits) { - gchar *code, *p; - glBarcode *gbc; - glBarcodeLine *line; - gdouble x; + gchar *code, *p; + glBarcode *gbc; + glBarcodeShapeLine *line; + gdouble x; /* Validate code length for all subtypes. */ if ( (g_ascii_strcasecmp (id, "POSTNET") == 0) ) { @@ -128,7 +128,7 @@ 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 = g_new0 (glBarcodeLine, 1); + line = gl_barcode_shape_line_new (); line->x = x; line->y = POSTNET_VERT_MARGIN; if (*p == '0') { @@ -140,7 +140,7 @@ gl_barcode_postnet_new (const gchar *id, } line->width = POSTNET_BAR_WIDTH; - gbc->lines = g_list_append (gbc->lines, line); + gl_barcode_add_shape (gbc, (glBarcodeShape *)line); x += POSTNET_BAR_PITCH; } @@ -150,8 +150,6 @@ gl_barcode_postnet_new (const gchar *id, gbc->width = x + POSTNET_HORIZ_MARGIN; gbc->height = POSTNET_FULLBAR_HEIGHT + 2 * POSTNET_VERT_MARGIN; - gbc->chars = NULL; - return gbc; } diff --git a/src/bc-zint.c b/src/bc-zint.c index 5c8502c5..0ad80869 100644 --- a/src/bc-zint.c +++ b/src/bc-zint.c @@ -122,9 +122,9 @@ gl_barcode_zint_new (const gchar *id, *--------------------------------------------------------------------------*/ static glBarcode *render_zint(struct zint_symbol *symbol, gboolean text_flag) { - glBarcode *gbc; - glBarcodeLine *line; - glBarcodeChar *bchar; + glBarcode *gbc; + glBarcodeShapeLine *line; + glBarcodeShapeAlpha *bchar; gint i, r, block_width, latch, this_row; gfloat textpos, large_bar_height, preset_height, row_height, row_posn = 0.0; @@ -226,7 +226,7 @@ static glBarcode *render_zint(struct zint_symbol *symbol, gboolean text_flag) { } if(latch == 1) { /* a bar */ - line = g_new0 (glBarcodeLine, 1); + line = gl_barcode_shape_line_new (); line->width = block_width * scaler; /* glBarcodeLine centers based on width, counter-act!!! */ @@ -241,7 +241,7 @@ static glBarcode *render_zint(struct zint_symbol *symbol, gboolean text_flag) { } latch = 0; // g_message ("Zint Adding Line at: %f x %f dim: %f x %f", line->x, line->y, line->width, line->length); - gbc->lines = g_list_append (gbc->lines, line); + gl_barcode_add_shape (gbc, (glBarcodeShape *)line); } else { /* a space */ latch = 1; @@ -267,12 +267,12 @@ static glBarcode *render_zint(struct zint_symbol *symbol, gboolean text_flag) { for (p = symbol->text; *p != 0; p++) { if (p != (gchar *)symbol->text && *p == '(') xoffset += 3.0; - bchar = g_new0 (glBarcodeChar, 1); + bchar = gl_barcode_shape_alpha_new (); bchar->x = (textpos + xoffset) * scaler; bchar->y = default_text_posn; bchar->fsize = 8.0 * scaler; bchar->c = (gchar) *p; - gbc->chars = g_list_append (gbc->chars, bchar); + gl_barcode_add_shape (gbc, (glBarcodeShape *)bchar); // Poor mans kerning if (*p == '(') { xoffset += 3.0; diff --git a/src/bc.c b/src/bc.c index 3c3d1dc1..0761f6e8 100644 --- a/src/bc.c +++ b/src/bc.c @@ -232,6 +232,32 @@ 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 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; +} + + /*****************************************************************************/ /* Call appropriate barcode backend to create barcode in intermediate format.*/ /*****************************************************************************/ @@ -270,19 +296,12 @@ gl_barcode_free (glBarcode **gbc) if (*gbc != NULL) { - for (p = (*gbc)->lines; p != NULL; p = p->next) { + for (p = (*gbc)->shapes; p != NULL; p = p->next) { g_free (p->data); p->data = NULL; } - g_list_free ((*gbc)->lines); - (*gbc)->lines = NULL; - - for (p = (*gbc)->chars; p != NULL; p = p->next) { - g_free (p->data); - p->data = NULL; - } - g_list_free ((*gbc)->chars); - (*gbc)->chars = NULL; + g_list_free ((*gbc)->shapes); + (*gbc)->shapes = NULL; g_free (*gbc); *gbc = NULL; @@ -290,6 +309,20 @@ gl_barcode_free (glBarcode **gbc) } +/*****************************************************************************/ +/* Add shape to barcode. */ +/*****************************************************************************/ +void +gl_barcode_add_shape (glBarcode *bc, + glBarcodeShape *shape) +{ + g_return_if_fail (bc); + g_return_if_fail (shape); + + bc->shapes = g_list_append (bc->shapes, shape); +} + + /*****************************************************************************/ /* Get a list of names for valid barcode styles. */ /*****************************************************************************/ diff --git a/src/bc.h b/src/bc.h index 1e62a6fa..909f74f2 100644 --- a/src/bc.h +++ b/src/bc.h @@ -26,19 +26,91 @@ G_BEGIN_DECLS +typedef enum { + GL_BARCODE_SHAPE_LINE, + GL_BARCODE_SHAPE_ALPHA, +} glBarcodeShapeType; + typedef struct { - gdouble x, y, length, width; -} glBarcodeLine; + /* Begin Common Fields */ + glBarcodeShapeType type; + gdouble x; + gdouble y; + /* End Common Fields */ + +} glBarcodeShapeAny; + +/* + * glBarcodeShapeLine: + * + * @ = origin (x,y) from top left corner of barcode + * + * +--@--+ + * | | + * | | + * | | + * | | length + * | | + * | | + * | | + * +-----+ + * width + */ typedef struct { - gdouble x, y, fsize; - gchar c; -} glBarcodeChar; + + /* Begin Common Fields */ + glBarcodeShapeType type; /* Always GL_BARCODE_SHAPE_LINE. */ + gdouble x; + gdouble y; + /* End Common Fields */ + + gdouble length; + gdouble width; + +} glBarcodeShapeLine; + +/* + * glBarcodeShapeAlpha: + * + * @ = origin (x,y) from top left corner of barcode + * + * ____ ------------ + * / \ ^ + * / /\ \ | + * / /__\ \ | + * / ______ \ | ~fsize + * / / \ \ | + * /__/ \__\ | + * v + * @ ---------------------- + */ +typedef struct { + + /* Begin Common Fields */ + glBarcodeShapeType type; /* Always GL_BARCODE_SHAPE_ALPHA. */ + gdouble x; + gdouble y; + /* End Common Fields */ + + gdouble fsize; + gchar c; + +} glBarcodeShapeAlpha; + +typedef union { + + glBarcodeShapeType type; + glBarcodeShapeAny any; + + glBarcodeShapeLine line; + glBarcodeShapeAlpha alpha; + +} glBarcodeShape; typedef struct { gdouble width, height; - GList *lines; /* List of glBarcodeLine */ - GList *chars; /* List of glBarcodeChar */ + GList *shapes; /* List of glBarcodeShape */ } glBarcode; typedef glBarcode *(*glBarcodeNewFunc) (const gchar *id, @@ -53,6 +125,9 @@ typedef glBarcode *(*glBarcodeNewFunc) (const gchar *id, #define GL_BARCODE_FONT_WEIGHT PANGO_WEIGHT_NORMAL +glBarcodeShapeLine *gl_barcode_shape_line_new (void); +glBarcodeShapeAlpha *gl_barcode_shape_alpha_new (void); + glBarcode *gl_barcode_new (const gchar *id, gboolean text_flag, gboolean checksum_flag, @@ -62,6 +137,9 @@ glBarcode *gl_barcode_new (const gchar *id, void gl_barcode_free (glBarcode **bc); +void gl_barcode_add_shape (glBarcode *bc, + glBarcodeShape *shape); + 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 710ceaee..0e12b3d4 100644 --- a/src/label-barcode.c +++ b/src/label-barcode.c @@ -419,24 +419,25 @@ draw_object (glLabelObject *object, gboolean screen_flag, glMergeRecord *record) { - gdouble x0, y0; - cairo_matrix_t matrix; - glBarcode *gbc; - glBarcodeLine *line; - glBarcodeChar *bchar; - GList *li; - gdouble y_offset; - PangoLayout *layout; + gdouble x0, y0; + cairo_matrix_t matrix; + glBarcode *gbc; + glBarcodeShape *shape; + glBarcodeShapeLine *line; + glBarcodeShapeAlpha *bchar; + GList *p; + gdouble y_offset; + PangoLayout *layout; PangoFontDescription *desc; - gchar *text, *cstring; - glTextNode *text_node; - gchar *id; - gboolean text_flag; - gboolean checksum_flag; - guint color; - glColorNode *color_node; - guint format_digits; - gdouble w, h; + gchar *text, *cstring; + glTextNode *text_node; + gchar *id; + gboolean text_flag; + gboolean checksum_flag; + guint color; + glColorNode *color_node; + guint format_digits; + gdouble w, h; gl_debug (DEBUG_LABEL, "START"); @@ -493,36 +494,50 @@ draw_object (glLabelObject *object, } else { - for (li = gbc->lines; li != NULL; li = li->next) { - line = (glBarcodeLine *) li->data; + for (p = gbc->shapes; p != NULL; p = p->next) { + shape = (glBarcodeShape *)p->data; + switch (shape->type) + { - cairo_move_to (cr, line->x, line->y); - cairo_line_to (cr, line->x, line->y + line->length); - cairo_set_line_width (cr, line->width); - cairo_stroke (cr); - } + case GL_BARCODE_SHAPE_LINE: + line = (glBarcodeShapeLine *) shape; + + cairo_move_to (cr, line->x, line->y); + cairo_line_to (cr, line->x, line->y + line->length); + cairo_set_line_width (cr, line->width); + cairo_stroke (cr); + + break; + + case GL_BARCODE_SHAPE_ALPHA: + bchar = (glBarcodeShapeAlpha *) shape; + + layout = pango_cairo_create_layout (cr); + + desc = pango_font_description_new (); + pango_font_description_set_family (desc, GL_BARCODE_FONT_FAMILY); + pango_font_description_set_size (desc, bchar->fsize * PANGO_SCALE * FONT_SCALE); + pango_layout_set_font_description (layout, desc); + pango_font_description_free (desc); - for (li = gbc->chars; li != NULL; li = li->next) { - bchar = (glBarcodeChar *) li->data; + cstring = g_strdup_printf ("%c", bchar->c); + pango_layout_set_text (layout, cstring, -1); + g_free (cstring); - layout = pango_cairo_create_layout (cr); + y_offset = 0.2 * bchar->fsize; - desc = pango_font_description_new (); - pango_font_description_set_family (desc, GL_BARCODE_FONT_FAMILY); - pango_font_description_set_size (desc, bchar->fsize * PANGO_SCALE * FONT_SCALE); - pango_layout_set_font_description (layout, desc); - pango_font_description_free (desc); + cairo_move_to (cr, bchar->x, bchar->y-y_offset); + pango_cairo_show_layout (cr, layout); - cstring = g_strdup_printf ("%c", bchar->c); - pango_layout_set_text (layout, cstring, -1); - g_free (cstring); + g_object_unref (layout); - y_offset = 0.2 * bchar->fsize; + break; - cairo_move_to (cr, bchar->x, bchar->y-y_offset); - pango_cairo_show_layout (cr, layout); + default: + g_assert_not_reached (); + break; - g_object_unref (layout); + } } -- 2.39.5