Debian Codegraph @

pTk/mTk/generic/tkCanvPs.c

raw symbols SCIP
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-1997 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 * RCS: @(#) $Id: tkCanvPs.c,v 1.13 2003/02/18 21:54:18 hobbs Exp $
15 */
16 #include "tkInt.h"
17 #include "tkCanvas.h"
18
19 /*
20 * See tkCanvas.h for key data structures used to implement canvases.
21 */
22
23 /*
24 * The following definition is used in generating postscript for images
25 * and windows.
26 */
27
28 typedef struct TkColormapData { /* Hold color information for a window */
29 int separated; /* Whether to use separate color bands */
30 int color; /* Whether window is color or black/white */
31 int ncolors; /* Number of color values stored */
32 XColor *colors; /* Pixel value -> RGB mappings */
33 int red_mask, green_mask, blue_mask; /* Masks and shifts for each */
34 int red_shift, green_shift, blue_shift; /* color band */
35 } TkColormapData;
36
37 /*
38 * One of the following structures is created to keep track of Postscript
39 * output being generated. It consists mostly of information provided on
40 * the widget command line.
41 */
42
43 typedef struct TkPostscriptInfo {
44 int x, y, width, height; /* Area to print, in canvas pixel
45 * coordinates. */
46 int x2, y2; /* x+width and y+height. */
47 char *pageXString; /* String value of "-pagex" option or NULL. */
48 char *pageYString; /* String value of "-pagey" option or NULL. */
49 double pageX, pageY; /* Postscript coordinates (in points)
50 * corresponding to pageXString and
51 * pageYString. Don't forget that y-values
52 * grow upwards for Postscript! */
53 char *pageWidthString; /* Printed width of output. */
54 char *pageHeightString; /* Printed height of output. */
55 double scale; /* Scale factor for conversion: each pixel
56 * maps into this many points. */
57 Tk_Anchor pageAnchor; /* How to anchor bbox on Postscript page. */
58 int rotate; /* Non-zero means output should be rotated
59 * on page (landscape mode). */
60 Var fontVar; /* If non-NULL, gives name of global variable
61 * containing font mapping information.
62 * Malloc'ed. */
63 Var colorVar; /* If non-NULL, give name of global variable
64 * containing color mapping information.
65 * Malloc'ed. */
66 char *colorMode; /* Mode for handling colors: "monochrome",
67 * "gray", or "color". Malloc'ed. */
68 int colorLevel; /* Numeric value corresponding to colorMode:
69 * 0 for mono, 1 for gray, 2 for color. */
70 char *fileName; /* Name of file in which to write Postscript;
71 * NULL means return Postscript info as
72 * result. Malloc'ed. */
73 char *channelName; /* If -channel is specified, the name of
74 * the channel to use. */
75 Tcl_Channel chan; /* Open channel corresponding to fileName. */
76 Tcl_HashTable fontTable; /* Hash table containing names of all font
77 * families used in output. The hash table
78 * values are not used. */
79 int prepass; /* Non-zero means that we're currently in
80 * the pre-pass that collects font information,
81 * so the Postscript generated isn't
82 * relevant. */
83 int prolog; /* Non-zero means output should contain
84 the file prolog.ps in the header. */
85 } TkPostscriptInfo;
86
87 /*
88 * The table below provides a template that's used to process arguments
89 * to the canvas "postscript" command and fill in TkPostscriptInfo
90 * structures.
91 */
92
93 static Tk_ConfigSpec configSpecs[] = {
94 {TK_CONFIG_HASHVAR, "-colormap", (char *) NULL, (char *) NULL,
95 "", Tk_Offset(TkPostscriptInfo, colorVar), 0},
96 {TK_CONFIG_STRING, "-colormode", (char *) NULL, (char *) NULL,
97 "", Tk_Offset(TkPostscriptInfo, colorMode), 0},
98 {TK_CONFIG_STRING, "-file", (char *) NULL, (char *) NULL,
99 "", Tk_Offset(TkPostscriptInfo, fileName), 0},
100 {TK_CONFIG_STRING, "-channel", (char *) NULL, (char *) NULL,
101 "", Tk_Offset(TkPostscriptInfo, channelName), 0},
102 {TK_CONFIG_HASHVAR, "-fontmap", (char *) NULL, (char *) NULL,
103 "", Tk_Offset(TkPostscriptInfo, fontVar), 0},
104 {TK_CONFIG_PIXELS, "-height", (char *) NULL, (char *) NULL,
105 "", Tk_Offset(TkPostscriptInfo, height), 0},
106 {TK_CONFIG_ANCHOR, "-pageanchor", (char *) NULL, (char *) NULL,
107 "", Tk_Offset(TkPostscriptInfo, pageAnchor), 0},
108 {TK_CONFIG_STRING, "-pageheight", (char *) NULL, (char *) NULL,
109 "", Tk_Offset(TkPostscriptInfo, pageHeightString), 0},
110 {TK_CONFIG_STRING, "-pagewidth", (char *) NULL, (char *) NULL,
111 "", Tk_Offset(TkPostscriptInfo, pageWidthString), 0},
112 {TK_CONFIG_STRING, "-pagex", (char *) NULL, (char *) NULL,
113 "", Tk_Offset(TkPostscriptInfo, pageXString), 0},
114 {TK_CONFIG_STRING, "-pagey", (char *) NULL, (char *) NULL,
115 "", Tk_Offset(TkPostscriptInfo, pageYString), 0},
116 {TK_CONFIG_BOOLEAN, "-prolog", (char *) NULL, (char *) NULL,
117 "", Tk_Offset(TkPostscriptInfo, prolog), 0},
118 {TK_CONFIG_BOOLEAN, "-rotate", (char *) NULL, (char *) NULL,
119 "", Tk_Offset(TkPostscriptInfo, rotate), 0},
120 {TK_CONFIG_PIXELS, "-width", (char *) NULL, (char *) NULL,
121 "", Tk_Offset(TkPostscriptInfo, width), 0},
122 {TK_CONFIG_PIXELS, "-x", (char *) NULL, (char *) NULL,
123 "", Tk_Offset(TkPostscriptInfo, x), 0},
124 {TK_CONFIG_PIXELS, "-y", (char *) NULL, (char *) NULL,
125 "", Tk_Offset(TkPostscriptInfo, y), 0},
126 {TK_CONFIG_END, (char *) NULL, (char *) NULL, (char *) NULL,
127 (char *) NULL, 0, 0}
128 };
129
130 /*
131 * Forward declarations for procedures defined later in this file:
132 */
133
134 static int GetPostscriptPoints _ANSI_ARGS_((Tcl_Interp *interp,
135 char *string, double *doublePtr));
136
137 /*
138 *--------------------------------------------------------------
139 *
140 * TkCanvPostscriptCmd --
141 *
142 * This procedure is invoked to process the "postscript" options
143 * of the widget command for canvas widgets. See the user
144 * documentation for details on what it does.
145 *
146 * Results:
147 * A standard Tcl result.
148 *
149 * Side effects:
150 * See the user documentation.
151 *
152 *--------------------------------------------------------------
153 */
154
155 /* ARGSUSED */
156 int
157 TkCanvPostscriptCmd(canvasPtr, interp, argc, argv)
158 TkCanvas *canvasPtr; /* Information about canvas widget. */
159 Tcl_Interp *interp; /* Current interpreter. */
160 int argc; /* Number of arguments. */
161 CONST84 Tcl_Obj *CONST *objv; /* Argument strings. Caller has
162 * already parsed this command enough
163 * to know that argv[1] is
164 * "postscript". */
165 {
166 TkPostscriptInfo psInfo;
167 Tk_PostscriptInfo oldInfoPtr;
168 int result;
169 Tk_Item *itemPtr;
170 #define STRING_LENGTH 400
171 char string[STRING_LENGTH+1];
172 CONST char *p;
173 time_t now;
174 size_t length;
175 Tk_Window tkwin = canvasPtr->tkwin;
176 int deltaX = 0, deltaY = 0; /* Offset of lower-left corner of
177 * area to be marked up, measured
178 * in canvas units from the positioning
179 * point on the page (reflects
180 * anchor position). Initial values
181 * needed only to stop compiler
182 * warnings. */
183 Tcl_HashSearch search;
184 Tcl_HashEntry *hPtr;
185 Tcl_DString buffer;
186 #ifndef _LANG
187 char psenccmd[]="::tk::ensure_psenc_is_loaded";
188
189 /*
190 *----------------------------------------------------------------
191 * Initialize the data structure describing Postscript generation,
192 * then process all the arguments to fill the data structure in.
193 *----------------------------------------------------------------
194 */
195 result = Tcl_EvalEx(interp,psenccmd,-1,TCL_EVAL_GLOBAL);
196 if (result != TCL_OK) {
197 return result;
198 }
199 #endif
200 oldInfoPtr = canvasPtr->psInfo;
201 canvasPtr->psInfo = (Tk_PostscriptInfo) &psInfo;
202 psInfo.x = canvasPtr->xOrigin;
203 psInfo.y = canvasPtr->yOrigin;
204 psInfo.width = -1;
205 psInfo.height = -1;
206 psInfo.pageXString = NULL;
207 psInfo.pageYString = NULL;
208 psInfo.pageX = 72*4.25;
209 psInfo.pageY = 72*5.5;
210 psInfo.pageWidthString = NULL;
211 psInfo.pageHeightString = NULL;
212 psInfo.scale = 1.0;
213 psInfo.pageAnchor = TK_ANCHOR_CENTER;
214 psInfo.rotate = 0;
215 psInfo.fontVar = NULL;
216 psInfo.colorVar = NULL;
217 psInfo.colorMode = NULL;
218 psInfo.colorLevel = 0;
219 psInfo.fileName = NULL;
220 psInfo.channelName = NULL;
221 psInfo.chan = NULL;
222 psInfo.prepass = 0;
223 psInfo.prolog = 1;
224 Tcl_InitHashTable(&psInfo.fontTable, TCL_STRING_KEYS);
225 result = Tk_ConfigureWidget(interp, tkwin,
226 configSpecs, argc-2, argv+2, (char *) &psInfo,
227 TK_CONFIG_ARGV_ONLY);
228 if (result != TCL_OK) {
229 goto cleanup;
230 }
231
232 if (psInfo.width == -1) {
233 psInfo.width = Tk_Width(tkwin);
234 }
235 if (psInfo.height == -1) {
236 psInfo.height = Tk_Height(tkwin);
237 }
238 psInfo.x2 = psInfo.x + psInfo.width;
239 psInfo.y2 = psInfo.y + psInfo.height;
240
241 if (psInfo.pageXString != NULL) {
242 if (GetPostscriptPoints(interp, psInfo.pageXString,
243 &psInfo.pageX) != TCL_OK) {
244 goto cleanup;
245 }
246 }
247 if (psInfo.pageYString != NULL) {
248 if (GetPostscriptPoints(interp, psInfo.pageYString,
249 &psInfo.pageY) != TCL_OK) {
250 goto cleanup;
251 }
252 }
253 if (psInfo.pageWidthString != NULL) {
254 if (GetPostscriptPoints(interp, psInfo.pageWidthString,
255 &psInfo.scale) != TCL_OK) {
256 goto cleanup;
257 }
258 psInfo.scale /= psInfo.width;
259 } else if (psInfo.pageHeightString != NULL) {
260 if (GetPostscriptPoints(interp, psInfo.pageHeightString,
261 &psInfo.scale) != TCL_OK) {
262 goto cleanup;
263 }
264 psInfo.scale /= psInfo.height;
265 } else {
266 psInfo.scale = (72.0/25.4)*WidthMMOfScreen(Tk_Screen(tkwin));
267 psInfo.scale /= WidthOfScreen(Tk_Screen(tkwin));
268 }
269 switch (psInfo.pageAnchor) {
270 case TK_ANCHOR_NW:
271 case TK_ANCHOR_W:
272 case TK_ANCHOR_SW:
273 deltaX = 0;
274 break;
275 case TK_ANCHOR_N:
276 case TK_ANCHOR_CENTER:
277 case TK_ANCHOR_S:
278 deltaX = -psInfo.width/2;
279 break;
280 case TK_ANCHOR_NE:
281 case TK_ANCHOR_E:
282 case TK_ANCHOR_SE:
283 deltaX = -psInfo.width;
284 break;
285 }
286 switch (psInfo.pageAnchor) {
287 case TK_ANCHOR_NW:
288 case TK_ANCHOR_N:
289 case TK_ANCHOR_NE:
290 deltaY = - psInfo.height;
291 break;
292 case TK_ANCHOR_W:
293 case TK_ANCHOR_CENTER:
294 case TK_ANCHOR_E:
295 deltaY = -psInfo.height/2;
296 break;
297 case TK_ANCHOR_SW:
298 case TK_ANCHOR_S:
299 case TK_ANCHOR_SE:
300 deltaY = 0;
301 break;
302 }
303
304 if (psInfo.colorMode == NULL) {
305 psInfo.colorLevel = 2;
306 } else {
307 length = strlen(psInfo.colorMode);
308 if (strncmp(psInfo.colorMode, "monochrome", length) == 0) {
309 psInfo.colorLevel = 0;
310 } else if (strncmp(psInfo.colorMode, "gray", length) == 0) {
311 psInfo.colorLevel = 1;
312 } else if (strncmp(psInfo.colorMode, "color", length) == 0) {
313 psInfo.colorLevel = 2;
314 } else {
315 Tcl_AppendResult(interp, "bad color mode \"",
316 psInfo.colorMode, "\": must be monochrome, ",
317 "gray, or color", (char *) NULL);
318 goto cleanup;
319 }
320 }
321
322 if (psInfo.fileName != NULL) {
323
324 /*
325 * Check that -file and -channel are not both specified.
326 */
327
328 if (psInfo.channelName != NULL) {
329 Tcl_AppendResult(interp, "can't specify both -file",
330 " and -channel", (char *) NULL);
331 result = TCL_ERROR;
332 goto cleanup;
333 }
334
335 /*
336 * Check that we are not in a safe interpreter. If we are, disallow
337 * the -file specification.
338 */
339
340 if (Tcl_IsSafe(interp)) {
341 Tcl_AppendResult(interp, "can't specify -file in a",
342 " safe interpreter", (char *) NULL);
343 result = TCL_ERROR;
344 goto cleanup;
345 }
346
347 p = Tcl_TranslateFileName(interp, psInfo.fileName, &buffer);
348 if (p == NULL) {
349 goto cleanup;
350 }
351 psInfo.chan = Tcl_OpenFileChannel(interp, p, "w", 0666);
352 Tcl_DStringFree(&buffer);
353 if (psInfo.chan == NULL) {
354 goto cleanup;
355 }
356 }
357
358 if (psInfo.channelName != NULL) {
359 int mode;
360
361 /*
362 * Check that the channel is found in this interpreter and that it
363 * is open for writing.
364 */
365
366 psInfo.chan = Tcl_GetChannel(interp, psInfo.channelName,
367 &mode);
368 if (psInfo.chan == (Tcl_Channel) NULL) {
369 result = TCL_ERROR;
370 goto cleanup;
371 }
372 if ((mode & TCL_WRITABLE) == 0) {
373 Tcl_AppendResult(interp, "channel \"",
374 psInfo.channelName, "\" wasn't opened for writing",
375 (char *) NULL);
376 result = TCL_ERROR;
377 goto cleanup;
378 }
379 }
380
381 /*
382 *--------------------------------------------------------
383 * Make a pre-pass over all of the items, generating Postscript
384 * and then throwing it away. The purpose of this pass is just
385 * to collect information about all the fonts in use, so that
386 * we can output font information in the proper form required
387 * by the Document Structuring Conventions.
388 *--------------------------------------------------------
389 */
390
391 psInfo.prepass = 1;
392 for (itemPtr = canvasPtr->firstItemPtr; itemPtr != NULL;
393 itemPtr = itemPtr->nextPtr) {
394 if ((itemPtr->x1 >= psInfo.x2) || (itemPtr->x2 < psInfo.x)
395 || (itemPtr->y1 >= psInfo.y2) || (itemPtr->y2 < psInfo.y)) {
396 continue;
397 }
398 if (itemPtr->group != canvasPtr->activeGroup) {
399 continue;
400 }
401 if (itemPtr->typePtr->postscriptProc == NULL) {
402 continue;
403 }
404 result = (*itemPtr->typePtr->postscriptProc)(interp,
405 (Tk_Canvas) canvasPtr, itemPtr, 1);
406 Tcl_ResetResult(interp);
407 if (result != TCL_OK) {
408 /*
409 * An error just occurred. Just skip out of this loop.
410 * There's no need to report the error now; it can be
411 * reported later (errors can happen later that don't
412 * happen now, so we still have to check for errors later
413 * anyway).
414 */
415 break;
416 }
417 }
418 psInfo.prepass = 0;
419
420 /*
421 *--------------------------------------------------------
422 * Generate the header and prolog for the Postscript.
423 *--------------------------------------------------------
424 */
425
426 if (psInfo.prolog) {
427 Tcl_AppendResult(interp, "%!PS-Adobe-3.0 EPSF-3.0\n",
428 "%%Creator: Tk Canvas Widget\n", (char *) NULL);
429 #ifdef HAVE_PW_GECOS
430 if (!Tcl_IsSafe(interp)) {
431 struct passwd *pwPtr = getpwuid(getuid()); /* INTL: Native. */
432 Tcl_AppendResult(interp, "%%For: ",
433 (pwPtr != NULL) ? pwPtr->pw_gecos : "Unknown", "\n",
434 (char *) NULL);
435 endpwent();
436 }
437 #endif /* HAVE_PW_GECOS */
438 Tcl_AppendResult(interp, "%%Title: Window ",
439 Tk_PathName(tkwin), "\n", (char *) NULL);
440 time(&now);
441 Tcl_AppendResult(interp, "%%CreationDate: ",
442 ctime(&now), (char *) NULL); /* INTL: Native. */
443 if (!psInfo.rotate) {
444 sprintf(string, "%d %d %d %d",
445 (int) (psInfo.pageX + psInfo.scale*deltaX),
446 (int) (psInfo.pageY + psInfo.scale*deltaY),
447 (int) (psInfo.pageX + psInfo.scale*(deltaX + psInfo.width)
448 + 1.0),
449 (int) (psInfo.pageY + psInfo.scale*(deltaY + psInfo.height)
450 + 1.0));
451 } else {
452 sprintf(string, "%d %d %d %d",
453 (int) (psInfo.pageX - psInfo.scale*(deltaY + psInfo.height)),
454 (int) (psInfo.pageY + psInfo.scale*deltaX),
455 (int) (psInfo.pageX - psInfo.scale*deltaY + 1.0),
456 (int) (psInfo.pageY + psInfo.scale*(deltaX + psInfo.width)
457 + 1.0));
458 }
459 Tcl_AppendResult(interp, "%%BoundingBox: ", string,
460 "\n", (char *) NULL);
461 Tcl_AppendResult(interp, "%%Pages: 1\n",
462 "%%DocumentData: Clean7Bit\n", (char *) NULL);
463 Tcl_AppendResult(interp, "%%Orientation: ",
464 psInfo.rotate ? "Landscape\n" : "Portrait\n", (char *) NULL);
465 p = "%%DocumentNeededResources: font ";
466 for (hPtr = Tcl_FirstHashEntry(&psInfo.fontTable, &search);
467 hPtr != NULL; hPtr = Tcl_NextHashEntry(&search)) {
468 Tcl_AppendResult(interp, p,
469 Tcl_GetHashKey(&psInfo.fontTable, hPtr),
470 "\n", (char *) NULL);
471 p = "%%+ font ";
472 }
473 Tcl_AppendResult(interp, "%%EndComments\n\n", (char *) NULL);
474
475 /*
476 * Insert the prolog
477 */
478 Tcl_AppendResult(interp, Tcl_GetVar(interp,"::tk::ps_preamable",
479 TCL_GLOBAL_ONLY), (char *) NULL);
480
481 if (psInfo.chan != NULL) {
482 Tcl_Write(psInfo.chan, Tcl_GetStringResult(interp), -1);
483 Tcl_ResetResult(canvasPtr->interp);
484 }
485
486 /*
487 *-----------------------------------------------------------
488 * Document setup: set the color level and include fonts.
489 *-----------------------------------------------------------
490 */
491
492 sprintf(string, "/CL %d def\n", psInfo.colorLevel);
493 Tcl_AppendResult(interp, "%%BeginSetup\n", string,
494 (char *) NULL);
495 for (hPtr = Tcl_FirstHashEntry(&psInfo.fontTable, &search);
496 hPtr != NULL; hPtr = Tcl_NextHashEntry(&search)) {
497 Tcl_AppendResult(interp, "%%IncludeResource: font ",
498 Tcl_GetHashKey(&psInfo.fontTable, hPtr), "\n", (char *) NULL);
499 }
500 Tcl_AppendResult(interp, "%%EndSetup\n\n", (char *) NULL);
501
502 /*
503 *-----------------------------------------------------------
504 * Page setup: move to page positioning point, rotate if
505 * needed, set scale factor, offset for proper anchor position,
506 * and set clip region.
507 *-----------------------------------------------------------
508 */
509
510 Tcl_AppendResult(interp, "%%Page: 1 1\n", "save\n",
511 (char *) NULL);
512 sprintf(string, "%.1f %.1f translate\n", psInfo.pageX, psInfo.pageY);
513 Tcl_AppendResult(interp, string, (char *) NULL);
514 if (psInfo.rotate) {
515 Tcl_AppendResult(interp, "90 rotate\n", (char *) NULL);
516 }
517 sprintf(string, "%.4g %.4g scale\n", psInfo.scale, psInfo.scale);
518 Tcl_AppendResult(interp, string, (char *) NULL);
519 sprintf(string, "%d %d translate\n", deltaX - psInfo.x, deltaY);
520 Tcl_AppendResult(interp, string, (char *) NULL);
521 sprintf(string, "%d %.15g moveto %d %.15g lineto %d %.15g lineto %d %.15g",
522 psInfo.x,
523 Tk_PostscriptY((double) psInfo.y, (Tk_PostscriptInfo) &psInfo),
524 psInfo.x2,
525 Tk_PostscriptY((double) psInfo.y, (Tk_PostscriptInfo) &psInfo),
526 psInfo.x2,
527 Tk_PostscriptY((double) psInfo.y2, (Tk_PostscriptInfo) &psInfo),
528 psInfo.x,
529 Tk_PostscriptY((double) psInfo.y2, (Tk_PostscriptInfo) &psInfo));
530 Tcl_AppendResult(interp, string,
531 " lineto closepath clip newpath\n", (char *) NULL);
532 }
533 if (psInfo.chan != NULL) {
534 Tcl_Write(psInfo.chan, Tcl_GetStringResult(interp), -1);
535 Tcl_ResetResult(canvasPtr->interp);
536 }
537
538 /*
539 *---------------------------------------------------------------------
540 * Iterate through all the items, having each relevant one draw itself.
541 * Quit if any of the items returns an error.
542 *---------------------------------------------------------------------
543 */
544
545 result = TCL_OK;
546 for (itemPtr = canvasPtr->firstItemPtr; itemPtr != NULL;
547 itemPtr = itemPtr->nextPtr) {
548 if ((itemPtr->x1 >= psInfo.x2) || (itemPtr->x2 < psInfo.x)
549 || (itemPtr->y1 >= psInfo.y2) || (itemPtr->y2 < psInfo.y)) {
550 continue;
551 }
552 if (itemPtr->group != canvasPtr->activeGroup) {
553 continue;
554 }
555 if (itemPtr->typePtr->postscriptProc == NULL) {
556 continue;
557 }
558 if (itemPtr->state == TK_STATE_HIDDEN) {
559 continue;
560 }
561 Tcl_AppendResult(interp, "gsave\n", (char *) NULL);
562 result = (*itemPtr->typePtr->postscriptProc)(interp,
563 (Tk_Canvas) canvasPtr, itemPtr, 0);
564 if (result != TCL_OK) {
565 char msg[64 + TCL_INTEGER_SPACE];
566
567 sprintf(msg, "\n (generating Postscript for item %d)",
568 itemPtr->id);
569 Tcl_AddErrorInfo(interp, msg);
570 goto cleanup;
571 }
572 Tcl_AppendResult(interp, "grestore\n", (char *) NULL);
573 if (psInfo.chan != NULL) {
574 Tcl_Write(psInfo.chan, Tcl_GetStringResult(interp), -1);
575 Tcl_ResetResult(interp);
576 }
577 }
578
579 /*
580 *---------------------------------------------------------------------
581 * Output page-end information, such as commands to print the page
582 * and document trailer stuff.
583 *---------------------------------------------------------------------
584 */
585
586 if (psInfo.prolog) {
587 Tcl_AppendResult(interp, "restore showpage\n\n",
588 "%%Trailer\nend\n%%EOF\n", (char *) NULL);
589 }
590 if (psInfo.chan != NULL) {
591 Tcl_Write(psInfo.chan, Tcl_GetStringResult(interp), -1);
592 Tcl_ResetResult(canvasPtr->interp);
593 }
594
595 /*
596 * Clean up psInfo to release malloc'ed stuff.
597 */
598
599 cleanup:
600 if (psInfo.pageXString != NULL) {
601 ckfree(psInfo.pageXString);
602 }
603 if (psInfo.pageYString != NULL) {
604 ckfree(psInfo.pageYString);
605 }
606 if (psInfo.pageWidthString != NULL) {
607 ckfree(psInfo.pageWidthString);
608 }
609 if (psInfo.pageHeightString != NULL) {
610 ckfree(psInfo.pageHeightString);
611 }
612 if (psInfo.fontVar != NULL) {
613 LangFreeVar(psInfo.fontVar);
614 }
615 if (psInfo.colorVar != NULL) {
616 LangFreeVar(psInfo.colorVar);
617 }
618 if (psInfo.colorMode != NULL) {
619 ckfree(psInfo.colorMode);
620 }
621 if (psInfo.fileName != NULL) {
622 ckfree(psInfo.fileName);
623 }
624 if ((psInfo.chan != NULL) && (psInfo.channelName == NULL)) {
625 Tcl_Close(interp, psInfo.chan);
626 }
627 if (psInfo.channelName != NULL) {
628 ckfree(psInfo.channelName);
629 }
630 Tcl_DeleteHashTable(&psInfo.fontTable);
631 canvasPtr->psInfo = (Tk_PostscriptInfo) oldInfoPtr;
632 return result;
633 }
634
635 /*
636 *--------------------------------------------------------------
637 *
638 * Tk_PostscriptColor --
639 *
640 * This procedure is called by individual canvas items when
641 * they want to set a color value for output. Given information
642 * about an X color, this procedure will generate Postscript
643 * commands to set up an appropriate color in Postscript.
644 *
645 * Results:
646 * Returns a standard Tcl return value. If an error occurs
647 * then an error message will be left in the interp's result.
648 * If no error occurs, then additional Postscript will be
649 * appended to the interp's result.
650 *
651 * Side effects:
652 * None.
653 *
654 *--------------------------------------------------------------
655 */
656
657 int
658 Tk_PostscriptColor(interp, psInfo, colorPtr)
659 Tcl_Interp *interp;
660 Tk_PostscriptInfo psInfo; /* Postscript info. */
661 XColor *colorPtr; /* Information about color. */
662 {
663 TkPostscriptInfo *psInfoPtr = (TkPostscriptInfo *) psInfo;
664 int tmp;
665 double red, green, blue;
666 char string[200];
667
668 if (psInfoPtr->prepass) {
669 return TCL_OK;
670 }
671
672 /*
673 * If there is a color map defined, then look up the color's name
674 * in the map and use the Postscript commands found there, if there
675 * are any.
676 */
677
678 if (psInfoPtr->colorVar != NULL) {
679 Tcl_Obj * cmdString;
680 Tcl_Obj *name = Tcl_NewStringObj(Tk_NameOfColor(colorPtr),-1);
681
682 cmdString = Tcl_ObjGetVar2(interp, psInfoPtr->colorVar, name,0);
683 Tcl_DecrRefCount(name);
684
685 if (cmdString != NULL) {
686 Tcl_AppendResult(interp, Tcl_GetString(cmdString), "\n", (char *) NULL);
687 return TCL_OK;
688 }
689 }
690
691 /*
692 * No color map entry for this color. Grab the color's intensities
693 * and output Postscript commands for them. Special note: X uses
694 * a range of 0-65535 for intensities, but most displays only use
695 * a range of 0-255, which maps to (0, 256, 512, ... 65280) in the
696 * X scale. This means that there's no way to get perfect white,
697 * since the highest intensity is only 65280 out of 65535. To
698 * work around this problem, rescale the X intensity to a 0-255
699 * scale and use that as the basis for the Postscript colors. This
700 * scheme still won't work if the display only uses 4 bits per color,
701 * but most diplays use at least 8 bits.
702 */
703
704 tmp = colorPtr->red;
705 red = ((double) (tmp >> 8))/255.0;
706 tmp = colorPtr->green;
707 green = ((double) (tmp >> 8))/255.0;
708 tmp = colorPtr->blue;
709 blue = ((double) (tmp >> 8))/255.0;
710 sprintf(string, "%.3f %.3f %.3f setrgbcolor AdjustColor\n",
711 red, green, blue);
712 Tcl_AppendResult(interp, string, (char *) NULL);
713 return TCL_OK;
714 }
715
716 /*
717 *--------------------------------------------------------------
718 *
719 * Tk_PostscriptFont --
720 *
721 * This procedure is called by individual canvas items when
722 * they want to output text. Given information about an X
723 * font, this procedure will generate Postscript commands
724 * to set up an appropriate font in Postscript.
725 *
726 * Results:
727 * Returns a standard Tcl return value. If an error occurs
728 * then an error message will be left in the interp's result.
729 * If no error occurs, then additional Postscript will be
730 * appended to the interp's result.
731 *
732 * Side effects:
733 * The Postscript font name is entered into psInfoPtr->fontTable
734 * if it wasn't already there.
735 *
736 *--------------------------------------------------------------
737 */
738
739 int
740 Tk_PostscriptFont(interp, psInfo, tkfont)
741 Tcl_Interp *interp;
742 Tk_PostscriptInfo psInfo; /* Postscript Info. */
743 Tk_Font tkfont; /* Information about font in which text
744 * is to be printed. */
745 {
746 TkPostscriptInfo *psInfoPtr = (TkPostscriptInfo *) psInfo;
747 char *end;
748 char pointString[TCL_INTEGER_SPACE];
749 Tcl_DString ds;
750 int i, points;
751
752 /*
753 * First, look up the font's name in the font map, if there is one.
754 * If there is an entry for this font, it consists of a list
755 * containing font name and size. Use this information.
756 */
757
758 Tcl_DStringInit(&ds);
759
760 if (psInfoPtr->fontVar != NULL) {
761 Tcl_Obj *list;
762 int argc;
763 double size;
764 Tcl_Obj **objv;
765 Tcl_Obj *name;
766
767 name = Tcl_NewStringObj(Tk_NameOfFont(tkfont),-1);
768 list = Tcl_ObjGetVar2(interp, psInfoPtr->fontVar, name, 0);
769 Tcl_DecrRefCount(name);
770 if (list != NULL) {
771 if (Tcl_ListObjGetElements(interp, list, &argc, &argv)
772 != TCL_OK) {
773 badMapEntry:
774 Tcl_ResetResult(interp);
775 Tcl_AppendResult(interp, "bad font map entry for \"", name,
776 "\": \"", list, "\"", (char *) NULL);
777 return TCL_ERROR;
778 }
779 if (argc != 2) {
780 goto badMapEntry;
781 }
782 if (Tcl_GetDoubleFromObj(interp, objv[1], &size) != TCL_OK) {
783 goto badMapEntry;
784 }
785
786 Tcl_DStringAppend(&ds, argv[0], -1);
787 points = (int) size;
788 #ifdef FIXME
789 ckfree((char *) argv);
790 #endif
791 goto findfont;
792 }
793 }
794
795 points = Tk_PostscriptFontName(tkfont, &ds);
796
797 findfont:
798 sprintf(pointString, "%d", points);
799 Tcl_AppendResult(interp, "/", Tcl_DStringValue(&ds), " findfont ",
800 pointString, " scalefont ", (char *) NULL);
801 if (strncasecmp(Tcl_DStringValue(&ds), "Symbol", 7) != 0) {
802 Tcl_AppendResult(interp, "ISOEncode ", (char *) NULL);
803 }
804 Tcl_AppendResult(interp, "setfont\n", (char *) NULL);
805 Tcl_CreateHashEntry(&psInfoPtr->fontTable, Tcl_DStringValue(&ds), &i);
806 Tcl_DStringFree(&ds);
807
808 return TCL_OK;
809 }
810
811 /*
812 *--------------------------------------------------------------
813 *
814 * Tk_PostscriptBitmap --
815 *
816 * This procedure is called to output the contents of a
817 * sub-region of a bitmap in proper image data format for
818 * Postscript (i.e. data between angle brackets, one bit
819 * per pixel).
820 *
821 * Results:
822 * Returns a standard Tcl return value. If an error occurs
823 * then an error message will be left in the interp's result.
824 * If no error occurs, then additional Postscript will be
825 * appended to the interp's result.
826 *
827 * Side effects:
828 * None.
829 *
830 *--------------------------------------------------------------
831 */
832
833 #undef TkGetProlog
834 int
835 Tk_PostscriptBitmap(interp, tkwin, psInfo, bitmap, startX, startY, width,
836 height)
837 Tcl_Interp *interp;
838 Tk_Window tkwin;
839 Tk_PostscriptInfo psInfo; /* Postscript info. */
840 Pixmap bitmap; /* Bitmap for which to generate
841 * Postscript. */
842 int startX, startY; /* Coordinates of upper-left corner
843 * of rectangular region to output. */
844 int width, height; /* Height of rectangular region. */
845 {
846 TkPostscriptInfo *psInfoPtr = (TkPostscriptInfo *) psInfo;
847 XImage *imagePtr;
848 int charsInLine, x, y, lastX, lastY, value, mask;
849 unsigned int totalWidth, totalHeight;
850 char string[100];
851 Window dummyRoot;
852 int dummyX, dummyY;
853 unsigned dummyBorderwidth, dummyDepth;
854
855 if (psInfoPtr->prepass) {
856 return TCL_OK;
857 }
858
859 /*
860 * The following call should probably be a call to Tk_SizeOfBitmap
861 * instead, but it seems that we are occasionally invoked by custom
862 * item types that create their own bitmaps without registering them
863 * with Tk. XGetGeometry is a bit slower than Tk_SizeOfBitmap, but
864 * it shouldn't matter here.
865 */
866
867 XGetGeometry(Tk_Display(tkwin), bitmap, &dummyRoot,
868 (int *) &dummyX, (int *) &dummyY, (unsigned int *) &totalWidth,
869 (unsigned int *) &totalHeight, &dummyBorderwidth, &dummyDepth);
870 imagePtr = XGetImage(Tk_Display(tkwin), bitmap, 0, 0,
871 totalWidth, totalHeight, 1, XYPixmap);
872 Tcl_AppendResult(interp, "<", (char *) NULL);
873 mask = 0x80;
874 value = 0;
875 charsInLine = 0;
876 lastX = startX + width - 1;
877 lastY = startY + height - 1;
878 for (y = lastY; y >= startY; y--) {
879 for (x = startX; x <= lastX; x++) {
880 if (XGetPixel(imagePtr, x, y)) {
881 value |= mask;
882 }
883 mask >>= 1;
884 if (mask == 0) {
885 sprintf(string, "%02x", value);
886 Tcl_AppendResult(interp, string, (char *) NULL);
887 mask = 0x80;
888 value = 0;
889 charsInLine += 2;
890 if (charsInLine >= 60) {
891 Tcl_AppendResult(interp, "\n", (char *) NULL);
892 charsInLine = 0;
893 }
894 }
895 }
896 if (mask != 0x80) {
897 sprintf(string, "%02x", value);
898 Tcl_AppendResult(interp, string, (char *) NULL);
899 mask = 0x80;
900 value = 0;
901 charsInLine += 2;
902 }
903 }
904 Tcl_AppendResult(interp, ">", (char *) NULL);
905 XDestroyImage(imagePtr);
906 return TCL_OK;
907 }
908
909 /*
910 *--------------------------------------------------------------
911 *
912 * Tk_PostscriptStipple --
913 *
914 * This procedure is called by individual canvas items when
915 * they have created a path that they'd like to be filled with
916 * a stipple pattern. Given information about an X bitmap,
917 * this procedure will generate Postscript commands to fill
918 * the current clip region using a stipple pattern defined by the
919 * bitmap.
920 *
921 * Results:
922 * Returns a standard Tcl return value. If an error occurs
923 * then an error message will be left in the interp's result.
924 * If no error occurs, then additional Postscript will be
925 * appended to the interp's result.
926 *
927 * Side effects:
928 * None.
929 *
930 *--------------------------------------------------------------
931 */
932
933 int
934 Tk_PostscriptStipple(interp, tkwin, psInfo, bitmap)
935 Tcl_Interp *interp;
936 Tk_Window tkwin;
937 Tk_PostscriptInfo psInfo; /* Interpreter for returning Postscript
938 * or error message. */
939 Pixmap bitmap; /* Bitmap to use for stippling. */
940 {
941 TkPostscriptInfo *psInfoPtr = (TkPostscriptInfo *) psInfo;
942 int width, height;
943 char string[TCL_INTEGER_SPACE * 2];
944 Window dummyRoot;
945 int dummyX, dummyY;
946 unsigned dummyBorderwidth, dummyDepth;
947
948 if (psInfoPtr->prepass) {
949 return TCL_OK;
950 }
951
952 /*
953 * The following call should probably be a call to Tk_SizeOfBitmap
954 * instead, but it seems that we are occasionally invoked by custom
955 * item types that create their own bitmaps without registering them
956 * with Tk. XGetGeometry is a bit slower than Tk_SizeOfBitmap, but
957 * it shouldn't matter here.
958 */
959
960 XGetGeometry(Tk_Display(tkwin), bitmap, &dummyRoot,
961 (int *) &dummyX, (int *) &dummyY, (unsigned *) &width,
962 (unsigned *) &height, &dummyBorderwidth, &dummyDepth);
963 sprintf(string, "%d %d ", width, height);
964 Tcl_AppendResult(interp, string, (char *) NULL);
965 if (Tk_PostscriptBitmap(interp, tkwin, psInfo, bitmap, 0, 0,
966 width, height) != TCL_OK) {
967 return TCL_ERROR;
968 }
969 Tcl_AppendResult(interp, " StippleFill\n", (char *) NULL);
970 return TCL_OK;
971 }
972
973 /*
974 *--------------------------------------------------------------
975 *
976 * Tk_PostscriptY --
977 *
978 * Given a y-coordinate in local coordinates, this procedure
979 * returns a y-coordinate to use for Postscript output.
980 *
981 * Results:
982 * Returns the Postscript coordinate that corresponds to
983 * "y".
984 *
985 * Side effects:
986 * None.
987 *
988 *--------------------------------------------------------------
989 */
990
991 double
992 Tk_PostscriptY(y, psInfo)
993 double y; /* Y-coordinate in canvas coords. */
994 Tk_PostscriptInfo psInfo; /* Postscript info */
995 {
996 TkPostscriptInfo *psInfoPtr = (TkPostscriptInfo *) psInfo;
997
998 return psInfoPtr->y2 - y;
999 }
1000
1001 /*
1002 *--------------------------------------------------------------
1003 *
1004 * Tk_PostscriptPath --
1005 *
1006 * Given an array of points for a path, generate Postscript
1007 * commands to create the path.
1008 *
1009 * Results:
1010 * Postscript commands get appended to what's in the interp's result.
1011 *
1012 * Side effects:
1013 * None.
1014 *
1015 *--------------------------------------------------------------
1016 */
1017
1018 void
1019 Tk_PostscriptPath(interp, psInfo, coordPtr, numPoints)
1020 Tcl_Interp *interp;
1021 Tk_PostscriptInfo psInfo; /* Canvas on whose behalf Postscript
1022 * is being generated. */
1023 double *coordPtr; /* Pointer to first in array of
1024 * 2*numPoints coordinates giving
1025 * points for path. */
1026 int numPoints; /* Number of points at *coordPtr. */
1027 {
1028 TkPostscriptInfo *psInfoPtr = (TkPostscriptInfo *) psInfo;
1029 char buffer[200];
1030
1031 if (psInfoPtr->prepass) {
1032 return;
1033 }
1034 sprintf(buffer, "%.15g %.15g moveto\n", coordPtr[0],
1035 Tk_PostscriptY(coordPtr[1], psInfo));
1036 Tcl_AppendResult(interp, buffer, (char *) NULL);
1037 for (numPoints--, coordPtr += 2; numPoints > 0;
1038 numPoints--, coordPtr += 2) {
1039 sprintf(buffer, "%.15g %.15g lineto\n", coordPtr[0],
1040 Tk_PostscriptY(coordPtr[1], psInfo));
1041 Tcl_AppendResult(interp, buffer, (char *) NULL);
1042 }
1043 }
1044
1045 /*
1046 *--------------------------------------------------------------
1047 *
1048 * GetPostscriptPoints --
1049 *
1050 * Given a string, returns the number of Postscript points
1051 * corresponding to that string.
1052 *
1053 * Results:
1054 * The return value is a standard Tcl return result. If
1055 * TCL_OK is returned, then everything went well and the
1056 * screen distance is stored at *doublePtr; otherwise
1057 * TCL_ERROR is returned and an error message is left in
1058 * the interp's result.
1059 *
1060 * Side effects:
1061 * None.
1062 *
1063 *--------------------------------------------------------------
1064 */
1065
1066 static int
1067 GetPostscriptPoints(interp, string, doublePtr)
1068 Tcl_Interp *interp; /* Use this for error reporting. */
1069 char *string; /* String describing a screen distance. */
1070 double *doublePtr; /* Place to store converted result. */
1071 {
1072 char *end;
1073 double d;
1074
1075 d = strtod(string, &end);
1076 if (end == string) {
1077 error:
1078 Tcl_AppendResult(interp, "bad distance \"", string,
1079 "\"", (char *) NULL);
1080 return TCL_ERROR;
1081 }
1082 while ((*end != '\0') && isspace(UCHAR(*end))) {
1083 end++;
1084 }
1085 switch (*end) {
1086 case 'c':
1087 d *= 72.0/2.54;
1088 end++;
1089 break;
1090 case 'i':
1091 d *= 72.0;
1092 end++;
1093 break;
1094 case 'm':
1095 d *= 72.0/25.4;
1096 end++;
1097 break;
1098 case 0:
1099 break;
1100 case 'p':
1101 end++;
1102 break;
1103 default:
1104 goto error;
1105 }
1106 while ((*end != '\0') && isspace(UCHAR(*end))) {
1107 end++;
1108 }
1109 if (*end != 0) {
1110 goto error;
1111 }
1112 *doublePtr = d;
1113 return TCL_OK;
1114 }
1115
1116 /*
1117 *--------------------------------------------------------------
1118 *
1119 * TkImageGetColor --
1120 *
1121 * This procedure converts a pixel value to three floating
1122 * point numbers, representing the amount of red, green, and
1123 * blue in that pixel on the screen. It makes use of colormap
1124 * data passed as an argument, and should work for all Visual
1125 * types.
1126 *
1127 * This implementation is bogus on Windows because the colormap
1128 * data is never filled in. Instead all postscript generated
1129 * data coming through here is expected to be RGB color data.
1130 * To handle lower bit-depth images properly, XQueryColors
1131 * must be implemented for Windows.
1132 *
1133 * Results:
1134 * Returns red, green, and blue color values in the range
1135 * 0 to 1. There are no error returns.
1136 *
1137 * Side effects:
1138 * None.
1139 *
1140 *--------------------------------------------------------------
1141 */
1142 #ifdef WIN32
1143 #include <windows.h>
1144
1145 /*
1146 * We could just define these instead of pulling in windows.h.
1147 #define GetRValue(rgb) ((BYTE)(rgb))
1148 #define GetGValue(rgb) ((BYTE)(((WORD)(rgb)) >> 8))
1149 #define GetBValue(rgb) ((BYTE)((rgb)>>16))
1150 */
1151
1152 static void
1153 TkImageGetColor(cdata, pixel, red, green, blue)
1154 TkColormapData *cdata; /* Colormap data */
1155 unsigned long pixel; /* Pixel value to look up */
1156 double *red, *green, *blue; /* Color data to return */
1157 {
1158 *red = (double) GetRValue(pixel) / 255.0;
1159 *green = (double) GetGValue(pixel) / 255.0;
1160 *blue = (double) GetBValue(pixel) / 255.0;
1161 }
1162 #else
1163 static void
1164 TkImageGetColor(cdata, pixel, red, green, blue)
1165 TkColormapData *cdata; /* Colormap data */
1166 unsigned long pixel; /* Pixel value to look up */
1167 double *red, *green, *blue; /* Color data to return */
1168 {
1169 if (cdata->separated) {
1170 int r = (pixel & cdata->red_mask) >> cdata->red_shift;
1171 int g = (pixel & cdata->green_mask) >> cdata->green_shift;
1172 int b = (pixel & cdata->blue_mask) >> cdata->blue_shift;
1173 *red = cdata->colors[r].red / 65535.0;
1174 *green = cdata->colors[g].green / 65535.0;
1175 *blue = cdata->colors[b].blue / 65535.0;
1176 } else {
1177 *red = cdata->colors[pixel].red / 65535.0;
1178 *green = cdata->colors[pixel].green / 65535.0;
1179 *blue = cdata->colors[pixel].blue / 65535.0;
1180 }
1181 }
1182 #endif
1183
1184 /*
1185 *--------------------------------------------------------------
1186 *
1187 * TkPostscriptImage --
1188 *
1189 * This procedure is called to output the contents of an
1190 * image in Postscript, using a format appropriate for the
1191 * current color mode (i.e. one bit per pixel in monochrome,
1192 * one byte per pixel in gray, and three bytes per pixel in
1193 * color).
1194 *
1195 * Results:
1196 * Returns a standard Tcl return value. If an error occurs
1197 * then an error message will be left in interp->result.
1198 * If no error occurs, then additional Postscript will be
1199 * appended to interp->result.
1200 *
1201 * Side effects:
1202 * None.
1203 *
1204 *--------------------------------------------------------------
1205 */
1206
1207 int
1208 TkPostscriptImage(interp, tkwin, psInfo, ximage, x, y, width, height)
1209 Tcl_Interp *interp;
1210 Tk_Window tkwin;
1211 Tk_PostscriptInfo psInfo; /* postscript info */
1212 XImage *ximage; /* Image to draw */
1213 int x, y; /* First pixel to output */
1214 int width, height; /* Width and height of area */
1215 {
1216 TkPostscriptInfo *psInfoPtr = (TkPostscriptInfo *) psInfo;
1217 char buffer[256];
1218 int xx, yy, band, maxRows;
1219 double red, green, blue;
1220 int bytesPerLine=0, maxWidth=0;
1221 int level = psInfoPtr->colorLevel;
1222 Colormap cmap;
1223 int i, ncolors;
1224 Visual *visual;
1225 TkColormapData cdata;
1226
1227 if (psInfoPtr->prepass) {
1228 return TCL_OK;
1229 }
1230
1231 cmap = Tk_Colormap(tkwin);
1232 visual = Tk_Visual(tkwin);
1233
1234 /*
1235 * Obtain information about the colormap, ie the mapping between
1236 * pixel values and RGB values. The code below should work
1237 * for all Visual types.
1238 */
1239
1240 ncolors = visual->map_entries;
1241 cdata.colors = (XColor *) ckalloc(sizeof(XColor) * ncolors);
1242 cdata.ncolors = ncolors;
1243
1244 if (visual->class == DirectColor || visual->class == TrueColor) {
1245 cdata.separated = 1;
1246 cdata.red_mask = visual->red_mask;
1247 cdata.green_mask = visual->green_mask;
1248 cdata.blue_mask = visual->blue_mask;
1249 cdata.red_shift = 0;
1250 cdata.green_shift = 0;
1251 cdata.blue_shift = 0;
1252 while ((0x0001 & (cdata.red_mask >> cdata.red_shift)) == 0)
1253 cdata.red_shift ++;
1254 while ((0x0001 & (cdata.green_mask >> cdata.green_shift)) == 0)
1255 cdata.green_shift ++;
1256 while ((0x0001 & (cdata.blue_mask >> cdata.blue_shift)) == 0)
1257 cdata.blue_shift ++;
1258 for (i = 0; i < ncolors; i ++)
1259 cdata.colors[i].pixel =
1260 ((i << cdata.red_shift) & cdata.red_mask) |
1261 ((i << cdata.green_shift) & cdata.green_mask) |
1262 ((i << cdata.blue_shift) & cdata.blue_mask);
1263 } else {
1264 cdata.separated=0;
1265 for (i = 0; i < ncolors; i ++)
1266 cdata.colors[i].pixel = i;
1267 }
1268 if (visual->class == StaticGray || visual->class == GrayScale)
1269 cdata.color = 0;
1270 else
1271 cdata.color = 1;
1272
1273 XQueryColors(Tk_Display(tkwin), cmap, cdata.colors, ncolors);
1274
1275 /*
1276 * Figure out which color level to use (possibly lower than the
1277 * one specified by the user). For example, if the user specifies
1278 * color with monochrome screen, use gray or monochrome mode instead.
1279 */
1280
1281 if (!cdata.color && level == 2) {
1282 level = 1;
1283 }
1284
1285 if (!cdata.color && cdata.ncolors == 2) {
1286 level = 0;
1287 }
1288
1289 /*
1290 * Check that at least one row of the image can be represented
1291 * with a string less than 64 KB long (this is a limit in the
1292 * Postscript interpreter).
1293 */
1294
1295 switch (level) {
1296 case 0: bytesPerLine = (width + 7) / 8; maxWidth = 240000; break;
1297 case 1: bytesPerLine = width; maxWidth = 60000; break;
1298 case 2: bytesPerLine = 3 * width; maxWidth = 20000; break;
1299 }
1300
1301 if (bytesPerLine > 60000) {
1302 Tcl_ResetResult(interp);
1303 sprintf(buffer,
1304 "Can't generate Postscript for images more than %d pixels wide",
1305 maxWidth);
1306 Tcl_AppendResult(interp, buffer, (char *) NULL);
1307 ckfree((char *) cdata.colors);
1308 return TCL_ERROR;
1309 }
1310
1311 maxRows = 60000 / bytesPerLine;
1312
1313 for (band = height-1; band >= 0; band -= maxRows) {
1314 int rows = (band >= maxRows) ? maxRows : band + 1;
1315 int lineLen = 0;
1316 switch (level) {
1317 case 0:
1318 sprintf(buffer, "%d %d 1 matrix {\n<", width, rows);
1319 Tcl_AppendResult(interp, buffer, (char *) NULL);
1320 break;
1321 case 1:
1322 sprintf(buffer, "%d %d 8 matrix {\n<", width, rows);
1323 Tcl_AppendResult(interp, buffer, (char *) NULL);
1324 break;
1325 case 2:
1326 sprintf(buffer, "%d %d 8 matrix {\n<",
1327 width, rows);
1328 Tcl_AppendResult(interp, buffer, (char *) NULL);
1329 break;
1330 }
1331 for (yy = band; yy > band - rows; yy--) {
1332 switch (level) {
1333 case 0: {
1334 /*
1335 * Generate data for image in monochrome mode.
1336 * No attempt at dithering is made--instead, just
1337 * set a threshold.
1338 */
1339 unsigned char mask=0x80;
1340 unsigned char data=0x00;
1341 for (xx = x; xx< x+width; xx++) {
1342 TkImageGetColor(&cdata, XGetPixel(ximage, xx, yy),
1343 &red, &green, &blue);
1344 if (0.30 * red + 0.59 * green + 0.11 * blue > 0.5)
1345 data |= mask;
1346 mask >>= 1;
1347 if (mask == 0) {
1348 sprintf(buffer, "%02X", data);
1349 Tcl_AppendResult(interp, buffer, (char *) NULL);
1350 lineLen += 2;
1351 if (lineLen > 60) {
1352 lineLen = 0;
1353 Tcl_AppendResult(interp, "\n", (char *) NULL);
1354 }
1355 mask=0x80;
1356 data=0x00;
1357 }
1358 }
1359 if ((width % 8) != 0) {
1360 sprintf(buffer, "%02X", data);
1361 Tcl_AppendResult(interp, buffer, (char *) NULL);
1362 mask=0x80;
1363 data=0x00;
1364 }
1365 break;
1366 }
1367 case 1: {
1368 /*
1369 * Generate data in gray mode--in this case, take a
1370 * weighted sum of the red, green, and blue values.
1371 */
1372 for (xx = x; xx < x+width; xx ++) {
1373 TkImageGetColor(&cdata, XGetPixel(ximage, xx, yy),
1374 &red, &green, &blue);
1375 sprintf(buffer, "%02X", (int) floor(0.5 + 255.0 *
1376 (0.30 * red + 0.59 * green + 0.11 * blue)));
1377 Tcl_AppendResult(interp, buffer, (char *) NULL);
1378 lineLen += 2;
1379 if (lineLen > 60) {
1380 lineLen = 0;
1381 Tcl_AppendResult(interp, "\n", (char *) NULL);
1382 }
1383 }
1384 break;
1385 }
1386 case 2: {
1387 /*
1388 * Finally, color mode. Here, just output the red, green,
1389 * and blue values directly.
1390 */
1391 for (xx = x; xx < x+width; xx++) {
1392 TkImageGetColor(&cdata, XGetPixel(ximage, xx, yy),
1393 &red, &green, &blue);
1394 sprintf(buffer, "%02X%02X%02X",
1395 (int) floor(0.5 + 255.0 * red),
1396 (int) floor(0.5 + 255.0 * green),
1397 (int) floor(0.5 + 255.0 * blue));
1398 Tcl_AppendResult(interp, buffer, (char *) NULL);
1399 lineLen += 6;
1400 if (lineLen > 60) {
1401 lineLen = 0;
1402 Tcl_AppendResult(interp, "\n", (char *) NULL);
1403 }
1404 }
1405 break;
1406 }
1407 }
1408 }
1409 switch (level) {
1410 case 0: sprintf(buffer, ">\n} image\n"); break;
1411 case 1: sprintf(buffer, ">\n} image\n"); break;
1412 case 2: sprintf(buffer, ">\n} false 3 colorimage\n"); break;
1413 }
1414 Tcl_AppendResult(interp, buffer, (char *) NULL);
1415 sprintf(buffer, "0 %d translate\n", rows);
1416 Tcl_AppendResult(interp, buffer, (char *) NULL);
1417 }
1418 ckfree((char *) cdata.colors);
1419 return TCL_OK;
1420 }
1421
1422 /*
1423 *--------------------------------------------------------------
1424 *
1425 * Tk_PostscriptPhoto --
1426 *
1427 * This procedure is called to output the contents of a
1428 * photo image in Postscript, using a format appropriate for
1429 * the requested postscript color mode (i.e. one byte per pixel
1430 * in gray, and three bytes per pixel in color).
1431 *
1432 * Results:
1433 * Returns a standard Tcl return value. If an error occurs
1434 * then an error message will be left in interp->result.
1435 * If no error occurs, then additional Postscript will be
1436 * appended to the interpreter's result.
1437 *
1438 * Side effects:
1439 * None.
1440 *
1441 *--------------------------------------------------------------
1442 */
1443 int
1444 Tk_PostscriptPhoto(interp, blockPtr, psInfo, width, height)
1445 Tcl_Interp *interp;
1446 Tk_PhotoImageBlock *blockPtr;
1447 Tk_PostscriptInfo psInfo;
1448 int width, height;
1449 {
1450 TkPostscriptInfo *psInfoPtr = (TkPostscriptInfo *) psInfo;
1451 int colorLevel = psInfoPtr->colorLevel;
1452 static int codeIncluded = 0;
1453
1454 unsigned char *pixelPtr;
1455 char buffer[256], cspace[40], decode[40];
1456 int bpc;
1457 int xx, yy, lineLen;
1458 float red, green, blue;
1459 int alpha;
1460 int bytesPerLine=0, maxWidth=0;
1461
1462 unsigned char opaque = 255;
1463 unsigned char *alphaPtr;
1464 int alphaOffset, alphaPitch, alphaIncr;
1465
1466 if (psInfoPtr->prepass) {
1467 codeIncluded = 0;
1468 return TCL_OK;
1469 }
1470
1471 /*
1472 * Define the "TkPhoto" function, which is a modified version
1473 * of the original "transparentimage" function posted
1474 * by [email protected] (Ian Kemmish) to comp.lang.postscript.
1475 * For a monochrome colorLevel this is a slightly different
1476 * version that uses the imagemask command instead of image.
1477 */
1478
1479 if( !codeIncluded && (colorLevel != 0) ) {
1480 /*
1481 * Color and gray-scale code.
1482 */
1483
1484 codeIncluded = !0;
1485 Tcl_AppendResult( interp,
1486 "/TkPhoto { \n",
1487 " gsave \n",
1488 " 32 dict begin \n",
1489 " /tinteger exch def \n",
1490 " /transparent 1 string def \n",
1491 " transparent 0 tinteger put \n",
1492 " /olddict exch def \n",
1493 " olddict /DataSource get dup type /filetype ne { \n",
1494 " olddict /DataSource 3 -1 roll \n",
1495 " 0 () /SubFileDecode filter put \n",
1496 " } { \n",
1497 " pop \n",
1498 " } ifelse \n",
1499 " /newdict olddict maxlength dict def \n",
1500 " olddict newdict copy pop \n",
1501 " /w newdict /Width get def \n",
1502 " /crpp newdict /Decode get length 2 idiv def \n",
1503 " /str w string def \n",
1504 " /pix w crpp mul string def \n",
1505 " /substrlen 2 w log 2 log div floor exp cvi def \n",
1506 " /substrs [ \n",
1507 " { \n",
1508 " substrlen string \n",
1509 " 0 1 substrlen 1 sub { \n",
1510 " 1 index exch tinteger put \n",
1511 " } for \n",
1512 " /substrlen substrlen 2 idiv def \n",
1513 " substrlen 0 eq {exit} if \n",
1514 " } loop \n",
1515 " ] def \n",
1516 " /h newdict /Height get def \n",
1517 " 1 w div 1 h div matrix scale \n",
1518 " olddict /ImageMatrix get exch matrix concatmatrix \n",
1519 " matrix invertmatrix concat \n",
1520 " newdict /Height 1 put \n",
1521 " newdict /DataSource pix put \n",
1522 " /mat [w 0 0 h 0 0] def \n",
1523 " newdict /ImageMatrix mat put \n",
1524 " 0 1 h 1 sub { \n",
1525 " mat 5 3 -1 roll neg put \n",
1526 " olddict /DataSource get str readstring pop pop \n",
1527 " /tail str def \n",
1528 " /x 0 def \n",
1529 " olddict /DataSource get pix readstring pop pop \n",
1530 " { \n",
1531 " tail transparent search dup /done exch not def \n",
1532 " {exch pop exch pop} if \n",
1533 " /w1 exch length def \n",
1534 " w1 0 ne { \n",
1535 " newdict /DataSource ",
1536 " pix x crpp mul w1 crpp mul getinterval put \n",
1537 " newdict /Width w1 put \n",
1538 " mat 4 x neg put \n",
1539 " /x x w1 add def \n",
1540 " newdict image \n",
1541 " /tail tail w1 tail length w1 sub getinterval def \n",
1542 " } if \n",
1543 " done {exit} if \n",
1544 " tail substrs { \n",
1545 " anchorsearch {pop} if \n",
1546 " } forall \n",
1547 " /tail exch def \n",
1548 " tail length 0 eq {exit} if \n",
1549 " /x w tail length sub def \n",
1550 " } loop \n",
1551 " } for \n",
1552 " end \n",
1553 " grestore \n",
1554 "} bind def \n\n\n", (char *) NULL);
1555 } else if( !codeIncluded && (colorLevel == 0) ) {
1556 /*
1557 * Monochrome-only code
1558 */
1559
1560 codeIncluded = !0;
1561 Tcl_AppendResult( interp,
1562 "/TkPhoto { \n",
1563 " gsave \n",
1564 " 32 dict begin \n",
1565 " /dummyInteger exch def \n",
1566 " /olddict exch def \n",
1567 " olddict /DataSource get dup type /filetype ne { \n",
1568 " olddict /DataSource 3 -1 roll \n",
1569 " 0 () /SubFileDecode filter put \n",
1570 " } { \n",
1571 " pop \n",
1572 " } ifelse \n",
1573 " /newdict olddict maxlength dict def \n",
1574 " olddict newdict copy pop \n",
1575 " /w newdict /Width get def \n",
1576 " /pix w 7 add 8 idiv string def \n",
1577 " /h newdict /Height get def \n",
1578 " 1 w div 1 h div matrix scale \n",
1579 " olddict /ImageMatrix get exch matrix concatmatrix \n",
1580 " matrix invertmatrix concat \n",
1581 " newdict /Height 1 put \n",
1582 " newdict /DataSource pix put \n",
1583 " /mat [w 0 0 h 0 0] def \n",
1584 " newdict /ImageMatrix mat put \n",
1585 " 0 1 h 1 sub { \n",
1586 " mat 5 3 -1 roll neg put \n",
1587 " 0.000 0.000 0.000 setrgbcolor \n",
1588 " olddict /DataSource get pix readstring pop pop \n",
1589 " newdict /DataSource pix put \n",
1590 " newdict imagemask \n",
1591 " 1.000 1.000 1.000 setrgbcolor \n",
1592 " olddict /DataSource get pix readstring pop pop \n",
1593 " newdict /DataSource pix put \n",
1594 " newdict imagemask \n",
1595 " } for \n",
1596 " end \n",
1597 " grestore \n",
1598 "} bind def \n\n\n", (char *) NULL);
1599 }
1600
1601 /*
1602 * Check that at least one row of the image can be represented
1603 * with a string less than 64 KB long (this is a limit in the
1604 * Postscript interpreter).
1605 */
1606
1607 switch (colorLevel)
1608 {
1609 case 0: bytesPerLine = (width + 7) / 8; maxWidth = 240000; break;
1610 case 1: bytesPerLine = width; maxWidth = 60000; break;
1611 case 2: bytesPerLine = 3 * width; maxWidth = 20000; break;
1612 }
1613 if (bytesPerLine > 60000) {
1614 Tcl_ResetResult(interp);
1615 sprintf(buffer,
1616 "Can't generate Postscript for images more than %d pixels wide",
1617 maxWidth);
1618 Tcl_AppendResult(interp, buffer, (char *) NULL);
1619 return TCL_ERROR;
1620 }
1621
1622 /*
1623 * Set up the postscript code except for the image-data stream.
1624 */
1625
1626 switch (colorLevel) {
1627 case 0:
1628 strcpy( cspace, "/DeviceGray");
1629 strcpy( decode, "[1 0]");
1630 bpc = 1;
1631 break;
1632 case 1:
1633 strcpy( cspace, "/DeviceGray");
1634 strcpy( decode, "[0 1]");
1635 bpc = 8;
1636 break;
1637 default:
1638 strcpy( cspace, "/DeviceRGB");
1639 strcpy( decode, "[0 1 0 1 0 1]");
1640 bpc = 8;
1641 break;
1642 }
1643
1644
1645 Tcl_AppendResult(interp,
1646 cspace, " setcolorspace\n\n", (char *) NULL);
1647
1648 sprintf(buffer,
1649 " /Width %d\n /Height %d\n /BitsPerComponent %d\n",
1650 width, height, bpc);
1651 Tcl_AppendResult(interp,
1652 "<<\n /ImageType 1\n", buffer,
1653 " /DataSource currentfile",
1654 " /ASCIIHexDecode filter\n", (char *) NULL);
1655
1656
1657 sprintf(buffer,
1658 " /ImageMatrix [1 0 0 -1 0 %d]\n", height);
1659 Tcl_AppendResult(interp, buffer,
1660 " /Decode ", decode, "\n>>\n1 TkPhoto\n", (char *) NULL);
1661
1662
1663 /*
1664 * Check the PhotoImageBlock information.
1665 * We assume that:
1666 * if pixelSize is 1,2 or 4, the image is R,G,B,A;
1667 * if pixelSize is 3, the image is R,G,B and offset[3] is bogus.
1668 */
1669
1670 if (blockPtr->pixelSize == 3) {
1671 /*
1672 * No alpha information: the whole image is opaque.
1673 */
1674
1675 alphaPtr = &opaque;
1676 alphaPitch = alphaIncr = alphaOffset = 0;
1677 } else {
1678 /*
1679 * Set up alpha handling.
1680 */
1681
1682 alphaPtr = blockPtr->pixelPtr;
1683 alphaPitch = blockPtr->pitch;
1684 alphaIncr = blockPtr->pixelSize;
1685 alphaOffset = blockPtr->offset[3];
1686 }
1687
1688
1689 for (yy = 0, lineLen=0; yy < height; yy++) {
1690 switch (colorLevel) {
1691 case 0: {
1692 /*
1693 * Generate data for image in monochrome mode.
1694 * No attempt at dithering is made--instead, just
1695 * set a threshold.
1696 * To handle transparecies we need to output two lines:
1697 * one for the black pixels, one for the white ones.
1698 */
1699
1700 unsigned char mask=0x80;
1701 unsigned char data=0x00;
1702 for (xx = 0; xx< width; xx ++) {
1703 pixelPtr = blockPtr->pixelPtr
1704 + (yy * blockPtr->pitch)
1705 + (xx *blockPtr->pixelSize);
1706
1707 red = pixelPtr[blockPtr->offset[0]];
1708 green = pixelPtr[blockPtr->offset[1]];
1709 blue = pixelPtr[blockPtr->offset[2]];
1710
1711 alpha = *(alphaPtr + (yy * alphaPitch)
1712 + (xx * alphaIncr) + alphaOffset);
1713
1714 /*
1715 * If pixel is less than threshold, then it is black.
1716 */
1717
1718 if ((alpha != 0) &&
1719 ( 0.3086 * red
1720 + 0.6094 * green
1721 + 0.082 * blue < 128)) {
1722 data |= mask;
1723 }
1724 mask >>= 1;
1725 if (mask == 0) {
1726 sprintf(buffer, "%02X", data);
1727 Tcl_AppendResult(interp, buffer, (char *) NULL);
1728 lineLen += 2;
1729 if (lineLen >= 60) {
1730 lineLen = 0;
1731 Tcl_AppendResult(interp, "\n", (char *) NULL);
1732 }
1733 mask=0x80;
1734 data=0x00;
1735 }
1736 }
1737 if ((width % 8) != 0) {
1738 sprintf(buffer, "%02X", data);
1739 Tcl_AppendResult(interp, buffer, (char *) NULL);
1740 mask=0x80;
1741 data=0x00;
1742 }
1743
1744 mask=0x80;
1745 data=0x00;
1746 for (xx = 0; xx< width; xx ++) {
1747 pixelPtr = blockPtr->pixelPtr
1748 + (yy * blockPtr->pitch)
1749 + (xx *blockPtr->pixelSize);
1750
1751 red = pixelPtr[blockPtr->offset[0]];
1752 green = pixelPtr[blockPtr->offset[1]];
1753 blue = pixelPtr[blockPtr->offset[2]];
1754
1755 alpha = *(alphaPtr + (yy * alphaPitch)
1756 + (xx * alphaIncr) + alphaOffset);
1757
1758 /*
1759 * If pixel is greater than threshold, then it is white.
1760 */
1761
1762 if ((alpha != 0) &&
1763 ( 0.3086 * red
1764 + 0.6094 * green
1765 + 0.082 * blue >= 128)) {
1766 data |= mask;
1767 }
1768 mask >>= 1;
1769 if (mask == 0) {
1770 sprintf(buffer, "%02X", data);
1771 Tcl_AppendResult(interp, buffer, (char *) NULL);
1772 lineLen += 2;
1773 if (lineLen >= 60) {
1774 lineLen = 0;
1775 Tcl_AppendResult(interp, "\n", (char *) NULL);
1776 }
1777 mask=0x80;
1778 data=0x00;
1779 }
1780 }
1781 if ((width % 8) != 0) {
1782 sprintf(buffer, "%02X", data);
1783 Tcl_AppendResult(interp, buffer, (char *) NULL);
1784 mask=0x80;
1785 data=0x00;
1786 }
1787 break;
1788 }
1789 case 1: {
1790 /*
1791 * Generate transparency data.
1792 * We must prevent a transparent value of 0
1793 * because of a bug in some HP printers.
1794 */
1795
1796 for (xx = 0; xx < width; xx ++) {
1797 alpha = *(alphaPtr + (yy * alphaPitch)
1798 + (xx * alphaIncr) + alphaOffset);
1799 sprintf(buffer, "%02X", alpha | 0x01);
1800 Tcl_AppendResult(interp, buffer, (char *) NULL);
1801 lineLen += 2;
1802 if (lineLen >= 60) {
1803 lineLen = 0;
1804 Tcl_AppendResult(interp, "\n", (char *) NULL);
1805 }
1806 }
1807
1808
1809 /*
1810 * Generate data in gray mode--in this case, take a
1811 * weighted sum of the red, green, and blue values.
1812 */
1813
1814 for (xx = 0; xx < width; xx ++) {
1815 pixelPtr = blockPtr->pixelPtr
1816 + (yy * blockPtr->pitch)
1817 + (xx *blockPtr->pixelSize);
1818
1819 red = pixelPtr[blockPtr->offset[0]];
1820 green = pixelPtr[blockPtr->offset[1]];
1821 blue = pixelPtr[blockPtr->offset[2]];
1822
1823 sprintf(buffer, "%02X", (int) floor(0.5 +
1824 ( 0.3086 * red + 0.6094 * green + 0.0820 * blue)));
1825 Tcl_AppendResult(interp, buffer, (char *) NULL);
1826 lineLen += 2;
1827 if (lineLen >= 60) {
1828 lineLen = 0;
1829 Tcl_AppendResult(interp, "\n", (char *) NULL);
1830 }
1831 }
1832 break;
1833 }
1834 default: {
1835 /*
1836 * Generate transparency data.
1837 * We must prevent a transparent value of 0
1838 * because of a bug in some HP printers.
1839 */
1840
1841 for (xx = 0; xx < width; xx ++) {
1842 alpha = *(alphaPtr + (yy * alphaPitch)
1843 + (xx * alphaIncr) + alphaOffset);
1844 sprintf(buffer, "%02X", alpha | 0x01);
1845 Tcl_AppendResult(interp, buffer, (char *) NULL);
1846 lineLen += 2;
1847 if (lineLen >= 60) {
1848 lineLen = 0;
1849 Tcl_AppendResult(interp, "\n", (char *) NULL);
1850 }
1851 }
1852
1853
1854 /*
1855 * Finally, color mode. Here, just output the red, green,
1856 * and blue values directly.
1857 */
1858
1859 for (xx = 0; xx < width; xx ++) {
1860 pixelPtr = blockPtr->pixelPtr
1861 + (yy * blockPtr->pitch)
1862 + (xx *blockPtr->pixelSize);
1863
1864 sprintf(buffer, "%02X%02X%02X",
1865 pixelPtr[blockPtr->offset[0]],
1866 pixelPtr[blockPtr->offset[1]],
1867 pixelPtr[blockPtr->offset[2]]);
1868 Tcl_AppendResult(interp, buffer, (char *) NULL);
1869 lineLen += 6;
1870 if (lineLen >= 60) {
1871 lineLen = 0;
1872 Tcl_AppendResult(interp, "\n", (char *) NULL);
1873 }
1874 }
1875 break;
1876 }
1877 }
1878 }
1879
1880 Tcl_AppendResult(interp, ">\n", (char *) NULL);
1881 return TCL_OK;
1882 }
1883
1884
1885
1886
1887
1888