Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
att
GitHub Repository: att/ast
Path: blob/master/src/lib/libtk/generic/tkCanvBmap.c
1810 views
1
/*
2
* tkCanvBmap.c --
3
*
4
* This file implements bitmap items for canvas widgets.
5
*
6
* Copyright (c) 1992-1994 The Regents of the University of California.
7
* Copyright (c) 1994-1995 Sun Microsystems, Inc.
8
*
9
* See the file "license.terms" for information on usage and redistribution
10
* of this file, and for a DISCLAIMER OF ALL WARRANTIES.
11
*
12
* SCCS: @(#) tkCanvBmap.c 1.29 96/02/17 16:59:10
13
*/
14
15
#include "tkInt.h"
16
#include "tkCanvas.h"
17
18
/*
19
* The structure below defines the record for each bitmap item.
20
*/
21
22
typedef struct BitmapItem {
23
Tk_Item header; /* Generic stuff that's the same for all
24
* types. MUST BE FIRST IN STRUCTURE. */
25
double x, y; /* Coordinates of positioning point for
26
* bitmap. */
27
Tk_Anchor anchor; /* Where to anchor bitmap relative to
28
* (x,y). */
29
Pixmap bitmap; /* Bitmap to display in window. */
30
XColor *fgColor; /* Foreground color to use for bitmap. */
31
XColor *bgColor; /* Background color to use for bitmap. */
32
GC gc; /* Graphics context to use for drawing
33
* bitmap on screen. */
34
} BitmapItem;
35
36
/*
37
* Information used for parsing configuration specs:
38
*/
39
40
static Tk_CustomOption tagsOption = {Tk_CanvasTagsParseProc,
41
Tk_CanvasTagsPrintProc, (ClientData) NULL
42
};
43
44
static Tk_ConfigSpec configSpecs[] = {
45
{TK_CONFIG_ANCHOR, "-anchor", (char *) NULL, (char *) NULL,
46
"center", Tk_Offset(BitmapItem, anchor), TK_CONFIG_DONT_SET_DEFAULT},
47
{TK_CONFIG_COLOR, "-background", (char *) NULL, (char *) NULL,
48
(char *) NULL, Tk_Offset(BitmapItem, bgColor), TK_CONFIG_NULL_OK},
49
{TK_CONFIG_BITMAP, "-bitmap", (char *) NULL, (char *) NULL,
50
(char *) NULL, Tk_Offset(BitmapItem, bitmap), TK_CONFIG_NULL_OK},
51
{TK_CONFIG_COLOR, "-foreground", (char *) NULL, (char *) NULL,
52
"black", Tk_Offset(BitmapItem, fgColor), 0},
53
{TK_CONFIG_CUSTOM, "-tags", (char *) NULL, (char *) NULL,
54
(char *) NULL, 0, TK_CONFIG_NULL_OK, &tagsOption},
55
{TK_CONFIG_END, (char *) NULL, (char *) NULL, (char *) NULL,
56
(char *) NULL, 0, 0}
57
};
58
59
/*
60
* Prototypes for procedures defined in this file:
61
*/
62
63
static int BitmapCoords _ANSI_ARGS_((Tcl_Interp *interp,
64
Tk_Canvas canvas, Tk_Item *itemPtr, int argc,
65
char **argv));
66
static int BitmapToArea _ANSI_ARGS_((Tk_Canvas canvas,
67
Tk_Item *itemPtr, double *rectPtr));
68
static double BitmapToPoint _ANSI_ARGS_((Tk_Canvas canvas,
69
Tk_Item *itemPtr, double *coordPtr));
70
static int BitmapToPostscript _ANSI_ARGS_((Tcl_Interp *interp,
71
Tk_Canvas canvas, Tk_Item *itemPtr, int prepass));
72
static void ComputeBitmapBbox _ANSI_ARGS_((Tk_Canvas canvas,
73
BitmapItem *bmapPtr));
74
static int ConfigureBitmap _ANSI_ARGS_((Tcl_Interp *interp,
75
Tk_Canvas canvas, Tk_Item *itemPtr, int argc,
76
char **argv, int flags));
77
static int CreateBitmap _ANSI_ARGS_((Tcl_Interp *interp,
78
Tk_Canvas canvas, struct Tk_Item *itemPtr,
79
int argc, char **argv));
80
static void DeleteBitmap _ANSI_ARGS_((Tk_Canvas canvas,
81
Tk_Item *itemPtr, Display *display));
82
static void DisplayBitmap _ANSI_ARGS_((Tk_Canvas canvas,
83
Tk_Item *itemPtr, Display *display, Drawable dst,
84
int x, int y, int width, int height));
85
static void ScaleBitmap _ANSI_ARGS_((Tk_Canvas canvas,
86
Tk_Item *itemPtr, double originX, double originY,
87
double scaleX, double scaleY));
88
static void TranslateBitmap _ANSI_ARGS_((Tk_Canvas canvas,
89
Tk_Item *itemPtr, double deltaX, double deltaY));
90
91
/*
92
* The structures below defines the bitmap item type in terms of
93
* procedures that can be invoked by generic item code.
94
*/
95
96
Tk_ItemType tkBitmapType = {
97
"bitmap", /* name */
98
sizeof(BitmapItem), /* itemSize */
99
CreateBitmap, /* createProc */
100
configSpecs, /* configSpecs */
101
ConfigureBitmap, /* configureProc */
102
BitmapCoords, /* coordProc */
103
DeleteBitmap, /* deleteProc */
104
DisplayBitmap, /* displayProc */
105
0, /* alwaysRedraw */
106
BitmapToPoint, /* pointProc */
107
BitmapToArea, /* areaProc */
108
BitmapToPostscript, /* postscriptProc */
109
ScaleBitmap, /* scaleProc */
110
TranslateBitmap, /* translateProc */
111
(Tk_ItemIndexProc *) NULL, /* indexProc */
112
(Tk_ItemCursorProc *) NULL, /* icursorProc */
113
(Tk_ItemSelectionProc *) NULL, /* selectionProc */
114
(Tk_ItemInsertProc *) NULL, /* insertProc */
115
(Tk_ItemDCharsProc *) NULL, /* dTextProc */
116
(Tk_ItemType *) NULL /* nextPtr */
117
};
118
119
/*
120
*--------------------------------------------------------------
121
*
122
* CreateBitmap --
123
*
124
* This procedure is invoked to create a new bitmap
125
* item in a canvas.
126
*
127
* Results:
128
* A standard Tcl return value. If an error occurred in
129
* creating the item, then an error message is left in
130
* interp->result; in this case itemPtr is left uninitialized,
131
* so it can be safely freed by the caller.
132
*
133
* Side effects:
134
* A new bitmap item is created.
135
*
136
*--------------------------------------------------------------
137
*/
138
139
static int
140
CreateBitmap(interp, canvas, itemPtr, argc, argv)
141
Tcl_Interp *interp; /* Interpreter for error reporting. */
142
Tk_Canvas canvas; /* Canvas to hold new item. */
143
Tk_Item *itemPtr; /* Record to hold new item; header
144
* has been initialized by caller. */
145
int argc; /* Number of arguments in argv. */
146
char **argv; /* Arguments describing rectangle. */
147
{
148
BitmapItem *bmapPtr = (BitmapItem *) itemPtr;
149
150
if (argc < 2) {
151
Tcl_AppendResult(interp, "wrong # args: should be \"",
152
Tk_PathName(Tk_CanvasTkwin(canvas)), " create ",
153
itemPtr->typePtr->name, " x y ?options?\"",
154
(char *) NULL);
155
return TCL_ERROR;
156
}
157
158
/*
159
* Initialize item's record.
160
*/
161
162
bmapPtr->anchor = TK_ANCHOR_CENTER;
163
bmapPtr->bitmap = None;
164
bmapPtr->fgColor = NULL;
165
bmapPtr->bgColor = NULL;
166
bmapPtr->gc = None;
167
168
/*
169
* Process the arguments to fill in the item record.
170
*/
171
172
if ((Tk_CanvasGetCoord(interp, canvas, argv[0], &bmapPtr->x) != TCL_OK)
173
|| (Tk_CanvasGetCoord(interp, canvas, argv[1], &bmapPtr->y)
174
!= TCL_OK)) {
175
return TCL_ERROR;
176
}
177
178
if (ConfigureBitmap(interp, canvas, itemPtr, argc-2, argv+2, 0) != TCL_OK) {
179
DeleteBitmap(canvas, itemPtr, Tk_Display(Tk_CanvasTkwin(canvas)));
180
return TCL_ERROR;
181
}
182
return TCL_OK;
183
}
184
185
/*
186
*--------------------------------------------------------------
187
*
188
* BitmapCoords --
189
*
190
* This procedure is invoked to process the "coords" widget
191
* command on bitmap items. See the user documentation for
192
* details on what it does.
193
*
194
* Results:
195
* Returns TCL_OK or TCL_ERROR, and sets interp->result.
196
*
197
* Side effects:
198
* The coordinates for the given item may be changed.
199
*
200
*--------------------------------------------------------------
201
*/
202
203
static int
204
BitmapCoords(interp, canvas, itemPtr, argc, argv)
205
Tcl_Interp *interp; /* Used for error reporting. */
206
Tk_Canvas canvas; /* Canvas containing item. */
207
Tk_Item *itemPtr; /* Item whose coordinates are to be
208
* read or modified. */
209
int argc; /* Number of coordinates supplied in
210
* argv. */
211
char **argv; /* Array of coordinates: x1, y1,
212
* x2, y2, ... */
213
{
214
BitmapItem *bmapPtr = (BitmapItem *) itemPtr;
215
char x[TCL_DOUBLE_SPACE], y[TCL_DOUBLE_SPACE];
216
217
if (argc == 0) {
218
Tcl_PrintDouble(interp, bmapPtr->x, x);
219
Tcl_PrintDouble(interp, bmapPtr->y, y);
220
Tcl_AppendResult(interp, x, " ", y, (char *) NULL);
221
} else if (argc == 2) {
222
if ((Tk_CanvasGetCoord(interp, canvas, argv[0], &bmapPtr->x) != TCL_OK)
223
|| (Tk_CanvasGetCoord(interp, canvas, argv[1], &bmapPtr->y)
224
!= TCL_OK)) {
225
return TCL_ERROR;
226
}
227
ComputeBitmapBbox(canvas, bmapPtr);
228
} else {
229
sprintf(interp->result,
230
"wrong # coordinates: expected 0 or 2, got %d", argc);
231
return TCL_ERROR;
232
}
233
return TCL_OK;
234
}
235
236
/*
237
*--------------------------------------------------------------
238
*
239
* ConfigureBitmap --
240
*
241
* This procedure is invoked to configure various aspects
242
* of a bitmap item, such as its anchor position.
243
*
244
* Results:
245
* A standard Tcl result code. If an error occurs, then
246
* an error message is left in interp->result.
247
*
248
* Side effects:
249
* Configuration information may be set for itemPtr.
250
*
251
*--------------------------------------------------------------
252
*/
253
254
static int
255
ConfigureBitmap(interp, canvas, itemPtr, argc, argv, flags)
256
Tcl_Interp *interp; /* Used for error reporting. */
257
Tk_Canvas canvas; /* Canvas containing itemPtr. */
258
Tk_Item *itemPtr; /* Bitmap item to reconfigure. */
259
int argc; /* Number of elements in argv. */
260
char **argv; /* Arguments describing things to configure. */
261
int flags; /* Flags to pass to Tk_ConfigureWidget. */
262
{
263
BitmapItem *bmapPtr = (BitmapItem *) itemPtr;
264
XGCValues gcValues;
265
GC newGC;
266
Tk_Window tkwin;
267
unsigned long mask;
268
269
tkwin = Tk_CanvasTkwin(canvas);
270
if (Tk_ConfigureWidget(interp, tkwin, configSpecs, argc, argv,
271
(char *) bmapPtr, flags) != TCL_OK) {
272
return TCL_ERROR;
273
}
274
275
/*
276
* A few of the options require additional processing, such as those
277
* that determine the graphics context.
278
*/
279
280
gcValues.foreground = bmapPtr->fgColor->pixel;
281
mask = GCForeground;
282
if (bmapPtr->bgColor != NULL) {
283
gcValues.background = bmapPtr->bgColor->pixel;
284
mask |= GCBackground;
285
} else {
286
gcValues.clip_mask = bmapPtr->bitmap;
287
mask |= GCClipMask;
288
}
289
newGC = Tk_GetGC(tkwin, mask, &gcValues);
290
if (bmapPtr->gc != None) {
291
Tk_FreeGC(Tk_Display(tkwin), bmapPtr->gc);
292
}
293
bmapPtr->gc = newGC;
294
295
ComputeBitmapBbox(canvas, bmapPtr);
296
297
return TCL_OK;
298
}
299
300
/*
301
*--------------------------------------------------------------
302
*
303
* DeleteBitmap --
304
*
305
* This procedure is called to clean up the data structure
306
* associated with a bitmap item.
307
*
308
* Results:
309
* None.
310
*
311
* Side effects:
312
* Resources associated with itemPtr are released.
313
*
314
*--------------------------------------------------------------
315
*/
316
317
static void
318
DeleteBitmap(canvas, itemPtr, display)
319
Tk_Canvas canvas; /* Info about overall canvas widget. */
320
Tk_Item *itemPtr; /* Item that is being deleted. */
321
Display *display; /* Display containing window for
322
* canvas. */
323
{
324
BitmapItem *bmapPtr = (BitmapItem *) itemPtr;
325
326
if (bmapPtr->bitmap != None) {
327
Tk_FreeBitmap(display, bmapPtr->bitmap);
328
}
329
if (bmapPtr->fgColor != NULL) {
330
Tk_FreeColor(bmapPtr->fgColor);
331
}
332
if (bmapPtr->bgColor != NULL) {
333
Tk_FreeColor(bmapPtr->bgColor);
334
}
335
if (bmapPtr->gc != NULL) {
336
Tk_FreeGC(display, bmapPtr->gc);
337
}
338
}
339
340
/*
341
*--------------------------------------------------------------
342
*
343
* ComputeBitmapBbox --
344
*
345
* This procedure is invoked to compute the bounding box of
346
* all the pixels that may be drawn as part of a bitmap item.
347
* This procedure is where the child bitmap's placement is
348
* computed.
349
*
350
* Results:
351
* None.
352
*
353
* Side effects:
354
* The fields x1, y1, x2, and y2 are updated in the header
355
* for itemPtr.
356
*
357
*--------------------------------------------------------------
358
*/
359
360
/* ARGSUSED */
361
static void
362
ComputeBitmapBbox(canvas, bmapPtr)
363
Tk_Canvas canvas; /* Canvas that contains item. */
364
BitmapItem *bmapPtr; /* Item whose bbox is to be
365
* recomputed. */
366
{
367
int width, height;
368
int x, y;
369
370
x = bmapPtr->x + ((bmapPtr->x >= 0) ? 0.5 : - 0.5);
371
y = bmapPtr->y + ((bmapPtr->y >= 0) ? 0.5 : - 0.5);
372
373
if (bmapPtr->bitmap == None) {
374
bmapPtr->header.x1 = bmapPtr->header.x2 = x;
375
bmapPtr->header.y1 = bmapPtr->header.y2 = y;
376
return;
377
}
378
379
/*
380
* Compute location and size of bitmap, using anchor information.
381
*/
382
383
Tk_SizeOfBitmap(Tk_Display(Tk_CanvasTkwin(canvas)), bmapPtr->bitmap,
384
&width, &height);
385
switch (bmapPtr->anchor) {
386
case TK_ANCHOR_N:
387
x -= width/2;
388
break;
389
case TK_ANCHOR_NE:
390
x -= width;
391
break;
392
case TK_ANCHOR_E:
393
x -= width;
394
y -= height/2;
395
break;
396
case TK_ANCHOR_SE:
397
x -= width;
398
y -= height;
399
break;
400
case TK_ANCHOR_S:
401
x -= width/2;
402
y -= height;
403
break;
404
case TK_ANCHOR_SW:
405
y -= height;
406
break;
407
case TK_ANCHOR_W:
408
y -= height/2;
409
break;
410
case TK_ANCHOR_NW:
411
break;
412
case TK_ANCHOR_CENTER:
413
x -= width/2;
414
y -= height/2;
415
break;
416
}
417
418
/*
419
* Store the information in the item header.
420
*/
421
422
bmapPtr->header.x1 = x;
423
bmapPtr->header.y1 = y;
424
bmapPtr->header.x2 = x + width;
425
bmapPtr->header.y2 = y + height;
426
}
427
428
/*
429
*--------------------------------------------------------------
430
*
431
* DisplayBitmap --
432
*
433
* This procedure is invoked to draw a bitmap item in a given
434
* drawable.
435
*
436
* Results:
437
* None.
438
*
439
* Side effects:
440
* ItemPtr is drawn in drawable using the transformation
441
* information in canvas.
442
*
443
*--------------------------------------------------------------
444
*/
445
446
static void
447
DisplayBitmap(canvas, itemPtr, display, drawable, x, y, width, height)
448
Tk_Canvas canvas; /* Canvas that contains item. */
449
Tk_Item *itemPtr; /* Item to be displayed. */
450
Display *display; /* Display on which to draw item. */
451
Drawable drawable; /* Pixmap or window in which to draw
452
* item. */
453
int x, y, width, height; /* Describes region of canvas that
454
* must be redisplayed (not used). */
455
{
456
BitmapItem *bmapPtr = (BitmapItem *) itemPtr;
457
int bmapX, bmapY, bmapWidth, bmapHeight;
458
short drawableX, drawableY;
459
460
/*
461
* If the area being displayed doesn't cover the whole bitmap,
462
* then only redisplay the part of the bitmap that needs
463
* redisplay.
464
*/
465
466
if (bmapPtr->bitmap != None) {
467
if (x > bmapPtr->header.x1) {
468
bmapX = x - bmapPtr->header.x1;
469
bmapWidth = bmapPtr->header.x2 - x;
470
} else {
471
bmapX = 0;
472
if ((x+width) < bmapPtr->header.x2) {
473
bmapWidth = x + width - bmapPtr->header.x1;
474
} else {
475
bmapWidth = bmapPtr->header.x2 - bmapPtr->header.x1;
476
}
477
}
478
if (y > bmapPtr->header.y1) {
479
bmapY = y - bmapPtr->header.y1;
480
bmapHeight = bmapPtr->header.y2 - y;
481
} else {
482
bmapY = 0;
483
if ((y+height) < bmapPtr->header.y2) {
484
bmapHeight = y + height - bmapPtr->header.y1;
485
} else {
486
bmapHeight = bmapPtr->header.y2 - bmapPtr->header.y1;
487
}
488
}
489
Tk_CanvasDrawableCoords(canvas,
490
(double) (bmapPtr->header.x1 + bmapX),
491
(double) (bmapPtr->header.y1 + bmapY),
492
&drawableX, &drawableY);
493
494
/*
495
* Must modify the mask origin within the graphics context
496
* to line up with the bitmap's origin (in order to make
497
* bitmaps with "-background {}" work right).
498
*/
499
500
XSetClipOrigin(display, bmapPtr->gc, drawableX - bmapX,
501
drawableY - bmapY);
502
XCopyPlane(display, bmapPtr->bitmap, drawable,
503
bmapPtr->gc, bmapX, bmapY, (unsigned int) bmapWidth,
504
(unsigned int) bmapHeight, drawableX, drawableY, 1);
505
}
506
}
507
508
/*
509
*--------------------------------------------------------------
510
*
511
* BitmapToPoint --
512
*
513
* Computes the distance from a given point to a given
514
* rectangle, in canvas units.
515
*
516
* Results:
517
* The return value is 0 if the point whose x and y coordinates
518
* are coordPtr[0] and coordPtr[1] is inside the bitmap. If the
519
* point isn't inside the bitmap then the return value is the
520
* distance from the point to the bitmap.
521
*
522
* Side effects:
523
* None.
524
*
525
*--------------------------------------------------------------
526
*/
527
528
/* ARGSUSED */
529
static double
530
BitmapToPoint(canvas, itemPtr, coordPtr)
531
Tk_Canvas canvas; /* Canvas containing item. */
532
Tk_Item *itemPtr; /* Item to check against point. */
533
double *coordPtr; /* Pointer to x and y coordinates. */
534
{
535
BitmapItem *bmapPtr = (BitmapItem *) itemPtr;
536
double x1, x2, y1, y2, xDiff, yDiff;
537
538
x1 = bmapPtr->header.x1;
539
y1 = bmapPtr->header.y1;
540
x2 = bmapPtr->header.x2;
541
y2 = bmapPtr->header.y2;
542
543
/*
544
* Point is outside rectangle.
545
*/
546
547
if (coordPtr[0] < x1) {
548
xDiff = x1 - coordPtr[0];
549
} else if (coordPtr[0] > x2) {
550
xDiff = coordPtr[0] - x2;
551
} else {
552
xDiff = 0;
553
}
554
555
if (coordPtr[1] < y1) {
556
yDiff = y1 - coordPtr[1];
557
} else if (coordPtr[1] > y2) {
558
yDiff = coordPtr[1] - y2;
559
} else {
560
yDiff = 0;
561
}
562
563
return hypot(xDiff, yDiff);
564
}
565
566
/*
567
*--------------------------------------------------------------
568
*
569
* BitmapToArea --
570
*
571
* This procedure is called to determine whether an item
572
* lies entirely inside, entirely outside, or overlapping
573
* a given rectangle.
574
*
575
* Results:
576
* -1 is returned if the item is entirely outside the area
577
* given by rectPtr, 0 if it overlaps, and 1 if it is entirely
578
* inside the given area.
579
*
580
* Side effects:
581
* None.
582
*
583
*--------------------------------------------------------------
584
*/
585
586
/* ARGSUSED */
587
static int
588
BitmapToArea(canvas, itemPtr, rectPtr)
589
Tk_Canvas canvas; /* Canvas containing item. */
590
Tk_Item *itemPtr; /* Item to check against rectangle. */
591
double *rectPtr; /* Pointer to array of four coordinates
592
* (x1, y1, x2, y2) describing rectangular
593
* area. */
594
{
595
BitmapItem *bmapPtr = (BitmapItem *) itemPtr;
596
597
if ((rectPtr[2] <= bmapPtr->header.x1)
598
|| (rectPtr[0] >= bmapPtr->header.x2)
599
|| (rectPtr[3] <= bmapPtr->header.y1)
600
|| (rectPtr[1] >= bmapPtr->header.y2)) {
601
return -1;
602
}
603
if ((rectPtr[0] <= bmapPtr->header.x1)
604
&& (rectPtr[1] <= bmapPtr->header.y1)
605
&& (rectPtr[2] >= bmapPtr->header.x2)
606
&& (rectPtr[3] >= bmapPtr->header.y2)) {
607
return 1;
608
}
609
return 0;
610
}
611
612
/*
613
*--------------------------------------------------------------
614
*
615
* ScaleBitmap --
616
*
617
* This procedure is invoked to rescale a bitmap item in a
618
* canvas. It is one of the standard item procedures for
619
* bitmap items, and is invoked by the generic canvas code.
620
*
621
* Results:
622
* None.
623
*
624
* Side effects:
625
* The item referred to by itemPtr is rescaled so that the
626
* following transformation is applied to all point coordinates:
627
* x' = originX + scaleX*(x-originX)
628
* y' = originY + scaleY*(y-originY)
629
*
630
*--------------------------------------------------------------
631
*/
632
633
static void
634
ScaleBitmap(canvas, itemPtr, originX, originY, scaleX, scaleY)
635
Tk_Canvas canvas; /* Canvas containing rectangle. */
636
Tk_Item *itemPtr; /* Rectangle to be scaled. */
637
double originX, originY; /* Origin about which to scale item. */
638
double scaleX; /* Amount to scale in X direction. */
639
double scaleY; /* Amount to scale in Y direction. */
640
{
641
BitmapItem *bmapPtr = (BitmapItem *) itemPtr;
642
643
bmapPtr->x = originX + scaleX*(bmapPtr->x - originX);
644
bmapPtr->y = originY + scaleY*(bmapPtr->y - originY);
645
ComputeBitmapBbox(canvas, bmapPtr);
646
}
647
648
/*
649
*--------------------------------------------------------------
650
*
651
* TranslateBitmap --
652
*
653
* This procedure is called to move an item by a given amount.
654
*
655
* Results:
656
* None.
657
*
658
* Side effects:
659
* The position of the item is offset by (xDelta, yDelta), and
660
* the bounding box is updated in the generic part of the item
661
* structure.
662
*
663
*--------------------------------------------------------------
664
*/
665
666
static void
667
TranslateBitmap(canvas, itemPtr, deltaX, deltaY)
668
Tk_Canvas canvas; /* Canvas containing item. */
669
Tk_Item *itemPtr; /* Item that is being moved. */
670
double deltaX, deltaY; /* Amount by which item is to be
671
* moved. */
672
{
673
BitmapItem *bmapPtr = (BitmapItem *) itemPtr;
674
675
bmapPtr->x += deltaX;
676
bmapPtr->y += deltaY;
677
ComputeBitmapBbox(canvas, bmapPtr);
678
}
679
680
/*
681
*--------------------------------------------------------------
682
*
683
* BitmapToPostscript --
684
*
685
* This procedure is called to generate Postscript for
686
* bitmap items.
687
*
688
* Results:
689
* The return value is a standard Tcl result. If an error
690
* occurs in generating Postscript then an error message is
691
* left in interp->result, replacing whatever used to be there.
692
* If no error occurs, then Postscript for the item is appended
693
* to the result.
694
*
695
* Side effects:
696
* None.
697
*
698
*--------------------------------------------------------------
699
*/
700
701
static int
702
BitmapToPostscript(interp, canvas, itemPtr, prepass)
703
Tcl_Interp *interp; /* Leave Postscript or error message
704
* here. */
705
Tk_Canvas canvas; /* Information about overall canvas. */
706
Tk_Item *itemPtr; /* Item for which Postscript is
707
* wanted. */
708
int prepass; /* 1 means this is a prepass to
709
* collect font information; 0 means
710
* final Postscript is being created. */
711
{
712
BitmapItem *bmapPtr = (BitmapItem *) itemPtr;
713
double x, y;
714
int width, height, rowsAtOnce, rowsThisTime;
715
int curRow;
716
char buffer[200];
717
718
if (bmapPtr->bitmap == None) {
719
return TCL_OK;
720
}
721
722
/*
723
* Compute the coordinates of the lower-left corner of the bitmap,
724
* taking into account the anchor position for the bitmp.
725
*/
726
727
x = bmapPtr->x;
728
y = Tk_CanvasPsY(canvas, bmapPtr->y);
729
Tk_SizeOfBitmap(Tk_Display(Tk_CanvasTkwin(canvas)), bmapPtr->bitmap,
730
&width, &height);
731
switch (bmapPtr->anchor) {
732
case TK_ANCHOR_NW: y -= height; break;
733
case TK_ANCHOR_N: x -= width/2.0; y -= height; break;
734
case TK_ANCHOR_NE: x -= width; y -= height; break;
735
case TK_ANCHOR_E: x -= width; y -= height/2.0; break;
736
case TK_ANCHOR_SE: x -= width; break;
737
case TK_ANCHOR_S: x -= width/2.0; break;
738
case TK_ANCHOR_SW: break;
739
case TK_ANCHOR_W: y -= height/2.0; break;
740
case TK_ANCHOR_CENTER: x -= width/2.0; y -= height/2.0; break;
741
}
742
743
/*
744
* Color the background, if there is one.
745
*/
746
747
if (bmapPtr->bgColor != NULL) {
748
sprintf(buffer,
749
"%.15g %.15g moveto %d 0 rlineto 0 %d rlineto %d %s\n",
750
x, y, width, height, -width,"0 rlineto closepath");
751
Tcl_AppendResult(interp, buffer, (char *) NULL);
752
if (Tk_CanvasPsColor(interp, canvas, bmapPtr->bgColor) != TCL_OK) {
753
return TCL_ERROR;
754
}
755
Tcl_AppendResult(interp, "fill\n", (char *) NULL);
756
}
757
758
/*
759
* Draw the bitmap, if there is a foreground color. If the bitmap
760
* is very large, then chop it up into multiple bitmaps, each
761
* consisting of one or more rows. This is needed because Postscript
762
* can't handle single strings longer than 64 KBytes long.
763
*/
764
765
if (bmapPtr->fgColor != NULL) {
766
if (Tk_CanvasPsColor(interp, canvas, bmapPtr->fgColor) != TCL_OK) {
767
return TCL_ERROR;
768
}
769
if (width > 60000) {
770
Tcl_ResetResult(interp);
771
Tcl_AppendResult(interp, "can't generate Postscript",
772
" for bitmaps more than 60000 pixels wide",
773
(char *) NULL);
774
return TCL_ERROR;
775
}
776
rowsAtOnce = 60000/width;
777
if (rowsAtOnce < 1) {
778
rowsAtOnce = 1;
779
}
780
sprintf(buffer, "%.15g %.15g translate\n", x, y+height);
781
Tcl_AppendResult(interp, buffer, (char *) NULL);
782
for (curRow = 0; curRow < height; curRow += rowsAtOnce) {
783
rowsThisTime = rowsAtOnce;
784
if (rowsThisTime > (height - curRow)) {
785
rowsThisTime = height - curRow;
786
}
787
sprintf(buffer, "0 -%.15g translate\n%d %d true matrix {\n",
788
(double) rowsThisTime, width, rowsThisTime);
789
Tcl_AppendResult(interp, buffer, (char *) NULL);
790
if (Tk_CanvasPsBitmap(interp, canvas, bmapPtr->bitmap,
791
0, curRow, width, rowsThisTime) != TCL_OK) {
792
return TCL_ERROR;
793
}
794
Tcl_AppendResult(interp, "\n} imagemask\n", (char *) NULL);
795
}
796
}
797
return TCL_OK;
798
}
799
800