]> gitweb.michael.orlitzky.com - libsvgtiny.git/blob - test/svgtiny_test.c
Prefix build dirs with build- and svn ignore them. Simplify clean rule.
[libsvgtiny.git] / test / svgtiny_test.c
1 /*
2 * This file is part of Libsvgtiny
3 * Licensed under the MIT License,
4 * http://opensource.org/licenses/mit-license.php
5 * Copyright 2008 James Bursa <james@semichrome.net>
6 */
7
8 #include <stdio.h>
9 #include <sys/types.h>
10 #include <sys/stat.h>
11 #include <unistd.h>
12 #include "svgtiny.h"
13
14
15 int main(int argc, char *argv[])
16 {
17 FILE *fd;
18 float scale = 1.0;
19 struct stat sb;
20 char *buffer;
21 size_t size;
22 size_t n;
23 struct svgtiny_diagram *diagram;
24 svgtiny_code code;
25
26 if (argc != 2 && argc != 3) {
27 fprintf(stderr, "Usage: %s FILE [SCALE]\n", argv[0]);
28 return 1;
29 }
30
31 /* load file into memory buffer */
32 fd = fopen(argv[1], "rb");
33 if (!fd) {
34 perror(argv[1]);
35 return 1;
36 }
37
38 if (stat(argv[1], &sb)) {
39 perror(argv[1]);
40 return 1;
41 }
42 size = sb.st_size;
43
44 buffer = malloc(size);
45 if (!buffer) {
46 fprintf(stderr, "Unable to allocate %lld bytes\n",
47 (long long) size);
48 return 1;
49 }
50
51 n = fread(buffer, 1, size, fd);
52 if (n != size) {
53 perror(argv[1]);
54 return 1;
55 }
56
57 fclose(fd);
58
59 /* read scale argument */
60 if (argc == 3) {
61 scale = atof(argv[2]);
62 if (scale == 0)
63 scale = 1.0;
64 }
65
66 /* create svgtiny object */
67 diagram = svgtiny_create();
68 if (!diagram) {
69 fprintf(stderr, "svgtiny_create failed\n");
70 return 1;
71 }
72
73 /* parse */
74 code = svgtiny_parse(diagram, buffer, size, argv[1], 1000, 1000);
75 if (code != svgtiny_OK) {
76 fprintf(stderr, "svgtiny_parse failed: ");
77 switch (code) {
78 case svgtiny_OUT_OF_MEMORY:
79 fprintf(stderr, "svgtiny_OUT_OF_MEMORY");
80 break;
81 case svgtiny_LIBXML_ERROR:
82 fprintf(stderr, "svgtiny_LIBXML_ERROR");
83 break;
84 case svgtiny_NOT_SVG:
85 fprintf(stderr, "svgtiny_NOT_SVG");
86 break;
87 case svgtiny_SVG_ERROR:
88 fprintf(stderr, "svgtiny_SVG_ERROR: line %i: %s",
89 diagram->error_line,
90 diagram->error_message);
91 break;
92 default:
93 fprintf(stderr, "unknown svgtiny_code %i", code);
94 break;
95 }
96 fprintf(stderr, "\n");
97 }
98
99 free(buffer);
100
101 printf("viewbox 0 0 %g %g\n",
102 scale * diagram->width, scale * diagram->height);
103
104 for (unsigned int i = 0; i != diagram->shape_count; i++) {
105 if (diagram->shape[i].fill == svgtiny_TRANSPARENT)
106 printf("fill none ");
107 else
108 printf("fill #%.6x ", diagram->shape[i].fill);
109 if (diagram->shape[i].stroke == svgtiny_TRANSPARENT)
110 printf("stroke none ");
111 else
112 printf("stroke #%.6x ", diagram->shape[i].stroke);
113 printf("stroke-width %g ",
114 scale * diagram->shape[i].stroke_width);
115 if (diagram->shape[i].path) {
116 printf("path '");
117 for (unsigned int j = 0;
118 j != diagram->shape[i].path_length; ) {
119 switch ((int) diagram->shape[i].path[j]) {
120 case svgtiny_PATH_MOVE:
121 printf("M %g %g ",
122 scale * diagram->shape[i].path[j + 1],
123 scale * diagram->shape[i].path[j + 2]);
124 j += 3;
125 break;
126 case svgtiny_PATH_CLOSE:
127 printf("Z ");
128 j += 1;
129 break;
130 case svgtiny_PATH_LINE:
131 printf("L %g %g ",
132 scale * diagram->shape[i].path[j + 1],
133 scale * diagram->shape[i].path[j + 2]);
134 j += 3;
135 break;
136 case svgtiny_PATH_BEZIER:
137 printf("C %g %g %g %g %g %g ",
138 scale * diagram->shape[i].path[j + 1],
139 scale * diagram->shape[i].path[j + 2],
140 scale * diagram->shape[i].path[j + 3],
141 scale * diagram->shape[i].path[j + 4],
142 scale * diagram->shape[i].path[j + 5],
143 scale * diagram->shape[i].path[j + 6]);
144 j += 7;
145 break;
146 default:
147 printf("error ");
148 j += 1;
149 }
150 }
151 printf("' ");
152 } else if (diagram->shape[i].text) {
153 printf("text %g %g '%s' ",
154 scale * diagram->shape[i].text_x,
155 scale * diagram->shape[i].text_y,
156 diagram->shape[i].text);
157 }
158 printf("\n");
159 }
160
161 svgtiny_free(diagram);
162
163 return 0;
164 }
165