forked from Embroidermodder/libembroidery
-
Notifications
You must be signed in to change notification settings - Fork 0
/
embroider-main.c
551 lines (495 loc) · 15.9 KB
/
embroider-main.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
#include "embroidery.h"
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define RED_TERM_COLOR "\e[0;31m"
#define GREEN_TERM_COLOR "\e[0;32m"
#define YELLOW_TERM_COLOR "\e[1;33m"
#define RESET_TERM_COLOR "\033[0m"
static void usage(void);
static void formats(void);
static void testTangentPoints(EmbCircle c, EmbVector p, EmbVector*, EmbVector*);
static int testEmbCircle(void);
static int testThreadColor(void);
static void report(int result, char* label);
static void testMain(void);
static int convert(const char* inf, const char* outf);
static int create_test_file_1(const char* outf);
static int create_test_file_2(const char* outf);
static const char version_string[] = "embroider v0.1";
static void usage(void)
{
puts("EMBROIDER");
puts(" A command line program for machine embroidery.");
puts(" Copyright 2013-2021 The Embroidermodder Team");
puts(" Licensed under the terms of the zlib license.");
puts("");
puts(" https://github.com/Embroidermodder/libembroidery");
puts(" https://embroidermodder.org");
puts("");
puts("Usage: embroider [OPTIONS] fileToRead... \n");
puts("");
puts("Conversion:");
puts(" -t, -to Convert all files given to the format specified");
puts(" by the arguments to the flag, for example:");
puts(" $ embroider -t dst input.pes");
puts(" would convert \"input.pes\" to \"input.dst\"");
puts(" in the same directory the program runs in.");
puts("");
puts(" The accepted input formats are (TO BE DETERMINED).");
puts(" The accepted output formats are (TO BE DETERMINED).");
puts("");
puts("Output:");
puts(" -h, -help Print this message.");
puts(" -f, -format Print help on the formats that embroider can deal with.");
puts(" -q, -quiet Only print fatal errors.");
puts(" -V, -verbose Print everything that has reporting.");
puts(" -v, -version Print the version.");
puts("");
puts("Graphics:");
puts(" -c, -circle Add a circle defined by the arguments given to the current pattern.");
puts(" -e, -ellipse Add a circle defined by the arguments given to the current pattern.");
puts(" -l, -line Add a line defined by the arguments given to the current pattern.");
puts(" -P, -polyline Add a polyline.");
puts(" -p, -polygon Add a polygon.");
puts(" -s, -satin Fill the current geometry with satin stitches according");
puts(" to the defined algorithm.");
puts(" -S, -stitch Add a stitch defined by the arguments given to the current pattern.");
puts("");
puts("Quality Assurance:");
puts(" -test Run the test suite.");
}
static void formats(void)
{
const char* extension = 0;
const char* description = 0;
char readerState;
char writerState;
int numReaders = 0;
int numWriters = 0;
int i;
puts("List of Formats");
puts("---------------");
puts("");
puts(" KEY");
puts(" 'S' = Yes, and is considered stable.");
puts(" 'U' = Yes, but may be unstable.");
puts(" ' ' = No.");
puts("");
puts(" Format Read Write Description\n");
puts("|________|_______|_______|____________________________________________________|\n");
puts("| | | | |\n");
for (i = 0; i < numberOfFormats; i++) {
extension = formatTable[i].extension;
description = formatTable[i].description;
readerState = formatTable[i].reader;
writerState = formatTable[i].writer;
numReaders += readerState != ' ' ? 1 : 0;
numWriters += writerState != ' ' ? 1 : 0;
printf("| %-4s | %c | %c | %-49s |\n", extension, readerState, writerState, description);
}
puts("|________|_______|_______|____________________________________________________|\n");
puts("| | | | |\n");
printf("| Total: | %3d | %3d | |\n", numReaders, numWriters);
puts("|________|_______|_______|____________________________________________________|\n");
puts("");
}
static int create_test_file_1(const char* outf)
{
int i, result;
EmbPattern* p;
EmbStitch st;
p = embPattern_create();
if (!p) {
puts("ERROR: convert(), cannot allocate memory for p\n");
return 1;
}
/* 10mm circle */
for (i = 0; i < 100; i++) {
st.x = 10 + 10 * sin(i * (0.5 / 3.141592));
st.y = 10 + 10 * cos(i * (0.5 / 3.141592));
st.flags = NORMAL;
st.color = 0;
embArray_addStitch(p->stitchList, st);
}
EmbThread thr = { { 0, 0, 0 }, "Black", "Black" };
embArray_addThread(p->threads, thr);
result = embPattern_write(p, outf, EMB_FORMAT_CSV);
embPattern_free(p);
return 0;
}
static int create_test_file_2(const char* outf)
{
int i, result;
EmbPattern* p;
EmbStitch st;
p = embPattern_create();
if (!p) {
puts("ERROR: convert(), cannot allocate memory for p\n");
return 1;
}
/* sin wave */
for (i = 0; i < 100; i++) {
st.x = 10 + 10 * sin(i * (0.5 / 3.141592));
st.y = 10 + i * 0.1;
st.flags = NORMAL;
st.color = 0;
embArray_addStitch(p->stitchList, st);
}
EmbThread thr = { { 0, 0, 0 }, "Black", "Black" };
embArray_addThread(p->threads, thr);
result = embPattern_write(p, outf, EMB_FORMAT_CSV);
embPattern_free(p);
return 0;
}
static int convert(const char* inf, const char* outf)
{
EmbFile *file;
EmbPattern* p;
int reader, writer, formatType;
p = embPattern_create();
if (!p) {
puts("ERROR: convert(), cannot allocate memory for p\n");
return 1;
}
reader = embReaderWriter_getByFileName(inf);
if (reader < 0) {
puts("convert(), unsupported read file type:");
puts(inf);
embPattern_free(p);
return 1;
}
embPattern_read(p, inf, reader);
formatType = embFormat_typeFromName(inf);
if (formatType == EMBFORMAT_OBJECTONLY) {
formatType = embFormat_typeFromName(outf);
if (formatType == EMBFORMAT_STITCHONLY) {
embPattern_movePolylinesToStitchList(p);
}
}
writer = embReaderWriter_getByFileName(outf);
if (writer < 0) {
puts("convert(), unsupported write file type:");
puts(outf);
embPattern_free(p);
return 1;
}
embPattern_write(p, outf, writer);
embPattern_free(p);
return 0;
}
/* TODO: Add capability for converting multiple files of various types to a single format. Currently, we only convert a single file to multiple formats. */
int main(int argc, const char* argv[])
{
int i, flags;
if (argc == 1) {
usage();
return 0;
}
flags = argc - 1;
for (i = 1; i < argc; i++) {
if (!strcmp(argv[i], "-help") || !strcmp(argv[i], "-h")) {
usage();
continue;
}
if (!strcmp(argv[i], "-formats") || !strcmp(argv[i], "-f")) {
formats();
continue;
}
if (!strcmp(argv[i], "-version") || !strcmp(argv[i], "-v")) {
puts(version_string);
continue;
}
if (!strcmp(argv[i], "-test")) {
testMain();
continue;
}
flags--;
}
/* simple from-to conversion */
if (!flags) {
convert(argv[1], argv[2]);
}
return 0;
}
static void testTangentPoints(EmbCircle c, EmbVector p, EmbVector* t0, EmbVector* t1)
{
if (!getCircleTangentPoints(c, p, t0, t1)) {
printf("Error calculating tangent points.\n");
} else {
printf("Circle : cr=%f, cx=%f, cy=%f\n"
"Point : px=%f, py=%f\n"
"Tangent: tx0=%f, ty0=%f\n"
"Tangent: tx1=%f, ty1=%f\n\n",
c.radius, c.center.x, c.center.y,
p.x, p.y,
t0->x, t0->y,
t1->x, t1->y);
}
}
int testEmbCircle(void)
{
double epsilon = 1e-3;
EmbVector p0, p1;
/* Problem */
EmbCircle c1 = { { 0.0, 0.0 }, 3.0 };
/* Solution */
EmbVector t0 = { 2.2500, 1.9843 };
EmbVector t1 = { 2.2500, -1.9843 };
/* Test */
EmbVector test0 = { 4.0, 0.0 };
testTangentPoints(c1, test0, &p0, &p1);
if (embVector_distance(p0, t0) + embVector_distance(p1, t1) > epsilon) {
return 16;
}
EmbCircle c2 = { { 20.1762, 10.7170 }, 6.8221 };
/* Solution */
EmbVector s0 = { 19.0911, 17.4522 };
EmbVector s1 = { 26.4428, 13.4133 };
/* Test */
EmbVector test1 = { 24.3411, 18.2980 };
testTangentPoints(c2, test1, &p0, &p1);
double error = embVector_distance(p0, s0) + embVector_distance(p1, s1);
if (error > epsilon) {
printf("Error larger than tolerence, circle test 2: %f.\n\n", error);
return 17;
}
return 0;
}
static void printArcResults(double bulge, EmbArc arc, EmbVector center,
double radius, double diameter, double chord, EmbVector chordMid,
double sagitta, double apothem, double incAngle, char clockwise)
{
fprintf(stdout, "bulge = %f\n"
"startX = %f\n"
"startY = %f\n"
"endX = %f\n"
"endY = %f\n"
"midX = %f\n"
"midY = %f\n"
"centerX = %f\n"
"centerY = %f\n"
"radius = %f\n"
"diameter = %f\n"
"chord = %f\n"
"chordMidX = %f\n"
"chordMidY = %f\n"
"sagitta = %f\n"
"apothem = %f\n"
"incAngle = %f\n"
"clockwise = %d\n"
"\n",
bulge,
arc.start.x,
arc.start.y,
arc.end.x,
arc.end.y,
arc.mid.x,
arc.mid.y,
center.x,
center.y,
radius,
diameter,
chord,
chordMid.x,
chordMid.y,
sagitta,
apothem,
incAngle,
clockwise);
}
int testGeomArc(void)
{
EmbArc arc;
EmbVector center, chordMid;
double bulge, radius, diameter, chord, sagitta, apothem, incAngle;
char clockwise;
fprintf(stdout, "Clockwise Test:\n");
bulge = -0.414213562373095;
arc.start.x = 1.0;
arc.start.y = 0.0;
arc.end.x = 2.0;
arc.end.y = 1.0;
if (getArcDataFromBulge(bulge, &arc, ¢er, &radius, &diameter, &chord,
&chordMid, &sagitta, &apothem, &incAngle, &clockwise)) {
printArcResults(bulge, arc, center, radius, diameter,
chord, chordMid, sagitta, apothem, incAngle, clockwise);
}
fprintf(stdout, "Counter-Clockwise Test:\n");
bulge = 2.414213562373095;
arc.start.x = 4.0;
arc.start.y = 0.0;
arc.end.x = 5.0;
arc.end.y = 1.0;
if (getArcDataFromBulge(bulge, &arc, ¢er, &radius, &diameter, &chord,
&chordMid, &sagitta, &apothem, &incAngle, &clockwise)) {
printArcResults(bulge, arc, center, radius, diameter,
chord, chordMid, sagitta, apothem, incAngle, clockwise);
}
return 0;
}
static int testThreadColor(void)
{
unsigned int tColor = 0xFFD25F00;
EmbColor c;
c.r = 0xD2;
c.g = 0x5F;
c.b = 0x00;
int tBrand = Sulky_Rayon;
int tNum = threadColorNum(c, tBrand);
const char* tName = threadColorName(c, tBrand);
printf("Color : 0x%X\n"
"Brand : %d\n"
"Num : %d\n"
"Name : %s\n\n",
tColor,
tBrand,
tNum, /* Solution: 1833 */
tName); /* Solution: Pumpkin Pie */
return 0;
}
static int testEmbFormat(void)
{
const char* tName = "example.zsk";
char extension[10];
const char* description = embFormat_descriptionFromName(tName);
char reader = embFormat_readerStateFromName(tName);
char writer = embFormat_writerStateFromName(tName);
int type = embFormat_typeFromName(tName);
embFormat_getExtension(tName, extension);
printf("Filename : %s\n"
"Extension : %s\n"
"Description: %s\n"
"Reader : %c\n"
"Writer : %c\n"
"Type : %d\n\n",
tName,
extension,
description,
reader,
writer,
type);
if (strcmp(extension, ".zsk"))
return 20;
if (strcmp(description, "ZSK USA Embroidery Format"))
return 21;
if (reader != 'U')
return 22;
if (writer != ' ')
return 23;
if (type != 1)
return 24;
return 0;
}
#if 0
static int testHash(void)
{
EmbHash* hash = 0;
hash = embHash_create();
if (!hash)
return 1;
if (!embHash_empty(hash))
return 2;
if (embHash_count(hash) != 0)
return 3;
/* insert */
if (embHash_insert(hash, "four", (void*)4))
return 4;
if (embHash_insert(hash, "five", (void*)5))
return 5;
if (embHash_insert(hash, "six", (void*)6))
return 6;
if (embHash_count(hash) != 3)
return 7;
/* replace */
if (embHash_insert(hash, "four", (void*)8))
return 8;
if (embHash_insert(hash, "five", (void*)10))
return 9;
if (embHash_insert(hash, "six", (void*)12))
return 10;
if (embHash_count(hash) != 3)
return 11;
/* contains */
if (!embHash_contains(hash, "four"))
return 12;
if (embHash_contains(hash, "empty"))
return 13;
/* remove */
embHash_remove(hash, "four");
if (embHash_count(hash) != 2)
return 14;
embHash_clear(hash);
if (embHash_count(hash) != 0)
return 15;
embHash_free(hash);
return 0;
}
#endif
static void
report(int result, char* label)
{
printf("%s Test...%*c", label, (int)(20 - strlen(label)), ' ');
if (result) {
printf(RED_TERM_COLOR "[FAIL] [CODE=%d]\n" RESET_TERM_COLOR, result);
} else {
printf(GREEN_TERM_COLOR "[PASS]\n" RESET_TERM_COLOR);
}
}
/**
* Note that we can use printf here because this file isn't compiled into
* libembroidery.so.
*/
#if 0
static int embeddedFunctionsResult(void)
{
char buffer[30];
int dnumber;
float fnumber;
dnumber = 2031;
fnumber = 12.4123;
/* embPointerToArray(buffer, (void *)buffer, 10);
printf("%s %p\n", buffer, (void *)buffer); */
embIntToArray(buffer, dnumber, 10);
printf("%s %d\n", buffer, dnumber);
if (strcmp(buffer, "2031")) {
return 1;
}
embFloatToArray(buffer, fnumber, 1.0e-7, 3, 3);
printf("%s %f\n", buffer, fnumber);
if (strcmp(buffer, "12.412")) {
return 1;
}
return 0;
}
#endif
static void testMain(void)
{
/* int hashResult = testHash(); */
int circleResult = testEmbCircle();
int threadResult = testThreadColor();
int formatResult = testEmbFormat();
int arcResult = testGeomArc();
int create1Result = create_test_file_1("test01.csv");
int create2Result = create_test_file_2("test02.csv");
int svg1Result = convert("test01.csv", "test01.svg");
int svg2Result = convert("test02.csv", "test02.svg");
int dst1Result = convert("test01.csv", "test01.dst");
int dst2Result = convert("test02.csv", "test02.dst");
/* int embeddedResult = embeddedFunctionsResult(); */
puts("SUMMARY OF RESULTS");
puts("------------------");
/* report(hashResult, "Hash"); */
report(circleResult, "Tangent Point");
report(threadResult, "Thread");
report(formatResult, "Format");
report(arcResult, "Arc");
report(create1Result, "Create CSV 1");
report(create2Result, "Create CSV 2");
report(svg1Result, "Convert CSV-SVG 1");
report(svg2Result, "Convert CSV-SVG 2");
report(dst1Result, "Convert CSV-DST 1");
report(dst2Result, "Convert CSV-DST 2");
/* report(embeddedResult, "Embedded Functions"); */
}