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
|
/* options.c - command line arguments processing
$Id: options.c,v 0.2 1997/03/28 03:17:12 tjchol01 Exp $
Authors: Andrew Trevorrow, Ian Dall, Geoffrey Tobin, Tomasz J. Cholewo
*/
#include "dvgt.h"
#include "screenio.h"
#include "options.h"
#include "defaults.h"
/* Variables Exported, via "options.h" */
/* X and Y resolutions, in dpi */
double xres, yres;
/* Magnification */
int mag;
/* The next four are in paper pixels, and calculated. */
int hoffset, voffset;
int paperwd, paperht;
String dummy_pk, dummy_tfm;
String vdu;
/* DVI file's name */
string DVIname;
typedef struct
{
units unit;
char *name;
double per_inch;
}
Dan; /* `Dimension and name' */
static
Dan dans[] = /* `dimensions and names series' */
{
{ic, "in", 1.0},
{cm, "cm", 2.54},
{mm, "mm", 25.4},
{bp, "bp", 72.0},
{pc, "pc", 72.27 / 12},
{pt, "pt", 72.27},
{sp, "sp", 72.27 * 65536},
{px, "px", 300.0} /* "px"'s per_inch value is not used, */
}; /* but Qume, Laserwriter, etc, are 300 dpi */
static
size_t ldans = sizeof (dans) / sizeof (Dan); /* length of dans[] */
static int Inint PARAMS ((double f));
/*--------------------------------------------------------------------*/
/* Auxiliary routines */
/*--------------------------------------------------------------------*/
/* Copy at most n characters from ct to s; set s[n] = NUL character. */
/* This ASSUMES that s has memory >= n+1 characters. */
char *
strMcpy (char *s, const char *ct, size_t n)
{
strncpy (s, ct, n);
s[n] = '\0';
return s;
}
/* strMcpy */
/*--------------------------------------------------------------------*/
/* Copy at most maxstring characters from ct to s; */
/* set s[maxstring] = NUL character. */
char *
stringcopy (String s, const char *ct)
{
strncpy (s, ct, maxstring);
s[maxstring] = '\0';
return (char *) &s;
}
/* stringcopy */
/*--------------------------------------------------------------------*/
static int
Inint (double f)
{
return ((int) (f + 0.5));
}
/* Inint */
/*--------------------------------------------------------------------*/
static void
ToCardinal (const char *option, const char *value, int *n)
{
/* If value represents a positive integer, then return via n. */
int check;
double r;
check = sscanf (value, "%lf", &r);
*n = Inint (r); /* round value to nearest integer */
/* check can be 1 or EOF if ok! */
if ((check == 1 || check == EOF) && *n > 0)
return;
FATAL2 ("Bad `%s' value: %s. Specify a positive integer.", option, value);
}
/* ToCardinal */
/*--------------------------------------------------------------------*/
static void
ToRes (const char *option, const char *value, double *xr, double *yr)
{
/* X and Y resolutions, specified in dots per inch. */
int check;
check = sscanf (value, "%lf,%lf", xr, yr);
if (check == 2 && *xr > 0 && *yr > 0)
{
return;
}
else
{
check = sscanf (value, "%lf", xr);
if (check == 1 && *xr > 0)
{
*yr = *xr;
return;
}
}
FATAL2 ("Bad `%s' value(s): %s.\n"
"Resolutions must be positive reals,\n"
"specified as `-r xres' or `-r xres,yres'\n"
"in dots per inch.", option, value);
}
/* ToRes */
/*--------------------------------------------------------------------*/
static units
Name2Unit (const char *uname)
{
units unit = ic;
char Luname[3]; /* Lower case unit name */
int j;
#define ToLower(x) (isupper(x) ? tolower(x) : (x))
Luname[0] = ToLower (uname[0]);
Luname[1] = ToLower (uname[1]);
Luname[2] = '\0';
for (j = 0; j < ldans && strcmp (Luname, dans[j].name); j++); /* Search for the name of the unit. */
if (j < ldans)
unit = dans[j].unit;
else
{
MesgString ("Bad unit name `");
MesgString (uname);
MesgString ("'");
MesgLine ();
MesgString ("Last two letters should be one of ");
for (j = 0; j < ldans; j++)
{
MesgString (" `");
MesgString (dans[j].name);
MesgString ("'");
}
MesgChar ('.');
MesgLine ();
MesgString ("Using inch.");
MesgLine ();
unit = ic;
}
return unit;
}
/* Name2Unit */
/*--------------------------------------------------------------------*/
static void
ToDimen (const char *option, const char *value, double *r, units *un)
{
/* A valid dimension consists of an integer or real number followed
by a two-letter unit: see dans[] above.
If value represents a valid dimension,
return number part in r, and units part in un.
*/
int check;
String nun; /* name of unit - ample space in a string for it */
check = sscanf (value, "%lf%2s", r, nun);
if (check == 2)
{
/* extract unit's code from its name */
*un = Name2Unit (nun);
}
else if (check == 1)
{
MesgString ("No dimension given, using inch.");
MesgLine ();
*un = ic;
}
else
{ /* check < 1 */
FATAL2 ("Bad `%s' value: %s.\nSpecify a dimensioned quantity.",
option, value);
}
}
/* ToDimen */
/*--------------------------------------------------------------------*/
static void
ToPosDimen (const char *option, const char *value, double *r, units *un)
{
/* A valid +ve dimension consists of a positive integer or real number
followed by a two-letter unit: see dans[] above.
If value represents a valid dimension,
return numeric part in r, and units part in un.
*/
ToDimen (option, value, r, un);
if (*r <= 0)
{
FATAL2 ("Bad `%s' value: %s.\nSpecify a positive dimension.",
option, value);
}
}
/* ToPosDimen */
/*--------------------------------------------------------------------*/
static int
DimenPixels (double r, units u, double res)
{
/* Return given dimension as nearest whole number of pixels, */
/* given the appropriate (eg, X or Y direction's) resolution in dpi. */
int Result;
int i;
if (u == px)
Result = Inint (r);
else
{
for (i = 0; i < ldans && u != dans[i].unit; i++); /* search for unit */
if (i < ldans)
Result = Inint (r / dans[i].per_inch * res);
else
{
MesgString ("Unimplemented unit, treating as inch.");
MesgLine ();
Result = Inint (r * res);
}
}
return Result;
}
/* DimenPixels */
/*--------------------------------------------------------------------*/
void
WriteCardinal (char *name, int value)
{
String outstring;
sprintf (outstring, "%s= %d", name, value);
MesgString (outstring);
MesgLine ();
}
/* WriteCardinal */
/*--------------------------------------------------------------------*/
void
WriteReal (char *name, double value)
{
String outstring;
sprintf (outstring, "%s= %f", name, value);
MesgString (outstring);
MesgLine ();
}
/* WriteReal */
/*--------------------------------------------------------------------*/
/* GT - TO BE FINISHED - Write... FUNCTIONS BELOW, TOO! */
/* Might be better to use a pair of arrays, or array of pairs */
/* That is, data-driven may be more flexible. */
void
NameUnit (units un, String nun)
/* Interface: un (I), *nun (O). */
{
int i;
for (i = 0; i < ldans && un != dans[i].unit; i++); /* Search for unit un. */
if (i < ldans)
stringcopy (nun, dans[i].name);
else
{
MesgString ("Unknown unit! Assuming inch.");
MesgLine ();
stringcopy (nun, "in");
}
}
/* NameUnit */
/*--------------------------------------------------------------------*/
void
WriteDimen (char *varname, double r, units un)
{
String outstring, nun;
NameUnit (un, nun);
/* !! gt - this line is unsafe, as outstring may overflow! */
sprintf (outstring, "%s= %f %s", varname, r, nun);
MesgString (outstring);
MesgLine ();
}
/* WriteDimen */
/*--------------------------------------------------------------------*/
void
WriteName (char *varname, char *svalue)
{
String outstring;
sprintf (outstring, "%s= %s", varname, svalue);
MesgString (outstring);
MesgLine ();
}
/* WriteName */
/*--------------------------------------------------------------------*/
double
Unit2Value (units unit)
{
double per_inch = 1.0; /* 1 unit per inch */
int j;
for (j = 0; (j < ldans) && (unit != dans[j].unit); j++); /* Search for the unit. */
if (j < ldans)
per_inch = dans[j].per_inch;
else
{
FATAL1 ("Bad unit, enumeration value = %d", unit);
}
return per_inch;
}
/* Unit2Value */
/*--------------------------------------------------------------------*/
int
InitOptions (int argc, char **argv)
{
/* Get DVI file, and any options, from the command line.
If an option appears more than once, then we return the last value.
Actually, proceed in three steps, in increasing priority:
(1) Initialise to default values, as set in "defaults.h".
(2) If the appropriate environment variable is set, use that.
(3) If the appropriate command line option is set, use that.
*/
char *env; /* environment variable */
units def_unit = ic; /* default unit is inch */
units hoffu, voffu, xu, yu;
double hoffr, voffr, xr, yr;
boolean landscape;
int c;
/*
(1) DEFAULTS.
initialize option values with defaults; note that the dv script can
set up different defaults [- quoth Andrew Trevorrow, I presume]
- gt observes - no "dv" script provided with DVItoVDU version 3.0!
*/
/* "dpi" means "dots per inch" */
/* printer's X and Y resolutions in dpi */
xres = (double) DEF_XRES;
yres = (double) DEF_YRES;
/* Page Offsets from Paper edges */
hoffu = def_unit;
voffu = def_unit;
hoffr = 0.0; /* no margin shifting, so 1" left and top margins */
voffr = 0.0;
mag = 0; /* use DVI file's intrinsic magnification */
/*
(2) ENVIRONMENT VARIABLES.
*/
/* Resolution, in dots per inch */
/* If DV_RES is set in the environment, then try to use that. */
/* If two real values are in DV_RES, copy them to xres and yres. */
/* If only one is in DV_RES, copy it to both xres and yres. */
/* If none is, then ignore DV_RES. */
if ((env = getenv ("DV_RES")) != (char *) NULL)
ToRes (" -r", env, &xres, &yres);
#if 0
if (!(env = getenv ("DV_TERM")))
env = getenv ("TERM");
stringcopy (vdu, !env ? "" : env);
#else
stringcopy (vdu, "tek4010");
#endif
/* Paper Width */
ToPosDimen ("", !(env = getenv ("DV_PAPERWD")) ? DEF_PAPERWD : env,
&xr, &xu);
/* Paper Height */
ToPosDimen ("", !(env = getenv ("DV_PAPERHT")) ? DEF_PAPERHT : env,
&yr, &yu);
/* fall-back PK font */
stringcopy (dummy_pk, !(env = getenv ("DV_DUMMY_PK")) ? DEF_DUMMY_PK : env);
/* fall-back TFM metric */
stringcopy (dummy_tfm, !(env = getenv ("DV_DUMMY_TFM")) ? DEF_DUMMY_TFM : env);
/* portrait or landscape page dimensions? */
landscape = false; /* portrait: don't swap -x and -y values */
/*
(3) COMMAND LINE OPTIONS.
*/
while ((c = getopt (argc, argv, "H:V:d:e:k:lm:r:t:x:y:")) != -1)
{
switch (c)
{
case 0: /* flags already processed */
break;
case 'H':
ToDimen (" -H", optarg, &hoffr, &hoffu);
break;
case 'V':
ToDimen (" -V", optarg, &voffr, &voffu);
break;
case 'd':
stringcopy (dummy_pk, optarg);
break;
case 'e': /* gt - not very mnemonic, I know */
stringcopy (dummy_tfm, optarg);
break;
case 'k':
kpathsea_debug |= atoi (optarg);
break;
case 'l':
landscape = true;
break;
case 'm':
ToCardinal (" -m", optarg, &mag);
case 'r':
/* gt - resolutions should allow non-integer dpi */
/* - this is because there are such output devices */
/* - for example, metric printers. */
/* Get resolutions, specified in dots per inch */
ToRes (" -r", optarg, &xres, &yres);
break;
case 'v':
stringcopy (vdu, optarg);
break;
case 'x':
ToPosDimen (" -x", optarg, &xr, &xu);
break;
case 'y':
ToPosDimen (" -y", optarg, &yr, &yu);
break;
case '?': /* error message in getopt */
FATAL ("Exiting");
default:
FATAL ("Fatal error in getopt");
} /* switch */
}
argv += optind - 1;
argc -= optind - 1;
if (argc < 2)
{
return 0;
}
/* bad DVIname will be detected upon open in main module */
DVIname = extend_filename (argv[1], "dvi");
/* set h/voffset and paperwd/ht only after resolutions have been decided */
hoffset = DimenPixels (hoffr, hoffu, xres);
voffset = DimenPixels (voffr, voffu, yres);
paperwd = DimenPixels (xr, xu, xres);
paperht = DimenPixels (yr, yu, yres);
if (landscape)
{ /* swap paperwd and paperht */
int temp = paperwd;
paperwd = paperht;
paperht = temp;
}
return 1;
}
/* InitOptions */
/* end options.c */
|