]> git.sur5r.net Git - glabels/commitdiff
Refactoring of barcode structure.
authorJim Evins <evins@snaught.com>
Sun, 11 Jul 2010 04:49:15 +0000 (00:49 -0400)
committerJim Evins <evins@snaught.com>
Sun, 11 Jul 2010 04:49:15 +0000 (00:49 -0400)
Minor refactoring of API to barcode structure.

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 11f132ae7d089f29af3db73175ae459c92e6f620..d8d096d9a190f08ba3c67de5b519134fc1fb2608 100644 (file)
@@ -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);
                }
        }
 
index 834e9f9257b30f1a7e68750c5ea5a1290030c806..e56990d4771e6b36e4c3f71333dcc7b7d2eef3fc 100644 (file)
@@ -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);
                         }
 
                 }
index f5f1adafa2d13b623fd59702e47b5e1da3f1e795..33f27891cac894e4cf5dcde00c549c2e54f28e41 100644 (file)
@@ -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);
                         }
 
                 }
index 9a3ba322e4fda0894b579d46e85c54ed57681f68..3aa176510e902214fc5223a85090d131522953ad 100644 (file)
@@ -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;
        }
index ca83f619ae81aa7a30e69c107d035e5eb9dc67c3..c8d6d0793c1b8e0fb872b56a2f0e155354146b8f 100644 (file)
@@ -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);
                 }
         }
 
index 247b50658788f338a16be6686d34a5881dbe93e2..ca3d42f762b816ff7e71e143fa009cc0a94c9ae8 100644 (file)
--- 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.                             */
 /*****************************************************************************/
index 6c7cbcb5aedeaa9c98ddae9d570cf695ccf7c8f8..c32fbd362822f0ede2ad2673b24324929037d606 100644 (file)
--- 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);
index 7f3cf8f3a593dc92acfd04c154bf239577bfeef1..5367261b4d6c87589754a8960b82f73b9ac7fb18 100644 (file)
@@ -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;