-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfswtotex.cpp
More file actions
1989 lines (1877 loc) · 62.5 KB
/
Copy pathfswtotex.cpp
File metadata and controls
1989 lines (1877 loc) · 62.5 KB
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
#include <fstream>
#include <iostream>
#include <vector>
using namespace std;
/*
This parser works with as state machines within state machines.
There is also a general error state which says that we failed in our matching and need
to spit out what we have saved so far.
Our outer state machine has the following states:
Start -- the obligatory starting state
Punctuation -- processing a punctuation sign (can be done visually as well)
Prefix -- processing the temporal prefix (can be missing)
Visual -- processing the visual layout of the sign (required)
End -- Time to spit out what we have
The Punctuation state machine has the following states:
Symbol -- processing a base symbol
Placement -- processing the placement of the symbol
End -- Time for the outer state machine to finish
The prefix state machine has the following states:
Start -- the obligatory starting state
Symbol -- processing a base symbol
End -- If we used this state machine, time for the outer state machine to move to the next state
The visual state machine has the following states:
Start -- the obligatory starting state
Size -- processing the size of the word
Symbol -- processing a base symbol
Placement -- processing the placement of the symbol
End -- Time for the outer state machine to finish
The Symbol state machine has the following states
Start -- the obligatory starting state
FirstDigit -- the first digit
SecondDigit -- the second digit
ThirdDigt -- the third digit
Fill -- the fill
Rotation -- the rotation
End -- Time for the outer state machine to finish
The Size state machine has the following states:
Start -- the obligatory starting state
FirstW -- the first digit of the width
SecondW -- the second digit of the width
ThirdW -- the third digit of the width
x -- the letter `x'
FirstH -- the first digit of the height
SecondH -- the second digit of the height
ThirdH -- the third digit of the height
End -- Time for the outer state machine to finish
The Placement state machine is exactly the same as the size state machine
*/
const int s_error = -1;
const int s_start = 0;
const int s_punctuation = 1;
const int s_prefix = 2;
const int s_visual = 3;
const int s_size = 1;
const int s_symbol = 2;
const int s_placement = 3;
const int s_first = 1;
const int s_second = 2;
const int s_third = 3;
const int s_fill = 4;
const int s_rotation = 5;
const int s_firstw = 1;
const int s_secondw = 2;
const int s_thirdw = 3;
const int s_x = 4;
const int s_firsth = 5;
const int s_secondh = 6;
const int s_thirdh = 7;
const int s_end = 8;
int state = s_start;
int substate = s_start;
int subsubstate = s_start;
/*
Now we come to reading.
If we want to be able to read from a wide variety of files, then we
have to be ready for all the different encodings and be able to convert
each of them into utf-32.
We will begin here with a set of functions to read a character of a
given type. Unknown or undetermined as of yet and utf8/16/32/le/be. We
will then have a set of functions to convert to utf32(internal version)
from all the other types of characters.
Even though these are technically characters, because we are dealing
with SignWriting characters which are outside of plane-0 we are going
to pass them around as if they are unsigned integers anyway.
*/
enum theTextFormat
{
unknown, utf8, utf16le, utf16be, utf32le, utf32be
} textFormat = unknown;
uint32_t getChar(istream* fileIn);
uint32_t getCharUnknown(istream* fileIn);
uint32_t getCharUtf8(istream* fileIn);
uint32_t getCharUtf16le(istream* fileIn);
uint32_t getCharUtf16be(istream* fileIn);
uint32_t getCharUtf32le(istream* fileIn);
uint32_t getCharUtf32be(istream* fileIn);
int utf8ToUtf32(uint8_t* c, uint32_t* fileOut);
int utf16leToUtf32(uint8_t* c, uint32_t* fileOut);
int utf16beToUtf32(uint8_t* c, uint32_t* fileOut);
int utf32leToUtf32(uint8_t* c, uint32_t* fileOut);
int utf32beToUtf32(uint8_t* c, uint32_t* fileOut);
uint32_t getCharUnknown(istream* fileIn)
{
uint32_t result = 0;
// feff is the byte order
// In each format, this can serve as a key
// utf 8 ef bb bf
// utf 16 le ff fe
// utf 16 be fe ff
// utf 32 le ff fe 00 00
// utf 32 be 00 00 fe ff
// So if we are unknown then we look for those,
// and if we don't see any of them we default to utf8
static uint8_t charBuff[4] = { 0, 0, 0, 0 };
static int charBuffSize = 0;
if (charBuffSize == 0)
{
for (; charBuffSize < 4; charBuffSize++)
{
charBuff[charBuffSize] = static_cast<uint8_t>(fileIn->get());
}
if (charBuff[0] == 0x0 && charBuff[1] == 0x0 &&
charBuff[2] == 0xfe && charBuff[3] == 0xff)
{
textFormat = utf32be;
return getCharUtf32be(fileIn);
}
else if (charBuff[0] == 0xff && charBuff[1] == 0xfe &&
charBuff[2] == 0x0 && charBuff[3] == 0x0)
{
textFormat = utf32le;
return getCharUtf32le(fileIn);
}
else if (charBuff[0] == 0xfe && charBuff[1] == 0xff)
{
textFormat = utf16be;
charBuff[0] = charBuff[2];
charBuff[1] = charBuff[3];
charBuff[2] = 0;
charBuff[3] = 0;
if (utf16beToUtf32(charBuff, &result) == 2)
{
return result;
}
charBuff[2] = static_cast<uint8_t>(fileIn->get());
charBuff[3] = static_cast<uint8_t>(fileIn->get());
if (utf16beToUtf32(charBuff, &result) == 4)
{
return result;
}
throw "Badly formed utf16be string.";
}
else if (charBuff[0] == 0xff && charBuff[1] == 0xfe)
{
textFormat = utf16le;
charBuff[0] = charBuff[2];
charBuff[1] = charBuff[3];
charBuff[2] = 0;
charBuff[3] = 0;
if (utf16leToUtf32(charBuff, &result) == 2)
{
return result;
}
charBuff[2] = static_cast<uint8_t>(fileIn->get());
charBuff[3] = static_cast<uint8_t>(fileIn->get());
if (utf16leToUtf32(charBuff, &result) == 4)
{
return result;
}
throw "Badly formed ut16le string.";
}
else if (charBuff[0] == 0xef && charBuff[1] == 0xbb &&
charBuff[2] == 0xbf)
{
textFormat = utf8;
charBuff[0] = charBuff[3];
charBuff[1] = 0;
charBuff[2] = 0;
charBuff[3] = 0;
if (utf8ToUtf32(charBuff, &result) == 1)
{
return result;
}
charBuff[1] = static_cast<uint8_t>(fileIn->get());
if (utf8ToUtf32(charBuff, &result) == 2)
{
return result;
}
charBuff[2] = static_cast<uint8_t>(fileIn->get());
if (utf8ToUtf32(charBuff, &result) == 3)
{
return result;
}
charBuff[3] = static_cast<uint8_t>(fileIn->get());
if (utf8ToUtf32(charBuff, &result) == 4)
{
return result;
}
throw "Badly formed utf8 string.";
}
}
int offset = utf8ToUtf32(charBuff, &result);
while (offset < 0 && charBuffSize < 4)
{
charBuff[charBuffSize] = static_cast<uint8_t>(fileIn->get());
charBuffSize++;
offset = utf8ToUtf32(charBuff, &result);
}
int i;
for (i = 0; i < charBuffSize - offset; i++)
{
charBuff[i] = charBuff[i + offset];
}
for (; i < charBuffSize; i++)
{
charBuff[i] = 0;
}
charBuffSize -= offset;
if (charBuffSize == 0)
{
textFormat = utf8;
}
return result;
}
uint32_t getCharUtf8(istream* fileIn)
{
uint8_t charBuff[4] = { 0,0,0,0 };
int read = 0;
int size = 0;
uint32_t result = 0;
do
{
read = (fileIn->get());
if (read == -1)
return 0xffffffff;
charBuff[size++] = static_cast<uint8_t>(read);
} while ((size < 4) && (utf8ToUtf32(charBuff, &result) != size));
if (utf8ToUtf32(charBuff, &result) == size)
{
return result;
}
throw "Badly formed utf8 string.";
}
uint32_t getCharUtf16le(istream* fileIn)
{
uint8_t charBuff[4] = { 0,0,0,0 };
int read = 0;
int size = 0;
uint32_t result = 0;
do
{
read = (fileIn->get());
if (read == -1)
return 0xffffffff;
charBuff[size++] = static_cast<uint8_t>(read);
read = (fileIn->get());
if (read == -1)
return 0xffffffff;
charBuff[size++] = static_cast<uint8_t>(read);
} while ((size < 4) && (utf16leToUtf32(charBuff, &result) != size));
if (utf16leToUtf32(charBuff, &result) == size)
{
return result;
}
throw "Badly formed utf16le string.";
}
uint32_t getCharUtf16be(istream* fileIn)
{
uint8_t charBuff[4] = { 0,0,0,0 };
int read = 0;
int size = 0;
uint32_t result = 0;
do
{
read = (fileIn->get());
if (read == -1)
return 0xffffffff;
charBuff[size++] = static_cast<uint8_t>(read);
read = (fileIn->get());
if (read == -1)
return 0xffffffff;
charBuff[size++] = static_cast<uint8_t>(read);
} while ((size < 4) && (utf16beToUtf32(charBuff, &result) != size));
if (utf16beToUtf32(charBuff, &result) == size)
{
return result;
}
throw "Badly formed utf16be string.";
}
uint32_t getCharUtf32le(istream* fileIn)
{
uint8_t charBuff[4] = { 0,0,0,0 };
int read = 0;
int size = 0;
uint32_t result;
read = (fileIn->get());
if (read == -1)
return 0xffffffff;
charBuff[size++] = static_cast<uint8_t>(read);
read = (fileIn->get());
if (read == -1)
return 0xffffffff;
charBuff[size++] = static_cast<uint8_t>(read);
read = (fileIn->get());
if (read == -1)
return 0xffffffff;
charBuff[size++] = static_cast<uint8_t>(read);
read = (fileIn->get());
if (read == -1)
return 0xffffffff;
charBuff[size++] = static_cast<uint8_t>(read);
utf32leToUtf32(charBuff, &result);
return result;
}
uint32_t getCharUtf32be(istream* fileIn)
{
uint8_t charBuff[4] = { 0,0,0,0 };
int read = 0;
int size = 0;
uint32_t result;
read = (fileIn->get());
if (read == -1)
return 0xffffffff;
charBuff[size++] = static_cast<uint8_t>(read);
read = (fileIn->get());
if (read == -1)
return 0xffffffff;
charBuff[size++] = static_cast<uint8_t>(read);
read = (fileIn->get());
if (read == -1)
return 0xffffffff;
charBuff[size++] = static_cast<uint8_t>(read);
read = (fileIn->get());
if (read == -1)
return 0xffffffff;
charBuff[size++] = static_cast<uint8_t>(read);
utf32beToUtf32(charBuff, &result);
return result;
}
uint32_t getChar(istream* fileIn)
{
switch (textFormat)
{
case utf8: return getCharUtf8(fileIn);
case utf16le: return getCharUtf16le(fileIn);
case utf16be: return getCharUtf16be(fileIn);
case utf32le: return getCharUtf32le(fileIn);
case utf32be: return getCharUtf32be(fileIn);
default: return getCharUnknown(fileIn);
}
}
/*
We always convert to utf-8 on output since we know that we are
outputting a lot of XeLaTeX code with expanded characters of the
format ``\char#xxxx''.
*/
string utf32ToUtf8(uint32_t c)
{
string to;
if (c < 0x80)
{
to.push_back(static_cast<char>(c));
}
else if (c < 0x800)
{
to.push_back(static_cast<char>(((c >> 6) & 0x1f) | 0xc0));
to.push_back(static_cast<char>(((c >> 0) & 0x3f) | 0x80));
}
else if (c < 0x10000)
{
to.push_back(static_cast<char>(((c >> 12) & 0xf) | 0xe0));
to.push_back(static_cast<char>(((c >> 6) & 0x3f) | 0x80));
to.push_back(static_cast<char>(((c >> 0) & 0x3f) | 0x80));
}
else
{
to.push_back(static_cast<char>(((c >> 18) & 0x7) | 0xf0));
to.push_back(static_cast<char>(((c >> 12) & 0x3f) | 0x80));
to.push_back(static_cast<char>(((c >> 6) & 0x3f) | 0x80));
to.push_back(static_cast<char>(((c >> 0) & 0x3f) | 0x80));
}
return to;
}
// And now we send it out
void sendOut(ostream* fileOut, uint32_t c)
{
(*fileOut) << utf32ToUtf8(c);
}
void sendOut(ostream* fileOut, vector<uint32_t>& l, uint32_t c)
{
for (unsigned int i = 0; i < l.size(); i++)
sendOut(fileOut, l[i]);
sendOut(fileOut, c);
l.clear();
state = substate = subsubstate = s_start;
}
// Now that we have been using all those ``convert to uft32'', let's define them.
int utf8ToUtf32(uint8_t* coming, uint32_t* going)
{
// Quick review:
// 0xxx xxxx
// 110x xxxx 10xx xxxx
// 1110 xxxx 10xx xxxx 10xx xxxx
// 1111 -xxx 10xx xxxx 10xx xxxx 10xx xxxx
if ((coming[0] & 0xf0) == 0xf0)
{
going[0] = coming[0] & 0x7;
for (int i = 1; i < 4; i++)
{
if ((coming[i] & 0xc0) != 0x80)
return 0;
going[0] <<= 6;
going[0] |= (coming[i] & 0x3f);
}
return 4;
}
if ((coming[0] & 0xf0) == 0xe0)
{
going[0] = coming[0] & 0xf;
for (int i = 1; i < 3; i++)
{
if ((coming[i] & 0xc0) != 0x80)
return 0;
going[0] <<= 6;
going[0] |= (coming[i] & 0x3f);
}
return 3;
}
if ((coming[0] & 0xe0) == 0xc0)
{
going[0] = coming[0] & 0x1f;
for (int i = 1; i < 2; i++)
{
if ((coming[i] & 0xc0) != 0x80)
return 0;
going[0] <<= 6;
going[0] |= (coming[i] & 0x3f);
}
return 2;
}
if ((coming[0] & 0xc0) == 0x80)
{
throw "Malformed utf8 string.";
}
going[0] = coming[0];
return 1;
}
int utf16leToUtf32(uint8_t* coming, uint32_t* going)
{
// Quick review, after accounting for endianess
// 0000--d7ff = pass along
// d800--d8ff = a following dc00-dfff tells the low 10 bits
// dc00--dfff = better be following d800-d8ff
// e000--ffff = pass along
going[0] = coming[0] << 0;
going[0] |= coming[1] << 8;
if (going[0] < 0xd800)
return 2;
if (going[0] < 0xdc00)
{
if (coming[3] < 0xdc || coming[3]>0xdf)
return 0;
going[0] &= 0x3ff;
going[0] |= 0x400;
going[0] <<= 10;
going[0] |= coming[2];
going[0] |= (coming[3] & 0x3) << 8;
return 2;
}
if (going[0] < 0xe000)
throw "Malformed utf16le string";
return 2;
}
int utf16beToUtf32(uint8_t* coming, uint32_t* going)
{
// Quick review, after accounting for endianess
// 0000--d7ff = pass along
// d800--d8ff = a following dc00-dfff tells the low 10 bits
// dc00--dfff = better be following d800-d8ff
going[0] = coming[0] << 8;
going[0] |= coming[1] << 0;
if (going[0] < 0xd800)
return 2;
if (going[0] < 0xdc00)
{
if (coming[2] < 0xdc || coming[2]>0xdf)
return 0;
going[0] &= 0x3ff;
going[0] |= 0x400;
going[0] <<= 10;
going[0] |= (coming[2] & 0x3) << 8;
going[0] |= coming[3];
return 2;
}
if (going[0] < 0xe000)
throw "Malformed utf16le string";
return 2;
}
int utf32leToUtf32(uint8_t* coming, uint32_t* going)
{
going[0] = coming[0] << 0;
going[0] |= coming[1] << 8;
going[0] |= coming[2] << 16;
going[0] |= coming[3] << 24;
return 4;
}
int utf32beToUtf32(uint8_t* coming, uint32_t* going)
{
going[0] = coming[0] << 24;
going[0] |= coming[1] << 16;
going[0] |= coming[2] << 8;
going[0] |= coming[3] << 0;
return 4;
}
// Finally, let's explain and start the program
int usage()
{
cout << "This is fswtotex." << endl;
cout << endl;
cout << "This program runs in one of three ways." << endl;
cout << "If you provide no arguments, we read from standard in and send the results to standard out." << endl;
cout << "If you provide one argument, we read from that file and send the results to standard out." << endl;
cout << "If you provide two arguments, we read from the first file and send the results to the second file." << endl;
cout << endl;
cout << "So what do we do?" << endl;
cout << endl;
cout << "We read LaTeX code with embedded SignWriting words (see http://signwriting.org) and output LaTeX "
"code with TiKz drawings of SignWriting symbols." << endl;
cout << endl;
cout << "There are a few modifications you can take advantage of though:" << endl;
cout << "--fsize <string> By default we assume a string called f@size holds the size which is" << endl;
cout << " generally a usable value to use in LaTex documents. You can change" << endl;
cout << " this if (for instance) you want SignWriting text to be a different" << endl;
cout << " size or you already have some other length being used. Fswtotex does" << endl;
cout << " add a \\ in front of the string you provide." << endl;
cout << "--nomirror By default we mirror, this turns that off." << endl;
cout << "--rotate <number> By default we assume a value of -90. The reason we default to mirrored" << endl;
cout << " and rotated by -90 degrees pages is because SignWriting is a vertical" << endl;
cout << " writing system. By rotating the page by -90 degrees we turn horizontal" << endl;
cout << " lines into vertical columns. But this alone would make for columns of" << endl;
cout << " text moving from right to left, so by adding a mirror to the text we" << endl;
cout << " get vertial columns of text moving from left to right. The next most" << endl;
cout << " likely settings you may want would be --nomirror --rotate 0, as this" << endl;
cout << " allows for insertion of SignWriting in the midst of English text but" << endl;
cout << " does require the author to handle things like placing the text into" << endl;
cout << " a multi-column environment and adding linebreaks after each word." << endl;
cout << "--spelling By default, we don't spell. If this option is added then columns of" << endl;
cout << " of symbols will appear above the word if it has a time domain prefix." << endl;
return 0;
}
int fswtotex(istream* fileIn, ostream* fileOut);
const string defaultfsize = "f@size";
string fsize = defaultfsize;
bool hasat = true;
bool mirror = true;
int rotation = -90;
bool spelling = false;
int main(int argc, char** argv)
{
// We an run in about three different ways.
// 1) read from standard in and write to standard out (0 arguments)
// 2) read from a file and write to standard out (1 argument)
// 3) read from a file and write to a file (2 arguments)
// In order to accomplish this, we first process the arguments,
// and then set up in istream and ostream, and let it rip.
// When processing the arguments we also have to consider the arguments to control
// fsize string
// mirroring
// rotation
int fileCounts = 0;
string fileNames[2];
int result = -1;
for (int i = 1; i < argc; i++)
{
if (string(argv[i]) == "--fsize")
{
i++;
if (i >= argc)
{
cout << "--fsize requires a following string" << endl;
return result;
}
fsize = argv[i];
if (fsize.find("@") == string::npos)
hasat = false;
}
else if (string(argv[i]) == "--nomirror")
{
mirror = false;
}
else if (string(argv[i]) == "--rotate")
{
i++;
if (i >= argc)
{
cout << "--rotate requires a following number" << endl;
return result;
}
rotation = atoi(argv[i]);
}
else if (string(argv[i]) == "--spelling")
{
spelling = true;
}
else if (argv[i][0] == '-')
{
return usage();
}
else
{
if (fileCounts >= 2)
{
cout << "We can only accept two files, and the second one is overwritten." << endl;
return result;
}
fileNames[fileCounts++] = argv[i];
}
}
try
{
if (fileCounts == 0)
{
// standard in and standard out
result = fswtotex(&cin, &cout);
cout << "% This file was generated by:" << endl;
cout << "% ";
for (int i = 0; i < argc; i++)
cout << argv[i] << " ";
cout << endl;
}
else if (fileCounts == 1)
{
// file to standard out
fstream fin;
fin.open(fileNames[0], ios::in);
result = fswtotex(&fin, &cout);
cout << "% This file was generated by:" << endl;
cout << "% ";
for (int i = 0; i < argc; i++)
cout << argv[i] << " ";
cout << endl;
}
else if (fileCounts == 2)
{
// file to file
fstream fin, fout;
fin.open(fileNames[0], ios::in);
fout.open(fileNames[1], ios::out);
result = fswtotex(&fin, &fout);
fout << "% This file was generated by:" << endl;
fout << "% ";
for (int i = 0; i < argc; i++)
fout << argv[i] << " ";
fout << endl;
}
}
catch (char const* message)
{
cout << "Failure: " << message << endl;
result = -1;
}
return result;
}
/*
Now we have our state processing functions.
For each set of states we have a separate function that expects a uint32_t
(which represents the Unicode character in question). There are three possible
results from being in a state and receiving a character.
1) Move forward to a new state.
2) Notice an error and spit out the currently stored data unchanged.
3) Notice that the match is complete and translate it.
By far the largest section of code will be to move forward to a new state,
and each potential move forward will hake a check for errors. Occasionally,
an ``error'' state will actually indicate a successful completion and we
will do a translation.
Let's start with our storage and state management followed by declaring our
state progression functions.
One final note, this SignWriting converter is actually too permissive.
In this converter you can use unicode for the symbol and ``text'' for
the numbers, or the reverse. I believe that you should actually be
required to do one or the other, but there it is. If I were ever to make a
version that did not let you mix and match FSWA and FSWU, we would have two
sets of functions.
It's may also not be permissive enough in that if you have "AS123M", it may
miss that M starts a word. I didn't bother testing for this behavior. I also
don't look through a partially accepted string to see if a new one should
start so if you say "M500x500S10000500x500S" then this is a failure and the
first portion will not be translated.
Each of these states tells us what is being expected. So, for instance,
start_start_start is expecting to see a word start. If it doesn't, then it just
sends the character along. But visual_size_secondw is expecting the second digit
of the number expressing the width. If it doesn't, then it will need to spit out
what it has already stored and then go back to start. This is also why there
aren't any functions for "_end", that would mean that it's expecting one
character past the last one.
*/
vector<uint32_t> line;
void start(ostream* fileOut, uint32_t c);
void punctuation(ostream* fileOut, uint32_t c);
void prefix(ostream* fileOut, uint32_t c);
void visual(ostream* fileOut, uint32_t c);
void start_start(ostream* fileOut, uint32_t c);
void punctuation_start(ostream* fileOut, uint32_t c);
void punctuation_symbol(ostream* fileOut, uint32_t c);
void punctuation_placement(ostream* fileOut, uint32_t c);
void prefix_symbol(ostream* fileOut, uint32_t c);
void visual_start(ostream* fileOut, uint32_t c);
void visual_size(ostream* fileOut, uint32_t c);
void visual_symbol(ostream* fileOut, uint32_t c);
void visual_placement(ostream* fileOut, uint32_t c);
void start_start_start(ostream* fileOut, uint32_t c);
void punctuation_start_start(ostream* fileOut, uint32_t c);
void punctuation_symbol_start(ostream* fileOut, uint32_t c);
void punctuation_symbol_first(ostream* fileOut, uint32_t c);
void punctuation_symbol_second(ostream* fileOut, uint32_t c);
void punctuation_symbol_third(ostream* fileOut, uint32_t c);
void punctuation_symbol_fill(ostream* fileOut, uint32_t c);
void punctuation_symbol_rotation(ostream* fileOut, uint32_t c);
void punctuation_placement_firstw(ostream* fileOut, uint32_t c);
void punctuation_placement_secondw(ostream* fileOut, uint32_t c);
void punctuation_placement_thirdw(ostream* fileOut, uint32_t c);
void punctuation_placement_x(ostream* fileOut, uint32_t c);
void punctuation_placement_firsth(ostream* fileOut, uint32_t c);
void punctuation_placement_secondh(ostream* fileOut, uint32_t c);
void punctuation_placement_thirdh(ostream* fileOut, uint32_t c);
void punctuation_placement_end(ostream* fileOut, uint32_t c);
void prefix_symbol_start(ostream* fileOut, uint32_t c);
void prefix_symbol_first(ostream* fileOut, uint32_t c);
void prefix_symbol_second(ostream* fileOut, uint32_t c);
void prefix_symbol_third(ostream* fileOut, uint32_t c);
void prefix_symbol_fill(ostream* fileOut, uint32_t c);
void prefix_symbol_rotation(ostream* fileOut, uint32_t c);
void visual_start_start(ostream* fileOut, uint32_t c);
void visual_size_firstw(ostream* fileOut, uint32_t c);
void visual_size_secondw(ostream* fileOut, uint32_t c);
void visual_size_thirdw(ostream* fileOut, uint32_t c);
void visual_size_x(ostream* fileOut, uint32_t c);
void visual_size_firsth(ostream* fileOut, uint32_t c);
void visual_size_secondh(ostream* fileOut, uint32_t c);
void visual_size_thirdh(ostream* fileOut, uint32_t c);
void visual_symbol_start(ostream* fileOut, uint32_t c);
void visual_symbol_first(ostream* fileOut, uint32_t c);
void visual_symbol_second(ostream* fileOut, uint32_t c);
void visual_symbol_third(ostream* fileOut, uint32_t c);
void visual_symbol_fill(ostream* fileOut, uint32_t c);
void visual_symbol_rotation(ostream* fileOut, uint32_t c);
void visual_placement_firstw(ostream* fileOut, uint32_t c);
void visual_placement_secondw(ostream* fileOut, uint32_t c);
void visual_placement_thirdw(ostream* fileOut, uint32_t c);
void visual_placement_x(ostream* fileOut, uint32_t c);
void visual_placement_firsth(ostream* fileOut, uint32_t c);
void visual_placement_secondh(ostream* fileOut, uint32_t c);
void visual_placement_thirdh(ostream* fileOut, uint32_t c);
void visual_placement_end(ostream* fileOut, uint32_t c);
int fswtotex(istream* fileIn, ostream* fileOut)
{
state = substate = subsubstate = s_start;
uint32_t c = 0;
while (c != 0xffffffff)
{
c = getChar(fileIn);
if (c == 0xffffffff)
continue;
if (state == s_start) start(fileOut, c);
else if (state == s_prefix) prefix(fileOut, c);
else if (state == s_visual) visual(fileOut, c);
else if (state == s_punctuation) punctuation(fileOut, c);
else throw "Unknown state.";
}
(*fileOut) << endl;
(*fileOut) << "% In order for this conversion to work your document needs a few things around "
"your SignWriting text." << endl;
(*fileOut) << endl;
(*fileOut) << "\\documentclass{article}" << endl;
(*fileOut) << endl;
(*fileOut) << "\\usepackage{fontspec}" << endl;
(*fileOut) << "\\usepackage{tikz}" << endl;
if ((rotation == -90) || (rotation == 90))
(*fileOut) << "\\usepackage[landscape]{geometry}" << endl;
if (mirror)
(*fileOut) << "\\usepackage[mirror]{crop}" << endl;
if (rotation != 0)
{
(*fileOut) << "\\usepackage{everypage}" << endl;
(*fileOut) << "\\AddEverypageHook{\\special{pdf: put @thispage <</Rotate " << rotation << ">>}}" << endl;
}
(*fileOut) << endl;
(*fileOut) << "\\begin{document}" << endl;
(*fileOut) << endl;
(*fileOut) << "\\newfontfamily\\swfill{SuttonSignWritingFill.ttf}" << endl;
(*fileOut) << "\\newfontfamily\\swline{SuttonSignWritingLine.ttf}" << endl;
if (fsize != defaultfsize)
{
(*fileOut) << "\\newlength{\\" << fsize << "}" << endl;
(*fileOut) << "\\setlength{\\" << fsize << "}{12pt}" << endl;
}
(*fileOut) << endl;
(*fileOut) << "% SignWriting text goes here" << endl;
(*fileOut) << endl;
(*fileOut) << "\\end{document}" << endl;
return 0;
}
/*
We are going to cover these states breadth first.
That is, all the base states, then the two-level states, then ...
*/
void start(ostream* fileOut, uint32_t c)
{
if (substate == s_start) start_start(fileOut, c);
else throw "Unknown substate in start.";
}
void punctuation(ostream* fileOut, uint32_t c)
{
if (substate == s_start) punctuation_start(fileOut, c);
else if (substate == s_symbol) punctuation_symbol(fileOut, c);
else if (substate == s_placement) punctuation_placement(fileOut, c);
else throw "Unknown substate in punctuation.";
}
void prefix(ostream* fileOut, uint32_t c)
{
if (substate == s_symbol) prefix_symbol(fileOut, c);
else throw "Unknown substate in prefix.";
}
void visual(ostream* fileOut, uint32_t c)
{
if (substate == s_start) visual_start(fileOut, c);
else if (substate == s_size) visual_size(fileOut, c);
else if (substate == s_symbol) visual_symbol(fileOut, c);
else if (substate == s_placement) visual_placement(fileOut, c);
else throw "Unknown substate in visual.";
}
void start_start(ostream* fileOut, uint32_t c)
{
if (subsubstate == s_start) start_start_start(fileOut, c);
else throw "Unknown subsubstate.";
}
void punctuation_start(ostream* fileOut, uint32_t c)
{
if (subsubstate == s_start) punctuation_start_start(fileOut, c);
else throw "Unknown subsubstate in punctuation, start.";
}
void punctuation_symbol(ostream* fileOut, uint32_t c)
{
if (subsubstate == s_start) punctuation_symbol_start(fileOut, c);
else if (subsubstate == s_first) punctuation_symbol_first(fileOut, c);
else if (subsubstate == s_second) punctuation_symbol_second(fileOut, c);
else if (subsubstate == s_third) punctuation_symbol_third(fileOut, c);
else if (subsubstate == s_fill) punctuation_symbol_fill(fileOut, c);
else if (subsubstate == s_rotation) punctuation_symbol_rotation(fileOut, c);
else throw "Unknown subsubstate in punctuation, symbol.";
}
void punctuation_placement(ostream* fileOut, uint32_t c)
{
if (subsubstate == s_firstw) punctuation_placement_firstw(fileOut, c);
else if (subsubstate == s_secondw) punctuation_placement_secondw(fileOut, c);
else if (subsubstate == s_thirdw) punctuation_placement_thirdw(fileOut, c);
else if (subsubstate == s_x) punctuation_placement_x(fileOut, c);
else if (subsubstate == s_firsth) punctuation_placement_firsth(fileOut, c);
else if (subsubstate == s_secondh) punctuation_placement_secondh(fileOut, c);
else if (subsubstate == s_thirdh) punctuation_placement_thirdh(fileOut, c);
else if (subsubstate == s_end) punctuation_placement_end(fileOut, c);
else throw "Unknown subsubstate in punctuation, placement.";
}
void prefix_symbol(ostream* fileOut, uint32_t c)
{
if (subsubstate == s_start) prefix_symbol_start(fileOut, c);
else if (subsubstate == s_first) prefix_symbol_first(fileOut, c);
else if (subsubstate == s_second) prefix_symbol_second(fileOut, c);
else if (subsubstate == s_third) prefix_symbol_third(fileOut, c);
else if (subsubstate == s_fill) prefix_symbol_fill(fileOut, c);
else if (subsubstate == s_rotation) prefix_symbol_rotation(fileOut, c);
else throw "Unknown subsubstate.";
}
void visual_start(ostream* fileOut, uint32_t c)
{
if (subsubstate == s_start) visual_start_start(fileOut, c);
else throw "Unknown subsubstate.";
}
void visual_size(ostream* fileOut, uint32_t c)
{
if (subsubstate == s_firstw) visual_size_firstw(fileOut, c);
else if (subsubstate == s_secondw) visual_size_secondw(fileOut, c);
else if (subsubstate == s_thirdw) visual_size_thirdw(fileOut, c);
else if (subsubstate == s_x) visual_size_x(fileOut, c);
else if (subsubstate == s_firsth) visual_size_firsth(fileOut, c);
else if (subsubstate == s_secondh) visual_size_secondh(fileOut, c);
else if (subsubstate == s_thirdh) visual_size_thirdh(fileOut, c);
else throw "Unknown subsubstate.";
}
void visual_symbol(ostream* fileOut, uint32_t c)
{
if (subsubstate == s_start) visual_symbol_start(fileOut, c);
else if (subsubstate == s_first) visual_symbol_first(fileOut, c);
else if (subsubstate == s_second) visual_symbol_second(fileOut, c);
else if (subsubstate == s_third) visual_symbol_third(fileOut, c);
else if (subsubstate == s_fill) visual_symbol_fill(fileOut, c);
else if (subsubstate == s_rotation) visual_symbol_rotation(fileOut, c);
else throw "Unknown subsubstate.";
}
void visual_placement(ostream* fileOut, uint32_t c)
{
if (subsubstate == s_firstw) visual_placement_firstw(fileOut, c);
else if (subsubstate == s_secondw) visual_placement_secondw(fileOut, c);
else if (subsubstate == s_thirdw) visual_placement_thirdw(fileOut, c);
else if (subsubstate == s_x) visual_placement_x(fileOut, c);
else if (subsubstate == s_firsth) visual_placement_firsth(fileOut, c);
else if (subsubstate == s_secondh) visual_placement_secondh(fileOut, c);
else if (subsubstate == s_thirdh) visual_placement_thirdh(fileOut, c);
else if (subsubstate == s_end) visual_placement_end(fileOut, c);
else throw "Unknown subsubstate.";
}
void start_start_start(ostream* fileOut, uint32_t c)
{
if (c == 'A' || c == 0x1d800)
{
line.push_back(c); state = s_prefix; substate = s_symbol; subsubstate = s_start;
}
else if (c == 'B' || (c >= 'L' && c <= 'M') || c == 'R' || (c >= 0x1d801 && c <= 0x1d804))
{
line.push_back(c); state = s_visual; substate = s_size; subsubstate = s_firstw;
}
else if (c == 'S')
{
line.push_back(c); state = s_punctuation; substate = s_symbol; subsubstate = s_first;
}
else
sendOut(fileOut, c);
}
void punctuation_start_start(ostream* fileOut, uint32_t c)
{
if (c == 'S')