Debian Codegraph @

pTk/mTk/generic/tkCanvUtil.c

raw symbols SCIP
1 /*
2 * tkCanvUtil.c --
3 *
4 * This procedure contains a collection of utility procedures
5 * used by the implementations of various canvas item types.
6 *
7 * Copyright (c) 1994 Sun Microsystems, Inc.
8 * Copyright (c) 1994 Sun Microsystems, Inc.
9 *
10 * See the file "license.terms" for information on usage and redistribution
11 * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
12 *
13 * RCS: @(#) $Id: tkCanvUtil.c,v 1.10 2003/01/17 19:54:09 drh Exp $
14 */
15
16 #include "tkPort.h"
17 #include "tkInt.h"
18 #include "tkCanvases.h"
19
20 /*
21 * Custom option for handling "-tags" options for canvas items:
22 */
23 #include "tkCanvas.h"
24 #include <assert.h>
25
26
27 /*
28 *----------------------------------------------------------------------
29 *
30 * Tk_CanvasTkwin --
31 *
32 * Given a token for a canvas, this procedure returns the
33 * widget that represents the canvas.
34 *
35 * Results:
36 * The return value is a handle for the widget.
37 *
38 * Side effects:
39 * None.
40 *
41 *----------------------------------------------------------------------
42 */
43
44 Tk_Window
45 Tk_CanvasTkwin(canvas)
46 Tk_Canvas canvas; /* Token for the canvas. */
47 {
48 TkCanvas *canvasPtr = (TkCanvas *) canvas;
49 return canvasPtr->tkwin;
50 }
51
52 /*
53 *----------------------------------------------------------------------
54 *
55 * Tk_CanvasDrawableCoords --
56 *
57 * Given an (x,y) coordinate pair within a canvas, this procedure
58 * returns the corresponding coordinates at which the point should
59 * be drawn in the drawable used for display.
60 *
61 * Results:
62 * There is no return value. The values at *drawableXPtr and
63 * *drawableYPtr are filled in with the coordinates at which
64 * x and y should be drawn. These coordinates are clipped
65 * to fit within a "short", since this is what X uses in
66 * most cases for drawing.
67 *
68 * Side effects:
69 * None.
70 *
71 *----------------------------------------------------------------------
72 */
73
74 void
75 Tk_CanvasDrawableCoords(canvas, x, y, drawableXPtr, drawableYPtr)
76 Tk_Canvas canvas; /* Token for the canvas. */
77 double x, y; /* Coordinates in canvas space. */
78 short *drawableXPtr, *drawableYPtr; /* Screen coordinates are stored
79 * here. */
80 {
81 TkCanvas *canvasPtr = (TkCanvas *) canvas;
82 double tmp;
83
84 tmp = x - canvasPtr->drawableXOrigin;
85 if (tmp > 0) {
86 tmp += 0.5;
87 } else {
88 tmp -= 0.5;
89 }
90 if (tmp > 32767) {
91 *drawableXPtr = 32767;
92 } else if (tmp < -32768) {
93 *drawableXPtr = -32768;
94 } else {
95 *drawableXPtr = (short) tmp;
96 }
97
98 tmp = y - canvasPtr->drawableYOrigin;
99 if (tmp > 0) {
100 tmp += 0.5;
101 } else {
102 tmp -= 0.5;
103 }
104 if (tmp > 32767) {
105 *drawableYPtr = 32767;
106 } else if (tmp < -32768) {
107 *drawableYPtr = -32768;
108 } else {
109 *drawableYPtr = (short) tmp;
110 }
111 }
112
113 /*
114 *----------------------------------------------------------------------
115 *
116 * Tk_CanvasWindowCoords --
117 *
118 * Given an (x,y) coordinate pair within a canvas, this procedure
119 * returns the corresponding coordinates in the canvas's window.
120 *
121 * Results:
122 * There is no return value. The values at *screenXPtr and
123 * *screenYPtr are filled in with the coordinates at which
124 * (x,y) appears in the canvas's window. These coordinates
125 * are clipped to fit within a "short", since this is what X
126 * uses in most cases for drawing.
127 *
128 * Side effects:
129 * None.
130 *
131 *----------------------------------------------------------------------
132 */
133
134 void
135 Tk_CanvasWindowCoords(canvas, x, y, screenXPtr, screenYPtr)
136 Tk_Canvas canvas; /* Token for the canvas. */
137 double x, y; /* Coordinates in canvas space. */
138 short *screenXPtr, *screenYPtr; /* Screen coordinates are stored
139 * here. */
140 {
141 TkCanvas *canvasPtr = (TkCanvas *) canvas;
142 double tmp;
143
144 tmp = x - canvasPtr->xOrigin;
145 if (tmp > 0) {
146 tmp += 0.5;
147 } else {
148 tmp -= 0.5;
149 }
150 if (tmp > 32767) {
151 *screenXPtr = 32767;
152 } else if (tmp < -32768) {
153 *screenXPtr = -32768;
154 } else {
155 *screenXPtr = (short) tmp;
156 }
157
158 tmp = y - canvasPtr->yOrigin;
159 if (tmp > 0) {
160 tmp += 0.5;
161 } else {
162 tmp -= 0.5;
163 }
164 if (tmp > 32767) {
165 *screenYPtr = 32767;
166 } else if (tmp < -32768) {
167 *screenYPtr = -32768;
168 } else {
169 *screenYPtr = (short) tmp;
170 }
171 }
172
173 /*
174 *--------------------------------------------------------------
175 *
176 * Tk_CanvasGetCoord --
177 *
178 * Given a string, returns a floating-point canvas coordinate
179 * corresponding to that string.
180 *
181 * Results:
182 * The return value is a standard Tcl return result. If
183 * TCL_OK is returned, then everything went well and the
184 * canvas coordinate is stored at *doublePtr; otherwise
185 * TCL_ERROR is returned and an error message is left in
186 * the interp's result.
187 *
188 * Side effects:
189 * None.
190 *
191 *--------------------------------------------------------------
192 */
193
194 int
195 Tk_CanvasGetCoord(interp, canvas, string, doublePtr)
196 Tcl_Interp *interp; /* Interpreter for error reporting. */
197 Tk_Canvas canvas; /* Canvas to which coordinate applies. */
198 CONST char *string; /* Describes coordinate (any screen
199 * coordinate form may be used here). */
200 double *doublePtr; /* Place to store converted coordinate. */
201 {
202 TkCanvas *canvasPtr = (TkCanvas *) canvas;
203 if (Tk_GetScreenMM(canvasPtr->interp, canvasPtr->tkwin, string,
204 doublePtr) != TCL_OK) {
205 return TCL_ERROR;
206 }
207 *doublePtr *= canvasPtr->pixelsPerMM;
208 return TCL_OK;
209 }
210
211 /*
212 *--------------------------------------------------------------
213 *
214 * Tk_CanvasGetCoordFromObj --
215 *
216 * Given a string, returns a floating-point canvas coordinate
217 * corresponding to that string.
218 *
219 * Results:
220 * The return value is a standard Tcl return result. If
221 * TCL_OK is returned, then everything went well and the
222 * canvas coordinate is stored at *doublePtr; otherwise
223 * TCL_ERROR is returned and an error message is left in
224 * interp->result.
225 *
226 * Side effects:
227 * None.
228 *
229 *--------------------------------------------------------------
230 */
231
232 int
233 Tk_CanvasGetCoordFromObj(interp, canvas, obj, doublePtr)
234 Tcl_Interp *interp; /* Interpreter for error reporting. */
235 Tk_Canvas canvas; /* Canvas to which coordinate applies. */
236 Tcl_Obj *obj; /* Describes coordinate (any screen
237 * coordinate form may be used here). */
238 double *doublePtr; /* Place to store converted coordinate. */
239 {
240 TkCanvas *canvasPtr = (TkCanvas *) canvas;
241 if (Tk_GetMMFromObj(canvasPtr->interp, canvasPtr->tkwin, obj,
242 doublePtr) != TCL_OK) {
243 return TCL_ERROR;
244 }
245 *doublePtr *= canvasPtr->pixelsPerMM;
246 return TCL_OK;
247 }
248
249 /*
250 *----------------------------------------------------------------------
251 *
252 * Tk_CanvasSetStippleOrigin --
253 *
254 * This procedure sets the stipple origin in a graphics context
255 * so that stipples drawn with the GC will line up with other
256 * stipples previously drawn in the canvas.
257 *
258 * Results:
259 * None.
260 *
261 * Side effects:
262 * The graphics context is modified.
263 *
264 *----------------------------------------------------------------------
265 */
266
267 void
268 Tk_CanvasSetStippleOrigin(canvas, gc)
269 Tk_Canvas canvas; /* Token for a canvas. */
270 GC gc; /* Graphics context that is about to be
271 * used to draw a stippled pattern as
272 * part of redisplaying the canvas. */
273
274 {
275 TkCanvas *canvasPtr = (TkCanvas *) canvas;
276
277 XSetTSOrigin(canvasPtr->display, gc, -canvasPtr->drawableXOrigin,
278 -canvasPtr->drawableYOrigin);
279 }
280
281 /*
282 *----------------------------------------------------------------------
283 *
284 * Tk_CanvasSetOffset--
285 *
286 * This procedure sets the stipple offset in a graphics
287 * context so that stipples drawn with the GC will
288 * line up with other stipples with the same offset.
289 *
290 * Results:
291 * None.
292 *
293 * Side effects:
294 * The graphics context is modified.
295 *
296 *----------------------------------------------------------------------
297 */
298
299 void
300 Tk_CanvasSetOffset(canvas, gc, offset)
301 Tk_Canvas canvas; /* Token for a canvas. */
302 GC gc; /* Graphics context that is about to be
303 * used to draw a stippled pattern as
304 * part of redisplaying the canvas. */
305 Tk_TSOffset *offset; /* offset (may be NULL pointer)*/
306 {
307 TkCanvas *canvasPtr = (TkCanvas *) canvas;
308 int flags = 0;
309 int x = - canvasPtr->drawableXOrigin;
310 int y = - canvasPtr->drawableYOrigin;
311
312 if (offset != NULL) {
313 flags = offset->flags;
314 x += offset->xoffset;
315 y += offset->yoffset;
316 }
317 if ((flags & TK_OFFSET_RELATIVE) && !(flags & TK_OFFSET_INDEX)) {
318 Tk_SetTSOrigin(canvasPtr->tkwin, gc, x - canvasPtr->xOrigin,
319 y - canvasPtr->yOrigin);
320 } else {
321 XSetTSOrigin(canvasPtr->display, gc, x, y);
322 }
323 }
324
325 /*
326 *----------------------------------------------------------------------
327 *
328 * Tk_CanvasGetTextInfo --
329 *
330 * This procedure returns a pointer to a structure containing
331 * information about the selection and insertion cursor for
332 * a canvas widget. Items such as text items save the pointer
333 * and use it to share access to the information with the generic
334 * canvas code.
335 *
336 * Results:
337 * The return value is a pointer to the structure holding text
338 * information for the canvas. Most of the fields should not
339 * be modified outside the generic canvas code; see the user
340 * documentation for details.
341 *
342 * Side effects:
343 * None.
344 *
345 *----------------------------------------------------------------------
346 */
347
348 Tk_CanvasTextInfo *
349 Tk_CanvasGetTextInfo(canvas)
350 Tk_Canvas canvas; /* Token for the canvas widget. */
351 {
352 return &((TkCanvas *) canvas)->textInfo;
353 }
354
355 /*
356 *--------------------------------------------------------------
357 *
358 * Tk_CanvasTagsParseProc --
359 *
360 * This procedure is invoked during option processing to handle
361 * "-tags" options for canvas items.
362 *
363 * Results:
364 * A standard Tcl return value.
365 *
366 * Side effects:
367 * The tags for a given item get replaced by those indicated
368 * in the value argument.
369 *
370 *--------------------------------------------------------------
371 */
372
373 int
374 Tk_CanvasTagsParseProc(clientData, interp, tkwin, arg, widgRec, offset)
375 ClientData clientData; /* Not used.*/
376 Tcl_Interp *interp; /* Used for reporting errors. */
377 Tk_Window tkwin; /* Window containing canvas widget. */
378 Tcl_Obj *arg; /* Value of option (list of tag
379 * names). */
380 char *widgRec; /* Pointer to record for item. */
381 int offset; /* Offset into item (ignored). */
382 {
383 register Tk_Item *itemPtr = (Tk_Item *) widgRec;
384 int argc, i;
385 Tcl_Obj **objv;
386 Tk_Uid *newPtr;
387
388 /*
389 * Break the value up into the individual tag names.
390 */
391
392 if (Tcl_ListObjGetElements(interp, arg, &argc, &objv) != TCL_OK) {
393 return TCL_ERROR;
394 }
395
396 /*
397 * Make sure that there's enough space in the item to hold the
398 * tag names.
399 */
400
401 if (itemPtr->tagSpace < argc) {
402 newPtr = (Tk_Uid *) ckalloc((unsigned) (argc * sizeof(Tk_Uid)));
403 for (i = itemPtr->numTags-1; i >= 0; i--) {
404 newPtr[i] = itemPtr->tagPtr[i];
405 }
406 if (itemPtr->tagPtr != itemPtr->staticTagSpace) {
407 ckfree((char *) itemPtr->tagPtr);
408 }
409 itemPtr->tagPtr = newPtr;
410 itemPtr->tagSpace = argc;
411 }
412 itemPtr->numTags = argc;
413 for (i = 0; i < argc; i++) {
414 itemPtr->tagPtr[i] = Tk_GetUid(Tcl_GetString(objv[i]));
415 }
416 return TCL_OK;
417 }
418
419 /*
420 *--------------------------------------------------------------
421 *
422 * Tk_CanvasTagsPrintProc --
423 *
424 * This procedure is invoked by the Tk configuration code
425 * to produce a printable string for the "-tags" configuration
426 * option for canvas items.
427 *
428 * Results:
429 * The return value is a string describing all the tags for
430 * the item referred to by "widgRec". In addition, *freeProcPtr
431 * is filled in with the address of a procedure to call to free
432 * the result string when it's no longer needed (or NULL to
433 * indicate that the string doesn't need to be freed).
434 *
435 * Side effects:
436 * None.
437 *
438 *--------------------------------------------------------------
439 */
440
441 Tcl_Obj *
442 Tk_CanvasTagsPrintProc(clientData, tkwin, widgRec, offset, freeProcPtr)
443 ClientData clientData; /* Ignored. */
444 Tk_Window tkwin; /* Window containing canvas widget. */
445 char *widgRec; /* Pointer to record for item. */
446 int offset; /* Ignored. */
447 Tcl_FreeProc **freeProcPtr; /* Pointer to variable to fill in with
448 * information about how to reclaim
449 * storage for return string. */
450 {
451 register Tk_Item *itemPtr = (Tk_Item *) widgRec;
452 Tcl_Obj *result = NULL;
453 int i;
454
455 result = Tcl_NewListObj(0,NULL);
456 for (i=0; i < itemPtr->numTags; i++) {
457 Tcl_ListObjAppendElement(NULL,result,
458 Tcl_NewStringObj((char *)itemPtr->tagPtr[i],-1));
459 }
460 return result;
461 }
462
463
464 static int DashConvert _ANSI_ARGS_((char *l, CONST char *p,
465 int n, double width));
466 #define ABS(a) ((a>=0)?(a):(-(a)))
467
468 /*
469 *--------------------------------------------------------------
470 *
471 * TkCanvasDashParseProc --
472 *
473 * This procedure is invoked during option processing to handle
474 * "-dash", "-activedash" and "-disableddash" options for canvas
475 * objects.
476 *
477 * Results:
478 * A standard Tcl return value.
479 *
480 * Side effects:
481 * The dash list for a given canvas object gets replaced by
482 * those indicated in the value argument.
483 *
484 *--------------------------------------------------------------
485 */
486
487 int
488 TkCanvasDashParseProc(clientData, interp, tkwin, value, widgRec, offset)
489 ClientData clientData; /* Not used.*/
490 Tcl_Interp *interp; /* Used for reporting errors. */
491 Tk_Window tkwin; /* Window containing canvas widget. */
492 Tcl_Obj * value; /* Value of option. */
493 char *widgRec; /* Pointer to record for item. */
494 int offset; /* Offset into item. */
495 {
496 return Tk_GetDash(interp, value, (Tk_Dash *)(widgRec+offset));
497 }
498
499 /*
500 *--------------------------------------------------------------
501 *
502 * TkCanvasDashPrintProc --
503 *
504 * This procedure is invoked by the Tk configuration code
505 * to produce a printable string for the "-dash", "-activedash"
506 * and "-disableddash" configuration options for canvas items.
507 *
508 * Results:
509 * The return value is a string describing all the dash list for
510 * the item referred to by "widgRec"and "offset". In addition,
511 * *freeProcPtr is filled in with the address of a procedure to
512 * call to free the result string when it's no longer needed (or
513 * NULL to indicate that the string doesn't need to be freed).
514 *
515 * Side effects:
516 * None.
517 *
518 *--------------------------------------------------------------
519 */
520
521 Tcl_Obj *
522 TkCanvasDashPrintProc(clientData, tkwin, widgRec, offset, freeProcPtr)
523 ClientData clientData; /* Ignored. */
524 Tk_Window tkwin; /* Window containing canvas widget. */
525 char *widgRec; /* Pointer to record for item. */
526 int offset; /* Offset in record for item. */
527 Tcl_FreeProc **freeProcPtr; /* Pointer to variable to fill in with
528 * information about how to reclaim
529 * storage for return string. */
530 {
531 Tk_Dash *dash = (Tk_Dash *) (widgRec+offset);
532 char *buffer = NULL;
533 char *p;
534 int i = dash->number;
535 Tcl_Obj * result = NULL;
536
537 if (i<0) {
538 p = (-i > (int) sizeof(char *)) ? dash->pattern.pt : dash->pattern.array;
539 result = Tcl_NewStringObj(p,-i);
540 return result;
541 } else if (!i) {
542 *freeProcPtr = (Tcl_FreeProc *) NULL;
543 LangSetDefault(&result,"");
544 return result;
545 }
546 result = Tcl_NewListObj(0,NULL);
547 p = (i > (int) sizeof(char *)) ? dash->pattern.pt : dash->pattern.array;
548 while(i--) {
549 Tcl_ListObjAppendElement(NULL,result,Tcl_NewIntObj(*p++ & 0xff));
550 }
551 return result;
552 }
553
554 /*
555 *--------------------------------------------------------------
556 *
557 * Tk_CreateSmoothMethod --
558 *
559 * This procedure is invoked to add additional values
560 * for the "-smooth" option to the list.
561 *
562 * Results:
563 * A standard Tcl return value.
564 *
565 * Side effects:
566 * In the future "-smooth <name>" will be accepted as
567 * smooth method for the line and polygon.
568 *
569 *--------------------------------------------------------------
570 */
571
572 Tk_SmoothMethod tkBezierSmoothMethod = {
573 "bezier",
574 TkMakeBezierCurve,
575 (void (*) _ANSI_ARGS_((Tcl_Interp *interp, Tk_Canvas canvas,
576 double *coordPtr, int numPoints, int numSteps)))
577 TkMakeBezierPostscript,
578 };
579
580 static void SmoothMethodCleanupProc _ANSI_ARGS_((ClientData clientData,
581 Tcl_Interp *interp));
582
583 typedef struct SmoothAssocData {
584 struct SmoothAssocData *nextPtr; /* pointer to next SmoothAssocData */
585 Tk_SmoothMethod smooth; /* name and functions associated with this
586 * option */
587 } SmoothAssocData;
588
589 void
590 Tk_CreateSmoothMethod(interp, smooth)
591 Tcl_Interp *interp;
592 Tk_SmoothMethod *smooth;
593 {
594 SmoothAssocData *methods, *typePtr2, *prevPtr, *ptr;
595 methods = (SmoothAssocData *) Tcl_GetAssocData(interp, "smoothMethod",
596 (Tcl_InterpDeleteProc **) NULL);
597
598 /*
599 * If there's already a smooth method with the given name, remove it.
600 */
601
602 for (typePtr2 = methods, prevPtr = NULL; typePtr2 != NULL;
603 prevPtr = typePtr2, typePtr2 = typePtr2->nextPtr) {
604 if (!strcmp(typePtr2->smooth.name, smooth->name)) {
605 if (prevPtr == NULL) {
606 methods = typePtr2->nextPtr;
607 } else {
608 prevPtr->nextPtr = typePtr2->nextPtr;
609 }
610 ckfree((char *) typePtr2);
611 break;
612 }
613 }
614 ptr = (SmoothAssocData *) ckalloc(sizeof(SmoothAssocData));
615 ptr->smooth.name = smooth->name;
616 ptr->smooth.coordProc = smooth->coordProc;
617 ptr->smooth.postscriptProc = smooth->postscriptProc;
618 ptr->nextPtr = methods;
619 Tcl_SetAssocData(interp, "smoothMethod", SmoothMethodCleanupProc,
620 (ClientData) ptr);
621 }
622 /*
623 *----------------------------------------------------------------------
624 *
625 * SmoothMethodCleanupProc --
626 *
627 * This procedure is invoked whenever an interpreter is deleted
628 * to cleanup the smooth methods.
629 *
630 * Results:
631 * None.
632 *
633 * Side effects:
634 * Smooth methods are removed.
635 *
636 *----------------------------------------------------------------------
637 */
638
639 static void
640 SmoothMethodCleanupProc(clientData, interp)
641 ClientData clientData; /* Points to "smoothMethod" AssocData
642 * for the interpreter. */
643 Tcl_Interp *interp; /* Interpreter that is being deleted. */
644 {
645 SmoothAssocData *ptr, *methods = (SmoothAssocData *) clientData;
646
647 while (methods != NULL) {
648 methods = (ptr = methods)->nextPtr;
649 ckfree((char *) ptr);
650 }
651 }
652 /*
653 *--------------------------------------------------------------
654 *
655 * TkSmoothParseProc --
656 *
657 * This procedure is invoked during option processing to handle
658 * the "-smooth" option.
659 *
660 * Results:
661 * A standard Tcl return value.
662 *
663 * Side effects:
664 * The smooth option for a given item gets replaced by the value
665 * indicated in the value argument.
666 *
667 *--------------------------------------------------------------
668 */
669
670 int
671 TkSmoothParseProc(clientData, interp, tkwin, ovalue, widgRec, offset)
672 ClientData clientData; /* some flags.*/
673 Tcl_Interp *interp; /* Used for reporting errors. */
674 Tk_Window tkwin; /* Window containing canvas widget. */
675 Tcl_Obj * ovalue; /* Value of option. */
676 char *widgRec; /* Pointer to record for item. */
677 int offset; /* Offset into item. */
678 {
679 register Tk_SmoothMethod **smoothPtr =
680 (Tk_SmoothMethod **) (widgRec + offset);
681 Tk_SmoothMethod *smooth = NULL;
682 int b;
683 size_t length;
684 SmoothAssocData *methods;
685 char *value = Tcl_GetString(ovalue);
686
687 if (value == NULL || *value == 0) {
688 *smoothPtr = (Tk_SmoothMethod *) NULL;
689 return TCL_OK;
690 }
691 length = strlen(value);
692 methods = (SmoothAssocData *) Tcl_GetAssocData(interp, "smoothMethod",
693 (Tcl_InterpDeleteProc **) NULL);
694 while (methods != (SmoothAssocData *) NULL) {
695 if (strncmp(value, methods->smooth.name, length) == 0) {
696 if (smooth != (Tk_SmoothMethod *) NULL) {
697 Tcl_AppendResult(interp, "ambigeous smooth method \"", value,
698 "\"", (char *) NULL);
699 return TCL_ERROR;
700 }
701 smooth = &methods->smooth;
702 }
703 methods = methods->nextPtr;
704 }
705 if (smooth) {
706 *smoothPtr = smooth;
707 return TCL_OK;
708 } else if (strncmp(value, tkBezierSmoothMethod.name, length) == 0) {
709 /*
710 * We need to do handle the built-in bezier method.
711 */
712 *smoothPtr = &tkBezierSmoothMethod;
713 return TCL_OK;
714 }
715
716
717 if (Tcl_GetBooleanFromObj(interp, ovalue, &b) != TCL_OK) {
718 return TCL_ERROR;
719 }
720 *smoothPtr = b ? &tkBezierSmoothMethod : (Tk_SmoothMethod*) NULL;
721 return TCL_OK;
722 }
723 /*
724 *--------------------------------------------------------------
725 *
726 * TkSmoothPrintProc --
727 *
728 * This procedure is invoked by the Tk configuration code
729 * to produce a printable string for the "-smooth"
730 * configuration option.
731 *
732 * Results:
733 * The return value is a string describing the smooth option for
734 * the item referred to by "widgRec". In addition, *freeProcPtr
735 * is filled in with the address of a procedure to call to free
736 * the result string when it's no longer needed (or NULL to
737 * indicate that the string doesn't need to be freed).
738 *
739 * Side effects:
740 * None.
741 *
742 *--------------------------------------------------------------
743 */
744
745 Tcl_Obj *
746 TkSmoothPrintProc(clientData, tkwin, widgRec, offset, freeProcPtr)
747 ClientData clientData; /* Ignored. */
748 Tk_Window tkwin; /* Window containing canvas widget. */
749 char *widgRec; /* Pointer to record for item. */
750 int offset; /* Offset into item. */
751 Tcl_FreeProc **freeProcPtr; /* Pointer to variable to fill in with
752 * information about how to reclaim
753 * storage for return string. */
754 {
755 register Tk_SmoothMethod **smoothPtr = (Tk_SmoothMethod **) (widgRec + offset);
756 Tcl_Obj * result = NULL;
757 if (*smoothPtr) {
758 LangSetDefault(&result,(*smoothPtr)->name);
759 }
760 return result;
761 }
762 /*
763 *--------------------------------------------------------------
764 *
765 * Tk_GetDash
766 *
767 * This procedure is used to parse a string, assuming
768 * it is dash information.
769 *
770 * Results:
771 * The return value is a standard Tcl result: TCL_OK means
772 * that the dash information was parsed ok, and
773 * TCL_ERROR means it couldn't be parsed.
774 *
775 * Side effects:
776 * Dash information in the dash structure is updated.
777 *
778 *--------------------------------------------------------------
779 */
780
781 int
782 Tk_GetDash(interp, ovalue, dash)
783 Tcl_Interp *interp; /* Used for error reporting. */
784 Tcl_Obj * ovalue; /* Textual specification of dash list. */
785 Tk_Dash *dash; /* Pointer to record in which to
786 * store dash information. */
787 {
788 int argc, i;
789 Tcl_Obj **largv = NULL;
790 Tcl_Obj **objv = NULL;
791 char *pt;
792 char *value = Tcl_GetString(ovalue);
793
794 if ((value==(char *) NULL) || (*value==0) ) {
795 dash->number = 0;
796 return TCL_OK;
797 }
798 if ((*value == '.') || (*value == ',') ||
799 (*value == '-') || (*value == '_')) {
800 i = DashConvert((char *) NULL, value, -1, 0.0);
801 if (i<0) {
802 goto badDashList;
803 }
804 dash->pattern.pt = pt = (char *) ckalloc(strlen(value)+1);
805 strcpy( pt, value );
806 dash->number = -i;
807 return TCL_OK;
808 }
809 if (Tcl_ListObjGetElements(interp, ovalue, &argc, &objv) != TCL_OK ||
810 argc <= 1) {
811 Tcl_ResetResult(interp);
812 badDashList:
813 Tcl_AppendResult(interp, "bad dash list \"", value,
814 "\": must be a list of integers or a format like \"-..\"",
815 (char *) NULL);
816 syntaxError:
817 if (ABS(dash->number) > sizeof(char *))
818 ckfree((char *) dash->pattern.pt);
819 dash->number = 0;
820 return TCL_ERROR;
821 }
822
823 if (ABS(dash->number) > sizeof(char *)) {
824 ckfree((char *) dash->pattern.pt);
825 }
826 if (argc > (int) sizeof(char *)) {
827 dash->pattern.pt = pt = (char *) ckalloc((unsigned int) argc);
828 } else {
829 pt = dash->pattern.array;
830 }
831 dash->number = argc;
832
833 largv = objv;
834 while(argc>0) {
835 if (Tcl_GetIntFromObj(interp, *largv, &i) != TCL_OK ||
836 i < 1 || i>255) {
837 Tcl_ResetResult(interp);
838 Tcl_AppendResult(interp, "expected integer in the range 1..255 but got \"",
839 Tcl_GetString(*largv), "\"", (char *) NULL);
840 goto syntaxError;
841 }
842 *pt++ = i;
843 argc--; largv++;
844 }
845
846 #ifdef FIXME
847 if (argv != NULL) {
848 ckfree((char *) argv);
849 }
850 #endif
851
852 return TCL_OK;
853 }
854
855 /*
856 *--------------------------------------------------------------
857 *
858 * Tk_CreateOutline
859 *
860 * This procedure initializes the Tk_Outline structure
861 * with default values.
862 *
863 * Results:
864 * None
865 *
866 * Side effects:
867 * None
868 *
869 *--------------------------------------------------------------
870 */
871
872 void Tk_CreateOutline(outline)
873 Tk_Outline *outline;
874 {
875 outline->gc = None;
876 outline->width = 1.0;
877 outline->activeWidth = 0.0;
878 outline->disabledWidth = 0.0;
879 outline->offset = 0;
880 outline->dash.number = 0;
881 outline->activeDash.number = 0;
882 outline->disabledDash.number = 0;
883 outline->tsoffset.flags = 0;
884 outline->tsoffset.xoffset = 0;
885 outline->tsoffset.yoffset = 0;
886 outline->color = NULL;
887 outline->activeColor = NULL;
888 outline->disabledColor = NULL;
889 outline->stipple = None;
890 outline->activeStipple = None;
891 outline->disabledStipple = None;
892 }
893
894 /*
895 *--------------------------------------------------------------
896 *
897 * Tk_DeleteOutline
898 *
899 * This procedure frees all memory that might be
900 * allocated and referenced in the Tk_Outline structure.
901 *
902 * Results:
903 * None
904 *
905 * Side effects:
906 * None
907 *
908 *--------------------------------------------------------------
909 */
910
911 void Tk_DeleteOutline(display, outline)
912 Display *display; /* Display containing window */
913 Tk_Outline *outline;
914 {
915 if (outline->gc != None) {
916 Tk_FreeGC(display, outline->gc);
917 }
918 if (ABS(outline->dash.number) > sizeof(char *)) {
919 ckfree((char *) outline->dash.pattern.pt);
920 }
921 if (ABS(outline->activeDash.number) > sizeof(char *)) {
922 ckfree((char *) outline->activeDash.pattern.pt);
923 }
924 if (ABS(outline->disabledDash.number) > sizeof(char *)) {
925 ckfree((char *) outline->disabledDash.pattern.pt);
926 }
927 if (outline->color != NULL) {
928 Tk_FreeColor(outline->color);
929 }
930 if (outline->activeColor != NULL) {
931 Tk_FreeColor(outline->activeColor);
932 }
933 if (outline->disabledColor != NULL) {
934 Tk_FreeColor(outline->disabledColor);
935 }
936 if (outline->stipple != None) {
937 Tk_FreeBitmap(display, outline->stipple);
938 }
939 if (outline->activeStipple != None) {
940 Tk_FreeBitmap(display, outline->activeStipple);
941 }
942 if (outline->disabledStipple != None) {
943 Tk_FreeBitmap(display, outline->disabledStipple);
944 }
945 }
946
947 /*
948 *--------------------------------------------------------------
949 *
950 * Tk_ConfigOutlineGC
951 *
952 * This procedure should be called in the canvas object
953 * during the configure command. The graphics context
954 * description in gcValues is updated according to the
955 * information in the dash structure, as far as possible.
956 *
957 * Results:
958 * The return-value is a mask, indicating which
959 * elements of gcValues have been updated.
960 * 0 means there is no outline.
961 *
962 * Side effects:
963 * GC information in gcValues is updated.
964 *
965 *--------------------------------------------------------------
966 */
967
968 int Tk_ConfigOutlineGC(gcValues, canvas, item, outline)
969 XGCValues *gcValues;
970 Tk_Canvas canvas;
971 Tk_Item *item;
972 Tk_Outline *outline;
973 {
974 int mask = 0;
975 double width;
976 Tk_Dash *dash;
977 XColor *color;
978 Pixmap stipple;
979 Tk_State state = item->state;
980
981 if (outline->width < 0.0) {
982 outline->width = 0.0;
983 }
984 if (outline->activeWidth < 0.0) {
985 outline->activeWidth = 0.0;
986 }
987 if (outline->disabledWidth < 0) {
988 outline->disabledWidth = 0.0;
989 }
990 if (state==TK_STATE_HIDDEN) {
991 return 0;
992 }
993
994 width = outline->width;
995 dash = &(outline->dash);
996 color = outline->color;
997 stipple = outline->stipple;
998 if (state == TK_STATE_NULL) {
999 state = ((TkCanvas *)canvas)->canvas_state;
1000 }
1001 if (((TkCanvas *)canvas)->currentItemPtr == item) {
1002 if (outline->activeWidth>width) {
1003 width = outline->activeWidth;
1004 }
1005 if (outline->activeDash.number != 0) {
1006 dash = &(outline->activeDash);
1007 }
1008 if (outline->activeColor!=NULL) {
1009 color = outline->activeColor;
1010 }
1011 if (outline->activeStipple!=None) {
1012 stipple = outline->activeStipple;
1013 }
1014 } else if (state==TK_STATE_DISABLED) {
1015 if (outline->disabledWidth>0) {
1016 width = outline->disabledWidth;
1017 }
1018 if (outline->disabledDash.number != 0) {
1019 dash = &(outline->disabledDash);
1020 }
1021 if (outline->disabledColor!=NULL) {
1022 color = outline->disabledColor;
1023 }
1024 if (outline->disabledStipple!=None) {
1025 stipple = outline->disabledStipple;
1026 }
1027 }
1028
1029 if (color==NULL) {
1030 return 0;
1031 }
1032
1033 if (width < 1.0) {
1034 width = 1.0;
1035 }
1036
1037 gcValues->line_width = (int) (width + 0.5);
1038 if (color != NULL) {
1039 gcValues->foreground = color->pixel;
1040 mask = GCForeground|GCLineWidth;
1041 if (stipple != None) {
1042 gcValues->stipple = stipple;
1043 gcValues->fill_style = FillStippled;
1044 mask |= GCStipple|GCFillStyle;
1045 }
1046 }
1047 if (mask && (dash->number != 0)) {
1048 gcValues->line_style = LineOnOffDash;
1049 gcValues->dash_offset = outline->offset;
1050 if (dash->number >= 2) {
1051 gcValues->dashes = 4;
1052 } else {
1053 gcValues->dashes = (char) (4 * width);
1054 }
1055 mask |= GCLineStyle|GCDashList|GCDashOffset;
1056 }
1057 return mask;
1058 }
1059
1060 /*
1061 *--------------------------------------------------------------
1062 *
1063 * Tk_ChangeOutlineGC
1064 *
1065 * Updates the GC to represent the full information of
1066 * the dash structure. Partly this is already done in
1067 * Tk_ConfigOutlineGC().
1068 * This function should be called just before drawing
1069 * the dashed item.
1070 *
1071 * Results:
1072 * 1 if there is a stipple pattern.
1073 * 0 otherwise.
1074 *
1075 * Side effects:
1076 * GC is updated.
1077 *
1078 *--------------------------------------------------------------
1079 */
1080
1081 int
1082 Tk_ChangeOutlineGC(canvas, item, outline)
1083 Tk_Canvas canvas;
1084 Tk_Item *item;
1085 Tk_Outline *outline;
1086 {
1087 CONST char *p;
1088 double width;
1089 Tk_Dash *dash;
1090 XColor *color;
1091 Pixmap stipple;
1092 Tk_State state = item->state;
1093 XGCValues values;
1094 Tk_Tile tile = outline->tile;
1095
1096 width = outline->width;
1097 if (width < 1.0) {
1098 width = 1.0;
1099 }
1100 dash = &(outline->dash);
1101 color = outline->color;
1102 stipple = outline->stipple;
1103 if (state == TK_STATE_NULL) {
1104 state = ((TkCanvas *)canvas)->canvas_state;
1105 }
1106 if (((TkCanvas *)canvas)->currentItemPtr == item) {
1107 if (outline->activeWidth > width) {
1108 width = outline->activeWidth;
1109 }
1110 if (outline->activeDash.number != 0) {
1111 dash = &(outline->activeDash);
1112 }
1113 if (outline->activeColor != NULL) {
1114 color = outline->activeColor;
1115 }
1116 if (outline->activeStipple != None) {
1117 stipple = outline->activeStipple;
1118 }
1119 } else if (state == TK_STATE_DISABLED) {
1120 if (outline->disabledWidth > width) {
1121 width = outline->disabledWidth;
1122 }
1123 if (outline->disabledDash.number != 0) {
1124 dash = &(outline->disabledDash);
1125 }
1126 if (outline->disabledColor != NULL) {
1127 color = outline->disabledColor;
1128 }
1129 if (outline->disabledStipple != None) {
1130 stipple = outline->disabledStipple;
1131 }
1132 }
1133 if (color==NULL) {
1134 return 0;
1135 }
1136
1137 if (dash->number<=-2) {
1138 char *q;
1139 int i = -dash->number;
1140
1141 p = dash->pattern.pt;
1142 q = (char *) ckalloc(2*(unsigned int)i);
1143 i = DashConvert(q, p, i, width);
1144 XSetDashes(((TkCanvas *)canvas)->display, outline->gc, outline->offset, q, i);
1145 values.line_style = LineOnOffDash;
1146 ckfree(q);
1147 } else if ( dash->number>=2 ) {
1148 p = (char *) (dash->number > (int) sizeof(char *)) ? dash->pattern.pt : dash->pattern.array;
1149 XSetDashes(((TkCanvas *)canvas)->display, outline->gc, outline->offset, p, dash->number);
1150 values.line_style = LineOnOffDash;
1151 } else {
1152 values.line_style = LineSolid;
1153 }
1154 XChangeGC(((TkCanvas *)canvas)->display, outline->gc, GCLineStyle, &values);
1155 if ((tile!= NULL) || (stipple!=None)) {
1156 int w=0; int h=0;
1157 Tk_TSOffset *tsoffset = &outline->tsoffset;
1158 int flags = tsoffset->flags;
1159 if (!(flags & TK_OFFSET_INDEX) && (flags & (TK_OFFSET_CENTER|TK_OFFSET_MIDDLE))) {
1160 Tk_SizeOfBitmap(((TkCanvas *)canvas)->display, stipple, &w, &h);
1161 if (flags & TK_OFFSET_CENTER) {
1162 w /= 2;
1163 } else {
1164 w = 0;
1165 }
1166 if (flags & TK_OFFSET_MIDDLE) {
1167 h /= 2;
1168 } else {
1169 h = 0;
1170 }
1171 }
1172 tsoffset->xoffset -= w;
1173 tsoffset->yoffset -= h;
1174 Tk_CanvasSetOffset(canvas, outline->gc, tsoffset);
1175 tsoffset->xoffset += w;
1176 tsoffset->yoffset += h;
1177 return 1;
1178 }
1179 return 0;
1180 }
1181
1182
1183 /*
1184 *--------------------------------------------------------------
1185 *
1186 * Tk_ResetOutlineGC
1187 *
1188 * Restores the GC to the situation before
1189 * Tk_ChangeDashGC() was called.
1190 * This function should be called just after the dashed
1191 * item is drawn, because the GC is supposed to be
1192 * read-only.
1193 *
1194 * Results:
1195 * 1 if there is a stipple pattern.
1196 * 0 otherwise.
1197 *
1198 * Side effects:
1199 * GC is updated.
1200 *
1201 *--------------------------------------------------------------
1202 */
1203 int
1204 Tk_ResetOutlineGC(canvas, item, outline)
1205 Tk_Canvas canvas;
1206 Tk_Item *item;
1207 Tk_Outline *outline;
1208 {
1209 char dashList;
1210 double width;
1211 Tk_Dash *dash;
1212 XColor *color;
1213 Pixmap stipple;
1214 Tk_State state = item->state;
1215
1216 width = outline->width;
1217 if (width < 1.0) {
1218 width = 1.0;
1219 }
1220 dash = &(outline->dash);
1221 color = outline->color;
1222 stipple = outline->stipple;
1223 if (state == TK_STATE_NULL) {
1224 state = ((TkCanvas *)canvas)->canvas_state;
1225 }
1226 if (((TkCanvas *)canvas)->currentItemPtr == item) {
1227 if (outline->activeWidth>width) {
1228 width = outline->activeWidth;
1229 }
1230 if (outline->activeDash.number != 0) {
1231 dash = &(outline->activeDash);
1232 }
1233 if (outline->activeColor!=NULL) {
1234 color = outline->activeColor;
1235 }
1236 if (outline->activeStipple!=None) {
1237 stipple = outline->activeStipple;
1238 }
1239 } else if (state==TK_STATE_DISABLED) {
1240 if (outline->disabledWidth>width) {
1241 width = outline->disabledWidth;
1242 }
1243 if (outline->disabledDash.number != 0) {
1244 dash = &(outline->disabledDash);
1245 }
1246 if (outline->disabledColor!=NULL) {
1247 color = outline->disabledColor;
1248 }
1249 if (outline->disabledStipple!=None) {
1250 stipple = outline->disabledStipple;
1251 }
1252 }
1253 if (color==NULL) {
1254 return 0;
1255 }
1256
1257 if ( (dash->number >= 2) || (dash->number <= -2) ) {
1258 if (dash->number < 0) {
1259 dashList = (int) (4 * width + 0.5);
1260 } else {
1261 dashList = 4;
1262 }
1263 XSetDashes(((TkCanvas *)canvas)->display, outline->gc, outline->offset, &dashList , 1);
1264 } else {
1265 XGCValues values;
1266 values.line_style = LineSolid;
1267 XChangeGC(((TkCanvas *)canvas)->display, outline->gc, GCLineStyle, &values);
1268 }
1269 if (stipple != None) {
1270 XSetTSOrigin(((TkCanvas *)canvas)->display, outline->gc, 0, 0);
1271 return 1;
1272 }
1273 return 0;
1274 }
1275
1276
1277 /*
1278 *--------------------------------------------------------------
1279 *
1280 * Tk_CanvasPsOutline
1281 *
1282 * Creates the postscript command for the correct
1283 * Outline-information (width, dash, color and stipple).
1284 *
1285 * Results:
1286 * TCL_OK if succeeded, otherwise TCL_ERROR.
1287 *
1288 * Side effects:
1289 * canvas->interp->result contains the postscript string,
1290 * or an error message if the result was TCL_ERROR.
1291 *
1292 *--------------------------------------------------------------
1293 */
1294 int
1295 Tk_CanvasPsOutline(canvas, item, outline)
1296 Tk_Canvas canvas;
1297 Tk_Item *item;
1298 Tk_Outline *outline;
1299 {
1300 char string[41];
1301 char pattern[11];
1302 int i;
1303 char *ptr;
1304 char *str = string;
1305 char *lptr = pattern;
1306 Tcl_Interp *interp = ((TkCanvas *)canvas)->interp;
1307 double width;
1308 Tk_Dash *dash;
1309 XColor *color;
1310 Pixmap stipple;
1311 Tk_State state = item->state;
1312
1313 width = outline->width;
1314 dash = &(outline->dash);
1315 color = outline->color;
1316 stipple = outline->stipple;
1317 if (state == TK_STATE_NULL) {
1318 state = ((TkCanvas *)canvas)->canvas_state;
1319 }
1320 if (((TkCanvas *)canvas)->currentItemPtr == item) {
1321 if (outline->activeWidth > width) {
1322 width = outline->activeWidth;
1323 }
1324 if (outline->activeDash.number > 0) {
1325 dash = &(outline->activeDash);
1326 }
1327 if (outline->activeColor != NULL) {
1328 color = outline->activeColor;
1329 }
1330 if (outline->activeStipple != None) {
1331 stipple = outline->activeStipple;
1332 }
1333 } else if (state == TK_STATE_DISABLED) {
1334 if (outline->disabledWidth > 0) {
1335 width = outline->disabledWidth;
1336 }
1337 if (outline->disabledDash.number > 0) {
1338 dash = &(outline->disabledDash);
1339 }
1340 if (outline->disabledColor != NULL) {
1341 color = outline->disabledColor;
1342 }
1343 if (outline->disabledStipple != None) {
1344 stipple = outline->disabledStipple;
1345 }
1346 }
1347 sprintf(string, "%.15g setlinewidth\n", width);
1348 Tcl_AppendResult(interp, string, (char *) NULL);
1349
1350 if (dash->number > 10) {
1351 str = (char *)ckalloc((unsigned int) (1 + 4*dash->number));
1352 } else if (dash->number < -5) {
1353 str = (char *)ckalloc((unsigned int) (1 - 8*dash->number));
1354 lptr = (char *)ckalloc((unsigned int) (1 - 2*dash->number));
1355 }
1356 ptr = (char *) ( dash->number > sizeof(char *)) ? dash->pattern.pt :
1357 ( dash->number < 0 ) ? dash->pattern.pt : dash->pattern.array;
1358 if (dash->number > 0) {
1359 char *ptr0 = ptr;
1360 sprintf(str, "[%d", *ptr++ & 0xff);
1361 i = dash->number-1;
1362 while (i--) {
1363 sprintf(str+strlen(str), " %d", *ptr++ & 0xff);
1364 }
1365 Tcl_AppendResult(interp, str, (char *)NULL);
1366 if (dash->number&1) {
1367 Tcl_AppendResult(interp, " ", str+1, (char *)NULL);
1368 }
1369 sprintf(str, "] %d setdash\n", outline->offset);
1370 Tcl_AppendResult(interp, str, (char *)NULL);
1371 ptr = ptr0;
1372 } else if (dash->number < 0) {
1373 if ((i = DashConvert(lptr, ptr, -dash->number, width)) != 0) {
1374 char *lptr0 = lptr;
1375 sprintf(str, "[%d", *lptr++ & 0xff);
1376 while (--i) {
1377 sprintf(str+strlen(str), " %d", *lptr++ & 0xff);
1378 }
1379 Tcl_AppendResult(interp, str, (char *)NULL);
1380 sprintf(str, "] %d setdash\n", outline->offset);
1381 Tcl_AppendResult(interp, str, (char *)NULL);
1382 lptr = lptr0;
1383 } else {
1384 Tcl_AppendResult(interp, "[] 0 setdash\n", (char *)NULL);
1385 }
1386 } else {
1387 Tcl_AppendResult(interp, "[] 0 setdash\n", (char *)NULL);
1388 }
1389 if (str != string) {
1390 ckfree(str);
1391 }
1392 if (lptr != pattern) {
1393 ckfree(lptr);
1394 }
1395 if (Tk_CanvasPsColor(interp, canvas, color) != TCL_OK) {
1396 return TCL_ERROR;
1397 }
1398 if (stipple != None) {
1399 Tcl_AppendResult(interp, "StrokeClip ", (char *) NULL);
1400 if (Tk_CanvasPsStipple(interp, canvas, stipple) != TCL_OK) {
1401 return TCL_ERROR;
1402 }
1403 } else {
1404 Tcl_AppendResult(interp, "stroke\n", (char *) NULL);
1405 }
1406
1407 return TCL_OK;
1408 }
1409
1410
1411 /*
1412 *--------------------------------------------------------------
1413 *
1414 * DashConvert
1415 *
1416 * Converts a character-like dash-list (e.g. "-..")
1417 * into an X11-style. l must point to a string that
1418 * holds room to at least 2*n characters. If
1419 * l == NULL, this function can be used for
1420 * syntax checking only.
1421 *
1422 * Results:
1423 * The length of the resulting X11 compatible
1424 * dash-list. -1 if failed.
1425 *
1426 * Side effects:
1427 * None
1428 *
1429 *--------------------------------------------------------------
1430 */
1431
1432 static int
1433 DashConvert (l, p, n, width)
1434 char *l;
1435 CONST char *p;
1436 int n;
1437 double width;
1438 {
1439 int result = 0;
1440 int size, intWidth;
1441
1442 if (n<0) {
1443 n = strlen(p);
1444 }
1445 intWidth = (int) (width + 0.5);
1446 if (intWidth < 1) {
1447 intWidth = 1;
1448 }
1449 while (n-- && *p) {
1450 switch (*p++) {
1451 case ' ':
1452 if (result) {
1453 if (l) {
1454 l[-1] += 2 * intWidth;
1455 }
1456 continue;
1457 } else {
1458 return 0;
1459 }
1460 break;
1461 case '_':
1462 size = 4;
1463 break;
1464 case '-':
1465 size = 3;
1466 break;
1467 case ',':
1468 size = 2;
1469 break;
1470 case '.':
1471 size = 1;
1472 break;
1473 default:
1474 return -1;
1475 }
1476 if (l) {
1477 *l++ = size * intWidth;
1478 *l++ = 2 * intWidth;
1479 }
1480 result += 2;
1481 }
1482 return result;
1483 }
1484
1485 /*
1486 *----------------------------------------------------------------------
1487 *
1488 * translateAndAppendCoords --
1489 *
1490 * This is a helper routine for TkCanvTranslatePath() below.
1491 *
1492 * Given an (x,y) coordinate pair within a canvas, this procedure
1493 * computes the corresponding coordinates at which the point should
1494 * be drawn in the drawable used for display. Those coordinates are
1495 * then written into outArr[numOut*2] and outArr[numOut*2+1].
1496 *
1497 * Results:
1498 * There is no return value.
1499 *
1500 * Side effects:
1501 * None.
1502 *
1503 *----------------------------------------------------------------------
1504 */
1505
1506 static void
1507 translateAndAppendCoords(canvPtr, x, y, outArr, numOut)
1508 TkCanvas *canvPtr; /* The canvas. */
1509 double x, y; /* Coordinates in canvas space. */
1510 XPoint *outArr; /* Write results into this array */
1511 int numOut; /* Num of prior entries in outArr[] */
1512 {
1513 double tmp;
1514
1515 tmp = x - canvPtr->drawableXOrigin;
1516 if (tmp > 0) {
1517 tmp += 0.5;
1518 } else {
1519 tmp -= 0.5;
1520 }
1521 outArr[numOut].x = (short) tmp;
1522
1523 tmp = y - canvPtr->drawableYOrigin;
1524 if (tmp > 0) {
1525 tmp += 0.5;
1526 } else {
1527 tmp -= 0.5;
1528 }
1529 outArr[numOut].y = (short) tmp;
1530 }
1531
1532 /*
1533 *--------------------------------------------------------------
1534 *
1535 * TkCanvTranslatePath
1536 *
1537 * Translate a line or polygon path so that all vertices are
1538 * within a rectangle that is 1000 pixels larger than the total
1539 * size of the canvas window. This will prevent pixel coordinates
1540 * from overflowing the 16-bit integer size limitation imposed by
1541 * most windowing systems.
1542 *
1543 * coordPtr must point to an array of doubles, two doubles per
1544 * vertex. There are a total of numVertex vertices, or 2*numVertex
1545 * entries in coordPtr. The result vertices written into outArr
1546 * have their coordinate origin shifted to canvPtr->drawableXOrigin
1547 * by canvPtr->drawableYOrigin. There might be as many as 3 times
1548 * more output vertices than there are input vertices. The calling
1549 * function should allocate space accordingly.
1550 *
1551 * This routine limits the width and height of a canvas window
1552 * to 31767 pixels. At the highest resolution display devices
1553 * available today (210 ppi in Jan 2003) that's a window that is
1554 * over 13 feet wide and tall. Should be enough for the near
1555 * future.
1556 *
1557 * Results:
1558 * Clipped and translated path vertices are written into outArr[].
1559 * There might be as many as twice the vertices in outArr[] as there
1560 * are in coordPtr[]. The return value is the number of vertices
1561 * actually written into outArr[].
1562 *
1563 * Side effects:
1564 * None
1565 *
1566 *--------------------------------------------------------------
1567 */
1568 int
1569 TkCanvTranslatePath (canvPtr, numVertex, coordArr, closedPath, outArr)
1570 TkCanvas *canvPtr; /* The canvas */
1571 int numVertex; /* Number of vertices specified by coordArr[] */
1572 double *coordArr; /* X and Y coordinates for each vertex */
1573 int closedPath; /* True if this is a closed polygon */
1574 XPoint *outArr; /* Write results here, if not NULL */
1575 {
1576 int numOutput = 0; /* Number of output coordinates */
1577 double lft, rgh; /* Left and right sides of the bounding box */
1578 double top, btm; /* Top and bottom sizes of the bounding box */
1579 double *tempArr; /* Temporary storage used by the clipper */
1580 double *a, *b, *t; /* Pointers to parts of the temporary storage */
1581 int i, j; /* Loop counters */
1582 int maxOutput; /* Maximum number of outputs that we will allow */
1583 double limit[4]; /* Boundries at which clipping occurs */
1584 double staticSpace[480]; /* Temp space from the stack */
1585
1586 /*
1587 ** Constrain all vertices of the path to be within a box that is no
1588 ** larger than 32000 pixels wide or height. The top-left corner of
1589 ** this clipping box is 1000 pixels above and to the left of the top
1590 ** left corner of the window on which the canvas is displayed.
1591 **
1592 ** This means that a canvas will not display properly on a canvas
1593 ** window that is larger than 31000 pixels wide or high. That is not
1594 ** a problem today, but might someday become a factor for ultra-high
1595 ** resolutions displays.
1596 **
1597 ** The X11 protocol allows us (in theory) to expand the size of the
1598 ** clipping box to 32767 pixels. But we have found experimentally that
1599 ** XFree86 sometimes fails to draw lines correctly if they are longer
1600 ** than about 32500 pixels. So we have left a little margin in the
1601 ** size to mask that bug.
1602 */
1603 lft = canvPtr->xOrigin - 1000.0;
1604 top = canvPtr->yOrigin - 1000.0;
1605 rgh = lft + 32000.0;
1606 btm = top + 32000.0;
1607
1608 /* Try the common case first - no clipping. Loop over the input
1609 ** coordinates and translate them into appropriate output coordinates.
1610 ** But if a vertex outside of the bounding box is seen, break out of
1611 ** the loop.
1612 **
1613 ** Most of the time, no clipping is needed, so this one loop is
1614 ** sufficient to do the translation.
1615 */
1616 for(i=0; i<numVertex; i++){
1617 double x, y;
1618 x = coordArr[i*2];
1619 y = coordArr[i*2+1];
1620 if( x<lft || x>rgh || y<top || y>btm ) break;
1621 translateAndAppendCoords(canvPtr, x, y, outArr, numOutput++);
1622 }
1623 if( i==numVertex ){
1624 assert( numOutput==numVertex );
1625 return numOutput;
1626 }
1627
1628 /* If we reach this point, it means that some clipping is required.
1629 ** Begin by allocating some working storage - at least 6 times as much space
1630 ** as coordArr[] requires. Divide this space into two separate arrays
1631 ** a[] and b[]. Initialize a[] to be equal to coordArr[].
1632 */
1633 if( numVertex*12 <= (int) (sizeof(staticSpace)/sizeof(staticSpace[0])) ){
1634 tempArr = staticSpace;
1635 } else {
1636 tempArr = (double*)ckalloc( numVertex*12*sizeof(tempArr[0]) );
1637 }
1638 for(i=0; i<numVertex*2; i++){
1639 tempArr[i] = coordArr[i];
1640 }
1641 a = tempArr;
1642 b = &tempArr[numVertex*6];
1643
1644 /* We will make four passes through the input data. On each pass,
1645 ** we copy the contents of a[] over into b[]. As we copy, we clip
1646 ** any line segments that extend to the right past xClip then we
1647 ** rotate the coordinate system 90 degrees clockwise. After each
1648 ** pass is complete, we interchange a[] and b[] in preparation for
1649 ** the next pass.
1650 **
1651 ** Each pass clips line segments that extend beyond a single side
1652 ** of the bounding box, and four passes rotate the coordinate system
1653 ** back to its original value. I'm not an expert on graphics
1654 ** algorithms, but I think this is called Cohen-Sutherland polygon
1655 ** clipping.
1656 **
1657 ** The limit[] array contains the xClip value used for each of the
1658 ** four passes.
1659 */
1660 limit[0] = rgh;
1661 limit[1] = -top;
1662 limit[2] = -lft;
1663 limit[3] = btm;
1664
1665 /* This is the loop that makes the four passes through the data.
1666 */
1667 maxOutput = numVertex*3;
1668 for(j=0; j<4; j++){
1669 double xClip = limit[j];
1670 int inside = a[0]<xClip;
1671 double priorY = a[1];
1672 numOutput = 0;
1673
1674 /* Clip everything to the right of xClip. Store the results in
1675 ** b[] rotated by 90 degrees clockwise.
1676 */
1677 for(i=0; i<numVertex; i++){
1678 double x = a[i*2];
1679 double y = a[i*2+1];
1680 if( x>=xClip ){
1681 /* The current vertex is to the right of xClip.
1682 */
1683 if( inside ){
1684 /* If the current vertex is to the right of xClip but
1685 ** the previous vertex was left of xClip, then draw a
1686 ** line segment from the previous vertex to until it
1687 ** intersects the vertical at xClip.
1688 */
1689 double x0, y0, yN;
1690 assert( i>0 );
1691 x0 = a[i*2-2];
1692 y0 = a[i*2-1];
1693 yN = y0 + (y - y0)*(xClip-x0)/(x-x0);
1694 b[numOutput*2] = -yN;
1695 b[numOutput*2+1] = xClip;
1696 numOutput++;
1697 assert( numOutput<=maxOutput );
1698 priorY = yN;
1699 inside = 0;
1700 }else if( i==0 ){
1701 /* If the first vertex is to the right of xClip, add
1702 ** a vertex that is the projection of the first vertex
1703 ** onto the vertical xClip line.
1704 */
1705 b[0] = -y;
1706 b[1] = xClip;
1707 numOutput = 1;
1708 priorY = y;
1709 }
1710 }else{
1711 /* The current vertex is to the left of xClip
1712 */
1713 if( !inside ){
1714 /* If the current vertex is on the left of xClip and
1715 ** one or more prior vertices where to the right, then
1716 ** we have to draw a line segment along xClip that extends
1717 ** from the spot where we first crossed from left to right
1718 ** to the spot where we cross back from right to left.
1719 */
1720 double x0, y0, yN;
1721 assert( i>0 );
1722 x0 = a[i*2-2];
1723 y0 = a[i*2-1];
1724 yN = y0 + (y - y0)*(xClip-x0)/(x-x0);
1725 if( yN!=priorY ){
1726 b[numOutput*2] = -yN;
1727 b[numOutput*2+1] = xClip;
1728 numOutput++;
1729 assert( numOutput<=maxOutput );
1730 }
1731 inside = 1;
1732 }
1733 b[numOutput*2] = -y;
1734 b[numOutput*2+1] = x;
1735 numOutput++;
1736 assert( numOutput<=maxOutput );
1737 }
1738 }
1739
1740 /* Interchange a[] and b[] in preparation for the next pass.
1741 */
1742 t = a;
1743 a = b;
1744 b = t;
1745 numVertex = numOutput;
1746 }
1747
1748 /* All clipping is now finished. Convert the coordinates from doubles
1749 ** into XPoints and translate the origin for the drawable.
1750 */
1751 for(i=0; i<numVertex; i++){
1752 translateAndAppendCoords(canvPtr, a[i*2], a[i*2+1], outArr, i);
1753 }
1754 if( tempArr!=staticSpace ){
1755 ckfree((char *) tempArr);
1756 }
1757 return numOutput;
1758 }
1759