]>
gitweb.michael.orlitzky.com - libsvgtiny.git/blob - test/svgtiny_test.c
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>
15 int main(int argc
, char *argv
[])
23 struct svgtiny_diagram
*diagram
;
26 if (argc
!= 2 && argc
!= 3) {
27 fprintf(stderr
, "Usage: %s FILE [SCALE]\n", argv
[0]);
31 /* load file into memory buffer */
32 fd
= fopen(argv
[1], "rb");
38 if (stat(argv
[1], &sb
)) {
44 buffer
= malloc(size
);
46 fprintf(stderr
, "Unable to allocate %lld bytes\n",
51 n
= fread(buffer
, 1, size
, fd
);
59 /* read scale argument */
61 scale
= atof(argv
[2]);
66 /* create svgtiny object */
67 diagram
= svgtiny_create();
69 fprintf(stderr
, "svgtiny_create failed\n");
74 code
= svgtiny_parse(diagram
, buffer
, size
, argv
[1], 1000, 1000);
75 if (code
!= svgtiny_OK
) {
76 fprintf(stderr
, "svgtiny_parse failed: ");
78 case svgtiny_OUT_OF_MEMORY
:
79 fprintf(stderr
, "svgtiny_OUT_OF_MEMORY");
81 case svgtiny_LIBXML_ERROR
:
82 fprintf(stderr
, "svgtiny_LIBXML_ERROR");
85 fprintf(stderr
, "svgtiny_NOT_SVG");
87 case svgtiny_SVG_ERROR
:
88 fprintf(stderr
, "svgtiny_SVG_ERROR: line %i: %s",
90 diagram
->error_message
);
93 fprintf(stderr
, "unknown svgtiny_code %i", code
);
96 fprintf(stderr
, "\n");
101 printf("viewbox 0 0 %g %g\n",
102 scale
* diagram
->width
, scale
* diagram
->height
);
104 for (unsigned int i
= 0; i
!= diagram
->shape_count
; i
++) {
105 if (diagram
->shape
[i
].fill
== svgtiny_TRANSPARENT
)
106 printf("fill none ");
108 printf("fill #%.6x ", diagram
->shape
[i
].fill
);
109 if (diagram
->shape
[i
].stroke
== svgtiny_TRANSPARENT
)
110 printf("stroke none ");
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
) {
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
:
122 scale
* diagram
->shape
[i
].path
[j
+ 1],
123 scale
* diagram
->shape
[i
].path
[j
+ 2]);
126 case svgtiny_PATH_CLOSE
:
130 case svgtiny_PATH_LINE
:
132 scale
* diagram
->shape
[i
].path
[j
+ 1],
133 scale
* diagram
->shape
[i
].path
[j
+ 2]);
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]);
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
);
161 svgtiny_free(diagram
);