]> git.sur5r.net Git - glabels/commitdiff
Minor refactoring of glBarcode structure.
authorJim Evins <evins@snaught.com>
Sat, 22 May 2010 23:02:23 +0000 (19:02 -0400)
committerJim Evins <evins@snaught.com>
Sat, 22 May 2010 23:02:23 +0000 (19:02 -0400)
Minor refactoring of glBarcode structure in preparation of support for
more exotic barcode shapes.

src/bc-gnubarcode.c
src/bc-iec16022.c
src/bc-iec18004.c
src/bc-postnet.c
src/bc-zint.c
src/bc.c
src/bc.h
src/label-barcode.c

index 3e0ba437f60c54b922614571d3b46b1a83892717..11f132ae7d089f29af3db73175ae459c92e6f620 100644 (file)
@@ -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);
                }
        }
 
index ebaa4e6ba25e9b548a8ae861c6a07b805a134449..834e9f9257b30f1a7e68750c5ea5a1290030c806 100644 (file)
@@ -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);
                         }
 
                 }
index 7070fcabff5a4fc06538f5c0253a247bac3eb5c5..f5f1adafa2d13b623fd59702e47b5e1da3f1e795 100644 (file)
@@ -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);
                         }
 
                 }
index 52c07dba4900366818a327450185b1951d9dcd4c..9a3ba322e4fda0894b579d46e85c54ed57681f68 100644 (file)
@@ -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;
 }
 
index 5c8502c55e51df4e23246e2f5a163dc86c1de78e..0ad80869dd14c8370685aa5df7f9bcdef4b9b318 100644 (file)
@@ -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;
index 3c3d1dc1cf25cde3743783a0495ef57849c8ae18..0761f6e89a6578638cccbb2e08364d76b132adca 100644 (file)
--- 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.                             */
 /*****************************************************************************/
index 1e62a6fa879da42cc50cbaae31905cd925927b83..909f74f24da9941c20a946a6cf055845f4fcdd0d 100644 (file)
--- a/src/bc.h
+++ b/src/bc.h
 
 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);
 
index 710ceaeedba9d3dd5dab5d5aee7f2adc1af0e0c9..0e12b3d4b68d79498343e498cbc44b17f713435e 100644 (file)
@@ -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);
+                        }
 
                }