]> gitweb.michael.orlitzky.com - libsvgtiny.git/blob - svgtiny_test.c
Linear gradients, part 1.
[libsvgtiny.git] / 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 struct stat sb;
19 char *buffer;
20 size_t size;
21 size_t n;
22 struct svgtiny_diagram *diagram;
23 svgtiny_code code;
24
25 if (argc != 2) {
26 fprintf(stderr, "Usage: %s FILE\n", argv[0]);
27 return 1;
28 }
29
30 /* load file into memory buffer */
31 fd = fopen(argv[1], "rb");
32 if (!fd) {
33 perror(argv[1]);
34 return 1;
35 }
36
37 if (stat(argv[1], &sb)) {
38 perror(argv[1]);
39 return 1;
40 }
41 size = sb.st_size;
42
43 buffer = malloc(size);
44 if (!buffer) {
45 fprintf(stderr, "Unable to allocate %lld bytes\n",
46 (long long) size);
47 return 1;
48 }
49
50 n = fread(buffer, 1, size, fd);
51 if (n != size) {
52 perror(argv[1]);
53 return 1;
54 }
55
56 fclose(fd);
57
58 /* create svgtiny object */
59 diagram = svgtiny_create();
60 if (!diagram) {
61 fprintf(stderr, "svgtiny_create failed\n");
62 return 1;
63 }
64
65 /* parse */
66 code = svgtiny_parse(diagram, buffer, size, argv[1], 1000, 1000);
67 if (code != svgtiny_OK) {
68 fprintf(stderr, "svgtiny_parse failed: ");
69 switch (code) {
70 case svgtiny_OUT_OF_MEMORY:
71 fprintf(stderr, "svgtiny_OUT_OF_MEMORY");
72 break;
73 case svgtiny_LIBXML_ERROR:
74 fprintf(stderr, "svgtiny_LIBXML_ERROR");
75 break;
76 case svgtiny_NOT_SVG:
77 fprintf(stderr, "svgtiny_NOT_SVG");
78 break;
79 case svgtiny_SVG_ERROR:
80 fprintf(stderr, "svgtiny_SVG_ERROR: line %i: %s",
81 diagram->error_line,
82 diagram->error_message);
83 break;
84 default:
85 fprintf(stderr, "unknown svgtiny_code %i", code);
86 break;
87 }
88 fprintf(stderr, "\n");
89 }
90
91 free(buffer);
92
93 printf("viewbox 0 0 %i %i\n", diagram->width, diagram->height);
94
95 for (unsigned int i = 0; i != diagram->shape_count; i++) {
96 if (diagram->shape[i].fill == svgtiny_TRANSPARENT)
97 printf("fill none ");
98 else
99 printf("fill #%.6x ", diagram->shape[i].fill);
100 if (diagram->shape[i].stroke == svgtiny_TRANSPARENT)
101 printf("stroke none ");
102 else
103 printf("stroke #%.6x ", diagram->shape[i].stroke);
104 printf("stroke-width %i ", diagram->shape[i].stroke_width);
105 if (diagram->shape[i].path) {
106 printf("path '");
107 for (unsigned int j = 0;
108 j != diagram->shape[i].path_length; ) {
109 switch ((int) diagram->shape[i].path[j]) {
110 case svgtiny_PATH_MOVE:
111 printf("M %g %g ",
112 diagram->shape[i].path[j + 1],
113 diagram->shape[i].path[j + 2]);
114 j += 3;
115 break;
116 case svgtiny_PATH_CLOSE:
117 printf("Z ");
118 j += 1;
119 break;
120 case svgtiny_PATH_LINE:
121 printf("L %g %g ",
122 diagram->shape[i].path[j + 1],
123 diagram->shape[i].path[j + 2]);
124 j += 3;
125 break;
126 case svgtiny_PATH_BEZIER:
127 printf("C %g %g %g %g %g %g ",
128 diagram->shape[i].path[j + 1],
129 diagram->shape[i].path[j + 2],
130 diagram->shape[i].path[j + 3],
131 diagram->shape[i].path[j + 4],
132 diagram->shape[i].path[j + 5],
133 diagram->shape[i].path[j + 6]);
134 j += 7;
135 break;
136 default:
137 printf("error ");
138 j += 1;
139 }
140 }
141 printf("' ");
142 } else if (diagram->shape[i].text) {
143 printf("text %g %g '%s' ",
144 diagram->shape[i].text_x,
145 diagram->shape[i].text_y,
146 diagram->shape[i].text);
147 }
148 printf("\n");
149 }
150
151 svgtiny_free(diagram);
152
153 return 0;
154 }
155