Add support for rendering UPC

Extended bars and text formatting for UPC barcodes now included in render.c
This commit is contained in:
Robin Stuart 2010-06-13 11:56:31 +01:00
parent 95e51cf96d
commit 807d9460d1

View File

@ -51,7 +51,7 @@ int render_plot(struct zint_symbol *symbol, unsigned int hide_text, float width,
float addon_text_posn; float addon_text_posn;
float default_text_posn; float default_text_posn;
float scaler = symbol->scale; float scaler = symbol->scale;
float w, h; float w, h;
const char *locale = NULL; const char *locale = NULL;
// Allocate memory for the rendered version // Allocate memory for the rendered version
@ -140,25 +140,23 @@ int render_plot(struct zint_symbol *symbol, unsigned int hide_text, float width,
xoffset = symbol->border_width + symbol->whitespace_width; xoffset = symbol->border_width + symbol->whitespace_width;
// Calculate the initial scale factor if width provided // Calculate the initial scale factor if width provided
w = main_width + (xoffset * 2); w = main_width + (xoffset * 2);
if (width) { if (width) {
scaler = width / w; scaler = width / w;
} }
/* // Calculate the height
* Calculate the height if (height) {
*/ symbol->height = height / scaler; // starting height
if (height) { } else if (symbol->height == 0) {
symbol->height = height / scaler; // starting height
} else if (symbol->height == 0) {
symbol->height = 50; symbol->height = 50;
} }
// Update height for texts // Update height for texts
symbol->height -= textheight + textoffset; symbol->height -= textheight + textoffset;
// Determine if height should be overridden // Determine if height should be overridden
large_bar_count = 0; large_bar_count = 0;
preset_height = 0.0; preset_height = 0.0;
for(i = 0; i < symbol->rows; i++) { for(i = 0; i < symbol->rows; i++) {
@ -174,15 +172,15 @@ int render_plot(struct zint_symbol *symbol, unsigned int hide_text, float width,
} }
yoffset = symbol->border_width; yoffset = symbol->border_width;
// Calculate the scale factor from the height, incase it needs to be lowered for width // Calculate the scale factor from the height, incase it needs to be lowered for width
h = (symbol->height + textheight + textoffset + (yoffset * 2)); h = (symbol->height + textheight + textoffset + (yoffset * 2));
if ((h * scaler) > height) { if ((h * scaler) > height) {
scaler = height / h; scaler = height / h;
} }
// Set initial render dimensions // Set initial render dimensions
render->width = w * scaler; render->width = w * scaler;
render->height = h * scaler; render->height = h * scaler;
if(((symbol->output_options & BARCODE_BOX) != 0) || ((symbol->output_options & BARCODE_BIND) != 0)) { if(((symbol->output_options & BARCODE_BOX) != 0) || ((symbol->output_options & BARCODE_BIND) != 0)) {
@ -295,7 +293,7 @@ int render_plot(struct zint_symbol *symbol, unsigned int hide_text, float width,
} }
textpart[4] = '\0'; textpart[4] = '\0';
textpos = 17; textpos = 17;
textwidth = 28.0; textwidth = 28.0;
render_plot_add_string(symbol, textpart, (textpos + xoffset) * scaler, default_text_posn, 9.0 * scaler, textwidth * scaler, &last_string); render_plot_add_string(symbol, textpart, (textpos + xoffset) * scaler, default_text_posn, 9.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];
@ -307,12 +305,12 @@ int render_plot(struct zint_symbol *symbol, unsigned int hide_text, float width,
switch(strlen(addon)) { switch(strlen(addon)) {
case 2: case 2:
textpos = xoffset + 86; textpos = xoffset + 86;
textwidth = 2.0 * 7.0; textwidth = 2.0 * 7.0;
render_plot_add_string(symbol, addon, textpos * scaler, addon_text_posn * scaler, 9.0 * scaler, textwidth * scaler, &last_string); render_plot_add_string(symbol, addon, textpos * scaler, addon_text_posn * scaler, 9.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 * 7.0;
render_plot_add_string(symbol, addon, textpos * scaler, addon_text_posn * scaler, 9.0 * scaler, textwidth * scaler, &last_string); render_plot_add_string(symbol, addon, textpos * scaler, addon_text_posn * scaler, 9.0 * scaler, textwidth * scaler, &last_string);
break; break;
} }
@ -337,7 +335,7 @@ int render_plot(struct zint_symbol *symbol, unsigned int hide_text, float width,
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 = 7.0;
render_plot_add_string(symbol, textpart, (textpos + xoffset) * scaler, default_text_posn, 9.0 * scaler, textwidth * scaler, &last_string); render_plot_add_string(symbol, textpart, (textpos + xoffset) * scaler, default_text_posn, 9.0 * scaler, textwidth * scaler, &last_string);
for(i = 0; i < 6; i++) { for(i = 0; i < 6; i++) {
@ -345,7 +343,7 @@ int render_plot(struct zint_symbol *symbol, unsigned int hide_text, float width,
} }
textpart[6] = '\0'; textpart[6] = '\0';
textpos = 25; textpos = 25;
textwidth = 6.0 * 7.0; textwidth = 6.0 * 7.0;
render_plot_add_string(symbol, textpart, (textpos + xoffset) * scaler, default_text_posn, 9.0 * scaler, textwidth * scaler, &last_string); render_plot_add_string(symbol, textpart, (textpos + xoffset) * scaler, default_text_posn, 9.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];
@ -357,23 +355,103 @@ int render_plot(struct zint_symbol *symbol, unsigned int hide_text, float width,
switch(strlen(addon)) { switch(strlen(addon)) {
case 2: case 2:
textpos = xoffset + 114; textpos = xoffset + 114;
textwidth = 2.0 * 7.0; textwidth = 2.0 * 7.0;
render_plot_add_string(symbol, addon, textpos * scaler, addon_text_posn * scaler, 9.0 * scaler, textwidth * scaler, &last_string); render_plot_add_string(symbol, addon, textpos * scaler, addon_text_posn * scaler, 9.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 * 7.0;
render_plot_add_string(symbol, addon, textpos * scaler, addon_text_posn * scaler, 9.0 * scaler, textwidth * scaler, &last_string); render_plot_add_string(symbol, addon, textpos * scaler, addon_text_posn * scaler, 9.0 * scaler, textwidth * scaler, &last_string);
break; break;
} }
break; break;
} }
} }
if (((symbol->symbology == BARCODE_UPCA) && (symbol->rows == 1)) || (symbol->symbology == BARCODE_UPCA_CC)) {
/* guard bar extensions and text formatting for UPCA */
latch = 1;
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[1] = '\0';
textpos = -5;
textwidth = 7.0;
render_plot_add_string(symbol, textpart, (textpos + xoffset) * scaler, default_text_posn, 9.0 * scaler, textwidth * scaler, &last_string);
for(i = 0; i < 5; i++) {
textpart[i] = symbol->text[i + 1];
}
textpart[5] = '\0';
textpos = 27;
textwidth = 4.0 * 7.0;
render_plot_add_string(symbol, textpart, (textpos + xoffset) * scaler, default_text_posn, 9.0 * scaler, textwidth * scaler, &last_string);
for(i = 0; i < 5; i++) {
textpart[i] = symbol->text[i + 6];
}
textpos = 68;
render_plot_add_string(symbol, textpart, (textpos + xoffset) * scaler, default_text_posn, 9.0 * scaler, textwidth * scaler, &last_string);
textpart[0] = symbol->text[11];
textpart[1] = '\0';
textpos = 100;
textwidth = 7.0;
render_plot_add_string(symbol, textpart, (textpos + xoffset) * scaler, default_text_posn, 9.0 * scaler, textwidth * scaler, &last_string);
textdone = 1;
switch(strlen(addon)) {
case 2:
textpos = xoffset + 116;
textwidth = 2.0 * 7.0;
render_plot_add_string(symbol, textpart, (textpos + xoffset) * scaler, default_text_posn, 9.0 * scaler, textwidth * scaler, &last_string);
break;
case 5:
textpos = xoffset + 130;
textwidth = 5.0 * 7.0;
render_plot_add_string(symbol, textpart, (textpos + xoffset) * scaler, default_text_posn, 9.0 * scaler, textwidth * scaler, &last_string);
break;
}
}
/* Put normal human readable text at the bottom (and centered) */ /* Put normal human readable text at the bottom (and centered) */
if (textdone == 0) { if (textdone == 0) {
// caculate start xoffset to center text // caculate start xoffset to center text
render_plot_add_string(symbol, (char *) symbol->text, (symbol->width / 2.0) * scaler, default_text_posn, 9.0 * scaler, 0.0, &last_string); render_plot_add_string(symbol, (char *) symbol->text, (symbol->width / 2.0) * scaler, default_text_posn, 9.0 * scaler, 0.0, &last_string);
} }
} }
@ -429,20 +507,20 @@ int render_plot_add_string(struct zint_symbol *symbol,
{ {
struct zint_render_string *string; struct zint_render_string *string;
string = malloc(sizeof(struct zint_render_string)); string = malloc(sizeof(struct zint_render_string));
string->next = NULL; string->next = NULL;
string->x = x; string->x = x;
string->y = y; string->y = y;
string->width = width; string->width = width;
string->fsize = fsize; string->fsize = fsize;
string->text = malloc(sizeof(char) * (ustrlen((unsigned char *) text) + 1)); string->text = malloc(sizeof(char) * (ustrlen((unsigned char *) text) + 1));
strcpy(string->text, text); strcpy(string->text, text);
if (*last_string) if (*last_string)
(*last_string)->next = string; (*last_string)->next = string;
else else
symbol->rendered->strings = string; // First character symbol->rendered->strings = string; // First character
*last_string = string; *last_string = string;
return 1; return 1;
} }