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;
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;
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 =
}
bchar->fsize = f2 * FONT_SCALE * scalef;
bchar->c = c;
- gbc->chars = g_list_append (gbc->chars, bchar);
+ gl_barcode_add_shape (gbc, (glBarcodeShape *)bchar);
}
}
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. */
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);
}
}
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. */
* 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);
}
}
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) ) {
/* 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') {
}
line->width = POSTNET_BAR_WIDTH;
- gbc->lines = g_list_append (gbc->lines, line);
+ gl_barcode_add_shape (gbc, (glBarcodeShape *)line);
x += POSTNET_BAR_PITCH;
}
gbc->width = x + POSTNET_HORIZ_MARGIN;
gbc->height = POSTNET_FULLBAR_HEIGHT + 2 * POSTNET_VERT_MARGIN;
- gbc->chars = NULL;
-
return gbc;
}
*--------------------------------------------------------------------------*/
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;
}
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!!! */
}
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;
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;
}
+/*****************************************************************************/
+/* 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.*/
/*****************************************************************************/
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;
}
+/*****************************************************************************/
+/* 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. */
/*****************************************************************************/
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,
#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,
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);
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");
} 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);
+ }
}