Correct UPC and EAN rendering

Made corrections to the rendering of UPC and EAN symbols in render.c
including taking account of EAN-2 and EAN-5 addon codes, correct text sizing
and simplified guard bar extension.
This commit is contained in:
Robin Stuart 2010-08-22 10:45:21 +01:00
parent 75067959b4
commit 6702e7a005
2 changed files with 167 additions and 98 deletions

View File

@ -33,7 +33,7 @@
struct zint_symbol *ZBarcode_Create() struct zint_symbol *ZBarcode_Create()
{ {
struct zint_symbol *symbol; struct zint_symbol *symbol;
int i, j; int i;
symbol = (struct zint_symbol*)malloc(sizeof(*symbol)); symbol = (struct zint_symbol*)malloc(sizeof(*symbol));
if (!symbol) return NULL; if (!symbol) return NULL;

View File

@ -51,7 +51,7 @@ int render_plot(struct zint_symbol *symbol, float width, float height)
int i, r, block_width, latch, this_row; int i, r, block_width, latch, this_row;
float textpos, textwidth, large_bar_height, preset_height, row_height, row_posn = 0.0; float textpos, textwidth, large_bar_height, preset_height, row_height, row_posn = 0.0;
// int error_number = 0; // int error_number = 0;
int textoffset, textheight, xoffset, yoffset, textdone, main_width; int textoffset, textheight, xoffset, yoffset, textdone, main_width, addon_width;
char addon[6], textpart[10]; char addon[6], textpart[10];
int large_bar_count, comp_offset; int large_bar_count, comp_offset;
float addon_text_posn; float addon_text_posn;
@ -79,6 +79,7 @@ int render_plot(struct zint_symbol *symbol, float width, float height)
strcpy(addon, ""); strcpy(addon, "");
comp_offset = 0; comp_offset = 0;
addon_text_posn = 0.0; addon_text_posn = 0.0;
addon_width = 0;
/* /*
* Determine if there will be any addon texts and text height * Determine if there will be any addon texts and text height
@ -129,9 +130,27 @@ int render_plot(struct zint_symbol *symbol, float width, float height)
} }
main_width = 96 + comp_offset; main_width = 96 + comp_offset;
break; break;
case 2:
main_width = 22 + comp_offset;
break;
case 5:
main_width = 49 + comp_offset;
break;
default: default:
main_width = 68 + comp_offset; main_width = 68 + comp_offset;
} }
switch(ustrlen(symbol->text)) {
case 11:
case 16:
/* EAN-2 add-on */
addon_width = 31;
break;
case 14:
case 19:
/* EAN-5 add-on */
addon_width = 58;
break;
}
} }
if (((symbol->symbology == BARCODE_UPCA) && (symbol->rows == 1)) || (symbol->symbology == BARCODE_UPCA_CC)) { if (((symbol->symbology == BARCODE_UPCA) && (symbol->rows == 1)) || (symbol->symbology == BARCODE_UPCA_CC)) {
@ -139,6 +158,16 @@ int render_plot(struct zint_symbol *symbol, float width, float height)
symbol->whitespace_width = 10; symbol->whitespace_width = 10;
main_width = 96 + comp_offset; main_width = 96 + comp_offset;
} }
switch(ustrlen(symbol->text)) {
case 15:
/* EAN-2 add-on */
addon_width = 31;
break;
case 18:
/* EAN-5 add-on */
addon_width = 58;
break;
}
} }
if (((symbol->symbology == BARCODE_UPCE) && (symbol->rows == 1)) || (symbol->symbology == BARCODE_UPCE_CC)) { if (((symbol->symbology == BARCODE_UPCE) && (symbol->rows == 1)) || (symbol->symbology == BARCODE_UPCE_CC)) {
@ -146,6 +175,16 @@ int render_plot(struct zint_symbol *symbol, float width, float height)
symbol->whitespace_width = 10; symbol->whitespace_width = 10;
main_width = 51 + comp_offset; main_width = 51 + comp_offset;
} }
switch(ustrlen(symbol->text)) {
case 11:
/* EAN-2 add-on */
addon_width = 31;
break;
case 14:
/* EAN-5 add-on */
addon_width = 58;
break;
}
} }
xoffset = symbol->border_width + symbol->whitespace_width; xoffset = symbol->border_width + symbol->whitespace_width;
@ -163,21 +202,21 @@ int render_plot(struct zint_symbol *symbol, float width, float height)
if (large_bar_count == 0) { if (large_bar_count == 0) {
required_aspect = width / height; required_aspect = width / height;
symbol_aspect = (main_width + (2 * xoffset)) / (preset_height + (2 * yoffset) + textoffset + textheight); symbol_aspect = (main_width + addon_width + (2 * xoffset)) / (preset_height + (2 * yoffset) + textoffset + textheight);
symbol->height = preset_height; symbol->height = preset_height;
if (required_aspect > symbol_aspect) { if (required_aspect > symbol_aspect) {
/* horizontal padding is required */ /* horizontal padding is required */
scaler = height / (preset_height + (2 * yoffset) + textoffset + textheight); scaler = height / (preset_height + (2 * yoffset) + textoffset + textheight);
x_spacer = ((width / scaler) - (main_width + (2 * xoffset))) / 2; x_spacer = ((width / scaler) - (main_width + addon_width + (2 * xoffset))) / 2;
y_spacer = 0.0; y_spacer = 0.0;
} else { } else {
/* vertical padding is required */ /* vertical padding is required */
scaler = width / (main_width + (2 * xoffset)); scaler = width / (main_width + addon_width + (2 * xoffset));
y_spacer = ((height / scaler) - (preset_height + (2 * yoffset) + textoffset + textheight)) / 2; y_spacer = ((height / scaler) - (preset_height + (2 * yoffset) + textoffset + textheight)) / 2;
x_spacer = 0.0; x_spacer = 0.0;
} }
} else { } else {
scaler = width / (main_width + (2 * xoffset)); scaler = width / (main_width + addon_width + (2 * xoffset));
symbol->height = (height / scaler) - ((2 * yoffset) + textoffset + textheight); symbol->height = (height / scaler) - ((2 * yoffset) + textoffset + textheight);
x_spacer = 0.0; x_spacer = 0.0;
@ -259,7 +298,7 @@ int render_plot(struct zint_symbol *symbol, float width, float height)
block_width++; block_width++;
} while (module_is_set(symbol, this_row, i + block_width) == module_is_set(symbol, this_row, i)); } while (module_is_set(symbol, this_row, i + block_width) == module_is_set(symbol, this_row, i));
if((addon_latch == 0) && (r == (symbol->rows - 1)) && (i > main_width)) { if((addon_latch == 0) && (r == (symbol->rows - 1)) && (i > main_width)) {
addon_text_posn = (row_posn + 8.0) * scaler; addon_text_posn = row_posn * scaler;
addon_latch = 1; addon_latch = 1;
} }
if(latch == 1) { if(latch == 1) {
@ -296,43 +335,45 @@ int render_plot(struct zint_symbol *symbol, float width, float height)
case 8: /* EAN-8 */ case 8: /* EAN-8 */
case 11: case 11:
case 14: case 14:
line = render_plot_create_line((0 + xoffset) * scaler, row_posn, scaler, 5.0 * scaler); i = 0;
render_plot_add_line(symbol, line, &last_line); for (line = symbol->rendered->lines; line != NULL; line = line->next) {
line = render_plot_create_line((2 + xoffset) * scaler, row_posn, scaler, 5.0 * scaler); switch(i) {
render_plot_add_line(symbol, line, &last_line); case 0:
line = render_plot_create_line((32 + xoffset) * scaler, row_posn, scaler, 5.0 * scaler); case 1:
render_plot_add_line(symbol, line, &last_line); case 10:
line = render_plot_create_line((34 + xoffset) * scaler, row_posn, scaler, 5.0 * scaler); case 11:
render_plot_add_line(symbol, line, &last_line); case 20:
line = render_plot_create_line((64 + xoffset) * scaler, row_posn, scaler, 5.0 * scaler); case 21:
render_plot_add_line(symbol, line, &last_line); line->length += (5.0 * scaler);
line = render_plot_create_line((66 + xoffset) * scaler, row_posn, scaler, 5.0 * scaler); break;
render_plot_add_line(symbol, line, &last_line); }
i++;
}
for(i = 0; i < 4; i++) { for(i = 0; i < 4; i++) {
textpart[i] = symbol->text[i]; textpart[i] = symbol->text[i];
} }
textpart[4] = '\0'; textpart[4] = '\0';
textpos = 17; textpos = 17;
textwidth = 28.0; textwidth = 4.0 * 8.5;
render_plot_add_string(symbol, (unsigned char *) textpart, (textpos + xoffset) * scaler, default_text_posn, 9.0 * scaler, textwidth * scaler, &last_string); render_plot_add_string(symbol, (unsigned char *) textpart, (textpos + xoffset) * scaler, default_text_posn, 11.0 * scaler, textwidth * scaler, &last_string);
for(i = 0; i < 4; i++) { for(i = 0; i < 4; i++) {
textpart[i] = symbol->text[i + 4]; textpart[i] = symbol->text[i + 4];
} }
textpart[4] = '\0'; textpart[4] = '\0';
textpos = 50; textpos = 50;
render_plot_add_string(symbol, (unsigned char *) textpart, (textpos + xoffset) * scaler, default_text_posn, 9.0 * scaler, textwidth * scaler, &last_string); render_plot_add_string(symbol, (unsigned char *) textpart, (textpos + xoffset) * scaler, default_text_posn, 11.0 * scaler, textwidth * scaler, &last_string);
textdone = 1; textdone = 1;
switch(strlen(addon)) { switch(strlen(addon)) {
case 2: case 2:
textpos = xoffset + 86; textpos = xoffset + 86;
textwidth = 2.0 * 7.0; textwidth = 2.0 * 8.5;
render_plot_add_string(symbol, (unsigned char *) addon, textpos * scaler, addon_text_posn * scaler, 9.0 * scaler, textwidth * scaler, &last_string); render_plot_add_string(symbol, (unsigned char *) addon, textpos * scaler, addon_text_posn * scaler, 11.0 * scaler, textwidth * scaler, &last_string);
break; break;
case 5: case 5:
textpos = xoffset + 100; textpos = xoffset + 100;
textwidth = 5.0 * 7.0; textwidth = 5.0 * 8.5;
render_plot_add_string(symbol, (unsigned char *) addon, textpos * scaler, addon_text_posn * scaler, 9.0 * scaler, textwidth * scaler, &last_string); render_plot_add_string(symbol, (unsigned char *) addon, textpos * scaler, addon_text_posn * scaler, 11.0 * scaler, textwidth * scaler, &last_string);
break; break;
} }
@ -340,49 +381,51 @@ int render_plot(struct zint_symbol *symbol, float width, float height)
case 13: /* EAN 13 */ case 13: /* EAN 13 */
case 16: case 16:
case 19: case 19:
line = render_plot_create_line((0 + xoffset) * scaler, row_posn, scaler, 5.0 * scaler); i = 0;
render_plot_add_line(symbol, line, &last_line); for (line = symbol->rendered->lines; line != NULL; line = line->next) {
line = render_plot_create_line((2 + xoffset) * scaler, row_posn, scaler, 5.0 * scaler); switch(i) {
render_plot_add_line(symbol, line, &last_line); case 0:
line = render_plot_create_line((46 + xoffset) * scaler, row_posn, scaler, 5.0 * scaler); case 1:
render_plot_add_line(symbol, line, &last_line); case 14:
line = render_plot_create_line((48 + xoffset) * scaler, row_posn, scaler, 5.0 * scaler); case 15:
render_plot_add_line(symbol, line, &last_line); case 28:
line = render_plot_create_line((92 + xoffset) * scaler, row_posn, scaler, 5.0 * scaler); case 29:
render_plot_add_line(symbol, line, &last_line); line->length += (5.0 * scaler);
line = render_plot_create_line((94 + xoffset) * scaler, row_posn, scaler, 5.0 * scaler); break;
render_plot_add_line(symbol, line, &last_line); }
i++;
}
textpart[0] = symbol->text[0]; textpart[0] = symbol->text[0];
textpart[1] = '\0'; textpart[1] = '\0';
textpos = -5; // 7 textpos = -5; // 7
textwidth = 7.0; textwidth = 8.5;
render_plot_add_string(symbol, (unsigned char *) textpart, (textpos + xoffset) * scaler, default_text_posn, 9.0 * scaler, textwidth * scaler, &last_string); render_plot_add_string(symbol, (unsigned char *) textpart, (textpos + xoffset) * scaler, default_text_posn, 11.0 * scaler, textwidth * scaler, &last_string);
for(i = 0; i < 6; i++) { for(i = 0; i < 6; i++) {
textpart[i] = symbol->text[i + 1]; textpart[i] = symbol->text[i + 1];
} }
textpart[6] = '\0'; textpart[6] = '\0';
textpos = 25; textpos = 25;
textwidth = 6.0 * 7.0; textwidth = 6.0 * 8.5;
render_plot_add_string(symbol, (unsigned char *) textpart, (textpos + xoffset) * scaler, default_text_posn, 9.0 * scaler, textwidth * scaler, &last_string); render_plot_add_string(symbol, (unsigned char *) textpart, (textpos + xoffset) * scaler, default_text_posn, 11.0 * scaler, textwidth * scaler, &last_string);
for(i = 0; i < 6; i++) { for(i = 0; i < 6; i++) {
textpart[i] = symbol->text[i + 7]; textpart[i] = symbol->text[i + 7];
} }
textpart[6] = '\0'; textpart[6] = '\0';
textpos = 72; textpos = 72;
render_plot_add_string(symbol, (unsigned char *) textpart, (textpos + xoffset) * scaler, default_text_posn, 9.0 * scaler, textwidth * scaler, &last_string); render_plot_add_string(symbol, (unsigned char *) textpart, (textpos + xoffset) * scaler, default_text_posn, 11.0 * scaler, textwidth * scaler, &last_string);
textdone = 1; textdone = 1;
switch(strlen(addon)) { switch(strlen(addon)) {
case 2: case 2:
textpos = xoffset + 114; textpos = xoffset + 114;
textwidth = 2.0 * 7.0; textwidth = 2.0 * 8.5;
render_plot_add_string(symbol, (unsigned char *) addon, textpos * scaler, addon_text_posn * scaler, 9.0 * scaler, textwidth * scaler, &last_string); render_plot_add_string(symbol, (unsigned char *) addon, textpos * scaler, addon_text_posn * scaler, 11.0 * scaler, textwidth * scaler, &last_string);
break; break;
case 5: case 5:
textpos = xoffset + 128; textpos = xoffset + 128;
textwidth = 5.0 * 7.0; textwidth = 5.0 * 8.5;
render_plot_add_string(symbol, (unsigned char *) addon, textpos * scaler, addon_text_posn * scaler, 9.0 * scaler, textwidth * scaler, &last_string); render_plot_add_string(symbol, (unsigned char *) addon, textpos * scaler, addon_text_posn * scaler, 11.0 * scaler, textwidth * scaler, &last_string);
break; break;
} }
break; break;
@ -391,80 +434,106 @@ int render_plot(struct zint_symbol *symbol, float width, float height)
if (((symbol->symbology == BARCODE_UPCA) && (symbol->rows == 1)) || (symbol->symbology == BARCODE_UPCA_CC)) { if (((symbol->symbology == BARCODE_UPCA) && (symbol->rows == 1)) || (symbol->symbology == BARCODE_UPCA_CC)) {
/* guard bar extensions and text formatting for UPCA */ /* guard bar extensions and text formatting for UPCA */
latch = 1; i = 0;
for (line = symbol->rendered->lines; line != NULL; line = line->next) {
switch(i) {
case 0:
case 1:
case 2:
case 3:
case 14:
case 15:
case 26:
case 27:
case 28:
case 29:
line->length += (5.0 * scaler);
break;
}
i++;
}
i = 0 + comp_offset;
do {
block_width = 0;
do {
block_width++;
} while (module_is_set(symbol, symbol->rows - 1, i + block_width) == module_is_set(symbol, symbol->rows - 1, i));
if(latch == 1) {
/* a bar */
line = render_plot_create_line((i + xoffset - comp_offset) * scaler, row_posn, block_width * scaler, 5.0 * scaler);
render_plot_add_line(symbol, line, &last_line);
latch = 0;
} else {
/* a space */
latch = 1;
}
i += block_width;
} while (i < 11 + comp_offset);
line = render_plot_create_line((46 + xoffset) * scaler, row_posn, scaler, 5.0 * scaler);
render_plot_add_line(symbol, line, &last_line);
line = render_plot_create_line((48 + xoffset) * scaler, row_posn, scaler, 5.0 * scaler);
render_plot_add_line(symbol, line, &last_line);
latch = 1;
i = 85 + comp_offset;
do {
block_width = 0;
do {
block_width++;
} while (module_is_set(symbol, symbol->rows - 1, i + block_width) == module_is_set(symbol, symbol->rows - 1, i));
if(latch == 1) {
/* a bar */
line = render_plot_create_line((i + xoffset - comp_offset) * scaler, row_posn, block_width * scaler, 5.0 * scaler);
render_plot_add_line(symbol, line, &last_line);
latch = 0;
} else {
/* a space */
latch = 1;
}
i += block_width;
} while (i < 96 + comp_offset);
textpart[0] = symbol->text[0]; textpart[0] = symbol->text[0];
textpart[1] = '\0'; textpart[1] = '\0';
textpos = -5; textpos = -5;
textwidth = 7.0; textwidth = 6.2;
render_plot_add_string(symbol, (unsigned char *) textpart, (textpos + xoffset) * scaler, default_text_posn, 9.0 * scaler, textwidth * scaler, &last_string); render_plot_add_string(symbol, (unsigned char *) textpart, (textpos + xoffset) * scaler, default_text_posn + (2.0 * scaler), 8.0 * scaler, textwidth * scaler, &last_string);
for(i = 0; i < 5; i++) { for(i = 0; i < 5; i++) {
textpart[i] = symbol->text[i + 1]; textpart[i] = symbol->text[i + 1];
} }
textpart[5] = '\0'; textpart[5] = '\0';
textpos = 27; textpos = 27;
textwidth = 4.0 * 7.0; textwidth = 5.0 * 8.5;
render_plot_add_string(symbol, (unsigned char *) textpart, (textpos + xoffset) * scaler, default_text_posn, 9.0 * scaler, textwidth * scaler, &last_string); render_plot_add_string(symbol, (unsigned char *) textpart, (textpos + xoffset) * scaler, default_text_posn, 11.0 * scaler, textwidth * scaler, &last_string);
for(i = 0; i < 5; i++) { for(i = 0; i < 5; i++) {
textpart[i] = symbol->text[i + 6]; textpart[i] = symbol->text[i + 6];
} }
textpos = 68; textpos = 68;
render_plot_add_string(symbol, (unsigned char *) textpart, (textpos + xoffset) * scaler, default_text_posn, 9.0 * scaler, textwidth * scaler, &last_string); render_plot_add_string(symbol, (unsigned char *) textpart, (textpos + xoffset) * scaler, default_text_posn, 11.0 * scaler, textwidth * scaler, &last_string);
textpart[0] = symbol->text[11]; textpart[0] = symbol->text[11];
textpart[1] = '\0'; textpart[1] = '\0';
textpos = 100; textpos = 100;
textwidth = 7.0; textwidth = 6.2;
render_plot_add_string(symbol, (unsigned char *) textpart, (textpos + xoffset) * scaler, default_text_posn, 9.0 * scaler, textwidth * scaler, &last_string); render_plot_add_string(symbol, (unsigned char *) textpart, (textpos + xoffset) * scaler, default_text_posn + (2.0 * scaler), 8.0 * scaler, textwidth * scaler, &last_string);
textdone = 1; textdone = 1;
switch(strlen(addon)) { switch(strlen(addon)) {
case 2: case 2:
textpos = xoffset + 116; textpos = xoffset + 116;
textwidth = 2.0 * 7.0; textwidth = 2.0 * 8.5;
render_plot_add_string(symbol, (unsigned char *) textpart, (textpos + xoffset) * scaler, default_text_posn, 9.0 * scaler, textwidth * scaler, &last_string); render_plot_add_string(symbol, (unsigned char *) addon, textpos * scaler, addon_text_posn * scaler, 11.0 * scaler, textwidth * scaler, &last_string);
break; break;
case 5: case 5:
textpos = xoffset + 130; textpos = xoffset + 130;
textwidth = 5.0 * 7.0; textwidth = 5.0 * 8.5;
render_plot_add_string(symbol, (unsigned char *) textpart, (textpos + xoffset) * scaler, default_text_posn, 9.0 * scaler, textwidth * scaler, &last_string); render_plot_add_string(symbol, (unsigned char *) addon, textpos * scaler, addon_text_posn * scaler, 11.0 * scaler, textwidth * scaler, &last_string);
break;
}
}
if (((symbol->symbology == BARCODE_UPCE) && (symbol->rows == 1)) || (symbol->symbology == BARCODE_UPCE_CC)) {
/* guard bar extensions and text formatting for UPCE */
i = 0;
for (line = symbol->rendered->lines; line != NULL; line = line->next) {
switch(i) {
case 0:
case 1:
case 14:
case 15:
case 16:
line->length += (5.0 * scaler);
break;
}
i++;
}
textpart[0] = symbol->text[0];
textpart[1] = '\0';
textpos = -5;
textwidth = 6.2;
render_plot_add_string(symbol, (unsigned char *) textpart, (textpos + xoffset) * scaler, default_text_posn + (2.0 * scaler), 8.0 * scaler, textwidth * scaler, &last_string);
for(i = 0; i < 6; i++) {
textpart[i] = symbol->text[i + 1];
}
textpart[6] = '\0';
textpos = 24;
textwidth = 6.0 * 8.5;
render_plot_add_string(symbol, (unsigned char *) textpart, (textpos + xoffset) * scaler, default_text_posn, 11.0 * scaler, textwidth * scaler, &last_string);
textpart[0] = symbol->text[7];
textpart[1] = '\0';
textpos = 55;
textwidth = 6.2;
render_plot_add_string(symbol, (unsigned char *) textpart, (textpos + xoffset) * scaler, default_text_posn + (2.0 * scaler), 8.0 * scaler, textwidth * scaler, &last_string);
textdone = 1;
switch(strlen(addon)) {
case 2:
textpos = xoffset + 70;
textwidth = 2.0 * 8.5;
render_plot_add_string(symbol, (unsigned char *) addon, textpos * scaler, addon_text_posn * scaler, 11.0 * scaler, textwidth * scaler, &last_string);
break;
case 5:
textpos = xoffset + 84;
textwidth = 5.0 * 8.5;
render_plot_add_string(symbol, (unsigned char *) addon, textpos * scaler, addon_text_posn * scaler, 11.0 * scaler, textwidth * scaler, &last_string);
break; break;
} }
} }