Added width and height parameters to rendering for easier output control

This commit is contained in:
Sam Lown 2010-06-11 19:42:51 +02:00
parent 2cea2399d9
commit 847cd70e32
3 changed files with 73 additions and 54 deletions

View File

@ -179,7 +179,7 @@ extern int grid_matrix(struct zint_symbol *symbol, unsigned char source[], int l
extern int png_handle(struct zint_symbol *symbol, int rotate_angle);
#endif
extern int render_plot(struct zint_symbol *symbol, unsigned int hide_text);
extern int render_plot(struct zint_symbol *symbol, unsigned int hide_text, float width, float height);
extern int bmp_handle(struct zint_symbol *symbol, int rotate_angle);
extern int ps_plot(struct zint_symbol *symbol);
@ -868,9 +868,13 @@ int ZBarcode_Encode_File_and_Buffer(struct zint_symbol *symbol, char *filename,
*
* Converts encoded data into an intermediate format to be interpreted
* in other applications using this library.
*
* If the width and height are not set to zero, the barcode will be resized to those
* dimensions. The symbol->scale and symbol->height values are totally ignored in this case.
*
*/
int ZBarcode_Render(struct zint_symbol *symbol, unsigned int hide_text)
int ZBarcode_Render(struct zint_symbol *symbol, unsigned int hide_text, float width, float height)
{
// Send the request to the render_plot method
return render_plot(symbol, hide_text);
return render_plot(symbol, hide_text, width, height);
}

View File

@ -36,7 +36,7 @@ int render_plot_add_line(struct zint_symbol *symbol, struct zint_render_line *li
int render_plot_add_string(struct zint_symbol *symbol, char *text, float x, float y, float fsize, float width, struct zint_render_string **last_string);
int render_plot(struct zint_symbol *symbol, unsigned int hide_text)
int render_plot(struct zint_symbol *symbol, unsigned int hide_text, float width, float height)
{
struct zint_render *render;
struct zint_render_line *line, *last_line = NULL;
@ -51,6 +51,7 @@ int render_plot(struct zint_symbol *symbol, unsigned int hide_text)
float addon_text_posn;
float default_text_posn;
float scaler = symbol->scale;
float w, h;
const char *locale = NULL;
// Allocate memory for the rendered version
@ -68,33 +69,39 @@ int render_plot(struct zint_symbol *symbol, unsigned int hide_text)
comp_offset = 0;
addon_text_posn = 0.0;
if (symbol->height < 15) {
symbol->height = 15;
/*
* Determine if there will be any addon texts and text height
*/
latch = 0;
r = 0;
/* Isolate add-on text */
if(is_extendable(symbol->symbology)) {
for(i = 0; i < ustrlen(symbol->text); i++) {
if (latch == 1) {
addon[r] = symbol->text[i];
r++;
}
if (symbol->text[i] == '+') {
latch = 1;
}
}
}
addon[r] = '\0';
if((symbol->show_hrt == 0) || (ustrlen(symbol->text) != 0)) {
hide_text = 0;
}
// symbol->height = 50;
if(!hide_text && ustrlen(symbol->text)) {
textheight = 9.0;
textoffset = 2.0;
} else {
textheight = textoffset = 0.0;
}
// Update height for texts
symbol->height -= textheight + textoffset;
large_bar_count = 0;
preset_height = 0.0;
for(i = 0; i < symbol->rows; i++) {
preset_height += symbol->row_height[i];
if(symbol->row_height[i] == 0) {
large_bar_count++;
}
}
large_bar_height = (symbol->height - preset_height) / large_bar_count;
if (large_bar_count == 0) {
symbol->height = preset_height;
}
/*
* Calculate the width of the barcode, especially if there are any extra
* borders or white space to add.
*/
while(!(module_is_set(symbol, symbol->rows - 1, comp_offset))) {
comp_offset++;
@ -131,44 +138,52 @@ int render_plot(struct zint_symbol *symbol, unsigned int hide_text)
}
}
latch = 0;
r = 0;
/* Isolate add-on text */
if(is_extendable(symbol->symbology)) {
for(i = 0; i < ustrlen(symbol->text); i++) {
if (latch == 1) {
addon[r] = symbol->text[i];
r++;
}
if (symbol->text[i] == '+') {
latch = 1;
}
}
}
addon[r] = '\0';
if((symbol->show_hrt == 0) || (ustrlen(symbol->text) != 0)) {
hide_text = 0;
}
if(hide_text) {
textoffset = 0;
} else {
textoffset = 3.0;
}
xoffset = symbol->border_width + symbol->whitespace_width;
yoffset = symbol->border_width;
// Calculate the initial scale factor if width provided
w = main_width + (xoffset * 2);
if (width) {
scaler = width / w;
}
/*
* If main_width is bigger than symbol->width we need to recalculate the
* scaler so the barcode fits in the same area!
* Calculate the height
*/
if (main_width != symbol->width) {
scaler = scaler / ((symbol->width + (xoffset * 2.0)) / symbol->width);
if (height) {
symbol->height = height / scaler; // starting height
} else if (symbol->height == 0) {
symbol->height = 50;
}
render->width = (symbol->width + (xoffset * 2)) * scaler;
render->height = (symbol->height + textheight + textoffset + yoffset + yoffset) * scaler;
// Update height for texts
symbol->height -= textheight + textoffset;
// Determine if height should be overridden
large_bar_count = 0;
preset_height = 0.0;
for(i = 0; i < symbol->rows; i++) {
preset_height += symbol->row_height[i];
if(symbol->row_height[i] == 0) {
large_bar_count++;
}
}
large_bar_height = (symbol->height - preset_height) / large_bar_count;
if (large_bar_count == 0) {
symbol->height = preset_height;
}
yoffset = symbol->border_width;
// Calculate the scale factor from the height, incase it needs to be lowered for width
h = (symbol->height + textheight + textoffset + (yoffset * 2));
if ((h * scaler) > height) {
scaler = height / h;
}
// Set initial render dimensions
render->width = w * scaler;
render->height = h * scaler;
if(((symbol->output_options & BARCODE_BOX) != 0) || ((symbol->output_options & BARCODE_BIND) != 0)) {
default_text_posn = (symbol->height + textoffset + symbol->border_width + symbol->border_width) * scaler;

View File

@ -209,7 +209,7 @@ ZINT_EXTERN int ZBarcode_Print(struct zint_symbol *symbol, int rotate_angle);
ZINT_EXTERN int ZBarcode_Encode_and_Print(struct zint_symbol *symbol, unsigned char *input, int length, int rotate_angle);
ZINT_EXTERN int ZBarcode_Encode_File_and_Print(struct zint_symbol *symbol, char *filename, int rotate_angle);
ZINT_EXTERN int ZBarcode_Render(struct zint_symbol *symbol, unsigned int hide_text);
ZINT_EXTERN int ZBarcode_Render(struct zint_symbol *symbol, unsigned int hide_text, float width, float height);
ZINT_EXTERN int ZBarcode_Buffer(struct zint_symbol *symbol, int rotate_angle);
ZINT_EXTERN int ZBarcode_Encode_and_Buffer(struct zint_symbol *symbol, unsigned char *input, int length, int rotate_angle);