From aa64c5578702c0366d3cdc2a6650ce68f8db4f4e Mon Sep 17 00:00:00 2001 From: Jojakim Stahl Date: Wed, 18 Oct 2017 11:09:29 +0200 Subject: [PATCH] Height / width adjustment for certain symbologies with fixed requirements must be done before calculating the scaler --- backend/render.c | 165 +++++++++++++++++++++++++---------------------- 1 file changed, 89 insertions(+), 76 deletions(-) diff --git a/backend/render.c b/backend/render.c index a1cb0c9f..70d30154 100644 --- a/backend/render.c +++ b/backend/render.c @@ -57,7 +57,7 @@ int render_plot_add_hexagon(struct zint_symbol *symbol, struct zint_render_hexag int render_plot_add_string(struct zint_symbol *symbol, unsigned char *text, float x, float y, float fsize, float width, struct zint_render_string **last_string); void render_free(struct zint_symbol *symbol); -int render_plot(struct zint_symbol *symbol, const float width, const float height) { +int render_plot(struct zint_symbol *symbol, float width, float height) { struct zint_render *render; struct zint_render_line *line, *last_line = NULL; struct zint_render_string *last_string = NULL; @@ -219,6 +219,7 @@ int render_plot(struct zint_symbol *symbol, const float width, const float heigh xoffset = symbol->border_width + symbol->whitespace_width; yoffset = symbol->border_width; + // Determine if height should be overridden large_bar_count = 0; preset_height = 0.0; @@ -229,6 +230,93 @@ int render_plot(struct zint_symbol *symbol, const float width, const float heigh } } + + /* Set minimum size of symbol */ + /* Barcode must be at least 2mm high by 2mm across */ + if (width < (2.0 * GL_CONST)) { + width = (2.0 * GL_CONST); + } + x_dimension = width / total_area_width_x / GL_CONST; + if (height < ((x_dimension * ((2 * symbol->border_width) + text_offset + text_height)) + 2.0) * GL_CONST) { + height = ((x_dimension * ((2 * symbol->border_width) + text_offset + text_height)) + 2.0) * GL_CONST; + } + + if (symbol->symbology == BARCODE_CODABAR) { + /* The minimum X-dimension of Codabar is 0.191mm. The minimum bar height is 5mm */ + if (x_dimension < 0.191) { + x_dimension = 0.191; + width = 0.191 * GL_CONST * total_area_width_x; + } + if (height < ((x_dimension * ((2 * symbol->border_width) + text_offset + text_height)) + 5.0) * GL_CONST) { + height = ((x_dimension * ((2 * symbol->border_width) + text_offset + text_height)) + 5.0) * GL_CONST; + } + } + else if (symbol->symbology == BARCODE_CODE49) { + /* The minimum X-dimension of Code 49 is 0.191mm */ + if (x_dimension < 0.191) { + x_dimension = 0.191; + width = 0.191 * GL_CONST * total_area_width_x; + float encoded_symbol_aspect = total_area_width_x; + encoded_symbol_aspect /= (preset_height + (2 * yoffset) + text_offset + text_height); + height = width / encoded_symbol_aspect; + } + } + + if (upceanflag != 0) { + /* The X-dimension of UPC/EAN symbols is fixed at 0.330mm */ + /* The phrase before is questionable. It may scale in certain percentages (80% - 200%). + see https://internationalbarcodes.com/ean-13-specifications/ */ + /* NOTE: This code will need adjustment before it correctly deals with composite symbols */ + x_dimension = 0.330; + width = 0.330 * GL_CONST * total_area_width_x; + /* The height is also fixed */ + switch (upceanflag) { + case 6: + case 12: + case 13: + /* UPC-A, UPC-E and EAN-13 */ + /* Height of bars should be 22.85mm */ + height = ((0.330 * ((2 * symbol->border_width) + text_offset + text_height)) + 22.85) * GL_CONST; + break; + case 8: + /* EAN-8 */ + /* Height of bars should be 18.23mm */ + height = ((0.330 * ((2 * symbol->border_width) + text_offset + text_height)) + 18.23) * GL_CONST; + break; + default: + /* EAN-2 and EAN-5 */ + /* Height of bars should be 21.10mm */ + height = ((0.330 * ((2 * symbol->border_width) + text_offset + text_height)) + 21.10) * GL_CONST; + } + } + + if (symbol->symbology == BARCODE_ONECODE) { + /* The size of USPS Intelligent Mail barcode is fixed */ + x_dimension = 0.508; + width = 0.508 * GL_CONST * total_area_width_x; + height = 4.064 * GL_CONST; + } + else if ((symbol->symbology == BARCODE_POSTNET) || (symbol->symbology == BARCODE_PLANET)) { + /* The size of PostNet and PLANET are fized */ + x_dimension = 0.508; + width = 0.508 * GL_CONST * total_area_width_x; + height = 2.921 * GL_CONST; + } + else if (((symbol->symbology == BARCODE_AUSPOST) || (symbol->symbology == BARCODE_AUSREPLY)) || + ((symbol->symbology == BARCODE_AUSROUTE) || (symbol->symbology == BARCODE_AUSREDIRECT))) { + /* Australia Post use the same sizes as USPS */ + x_dimension = 0.508; + width = 0.508 * GL_CONST * total_area_width_x; + height = 4.064 * GL_CONST; + } + else if ((symbol->symbology == BARCODE_RM4SCC) || (symbol->symbology == BARCODE_KIX)) { + /* Royal Mail and KIX Code uses 22 bars per inch */ + x_dimension = 0.577; + width = 0.577 * GL_CONST * total_area_width_x; + height = 5.22 * GL_CONST; + } + + if (large_bar_count == 0) { float required_aspect = width / height; symbol_aspect = (total_symbol_width_x + (2 * xoffset)) / (preset_height + (2 * yoffset) + text_offset + text_height); @@ -259,81 +347,6 @@ int render_plot(struct zint_symbol *symbol, const float width, const float heigh default_text_posn = (symbol->height + text_offset + symbol->border_width) * scaler; } - x_dimension = render->width / total_area_width_x; - x_dimension /= GL_CONST; - - /* Set minimum size of symbol */ - /* Barcode must be at least 2mm high by 2mm across */ - if (render->height < ((x_dimension * ((2 * symbol->border_width) + text_offset + text_height)) + 2.0) * GL_CONST) { - render->height = ((x_dimension * ((2 * symbol->border_width) + text_offset + text_height)) + 2.0) * GL_CONST; - } - if (render->width < (2.0 * GL_CONST)) { - render->width = (2.0 * GL_CONST); - } - - if (symbol->symbology == BARCODE_CODABAR) { - /* The minimum X-dimension of Codabar is 0.191mm. The minimum bar height is 5mm */ - if (x_dimension < 0.191) { - render->width = 0.191 * GL_CONST * total_area_width_x; - } - if (render->height < ((x_dimension * ((2 * symbol->border_width) + text_offset + text_height)) + 5.0) * GL_CONST) { - render->height = ((x_dimension * ((2 * symbol->border_width) + text_offset + text_height)) + 5.0) * GL_CONST; - } - } - else if (symbol->symbology == BARCODE_CODE49) { - /* The minimum X-dimension of Code 49 is 0.191mm */ - if (x_dimension < 0.191) { - render->width = 0.191 * GL_CONST * total_area_width_x; - render->height = render->width / symbol_aspect; - } - } - - if (upceanflag != 0) { - /* The X-dimension of UPC/EAN symbols is fixed at 0.330mm */ - /* NOTE: This code will need adjustment before it correctly deals with composite symbols */ - render->width = 0.330 * GL_CONST * total_area_width_x; - /* The height is also fixed */ - switch (upceanflag) { - case 6: - case 12: - case 13: - /* UPC-A, UPC-E and EAN-13 */ - /* Height of bars should be 22.85mm */ - render->height = ((0.330 * ((2 * symbol->border_width) + text_offset + text_height)) + 22.85) * GL_CONST; - break; - case 8: - /* EAN-8 */ - /* Height of bars should be 18.23mm */ - render->height = ((0.330 * ((2 * symbol->border_width) + text_offset + text_height)) + 18.23) * GL_CONST; - break; - default: - /* EAN-2 and EAN-5 */ - /* Height of bars should be 21.10mm */ - render->height = ((0.330 * ((2 * symbol->border_width) + text_offset + text_height)) + 21.10) * GL_CONST; - } - } - - if (symbol->symbology == BARCODE_ONECODE) { - /* The size of USPS Intelligent Mail barcode is fixed */ - render->width = 0.508 * GL_CONST * total_area_width_x; - render->height = 4.064 * GL_CONST; - } - else if ((symbol->symbology == BARCODE_POSTNET) || (symbol->symbology == BARCODE_PLANET)) { - /* The size of PostNet and PLANET are fized */ - render->width = 0.508 * GL_CONST * total_area_width_x; - render->height = 2.921 * GL_CONST; - } - else if (((symbol->symbology == BARCODE_AUSPOST) || (symbol->symbology == BARCODE_AUSREPLY)) || - ((symbol->symbology == BARCODE_AUSROUTE) || (symbol->symbology == BARCODE_AUSREDIRECT))) { - /* Australia Post use the same sizes as USPS */ - render->width = 0.508 * GL_CONST * total_area_width_x; - render->height = 4.064 * GL_CONST; - } - else if ((symbol->symbology == BARCODE_RM4SCC) || (symbol->symbology == BARCODE_KIX)) { - /* Royal Mail and KIX Code uses 22 bars per inch */ - render->width = 0.577 * GL_CONST * total_area_width_x; - render->height = 5.22 * GL_CONST; - } if (symbol->symbology == BARCODE_MAXICODE) { struct zint_render_ring *ring;