mirror of
https://github.com/zint/zint
synced 2024-11-16 20:57:25 +13:00
2019-09-11 HaO E-Mail Christian Schmitz 2019-09-10: svg_plot: check for null symbol->vector.
Reason is unknown.
This commit is contained in:
parent
faeb20dcd7
commit
4dfc090416
281
backend/svg.c
281
backend/svg.c
@ -1,138 +1,143 @@
|
|||||||
/* svg.c - Scalable Vector Graphics */
|
/* svg.c - Scalable Vector Graphics */
|
||||||
|
|
||||||
/*
|
/*
|
||||||
libzint - the open source barcode library
|
libzint - the open source barcode library
|
||||||
Copyright (C) 2009-2018 Robin Stuart <rstuart114@gmail.com>
|
Copyright (C) 2009-2018 Robin Stuart <rstuart114@gmail.com>
|
||||||
|
|
||||||
Redistribution and use in source and binary forms, with or without
|
Redistribution and use in source and binary forms, with or without
|
||||||
modification, are permitted provided that the following conditions
|
modification, are permitted provided that the following conditions
|
||||||
are met:
|
are met:
|
||||||
|
|
||||||
1. Redistributions of source code must retain the above copyright
|
1. Redistributions of source code must retain the above copyright
|
||||||
notice, this list of conditions and the following disclaimer.
|
notice, this list of conditions and the following disclaimer.
|
||||||
2. Redistributions in binary form must reproduce the above copyright
|
2. Redistributions in binary form must reproduce the above copyright
|
||||||
notice, this list of conditions and the following disclaimer in the
|
notice, this list of conditions and the following disclaimer in the
|
||||||
documentation and/or other materials provided with the distribution.
|
documentation and/or other materials provided with the distribution.
|
||||||
3. Neither the name of the project nor the names of its contributors
|
3. Neither the name of the project nor the names of its contributors
|
||||||
may be used to endorse or promote products derived from this software
|
may be used to endorse or promote products derived from this software
|
||||||
without specific prior written permission.
|
without specific prior written permission.
|
||||||
|
|
||||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||||
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||||
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||||
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
|
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
|
||||||
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||||
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||||
OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||||
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||||
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||||
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||||
SUCH DAMAGE.
|
SUCH DAMAGE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <locale.h>
|
#include <locale.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
#ifdef _MSC_VER
|
#ifdef _MSC_VER
|
||||||
#include <malloc.h>
|
#include <malloc.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include "common.h"
|
#include "common.h"
|
||||||
|
|
||||||
int svg_plot(struct zint_symbol *symbol) {
|
int svg_plot(struct zint_symbol *symbol) {
|
||||||
FILE *fsvg;
|
FILE *fsvg;
|
||||||
int error_number = 0;
|
int error_number = 0;
|
||||||
const char *locale = NULL;
|
const char *locale = NULL;
|
||||||
float ax, ay, bx, by, cx, cy, dx, dy, ex, ey, fx, fy;
|
float ax, ay, bx, by, cx, cy, dx, dy, ex, ey, fx, fy;
|
||||||
float radius;
|
float radius;
|
||||||
|
|
||||||
struct zint_vector_rect *rect;
|
struct zint_vector_rect *rect;
|
||||||
struct zint_vector_hexagon *hex;
|
struct zint_vector_hexagon *hex;
|
||||||
struct zint_vector_circle *circle;
|
struct zint_vector_circle *circle;
|
||||||
struct zint_vector_string *string;
|
struct zint_vector_string *string;
|
||||||
|
|
||||||
if (symbol->output_options & BARCODE_STDOUT) {
|
/* Check for no created vector set */
|
||||||
fsvg = stdout;
|
/* E-Mail Christian Schmitz 2019-09-10: reason unknown */
|
||||||
} else {
|
if (symbol->vector == NULL) {
|
||||||
fsvg = fopen(symbol->outfile, "w");
|
return ZINT_ERROR_INVALID_DATA;
|
||||||
}
|
}
|
||||||
if (fsvg == NULL) {
|
if (symbol->output_options & BARCODE_STDOUT) {
|
||||||
strcpy(symbol->errtxt, "660: Could not open output file");
|
fsvg = stdout;
|
||||||
return ZINT_ERROR_FILE_ACCESS;
|
} else {
|
||||||
}
|
fsvg = fopen(symbol->outfile, "w");
|
||||||
|
}
|
||||||
locale = setlocale(LC_ALL, "C");
|
if (fsvg == NULL) {
|
||||||
|
strcpy(symbol->errtxt, "660: Could not open output file");
|
||||||
/* Start writing the header */
|
return ZINT_ERROR_FILE_ACCESS;
|
||||||
fprintf(fsvg, "<?xml version=\"1.0\" standalone=\"no\"?>\n");
|
}
|
||||||
fprintf(fsvg, "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n");
|
|
||||||
fprintf(fsvg, " \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n");
|
locale = setlocale(LC_ALL, "C");
|
||||||
fprintf(fsvg, "<svg width=\"%d\" height=\"%d\" version=\"1.1\"\n", (int) ceil(symbol->vector->width), (int) ceil(symbol->vector->height));
|
|
||||||
fprintf(fsvg, " xmlns=\"http://www.w3.org/2000/svg\">\n");
|
/* Start writing the header */
|
||||||
fprintf(fsvg, " <desc>Zint Generated Symbol\n");
|
fprintf(fsvg, "<?xml version=\"1.0\" standalone=\"no\"?>\n");
|
||||||
fprintf(fsvg, " </desc>\n");
|
fprintf(fsvg, "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n");
|
||||||
fprintf(fsvg, "\n <g id=\"barcode\" fill=\"#%s\">\n", symbol->fgcolour);
|
fprintf(fsvg, " \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n");
|
||||||
|
fprintf(fsvg, "<svg width=\"%d\" height=\"%d\" version=\"1.1\"\n", (int) ceil(symbol->vector->width), (int) ceil(symbol->vector->height));
|
||||||
fprintf(fsvg, " <rect x=\"0\" y=\"0\" width=\"%d\" height=\"%d\" fill=\"#%s\" />\n", (int) ceil(symbol->vector->width), (int) ceil(symbol->vector->height), symbol->bgcolour);
|
fprintf(fsvg, " xmlns=\"http://www.w3.org/2000/svg\">\n");
|
||||||
|
fprintf(fsvg, " <desc>Zint Generated Symbol\n");
|
||||||
|
fprintf(fsvg, " </desc>\n");
|
||||||
rect = symbol->vector->rectangles;
|
fprintf(fsvg, "\n <g id=\"barcode\" fill=\"#%s\">\n", symbol->fgcolour);
|
||||||
while (rect) {
|
|
||||||
fprintf(fsvg, " <rect x=\"%.2f\" y=\"%.2f\" width=\"%.2f\" height=\"%.2f\" />\n", rect->x, rect->y, rect->width, rect->height);
|
fprintf(fsvg, " <rect x=\"0\" y=\"0\" width=\"%d\" height=\"%d\" fill=\"#%s\" />\n", (int) ceil(symbol->vector->width), (int) ceil(symbol->vector->height), symbol->bgcolour);
|
||||||
rect = rect->next;
|
|
||||||
}
|
|
||||||
|
rect = symbol->vector->rectangles;
|
||||||
hex = symbol->vector->hexagons;
|
while (rect) {
|
||||||
while (hex) {
|
fprintf(fsvg, " <rect x=\"%.2f\" y=\"%.2f\" width=\"%.2f\" height=\"%.2f\" />\n", rect->x, rect->y, rect->width, rect->height);
|
||||||
radius = hex->diameter / 2.0;
|
rect = rect->next;
|
||||||
ay = hex->y + (1.0 * radius);
|
}
|
||||||
by = hex->y + (0.5 * radius);
|
|
||||||
cy = hex->y - (0.5 * radius);
|
hex = symbol->vector->hexagons;
|
||||||
dy = hex->y - (1.0 * radius);
|
while (hex) {
|
||||||
ey = hex->y - (0.5 * radius);
|
radius = hex->diameter / 2.0;
|
||||||
fy = hex->y + (0.5 * radius);
|
ay = hex->y + (1.0 * radius);
|
||||||
ax = hex->x;
|
by = hex->y + (0.5 * radius);
|
||||||
bx = hex->x + (0.86 * radius);
|
cy = hex->y - (0.5 * radius);
|
||||||
cx = hex->x + (0.86 * radius);
|
dy = hex->y - (1.0 * radius);
|
||||||
dx = hex->x;
|
ey = hex->y - (0.5 * radius);
|
||||||
ex = hex->x - (0.86 * radius);
|
fy = hex->y + (0.5 * radius);
|
||||||
fx = hex->x - (0.86 * radius);
|
ax = hex->x;
|
||||||
fprintf(fsvg, " <path d=\"M %.2f %.2f L %.2f %.2f L %.2f %.2f L %.2f %.2f L %.2f %.2f L %.2f %.2f Z\" />\n", ax, ay, bx, by, cx, cy, dx, dy, ex, ey, fx, fy);
|
bx = hex->x + (0.86 * radius);
|
||||||
hex = hex->next;
|
cx = hex->x + (0.86 * radius);
|
||||||
}
|
dx = hex->x;
|
||||||
|
ex = hex->x - (0.86 * radius);
|
||||||
circle = symbol->vector->circles;
|
fx = hex->x - (0.86 * radius);
|
||||||
while (circle) {
|
fprintf(fsvg, " <path d=\"M %.2f %.2f L %.2f %.2f L %.2f %.2f L %.2f %.2f L %.2f %.2f L %.2f %.2f Z\" />\n", ax, ay, bx, by, cx, cy, dx, dy, ex, ey, fx, fy);
|
||||||
if (circle->colour) {
|
hex = hex->next;
|
||||||
fprintf(fsvg, " <circle cx=\"%.2f\" cy=\"%.2f\" r=\"%.2f\" fill=\"#%s\" />\n", circle->x, circle->y, circle->diameter / 2.0, symbol->bgcolour);
|
}
|
||||||
} else {
|
|
||||||
fprintf(fsvg, " <circle cx=\"%.2f\" cy=\"%.2f\" r=\"%.2f\" fill=\"#%s\" />\n", circle->x, circle->y, circle->diameter / 2.0, symbol->fgcolour);
|
circle = symbol->vector->circles;
|
||||||
}
|
while (circle) {
|
||||||
circle = circle->next;
|
if (circle->colour) {
|
||||||
}
|
fprintf(fsvg, " <circle cx=\"%.2f\" cy=\"%.2f\" r=\"%.2f\" fill=\"#%s\" />\n", circle->x, circle->y, circle->diameter / 2.0, symbol->bgcolour);
|
||||||
|
} else {
|
||||||
string = symbol->vector->strings;
|
fprintf(fsvg, " <circle cx=\"%.2f\" cy=\"%.2f\" r=\"%.2f\" fill=\"#%s\" />\n", circle->x, circle->y, circle->diameter / 2.0, symbol->fgcolour);
|
||||||
while (string) {
|
}
|
||||||
fprintf(fsvg, " <text x=\"%.2f\" y=\"%.2f\" text-anchor=\"middle\"\n", string->x, string->y);
|
circle = circle->next;
|
||||||
fprintf(fsvg, " font-family=\"Helvetica\" font-size=\"%.1f\" fill=\"#%s\" >\n", string->fsize, symbol->fgcolour);
|
}
|
||||||
fprintf(fsvg, " %s\n", string->text);
|
|
||||||
fprintf(fsvg, " </text>\n");
|
string = symbol->vector->strings;
|
||||||
string = string->next;
|
while (string) {
|
||||||
}
|
fprintf(fsvg, " <text x=\"%.2f\" y=\"%.2f\" text-anchor=\"middle\"\n", string->x, string->y);
|
||||||
|
fprintf(fsvg, " font-family=\"Helvetica\" font-size=\"%.1f\" fill=\"#%s\" >\n", string->fsize, symbol->fgcolour);
|
||||||
fprintf(fsvg, " </g>\n");
|
fprintf(fsvg, " %s\n", string->text);
|
||||||
fprintf(fsvg, "</svg>\n");
|
fprintf(fsvg, " </text>\n");
|
||||||
|
string = string->next;
|
||||||
if (symbol->output_options & BARCODE_STDOUT) {
|
}
|
||||||
fflush(fsvg);
|
|
||||||
} else {
|
fprintf(fsvg, " </g>\n");
|
||||||
fclose(fsvg);
|
fprintf(fsvg, "</svg>\n");
|
||||||
}
|
|
||||||
|
if (symbol->output_options & BARCODE_STDOUT) {
|
||||||
if (locale)
|
fflush(fsvg);
|
||||||
setlocale(LC_ALL, locale);
|
} else {
|
||||||
|
fclose(fsvg);
|
||||||
return error_number;
|
}
|
||||||
}
|
|
||||||
|
if (locale)
|
||||||
|
setlocale(LC_ALL, locale);
|
||||||
|
|
||||||
|
return error_number;
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user