summaryrefslogtreecommitdiff
path: root/support/dktools/fig2lat.ctr
blob: 22658a4b6a5b24182b2c6bcb3fb4b72b2009ddda (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
%%	options

copyright owner			=	Dirk Krause
copyright year			=	2012-xxxx
license				=	bsd
# splint comment character	=	@


%%	header

#include "dk3fig.h"
#include "dk3figrd.h"
#include "dk3figpr.h"


/**	To draw a quadrant of a circle we do not use 1/3 of the
	derivative, we use another factor instead.
*/
#define	FIG2LAT_CIRCLE_QUADRANT_BEZIER	0.5522847498307932



/**	Conversion configuration.
*/
typedef struct {
  double	nts;	/**< Normal text size (scale factor or -1.0). */
  int		ntf;	/**< Normal text font (0=similar, 1=fig). */
} fig2lat_config_t;



/**	How to handle a text.
*/
typedef struct {
  double	fontSize;	/**< Font size in pt. */
  unsigned long	fontNumber;	/**< Unique font number for LaTeX output. */
  int		psFontNo;	/**< PS font number. */
} f2l_text_handling_t;



/**	Image structure.
*/
typedef struct {
  dk3_ufi_t	 	 ufi;		/**< Unique file identifier image. */
  char const		*filename;	/**< Image file name. */
  dk3_pdf_xobject_t	*xo;		/**< PDF X object. */
  dk3_bif_coord_t	 width;		/**< Image width. */
  dk3_bif_coord_t	 height;	/**< Image height. */
  double		 xres;		/**< X resolution. */
  double		 yres;		/**< Y resolution. */
  unsigned long		 imageNumber;	/**< Unique number of image. */
  unsigned long		 lineno;	/**< Line number of object. */
} f2l_image_t;



/**	Fig2lat job structure.
*/
typedef struct {
  /*	@DRIVER@
  	You can add driver-specific configuration compomenents here.
	For alignment reasons the order of elements is in decreasing
	type size.
	Remember to re-compile all modules after changing data types.
	Initialize the new components in fig2lat_job_init(),
	release allocated resources in fig2lat_job_end().
  */
  dk3_graphics_state_t	 gs;		/**< Graphics state. */
  dk3_ct_2d_t		 ct2d;		/**< Coordinates transformation data. */
  dk3_fig_drawing_t	*drw;		/**< Fig drawing to convert. */
  dk3_app_t		*app;		/**< Application structure. */
  dk3_option_set_t	*opt;		/**< Options and cmd arguments. */
  FILE			*of1;		/**< Output file 1. */
  FILE			*of2;		/**< Output file 2. */
  dkChar const		*on1;		/**< Output file name 1. */
  dkChar const		*on2;		/**< Output file name 2. */
  dkChar const * const	*msg;		/**< Localized text messages. */
  dkChar const * const	*figmsg;	/**< Localized messages dk3fig mod. */
  double		 width;		/**< Width (typically in bp). */
  double		 height;	/**< Height (typically in bp). */
  double		 w2;		/**< Secondary width information. */
  double		 h2;		/**< Secondary height information. */
  double		 nts;		/**< Normal text size. */
  double		 tts;		/**< TeX text size. */
  double		 arcspp;	/**< Spline points per arc. */
  double		 splspp;	/**< Spline points per interval. */
  double		 xsprec;	/**< Iteration precision. */
  double		 lwbp;		/**< Line width in bp. */
  long			 lwidth;	/**< Width in bp. */
  long			 lheight;	/**< Height in bp. */
  size_t		 xssbs;		/**< X-spline segement Bezier segs. */
  size_t		 minspp;	/**< Minimum spline points. */
  size_t		 qbs;		/**< Quadrant Bezier segments. */
  size_t		 codi;		/**< Color digits. */
  int			 ntf;		/**< Text font (0=similar, 1=fig). */
  int			 dr;		/**< Driver, see @ref fig2latdrivers. */
  int			 exval;		/**< Exit, see @ref fig2latexitcodes. */
  int			 mm;		/**< Flag: Make mode. */
  int			 cmd;		/**< Command to execute. */
  int			 cosp;		/**< Flag: Spline compatibility. */
  int			 coah;		/**< Flag: Arrowhead compatibility. */
  int			 xsah;		/**< Flag: X-spline arrowhead. */
  int			 debug;		/**< Flag: Write debug information. */
  int			 showpage;	/**< Flag: Write showpage operator. */
  int			 pslevel;	/**< PS language level. */
  int			 dsc;		/**< Flag: Write PS DSC. */
  int			 srctype;	/**< Source file type. */
  int			 smash;		/**< Flag: Use smash instruction. */
  int			 mbox;		/**< Flag: Use mbox instruction. */
  int			 resfont;	/**< Flag: Use reset@font. */
  int			 css;		/**< Flag: CSS-styled SVG. */
  int			 fragment;	/**< Flag: Write SVG fragment only. */
  int			 svgfontbase;	/**< SVG font base: 0=none, 1=local, 2=web. */
  int			 group;		/**< Flag: Group SVG object with ahs. */
  int			 miterlim;	/**< Flag: Write miterlimit. */
  int			 cols;		/**< Flag: Compatible line styles. */
  int			 otherfonts;	/**< Flag: Use additional fonts SVG. */
  int			 bbts;		/**< Flag: Use text size for bb. */
  int			 stu8;		/**< Flag: Special text uses UTF-8. */
  int			 cofop;		/**< Flag: Compat fill open paths. */
  int			 lwauto;	/**< Flag: Automatic line width. */
} f2l_job_t;



/**	@defgroup	fig2latexitcodes	Exit codes for fig2lat.
*/
/**@{*/

/**	Program succeeded.
*/
#define	FIG2LAT_EXIT_SUCCESS		0

/**	Error occured, no detailed description.
*/
#define	FIG2LAT_EXIT_ERROR_UNKNOWN	1

/**	Error, no file found to convert.
*/
#define FIG2LAT_EXIT_ERROR_NO_SUCH_FILE	2

/**	File name is too long for processing.
*/
#define	FIG2LAT_EXIT_FILENAME_TOO_LONG	3

/**	Resources problem or system error.
*/
#define	FIG2LAT_EXIT_ERROR_SYSTEM	4

/**	Mathematical error.
*/
#define	FIG2LAT_EXIT_ERROR_MATH		5

/**	Syntax error in input file.
*/
#define	FIG2LAT_EXIT_ERROR_SYNTAX	6
/**@}*/



/**	@defgroup	fig2latdrivers		Fig2lat output drivers.
*/

/**@{*/

/**	Produce PGF output file (default driver).
*/
#define	FIG2LAT_DRIVER_PGF		0

/**	Produce full *.tex file, image as embedded PGF.
*/
#define	FIG2LAT_DRIVER_TEX_FULL_PGF	1

/**	Produce full *.tex file, image as included external PDF.
*/
#define	FIG2LAT_DRIVER_TEX_FULL_PDF	2

/**	Produce *.tex file including an external EPS.
*/
#define	FIG2LAT_DRIVER_EPS_WITH_TEX	3

/**	Produce *.tex file including an external PDF.
*/
#define	FIG2LAT_DRIVER_PDF_WITH_TEX	4

/**	Produce standalone EPS file.
*/
#define	FIG2LAT_DRIVER_EPS_STANDALONE	5

/**	Produce standalone SVG file.
*/
#define	FIG2LAT_DRIVER_SVG_STANDALONE	6

/*	@DRIVER@
	Add new driver numbers above this comment.
	The driver numbers must correspond to the order of
	driver names in f2lopt.ctr in the f2lopt_language_names array.
*/

/**@}*/



#include "f2lopt.h"
#include "f2lto.h"

%%	module


/**	Test flag to show Beziervalues.
*/
#define FIG2LAT_TEST_BEZIER 0



#include "dk3all.h"
#include "dk3bezcu.h"
#include "fig2lat.h"
#include "f2lud.h"
#include "f2lsvg.h"
#include "dk3figto.h"
#if 0
#include "dkt-version.h"
#endif
#include "dk4vers.h"


$!trace-include



/**	Keywords used by the module, not localized.
*/
static dkChar const * const f2l_kw[] = {
$!string-table	macro=dkT
#
#   0:	Application group name.
#
dktools
#
#   1:	String table file name.
#
fig2lat.str
#
#   2:	Help file name.
#
fig2lat.txt
#
#   3:	Source file name suffix.
#
.fig
#
#   4:	Output suffix.
#
.pgf
#
#   5:	Ouptut suffix.
#
.svg
#
#   6:	Output suffix.
#
.eps
#
#   7:	Output suffix.
#
.tex
#
#   8:	Output suffix.
#
.pdf
#
#   9:	File end for PDF file.
#
-i.pdf
$!end
};



/**	Version number.
*/
static dkChar const *fig2lat_version[] = {
  dkT("fig2lat "),
  DKT_VERSION_DK
};



/**	License conditions.
*/
static dkChar const * const fig2lat_license[] = {
$!text macro=dkT

Copyright (c) 2013-2016, Dirk Krause
All rights reserved.

Redistribution and use in source and binary forms,
with or without modification, are permitted provided
that the following conditions are met:

* Redistributions of source code must retain the above
  copyright notice, this list of conditions and the
  following disclaimer.
* Redistributions in binary form must reproduce the above 
  copyright notice, this list of conditions and the following
  disclaimer in the documentation and/or other materials
  provided with the distribution.
* Neither the name of the copyright holder(s) nor the names of
  contributors may be used to endorse or promote
  products derived from this software without specific
  prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED.
IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.

$!end
};



/**	Default help text, shown if no help file is found.
*/
static dkChar const * const	fig2lat_help_text[] = {
$!text macro=dkT

NAME

fig2lat - Fig to LaTeX conversion

SYNOPSIS

  fig2lat [-l <driver>] [<options>] [-o <key>=<value>]* <file>

DESCRIPTION

Fig2lat converts *.fig drawings created using XFig or jFig
to other graphics formats, mainly for use with the LaTeX typesetting
system.

OPTIONS

-l <driver>
 	Driver, one from the following:

	Drivers to use a Fig file with latex or pdflatex:

	pdf.tex		Produce a *.tex/*.pdf file pair.
			Include the *.tex file in your LaTeX source, the
			*.tex file includes the *.pdf file.

	eps.tex		Produce a *.tex/*.eps file pair.
			Include the *.tex file in your LaTeX source, the
			*.tex file includes the *.eps file.

	pgf		Produce a *.pgf file for inclusion in a *.tex
			source.

	Drivers to produce a standalone image:

	tex.pdf		Produce a full *.tex file for use with pdflatex.
			A file file-i.pdf is produced additionally,
			this file is included by the *.tex file.

	tex		Produce a full *.tex file for use with pdflatex.
			The image is embedded as a PGF image.

	eps		Produce a standalone EPS file.

	svg		Produce a standalone SVG file.

-m
	Make mode when running on a directory: Convert
	files only if no destination files up to date are found.

-o key=value
	Key/value options, may be used multiple times.
	The following keys can be used:


	General options:

	lw=<width>		Line width base.
				The default for Fig files is 1/80 inch.
				Width can be one of the following:
				<number>bp	PS points
				<number>pt	Width in pt
				<number>in	Width in inch
				<number>cm	Width in cm
				<number>mm	Width in mm
				lighten		Use 1/160 inch
				auto		0.4pt * textsize / 10

	Text handling options

	tf=<string>		Text font selection for normal (non-special)
				text.  Either ``similar'' or ``fig''.

	ts=<size>		Font size factor for normal (non-special) text.
				Either the keyword ``none'' or a scale factor
				(typically in the range 0.95 ... 1.0).

	bbts[=<boolean>]	Use text size in bounding box calculaion.

	smash[=<boolean>]	Use \smash instruction in LaTeX output.

	mbox[=<boolean>]	Use \mbos instruction in LaTeX output.

	reset@font[=<boolean>]	Use \reset@font instruction in LaTeX output.

	ste[=<encoding>]	Encoding used when writing special text
				to output file. Either ``plain'' or
				``utf-8''.


	X-Spline and Bezier-Spline options

	xsss[=<number>]		Number of Bezier-Spline subsegments per
				X-spline segment. Default: 8, minimum: 2.


	Arrowhead options:

	ahas=<number>		Number of X-spline segments for a 90 degree
				angle on arcs. Default: 4.

	ahss=<integer>		Number of arrowhead X-spline segments
				for each original X-spline segment for
				arrowheads on splines. Default: 4.

	ahms=<integer>		Minimum number of X-spline segments for
				each arrowhead edge. Default: 4

	ahip=<float>		Arrowhead iteration precision. Default: 0.04.


	Output options:

	codi=[<number>]		Number of digits used for colors. Default: 3.


	Compatibilty options (compatibility to fig2dev):

	cosp[=<boolean>]	Compatible splines.

	coah[=<boolean>]	Compatible arrowheads.

	xsah[=<boolean>]	X-spline arrowheads (not together with coah).

	cols[=<boolean>]	Compatible line styles.

	cofop[=<boolean>]	Compatible fill for open paths.


	Source type (mutually exclusive):

	xfig			File produced by XFig

	jfig			File produced by jFig.

	winfig			File produced by WinFIG.


	Driver specific options (tex, tex.pdf):

	dcts[=<number>]		Font size in pt used in the document class.


	Driver specific options (eps, eps.tex):

	showpage[=<boolean>]	Use showpage operator.

	level=<integer>		PS level, 2 or 3.

	Driver specific options (svg)

	css[=<boolean>]		Use CSS styling.

	group[=<boolean>]	Group arrowhead objects with the object they
				are attached to.

	fragment[=<boolean>]	Write an SVG fragment instead of a complete
				file.

	fontbase[=<string>]	Specify source for GhostScript fonts
				converted to TTF, "none", "local", or "web".

	replacementfonts=[<boolean>]	enables/disables use of replacement
					font families.

	miterlimit[=<boolean>]	Enables or disables use of "stroke-miterlimit"
				style setting.


EXIT STATUS

0 on success, all other exit status codes indicate an error.

FILES

Fig2lat uses the following files from the current working directory:

f2lfonts.tex	is used in the preamble of tex and tex.pdf output, the file
		contents replaces the font selection packages.

f2lother.tex	is used in the preamble of tex and tex.pdf output, the file
		contents replaces the packages for "other setup".

NOTES

There are differences between the output from fig2lat and the output
from fig2dev. This is intended, otherwise we would not need an additional
program.

* Interpolated X-splines: Fig2lat uses the correct formula q=-0.5*s
  as described in [BS1995].

* Arrowheads: All arrowhead types introduced in xfig 3.2.5 are supported.
  On splines and arcs we have curved arrowheads. The same line width as
  for the original line is used for the arrowhead too, the arrowhead
  linewidth specified in the Fig file is ignored.

* Text: When using LaTeX/pdfLaTeX related drivers (tex, tex.pdf, pdf.tex,
  eps.tex, pgf) all text is handled by LaTeX/pdfLaTeX. The tf setting
  controls whether to use PS fonts or similar feature LaTeX fonts for
  normal text. For special text no font selection instructions are written
  at all, so document default fonts are used.

* Fill patterns: Fill patterns are vector graphics in fig2lat.

* Splines: X-splines are converted to a sequence of Bezier splines instead
  of polylines.

KNOWN ISSUES

* The curved arrowheads on arcs and splines are implemented as sequences
  of short - sometimes very short - Bezier curves.
  At least for my arrowtest.fig test file I saw malformed arrowheads
  in some renderers although the same graphics were shown fine in other
  renderers.
  You can avoid the problem by using the coah option (compatible arrowheads).
  For Batik 1.7 Squiggle you can also attempt the miterlimit=no option.

AUTHOR

Dirk Krause

HISTORY

This program replaces the fig2vect program seen in previous versions
of dktools.

COPYRIGHT AND LICENSE

Run
  fig2lat --license-terms
to see the license conditions.

SEE ALSO

[BS1995]	Carole Blanc, Christophe Schlick:
		X-Splines: A Spline Model Designed for the End-User
		Proceedings of the SIGGRAPH 1995

[fig2lat]	http://dktools.sourceforge.net

$!end
};



/**	Default texts, used if localized texts are not found.
*/
static dkChar const * const f2l_default_messages[] = {
$!string-table	macro=dkT
#
#   0 1:	Invalid PS level preference value!
#
Invalid PS level preference value "
"!
#
#   2		Input file name too long!
#
Input file name too long:\n
#
#   3		Failed to read input file!
#
Failed to read input file!
#
#   4		Failed to process input file!
#
Failed to process input file!
#
#   5		No regular file, no directory!
#
Not a regular file, not a directory!
#
#   6		Command line options processing failed!
#
Command line options processing failed!
#
#   7 + 8	Unknown driver!
#
Unknown driver "
"!
#
#   9 + 10	Invalid tf value!
#
Invalid value "
" for tf, use "fig" or "similar"!
#
#  11 + 12	Invalid font size!
#
Invalid font size specification "
"!
#
#  13 + 14	Invalid size specification!
#
Invalid size specification "
" (overflow)!
#
#  15 + 16 + 17	+ 18	Value below minimum, maximum!
#
Value "
" is below the required minimum "
"!
" is above the required maximum "
#
#  19 + 20	Option requires positive argument!
#
Option "
" requires a positive argument!
#
#  21 + 22	Option requires a numeric argument!
#
Option "
" requires a numeric argument!
#
#  23		Source type already set!
#
Source type already set!
#
#  24 + 25	Option requires an unsigned numeric argument!
#
Option "
" requires an unsigned numeric argument!
#
#  26		Can not handle negative font size!
#
Can not handle negative font size!
#
#  27 + 28	Illegal option/key name!
#
Illegal option/key name "
"!
#
#  29 + 30	Option too long!
#
Option too long:\n"
"
#
#  31		Option argument too long!
#
Option argument too long:\n"
#
#  32		Option -l requires an argument!
#
Option -l requires an argument!
#
#  33		PS level must be 2 or 3!
#
PS level must be 2 or 3!
#
#  34		Minimum of arrowhead segments corrected to 2!
#
Minimum of arrowhead segments corrected to 2!
#
#  35		Mathematical problem (coordinates transformation)!
#
Mathematical problem (coordinates transformation)!
#
#  36		Mathematical problem (spline calculation)!
#
Mathematical problem (spline calculation)!
#
#  37		Arrowhead too long for spline!
#
Arrowhead too long for spline!
#
#  38		Mathematical problem (path construction)!
#
Mathematical problem (path construction)!
#
#  39		Arrowheads in summary too long for spline!
#
Arrowheads in summary too long for spline!
#
#  40		Failed to calculate graphics state transformation!
#
Failed to calculate graphics state transformation!
#
#  41		Failed to add instructions to PDF graphics!
#
Failed to add instructions to PDF graphics!
#
#  42		Skipping special text!
#
Skipping special text!
#
#  43		Unknown object type!
#
Unknown object type!
#
#  44
#
Special text found!
#
#  45 46
#
Not a line width: "
"!
$!end
};



/**	PS level preference name.
*/
static dkChar const fig2lat_pref_psl[] = {
  dkT("/print/ps/level")
};



#if 0
/**	Initialize a configuration structure.
	@param	conf	Structure to initialize.
*/
static
void
fig2lat_config_init(fig2lat_config_t *conf)
{
  $? "+ fig2lat_config_init PTR=%d", TR_IPTR(conf)
  dk3mem_res((void *)conf, sizeof(fig2lat_config_t));
  conf->nts = 1.0;
  conf->ntf = 0;
  $? "- fig2lat_config_init"
}
#endif



/**	Set PS level from preferences.
	@param	job	Job structure.
*/
static
void
fig2lat_set_ps_level_from_preferences(f2l_job_t *job)
{
  dkChar	buf[256];	/* Buffer to retrieve preference value. */
  int		i;		/* Conversion result. */
  if(dk3app_get_pref(job->app,fig2lat_pref_psl,buf,DK3_SIZEOF(buf,dkChar))) {
#if VERSION_BEFORE_20140716
    if(dk3sf_sscanf3(buf, dkT("%d"), &i))
#else
    if (0 != dk3ma_i_from_string(&i, buf, NULL))
#endif
    {
      if((i >= 2) && (i <= 3)) {
        job->pslevel = i;
      } else {
        /* Warning: Preference value is not a number! */
	dk3app_log_3(job->app, DK3_LL_WARNING, job->msg, 0, 1, buf);
      }
    } else {
      /* Warning: Preference value is not a number! */
      dk3app_log_3(job->app, DK3_LL_WARNING, job->msg, 0, 1, buf);
    }
  }
}



/**	Initialize job structure.
	@param	job	Job structure to initialize.
	@param	app	Application structure for diagnostics.
	@param	msg	Localized message texts.
	@param	figmsg	Localized message texts for the dk3fig module.
	@return 1 on success, 0 on error.
*/
static
int
fig2lat_job_init(
  f2l_job_t		*job,
  dk3_app_t		*app,
  dkChar const * const	*msg,
  dkChar const * const	*figmsg
)
{
  int back = 0;
  $? "+ fig2lat_job_init PTR=%d", TR_IPTR(job)
  dk3mem_res((void*)job, sizeof(f2l_job_t));
  dk3fig_gs_init(&(job->gs));
  job->app = app;
  job->msg = msg;
  job->figmsg = figmsg;
  job->opt = NULL;
  job->drw = NULL;
  job->on1 = NULL;
  job->on2 = NULL;
  job->of1 = NULL;
  job->of2 = NULL;
  job->nts = 1.0;
  job->tts = 12.0;
#if 0
  fig2lat_config_init(&(job->confProgram));
  fig2lat_config_init(&(job->confFile));
  fig2lat_config_init(&(job->confObject));
#endif
  job->dr = FIG2LAT_DRIVER_PDF_WITH_TEX;
  job->exval = FIG2LAT_EXIT_ERROR_UNKNOWN;
  job->mm = 0;
  job->cmd = 0;
  job->cosp = 0;
  job->coah = 0;
  job->xsah = 0;
  job->xssbs = 8;
  job->qbs = 4;
  job->debug = 0;
  job->showpage = 0;
  job->pslevel = 3;
  job->dsc = 0;
  job->arcspp = 8.0;
  job->splspp = 8.0;
  job->minspp = 4;
  job->xsprec = 0.04; /* originally 1200 / 25400 */
  job->srctype = DK3_FIG_SRCTYPE_UNKNOWN;
  job->smash = 1;
  job->mbox = 0;
  job->resfont = 1;
  job->codi = 3;
  job->css = 1;
  job->fragment = 0;
  job->svgfontbase = 0;
  job->group = 0;
  job->miterlim = 1;
  job->cols = 0;
  job->otherfonts = 1;
  job->bbts = 0;
  job->stu8 = 0;
  job->cofop = 1;
  job->lwbp = 0.9;	/* 1/80 inch */
  job->lwauto = 0;
  if(dk3app_get_encoding(app) == DK3_ENCODING_UTF8) {
    job->stu8 = 1;
  }
  fig2lat_set_ps_level_from_preferences(job);
  (job->ct2d).mx = (job->ct2d).my = (job->ct2d).nx = (job->ct2d).ny = 0.0;
  job->width = job->height = job->w2 = job->h2 = 0.0;
  job->lwidth = job->lheight = 0L;
  /*	@DRIVER@
  	If you added components to the f2l_job_t structure, initialize
	them to default values here.
  */
  back = 1;
  $? "- fig2lat_job_init %d", back
  return back;
}



/**	Clean up job structure.
	@param	job	Job structure to clean up.
*/
static
void
fig2lat_job_end(f2l_job_t *job)
{
  $? "+ fig2lat_job_end PTR=%d", TR_IPTR(job)
  /*	@DRIVER@
  	If you added components to the f2l_job_t structure
	make sure to release resources here.
  */
  if(job->opt) {
    dk3opt_close(job->opt); job->opt = NULL;
  }
  $? "- fig2lat_job_end"
}



/**	Show version information.
	@param	job	Job structure.
*/
static
void
fig2lat_show_version(f2l_job_t *job)
{
  dk3sf_initialize_stdout();
  dk3sf_fputs(fig2lat_version[0], stdout);
  dk3sf_fputs(fig2lat_version[1], stdout);
  dk3sf_fputc(dkT('\n'), stdout);
}



/**	Show license terms.
	@param	job	Job structure.
*/
static
void
fig2lat_show_license(f2l_job_t *job)
{
  dkChar const * const	*ptr;	/* Pointer to traverse all text lines. */
  ptr = fig2lat_license;
  dk3sf_initialize_stdout();
  while(*ptr) {
    dk3sf_fputs(*(ptr++), stdout);
    dk3sf_fputc(dkT('\n'), stdout);
  }
}



/**	Show help text.
	@param	job	Job structure.
*/
static
void
fig2lat_show_help(f2l_job_t *job)
{
  dk3app_help(job->app, f2l_kw[2], fig2lat_help_text);
}



/**	Check whether a file has a fig suffix.
	@param	fn	File name.
	@return	1 for fig files, 0 for other or no suffix.
*/
static
int
fig2lat_is_fig_file(dkChar const *fn)
{
  dkChar const	*sp;		/* Suffix found in file name. */
  int		 back = 0;
  sp = dk3str_get_suffix(fn);
  if(sp) {
    if(0 == dk3str_cmp(sp, f2l_kw[3])) {
      back = 1;
    }
  }
  return back;
}



/**	Create output file name.
	@param	job	Job structure.
	@param	ifn	Input file name.
	@param	osf	Output suffix.
	@return	Valid pointer to new output name on success, NULL on error.
*/
static
dkChar const *
fig2lat_set_one_output_name(
  f2l_job_t *job, dkChar const *ifn, dkChar const *osf
)
{
  dkChar	 buf[DK3_MAX_PATH];	/* Buffer for modification. */
  dkChar	*sp;			/* File name suffix in buffer. */
  dkChar const	*back = NULL;

  $? "+ fig2lat_set_one_output_name \"%!ds\" \"%!ds\"", TR_STR(ifn), TR_STR(osf)
  if(dk3str_len(ifn) < DK3_SIZEOF(buf,dkChar)) {
    dk3str_cpy_not_overlapped(buf, ifn);
    sp = dk3str_get_suffix(buf);
    if(sp) { *sp = dkT('\0'); }
    if((dk3str_len(buf) + dk3str_len(osf)) < DK3_SIZEOF(buf,dkChar)) {
      dk3str_cat(buf, osf);
      back = dk3str_dup_app(buf, job->app);
      if(!(back)) {
        job->exval = FIG2LAT_EXIT_ERROR_SYSTEM; $? ". exitcode"
      }
    } else {					$? "! too long"
      /* ERROR: Input file name too long! */
      job->exval = FIG2LAT_EXIT_FILENAME_TOO_LONG; $? ". exitcode"
      dk3app_log_2(job->app, DK3_LL_ERROR, job->msg, 2, buf);
    }
  } else {					$? "! ifn too long"
    /* ERROR: Input file name too long! */
    job->exval = FIG2LAT_EXIT_FILENAME_TOO_LONG; $? ". exitcode"
    dk3app_log_2(job->app, DK3_LL_ERROR, job->msg, 2, ifn);
  } $? "- fig2lat_set_one_output_name %!ds", TR_STR(back)
  return back;
}



/**	Set output file name(s) depending on input name and output driver.
	@param	job	Job structure.
	@param	ifn	Input file name.
	@return	1 on success, 0 on error.
*/
static
int
fig2lat_set_output_names(f2l_job_t *job, dkChar const *ifn)
{
  int			 back = 0;
  $? "+ fig2lat_set_output_names \"%!ds\"", TR_STR(ifn)
  switch(job->dr) {
    case FIG2LAT_DRIVER_TEX_FULL_PGF: {
      job->on1 = fig2lat_set_one_output_name(job, ifn, f2l_kw[7]);
      if(job->on1) { back = 1; }
    } break;
    case FIG2LAT_DRIVER_TEX_FULL_PDF: {
      job->on1 = fig2lat_set_one_output_name(job, ifn, f2l_kw[7]);
      job->on2 = fig2lat_set_one_output_name(job, ifn, f2l_kw[9]);
      if((job->on1) && (job->on2)) { back = 1; }
    } break;
    case FIG2LAT_DRIVER_EPS_WITH_TEX: {
      job->on1 = fig2lat_set_one_output_name(job, ifn, f2l_kw[7]);
      job->on2 = fig2lat_set_one_output_name(job, ifn, f2l_kw[6]);
      if((job->on1) && (job->on2)) { back = 1; }
    } break;
    case FIG2LAT_DRIVER_PDF_WITH_TEX: {
      job->on1 = fig2lat_set_one_output_name(job, ifn, f2l_kw[7]);
      job->on2 = fig2lat_set_one_output_name(job, ifn, f2l_kw[8]);
      if((job->on1) && (job->on2)) { back = 1; }
    } break;
    case FIG2LAT_DRIVER_EPS_STANDALONE: {
      job->on1 = fig2lat_set_one_output_name(job, ifn, f2l_kw[6]);
      if(job->on1) { back = 1; }
    } break;
    case FIG2LAT_DRIVER_SVG_STANDALONE: {
      job->on1 = fig2lat_set_one_output_name(job, ifn, f2l_kw[5]);
      if(job->on1) { back = 1; }
    } break;
    default: {	/* PGF */
      job->on1 = fig2lat_set_one_output_name(job, ifn, f2l_kw[4]);
      if(job->on1) { back = 1; }
    } break;
  }
  $? ". on1 = \"%!ds\"", TR_STR(job->on1)
  $? ". on2 = \"%!ds\"", TR_STR(job->on2)
  if(!(back)) {
    f2l_tool_set_exit_status(job, FIG2LAT_EXIT_ERROR_UNKNOWN); $? ". exitcode"
  }
  $? "- fig2lat_set_output_names %d \"%!ds\"", back, TR_STR(ifn)
  return back;
}



/**	Run for one file.
	@param	job	Job structure.
	@param	fn	File name (no wildcards contained).
*/
static
void
fig2lat_run_for_file(f2l_job_t *job, dkChar const *fn)
{
  dkChar const	*oldsrcname;	/* Saved source file name. */
#if DK3_HAVE_SETLOCALE && defined(LC_NUMERIC) && DK3_HAVE_LOCALE_H
  char		*oldlocale;	/* Old locale setting. */
#endif
  unsigned long	 oldsrcline;	/* Saved source file line number. */
  $? "+ fig2lat_run_for_file \"%!ds\"", TR_STR(fn)
  oldsrcname = dk3app_get_source_file(job->app);
  oldsrcline = dk3app_get_source_line(job->app);
  dk3app_set_source_file(job->app, fn);
  dk3app_set_source_line(job->app, 0UL);
#if 0
  /*
  	Copy program configuration to file configuration.
  */
  dk3mem_cpy(
    (void *)(&(job->confFile)),
    (void *)(&(job->confProgram)),
    sizeof(fig2lat_config_t)
  );
#endif
  job->on1 = NULL;
  job->on2 = NULL;
  if(fig2lat_set_output_names(job, fn)) {
    job->drw = dk3fig_drawing_new(job->app, job->figmsg);
    if(job->drw) {
      dk3fig_set_options_bsegs(job->drw, job->xssbs, job->qbs);
      dk3fig_set_options_lwbp(job->drw, job->lwbp);
      dk3fig_set_options_coah(job->drw, job->coah);
      dk3fig_set_options_xsah(job->drw, job->xsah);
      dk3fig_set_options_cosp(job->drw, job->cosp);
      dk3fig_set_options_bbts(job->drw, job->bbts);
      dk3fig_set_options_srctype(job->drw, job->srctype);
      dk3fig_set_options_arrows(job->drw,job->arcspp,job->splspp,job->minspp);
      dk3fig_set_options_iteration_precision(job->drw, job->xsprec);
      dk3fig_set_options_codi(job->drw, job->codi);
      dk3fig_set_options_cofop(job->drw, job->cofop);
      if(dk3fig_read_file_name(job->drw, fn)) {	$? ". drawing read"
        if(dk3fig_prepare(job->drw)) {		$? ". drawing prepared"
#if DK3_HAVE_SETLOCALE && defined(LC_NUMERIC) && DK3_HAVE_LOCALE_H
	  oldlocale = setlocale(LC_NUMERIC, "C");
#endif
	  dk3fig_gs_init(&(job->gs));
	  switch(job->dr) {
	    case FIG2LAT_DRIVER_SVG_STANDALONE: {
	      f2lsvg_output(job);
	    } break;
	    /*	@DRIVER@
	    	Add a case branch to call your output driver here.
		If any error occurs in the driver function, set
		job->exval to indicate the error.
	    */
	    default: {
	      f2lud_output(job);
	    } break;
	  }
#if DK3_HAVE_SETLOCALE && defined(LC_NUMERIC) && DK3_HAVE_LOCALE_H
	  setlocale(LC_NUMERIC, oldlocale);
#endif
        } else {				$? "! failed to prepare drw"
	  /* ERROR while preparing drawing! */
	  f2l_tool_set_exit_status(job, FIG2LAT_EXIT_ERROR_UNKNOWN); $? ". exitcode"
          dk3app_log_1(job->app, DK3_LL_ERROR, job->msg, 4);
        }
      } else {					$? "! failed to read drawing"
        /* ERROR while reading input file! */
        f2l_tool_set_exit_status(job, FIG2LAT_EXIT_ERROR_UNKNOWN); $? ". exitcode"
        dk3app_log_1(job->app, DK3_LL_ERROR, job->msg, 3);
      }
      dk3fig_drawing_delete(job->drw); job->drw = NULL;
    } else {
      f2l_tool_set_exit_status(job, FIG2LAT_EXIT_ERROR_SYSTEM); $? ". exitcode"
    }
  }
  dk3_release(job->on1);
  dk3_release(job->on2);
  dk3app_set_source_file(job->app, oldsrcname);
  dk3app_set_source_line(job->app, oldsrcline);
  $? "- fig2lat_run_for_file"
}



/**	Check whether there is an up-to-date output file for the given
	file name and output suffix.
	@param	job	Job structure.
	@param	fn	Source file name.
	@param	sn	Suffix for output file name.
	@return	1 if output file is available, 0 otherwise.
*/
static
int
fig2lat_up_to_date_output(f2l_job_t *job, dkChar const *fn, dkChar const *sn)
{
  dk3_stat_t	 stbs;			/* Source file information. */
  dk3_stat_t	 stbd;			/* Destination file information. */
  dkChar	 fnb[DK3_MAX_PATH];	/* File name buffer. */
  dkChar 	*sp;			/* File name suffix in buffer. */
  int		 back = 0;
  $? "+ fig2lat_up_to_date_output \"%!ds\" \"%!ds\"", TR_STR(fn), TR_STR(sn)
  if(dk3str_len(fn) < DK3_SIZEOF(fnb,dkChar)) {
    dk3str_cpy_not_overlapped(fnb, fn);
    sp = dk3str_get_suffix(fnb);
    if(sp) {
      *sp = dkT('\0');
      if((dk3str_len(fnb) + dk3str_len(sn)) < DK3_SIZEOF(fnb,dkChar)) {
        dk3str_cpy_not_overlapped(sp, sn);
	if(dk3sf_stat_app(&stbs, fn, job->app)) {
	  if(dk3sf_stat_app(&stbd, fnb, NULL)) {
	    if(stbd.mod > stbs.mod) {
	      back = 1;				$? ". output file ok"
	    }
	  }
	}
      }
    }
  } $? "- fig2lat_up_to_date_output %d", back
  return back;
}



/**	Check whether we need to process a fig file found in a directory.
	@param	job	Job structure.
	@param	fn	File name to check.
	@return	1 to process the file, 0 to skip processing.
*/
static
int
fig2lat_must_process_file(f2l_job_t *job, dkChar const *fn)
{
  int		 back = 1;
  $? "+ fig2lat_must_process_file \"%!ds\"", TR_STR(fn)
  if(job->mm) {
    switch(job->dr) {
      case FIG2LAT_DRIVER_TEX_FULL_PGF: {
        if(fig2lat_up_to_date_output(job, fn, f2l_kw[7])) {
	  back = 0;
	}
      } break;
      case FIG2LAT_DRIVER_TEX_FULL_PDF: {
        if(fig2lat_up_to_date_output(job, fn, f2l_kw[7])) {
        if(fig2lat_up_to_date_output(job, fn, f2l_kw[9])) {
	  back = 0;
	}
	}
      } break;
      case FIG2LAT_DRIVER_EPS_WITH_TEX: {
        if(fig2lat_up_to_date_output(job, fn, f2l_kw[6])) {
        if(fig2lat_up_to_date_output(job, fn, f2l_kw[7])) {
	  back = 0;
	}
	}
      } break;
      case FIG2LAT_DRIVER_PDF_WITH_TEX: {
        if(fig2lat_up_to_date_output(job, fn, f2l_kw[7])) {
        if(fig2lat_up_to_date_output(job, fn, f2l_kw[8])) {
	  back = 0;
	}
	}
      } break;
      case FIG2LAT_DRIVER_EPS_STANDALONE: {
        if(fig2lat_up_to_date_output(job, fn, f2l_kw[6])) {
	  back = 0;
	}
      } break;
      case FIG2LAT_DRIVER_SVG_STANDALONE: {
        if(fig2lat_up_to_date_output(job, fn, f2l_kw[5])) {
	  back = 0;
	}
      } break;
      default: {	/* PGF */
        if(fig2lat_up_to_date_output(job, fn, f2l_kw[4])) {
	  back = 0;
	}
      } break;
    }
  } $? "- fig2lat_must_process_file %d", back
  return back;
}



/**	Process a directory.
	@param	job	Job structure.
	@param	fn	Directory name.
*/
static
void
fig2lat_run_for_directory(f2l_job_t *job, dkChar const *fn)
{
  dk3_dir_t		*dir;		/* Directory traversal. */
  dkChar const		*filename;	/* Found file name. */
  $? "+ fig2lat_run_for_directory \"%!ds\"", TR_STR(fn)
  dir = dk3dir_open_app(fn, job->app);
  if(dir) {
    while(dk3dir_get_next_file(dir)) {
      filename = dk3dir_get_fullname(dir);
      if(filename) {
        if(fig2lat_is_fig_file(filename)) {
	  if(fig2lat_must_process_file(job, filename)) {
	    fig2lat_run_for_file(job, filename);
	  }
	}
      } else {					$? "! bug"
      }
    }
    dk3dir_close(dir);
  } else {
    f2l_tool_set_exit_status(job, FIG2LAT_EXIT_ERROR_SYSTEM); $? ". exitcode"
  }
  $? "- fig2lat_run_for_directory"
}



/**	Run for file or directory name directly (no wildcards).
	@param	job	Job structure.
	@param	fn	File name (wildcards already resolved).
*/
static
void
fig2lat_run_for_file_directly(f2l_job_t *job, dkChar const *fn)
{
  dk3_stat_t		stb;	/* File information. */
  $? "+ fig2lat_run_for_file_directly \"%!ds\"", TR_STR(fn)
  if(dk3sf_stat_app(&stb, fn, job->app)) {
    switch((stb.ft) & (~(DK3_FT_SYMLINK))) {
      case DK3_FT_REGULAR: {
        fig2lat_run_for_file(job, fn);
      } break;
      case DK3_FT_DIRECTORY: {
        fig2lat_run_for_directory(job, fn);
      } break;
      default: {
        /* ERROR: Neither directory nor regular file! */
        job->exval = FIG2LAT_EXIT_ERROR_NO_SUCH_FILE; $? ". exitcode"
        dk3app_log_1(job->app, DK3_LL_ERROR, job->msg, 5);
      } break;
    }
  } else {						$? "! stat failed"
    job->exval = FIG2LAT_EXIT_ERROR_NO_SUCH_FILE; $? ". exitcode"
  }
  $? "- fig2lat_run_for_file_directly"
}



/**	Run for file or directory name (may contain wildcards).
	@param	job	Job structure.
	@param	fn	File name (may contain wildcards).
*/
static
void
fig2lat_run_for_name(f2l_job_t *job, dkChar const *fn)
{
  dkChar	 fnb[DK3_MAX_PATH];	/* Name buffer for modification. */
  dkChar const	*filename;		/* File name found. */
  dk3_dir_t	*dir;			/* Expander for wildcards. */
  int		 found;			/* Flag: Source file found. */
  $? "+ fig2lat_run_for_name \"%!ds\"", TR_STR(fn)
  if(dk3str_len(fn) < DK3_SIZEOF(fnb,dkChar)) {
    dk3str_cpy_not_overlapped(fnb, fn);
    dk3str_correct_filename(fnb);
    if(dk3sf_must_expand(fnb)) {
      dir = dk3dir_fne_open_app(fnb, job->app);
      if(dir) {
        found = 0;
	while(dk3dir_get_next_file(dir)) {
	  filename = dk3dir_get_fullname(dir);
	  if(filename) {
	    if(fig2lat_is_fig_file(filename)) {			$? ". fig file"
	      found = 1;
	      fig2lat_run_for_file(job, filename);
	    } else {					$? ". other file"
	    }
	  } else {					$? "! bug, no filename"
	  }
	}
	if(0 == found) {
	  job->exval = FIG2LAT_EXIT_ERROR_NO_SUCH_FILE; $? ". exitcode"
	}
        dk3dir_close(dir);
      } else {						$? "! fne_open"
        job->exval = FIG2LAT_EXIT_ERROR_SYSTEM; $? ". exitcode"
      }
    } else {
      fig2lat_run_for_file_directly(job, fnb);
    }
  } else {						$? "! too long"
    /* ERROR: File name too long! */
    job->exval = FIG2LAT_EXIT_FILENAME_TOO_LONG; $? ". exitcode"
    dk3app_log_2(job->app, DK3_LL_ERROR, job->msg, 2, fn);
  } $? "- fig2lat_run_for_name"
}



/**	Run for current directory.
	@param	job	Job structure.
*/
static
void
fig2lat_run_for_current_directory(f2l_job_t *job)
{
  dkChar cwdbu[DK3_MAX_PATH];	/* Buffer for current working directory. */
  $? "+ fig2lat_run_for_current_directory"
  if(dk3sf_getcwd_app(cwdbu, DK3_SIZEOF(cwdbu,dkChar), job->app)) {
    fig2lat_run_for_file_directly(job, cwdbu);
  } else {		$? "! getcwd"
    f2l_tool_set_exit_status(job, FIG2LAT_EXIT_ERROR_SYSTEM); $? ". exitcode"
  }
  $? "- fig2lat_run_for_current_directory"
}



/**	Run conversion as indicated by command line arguments.
	@param	job	Job structure.
*/
static
void
fig2lat_run(f2l_job_t *job)
{
  dkChar const	*fn;	/* File name. */
  int		 argc;	/* Number of command line arguments. */
  int		 i;	/* Current command line argument processed. */
  $? "+ fig2lat_run"
  if((argc = dk3opt_get_num_args(job->opt)) > 0) { $? ". have arguments"
    job->exval = FIG2LAT_EXIT_SUCCESS;	$? ". exitcode-success"
    for(i = 0; i < argc; i++) {
      fn = dk3opt_get_arg(job->opt, i);
      if(fn) {
        fig2lat_run_for_name(job, fn);
      } else {
        job->exval = FIG2LAT_EXIT_ERROR_NO_SUCH_FILE; $? ". exitcode"
      }
    }
  } else {					$? ". no arguments"
    fig2lat_run_for_current_directory(job);
  }
  $? "- fig2lat_run"
}



#if TRACE_DEBUG
#if FIG2LAT_TEST_BEZIER
static
void
fig2lat_test_bezier(void)
{
  double t;
  size_t i;
  t = 0.0;
  for(i = 0; i <= 10; i++) {
    printf("%g\t%g\n",t,dk3bezier_value(0.0,1.0,9.0,10.0,t,NULL));
    t += 0.1;
  }
}
#endif
#endif


/**	Entry point.
	@param	argc	Number of command line arguments.
	@param	argv	Command line arguments array.
	@return	0 on success, any other value indicates an error.
*/
DK3_MAIN
{
  f2l_job_t		 job;		/* Job structure. */
  dk3_app_t		*app = NULL;	/* Application structure. */
  dkChar const * const	*msg = NULL;	/* Localized texts. */
  dkChar const * const	*figmsg = NULL;	/* dk3fig module localized texts. */
  int			 exval	= FIG2LAT_EXIT_ERROR_UNKNOWN;	/* Exit code. */
  int			 testcmd;	/* Mask for help/version/license. */
  $!trace-init	fig2lat.deb
  $? "+ main"
#if VERSION_BEFORE_20150821
  exval = FIG2LAT_EXIT_ERROR_UNKNOWN;
#endif
  job.opt = NULL;
  app = dk3app_open_command(
    argc, (dkChar const * const *)argv, f2l_kw[0]
  );
  if(app) {			$? ". app"
    msg = dk3app_messages(
      app, f2l_kw[1], (dkChar const **)f2l_default_messages
    );
    figmsg = dk3fig_get_localized_messages(app);
#if TRACE_DEBUG
#if FIG2LAT_TEST_BEZIER
    fig2lat_test_bezier();
#endif
#endif
    if(fig2lat_job_init(&job,app,msg,figmsg)) {	$? ". init"
      if(f2lopt_process(&job)) {		$? ". option processing"
        testcmd = DK3_APP_CMD_HELP | DK3_APP_CMD_VERSION | DK3_APP_CMD_LICENSE;
        if((job.cmd) & testcmd) {		$? ". help version license"
	  if(job.cmd) {				$? ". show version information"
	    fig2lat_show_version(&job);
	  }
	  if((job.cmd) & DK3_APP_CMD_LICENSE) {	$? ". show license"
	    fig2lat_show_license(&job);
	  }
	  if((job.cmd) & DK3_APP_CMD_HELP) {	$? ". show help text"
	    fig2lat_show_help(&job);
	  }
	  job.exval = FIG2LAT_EXIT_SUCCESS;
	} else {				$? ". normal processing"
	  fig2lat_run(&job);
	}
      } else {					$? "! option processing"
        dk3app_log_1(job.app, DK3_LL_ERROR, job.msg, 6);
      }
    } else {					$? "! init"
    }
    exval = job.exval;
    dk3app_close(app); app = NULL;
  } else {			$? "! app"
    fputs("fig2lat: ERROR: Not enough memory (RAM/swap)!\n", stderr);
    fflush(stderr);
    exval = FIG2LAT_EXIT_ERROR_SYSTEM;
  }
  fig2lat_job_end(&job);
  $? "- main %d", exval
  $!trace-end
  fflush(stdout);
  exit(exval); return exval;
}