forked from Embroidermodder/libembroidery
-
Notifications
You must be signed in to change notification settings - Fork 0
/
format-svg.c
1984 lines (1793 loc) · 77.2 KB
/
format-svg.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#define ARDUINO 1
/**
* Writes out a \a color to the EmbFile* \a file in hex format without using
* printf or varadic functions (for embedded systems).
*/
static void writeColor(EmbFile* file, EmbColor color)
{
char str[8];
const char hex[] = "0123456789ABCDEF";
str[0] = '#';
str[1] = hex[color.r % 16];
str[2] = hex[color.r / 16];
str[3] = hex[color.g % 16];
str[4] = hex[color.g / 16];
str[5] = hex[color.b % 16];
str[6] = hex[color.b / 16];
str[7] = 0;
embFile_print(file, str);
}
static void writePoint(EmbFile* file, double x, double y, int space)
{
if (space) {
embFile_print(file, " ");
}
writeFloat(file, x);
embFile_print(file, ",");
writeFloat(file, y);
}
void writeFloat(EmbFile* file, float number)
{
/* TODO: fix bugs in embFloatToArray */
/* char buffer[30];
embFloatToArray(buffer, number, 1.0e-7, 3, 5);
embFile_print(file, buffer);*/
fprintf(file->file, "%f", number);
}
#if ARDUINO
int readSvg(EmbPattern *pattern, EmbFile *file, const char *fileName)
{
return 0;
}
int writeSvg(EmbPattern *pattern, EmbFile *file, const char *fileName)
{
return 0;
}
#else
/* path flag codes */
#define LINETO 0
#define MOVETO 1
#define BULGETOCONTROL 2
#define BULGETOEND 4
#define ELLIPSETORAD 8
#define ELLIPSETOEND 16
#define CUBICTOCONTROL1 32
#define CUBICTOCONTROL2 64
#define CUBICTOEND 128
#define QUADTOCONTROL 256
#define QUADTOEND 512
/**
* EMBEDDED SYSTEMS OPTIMIZATION
*
* All tokens as unsigned char, that way we can store the subsets of strings
* in the smaller unsigned char array form.
*/
#define TOKEN_AUDIO_LEVEL 0
#define TOKEN_BUFFERED_AUDIO_RENDERING 1
#define TOKEN_COLOR 2
#define TOKEN_COLOR_RENDERING 3
#define TOKEN_DIRECTION 4
#define TOKEN_DISPLAY 5
#define TOKEN_DISPLAY_ALIGN 6
#define TOKEN_FILL 7
#define TOKEN_FILL_OPACITY 8
#define TOKEN_FILL_RULE 9
#define TOKEN_FONT_FAMILY 11
#define TOKEN_FONT_SIZE 12
#define TOKEN_FONT_STYLE 13
#define TOKEN_FONT_VARIANT 14
#define TOKEN_FONT_WEIGHT 15
#define TOKEN_IMAGE_RENDERING 16
#define TOKEN_LINE_INCREMENT 17
#define TOKEN_OPACITY 18
#define TOKEN_POINTER_EVENTS 19
#define TOKEN_SHAPE_RENDERING 20
#define TOKEN_SOLID_COLOR 21
#define TOKEN_ZOOM_AND_PAN 21
#define SVG_CREATOR_NULL 0
#define SVG_CREATOR_EMBROIDERMODDER 1
#define SVG_CREATOR_ILLUSTRATOR 2
#define SVG_CREATOR_INKSCAPE 3
#define SVG_EXPECT_NULL 0
#define SVG_EXPECT_ELEMENT 1
#define SVG_EXPECT_ATTRIBUTE 2
#define SVG_EXPECT_VALUE 3
/* SVG_TYPES */
#define SVG_NULL 0
#define SVG_ELEMENT 1
#define SVG_PROPERTY 2
#define SVG_MEDIA_PROPERTY 3
#define SVG_ATTRIBUTE 4
#define SVG_CATCH_ALL 5
static void writePoint(EmbFile* file, double x, double y, int space);
static void writeColor(EmbFile* file, EmbColor color);
static void writeCircles(EmbPattern* pattern, EmbFile* file);
static void writeEllipse(EmbPattern* pattern, EmbFile* file);
static void writePoints(EmbPattern* pattern, EmbFile* file);
static void writePolygons(EmbPattern* pattern, EmbFile* file);
static void writePolylines(EmbPattern* pattern, EmbFile* file);
static void writeStitchList(EmbPattern* pattern, EmbFile* file);
/**
* Tests for the presense of a string \a s in the supplied
* \a array.
*
* The end of the array is marked by an empty string.
*
* @return 0 if not present 1 if present.
*/
static int stringInArray(const char* s, const char** array)
{
int i;
for (i = 0; array[i][0]; i++) {
if (!strcmp(s, array[i])) {
return 1;
}
}
return 0;
}
/**
* Function is similar to the Unix utility tr.
*
* Character for character replacement in strings.
* Takes a string \a s and for every character in the
* \a from string replace with the corresponding character
* in the \a to string.
*
* For example: ("test", "tb", "..") -> ".es."
*/
static void charReplace(char* s, const char* from, const char* to)
{
int i;
for (; *s; s++) {
for (i = 0; from[i]; i++) {
if (*s == from[i]) {
*s = to[i];
}
}
}
}
static const char* svg_all_tokens[] = {
/* Catch All Properties */
"audio-level", "buffered-rendering", "color", "color-rendering", "direction",
"display", "display-align", "fill", "fill-opacity", "fill-rule",
"font-family", "font-size", "font-style", "font-variant", "font-weight",
"image-rendering", "line-increment", "opacity", "pointer-events", "shape-rendering",
"solid-color", "solid-opacity", "stop-color", "stop-opacity", "stroke",
"stroke-dasharray", "stroke-linecap", "stroke-linejoin",
"stroke-miterlimit", "stroke-opacity", "stroke-width",
"text-align", "text-anchor", "text-rendering", "unicode-bidi",
"vector-effect", "viewport-fill", "viewport-fill-opacity", "visibility",
/* Catch All Attributes */
"about", "accent-height", "accumulate", "additive", "alphabetic",
"arabic-form", "ascent", "attributeName", "attributeType", "bandwidth",
"baseProfile", "bbox", "begin", "by", "calcMode",
"cap-height", "class", "content", "contentScriptType", "cx", "cy",
"d", "datatype", "defaultAction", "descent", "dur", "editable",
"end", "ev:event", "event", "externalResourcesRequired",
"focusHighlight", "focusable", "font-family", "font-stretch",
"font-style", "font-variant", "font-weight", "from", "g1", "g2",
"glyph-name", "gradientUnits", "handler", "hanging", "height",
"horiz-adv-x", "horiz-origin-x", "id", "ideographic",
"initialVisibility", "k", "keyPoints", "keySplines", "keyTimes",
"lang", "mathematical", "max", "mediaCharacterEncoding",
"mediaContentEncodings", "mediaSize", "mediaTime", "min",
"nav-down", "nav-down-left", "nav-down-right", "nav-left", "nav-next",
"nav-prev", "nav-right", "nav-up", "nav-up-left", "nav-up-right",
"observer", "offset", "origin", "overlay", "overline-position",
"overline-thickness", "panose-1", "path", "pathLength", "phase",
"playbackOrder", "points", "preserveAspectRatio", "propagate",
"property", "r", "rel", "repeatCount", "repeatDur",
"requiredExtensions", "requiredFeatures", "requiredFonts",
"requiredFormats", "resource", "restart", "rev", "role", "rotate",
"rx", "ry", "slope", "snapshotTime", "stemh", "stemv",
"strikethrough-position", "strikethrough-thickness", "syncBehavior",
"syncBehaviorDefault", "syncMaster", "syncTolerance",
"syncToleranceDefault", "systemLanguage", "target", "timelineBegin",
"to", "transform", "transformBehavior", "type", "typeof", "u1", "u2",
"underline-position", "underline-thickness", "unicode", "unicode-range",
"units-per-em", "values", "version", "viewBox", "width", "widths",
"x", "x-height", "x1", "x2", "xlink:actuate", "xlink:arcrole",
"xlink:href", "xlink:role", "xlink:show", "xlink:title", "xlink:type",
"xml:base", "xml:id", "xml:lang", "xml:space", "y", "y1", "y2",
"zoomAndPan", "/", "\0"
};
typedef struct SvgAttribute_ {
char* name;
char* value;
} SvgAttribute;
typedef struct SvgAttributeList_ {
SvgAttribute attribute;
struct SvgAttributeList_* next;
} SvgAttributeList;
typedef struct SvgElement_ {
char* name;
SvgAttributeList* attributeList;
SvgAttributeList* lastAttribute;
} SvgElement;
static int svgCreator;
static int svgExpect;
static int svgMultiValue;
static SvgElement* currentElement;
static char* currentAttribute;
static char* currentValue;
#define ELEMENT_XML 0
#define ELEMENT_A 1
#define ELEMENT_ANIMATE 2
#define ELEMENT_ANIMATE_COLOR 3
#define ELEMENT_ANIMATE_MOTION 4
#define ELEMENT_ANIMATE_TRANSFORM 5
#define ELEMENT_ANIMATION 6
#define ELEMENT_AUDIO 7
#define ELEMENT_CIRCLE 8
#define ELEMENT_DEFS 9
#define ELEMENT_DESC 10
#define ELEMENT_DISCARD 11
#define ELEMENT_ELLIPSE 12
#define ELEMENT_FONT 13
#define ELEMENT_FONT_FACE 14
#define ELEMENT_FONT_FACE_SRC 15
#define ELEMENT_FONT_FACE_URI 16
#define ELEMENT_FOREIGN_OBJECT 17
#define ELEMENT_G 18
#define ELEMENT_GLYPH 19
#define ELEMENT_HANDLER 20
#define ELEMENT_HKERN 21
#define ELEMENT_IMAGE 22
#define ELEMENT_LINE 23
#define ELEMENT_LINEAR_GRADIENT 24
#define ELEMENT_LISTENER 25
#define ELEMENT_METADATA 26
#define ELEMENT_MISSING_GLYPH 27
#define ELEMENT_MPATH 28
#define ELEMENT_PATH 29
#define ELEMENT_POLYGON 30
#define ELEMENT_POLYLINE 31
#define ELEMENT_PREFETCH 32
#define ELEMENT_RADIAL_GRADIENT 33
#define ELEMENT_RECT 34
#define ELEMENT_SCRIPT 35
#define ELEMENT_SET 36
#define ELEMENT_SOLID_COLOR 37
#define ELEMENT_STOP 38
#define ELEMENT_SVG 39
#define ELEMENT_SWITCH 40
#define ELEMENT_TBREAK 41
#define ELEMENT_TEXT 42
#define ELEMENT_TEXT_AREA 43
#define ELEMENT_TITLE 44
#define ELEMENT_TSPAN 45
#define ELEMENT_USE 46
#define ELEMENT_VIDEO 47
#define ELEMENT_UNKNOWN 48
static const char* svg_element_tokens[] = {
"?xml", "a", "animate", "animateColor", "animateMotion", "animateTransform", "animation",
"audio", "circle", "defs", "desc", "discard", "ellipse",
"font", "font-face", "font-face-src", "font-face-uri", "foreignObject",
"g", "glyph", "handler", "hkern", "image", "line", "linearGradient", "listener",
"metadata", "missing-glyph", "mpath", "path", "polygon", "polyline", "prefetch",
"radialGradient", "rect", "script", "set", "solidColor", "stop", "svg", "switch",
"tbreak", "text", "textArea", "title", "tspan", "use", "video", "\0"
/* "altGlyph", "altGlyphDef", "altGlyphItem", "clipPath", "color-profile", "cursor",
* "feBlend", "feColorMatrix", "feComponentTransfer", "feComposite", "feConvolveMatrix",
* "feDiffuseLighting", "feDisplacementMap", "feDistantLight", "feFlood",
* "feFuncA", "feFuncB", "feFuncG", "feFuncR", "feGaussianBlur", "feImage",
* "feMerge", "feMergeNode", "feMorphology", "feOffset", "fePointLight",
* "feSpecularLighting", "feSpotLight", "feTile", "feTurbulence", "filter",
* "font-face-format", "font-face-name", "glyphRef", "marker", "mask",
* "pattern", "style", "symbol", "textPath", "tref", "view", "vkern"
* TODO: not implemented SVG Full 1.1 Spec Elements
*/
};
static const char* svg_media_property_tokens[] = {
"audio-level", "buffered-rendering", "display", "image-rendering",
"pointer-events", "shape-rendering", "text-rendering", "viewport-fill",
"viewport-fill-opacity", "visibility", "\0"
};
static const char* svg_property_tokens[] = {
"audio-level", "buffered-rendering", "color", "color-rendering", "direction",
"display", "display-align", "fill", "fill-opacity", "fill-rule",
"font-family", "font-size", "font-style", "font-variant", "font-weight",
"image-rendering", "line-increment", "opacity", "pointer-events",
"shape-rendering", "solid-color", "solid-opacity", "stop-color",
"stop-opacity", "stroke", "stroke-dasharray", "stroke-linecap", "stroke-linejoin",
"stroke-miterlimit", "stroke-opacity", "stroke-width", "text-align",
"text-anchor", "text-rendering", "unicode-bidi", "vector-effect",
"viewport-fill", "viewport-fill-opacity", "visibility", "\0"
};
EmbColor svgColorToEmbColor(char* colorStr)
{
EmbColor c;
char* pEnd = 0;
int i, length, percent;
/* Trim out any junk spaces */
length = 0;
for (i=0; colorStr[i]; i++) {
if (colorStr[i] == ' ' || colorStr[i] == '\t') continue;
if (colorStr[i] == '\n' || colorStr[i] == '\r') continue;
if (colorStr[i] == '%') percent = 1;
colorStr[length] = colorStr[i];
length++;
}
colorStr[length] = 0;
/* SVGTiny1.2 Spec Section 11.13.1 syntax for color values */
if (colorStr[0] == '#') {
if (length == 7) {
/* Six digit hex — #rrggbb */
c = embColor_fromHexStr(colorStr+1);
}
else {
/* Three digit hex — #rgb */
/* Convert the 3 digit hex to a six digit hex */
char hex[7];
hex[0] = colorStr[1];
hex[1] = colorStr[1];
hex[2] = colorStr[2];
hex[3] = colorStr[2];
hex[4] = colorStr[3];
hex[5] = colorStr[3];
hex[6] = 0;
c = embColor_fromHexStr(hex);
}
} else if (percent) {
/* Float functional — rgb(R%, G%, B%) */
charReplace(colorStr, "rgb,()%", " ");
c.r = (unsigned char)round(255.0 / 100.0 * strtod(colorStr, &pEnd));
c.g = (unsigned char)round(255.0 / 100.0 * strtod(pEnd, &pEnd));
c.b = (unsigned char)round(255.0 / 100.0 * strtod(pEnd, &pEnd));
} else if (length > 3 && colorStr[0] == 'r' && colorStr[1] == 'g' && colorStr[2] == 'b') {
/* Integer functional — rgb(rrr, ggg, bbb) */
charReplace(colorStr, "rgb,()", " ");
c.r = (unsigned char)strtol(colorStr, &pEnd, 10);
c.g = (unsigned char)strtol(pEnd, &pEnd, 10);
c.b = (unsigned char)strtol(pEnd, &pEnd, 10);
} else {
/* Color keyword */
int tableColor = threadColor(&c, colorStr, SVG_Colors);
if (!tableColor) {
printf("SVG color string not found: %s.\n", colorStr);
}
}
free(colorStr);
/* Returns black if all else fails */
return c;
}
static int svgPathCmdToEmbPathFlag(char cmd)
{
/* TODO: This function needs some work */
/*
if (toUpper(cmd) == 'M') return MOVETO;
else if(toUpper(cmd) == 'L') return LINETO;
else if(toUpper(cmd) == 'C') return CUBICTOCONTROL1;
else if(toUpper(cmd) == 'CC') return CUBICTOCONTROL2;
else if(toUpper(cmd) == 'CCC') return CUBICTOEND;
else if(toUpper(cmd) == 'A') return ELLIPSETORAD;
else if(toUpper(cmd) == 'AA') return ELLIPSETOEND;
else if(toUpper(cmd) == 'Q') return QUADTOCONTROL;
else if(toUpper(cmd) == 'QQ') return QUADTOEND;
else if(toUpper(cmd) == 'Z') return LINETO;
*/
/*else if(toUpper(cmd) == 'B') return BULGETOCONTROL; */ /* NOTE: This is not part of the SVG spec, but hopefully Bulges will be added to the SVG spec someday */
/*else if(toUpper(cmd) == 'BB') return BULGETOEND; */ /* NOTE: This is not part of the SVG spec, but hopefully Bulges will be added to the SVG spec someday */
/*else { embLog("ERROR: format-svg.c svgPathCmdToEmbPathFlag(), unknown command '%c'\n", cmd); return MOVETO; } */
return LINETO;
}
SvgAttribute svgAttribute_create(const char* name, const char* value)
{
SvgAttribute attribute;
char* modValue = 0;
modValue = emb_strdup((char*)value);
charReplace(modValue, "\"'/,", " ");
attribute.name = emb_strdup((char*)name);
attribute.value = modValue;
return attribute;
}
void svgElement_addAttribute(SvgElement* element, SvgAttribute data)
{
if (!element) {
embLog("ERROR: format-svg.c svgElement_addAttribute(), element argument is null.");
return;
}
if (!(element->attributeList)) {
element->attributeList = (SvgAttributeList*)malloc(sizeof(SvgAttributeList));
if (!(element->attributeList)) {
embLog("ERROR: format-svg.c svgElement_addAttribute(), cannot allocate memory for element->attributeList.");
return;
}
element->attributeList->attribute = data;
element->attributeList->next = 0;
element->lastAttribute = element->attributeList;
element->lastAttribute->next = 0;
} else {
SvgAttributeList* pointerLast = element->lastAttribute;
SvgAttributeList* list = (SvgAttributeList*)malloc(sizeof(SvgAttributeList));
if (!list) {
embLog("ERROR: format-svg.c svgElement_addAttribute(), cannot allocate memory for list.");
return;
}
list->attribute = data;
list->next = 0;
pointerLast->next = list;
element->lastAttribute = list;
}
}
void svgElement_free(SvgElement* element)
{
SvgAttributeList* list = 0;
SvgAttributeList* nextList = 0;
if (!element)
return;
list = element->attributeList;
while (list) {
free(list->attribute.name);
list->attribute.name = 0;
free(list->attribute.value);
list->attribute.value = 0;
nextList = list->next;
free(list);
list = nextList;
}
element->lastAttribute = 0;
free(element->name);
free(element);
}
SvgElement* svgElement_create(const char* name)
{
SvgElement* element = 0;
element = (SvgElement*)malloc(sizeof(SvgElement));
if (!element) {
embLog("ERROR: format-svg.c svgElement_create(), cannot allocate memory for element\n");
return 0;
}
element->name = emb_strdup((char*)name);
if (!element->name) {
embLog("ERROR: format-svg.c svgElement_create(), element->name is null\n");
return 0;
}
element->attributeList = 0;
element->lastAttribute = 0;
return element;
}
char* svgAttribute_getValue(SvgElement* element, const char* name)
{
SvgAttributeList* pointer = 0;
if (!element) {
embLog("ERROR: format-svg.c svgAttribute_getValue(), element argument is null\n");
return "none";
}
if (!name) {
embLog("ERROR: format-svg.c svgAttribute_getValue(), name argument is null\n");
return "none";
}
if (!element->attributeList) { /* TODO: error */
return "none";
}
pointer = element->attributeList;
while (pointer) {
if (!strcmp(pointer->attribute.name, name)) {
return pointer->attribute.value;
}
pointer = pointer->next;
}
return "none";
}
void svgAddToPattern(EmbPattern* p)
{
const char* buff = 0;
EmbPointObject test;
EmbColor color;
EmbPathObject* path;
if (!p) {
embLog("ERROR: format-svg.c svgAddToPattern(), p argument is null\n");
return;
}
if (!currentElement) {
return;
}
buff = currentElement->name;
if (!buff) {
return;
}
if (!strcmp(buff, "?xml")) {
} else if (!strcmp(buff, "a")) {
} else if (!strcmp(buff, "animate")) {
} else if (!strcmp(buff, "animateColor")) {
} else if (!strcmp(buff, "animateMotion")) {
} else if (!strcmp(buff, "animateTransform")) {
} else if (!strcmp(buff, "animation")) {
} else if (!strcmp(buff, "audio")) {
} else if (!strcmp(buff, "circle")) {
embPattern_addCircleObjectAbs(p, atof(svgAttribute_getValue(currentElement, "cx")),
atof(svgAttribute_getValue(currentElement, "cy")),
atof(svgAttribute_getValue(currentElement, "r")));
} else if (!strcmp(buff, "defs")) {
} else if (!strcmp(buff, "desc")) {
} else if (!strcmp(buff, "discard")) {
} else if (!strcmp(buff, "ellipse")) {
embPattern_addEllipseObjectAbs(p, atof(svgAttribute_getValue(currentElement, "cx")),
atof(svgAttribute_getValue(currentElement, "cy")),
atof(svgAttribute_getValue(currentElement, "rx")),
atof(svgAttribute_getValue(currentElement, "ry")));
} else if (!strcmp(buff, "font")) {
} else if (!strcmp(buff, "font-face")) {
} else if (!strcmp(buff, "font-face-src")) {
} else if (!strcmp(buff, "font-face-uri")) {
} else if (!strcmp(buff, "foreignObject")) {
} else if (!strcmp(buff, "g")) {
} else if (!strcmp(buff, "glyph")) {
} else if (!strcmp(buff, "handler")) {
} else if (!strcmp(buff, "hkern")) {
} else if (!strcmp(buff, "image")) {
} else if (!strcmp(buff, "line")) {
char* x1 = svgAttribute_getValue(currentElement, "x1");
char* y1 = svgAttribute_getValue(currentElement, "y1");
char* x2 = svgAttribute_getValue(currentElement, "x2");
char* y2 = svgAttribute_getValue(currentElement, "y2");
/* If the starting and ending points are the same, it is a point */
if (!strcmp(x1, x2) && !strcmp(y1, y2))
embPattern_addPointObjectAbs(p, atof(x1), atof(y1));
else
embPattern_addLineObjectAbs(p, atof(x1), atof(y1), atof(x2), atof(y2));
} else if (!strcmp(buff, "linearGradient")) {
} else if (!strcmp(buff, "listener")) {
} else if (!strcmp(buff, "metadata")) {
} else if (!strcmp(buff, "missing-glyph")) {
} else if (!strcmp(buff, "mpath")) {
} else if (!strcmp(buff, "path")) {
/* TODO: finish */
char* pointStr = svgAttribute_getValue(currentElement, "d");
char* mystrok = svgAttribute_getValue(currentElement, "stroke");
int last = strlen(pointStr);
int size = 32;
int i = 0;
int j = 0;
int pos = 0;
/* An odometer aka 'tripometer' used for stepping thru the pathData */
int trip = -1; /* count of float[] that has been filled. 0=first item of array, -1=not filled = empty array */
int reset = -1;
double xx = 0.0;
double yy = 0.0;
double fx = 0.0;
double fy = 0.0;
double lx = 0.0;
double ly = 0.0;
double cx1 = 0.0, cx2 = 0.0;
double cy1 = 0.0, cy2 = 0.0;
int cmd = 0;
double pathData[7];
unsigned int numMoves = 0;
int pendingTask = 0;
int relative = 0;
EmbArray* pointList = 0;
EmbArray* flagList;
char* pathbuff = 0;
pathbuff = (char*)malloc(size);
if (!pathbuff) {
embLog("ERROR: format-svg.c svgAddToPattern(), cannot allocate memory for pathbuff\n");
return;
}
printf("stroke:%s\n", mystrok);
/* M44.219,26.365c0,10.306-8.354,18.659-18.652,18.659c-10.299,0-18.663-8.354-18.663-18.659c0-10.305,8.354-18.659,18.659-18.659C35.867,7.707,44.219,16.06,44.219,26.365z */
for (i = 0; i < last; i++) {
char c = pointStr[i];
switch (c) {
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
case '.':
pathbuff[pos++] = (char)c; /* add a more char */
break;
case ' ':
case ',':
/*printf(" ,'%s' ~POS=%d ~TRIP=%d ~[pos]=%d\n", pathbuff,pos,trip, pathbuff[pos]);*/
if (pos > 0) { /* append float to array, if it not yet stored */
pathbuff[pos] = 0;
pos = 0;
printf(" ,val:%s\n", pathbuff);
pathData[++trip] = atof(pathbuff);
}
break;
case '-':
if (pos > 0) { /* append float to array, if it not yet stored */
pathbuff[pos] = 0;
pos = 0;
printf(" -val:%s\n", pathbuff);
pathData[++trip] = atof(pathbuff);
}
pathbuff[pos++] = (char)c; /* add a more char */
break;
default:
/*** ASSUMED ANY COMMAND FOUND ***/
if (pos > 0) { /* just make sure: append float to array, if it not yet stored */
pathbuff[pos] = 0;
pos = 0;
printf(" >val:%s\n", pathbuff);
pathData[++trip] = atof(pathbuff);
}
/**** Compose Point List ****/
/* below "while" is for avoid loosing last 'z' command that maybe never accomodated. */
pendingTask = 1;
if (i == last - 1) {
pendingTask = 2;
}
while (pendingTask > 0) {
pendingTask -= 1;
/* Check wether prior command need to be saved */
if (trip >= 0) {
trip = -1;
reset = -1;
relative = 0; /* relative to prior coordinate point or absolute coordinate? */
if (cmd == 'M') {
xx = pathData[0];
yy = pathData[1];
fx = xx;
fy = yy;
} else if (cmd == 'm') {
xx = pathData[0];
yy = pathData[1];
fx = xx;
fy = yy;
relative = 1;
} else if (cmd == 'L') {
xx = pathData[0];
yy = pathData[1];
} else if (cmd == 'l') {
xx = pathData[0];
yy = pathData[1];
relative = 1;
} else if (cmd == 'H') {
xx = pathData[0];
yy = ly;
} else if (cmd == 'h') {
xx = pathData[0];
yy = ly;
relative = 1;
} else if (cmd == 'V') {
xx = lx;
yy = pathData[1];
} else if (cmd == 'v') {
xx = lx;
yy = pathData[1];
relative = 1;
} else if (cmd == 'C') {
xx = pathData[4];
yy = pathData[5];
cx1 = pathData[0];
cy1 = pathData[1];
cx2 = pathData[2];
cy2 = pathData[3];
} else if (cmd == 'c') {
xx = pathData[4];
yy = pathData[5];
cx1 = pathData[0];
cy1 = pathData[1];
cx2 = pathData[2];
cy2 = pathData[3];
relative = 1;
}
/*
else if(cmd == 'S') { xx = pathData[0]; yy = pathData[1]; }
else if(cmd == 's') { xx = pathData[0]; yy = pathData[1]; }
else if(cmd == 'Q') { xx = pathData[0]; yy = pathData[1]; }
else if(cmd == 'q') { xx = pathData[0]; yy = pathData[1]; }
else if(cmd == 'T') { xx = pathData[0]; yy = pathData[1]; }
else if(cmd == 't') { xx = pathData[0]; yy = pathData[1]; }
else if(cmd == 'A') { xx = pathData[0]; yy = pathData[1]; }
else if(cmd == 'a') { xx = pathData[0]; yy = pathData[1]; }
*/
else if (cmd == 'Z') {
xx = fx;
yy = fy;
} else if (cmd == 'z') {
xx = fx;
yy = fy;
}
if (!pointList && !flagList) {
pointList = embArray_create(EMB_POINT);
flagList = embArray_create(EMB_FLAG);
}
test.point.x = xx;
test.point.y = yy;
embArray_addPoint(pointList, &test);
embArray_addFlag(flagList, svgPathCmdToEmbPathFlag(cmd));
lx = xx;
ly = yy;
pathbuff[0] = (char)cmd; /* set the command for compare */
pathbuff[1] = 0;
pos = 0;
printf("*prior:%s (%f, %f, %f, %f, %f,%f, %f) \n", pathbuff,
pathData[0],
pathData[1],
pathData[2],
pathData[3],
pathData[4],
pathData[5],
pathData[6]);
}
/* assign new command */
if (trip == -1 && reset == -1) {
pathbuff[0] = (char)c; /* set the command for compare */
pathbuff[1] = 0;
printf("cmd:%s\n", pathbuff);
cmd = c;
if (c>='A' && c<='Z') {
c += 'a' - 'A';
}
int resetValues[] = {
/*a b c d e f g h i j k l m */
7,-1, 6,-1,-1,-1,-1, 1,-1,-1,-1, 2, 2,
/*n o p q r s t u v w x y z */
-1,-1,-1, 4,-1, 4, 2,-1, 1,-1,-1,-1, 0
};
if (c>='a' && c<='z') {
reset = resetValues[c-'a'];
if (c=='m') numMoves++;
}
if (reset < 0) {
embLog("ERROR: format-svg.c svgAddToPattern(), %s is not a valid svg path command, skipping...");
embLog(pathbuff);
trip = -1;
break;
}
}
/* avoid loosing 'z' command that maybe never accomodated. */
if (i == last - 1) {
trip = 2;
}
} /* while pendingTask */
break;
}
if (pos >= size - 1) {
/* increase pathbuff length - leave room for 0 */
size *= 2;
pathbuff = (char*)realloc(pathbuff, size);
if (!pathbuff) {
embLog("ERROR: format-svg.c svgAddToPattern(), cannot re-allocate memory for pathbuff\n");
return;
}
}
}
free(pathbuff);
/* TODO: subdivide numMoves > 1 */
color = svgColorToEmbColor(svgAttribute_getValue(currentElement, "stroke"));
path->pointList = pointList;
path->flagList = flagList;
path->color = color;
path->lineType = 1;
embPattern_addPathObjectAbs(p, path);
} else if (!strcmp(buff, "polygon") || !strcmp(buff, "polyline")) {
char* pointStr = svgAttribute_getValue(currentElement, "points");
int last = strlen(pointStr);
int size = 32;
int i = 0;
int c = 0;
int pos = 0;
unsigned char odd = 1;
double xx = 0.0;
double yy = 0.0;
EmbArray* pointList = 0;
char* polybuff = 0;
polybuff = (char*)malloc(size);
if (!polybuff) {
embLog("ERROR: format-svg.c svgAddToPattern(), cannot allocate memory for polybuff\n");
return;
}
for (i = 0; i < last; i++) {
char c = pointStr[i];
switch (c) {
case ' ':
if (pos == 0)
break;
polybuff[pos] = 0;
pos = 0;
/*Compose Point List */
if (odd) {
odd = 0;
xx = atof(polybuff);
} else {
odd = 1;
yy = atof(polybuff);
if (!pointList) {
pointList = embArray_create(EMB_POINT);
}
EmbPointObject a;
a.point.x = xx;
a.point.y = yy;
embArray_addPoint(pointList, &a);
}
break;
default:
polybuff[pos++] = (char)c;
break;
}
if (pos >= size - 1) {
/* increase polybuff length - leave room for 0 */
size *= 2;
polybuff = (char*)realloc(polybuff, size);
if (!polybuff) {
embLog("ERROR: format-svg.c svgAddToPattern(), cannot re-allocate memory for polybuff\n");
return;
}
}
}
free(polybuff);
polybuff = 0;
if (!strcmp(buff, "polygon")) {
EmbPolygonObject polygonObj;
polygonObj.pointList = pointList;
polygonObj.color = svgColorToEmbColor(svgAttribute_getValue(currentElement, "stroke"));
polygonObj.lineType = 1; /* TODO: use lineType enum */
embPattern_addPolygonObjectAbs(p, &polygonObj);
} else /* polyline */
{
EmbPolylineObject* polylineObj;
polylineObj->pointList = pointList;
polylineObj->color = svgColorToEmbColor(svgAttribute_getValue(currentElement, "stroke"));
polylineObj->lineType = 1; /* TODO: use lineType enum */
embPattern_addPolylineObjectAbs(p, polylineObj);
}
} else if (!strcmp(buff, "prefetch")) {
} else if (!strcmp(buff, "radialGradient")) {
} else if (!strcmp(buff, "rect")) {
embPattern_addRectObjectAbs(p, atof(svgAttribute_getValue(currentElement, "x")),
atof(svgAttribute_getValue(currentElement, "y")),
atof(svgAttribute_getValue(currentElement, "width")),
atof(svgAttribute_getValue(currentElement, "height")));
} else if (!strcmp(buff, "script")) {
} else if (!strcmp(buff, "set")) {
} else if (!strcmp(buff, "solidColor")) {
} else if (!strcmp(buff, "stop")) {
} else if (!strcmp(buff, "svg")) {
} else if (!strcmp(buff, "switch")) {
} else if (!strcmp(buff, "tbreak")) {
} else if (!strcmp(buff, "text")) {
} else if (!strcmp(buff, "textArea")) {
} else if (!strcmp(buff, "title")) {
} else if (!strcmp(buff, "tspan")) {
} else if (!strcmp(buff, "use")) {
} else if (!strcmp(buff, "video")) {
}
svgElement_free(currentElement);
currentElement = 0;
}
static int svgIsElement(const char* buff)
{
if (stringInArray(buff, svg_element_tokens)) {
return SVG_ELEMENT;
}
/* Attempt to identify the program that created the SVG file. This should be in a comment at that occurs before the svg element. */
else if (!strcmp(buff, "Embroidermodder")) {
svgCreator = SVG_CREATOR_EMBROIDERMODDER;
} else if (!strcmp(buff, "Illustrator")) {
svgCreator = SVG_CREATOR_ILLUSTRATOR;
} else if (!strcmp(buff, "Inkscape")) {
svgCreator = SVG_CREATOR_INKSCAPE;
}
return SVG_NULL;
}
static char svgIsMediaProperty(const char* buff)
{
if (stringInArray(buff, svg_media_property_tokens)) {
return SVG_MEDIA_PROPERTY;
}
return SVG_NULL;
}
static char svgIsProperty(const char* buff)
{
if (stringInArray(buff, svg_property_tokens)) {
return SVG_PROPERTY;
}
return SVG_NULL;
}
/* attribute tokens */
static const char* xmlTokens[] = { "encoding", "standalone", "version", "/", "\0" };
static const char* linkTokens[] = {
"about", "class", "content",
"datatype", "externalResourcesRequired", "focusHighlight",
"focusable", "id", "nav-down", "nav-down-left",
"nav-down-right", "nav-left", "nav-next", "nav-prev",
"nav-right", "nav-up", "nav-up-left", "nav-up-right",
"property", "rel", "requiredExtensions",
"requiredFeatures", "requiredFonts", "requiredFormats",
"resource", "rev", "role", "systemLanguage", "target",
"transform", "typeof", "xlink:actuate", "xlink:arcrole",
"xlink:href", "xlink:role", "xlink:show", "xlink:title",
"xlink:type", "xml:base", "xml:id", "xml:lang",
"xml:space", "/", "\0"
};
static const char* animateTokens[] = {
"about", "accumulate", "additive", "attributeName", "attributeType",
"begin", "by", "calcMode", "class", "content", "datatype", "dur", "end",