Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
att
GitHub Repository: att/ast
Path: blob/master/src/lib/libtk/generic/tkCanvPs.c
1810 views
1
/*
2
* tkCanvPs.c --
3
*
4
* This module provides Postscript output support for canvases,
5
* including the "postscript" widget command plus a few utility
6
* procedures used for generating Postscript.
7
*
8
* Copyright (c) 1991-1994 The Regents of the University of California.
9
* Copyright (c) 1994-1996 Sun Microsystems, Inc.
10
*
11
* See the file "license.terms" for information on usage and redistribution
12
* of this file, and for a DISCLAIMER OF ALL WARRANTIES.
13
*
14
* SCCS: @(#) tkCanvPs.c 1.52 96/11/19 12:47:09
15
*/
16
17
#include "tkInt.h"
18
#include "tkCanvas.h"
19
20
/*
21
* See tkCanvas.h for key data structures used to implement canvases.
22
*/
23
24
/*
25
* One of the following structures is created to keep track of Postscript
26
* output being generated. It consists mostly of information provided on
27
* the widget command line.
28
*/
29
30
typedef struct TkPostscriptInfo {
31
int x, y, width, height; /* Area to print, in canvas pixel
32
* coordinates. */
33
int x2, y2; /* x+width and y+height. */
34
char *pageXString; /* String value of "-pagex" option or NULL. */
35
char *pageYString; /* String value of "-pagey" option or NULL. */
36
double pageX, pageY; /* Postscript coordinates (in points)
37
* corresponding to pageXString and
38
* pageYString. Don't forget that y-values
39
* grow upwards for Postscript! */
40
char *pageWidthString; /* Printed width of output. */
41
char *pageHeightString; /* Printed height of output. */
42
double scale; /* Scale factor for conversion: each pixel
43
* maps into this many points. */
44
Tk_Anchor pageAnchor; /* How to anchor bbox on Postscript page. */
45
int rotate; /* Non-zero means output should be rotated
46
* on page (landscape mode). */
47
char *fontVar; /* If non-NULL, gives name of global variable
48
* containing font mapping information.
49
* Malloc'ed. */
50
char *colorVar; /* If non-NULL, give name of global variable
51
* containing color mapping information.
52
* Malloc'ed. */
53
char *colorMode; /* Mode for handling colors: "monochrome",
54
* "gray", or "color". Malloc'ed. */
55
int colorLevel; /* Numeric value corresponding to colorMode:
56
* 0 for mono, 1 for gray, 2 for color. */
57
char *fileName; /* Name of file in which to write Postscript;
58
* NULL means return Postscript info as
59
* result. Malloc'ed. */
60
Tcl_Channel chan; /* Open channel corresponding to fileName. */
61
Tcl_HashTable fontTable; /* Hash table containing names of all font
62
* families used in output. The hash table
63
* values are not used. */
64
int prepass; /* Non-zero means that we're currently in
65
* the pre-pass that collects font information,
66
* so the Postscript generated isn't
67
* relevant. */
68
} TkPostscriptInfo;
69
70
/*
71
* The table below provides a template that's used to process arguments
72
* to the canvas "postscript" command and fill in TkPostscriptInfo
73
* structures.
74
*/
75
76
static Tk_ConfigSpec configSpecs[] = {
77
{TK_CONFIG_STRING, "-colormap", (char *) NULL, (char *) NULL,
78
"", Tk_Offset(TkPostscriptInfo, colorVar), 0},
79
{TK_CONFIG_STRING, "-colormode", (char *) NULL, (char *) NULL,
80
"", Tk_Offset(TkPostscriptInfo, colorMode), 0},
81
{TK_CONFIG_STRING, "-file", (char *) NULL, (char *) NULL,
82
"", Tk_Offset(TkPostscriptInfo, fileName), 0},
83
{TK_CONFIG_STRING, "-fontmap", (char *) NULL, (char *) NULL,
84
"", Tk_Offset(TkPostscriptInfo, fontVar), 0},
85
{TK_CONFIG_PIXELS, "-height", (char *) NULL, (char *) NULL,
86
"", Tk_Offset(TkPostscriptInfo, height), 0},
87
{TK_CONFIG_ANCHOR, "-pageanchor", (char *) NULL, (char *) NULL,
88
"", Tk_Offset(TkPostscriptInfo, pageAnchor), 0},
89
{TK_CONFIG_STRING, "-pageheight", (char *) NULL, (char *) NULL,
90
"", Tk_Offset(TkPostscriptInfo, pageHeightString), 0},
91
{TK_CONFIG_STRING, "-pagewidth", (char *) NULL, (char *) NULL,
92
"", Tk_Offset(TkPostscriptInfo, pageWidthString), 0},
93
{TK_CONFIG_STRING, "-pagex", (char *) NULL, (char *) NULL,
94
"", Tk_Offset(TkPostscriptInfo, pageXString), 0},
95
{TK_CONFIG_STRING, "-pagey", (char *) NULL, (char *) NULL,
96
"", Tk_Offset(TkPostscriptInfo, pageYString), 0},
97
{TK_CONFIG_BOOLEAN, "-rotate", (char *) NULL, (char *) NULL,
98
"", Tk_Offset(TkPostscriptInfo, rotate), 0},
99
{TK_CONFIG_PIXELS, "-width", (char *) NULL, (char *) NULL,
100
"", Tk_Offset(TkPostscriptInfo, width), 0},
101
{TK_CONFIG_PIXELS, "-x", (char *) NULL, (char *) NULL,
102
"", Tk_Offset(TkPostscriptInfo, x), 0},
103
{TK_CONFIG_PIXELS, "-y", (char *) NULL, (char *) NULL,
104
"", Tk_Offset(TkPostscriptInfo, y), 0},
105
{TK_CONFIG_END, (char *) NULL, (char *) NULL, (char *) NULL,
106
(char *) NULL, 0, 0}
107
};
108
109
/*
110
* Forward declarations for procedures defined later in this file:
111
*/
112
113
static int GetPostscriptPoints _ANSI_ARGS_((Tcl_Interp *interp,
114
char *string, double *doublePtr));
115
116
/*
117
*--------------------------------------------------------------
118
*
119
* TkCanvPostscriptCmd --
120
*
121
* This procedure is invoked to process the "postscript" options
122
* of the widget command for canvas widgets. See the user
123
* documentation for details on what it does.
124
*
125
* Results:
126
* A standard Tcl result.
127
*
128
* Side effects:
129
* See the user documentation.
130
*
131
*--------------------------------------------------------------
132
*/
133
134
/* ARGSUSED */
135
int
136
TkCanvPostscriptCmd(canvasPtr, interp, argc, argv)
137
TkCanvas *canvasPtr; /* Information about canvas widget. */
138
Tcl_Interp *interp; /* Current interpreter. */
139
int argc; /* Number of arguments. */
140
char **argv; /* Argument strings. Caller has
141
* already parsed this command enough
142
* to know that argv[1] is
143
* "postscript". */
144
{
145
TkPostscriptInfo psInfo, *oldInfoPtr;
146
int result = TCL_ERROR;
147
Tk_Item *itemPtr;
148
#define STRING_LENGTH 400
149
char string[STRING_LENGTH+1], *p;
150
time_t now;
151
#if !(defined(WIN_TCL) || defined(MAC_TCL))
152
struct passwd *pwPtr;
153
#endif /* WIN_TCL || MAC_TCL */
154
size_t length;
155
int deltaX = 0, deltaY = 0; /* Offset of lower-left corner of
156
* area to be marked up, measured
157
* in canvas units from the positioning
158
* point on the page (reflects
159
* anchor position). Initial values
160
* needed only to stop compiler
161
* warnings. */
162
Tcl_HashSearch search;
163
Tcl_HashEntry *hPtr;
164
Tcl_DString buffer;
165
166
/*
167
*----------------------------------------------------------------
168
* Initialize the data structure describing Postscript generation,
169
* then process all the arguments to fill the data structure in.
170
*----------------------------------------------------------------
171
*/
172
173
oldInfoPtr = canvasPtr->psInfoPtr;
174
canvasPtr->psInfoPtr = &psInfo;
175
psInfo.x = canvasPtr->xOrigin;
176
psInfo.y = canvasPtr->yOrigin;
177
psInfo.width = -1;
178
psInfo.height = -1;
179
psInfo.pageXString = NULL;
180
psInfo.pageYString = NULL;
181
psInfo.pageX = 72*4.25;
182
psInfo.pageY = 72*5.5;
183
psInfo.pageWidthString = NULL;
184
psInfo.pageHeightString = NULL;
185
psInfo.scale = 1.0;
186
psInfo.pageAnchor = TK_ANCHOR_CENTER;
187
psInfo.rotate = 0;
188
psInfo.fontVar = NULL;
189
psInfo.colorVar = NULL;
190
psInfo.colorMode = NULL;
191
psInfo.colorLevel = 0;
192
psInfo.fileName = NULL;
193
psInfo.chan = NULL;
194
psInfo.prepass = 0;
195
Tcl_InitHashTable(&psInfo.fontTable, TCL_STRING_KEYS);
196
result = Tk_ConfigureWidget(canvasPtr->interp, canvasPtr->tkwin,
197
configSpecs, argc-2, argv+2, (char *) &psInfo,
198
TK_CONFIG_ARGV_ONLY);
199
if (result != TCL_OK) {
200
goto cleanup;
201
}
202
203
if (psInfo.width == -1) {
204
psInfo.width = Tk_Width(canvasPtr->tkwin);
205
}
206
if (psInfo.height == -1) {
207
psInfo.height = Tk_Height(canvasPtr->tkwin);
208
}
209
psInfo.x2 = psInfo.x + psInfo.width;
210
psInfo.y2 = psInfo.y + psInfo.height;
211
212
if (psInfo.pageXString != NULL) {
213
if (GetPostscriptPoints(canvasPtr->interp, psInfo.pageXString,
214
&psInfo.pageX) != TCL_OK) {
215
goto cleanup;
216
}
217
}
218
if (psInfo.pageYString != NULL) {
219
if (GetPostscriptPoints(canvasPtr->interp, psInfo.pageYString,
220
&psInfo.pageY) != TCL_OK) {
221
goto cleanup;
222
}
223
}
224
if (psInfo.pageWidthString != NULL) {
225
if (GetPostscriptPoints(canvasPtr->interp, psInfo.pageWidthString,
226
&psInfo.scale) != TCL_OK) {
227
goto cleanup;
228
}
229
psInfo.scale /= psInfo.width;
230
} else if (psInfo.pageHeightString != NULL) {
231
if (GetPostscriptPoints(canvasPtr->interp, psInfo.pageHeightString,
232
&psInfo.scale) != TCL_OK) {
233
goto cleanup;
234
}
235
psInfo.scale /= psInfo.height;
236
} else {
237
psInfo.scale = (72.0/25.4)*WidthMMOfScreen(Tk_Screen(canvasPtr->tkwin));
238
psInfo.scale /= WidthOfScreen(Tk_Screen(canvasPtr->tkwin));
239
}
240
switch (psInfo.pageAnchor) {
241
case TK_ANCHOR_NW:
242
case TK_ANCHOR_W:
243
case TK_ANCHOR_SW:
244
deltaX = 0;
245
break;
246
case TK_ANCHOR_N:
247
case TK_ANCHOR_CENTER:
248
case TK_ANCHOR_S:
249
deltaX = -psInfo.width/2;
250
break;
251
case TK_ANCHOR_NE:
252
case TK_ANCHOR_E:
253
case TK_ANCHOR_SE:
254
deltaX = -psInfo.width;
255
break;
256
}
257
switch (psInfo.pageAnchor) {
258
case TK_ANCHOR_NW:
259
case TK_ANCHOR_N:
260
case TK_ANCHOR_NE:
261
deltaY = - psInfo.height;
262
break;
263
case TK_ANCHOR_W:
264
case TK_ANCHOR_CENTER:
265
case TK_ANCHOR_E:
266
deltaY = -psInfo.height/2;
267
break;
268
case TK_ANCHOR_SW:
269
case TK_ANCHOR_S:
270
case TK_ANCHOR_SE:
271
deltaY = 0;
272
break;
273
}
274
275
if (psInfo.colorMode == NULL) {
276
psInfo.colorLevel = 2;
277
} else {
278
length = strlen(psInfo.colorMode);
279
if (strncmp(psInfo.colorMode, "monochrome", length) == 0) {
280
psInfo.colorLevel = 0;
281
} else if (strncmp(psInfo.colorMode, "gray", length) == 0) {
282
psInfo.colorLevel = 1;
283
} else if (strncmp(psInfo.colorMode, "color", length) == 0) {
284
psInfo.colorLevel = 2;
285
} else {
286
Tcl_AppendResult(canvasPtr->interp, "bad color mode \"",
287
psInfo.colorMode, "\": must be monochrome, ",
288
"gray, or color", (char *) NULL);
289
goto cleanup;
290
}
291
}
292
293
if (psInfo.fileName != NULL) {
294
p = Tcl_TranslateFileName(canvasPtr->interp, psInfo.fileName, &buffer);
295
if (p == NULL) {
296
goto cleanup;
297
}
298
psInfo.chan = Tcl_OpenFileChannel(canvasPtr->interp, p, "w", 0666);
299
Tcl_DStringFree(&buffer);
300
if (psInfo.chan == NULL) {
301
goto cleanup;
302
}
303
}
304
305
/*
306
*--------------------------------------------------------
307
* Make a pre-pass over all of the items, generating Postscript
308
* and then throwing it away. The purpose of this pass is just
309
* to collect information about all the fonts in use, so that
310
* we can output font information in the proper form required
311
* by the Document Structuring Conventions.
312
*--------------------------------------------------------
313
*/
314
315
psInfo.prepass = 1;
316
for (itemPtr = canvasPtr->firstItemPtr; itemPtr != NULL;
317
itemPtr = itemPtr->nextPtr) {
318
if ((itemPtr->x1 >= psInfo.x2) || (itemPtr->x2 < psInfo.x)
319
|| (itemPtr->y1 >= psInfo.y2) || (itemPtr->y2 < psInfo.y)) {
320
continue;
321
}
322
if (itemPtr->typePtr->postscriptProc == NULL) {
323
continue;
324
}
325
result = (*itemPtr->typePtr->postscriptProc)(canvasPtr->interp,
326
(Tk_Canvas) canvasPtr, itemPtr, 1);
327
Tcl_ResetResult(canvasPtr->interp);
328
if (result != TCL_OK) {
329
/*
330
* An error just occurred. Just skip out of this loop.
331
* There's no need to report the error now; it can be
332
* reported later (errors can happen later that don't
333
* happen now, so we still have to check for errors later
334
* anyway).
335
*/
336
break;
337
}
338
}
339
psInfo.prepass = 0;
340
341
/*
342
*--------------------------------------------------------
343
* Generate the header and prolog for the Postscript.
344
*--------------------------------------------------------
345
*/
346
347
Tcl_AppendResult(canvasPtr->interp, "%!PS-Adobe-3.0 EPSF-3.0\n",
348
"%%Creator: Tk Canvas Widget\n", (char *) NULL);
349
#if !(defined(WIN_TCL) || defined(MAC_TCL) || 1)
350
pwPtr = getpwuid(getuid());
351
Tcl_AppendResult(canvasPtr->interp, "%%For: ",
352
(pwPtr != NULL) ? pwPtr->pw_gecos : "Unknown", "\n",
353
(char *) NULL);
354
endpwent();
355
#endif /* WIN_TCL || MAC_TCL */
356
Tcl_AppendResult(canvasPtr->interp, "%%Title: Window ",
357
Tk_PathName(canvasPtr->tkwin), "\n", (char *) NULL);
358
time(&now);
359
Tcl_AppendResult(canvasPtr->interp, "%%CreationDate: ",
360
ctime(&now), (char *) NULL);
361
if (!psInfo.rotate) {
362
sprintf(string, "%d %d %d %d",
363
(int) (psInfo.pageX + psInfo.scale*deltaX),
364
(int) (psInfo.pageY + psInfo.scale*deltaY),
365
(int) (psInfo.pageX + psInfo.scale*(deltaX + psInfo.width)
366
+ 1.0),
367
(int) (psInfo.pageY + psInfo.scale*(deltaY + psInfo.height)
368
+ 1.0));
369
} else {
370
sprintf(string, "%d %d %d %d",
371
(int) (psInfo.pageX - psInfo.scale*(deltaY + psInfo.height)),
372
(int) (psInfo.pageY + psInfo.scale*deltaX),
373
(int) (psInfo.pageX - psInfo.scale*deltaY + 1.0),
374
(int) (psInfo.pageY + psInfo.scale*(deltaX + psInfo.width)
375
+ 1.0));
376
}
377
Tcl_AppendResult(canvasPtr->interp, "%%BoundingBox: ", string,
378
"\n", (char *) NULL);
379
Tcl_AppendResult(canvasPtr->interp, "%%Pages: 1\n",
380
"%%DocumentData: Clean7Bit\n", (char *) NULL);
381
Tcl_AppendResult(canvasPtr->interp, "%%Orientation: ",
382
psInfo.rotate ? "Landscape\n" : "Portrait\n", (char *) NULL);
383
p = "%%DocumentNeededResources: font ";
384
for (hPtr = Tcl_FirstHashEntry(&psInfo.fontTable, &search);
385
hPtr != NULL; hPtr = Tcl_NextHashEntry(&search)) {
386
Tcl_AppendResult(canvasPtr->interp, p,
387
Tcl_GetHashKey(&psInfo.fontTable, hPtr),
388
"\n", (char *) NULL);
389
p = "%%+ font ";
390
}
391
Tcl_AppendResult(canvasPtr->interp, "%%EndComments\n\n", (char *) NULL);
392
393
/*
394
* Read a standard prolog file in a native way and insert it into
395
* the Postscript.
396
*/
397
398
if (TkGetNativeProlog(canvasPtr->interp) != TCL_OK) {
399
goto cleanup;
400
}
401
if (psInfo.chan != NULL) {
402
Tcl_Write(psInfo.chan, canvasPtr->interp->result, -1);
403
Tcl_ResetResult(canvasPtr->interp);
404
}
405
406
/*
407
*-----------------------------------------------------------
408
* Document setup: set the color level and include fonts.
409
*-----------------------------------------------------------
410
*/
411
412
sprintf(string, "/CL %d def\n", psInfo.colorLevel);
413
Tcl_AppendResult(canvasPtr->interp, "%%BeginSetup\n", string,
414
(char *) NULL);
415
for (hPtr = Tcl_FirstHashEntry(&psInfo.fontTable, &search);
416
hPtr != NULL; hPtr = Tcl_NextHashEntry(&search)) {
417
Tcl_AppendResult(canvasPtr->interp, "%%IncludeResource: font ",
418
Tcl_GetHashKey(&psInfo.fontTable, hPtr), "\n", (char *) NULL);
419
}
420
Tcl_AppendResult(canvasPtr->interp, "%%EndSetup\n\n", (char *) NULL);
421
422
/*
423
*-----------------------------------------------------------
424
* Page setup: move to page positioning point, rotate if
425
* needed, set scale factor, offset for proper anchor position,
426
* and set clip region.
427
*-----------------------------------------------------------
428
*/
429
430
Tcl_AppendResult(canvasPtr->interp, "%%Page: 1 1\n", "save\n",
431
(char *) NULL);
432
sprintf(string, "%.1f %.1f translate\n", psInfo.pageX, psInfo.pageY);
433
Tcl_AppendResult(canvasPtr->interp, string, (char *) NULL);
434
if (psInfo.rotate) {
435
Tcl_AppendResult(canvasPtr->interp, "90 rotate\n", (char *) NULL);
436
}
437
sprintf(string, "%.4g %.4g scale\n", psInfo.scale, psInfo.scale);
438
Tcl_AppendResult(canvasPtr->interp, string, (char *) NULL);
439
sprintf(string, "%d %d translate\n", deltaX - psInfo.x, deltaY);
440
Tcl_AppendResult(canvasPtr->interp, string, (char *) NULL);
441
sprintf(string, "%d %.15g moveto %d %.15g lineto %d %.15g lineto %d %.15g",
442
psInfo.x, Tk_CanvasPsY((Tk_Canvas) canvasPtr, (double) psInfo.y),
443
psInfo.x2, Tk_CanvasPsY((Tk_Canvas) canvasPtr, (double) psInfo.y),
444
psInfo.x2, Tk_CanvasPsY((Tk_Canvas) canvasPtr, (double) psInfo.y2),
445
psInfo.x, Tk_CanvasPsY((Tk_Canvas) canvasPtr, (double) psInfo.y2));
446
Tcl_AppendResult(canvasPtr->interp, string,
447
" lineto closepath clip newpath\n", (char *) NULL);
448
if (psInfo.chan != NULL) {
449
Tcl_Write(psInfo.chan, canvasPtr->interp->result, -1);
450
Tcl_ResetResult(canvasPtr->interp);
451
}
452
453
/*
454
*---------------------------------------------------------------------
455
* Iterate through all the items, having each relevant one draw itself.
456
* Quit if any of the items returns an error.
457
*---------------------------------------------------------------------
458
*/
459
460
result = TCL_OK;
461
for (itemPtr = canvasPtr->firstItemPtr; itemPtr != NULL;
462
itemPtr = itemPtr->nextPtr) {
463
if ((itemPtr->x1 >= psInfo.x2) || (itemPtr->x2 < psInfo.x)
464
|| (itemPtr->y1 >= psInfo.y2) || (itemPtr->y2 < psInfo.y)) {
465
continue;
466
}
467
if (itemPtr->typePtr->postscriptProc == NULL) {
468
continue;
469
}
470
Tcl_AppendResult(canvasPtr->interp, "gsave\n", (char *) NULL);
471
result = (*itemPtr->typePtr->postscriptProc)(canvasPtr->interp,
472
(Tk_Canvas) canvasPtr, itemPtr, 0);
473
if (result != TCL_OK) {
474
char msg[100];
475
476
sprintf(msg, "\n (generating Postscript for item %d)",
477
itemPtr->id);
478
Tcl_AddErrorInfo(canvasPtr->interp, msg);
479
goto cleanup;
480
}
481
Tcl_AppendResult(canvasPtr->interp, "grestore\n", (char *) NULL);
482
if (psInfo.chan != NULL) {
483
Tcl_Write(psInfo.chan, canvasPtr->interp->result, -1);
484
Tcl_ResetResult(canvasPtr->interp);
485
}
486
}
487
488
/*
489
*---------------------------------------------------------------------
490
* Output page-end information, such as commands to print the page
491
* and document trailer stuff.
492
*---------------------------------------------------------------------
493
*/
494
495
Tcl_AppendResult(canvasPtr->interp, "restore showpage\n\n",
496
"%%Trailer\nend\n%%EOF\n", (char *) NULL);
497
if (psInfo.chan != NULL) {
498
Tcl_Write(psInfo.chan, canvasPtr->interp->result, -1);
499
Tcl_ResetResult(canvasPtr->interp);
500
}
501
502
/*
503
* Clean up psInfo to release malloc'ed stuff.
504
*/
505
506
cleanup:
507
if (psInfo.pageXString != NULL) {
508
ckfree(psInfo.pageXString);
509
}
510
if (psInfo.pageYString != NULL) {
511
ckfree(psInfo.pageYString);
512
}
513
if (psInfo.pageWidthString != NULL) {
514
ckfree(psInfo.pageWidthString);
515
}
516
if (psInfo.pageHeightString != NULL) {
517
ckfree(psInfo.pageHeightString);
518
}
519
if (psInfo.fontVar != NULL) {
520
ckfree(psInfo.fontVar);
521
}
522
if (psInfo.colorVar != NULL) {
523
ckfree(psInfo.colorVar);
524
}
525
if (psInfo.colorMode != NULL) {
526
ckfree(psInfo.colorMode);
527
}
528
if (psInfo.fileName != NULL) {
529
ckfree(psInfo.fileName);
530
}
531
if (psInfo.chan != NULL) {
532
Tcl_Close(canvasPtr->interp, psInfo.chan);
533
}
534
Tcl_DeleteHashTable(&psInfo.fontTable);
535
canvasPtr->psInfoPtr = oldInfoPtr;
536
return result;
537
}
538
539
/*
540
*--------------------------------------------------------------
541
*
542
* Tk_CanvasPsColor --
543
*
544
* This procedure is called by individual canvas items when
545
* they want to set a color value for output. Given information
546
* about an X color, this procedure will generate Postscript
547
* commands to set up an appropriate color in Postscript.
548
*
549
* Results:
550
* Returns a standard Tcl return value. If an error occurs
551
* then an error message will be left in interp->result.
552
* If no error occurs, then additional Postscript will be
553
* appended to interp->result.
554
*
555
* Side effects:
556
* None.
557
*
558
*--------------------------------------------------------------
559
*/
560
561
int
562
Tk_CanvasPsColor(interp, canvas, colorPtr)
563
Tcl_Interp *interp; /* Interpreter for returning Postscript
564
* or error message. */
565
Tk_Canvas canvas; /* Information about canvas. */
566
XColor *colorPtr; /* Information about color. */
567
{
568
TkCanvas *canvasPtr = (TkCanvas *) canvas;
569
TkPostscriptInfo *psInfoPtr = canvasPtr->psInfoPtr;
570
int tmp;
571
double red, green, blue;
572
char string[200];
573
574
if (psInfoPtr->prepass) {
575
return TCL_OK;
576
}
577
578
/*
579
* If there is a color map defined, then look up the color's name
580
* in the map and use the Postscript commands found there, if there
581
* are any.
582
*/
583
584
if (psInfoPtr->colorVar != NULL) {
585
char *cmdString;
586
587
cmdString = Tcl_GetVar2(interp, psInfoPtr->colorVar,
588
Tk_NameOfColor(colorPtr), 0);
589
if (cmdString != NULL) {
590
Tcl_AppendResult(interp, cmdString, "\n", (char *) NULL);
591
return TCL_OK;
592
}
593
}
594
595
/*
596
* No color map entry for this color. Grab the color's intensities
597
* and output Postscript commands for them. Special note: X uses
598
* a range of 0-65535 for intensities, but most displays only use
599
* a range of 0-255, which maps to (0, 256, 512, ... 65280) in the
600
* X scale. This means that there's no way to get perfect white,
601
* since the highest intensity is only 65280 out of 65535. To
602
* work around this problem, rescale the X intensity to a 0-255
603
* scale and use that as the basis for the Postscript colors. This
604
* scheme still won't work if the display only uses 4 bits per color,
605
* but most diplays use at least 8 bits.
606
*/
607
608
tmp = colorPtr->red;
609
red = ((double) (tmp >> 8))/255.0;
610
tmp = colorPtr->green;
611
green = ((double) (tmp >> 8))/255.0;
612
tmp = colorPtr->blue;
613
blue = ((double) (tmp >> 8))/255.0;
614
sprintf(string, "%.3f %.3f %.3f setrgbcolor AdjustColor\n",
615
red, green, blue);
616
Tcl_AppendResult(interp, string, (char *) NULL);
617
return TCL_OK;
618
}
619
620
/*
621
*--------------------------------------------------------------
622
*
623
* Tk_CanvasPsFont --
624
*
625
* This procedure is called by individual canvas items when
626
* they want to output text. Given information about an X
627
* font, this procedure will generate Postscript commands
628
* to set up an appropriate font in Postscript.
629
*
630
* Results:
631
* Returns a standard Tcl return value. If an error occurs
632
* then an error message will be left in interp->result.
633
* If no error occurs, then additional Postscript will be
634
* appended to the interp->result.
635
*
636
* Side effects:
637
* The Postscript font name is entered into psInfoPtr->fontTable
638
* if it wasn't already there.
639
*
640
*--------------------------------------------------------------
641
*/
642
643
int
644
Tk_CanvasPsFont(interp, canvas, fontStructPtr)
645
Tcl_Interp *interp; /* Interpreter for returning Postscript
646
* or error message. */
647
Tk_Canvas canvas; /* Information about canvas. */
648
XFontStruct *fontStructPtr; /* Information about font in which text
649
* is to be printed. */
650
{
651
TkCanvas *canvasPtr = (TkCanvas *) canvas;
652
TkPostscriptInfo *psInfoPtr = canvasPtr->psInfoPtr;
653
char *name, *end, *weightString, *slantString;
654
#define TOTAL_FIELDS 8
655
#define FAMILY_FIELD 1
656
#define WEIGHT_FIELD 2
657
#define SLANT_FIELD 3
658
#define SIZE_FIELD 7
659
char *fieldPtrs[TOTAL_FIELDS];
660
#define MAX_NAME_SIZE 100
661
char fontName[MAX_NAME_SIZE+50], pointString[20];
662
int i, c, weightSize, nameSize, points;
663
char *p;
664
665
name = Tk_NameOfFontStruct(fontStructPtr);
666
667
/*
668
* First, look up the font's name in the font map, if there is one.
669
* If there is an entry for this font, it consists of a list
670
* containing font name and size. Use this information.
671
*/
672
673
if (psInfoPtr->fontVar != NULL) {
674
char *list, **argv;
675
int argc;
676
double size;
677
678
list = Tcl_GetVar2(interp, psInfoPtr->fontVar,
679
name, 0);
680
if (list != NULL) {
681
if (Tcl_SplitList(interp, list, &argc, &argv) != TCL_OK) {
682
badMapEntry:
683
Tcl_ResetResult(interp);
684
Tcl_AppendResult(interp, "bad font map entry for \"", name,
685
"\": \"", list, "\"", (char *) NULL);
686
return TCL_ERROR;
687
}
688
if (argc != 2) {
689
goto badMapEntry;
690
}
691
size = strtod(argv[1], &end);
692
if ((size <= 0) || (*end != 0)) {
693
goto badMapEntry;
694
}
695
sprintf(pointString, "%.15g", size);
696
Tcl_AppendResult(interp, "/", argv[0], " findfont ",
697
pointString, " scalefont ", (char *) NULL);
698
if (strncasecmp(argv[0], "Symbol", 7) != 0) {
699
Tcl_AppendResult(interp, "ISOEncode ", (char *) NULL);
700
}
701
Tcl_AppendResult(interp, "setfont\n", (char *) NULL);
702
Tcl_CreateHashEntry(&psInfoPtr->fontTable, argv[0], &i);
703
ckfree((char *) argv);
704
return TCL_OK;
705
}
706
}
707
708
/*
709
* Not in the font map. Try to parse the name to get four fields:
710
* family name, weight, slant, and point size. To do this, split the
711
* font name up into fields, storing pointers to the first character
712
* of each field in fieldPtrs.
713
*/
714
715
if (name[0] != '-') {
716
goto error;
717
}
718
for (p = name+1, i = 0; i < TOTAL_FIELDS; i++) {
719
fieldPtrs[i] = p;
720
while (*p != '-') {
721
if (*p == 0) {
722
goto error;
723
}
724
p++;
725
}
726
p++;
727
}
728
729
/*
730
* Use the information from the X font name to make a guess at a
731
* Postscript font name of the form "<family>-<weight><slant>" where
732
* <weight> and <slant> may be omitted and if both are omitted then
733
* the dash is also omitted. Postscript is very picky about font names,
734
* so there are several heuristics in the code below (e.g. don't
735
* include a "Roman" slant except for "Times" font, and make sure
736
* that the first letter of each field is capitalized but no other
737
* letters are in caps).
738
*/
739
740
nameSize = fieldPtrs[FAMILY_FIELD+1] - 1 - fieldPtrs[FAMILY_FIELD];
741
if ((nameSize == 0) || (nameSize > MAX_NAME_SIZE)) {
742
goto error;
743
}
744
strncpy(fontName, fieldPtrs[FAMILY_FIELD], (size_t) nameSize);
745
if (islower(UCHAR(fontName[0]))) {
746
fontName[0] = toupper(UCHAR(fontName[0]));
747
}
748
for (p = fontName+1, i = nameSize-1; i > 0; p++, i--) {
749
if (isupper(UCHAR(*p))) {
750
*p = tolower(UCHAR(*p));
751
}
752
}
753
*p = 0;
754
weightSize = fieldPtrs[WEIGHT_FIELD+1] - 1 - fieldPtrs[WEIGHT_FIELD];
755
if (weightSize == 0) {
756
goto error;
757
}
758
if (strncasecmp(fieldPtrs[WEIGHT_FIELD], "medium",
759
(size_t) weightSize) == 0) {
760
weightString = "";
761
} else if (strncasecmp(fieldPtrs[WEIGHT_FIELD], "bold",
762
(size_t) weightSize) == 0) {
763
weightString = "Bold";
764
} else {
765
goto error;
766
}
767
if (fieldPtrs[SLANT_FIELD+1] != (fieldPtrs[SLANT_FIELD] + 2)) {
768
goto error;
769
}
770
c = fieldPtrs[SLANT_FIELD][0];
771
if ((c == 'r') || (c == 'R')) {
772
slantString = "";
773
if ((weightString[0] == 0) && (nameSize == 5)
774
&& (strncmp(fontName, "Times", 5) == 0)) {
775
slantString = "Roman";
776
}
777
} else if ((c == 'i') || (c == 'I')) {
778
slantString = "Italic";
779
} else if ((c == 'o') || (c == 'O')) {
780
slantString = "Oblique";
781
} else {
782
goto error;
783
}
784
if ((weightString[0] != 0) || (slantString[0] != 0)) {
785
sprintf(p, "-%s%s", weightString, slantString);
786
}
787
points = strtoul(fieldPtrs[SIZE_FIELD], &end, 0);
788
if (points == 0) {
789
goto error;
790
}
791
sprintf(pointString, "%.15g", ((double) points)/10.0);
792
Tcl_AppendResult(interp, "/", fontName, " findfont ",
793
pointString, " scalefont ", (char *) NULL);
794
if (strcmp(fontName, "Symbol") != 0) {
795
Tcl_AppendResult(interp, "ISOEncode ", (char *) NULL);
796
}
797
Tcl_AppendResult(interp, "setfont\n", (char *) NULL);
798
Tcl_CreateHashEntry(&psInfoPtr->fontTable, fontName, &i);
799
return TCL_OK;
800
801
error:
802
Tcl_ResetResult(interp);
803
Tcl_AppendResult(interp, "couldn't translate font name \"",
804
name, "\" to Postscript", (char *) NULL);
805
return TCL_ERROR;
806
}
807
808
/*
809
*--------------------------------------------------------------
810
*
811
* Tk_CanvasPsBitmap --
812
*
813
* This procedure is called to output the contents of a
814
* sub-region of a bitmap in proper image data format for
815
* Postscript (i.e. data between angle brackets, one bit
816
* per pixel).
817
*
818
* Results:
819
* Returns a standard Tcl return value. If an error occurs
820
* then an error message will be left in interp->result.
821
* If no error occurs, then additional Postscript will be
822
* appended to interp->result.
823
*
824
* Side effects:
825
* None.
826
*
827
*--------------------------------------------------------------
828
*/
829
830
int
831
Tk_CanvasPsBitmap(interp, canvas, bitmap, startX, startY, width, height)
832
Tcl_Interp *interp; /* Interpreter for returning Postscript
833
* or error message. */
834
Tk_Canvas canvas; /* Information about canvas. */
835
Pixmap bitmap; /* Bitmap for which to generate
836
* Postscript. */
837
int startX, startY; /* Coordinates of upper-left corner
838
* of rectangular region to output. */
839
int width, height; /* Height of rectangular region. */
840
{
841
TkCanvas *canvasPtr = (TkCanvas *) canvas;
842
TkPostscriptInfo *psInfoPtr = canvasPtr->psInfoPtr;
843
XImage *imagePtr;
844
int charsInLine, x, y, lastX, lastY, value, mask;
845
unsigned int totalWidth, totalHeight;
846
char string[100];
847
Window dummyRoot;
848
int dummyX, dummyY;
849
unsigned dummyBorderwidth, dummyDepth;
850
851
if (psInfoPtr->prepass) {
852
return TCL_OK;
853
}
854
855
/*
856
* The following call should probably be a call to Tk_SizeOfBitmap
857
* instead, but it seems that we are occasionally invoked by custom
858
* item types that create their own bitmaps without registering them
859
* with Tk. XGetGeometry is a bit slower than Tk_SizeOfBitmap, but
860
* it shouldn't matter here.
861
*/
862
863
XGetGeometry(Tk_Display(Tk_CanvasTkwin(canvas)), bitmap, &dummyRoot,
864
(int *) &dummyX, (int *) &dummyY, (unsigned int *) &totalWidth,
865
(unsigned int *) &totalHeight, &dummyBorderwidth, &dummyDepth);
866
imagePtr = XGetImage(Tk_Display(canvasPtr->tkwin), bitmap, 0, 0,
867
totalWidth, totalHeight, 1, XYPixmap);
868
Tcl_AppendResult(interp, "<", (char *) NULL);
869
mask = 0x80;
870
value = 0;
871
charsInLine = 0;
872
lastX = startX + width - 1;
873
lastY = startY + height - 1;
874
for (y = lastY; y >= startY; y--) {
875
for (x = startX; x <= lastX; x++) {
876
if (XGetPixel(imagePtr, x, y)) {
877
value |= mask;
878
}
879
mask >>= 1;
880
if (mask == 0) {
881
sprintf(string, "%02x", value);
882
Tcl_AppendResult(interp, string, (char *) NULL);
883
mask = 0x80;
884
value = 0;
885
charsInLine += 2;
886
if (charsInLine >= 60) {
887
Tcl_AppendResult(interp, "\n", (char *) NULL);
888
charsInLine = 0;
889
}
890
}
891
}
892
if (mask != 0x80) {
893
sprintf(string, "%02x", value);
894
Tcl_AppendResult(interp, string, (char *) NULL);
895
mask = 0x80;
896
value = 0;
897
charsInLine += 2;
898
}
899
}
900
Tcl_AppendResult(interp, ">", (char *) NULL);
901
XDestroyImage(imagePtr);
902
return TCL_OK;
903
}
904
905
/*
906
*--------------------------------------------------------------
907
*
908
* Tk_CanvasPsStipple --
909
*
910
* This procedure is called by individual canvas items when
911
* they have created a path that they'd like to be filled with
912
* a stipple pattern. Given information about an X bitmap,
913
* this procedure will generate Postscript commands to fill
914
* the current clip region using a stipple pattern defined by the
915
* bitmap.
916
*
917
* Results:
918
* Returns a standard Tcl return value. If an error occurs
919
* then an error message will be left in interp->result.
920
* If no error occurs, then additional Postscript will be
921
* appended to interp->result.
922
*
923
* Side effects:
924
* None.
925
*
926
*--------------------------------------------------------------
927
*/
928
929
int
930
Tk_CanvasPsStipple(interp, canvas, bitmap)
931
Tcl_Interp *interp; /* Interpreter for returning Postscript
932
* or error message. */
933
Tk_Canvas canvas; /* Information about canvas. */
934
Pixmap bitmap; /* Bitmap to use for stippling. */
935
{
936
TkCanvas *canvasPtr = (TkCanvas *) canvas;
937
TkPostscriptInfo *psInfoPtr = canvasPtr->psInfoPtr;
938
int width, height;
939
char string[100];
940
Window dummyRoot;
941
int dummyX, dummyY;
942
unsigned dummyBorderwidth, dummyDepth;
943
944
if (psInfoPtr->prepass) {
945
return TCL_OK;
946
}
947
948
/*
949
* The following call should probably be a call to Tk_SizeOfBitmap
950
* instead, but it seems that we are occasionally invoked by custom
951
* item types that create their own bitmaps without registering them
952
* with Tk. XGetGeometry is a bit slower than Tk_SizeOfBitmap, but
953
* it shouldn't matter here.
954
*/
955
956
XGetGeometry(Tk_Display(Tk_CanvasTkwin(canvas)), bitmap, &dummyRoot,
957
(int *) &dummyX, (int *) &dummyY, (unsigned *) &width,
958
(unsigned *) &height, &dummyBorderwidth, &dummyDepth);
959
sprintf(string, "%d %d ", width, height);
960
Tcl_AppendResult(interp, string, (char *) NULL);
961
if (Tk_CanvasPsBitmap(interp, (Tk_Canvas) canvasPtr, bitmap, 0, 0,
962
width, height) != TCL_OK) {
963
return TCL_ERROR;
964
}
965
Tcl_AppendResult(interp, " StippleFill\n", (char *) NULL);
966
return TCL_OK;
967
}
968
969
/*
970
*--------------------------------------------------------------
971
*
972
* Tk_CanvasPsY --
973
*
974
* Given a y-coordinate in canvas coordinates, this procedure
975
* returns a y-coordinate to use for Postscript output.
976
*
977
* Results:
978
* Returns the Postscript coordinate that corresponds to
979
* "y".
980
*
981
* Side effects:
982
* None.
983
*
984
*--------------------------------------------------------------
985
*/
986
987
double
988
Tk_CanvasPsY(canvas, y)
989
Tk_Canvas canvas; /* Token for canvas on whose behalf
990
* Postscript is being generated. */
991
double y; /* Y-coordinate in canvas coords. */
992
{
993
TkPostscriptInfo *psInfoPtr = ((TkCanvas *) canvas)->psInfoPtr;
994
995
return psInfoPtr->y2 - y;
996
}
997
998
/*
999
*--------------------------------------------------------------
1000
*
1001
* Tk_CanvasPsPath --
1002
*
1003
* Given an array of points for a path, generate Postscript
1004
* commands to create the path.
1005
*
1006
* Results:
1007
* Postscript commands get appended to what's in interp->result.
1008
*
1009
* Side effects:
1010
* None.
1011
*
1012
*--------------------------------------------------------------
1013
*/
1014
1015
void
1016
Tk_CanvasPsPath(interp, canvas, coordPtr, numPoints)
1017
Tcl_Interp *interp; /* Put generated Postscript in this
1018
* interpreter's result field. */
1019
Tk_Canvas canvas; /* Canvas on whose behalf Postscript
1020
* is being generated. */
1021
double *coordPtr; /* Pointer to first in array of
1022
* 2*numPoints coordinates giving
1023
* points for path. */
1024
int numPoints; /* Number of points at *coordPtr. */
1025
{
1026
TkPostscriptInfo *psInfoPtr = ((TkCanvas *) canvas)->psInfoPtr;
1027
char buffer[200];
1028
1029
if (psInfoPtr->prepass) {
1030
return;
1031
}
1032
sprintf(buffer, "%.15g %.15g moveto\n", coordPtr[0],
1033
Tk_CanvasPsY(canvas, coordPtr[1]));
1034
Tcl_AppendResult(interp, buffer, (char *) NULL);
1035
for (numPoints--, coordPtr += 2; numPoints > 0;
1036
numPoints--, coordPtr += 2) {
1037
sprintf(buffer, "%.15g %.15g lineto\n", coordPtr[0],
1038
Tk_CanvasPsY(canvas, coordPtr[1]));
1039
Tcl_AppendResult(interp, buffer, (char *) NULL);
1040
}
1041
}
1042
1043
/*
1044
*--------------------------------------------------------------
1045
*
1046
* GetPostscriptPoints --
1047
*
1048
* Given a string, returns the number of Postscript points
1049
* corresponding to that string.
1050
*
1051
* Results:
1052
* The return value is a standard Tcl return result. If
1053
* TCL_OK is returned, then everything went well and the
1054
* screen distance is stored at *doublePtr; otherwise
1055
* TCL_ERROR is returned and an error message is left in
1056
* interp->result.
1057
*
1058
* Side effects:
1059
* None.
1060
*
1061
*--------------------------------------------------------------
1062
*/
1063
1064
static int
1065
GetPostscriptPoints(interp, string, doublePtr)
1066
Tcl_Interp *interp; /* Use this for error reporting. */
1067
char *string; /* String describing a screen distance. */
1068
double *doublePtr; /* Place to store converted result. */
1069
{
1070
char *end;
1071
double d;
1072
1073
d = strtod(string, &end);
1074
if (end == string) {
1075
error:
1076
Tcl_AppendResult(interp, "bad distance \"", string,
1077
"\"", (char *) NULL);
1078
return TCL_ERROR;
1079
}
1080
while ((*end != '\0') && isspace(UCHAR(*end))) {
1081
end++;
1082
}
1083
switch (*end) {
1084
case 'c':
1085
d *= 72.0/2.54;
1086
end++;
1087
break;
1088
case 'i':
1089
d *= 72.0;
1090
end++;
1091
break;
1092
case 'm':
1093
d *= 72.0/25.4;
1094
end++;
1095
break;
1096
case 0:
1097
break;
1098
case 'p':
1099
end++;
1100
break;
1101
default:
1102
goto error;
1103
}
1104
while ((*end != '\0') && isspace(UCHAR(*end))) {
1105
end++;
1106
}
1107
if (*end != 0) {
1108
goto error;
1109
}
1110
*doublePtr = d;
1111
return TCL_OK;
1112
}
1113
1114
/*
1115
*--------------------------------------------------------------
1116
*
1117
* TkGetProlog --
1118
*
1119
* Locate and load the postscript prolog.
1120
*
1121
* Results:
1122
* A standard Tcl Result. If everything is OK the prolog
1123
* will be located in the result string of the interpreter.
1124
*
1125
* Side effects:
1126
* None.
1127
*
1128
*--------------------------------------------------------------
1129
*/
1130
1131
int
1132
TkGetProlog(interp)
1133
Tcl_Interp *interp; /* Places the prolog in the result. */
1134
{
1135
char *libDir;
1136
Tcl_Channel chan;
1137
Tcl_DString buffer, buffer2;
1138
char *prologPathParts[2];
1139
int bufferSize;
1140
char *prologBuffer;
1141
1142
libDir = Tcl_GetVar(interp, "tk_library", TCL_GLOBAL_ONLY);
1143
if (libDir == NULL) {
1144
Tcl_ResetResult(interp);
1145
Tcl_AppendResult(interp, "couldn't find library directory: ",
1146
"tk_library variable doesn't exist", (char *) NULL);
1147
return TCL_ERROR;
1148
}
1149
Tcl_TranslateFileName(interp, libDir, &buffer);
1150
prologPathParts[0] = buffer.string;
1151
prologPathParts[1] = "prolog.ps";
1152
Tcl_DStringInit(&buffer2);
1153
Tcl_JoinPath(2, prologPathParts, &buffer2);
1154
Tcl_DStringFree(&buffer);
1155
1156
/*
1157
* Compute size of file by seeking to the end of the file. This will
1158
* overallocate if we are performing CRLF translation.
1159
*/
1160
1161
chan = Tcl_OpenFileChannel(interp, buffer2.string, "r", 0);
1162
if (chan == NULL) {
1163
Tcl_DStringFree(&buffer2);
1164
return TCL_ERROR;
1165
}
1166
bufferSize = Tcl_Seek(chan, 0L, SEEK_END);
1167
(void) Tcl_Seek(chan, 0L, SEEK_SET);
1168
if (bufferSize < 0) {
1169
Tcl_AppendResult(interp, "error seeking to end of file \"",
1170
buffer2.string, "\":", Tcl_PosixError(interp), (char *) NULL);
1171
Tcl_Close(NULL, chan);
1172
Tcl_DStringFree(&buffer2);
1173
return TCL_ERROR;
1174
1175
}
1176
prologBuffer = (char *) ckalloc((unsigned) bufferSize+1);
1177
bufferSize = Tcl_Read(chan, prologBuffer, bufferSize);
1178
Tcl_Close(NULL, chan);
1179
if (bufferSize < 0) {
1180
Tcl_AppendResult(interp, "error reading file \"", buffer2.string,
1181
"\":", Tcl_PosixError(interp), (char *) NULL);
1182
Tcl_DStringFree(&buffer2);
1183
return TCL_ERROR;
1184
}
1185
Tcl_DStringFree(&buffer2);
1186
prologBuffer[bufferSize] = 0;
1187
Tcl_AppendResult(interp, prologBuffer, (char *) NULL);
1188
ckfree(prologBuffer);
1189
1190
return TCL_OK;
1191
}
1192
1193