A number of updates supporting batch mode, for colors and fonts,
[xcircuit.git] / functions.c
blob0f363dae916c2291073c78fd388a1df922fe7d8e
1 /*-------------------------------------------------------------------------*/
2 /* functions.c */
3 /* Copyright (c) 2002 Tim Edwards, Johns Hopkins University */
4 /*-------------------------------------------------------------------------*/
6 /*-------------------------------------------------------------------------*/
7 /* written by Tim Edwards, 8/13/93 */
8 /*-------------------------------------------------------------------------*/
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include <math.h>
14 #include <limits.h>
16 #ifndef _MSC_VER
17 #include <X11/Intrinsic.h>
18 #include <X11/StringDefs.h>
19 #endif
21 /*-------------------------------------------------------------------------*/
22 /* Local includes */
23 /*-------------------------------------------------------------------------*/
25 #ifdef TCL_WRAPPER
26 #include <tk.h>
27 #endif
29 #include "colordefs.h"
30 #include "xcircuit.h"
32 /*----------------------------------------------------------------------*/
33 /* Function prototype declarations */
34 /*----------------------------------------------------------------------*/
35 #include "prototypes.h"
37 /*-------------------------------------------------------------------------*/
38 /* External Variable definitions */
39 /*-------------------------------------------------------------------------*/
41 extern Display *dpy;
42 extern Pixmap STIPPLE[8];
43 extern XCWindowData *areawin;
44 extern Globaldata xobjs;
45 extern int number_colors;
46 extern colorindex *colorlist;
48 /*------------------------------------------------------------------------*/
49 /* find the squared length of a wire (or distance between two points in */
50 /* user space). */
51 /*------------------------------------------------------------------------*/
53 long sqwirelen(XPoint *userpt1, XPoint *userpt2)
55 long xdist, ydist;
57 xdist = (long)userpt2->x - (long)userpt1->x;
58 ydist = (long)userpt2->y - (long)userpt1->y;
59 return (xdist * xdist + ydist * ydist);
62 /*------------------------------------------------------------------------*/
63 /* floating-point version of the above */
64 /*------------------------------------------------------------------------*/
66 float fsqwirelen(XfPoint *userpt1, XfPoint *userpt2)
68 float xdist, ydist;
70 xdist = userpt2->x - userpt1->x;
71 ydist = userpt2->y - userpt1->y;
72 return (xdist * xdist + ydist * ydist);
75 /*------------------------------------------------------------------------*/
76 /* Find absolute distance between two points in user space */
77 /*------------------------------------------------------------------------*/
79 int wirelength(XPoint *userpt1, XPoint *userpt2)
81 u_long xdist, ydist;
83 xdist = (long)(userpt2->x) - (long)(userpt1->x);
84 ydist = (long)(userpt2->y) - (long)(userpt1->y);
85 return (int)sqrt((double)(xdist * xdist + ydist * ydist));
88 /*------------------------------------------------------------------------*/
89 /* Find the closest (squared) distance from a point to a line */
90 /*------------------------------------------------------------------------*/
92 long finddist(XPoint *linept1, XPoint *linept2, XPoint *userpt)
94 long a, b, c, frac;
95 float protod;
97 c = sqwirelen(linept1, linept2);
98 a = sqwirelen(linept1, userpt);
99 b = sqwirelen(linept2, userpt);
100 frac = a - b;
101 if (frac >= c) return b; /* "=" is important if c = 0 ! */
102 else if (-frac >= c) return a;
103 else {
104 protod = (float)(c + a - b);
105 return (a - (long)((protod * protod) / (float)(c << 2)));
109 /*----------------------------------------------------------------------*/
110 /* Decompose an arc segment into one to four bezier curves according */
111 /* the approximation algorithm lifted from the paper by L. Maisonobe */
112 /* (spaceroots.org). This decomposition is done when an arc in a path */
113 /* is read from an (older) xcircuit file, or when an arc is a selected */
114 /* item when a path is created. Because arcs are decomposed when */
115 /* encountered, we assume that the arc is the last element of the path. */
116 /*----------------------------------------------------------------------*/
118 void decomposearc(pathptr thepath)
120 float fnc, ang1, ang2;
121 short ncurves, i;
122 arcptr thearc;
123 genericptr *pgen;
124 splineptr *newspline;
125 double nu1, nu2, lambda1, lambda2, alpha, tansq;
126 XfPoint E1, E2, Ep1, Ep2;
127 Boolean reverse = FALSE;
129 pgen = thepath->plist + thepath->parts - 1;
130 if (ELEMENTTYPE(*pgen) != ARC) return;
131 thearc = TOARC(pgen);
133 if (thearc->radius < 0) {
134 reverse = TRUE;
135 thearc->radius = -thearc->radius;
138 fnc = (thearc->angle2 - thearc->angle1) / 90.0;
139 ncurves = (short)fnc;
140 if (fnc - (float)((int)fnc) > 0.01) ncurves++;
142 thepath->parts--; /* Forget the arc */
144 for (i = 0; i < ncurves; i++) {
145 if (reverse) { /* arc path is reverse direction */
146 if (i == 0)
147 ang1 = thearc->angle2;
148 else
149 ang1 -= 90;
151 if (i == ncurves - 1)
152 ang2 = thearc->angle1;
153 else
154 ang2 = ang1 - 90;
156 else { /* arc path is forward direction */
157 if (i == 0)
158 ang1 = thearc->angle1;
159 else
160 ang1 += 90;
162 if (i == ncurves - 1)
163 ang2 = thearc->angle2;
164 else
165 ang2 = ang1 + 90;
168 lambda1 = (double)ang1 * RADFAC;
169 lambda2 = (double)ang2 * RADFAC;
171 nu1 = atan2(sin(lambda1) / (double)thearc->yaxis,
172 cos(lambda1) / (double)thearc->radius);
173 nu2 = atan2(sin(lambda2) / (double)thearc->yaxis,
174 cos(lambda2) / (double)thearc->radius);
175 E1.x = (float)thearc->position.x +
176 (float)thearc->radius * (float)cos(nu1);
177 E1.y = (float)thearc->position.y +
178 (float)thearc->yaxis * (float)sin(nu1);
179 E2.x = (float)thearc->position.x +
180 (float)thearc->radius * (float)cos(nu2);
181 E2.y = (float)thearc->position.y +
182 (float)thearc->yaxis * (float)sin(nu2);
183 Ep1.x = -(float)thearc->radius * (float)sin(nu1);
184 Ep1.y = (float)thearc->yaxis * (float)cos(nu1);
185 Ep2.x = -(float)thearc->radius * (float)sin(nu2);
186 Ep2.y = (float)thearc->yaxis * (float)cos(nu2);
188 tansq = tan((nu2 - nu1) / 2.0);
189 tansq *= tansq;
190 alpha = sin(nu2 - nu1) * 0.33333 * (sqrt(4 + (3 * tansq)) - 1);
192 NEW_SPLINE(newspline, thepath);
193 splinedefaults(*newspline, 0, 0);
194 (*newspline)->style = thearc->style;
195 (*newspline)->color = thearc->color;
196 (*newspline)->width = thearc->width;
198 (*newspline)->ctrl[0].x = E1.x;
199 (*newspline)->ctrl[0].y = E1.y;
201 (*newspline)->ctrl[1].x = E1.x + alpha * Ep1.x;
202 (*newspline)->ctrl[1].y = E1.y + alpha * Ep1.y;
204 (*newspline)->ctrl[2].x = E2.x - alpha * Ep2.x;
205 (*newspline)->ctrl[2].y = E2.y - alpha * Ep2.y;
207 (*newspline)->ctrl[3].x = E2.x;
208 (*newspline)->ctrl[3].y = E2.y;
210 calcspline(*newspline);
213 /* Delete the arc */
214 free_single((genericptr)thearc);
217 /*----------------------------------------------------------------------*/
218 /* Calculate points for an arc */
219 /*----------------------------------------------------------------------*/
221 void calcarc(arcptr thearc)
223 short idx;
224 int sarc;
225 float theta, delta;
227 /* assume that angle2 > angle1 always: must be guaranteed by other routines */
229 sarc = (int)(thearc->angle2 - thearc->angle1) * RSTEPS;
230 thearc->number = (sarc / 360) + 1;
231 if (sarc % 360 != 0) thearc->number++;
233 delta = RADFAC * ((float)(thearc->angle2 - thearc->angle1) / (thearc->number - 1));
234 theta = thearc->angle1 * RADFAC;
236 for (idx = 0; idx < thearc->number - 1; idx++) {
237 thearc->points[idx].x = (float)thearc->position.x +
238 fabs((float)thearc->radius) * cos(theta);
239 thearc->points[idx].y = (float)thearc->position.y +
240 (float)thearc->yaxis * sin(theta);
241 theta += delta;
244 /* place last point exactly to avoid roundoff error */
246 theta = thearc->angle2 * RADFAC;
247 thearc->points[thearc->number - 1].x = (float)thearc->position.x +
248 fabs((float)thearc->radius) * cos(theta);
249 thearc->points[thearc->number - 1].y = (float)thearc->position.y +
250 (float)thearc->yaxis * sin(theta);
252 if (thearc->radius < 0) reversefpoints(thearc->points, thearc->number);
255 /*------------------------------------------------------------------------*/
256 /* Create a Bezier curve approximation from control points */
257 /* (using PostScript formula for Bezier cubic curve) */
258 /*------------------------------------------------------------------------*/
260 float par[INTSEGS];
261 float parsq[INTSEGS];
262 float parcb[INTSEGS];
264 void initsplines()
266 float t;
267 short idx;
269 for (idx = 0; idx < INTSEGS; idx++) {
270 t = (float)(idx + 1) / (INTSEGS + 1);
271 par[idx] = t;
272 parsq[idx] = t * t;
273 parcb[idx] = parsq[idx] * t;
277 /*------------------------------------------------------------------------*/
278 /* Compute spline coefficients */
279 /*------------------------------------------------------------------------*/
281 void computecoeffs(splineptr thespline, float *ax, float *bx, float *cx,
282 float *ay, float *by, float *cy)
284 *cx = 3.0 * (float)(thespline->ctrl[1].x - thespline->ctrl[0].x);
285 *bx = 3.0 * (float)(thespline->ctrl[2].x - thespline->ctrl[1].x) - *cx;
286 *ax = (float)(thespline->ctrl[3].x - thespline->ctrl[0].x) - *cx - *bx;
288 *cy = 3.0 * (float)(thespline->ctrl[1].y - thespline->ctrl[0].y);
289 *by = 3.0 * (float)(thespline->ctrl[2].y - thespline->ctrl[1].y) - *cy;
290 *ay = (float)(thespline->ctrl[3].y - thespline->ctrl[0].y) - *cy - *by;
293 /*------------------------------------------------------------------------*/
295 void calcspline(splineptr thespline)
297 float ax, bx, cx, ay, by, cy;
298 short idx;
300 computecoeffs(thespline, &ax, &bx, &cx, &ay, &by, &cy);
301 for (idx = 0; idx < INTSEGS; idx++) {
302 thespline->points[idx].x = ax * parcb[idx] + bx * parsq[idx] +
303 cx * par[idx] + (float)thespline->ctrl[0].x;
304 thespline->points[idx].y = ay * parcb[idx] + by * parsq[idx] +
305 cy * par[idx] + (float)thespline->ctrl[0].y;
309 /*------------------------------------------------------------------------*/
310 /* Find the (x,y) position and tangent rotation of a point on a spline */
311 /*------------------------------------------------------------------------*/
313 void findsplinepos(splineptr thespline, float t, XPoint *retpoint, float *retrot)
315 float ax, bx, cx, ay, by, cy;
316 float tsq = t * t;
317 float tcb = tsq * t;
318 double dxdt, dydt;
320 computecoeffs(thespline, &ax, &bx, &cx, &ay, &by, &cy);
321 retpoint->x = (short)(ax * tcb + bx * tsq + cx * t + (float)thespline->ctrl[0].x);
322 retpoint->y = (short)(ay * tcb + by * tsq + cy * t + (float)thespline->ctrl[0].y);
324 if (retrot != NULL) {
325 dxdt = (double)(3 * ax * tsq + 2 * bx * t + cx);
326 dydt = (double)(3 * ay * tsq + 2 * by * t + cy);
327 *retrot = INVRFAC * atan2(dxdt, dydt); /* reversed y, x */
328 if (*retrot < 0) *retrot += 360;
332 /*------------------------------------------------------------------------*/
333 /* floating-point version of the above */
334 /*------------------------------------------------------------------------*/
336 void ffindsplinepos(splineptr thespline, float t, XfPoint *retpoint)
338 float ax, bx, cx, ay, by, cy;
339 float tsq = t * t;
340 float tcb = tsq * t;
342 computecoeffs(thespline, &ax, &bx, &cx, &ay, &by, &cy);
343 retpoint->x = ax * tcb + bx * tsq + cx * t + (float)thespline->ctrl[0].x;
344 retpoint->y = ay * tcb + by * tsq + cy * t + (float)thespline->ctrl[0].y;
347 /*------------------------------------------------------------------------*/
348 /* Find the closest distance between a point and a spline and return the */
349 /* fractional distance along the spline of this point. */
350 /*------------------------------------------------------------------------*/
352 float findsplinemin(splineptr thespline, XPoint *upoint)
354 XfPoint *spt, flpt, newspt;
355 float minval = 1000000, tval, hval, ndist;
356 short j, ival;
358 flpt.x = (float)(upoint->x);
359 flpt.y = (float)(upoint->y);
361 /* get estimate from precalculated spline points */
363 for (spt = thespline->points; spt < thespline->points + INTSEGS;
364 spt++) {
365 ndist = fsqwirelen(spt, &flpt);
366 if (ndist < minval) {
367 minval = ndist;
368 ival = (short)(spt - thespline->points);
371 tval = (float)(ival + 1) / (INTSEGS + 1);
372 hval = 0.5 / (INTSEGS + 1);
374 /* short fixed iterative loop to converge on minimum t */
376 for (j = 0; j < 5; j++) {
377 tval += hval;
378 ffindsplinepos(thespline, tval, &newspt);
379 ndist = fsqwirelen(&newspt, &flpt);
380 if (ndist < minval) minval = ndist;
381 else {
382 tval -= hval * 2;
383 ffindsplinepos(thespline, tval, &newspt);
384 ndist = fsqwirelen(&newspt, &flpt);
385 if (ndist < minval) minval = ndist;
386 else tval += hval;
388 hval /= 2;
391 if (tval < 0.1) {
392 if ((float)sqwirelen(&(thespline->ctrl[0]), upoint) < minval) tval = 0;
394 else if (tval > 0.9) {
395 if ((float)sqwirelen(&(thespline->ctrl[3]), upoint) < minval) tval = 1;
397 return tval;
400 /*----------------------------------------------------------------------*/
401 /* Convert a polygon to a Bezier curve path */
402 /* Curve must be selected and there must be only one selection. */
403 /* */
404 /* Note that this routine will draw inside the perimeter of a convex */
405 /* hull. A routine that places spline endpoints on the polygon */
406 /* vertices will draw outside the perimeter of a convex hull. An */
407 /* optimal algorithm presumably zeros the total area between the curve */
408 /* and the polygon (positive and negative), but I haven't worked out */
409 /* what that solution is. The algorithm below seems good enough for */
410 /* most purposes. */
411 /*----------------------------------------------------------------------*/
413 void converttocurve()
415 genericptr *ggen;
416 splineptr *newspline;
417 polyptr thispoly;
418 pathptr *newpath;
419 short *newselect;
420 XPoint firstpoint, lastpoint, initpoint;
421 int i, numpoints;
423 if (areawin->selects != 1) return;
425 thispoly = TOPOLY(topobject->plist + (*areawin->selectlist));
426 if (ELEMENTTYPE(thispoly) != POLYGON) return;
427 if (thispoly->number < 3) return; /* Will not convert */
429 standard_element_delete(ERASE);
430 if ((thispoly->style & UNCLOSED) && (thispoly->number == 3)) {
431 NEW_SPLINE(newspline, topobject);
432 splinedefaults(*newspline, 0, 0);
433 (*newspline)->ctrl[0] = thispoly->points[0];
434 (*newspline)->ctrl[1] = thispoly->points[1];
435 (*newspline)->ctrl[2] = thispoly->points[1];
436 (*newspline)->ctrl[3] = thispoly->points[2];
438 else {
439 numpoints = thispoly->number;
441 /* If the polygon is closed but the first and last points */
442 /* overlap, treat the last point as if it doesn't exist. */
444 if (!(thispoly->style & UNCLOSED))
445 if ((thispoly->points[0].x == thispoly->points[thispoly->number - 1].x)
446 && (thispoly->points[0].y ==
447 thispoly->points[thispoly->number - 1].y))
448 numpoints--;
450 NEW_PATH(newpath, topobject);
451 pathdefaults(*newpath, 0, 0);
452 (*newpath)->style = thispoly->style;
454 if (!(thispoly->style & UNCLOSED)) {
455 lastpoint = thispoly->points[numpoints - 1];
456 initpoint.x = (lastpoint.x + thispoly->points[0].x) / 2;
457 initpoint.y = (lastpoint.y + thispoly->points[0].y) / 2;
458 firstpoint.x = (thispoly->points[0].x
459 + thispoly->points[1].x) / 2;
460 firstpoint.y = (thispoly->points[0].y
461 + thispoly->points[1].y) / 2;
463 NEW_SPLINE(newspline, (*newpath));
464 splinedefaults(*newspline, 0, 0);
465 (*newspline)->ctrl[0] = initpoint;
466 (*newspline)->ctrl[1] = thispoly->points[0];
467 (*newspline)->ctrl[2] = thispoly->points[0];
468 (*newspline)->ctrl[3] = firstpoint;
469 calcspline(*newspline);
471 else
472 firstpoint = thispoly->points[0];
474 for (i = 0; i < numpoints - ((!(thispoly->style & UNCLOSED)) ?
475 2 : 3); i++) {
476 lastpoint.x = (thispoly->points[i + 1].x
477 + thispoly->points[i + 2].x) / 2;
478 lastpoint.y = (thispoly->points[i + 1].y
479 + thispoly->points[i + 2].y) / 2;
481 NEW_SPLINE(newspline, (*newpath));
482 splinedefaults(*newspline, 0, 0);
483 (*newspline)->ctrl[0] = firstpoint;
484 (*newspline)->ctrl[1] = thispoly->points[i + 1];
485 (*newspline)->ctrl[2] = thispoly->points[i + 1];
486 (*newspline)->ctrl[3] = lastpoint;
487 firstpoint = lastpoint;
488 calcspline(*newspline);
490 if (!(thispoly->style & UNCLOSED))
491 lastpoint = initpoint;
492 else
493 lastpoint = thispoly->points[i + 2];
495 NEW_SPLINE(newspline, (*newpath));
496 splinedefaults(*newspline, 0, 0);
497 (*newspline)->ctrl[0] = firstpoint;
498 (*newspline)->ctrl[1] = thispoly->points[i + 1];
499 (*newspline)->ctrl[2] = thispoly->points[i + 1];
500 (*newspline)->ctrl[3] = lastpoint;
502 calcspline(*newspline);
503 calcbbox(areawin->topinstance);
504 setoptionmenu();
505 drawarea(NULL, NULL, NULL);
508 /*----------------------------------------------------------------------*/
509 /* Find closest point of a polygon to the cursor */
510 /*----------------------------------------------------------------------*/
512 short closepointdistance(polyptr curpoly, XPoint *cursloc, short *mindist)
514 short curdist;
515 XPoint *curpt, *savept;
517 curpt = savept = curpoly->points;
518 *mindist = wirelength(curpt, cursloc);
519 while (++curpt < curpoly->points + curpoly->number) {
520 curdist = wirelength(curpt, cursloc);
521 if (curdist < *mindist) {
522 *mindist = curdist;
523 savept = curpt;
526 return (short)(savept - curpoly->points);
529 /*----------------------------------------------------------------------------*/
530 /* Find closest point of a polygon to the cursor */
531 /*----------------------------------------------------------------------------*/
533 short closepoint(polyptr curpoly, XPoint *cursloc)
535 short mindist;
536 return closepointdistance(curpoly, cursloc, &mindist);
539 /*----------------------------------------------------------------------------*/
540 /* Find the distance to the closest point of a polygon to the cursor */
541 /*----------------------------------------------------------------------------*/
543 short closedistance(polyptr curpoly, XPoint *cursloc)
545 short mindist;
546 closepointdistance(curpoly, cursloc, &mindist);
547 return mindist;
550 /*----------------------------------------------------------------------------*/
551 /* Coordinate system transformations */
552 /*----------------------------------------------------------------------------*/
554 /*------------------------------------------------------------------------------*/
555 /* Check screen bounds: minimum, maximum scale and translation is determined */
556 /* by values which fit in an X11 type XPoint (short int). If the window */
557 /* extremes exceed type short when mapped to user space, or if the page */
558 /* bounds exceed type short when mapped to X11 window space, return error. */
559 /*------------------------------------------------------------------------------*/
561 short checkbounds()
563 long lval;
565 /* check window-to-user space */
567 lval = 2 * (long)((float) (areawin->width) / areawin->vscale) +
568 (long)areawin->pcorner.x;
569 if (lval != (long)((short)lval)) return -1;
570 lval = 2 * (long)((float) (areawin->height) / areawin->vscale) +
571 (long)areawin->pcorner.y;
572 if (lval != (long)((short)lval)) return -1;
574 /* check user-to-window space */
576 lval = (long)((float)(topobject->bbox.lowerleft.x - areawin->pcorner.x) *
577 areawin->vscale);
578 if (lval != (long)((short)lval)) return -1;
579 lval = (long)areawin->height - (long)((float)(topobject->bbox.lowerleft.y -
580 areawin->pcorner.y) * areawin->vscale);
581 if (lval != (long)((short)lval)) return -1;
583 lval = (long)((float)(topobject->bbox.lowerleft.x + topobject->bbox.width -
584 areawin->pcorner.x) * areawin->vscale);
585 if (lval != (long)((short)lval)) return -1;
586 lval = (long)areawin->height - (long)((float)(topobject->bbox.lowerleft.y +
587 topobject->bbox.height - areawin->pcorner.y) * areawin->vscale);
588 if (lval != (long)((short)lval)) return -1;
590 return 0;
593 /*------------------------------------------------------------------------*/
594 /* Transform X-window coordinate to xcircuit coordinate system */
595 /*------------------------------------------------------------------------*/
597 void window_to_user(short xw, short yw, XPoint *upt)
599 float tmpx, tmpy;
601 tmpx = (float)xw / areawin->vscale + (float)areawin->pcorner.x;
602 tmpy = (float)(areawin->height - yw) / areawin->vscale +
603 (float)areawin->pcorner.y;
605 tmpx += (tmpx > 0) ? 0.5 : -0.5;
606 tmpy += (tmpy > 0) ? 0.5 : -0.5;
608 upt->x = (short)tmpx;
609 upt->y = (short)tmpy;
612 /*------------------------------------------------------------------------*/
613 /* Transform xcircuit coordinate back to X-window coordinate system */
614 /*------------------------------------------------------------------------*/
616 void user_to_window(XPoint upt, XPoint *wpt)
618 float tmpx, tmpy;
620 tmpx = (float)(upt.x - areawin->pcorner.x) * areawin->vscale;
621 tmpy = (float)areawin->height - (float)(upt.y - areawin->pcorner.y)
622 * areawin->vscale;
624 tmpx += (tmpx > 0) ? 0.5 : -0.5;
625 tmpy += (tmpy > 0) ? 0.5 : -0.5;
627 wpt->x = (short)tmpx;
628 wpt->y = (short)tmpy;
631 /*----------------------------------------------------------------------*/
632 /* Transformations in the object hierarchy */
633 /*----------------------------------------------------------------------*/
635 /*----------------------------------------------------------------------*/
636 /* Return rotation relative to a specific CTM */
637 /*----------------------------------------------------------------------*/
639 float UGetCTMRotation(Matrix *ctm)
641 float rads = (float)atan2((double)(ctm->d), (double)(ctm->a));
642 return rads / RADFAC;
645 /*----------------------------------------------------------------------*/
646 /* Return rotation relative to the top level */
647 /* Note that UTopRotation() is also the rotation relative to the window */
648 /* since the top-level drawing page is always upright relative to the */
649 /* window. Thus, there is no routine UTopDrawingRotation(). */
650 /*----------------------------------------------------------------------*/
652 float UTopRotation()
654 return UGetCTMRotation(DCTM);
657 /*----------------------------------------------------------------------*/
658 /* Return scale relative to a specific CTM */
659 /*----------------------------------------------------------------------*/
661 float UGetCTMScale(Matrix *ctm)
663 return (float)(sqrt((double)(ctm->a * ctm->a + ctm->d * ctm->d)));
666 /*----------------------------------------------------------------------*/
667 /* Return scale relative to window */
668 /*----------------------------------------------------------------------*/
670 float UTopScale()
672 return UGetCTMScale(DCTM);
675 /*----------------------------------------------------------------------*/
676 /* Return scale multiplied by length */
677 /*----------------------------------------------------------------------*/
679 float UTopTransScale(float length)
681 return (float)(length * UTopScale());
684 /*----------------------------------------------------------------------*/
685 /* Return scale relative to the top-level schematic (not the window) */
686 /*----------------------------------------------------------------------*/
688 float UTopDrawingScale()
690 Matrix lctm, wctm;
691 UCopyCTM(DCTM, &lctm);
692 UResetCTM(&wctm);
693 UMakeWCTM(&wctm);
694 InvertCTM(&wctm);
695 UPreMultCTMbyMat(&wctm, &lctm);
696 return UGetCTMScale(&wctm);
699 /*----------------------------------------------------------------------*/
700 /* Return position offset relative to a specific CTM */
701 /*----------------------------------------------------------------------*/
703 void UGetCTMOffset(Matrix *ctm, int *offx, int *offy)
705 if (offx) *offx = (int)ctm->c;
706 if (offy) *offy = (int)ctm->f;
709 /*----------------------------------------------------------------------*/
710 /* Return position offset relative to top-level */
711 /*----------------------------------------------------------------------*/
713 void UTopOffset(int *offx, int *offy)
715 UGetCTMOffset(DCTM, offx, offy);
718 /*----------------------------------------------------------------------*/
719 /* Return postion relative to the top-level schematic (not the window) */
720 /*----------------------------------------------------------------------*/
722 void UTopDrawingOffset(int *offx, int *offy)
724 Matrix lctm, wctm;
725 UCopyCTM(DCTM, &lctm);
726 UResetCTM(&wctm);
727 UMakeWCTM(&wctm);
728 InvertCTM(&wctm);
729 UPreMultCTMbyMat(&wctm, &lctm);
730 UGetCTMOffset(&wctm, offx, offy);
733 /*----------------------------------------------------------------------*/
734 /* Get the cursor position */
735 /*----------------------------------------------------------------------*/
737 XPoint UGetCursor()
739 Window nullwin;
740 int nullint, xpos, ypos;
741 u_int nullui;
742 XPoint newpos;
744 #ifdef TCL_WRAPPER
745 /* Don't use areawin->window; if called from inside an object */
746 /* (e.g., "here" in a Tcl expression), areawin->window will be */
747 /* an off-screen pixmap, and cause a crash. */
748 #ifndef _MSC_VER
749 XQueryPointer(dpy, Tk_WindowId(areawin->area), &nullwin, &nullwin,
750 &nullint, &nullint, &xpos, &ypos, &nullui);
751 #else
752 XQueryPointer_TkW32(dpy, Tk_WindowId(areawin->area), &nullwin, &nullwin,
753 &nullint, &nullint, &xpos, &ypos, &nullui);
754 #endif
755 #else
756 XQueryPointer(dpy, areawin->window, &nullwin, &nullwin, &nullint,
757 &nullint, &xpos, &ypos, &nullui);
758 #endif
760 newpos.x = xpos;
761 newpos.y = ypos;
763 return newpos;
766 /*----------------------------------------------------------------------*/
767 /* Get the cursor position and translate to user coordinates */
768 /*----------------------------------------------------------------------*/
770 XPoint UGetCursorPos()
772 XPoint winpos, userpos;
774 winpos = UGetCursor();
776 window_to_user(winpos.x, winpos.y, &userpos);
778 return userpos;
781 /*----------------------------------------------------------------------*/
782 /* Translate a point to the nearest snap-to grid point */
783 /*----------------------------------------------------------------------*/
784 /* user coordinates to user coordinates version */
786 void u2u_snap(XPoint *uvalue)
788 float tmpx, tmpy;
789 float tmpix, tmpiy;
791 if (areawin->snapto) {
792 tmpx = (float)uvalue->x / xobjs.pagelist[areawin->page]->snapspace;
793 if (tmpx > 0)
794 tmpix = (float)((int)(tmpx + 0.5));
795 else
796 tmpix = (float)((int)(tmpx - 0.5));
798 tmpy = (float)uvalue->y / xobjs.pagelist[areawin->page]->snapspace;
799 if (tmpy > 0)
800 tmpiy = (float)((int)(tmpy + 0.5));
801 else
802 tmpiy = (float)((int)(tmpy - 0.5));
804 tmpix *= xobjs.pagelist[areawin->page]->snapspace;
805 tmpix += (tmpix > 0) ? 0.5 : -0.5;
806 tmpiy *= xobjs.pagelist[areawin->page]->snapspace;
807 tmpiy += (tmpiy > 0) ? 0.5 : -0.5;
809 uvalue->x = (int)tmpix;
810 uvalue->y = (int)tmpiy;
814 /*------------------------------------------------------------------------*/
815 /* window coordinates to user coordinates version */
816 /*------------------------------------------------------------------------*/
818 void snap(short valuex, short valuey, XPoint *returnpt)
820 window_to_user(valuex, valuey, returnpt);
821 u2u_snap(returnpt);
824 /*------------------------------------------------------------------------*/
825 /* Transform object coordinates through scale, translation, and rotation */
826 /* This routine attempts to match the PostScript definition of trans- */
827 /* formation matrices. */
828 /*------------------------------------------------------------------------*/
830 /*------------------------------------------------------------------------*/
831 /* Current transformation matrix manipulation routines */
832 /*------------------------------------------------------------------------*/
834 void UResetCTM(Matrix *ctm)
836 ctm->a = ctm->e = 1;
837 ctm->b = ctm->d = 0;
838 ctm->c = ctm->f = 0; /* 0.5 for nearest-int real->int conversion? */
840 #ifdef HAVE_CAIRO
841 if (ctm == DCTM && areawin->redraw_ongoing)
842 xc_cairo_set_matrix(ctm);
843 #endif /* HAVE_CAIRO */
846 /*------------------------------------------------------------------------*/
848 void InvertCTM(Matrix *ctm)
850 float det = ctm->a * ctm->e - ctm->b * ctm->d;
851 float tx = ctm->b * ctm->f - ctm->c * ctm->e;
852 float ty = ctm->d * ctm->c - ctm->a * ctm->f;
854 float tmpa = ctm->a;
856 ctm->b = -ctm->b / det;
857 ctm->d = -ctm->d / det;
859 ctm->a = ctm->e / det;
860 ctm->e = tmpa / det;
861 ctm->c = tx / det;
862 ctm->f = ty / det;
864 #ifdef HAVE_CAIRO
865 if (ctm == DCTM && areawin->redraw_ongoing)
866 xc_cairo_set_matrix(ctm);
867 #endif /* HAVE_CAIRO */
870 /*------------------------------------------------------------------------*/
872 void UCopyCTM(fctm, tctm)
873 Matrix *fctm, *tctm;
875 tctm->a = fctm->a;
876 tctm->b = fctm->b;
877 tctm->c = fctm->c;
878 tctm->d = fctm->d;
879 tctm->e = fctm->e;
880 tctm->f = fctm->f;
882 #ifdef HAVE_CAIRO
883 if (tctm == DCTM && areawin->redraw_ongoing)
884 xc_cairo_set_matrix(tctm);
885 #endif /* HAVE_CAIRO */
888 /*-------------------------------------------------------------------------*/
889 /* Multiply CTM by current screen position and scale to get transformation */
890 /* matrix from a user point to the X11 window */
891 /*-------------------------------------------------------------------------*/
893 void UMakeWCTM(Matrix *ctm)
895 ctm->a *= areawin->vscale;
896 ctm->b *= areawin->vscale;
897 ctm->c = (ctm->c - (float)areawin->pcorner.x) * areawin->vscale
898 + areawin->panx;
900 ctm->d *= -areawin->vscale;
901 ctm->e *= -areawin->vscale;
902 ctm->f = (float)areawin->height + ((float)areawin->pcorner.y - ctm->f) *
903 areawin->vscale + areawin->pany;
905 #ifdef HAVE_CAIRO
906 if (ctm == DCTM && areawin->redraw_ongoing)
907 xc_cairo_set_matrix(ctm);
908 #endif /* HAVE_CAIRO */
911 /*------------------------------------------------------------------------*/
913 void UMultCTM(Matrix *ctm, XPoint position, float scale, float rotate)
915 float tmpa, tmpb, tmpd, tmpe, yscale;
916 float mata, matb, matc;
917 double drot = (double)rotate * RADFAC;
919 yscale = abs(scale); /* -scale implies flip in x direction only */
921 tmpa = scale * cos(drot);
922 tmpb = yscale * sin(drot);
923 tmpd = -scale * sin(drot);
924 tmpe = yscale * cos(drot);
926 mata = ctm->a * tmpa + ctm->d * tmpb;
927 matb = ctm->b * tmpa + ctm->e * tmpb;
928 matc = ctm->c * tmpa + ctm->f * tmpb + position.x;
930 ctm->d = ctm->d * tmpe + ctm->a * tmpd;
931 ctm->e = ctm->e * tmpe + ctm->b * tmpd;
932 ctm->f = ctm->f * tmpe + ctm->c * tmpd + position.y;
934 ctm->a = mata;
935 ctm->b = matb;
936 ctm->c = matc;
938 #ifdef HAVE_CAIRO
939 if (ctm == DCTM && areawin->redraw_ongoing)
940 xc_cairo_set_matrix(ctm);
941 #endif /* HAVE_CAIRO */
944 /*----------------------------------------------------------------------*/
945 /* Slanting function x' = x + beta * y, y' = y */
946 /*----------------------------------------------------------------------*/
948 void USlantCTM(Matrix *ctm, float beta)
950 ctm->b += ctm->a * beta;
951 ctm->e += ctm->d * beta;
953 #ifdef HAVE_CAIRO
954 if (ctm == DCTM && areawin->redraw_ongoing)
955 xc_cairo_set_matrix(ctm);
956 #endif /* HAVE_CAIRO */
959 #define EPS 1e-9
960 /*----------------------------------------------------------------------*/
961 /* Transform text to make it right-side up within 90 degrees of page */
962 /* NOTE: This is not yet resolved, as xcircuit does not agree with */
963 /* PostScript in a few cases! */
964 /*----------------------------------------------------------------------*/
966 void UPreScaleCTM(Matrix *ctm)
968 /* negative X scale (-1, +1) */
969 if ((ctm->a < -EPS) || ((ctm->a < EPS) && (ctm->a > -EPS) &&
970 ((ctm->d * ctm->b) < 0))) {
971 ctm->a = -ctm->a;
972 ctm->d = -ctm->d;
975 /* negative Y scale (+1, -1) */
976 if (ctm->e > EPS) {
977 ctm->e = -ctm->e;
978 ctm->b = -ctm->b;
981 /* At 90, 270 degrees need special attention to avoid discrepencies */
982 /* with the PostScript output due to roundoff error. This code */
983 /* matches what PostScript produces. */
985 #ifdef HAVE_CAIRO
986 if (ctm == DCTM && areawin->redraw_ongoing)
987 xc_cairo_set_matrix(ctm);
988 #endif /* HAVE_CAIRO */
991 /*----------------------------------------------------------------------*/
992 /* Adjust anchoring and CTM as necessary for flip invariance */
993 /*----------------------------------------------------------------------*/
995 short flipadjust(short anchor)
997 short tmpanchor = anchor & (~FLIPINV);
999 if (anchor & FLIPINV) {
1000 if (((DCTM)->a < -EPS) || (((DCTM)->a < EPS) && ((DCTM)->a > -EPS) &&
1001 (((DCTM)->d * (DCTM)->b) < 0))) {
1002 if ((tmpanchor & (RIGHT | NOTLEFT)) != NOTLEFT)
1003 tmpanchor ^= (RIGHT | NOTLEFT);
1005 /* NOTE: Justification does not change under flip invariance. */
1007 if ((DCTM)->e > EPS) {
1008 if ((tmpanchor & (TOP | NOTBOTTOM)) != NOTBOTTOM)
1009 tmpanchor ^= (TOP | NOTBOTTOM);
1011 UPreScaleCTM(DCTM);
1013 return tmpanchor;
1016 /*------------------------------------------------------------------------*/
1018 void UPreMultCTM(Matrix *ctm, XPoint position, float scale, float rotate)
1020 float tmpa, tmpb, tmpd, tmpe, yscale;
1021 float mata, matd;
1022 double drot = (double)rotate * RADFAC;
1024 yscale = abs(scale); /* negative scale value implies flip in x only */
1026 tmpa = scale * cos(drot);
1027 tmpb = yscale * sin(drot);
1028 tmpd = -scale * sin(drot);
1029 tmpe = yscale * cos(drot);
1031 ctm->c += ctm->a * position.x + ctm->b * position.y;
1032 ctm->f += ctm->d * position.x + ctm->e * position.y;
1034 mata = ctm->a * tmpa + ctm->b * tmpd;
1035 ctm->b = ctm->a * tmpb + ctm->b * tmpe;
1037 matd = ctm->d * tmpa + ctm->e * tmpd;
1038 ctm->e = ctm->d * tmpb + ctm->e * tmpe;
1040 ctm->a = mata;
1041 ctm->d = matd;
1043 #ifdef HAVE_CAIRO
1044 if (ctm == DCTM && areawin->redraw_ongoing)
1045 xc_cairo_set_matrix(ctm);
1046 #endif /* HAVE_CAIRO */
1049 /*----------------------------------------------------------------------*/
1050 /* Direct Matrix-Matrix multiplication */
1051 /*----------------------------------------------------------------------*/
1053 void UPreMultCTMbyMat(Matrix *ctm, Matrix *pre)
1055 float mata, matd;
1057 mata = pre->a * ctm->a + pre->d * ctm->b;
1058 ctm->c += pre->c * ctm->a + pre->f * ctm->b;
1059 ctm->b = pre->b * ctm->a + pre->e * ctm->b;
1060 ctm->a = mata;
1062 matd = pre->a * ctm->d + pre->d * ctm->e;
1063 ctm->f += pre->c * ctm->d + pre->f * ctm->e;
1064 ctm->e = pre->b * ctm->d + pre->e * ctm->e;
1065 ctm->d = matd;
1067 #ifdef HAVE_CAIRO
1068 if (ctm == DCTM && areawin->redraw_ongoing)
1069 xc_cairo_set_matrix(ctm);
1070 #endif /* HAVE_CAIRO */
1073 /*------------------------------------------------------------------------*/
1075 void UTransformbyCTM(Matrix *ctm, XPoint *ipoints, XPoint *points, short number)
1077 pointlist current, ptptr = points;
1078 float fx, fy;
1079 /* short tmpx; (jdk) */
1081 for (current = ipoints; current < ipoints + number; current++, ptptr++) {
1082 fx = ctm->a * (float)current->x + ctm->b * (float)current->y + ctm->c;
1083 fy = ctm->d * (float)current->x + ctm->e * (float)current->y + ctm->f;
1085 ptptr->x = (fx >= 0) ? (short)(fx + 0.5) : (short)(fx - 0.5);
1086 ptptr->y = (fy >= 0) ? (short)(fy + 0.5) : (short)(fy - 0.5);
1090 /*------------------------------------------------------------------------*/
1091 /* (same as above routine but using type (float) for point values; this */
1092 /* is for calculation of Bezier curve internal points. */
1093 /*------------------------------------------------------------------------*/
1095 void UfTransformbyCTM(Matrix *ctm, XfPoint *fpoints, XPoint *points, short number)
1097 fpointlist current;
1098 pointlist new = points;
1099 float fx, fy;
1101 for (current = fpoints; current < fpoints + number; current++, new++) {
1102 fx = ctm->a * current->x + ctm->b * current->y + ctm->c;
1103 fy = ctm->d * current->x + ctm->e * current->y + ctm->f;
1104 new->x = (fx >= 0) ? (short)(fx + 0.5) : (short)(fx - 0.5);
1105 new->y = (fy >= 0) ? (short)(fy + 0.5) : (short)(fy - 0.5);
1109 /*------------------------------------------------------------------------*/
1111 void UPopCTM()
1113 Matrixptr lastmatrix;
1115 if (areawin->MatStack == NULL) {
1116 Wprintf("Matrix stack pop error");
1117 return;
1119 lastmatrix = areawin->MatStack->nextmatrix;
1120 free(areawin->MatStack);
1121 areawin->MatStack = lastmatrix;
1123 #ifdef HAVE_CAIRO
1124 if (areawin->area) {
1125 xc_cairo_set_matrix(lastmatrix);
1127 #endif /* HAVE_CAIRO */
1130 /*------------------------------------------------------------------------*/
1132 void UPushCTM()
1134 Matrixptr nmatrix;
1136 nmatrix = (Matrixptr)malloc(sizeof(Matrix));
1137 if (areawin->MatStack == NULL)
1138 UResetCTM(nmatrix);
1139 else
1140 UCopyCTM(areawin->MatStack, nmatrix);
1141 nmatrix->nextmatrix = areawin->MatStack;
1142 areawin->MatStack = nmatrix;
1145 /*------------------------------------------------------------------------*/
1147 void UTransformPoints(XPoint *points, XPoint *newpoints, short number,
1148 XPoint atpt, float scale, float rotate)
1150 Matrix LCTM;
1152 UResetCTM(&LCTM);
1153 UMultCTM(&LCTM, atpt, scale, rotate);
1154 UTransformbyCTM(&LCTM, points, newpoints, number);
1157 /*----------------------------------------------------*/
1158 /* Transform points inward to next hierarchical level */
1159 /*----------------------------------------------------*/
1161 void InvTransformPoints(XPoint *points, XPoint *newpoints, short number,
1162 XPoint atpt, float scale, float rotate)
1164 Matrix LCTM;
1166 UResetCTM(&LCTM);
1167 UPreMultCTM(&LCTM, atpt, scale, rotate);
1168 InvertCTM(&LCTM);
1169 UTransformbyCTM(&LCTM, points, newpoints, number);
1172 /*----------------------------------------------------------------------*/
1173 /* Adjust wire coords to force a wire to a horizontal or vertical */
1174 /* position. */
1175 /* "pospt" is the target position for the point of interest. */
1176 /* "cycle" is the point number in the polygon of the point of interest. */
1177 /* cycle == -1 is equivalent to the last point of the polygon. */
1178 /* If "strict" is TRUE then single-segment wires are forced manhattan */
1179 /* even if that means that the endpoint drifts from the target point. */
1180 /* If "strict" is FALSE then single-segment wires will become non- */
1181 /* manhattan so that the target point is reached. */
1182 /* NOTE: It might be preferable to add a segment to maintain a */
1183 /* manhattan layout, except that we want to avoid merging nets */
1184 /* together. . . */
1185 /*----------------------------------------------------------------------*/
1187 void manhattanize(XPoint *pospt, polyptr newpoly, short cycle, Boolean strict)
1189 XPoint *curpt, *bpt, *bbpt, *fpt, *ffpt;
1190 int deltax, deltay;
1192 if (newpoly->number == 1) return; /* sanity check */
1194 if (cycle == -1 || cycle == newpoly->number - 1) {
1195 curpt = newpoly->points + newpoly->number - 1;
1196 bpt = newpoly->points + newpoly->number - 2;
1197 fpt = NULL;
1198 ffpt = NULL;
1199 if (newpoly->number > 2)
1200 bbpt = newpoly->points + newpoly->number - 3;
1201 else
1202 bbpt = NULL;
1204 else if (cycle == 0) {
1205 curpt = newpoly->points;
1206 fpt = newpoly->points + 1;
1207 bpt = NULL;
1208 bbpt = NULL;
1209 if (newpoly->number > 2)
1210 ffpt = newpoly->points + 2;
1211 else
1212 ffpt = NULL;
1214 else {
1215 curpt = newpoly->points + cycle;
1216 fpt = newpoly->points + cycle + 1;
1217 bpt = newpoly->points + cycle - 1;
1218 if (cycle > 1)
1219 bbpt = newpoly->points + cycle - 2;
1220 else
1221 bbpt = NULL;
1223 if (cycle < newpoly->number - 2)
1224 ffpt = newpoly->points + cycle + 2;
1225 else
1226 ffpt = NULL;
1229 /* enforce constraints on point behind cycle position */
1231 if (bpt != NULL) {
1232 if (bbpt != NULL) {
1233 if (bpt->x == bbpt->x) bpt->y = pospt->y;
1234 if (bpt->y == bbpt->y) bpt->x = pospt->x;
1236 else if (strict) {
1237 deltax = abs(bpt->x - pospt->x);
1238 deltay = abs(bpt->y - pospt->y);
1240 /* Only one segment---just make sure it's horizontal or vertical */
1241 if (deltay > deltax) pospt->x = bpt->x;
1242 else pospt->y = bpt->y;
1246 /* enforce constraints on point forward of cycle position */
1248 if (fpt != NULL) {
1249 if (ffpt != NULL) {
1250 if (fpt->x == ffpt->x) fpt->y = pospt->y;
1251 if (fpt->y == ffpt->y) fpt->x = pospt->x;
1253 else if (strict) {
1254 deltax = abs(fpt->x - pospt->x);
1255 deltay = abs(fpt->y - pospt->y);
1257 /* Only one segment---just make sure it's horizontal or vertical */
1258 if (deltay > deltax) pospt->x = fpt->x;
1259 else pospt->y = fpt->y;
1264 /*----------------------------------------------------------------------*/
1265 /* Bounding box calculation routines */
1266 /*----------------------------------------------------------------------*/
1268 void bboxcalc(short testval, short *lowerval, short *upperval)
1270 if (testval < *lowerval) *lowerval = testval;
1271 if (testval > *upperval) *upperval = testval;
1274 /*----------------------------------------------------------------------*/
1275 /* Bounding box calculation for elements which can be part of a path */
1276 /*----------------------------------------------------------------------*/
1278 void calcextents(genericptr *bboxgen, short *llx, short *lly,
1279 short *urx, short *ury)
1281 switch (ELEMENTTYPE(*bboxgen)) {
1282 case(POLYGON): {
1283 pointlist bboxpts;
1284 for (bboxpts = TOPOLY(bboxgen)->points; bboxpts < TOPOLY(bboxgen)->points
1285 + TOPOLY(bboxgen)->number; bboxpts++) {
1286 bboxcalc(bboxpts->x, llx, urx);
1287 bboxcalc(bboxpts->y, lly, ury);
1289 } break;
1291 case(SPLINE): {
1292 fpointlist bboxpts;
1293 bboxcalc(TOSPLINE(bboxgen)->ctrl[0].x, llx, urx);
1294 bboxcalc(TOSPLINE(bboxgen)->ctrl[0].y, lly, ury);
1295 bboxcalc(TOSPLINE(bboxgen)->ctrl[3].x, llx, urx);
1296 bboxcalc(TOSPLINE(bboxgen)->ctrl[3].y, lly, ury);
1297 for (bboxpts = TOSPLINE(bboxgen)->points; bboxpts <
1298 TOSPLINE(bboxgen)->points + INTSEGS; bboxpts++) {
1299 bboxcalc((short)(bboxpts->x), llx, urx);
1300 bboxcalc((short)(bboxpts->y), lly, ury);
1302 } break;
1304 case (ARC): {
1305 fpointlist bboxpts;
1306 for (bboxpts = TOARC(bboxgen)->points; bboxpts < TOARC(bboxgen)->points +
1307 TOARC(bboxgen)->number; bboxpts++) {
1308 bboxcalc((short)(bboxpts->x), llx, urx);
1309 bboxcalc((short)(bboxpts->y), lly, ury);
1311 } break;
1315 /*----------------------------------------------------------------------*/
1316 /* Calculate the bounding box of an object instance */
1317 /*----------------------------------------------------------------------*/
1319 void objinstbbox(objinstptr obbox, XPoint *npoints, int extend)
1321 XPoint points[4];
1323 points[0].x = points[1].x = obbox->bbox.lowerleft.x - extend;
1324 points[1].y = points[2].y = obbox->bbox.lowerleft.y + obbox->bbox.height
1325 + extend;
1326 points[2].x = points[3].x = obbox->bbox.lowerleft.x + obbox->bbox.width
1327 + extend;
1328 points[0].y = points[3].y = obbox->bbox.lowerleft.y - extend;
1330 UTransformPoints(points, npoints, 4, obbox->position,
1331 obbox->scale, obbox->rotation);
1334 /*----------------------------------------------------------------------*/
1335 /* Calculate the bounding box of a label */
1336 /*----------------------------------------------------------------------*/
1338 void labelbbox(labelptr labox, XPoint *npoints, objinstptr callinst)
1340 XPoint points[4];
1341 TextExtents tmpext;
1342 short j;
1344 tmpext = ULength(labox, callinst, NULL);
1345 points[0].x = points[1].x = (labox->anchor & NOTLEFT ?
1346 (labox->anchor & RIGHT ? -tmpext.maxwidth :
1347 -tmpext.maxwidth / 2) : 0);
1348 points[2].x = points[3].x = points[0].x + tmpext.maxwidth;
1349 points[0].y = points[3].y = (labox->anchor & NOTBOTTOM ?
1350 (labox->anchor & TOP ? -tmpext.ascent :
1351 -(tmpext.ascent + tmpext.base) / 2) : -tmpext.base)
1352 + tmpext.descent;
1353 points[1].y = points[2].y = points[0].y + tmpext.ascent - tmpext.descent;
1355 /* separate bounding box for pinlabels and infolabels */
1357 if (labox->pin)
1358 for (j = 0; j < 4; j++)
1359 pinadjust(labox->anchor, &points[j].x, &points[j].y, 1);
1361 UTransformPoints(points, npoints, 4, labox->position,
1362 labox->scale, labox->rotation);
1365 /*----------------------------------------------------------------------*/
1366 /* Calculate the bounding box of a graphic image */
1367 /*----------------------------------------------------------------------*/
1369 void graphicbbox(graphicptr gp, XPoint *npoints)
1371 XPoint points[4];
1372 int hw = xcImageGetWidth(gp->source) >> 1;
1373 int hh = xcImageGetHeight(gp->source) >> 1;
1375 points[1].x = points[2].x = hw;
1376 points[0].x = points[3].x = -hw;
1378 points[0].y = points[1].y = -hh;
1379 points[2].y = points[3].y = hh;
1381 UTransformPoints(points, npoints, 4, gp->position,
1382 gp->scale, gp->rotation);
1385 /*--------------------------------------------------------------*/
1386 /* Wrapper for single call to calcbboxsingle() in the netlister */
1387 /*--------------------------------------------------------------*/
1389 void calcinstbbox(genericptr *bboxgen, short *llx, short *lly, short *urx,
1390 short *ury)
1392 *llx = *lly = 32767;
1393 *urx = *ury = -32768;
1395 calcbboxsingle(bboxgen, areawin->topinstance, llx, lly, urx, ury);
1398 /*----------------------------------------------------------------------*/
1399 /* Bounding box calculation for a single generic element */
1400 /*----------------------------------------------------------------------*/
1402 void calcbboxsingle(genericptr *bboxgen, objinstptr thisinst,
1403 short *llx, short *lly, short *urx, short *ury)
1405 XPoint npoints[4];
1406 short j;
1408 /* For each screen element, compute the extents and revise bounding */
1409 /* box points, if necessary. */
1411 switch(ELEMENTTYPE(*bboxgen)) {
1413 case(OBJINST):
1414 objinstbbox(TOOBJINST(bboxgen), npoints, 0);
1416 for (j = 0; j < 4; j++) {
1417 bboxcalc(npoints[j].x, llx, urx);
1418 bboxcalc(npoints[j].y, lly, ury);
1420 break;
1422 case(LABEL):
1423 /* because a pin is offset from its position point, include */
1424 /* that point in the bounding box. */
1426 if (TOLABEL(bboxgen)->pin) {
1427 bboxcalc(TOLABEL(bboxgen)->position.x, llx, urx);
1428 bboxcalc(TOLABEL(bboxgen)->position.y, lly, ury);
1430 labelbbox(TOLABEL(bboxgen), npoints, thisinst);
1432 for (j = 0; j < 4; j++) {
1433 bboxcalc(npoints[j].x, llx, urx);
1434 bboxcalc(npoints[j].y, lly, ury);
1436 break;
1438 case(GRAPHIC):
1439 graphicbbox(TOGRAPHIC(bboxgen), npoints);
1440 for (j = 0; j < 4; j++) {
1441 bboxcalc(npoints[j].x, llx, urx);
1442 bboxcalc(npoints[j].y, lly, ury);
1444 break;
1446 case(PATH): {
1447 genericptr *pathc;
1448 for (pathc = TOPATH(bboxgen)->plist; pathc < TOPATH(bboxgen)->plist
1449 + TOPATH(bboxgen)->parts; pathc++)
1450 calcextents(pathc, llx, lly, urx, ury);
1451 } break;
1453 default:
1454 calcextents(bboxgen, llx, lly, urx, ury);
1458 /*------------------------------------------------------*/
1459 /* Find if an object is in the specified library */
1460 /*------------------------------------------------------*/
1462 Boolean object_in_library(short libnum, objectptr thisobject)
1464 short i;
1466 for (i = 0; i < xobjs.userlibs[libnum].number; i++) {
1467 if (*(xobjs.userlibs[libnum].library + i) == thisobject)
1468 return True;
1470 return False;
1473 /*-----------------------------------------------------------*/
1474 /* Find if an object is in the hierarchy of the given object */
1475 /* Returns the number (position in plist) or -1 if not found */
1476 /*-----------------------------------------------------------*/
1478 short find_object(objectptr pageobj, objectptr thisobject)
1480 short i, j;
1481 genericptr *pelem;
1483 for (i = 0; i < pageobj->parts; i++) {
1484 pelem = pageobj->plist + i;
1485 if (IS_OBJINST(*pelem)) {
1486 if ((TOOBJINST(pelem))->thisobject == thisobject)
1487 return i;
1488 else if ((j = find_object((TOOBJINST(pelem))->thisobject, thisobject)) >= 0)
1489 return i; /* was j---is this the right fix? */
1492 return -1;
1495 /*------------------------------------------------------*/
1496 /* Find all pages and libraries containing this object */
1497 /* and update accordingly. If this object is a page, */
1498 /* just update the page directory. */
1499 /*------------------------------------------------------*/
1501 void updatepagebounds(objectptr thisobject)
1503 short i, j;
1504 objectptr pageobj;
1506 if ((i = is_page(thisobject)) >= 0) {
1507 if (xobjs.pagelist[i]->background.name != (char *)NULL)
1508 backgroundbbox(i);
1509 updatepagelib(PAGELIB, i);
1511 else {
1512 for (i = 0; i < xobjs.pages; i++) {
1513 if (xobjs.pagelist[i]->pageinst != NULL) {
1514 pageobj = xobjs.pagelist[i]->pageinst->thisobject;
1515 if ((j = find_object(pageobj, thisobject)) >= 0) {
1516 calcbboxvalues(xobjs.pagelist[i]->pageinst,
1517 (genericptr *)(pageobj->plist + j));
1518 updatepagelib(PAGELIB, i);
1522 for (i = 0; i < xobjs.numlibs; i++)
1523 if (object_in_library(i, thisobject))
1524 composelib(i + LIBRARY);
1528 /*--------------------------------------------------------------*/
1529 /* Free memory for the schematic bounding box */
1530 /*--------------------------------------------------------------*/
1532 void invalidateschembbox(objinstptr thisinst)
1534 if (thisinst->schembbox != NULL) {
1535 free(thisinst->schembbox);
1536 thisinst->schembbox = NULL;
1540 /*--------------------------------------------------------------*/
1541 /* Calculate the bounding box for an object instance. Use the */
1542 /* existing bbox and finish calculation on all the elements */
1543 /* which have parameters not taking default values. */
1544 /* This finishes the calculation partially done by */
1545 /* calcbboxvalues(). */
1546 /*--------------------------------------------------------------*/
1548 void calcbboxinst(objinstptr thisinst)
1550 objectptr thisobj;
1551 genericptr *gelem;
1552 short llx, lly, urx, ury;
1554 short pllx, plly, purx, pury;
1555 Boolean hasschembbox = FALSE;
1556 Boolean didparamsubs = FALSE;
1558 if (thisinst == NULL) return;
1560 thisobj = thisinst->thisobject;
1562 llx = thisobj->bbox.lowerleft.x;
1563 lly = thisobj->bbox.lowerleft.y;
1564 urx = llx + thisobj->bbox.width;
1565 ury = lly + thisobj->bbox.height;
1567 pllx = plly = 32767;
1568 purx = pury = -32768;
1570 for (gelem = thisobj->plist; gelem < thisobj->plist + thisobj->parts;
1571 gelem++) {
1572 /* pins which do not appear outside of the object */
1573 /* contribute to the objects "schembbox". */
1575 if (IS_LABEL(*gelem)) {
1576 labelptr btext = TOLABEL(gelem);
1577 if (btext->pin && !(btext->anchor & PINVISIBLE)) {
1578 hasschembbox = TRUE;
1579 calcbboxsingle(gelem, thisinst, &pllx, &plly, &purx, &pury);
1580 continue;
1584 if (has_param(*gelem)) {
1585 if (didparamsubs == FALSE) {
1586 psubstitute(thisinst);
1587 didparamsubs = TRUE;
1589 calcbboxsingle(gelem, thisinst, &llx, &lly, &urx, &ury);
1592 /* If we have a clipmask, the clipmask is used to calculate the */
1593 /* bounding box, not the element it is masking. */
1595 switch(ELEMENTTYPE(*gelem)) {
1596 case POLYGON: case SPLINE: case ARC: case PATH:
1597 if (TOPOLY(gelem)->style & CLIPMASK) gelem++;
1598 break;
1602 thisinst->bbox.lowerleft.x = llx;
1603 thisinst->bbox.lowerleft.y = lly;
1604 thisinst->bbox.width = urx - llx;
1605 thisinst->bbox.height = ury - lly;
1607 if (hasschembbox) {
1608 if (thisinst->schembbox == NULL)
1609 thisinst->schembbox = (BBox *)malloc(sizeof(BBox));
1611 thisinst->schembbox->lowerleft.x = pllx;
1612 thisinst->schembbox->lowerleft.y = plly;
1613 thisinst->schembbox->width = purx - pllx;
1614 thisinst->schembbox->height = pury - plly;
1616 else
1617 invalidateschembbox(thisinst);
1620 /*--------------------------------------------------------------*/
1621 /* Update things based on a changed instance bounding box. */
1622 /* If the parameter was a single-instance */
1623 /* substitution, only the page should be updated. If the */
1624 /* parameter was a default value, the library should be updated */
1625 /* and any pages containing the object where the parameter */
1626 /* takes the default value. */
1627 /*--------------------------------------------------------------*/
1629 void updateinstparam(objectptr bobj)
1631 short i, j;
1632 objectptr pageobj;
1634 /* change bounds on pagelib and all pages */
1635 /* containing this *object* if and only if the object */
1636 /* instance takes the default value. Also update the */
1637 /* library page. */
1639 for (i = 0; i < xobjs.pages; i++)
1640 if (xobjs.pagelist[i]->pageinst != NULL) {
1641 pageobj = xobjs.pagelist[i]->pageinst->thisobject;
1642 if ((j = find_object(pageobj, topobject)) >= 0) {
1644 /* Really, we'd like to recalculate the bounding box only if the */
1645 /* parameter value is the default value which was just changed. */
1646 /* However, then any non-default values may contain the wrong */
1647 /* substitutions. */
1649 objinstptr cinst = TOOBJINST(pageobj->plist + j);
1650 if (cinst->thisobject->params == NULL) {
1651 calcbboxvalues(xobjs.pagelist[i]->pageinst, pageobj->plist + j);
1652 updatepagelib(PAGELIB, i);
1657 for (i = 0; i < xobjs.numlibs; i++)
1658 if (object_in_library(i, topobject))
1659 composelib(i + LIBRARY);
1662 /*--------------------------------------------------------------*/
1663 /* Calculate bbox on all elements of the given object */
1664 /*--------------------------------------------------------------*/
1666 void calcbbox(objinstptr binst)
1668 calcbboxvalues(binst, (genericptr *)NULL);
1669 if (binst == areawin->topinstance) {
1670 updatepagebounds(topobject);
1674 /*--------------------------------------------------------------*/
1675 /* Calculate bbox on the given element of the specified object. */
1676 /* This is a wrapper for calcbboxvalues() assuming that we're */
1677 /* on the top-level, and that page bounds need to be updated. */
1678 /*--------------------------------------------------------------*/
1680 void singlebbox(genericptr *gelem)
1682 calcbboxvalues(areawin->topinstance, (genericptr *)gelem);
1683 updatepagebounds(topobject);
1686 /*----------------------------------------------------------------------*/
1687 /* Extend bounding box based on selected elements only */
1688 /*----------------------------------------------------------------------*/
1690 void calcbboxselect()
1692 short *bsel;
1693 for (bsel = areawin->selectlist; bsel < areawin->selectlist +
1694 areawin->selects; bsel++)
1695 calcbboxvalues(areawin->topinstance, topobject->plist + *bsel);
1697 updatepagebounds(topobject);
1700 /*--------------------------------------------------------------*/
1701 /* Update Bounding box for an object. */
1702 /* If newelement == NULL, calculate bounding box from scratch. */
1703 /* Otherwise, expand bounding box to enclose newelement. */
1704 /*--------------------------------------------------------------*/
1706 void calcbboxvalues(objinstptr thisinst, genericptr *newelement)
1708 genericptr *bboxgen;
1709 short llx, lly, urx, ury;
1710 objectptr thisobj = thisinst->thisobject;
1712 /* no action if there are no elements */
1713 if (thisobj->parts == 0) return;
1715 /* If this object has parameters, then we will do a separate */
1716 /* bounding box calculation on parameterized parts. This */
1717 /* calculation ignores them, and the result is a base that the */
1718 /* instance bounding-box computation can use as a starting point. */
1720 /* set starting bounds as maximum bounds of screen */
1721 llx = lly = 32767;
1722 urx = ury = -32768;
1724 for (bboxgen = thisobj->plist; bboxgen < thisobj->plist +
1725 thisobj->parts; bboxgen++) {
1727 /* override the "for" loop if we're doing a single element */
1728 if (newelement != NULL) bboxgen = newelement;
1730 if ((thisobj->params == NULL) || (!has_param(*bboxgen))) {
1731 /* pins which do not appear outside of the object */
1732 /* are ignored now---will be computed per instance. */
1734 if (IS_LABEL(*bboxgen)) {
1735 labelptr btext = TOLABEL(bboxgen);
1736 if (btext->pin && !(btext->anchor & PINVISIBLE)) {
1737 goto nextgen;
1740 calcbboxsingle(bboxgen, thisinst, &llx, &lly, &urx, &ury);
1742 if (newelement == NULL)
1743 switch(ELEMENTTYPE(*bboxgen)) {
1744 case POLYGON: case SPLINE: case ARC: case PATH:
1745 if (TOPOLY(bboxgen)->style & CLIPMASK)
1746 bboxgen++;
1747 break;
1750 nextgen:
1751 if (newelement != NULL) break;
1754 /* if this is a single-element calculation and its bounding box */
1755 /* turned out to be smaller than the object's, then we need to */
1756 /* recompute the entire object's bounding box in case it got */
1757 /* smaller. This is not recursive, in spite of looks. */
1759 if (newelement != NULL) {
1760 if (llx > thisobj->bbox.lowerleft.x &&
1761 lly > thisobj->bbox.lowerleft.y &&
1762 urx < (thisobj->bbox.lowerleft.x + thisobj->bbox.width) &&
1763 ury < (thisobj->bbox.lowerleft.y + thisobj->bbox.height)) {
1764 calcbboxvalues(thisinst, NULL);
1765 return;
1767 else {
1768 bboxcalc(thisobj->bbox.lowerleft.x, &llx, &urx);
1769 bboxcalc(thisobj->bbox.lowerleft.y, &lly, &ury);
1770 bboxcalc(thisobj->bbox.lowerleft.x + thisobj->bbox.width, &llx, &urx);
1771 bboxcalc(thisobj->bbox.lowerleft.y + thisobj->bbox.height, &lly, &ury);
1775 /* Set the new bounding box. In pathological cases, such as a page */
1776 /* with only pin labels, the bounds may not have been changed from */
1777 /* their initial values. If so, then don't touch the bounding box. */
1779 if ((llx <= urx) && (lly <= ury)) {
1780 thisobj->bbox.lowerleft.x = llx;
1781 thisobj->bbox.lowerleft.y = lly;
1782 thisobj->bbox.width = urx - llx;
1783 thisobj->bbox.height = ury - lly;
1786 /* calculate instance-specific values */
1787 calcbboxinst(thisinst);
1790 /*------------------------------------------------------*/
1791 /* Center an object in the viewing window */
1792 /*------------------------------------------------------*/
1794 void centerview(objinstptr tinst)
1796 XPoint origin, corner;
1797 Dimension width, height;
1798 float fitwidth, fitheight;
1799 objectptr tobj = tinst->thisobject;
1801 origin = tinst->bbox.lowerleft;
1802 corner.x = origin.x + tinst->bbox.width;
1803 corner.y = origin.y + tinst->bbox.height;
1805 extendschembbox(tinst, &origin, &corner);
1807 width = corner.x - origin.x;
1808 height = corner.y - origin.y;
1810 fitwidth = (float)areawin->width / ((float)width + 2 * DEFAULTGRIDSPACE);
1811 fitheight = (float)areawin->height / ((float)height + 2 * DEFAULTGRIDSPACE);
1813 tobj->viewscale = (fitwidth < fitheight) ?
1814 min(MINAUTOSCALE, fitwidth) : min(MINAUTOSCALE, fitheight);
1816 tobj->pcorner.x = origin.x - (areawin->width
1817 / tobj->viewscale - width) / 2;
1818 tobj->pcorner.y = origin.y - (areawin->height
1819 / tobj->viewscale - height) / 2;
1821 /* Copy new position values to the current window */
1823 if ((areawin->topinstance != NULL) && (tobj == topobject)) {
1824 areawin->pcorner = tobj->pcorner;
1825 areawin->vscale = tobj->viewscale;
1829 /*-----------------------------------------------------------*/
1830 /* Refresh the window and scrollbars and write the page name */
1831 /*-----------------------------------------------------------*/
1833 void refresh(xcWidget bw, caddr_t clientdata, caddr_t calldata)
1835 areawin->redraw_needed = True;
1836 drawarea(NULL, NULL, NULL);
1837 if (areawin->scrollbarh)
1838 drawhbar(areawin->scrollbarh, NULL, NULL);
1839 if (areawin->scrollbarv)
1840 drawvbar(areawin->scrollbarv, NULL, NULL);
1841 printname(topobject);
1844 /*------------------------------------------------------*/
1845 /* Center the current page in the viewing window */
1846 /*------------------------------------------------------*/
1848 void zoomview(xcWidget w, caddr_t clientdata, caddr_t calldata)
1850 if (eventmode == NORMAL_MODE || eventmode == COPY_MODE ||
1851 eventmode == MOVE_MODE || eventmode == CATALOG_MODE ||
1852 eventmode == FONTCAT_MODE || eventmode == EFONTCAT_MODE ||
1853 eventmode == CATMOVE_MODE) {
1855 if (areawin->topinstance)
1856 centerview(areawin->topinstance);
1857 areawin->lastbackground = NULL;
1858 renderbackground();
1859 refresh(NULL, NULL, NULL);
1863 /*---------------------------------------------------------*/
1864 /* Basic X Graphics Routines in the User coordinate system */
1865 /*---------------------------------------------------------*/
1867 #ifndef HAVE_CAIRO
1868 void UDrawSimpleLine(XPoint *pt1, XPoint *pt2)
1870 XPoint newpt1, newpt2;
1872 if (!areawin->redraw_ongoing) {
1873 areawin->redraw_needed = True;
1874 return;
1877 UTransformbyCTM(DCTM, pt1, &newpt1, 1);
1878 UTransformbyCTM(DCTM, pt2, &newpt2, 1);
1880 DrawLine(dpy, areawin->window, areawin->gc,
1881 newpt1.x, newpt1.y, newpt2.x, newpt2.y);
1883 #endif /* !HAVE_CAIRO */
1885 /*-------------------------------------------------------------------------*/
1887 #ifndef HAVE_CAIRO
1888 void UDrawLine(XPoint *pt1, XPoint *pt2)
1890 float tmpwidth = UTopTransScale(xobjs.pagelist[areawin->page]->wirewidth);
1892 if (!areawin->redraw_ongoing) {
1893 areawin->redraw_needed = True;
1894 return;
1897 SetLineAttributes(dpy, areawin->gc, tmpwidth, LineSolid, CapRound, JoinBevel);
1898 UDrawSimpleLine(pt1, pt2);
1900 #endif /* !HAVE_CAIRO */
1902 /*----------------------------------------------------------------------*/
1903 /* Add circle at given point to indicate that the point is a parameter. */
1904 /* The circle is divided into quarters. For parameterized y-coordinate */
1905 /* the top and bottom quarters are drawn. For parameterized x- */
1906 /* coordinate, the left and right quarters are drawn. A full circle */
1907 /* indicates either both x- and y-coordinates are parameterized, or */
1908 /* else any other kind of parameterization (presently, not used). */
1909 /* */
1910 /* (note that the two angles in XDrawArc() are 1) the start angle, */
1911 /* measured in absolute 64th degrees from 0 (3 o'clock), and 2) the */
1912 /* path length, in relative 64th degrees (positive = counterclockwise, */
1913 /* negative = clockwise)). */
1914 /*----------------------------------------------------------------------*/
1916 #ifndef HAVE_CAIRO
1917 void UDrawCircle(XPoint *upt, u_char which)
1919 XPoint wpt;
1921 if (!areawin->redraw_ongoing) {
1922 areawin->redraw_needed = True;
1923 return;
1926 user_to_window(*upt, &wpt);
1927 SetThinLineAttributes(dpy, areawin->gc, 0, LineSolid, CapButt, JoinMiter);
1929 switch(which) {
1930 case P_POSITION_X:
1931 XDrawArc(dpy, areawin->window, areawin->gc, wpt.x - 4,
1932 wpt.y - 4, 8, 8, -(45 * 64), (90 * 64));
1933 XDrawArc(dpy, areawin->window, areawin->gc, wpt.x - 4,
1934 wpt.y - 4, 8, 8, (135 * 64), (90 * 64));
1935 break;
1936 case P_POSITION_Y:
1937 XDrawArc(dpy, areawin->window, areawin->gc, wpt.x - 4,
1938 wpt.y - 4, 8, 8, (45 * 64), (90 * 64));
1939 XDrawArc(dpy, areawin->window, areawin->gc, wpt.x - 4,
1940 wpt.y - 4, 8, 8, (225 * 64), (90 * 64));
1941 break;
1942 default:
1943 XDrawArc(dpy, areawin->window, areawin->gc, wpt.x - 4,
1944 wpt.y - 4, 8, 8, 0, (360 * 64));
1945 break;
1948 #endif /* !HAVE_CAIRO */
1950 /*----------------------------------------------------------------------*/
1951 /* Add "X" at string origin */
1952 /*----------------------------------------------------------------------*/
1954 #ifndef HAVE_CAIRO
1955 void UDrawXAt(XPoint *wpt)
1957 if (!areawin->redraw_ongoing) {
1958 areawin->redraw_needed = True;
1959 return;
1962 SetThinLineAttributes(dpy, areawin->gc, 0, LineSolid, CapButt, JoinMiter);
1963 DrawLine(dpy, areawin->window, areawin->gc, wpt->x - 3,
1964 wpt->y - 3, wpt->x + 3, wpt->y + 3);
1965 DrawLine(dpy, areawin->window, areawin->gc, wpt->x + 3,
1966 wpt->y - 3, wpt->x - 3, wpt->y + 3);
1968 #endif /* !HAVE_CAIRO */
1970 /*----------------------------------------------------------------------*/
1971 /* Draw "X" on current level */
1972 /*----------------------------------------------------------------------*/
1974 void UDrawX(labelptr curlabel)
1976 XPoint wpt;
1978 user_to_window(curlabel->position, &wpt);
1979 UDrawXAt(&wpt);
1982 /*----------------------------------------------------------------------*/
1983 /* Draw "X" on top level (only for LOCAL and GLOBAL pin labels) */
1984 /*----------------------------------------------------------------------*/
1986 void UDrawXDown(labelptr curlabel)
1988 XPoint wpt;
1990 UTransformbyCTM(DCTM, &curlabel->position, &wpt, 1);
1991 UDrawXAt(&wpt);
1994 /*----------------------------------------------------------------------*/
1995 /* Find the "real" width, height, and origin of an object including pin */
1996 /* labels and so forth that only show up on a schematic when it is the */
1997 /* top-level object. */
1998 /*----------------------------------------------------------------------*/
2000 int toplevelwidth(objinstptr bbinst, short *rllx)
2002 short llx, urx;
2003 short origin, corner;
2005 if (bbinst->schembbox == NULL) {
2006 if (rllx) *rllx = bbinst->bbox.lowerleft.x;
2007 return bbinst->bbox.width;
2010 origin = bbinst->bbox.lowerleft.x;
2011 corner = origin + bbinst->bbox.width;
2013 llx = bbinst->schembbox->lowerleft.x;
2014 urx = llx + bbinst->schembbox->width;
2016 bboxcalc(llx, &origin, &corner);
2017 bboxcalc(urx, &origin, &corner);
2019 if (rllx) *rllx = origin;
2020 return(corner - origin);
2023 /*----------------------------------------------------------------------*/
2025 int toplevelheight(objinstptr bbinst, short *rlly)
2027 short lly, ury;
2028 short origin, corner;
2030 if (bbinst->schembbox == NULL) {
2031 if (rlly) *rlly = bbinst->bbox.lowerleft.y;
2032 return bbinst->bbox.height;
2035 origin = bbinst->bbox.lowerleft.y;
2036 corner = origin + bbinst->bbox.height;
2038 lly = bbinst->schembbox->lowerleft.y;
2039 ury = lly + bbinst->schembbox->height;
2041 bboxcalc(lly, &origin, &corner);
2042 bboxcalc(ury, &origin, &corner);
2044 if (rlly) *rlly = origin;
2045 return(corner - origin);
2048 /*----------------------------------------------------------------------*/
2049 /* Add dimensions of schematic pins to an object's bounding box */
2050 /*----------------------------------------------------------------------*/
2052 void extendschembbox(objinstptr bbinst, XPoint *origin, XPoint *corner)
2054 short llx, lly, urx, ury;
2056 if ((bbinst == NULL) || (bbinst->schembbox == NULL)) return;
2058 llx = bbinst->schembbox->lowerleft.x;
2059 lly = bbinst->schembbox->lowerleft.y;
2060 urx = llx + bbinst->schembbox->width;
2061 ury = lly + bbinst->schembbox->height;
2063 bboxcalc(llx, &(origin->x), &(corner->x));
2064 bboxcalc(lly, &(origin->y), &(corner->y));
2065 bboxcalc(urx, &(origin->x), &(corner->x));
2066 bboxcalc(ury, &(origin->y), &(corner->y));
2069 /*----------------------------------------------------------------------*/
2070 /* Adjust a pinlabel position to account for pad spacing */
2071 /*----------------------------------------------------------------------*/
2073 void pinadjust (short anchor, short *xpoint, short *ypoint, short dir)
2075 int delx, dely;
2077 dely = (anchor & NOTBOTTOM) ?
2078 ((anchor & TOP) ? -PADSPACE : 0) : PADSPACE;
2079 delx = (anchor & NOTLEFT) ?
2080 ((anchor & RIGHT) ? -PADSPACE : 0) : PADSPACE;
2082 if (xpoint != NULL) *xpoint += (dir > 0) ? delx : -delx;
2083 if (ypoint != NULL) *ypoint += (dir > 0) ? dely : -dely;
2086 /*----------------------------------------------------------------------*/
2087 /* Draw line for editing text (position of cursor in string is given by */
2088 /* tpos (2nd parameter) */
2089 /*----------------------------------------------------------------------*/
2091 void UDrawTextLine(labelptr curlabel, short tpos)
2093 XPoint points[2]; /* top and bottom of text cursor line */
2094 short tmpanchor, xbase;
2095 int maxwidth;
2096 TextExtents tmpext;
2097 TextLinesInfo tlinfo;
2099 if (!areawin->redraw_ongoing) {
2100 areawin->redraw_needed = True;
2101 return;
2104 /* correct for position, rotation, scale, and flip invariance of text */
2106 UPushCTM();
2107 UPreMultCTM(DCTM, curlabel->position, curlabel->scale, curlabel->rotation);
2108 tmpanchor = flipadjust(curlabel->anchor);
2110 SetForeground(dpy, areawin->gc, AUXCOLOR);
2112 tlinfo.dostop = 0;
2113 tlinfo.tbreak = NULL;
2114 tlinfo.padding = NULL;
2116 tmpext = ULength(curlabel, areawin->topinstance, &tlinfo);
2117 maxwidth = tmpext.maxwidth;
2118 xbase = tmpext.base;
2119 tlinfo.dostop = tpos;
2120 tmpext = ULength(curlabel, areawin->topinstance, &tlinfo);
2122 points[0].x = (tmpanchor & NOTLEFT ?
2123 (tmpanchor & RIGHT ? -maxwidth : -maxwidth >> 1) : 0) + tmpext.width;
2124 if ((tmpanchor & JUSTIFYRIGHT) && tlinfo.padding)
2125 points[0].x += tlinfo.padding[tlinfo.line];
2126 else if ((tmpanchor & TEXTCENTERED) && tlinfo.padding)
2127 points[0].x += 0.5 * tlinfo.padding[tlinfo.line];
2128 points[0].y = (tmpanchor & NOTBOTTOM ?
2129 (tmpanchor & TOP ? -tmpext.ascent : -(tmpext.ascent + xbase) / 2)
2130 : -xbase) + tmpext.base - 3;
2131 points[1].x = points[0].x;
2132 points[1].y = points[0].y + TEXTHEIGHT + 6;
2134 if (curlabel->pin) {
2135 pinadjust(tmpanchor, &(points[0].x), &(points[0].y), 1);
2136 pinadjust(tmpanchor, &(points[1].x), &(points[1].y), 1);
2138 if (tlinfo.padding != NULL) free(tlinfo.padding);
2140 /* draw the line */
2142 UDrawLine(&points[0], &points[1]);
2143 UPopCTM();
2145 UDrawX(curlabel);
2148 /*-----------------------------------------------------------------*/
2149 /* Draw lines for editing text when multiple characters are chosen */
2150 /*-----------------------------------------------------------------*/
2152 void UDrawTLine(labelptr curlabel)
2154 UDrawTextLine(curlabel, areawin->textpos);
2155 if ((areawin->textend > 0) && (areawin->textend < areawin->textpos)) {
2156 UDrawTextLine(curlabel, areawin->textend);
2160 /*----------------------*/
2161 /* Draw an X */
2162 /*----------------------*/
2164 #ifndef HAVE_CAIRO
2165 void UDrawXLine(XPoint opt, XPoint cpt)
2167 XPoint upt, vpt;
2169 if (!areawin->redraw_ongoing) {
2170 areawin->redraw_needed = True;
2171 return;
2174 SetForeground(dpy, areawin->gc, AUXCOLOR);
2176 user_to_window(cpt, &upt);
2177 user_to_window(opt, &vpt);
2179 SetThinLineAttributes(dpy, areawin->gc, 0, LineOnOffDash, CapButt, JoinMiter);
2180 DrawLine(dpy, areawin->window, areawin->gc, vpt.x, vpt.y, upt.x, upt.y);
2182 SetThinLineAttributes(dpy, areawin->gc, 0, LineSolid, CapButt, JoinMiter);
2183 DrawLine(dpy, areawin->window, areawin->gc, upt.x - 3, upt.y - 3,
2184 upt.x + 3, upt.y + 3);
2185 DrawLine(dpy, areawin->window, areawin->gc, upt.x + 3, upt.y - 3,
2186 upt.x - 3, upt.y + 3);
2188 SetForeground(dpy, areawin->gc, areawin->gccolor);
2190 #endif /* HAVE_CAIRO */
2192 /*-------------------------------------------------------------------------*/
2194 #ifndef HAVE_CAIRO
2195 void UDrawBox(XPoint origin, XPoint corner)
2197 XPoint worig, wcorn;
2199 if (!areawin->redraw_ongoing) {
2200 areawin->redraw_needed = True;
2201 return;
2204 user_to_window(origin, &worig);
2205 user_to_window(corner, &wcorn);
2207 SetForeground(dpy, areawin->gc, AUXCOLOR);
2208 SetThinLineAttributes(dpy, areawin->gc, 0, LineSolid, CapRound, JoinBevel);
2209 DrawLine(dpy, areawin->window, areawin->gc, worig.x, worig.y,
2210 worig.x, wcorn.y);
2211 DrawLine(dpy, areawin->window, areawin->gc, worig.x, wcorn.y,
2212 wcorn.x, wcorn.y);
2213 DrawLine(dpy, areawin->window, areawin->gc, wcorn.x, wcorn.y,
2214 wcorn.x, worig.y);
2215 DrawLine(dpy, areawin->window, areawin->gc, wcorn.x, worig.y,
2216 worig.x, worig.y);
2218 #endif /* HAVE_CAIRO */
2220 /*----------------------------------------------------------------------*/
2221 /* Get a box indicating the dimensions of the edit element that most */
2222 /* closely reach the position "corner". */
2223 /*----------------------------------------------------------------------*/
2225 float UGetRescaleBox(XPoint *corner, XPoint *newpoints)
2227 genericptr rgen;
2228 float savescale, newscale;
2229 long mindist, testdist, refdist;
2230 labelptr rlab;
2231 graphicptr rgraph;
2232 objinstptr rinst;
2233 int i;
2235 if (!areawin->redraw_ongoing) {
2236 areawin->redraw_needed = True;
2237 // return 0.0;
2240 if (areawin->selects == 0) return 0.0;
2242 /* Use only the 1st selection as a reference to set the scale */
2244 rgen = SELTOGENERIC(areawin->selectlist);
2246 switch(ELEMENTTYPE(rgen)) {
2247 case LABEL:
2248 rlab = (labelptr)rgen;
2249 labelbbox(rlab, newpoints, areawin->topinstance);
2250 newpoints[4] = newpoints[0];
2251 mindist = LONG_MAX;
2252 for (i = 0; i < 4; i++) {
2253 testdist = finddist(&newpoints[i], &newpoints[i+1], corner);
2254 if (testdist < mindist)
2255 mindist = testdist;
2257 refdist = wirelength(corner, &(rlab->position));
2258 mindist = (int)sqrt(abs((double)mindist));
2259 savescale = rlab->scale;
2260 if (!test_insideness((int)corner->x, (int)corner->y, newpoints))
2261 mindist = -mindist;
2262 if (refdist == mindist) refdist = 1 - mindist;
2263 if (rlab->scale < 0) rlab->scale = -rlab->scale;
2264 newscale = fabs(rlab->scale * (float)refdist / (float)(refdist + mindist));
2265 if (newscale > 10 * rlab->scale) newscale = 10 * rlab->scale;
2266 if (areawin->snapto) {
2267 float snapstep = 2 * (float)xobjs.pagelist[areawin->page]->gridspace
2268 / (float)xobjs.pagelist[areawin->page]->snapspace;
2269 newscale = (float)((int)(newscale * snapstep)) / snapstep;
2270 if (newscale < (1.0 / snapstep)) newscale = (1.0 / snapstep);
2272 else if (newscale < 0.1 * rlab->scale) newscale = 0.1 * rlab->scale;
2273 rlab->scale = (savescale < 0) ? -newscale : newscale;
2274 labelbbox(rlab, newpoints, areawin->topinstance);
2275 rlab->scale = savescale;
2276 if (savescale < 0) newscale = -newscale;
2277 break;
2279 case GRAPHIC:
2280 rgraph = (graphicptr)rgen;
2281 graphicbbox(rgraph, newpoints);
2282 newpoints[4] = newpoints[0];
2283 mindist = LONG_MAX;
2284 for (i = 0; i < 4; i++) {
2285 testdist = finddist(&newpoints[i], &newpoints[i+1], corner);
2286 if (testdist < mindist)
2287 mindist = testdist;
2289 refdist = wirelength(corner, &(rgraph->position));
2290 mindist = (int)sqrt(abs((double)mindist));
2291 savescale = rgraph->scale;
2292 if (!test_insideness((int)corner->x, (int)corner->y, newpoints))
2293 mindist = -mindist;
2294 if (refdist == mindist) refdist = 1 - mindist; /* avoid inf result */
2295 if (rgraph->scale < 0) rgraph->scale = -rgraph->scale;
2296 newscale = fabs(rgraph->scale * (float)refdist / (float)(refdist + mindist));
2297 if (newscale > 10 * rgraph->scale) newscale = 10 * rgraph->scale;
2298 if (areawin->snapto) {
2299 float snapstep = 2 * (float)xobjs.pagelist[areawin->page]->gridspace
2300 / (float)xobjs.pagelist[areawin->page]->snapspace;
2301 newscale = (float)((int)(newscale * snapstep)) / snapstep;
2302 if (newscale < (1.0 / snapstep)) newscale = (1.0 / snapstep);
2304 else if (newscale < 0.1 * rgraph->scale) newscale = 0.1 * rgraph->scale;
2305 rgraph->scale = (savescale < 0) ? -newscale : newscale;
2306 graphicbbox(rgraph, newpoints);
2307 rgraph->scale = savescale;
2308 if (savescale < 0) newscale = -newscale;
2309 break;
2311 case OBJINST:
2312 rinst = (objinstptr)rgen;
2313 objinstbbox(rinst, newpoints, 0);
2314 newpoints[4] = newpoints[0];
2315 mindist = LONG_MAX;
2316 for (i = 0; i < 4; i++) {
2317 testdist = finddist(&newpoints[i], &newpoints[i+1], corner);
2318 if (testdist < mindist)
2319 mindist = testdist;
2321 refdist = wirelength(corner, &(rinst->position));
2322 mindist = (int)sqrt(abs((double)mindist));
2323 savescale = rinst->scale;
2324 if (!test_insideness((int)corner->x, (int)corner->y, newpoints))
2325 mindist = -mindist;
2326 if (refdist == mindist) refdist = 1 - mindist; /* avoid inf result */
2327 if (rinst->scale < 0) rinst->scale = -rinst->scale;
2328 newscale = fabs(rinst->scale * (float)refdist / (float)(refdist + mindist));
2329 if (newscale > 10 * rinst->scale) newscale = 10 * rinst->scale;
2330 if (areawin->snapto) {
2331 float snapstep = 2 * (float)xobjs.pagelist[areawin->page]->gridspace
2332 / (float)xobjs.pagelist[areawin->page]->snapspace;
2333 newscale = (float)((int)(newscale * snapstep)) / snapstep;
2334 if (newscale < (1.0 / snapstep)) newscale = (1.0 / snapstep);
2336 else if (newscale < 0.1 * rinst->scale) newscale = 0.1 * rinst->scale;
2337 rinst->scale = (savescale < 0) ? -newscale : newscale;
2338 objinstbbox(rinst, newpoints, 0);
2339 rinst->scale = savescale;
2340 if (savescale < 0) newscale = -newscale;
2341 break;
2344 return newscale;
2347 /*----------------------------------------------------------------------*/
2348 /* Draw a box indicating the dimensions of the edit element that most */
2349 /* closely reach the position "corner". */
2350 /*----------------------------------------------------------------------*/
2352 #ifndef HAVE_CAIRO
2353 void UDrawRescaleBox(XPoint *corner)
2355 XPoint origpoints[5], newpoints[5];
2357 if (!areawin->redraw_ongoing) {
2358 areawin->redraw_needed = True;
2359 return;
2362 if (areawin->selects == 0)
2363 return;
2365 UGetRescaleBox(corner, newpoints);
2367 SetForeground(dpy, areawin->gc, AUXCOLOR);
2368 SetThinLineAttributes(dpy, areawin->gc, 0, LineSolid, CapRound, JoinBevel);
2370 UTransformbyCTM(DCTM, newpoints, origpoints, 4);
2371 strokepath(origpoints, 4, 0, 1);
2373 #endif /* HAVE_CAIRO */
2375 /*-------------------------------------------------------------------------*/
2377 #ifndef HAVE_CAIRO
2378 void UDrawBBox()
2380 XPoint origin;
2381 XPoint worig, wcorn, corner;
2382 objinstptr bbinst = areawin->topinstance;
2384 if (!areawin->redraw_ongoing) {
2385 areawin->redraw_needed = True;
2386 return;
2389 if ((!areawin->bboxon) || (checkforbbox(topobject) != NULL)) return;
2391 origin = bbinst->bbox.lowerleft;
2392 corner.x = origin.x + bbinst->bbox.width;
2393 corner.y = origin.y + bbinst->bbox.height;
2395 /* Include any schematic labels in the bounding box. */
2396 extendschembbox(bbinst, &origin, &corner);
2398 user_to_window(origin, &worig);
2399 user_to_window(corner, &wcorn);
2401 SetForeground(dpy, areawin->gc, BBOXCOLOR);
2402 DrawLine(dpy, areawin->window, areawin->gc, worig.x, worig.y,
2403 worig.x, wcorn.y);
2404 DrawLine(dpy, areawin->window, areawin->gc, worig.x, wcorn.y,
2405 wcorn.x, wcorn.y);
2406 DrawLine(dpy, areawin->window, areawin->gc, wcorn.x, wcorn.y,
2407 wcorn.x, worig.y);
2408 DrawLine(dpy, areawin->window, areawin->gc, wcorn.x, worig.y,
2409 worig.x, worig.y);
2411 #endif /* !HAVE_CAIRO */
2413 /*----------------------------------------------------------------------*/
2414 /* Fill and/or draw a border around the stroking path */
2415 /*----------------------------------------------------------------------*/
2417 #ifndef HAVE_CAIRO
2418 void strokepath(XPoint *pathlist, short number, short style, float width)
2420 float tmpwidth;
2422 tmpwidth = UTopTransScale(width);
2424 if (!(style & CLIPMASK) || (areawin->showclipmasks == TRUE) ||
2425 (areawin->clipped < 0)) {
2426 if (style & FILLED || (!(style & FILLED) && style & OPAQUE)) {
2427 if ((style & FILLSOLID) == FILLSOLID)
2428 SetFillStyle(dpy, areawin->gc, FillSolid);
2429 else if (!(style & FILLED)) {
2430 SetFillStyle(dpy, areawin->gc, FillOpaqueStippled);
2431 SetStipple(dpy, areawin->gc, 7);
2433 else {
2434 if (style & OPAQUE)
2435 SetFillStyle(dpy, areawin->gc, FillOpaqueStippled);
2436 else
2437 SetFillStyle(dpy, areawin->gc, FillStippled);
2438 SetStipple(dpy, areawin->gc, ((style & FILLSOLID) >> 5));
2440 FillPolygon(dpy, areawin->window, areawin->gc, pathlist, number, Nonconvex,
2441 CoordModeOrigin);
2442 /* return to original state */
2443 SetFillStyle(dpy, areawin->gc, FillSolid);
2445 if (!(style & NOBORDER)) {
2446 if (style & (DASHED | DOTTED)) {
2447 /* Set up dots or dashes */
2448 char dashstring[2];
2449 /* prevent values greater than 255 from folding back into */
2450 /* type char. Limit to 63 (=255/4) to keep at least the */
2451 /* dot/gap ratio to scale when 'gap' is at its maximum */
2452 /* value. */
2453 unsigned char dotsize = min(63, max(1, (short)tmpwidth));
2454 if (style & DASHED)
2455 dashstring[0] = 4 * dotsize;
2456 else if (style & DOTTED)
2457 dashstring[0] = dotsize;
2458 dashstring[1] = 4 * dotsize;
2459 SetDashes(dpy, areawin->gc, 0, dashstring, 2);
2460 SetLineAttributes(dpy, areawin->gc, tmpwidth, LineOnOffDash,
2461 CapButt, (style & SQUARECAP) ? JoinMiter : JoinBevel);
2463 else
2464 SetLineAttributes(dpy, areawin->gc, tmpwidth, LineSolid,
2465 (style & SQUARECAP) ? CapProjecting : CapRound,
2466 (style & SQUARECAP) ? JoinMiter : JoinBevel);
2468 /* draw the spline and close off if so specified */
2469 DrawLines(dpy, areawin->window, areawin->gc, pathlist,
2470 number, CoordModeOrigin);
2471 if (!(style & UNCLOSED))
2472 DrawLine(dpy, areawin->window, areawin->gc, pathlist[0].x,
2473 pathlist[0].y, pathlist[number - 1].x, pathlist[number - 1].y);
2477 if (style & CLIPMASK) {
2478 if (areawin->clipped == 0) {
2479 XSetForeground(dpy, areawin->cmgc, 0);
2480 XFillRectangle(dpy, areawin->clipmask, areawin->cmgc, 0, 0,
2481 areawin->width, areawin->height);
2482 XSetForeground(dpy, areawin->cmgc, 1);
2483 FillPolygon(dpy, areawin->clipmask, areawin->cmgc, pathlist,
2484 number, Nonconvex, CoordModeOrigin);
2485 XSetClipMask(dpy, areawin->gc, areawin->clipmask);
2486 // printf("level 0: Clip to clipmask\n"); // Diagnostic
2487 areawin->clipped++;
2489 else if ((areawin->clipped > 0) && (areawin->clipped & 1) == 0) {
2490 if (areawin->pbuf == (Pixmap)NULL) {
2491 areawin->pbuf = XCreatePixmap (dpy, areawin->window,
2492 areawin->width, areawin->height, 1);
2494 XCopyArea(dpy, areawin->clipmask, areawin->pbuf, areawin->cmgc,
2495 0, 0, areawin->width, areawin->height, 0, 0);
2496 XSetForeground(dpy, areawin->cmgc, 0);
2497 XFillRectangle(dpy, areawin->clipmask, areawin->cmgc, 0, 0,
2498 areawin->width, areawin->height);
2499 XSetForeground(dpy, areawin->cmgc, 1);
2500 FillPolygon(dpy, areawin->clipmask, areawin->cmgc, pathlist,
2501 number, Nonconvex, CoordModeOrigin);
2502 XSetFunction(dpy, areawin->cmgc, GXand);
2503 XCopyArea(dpy, areawin->pbuf, areawin->clipmask, areawin->cmgc,
2504 0, 0, areawin->width, areawin->height, 0, 0);
2505 XSetFunction(dpy, areawin->cmgc, GXcopy);
2506 XSetClipMask(dpy, areawin->gc, areawin->clipmask);
2507 // printf("level X: Clip to clipmask\n"); // Diagnostic
2508 areawin->clipped++;
2512 #endif /* !HAVE_CAIRO */
2514 /*-------------------------------------------------------------------------*/
2516 void makesplinepath(splineptr thespline, XPoint *pathlist)
2518 XPoint *tmpptr = pathlist;
2520 UTransformbyCTM(DCTM, &(thespline->ctrl[0]), tmpptr, 1);
2521 UfTransformbyCTM(DCTM, thespline->points, ++tmpptr, INTSEGS);
2522 UTransformbyCTM(DCTM, &(thespline->ctrl[3]), tmpptr + INTSEGS, 1);
2525 /*-------------------------------------------------------------------------*/
2527 #ifndef HAVE_CAIRO
2528 void UDrawSpline(splineptr thespline, float passwidth)
2530 XPoint tmppoints[SPLINESEGS];
2531 float scaledwidth;
2533 if (!areawin->redraw_ongoing) {
2534 areawin->redraw_needed = True;
2535 return;
2538 scaledwidth = thespline->width * passwidth;
2540 makesplinepath(thespline, tmppoints);
2541 strokepath(tmppoints, SPLINESEGS, thespline->style, scaledwidth);
2543 #endif /* HAVE_CAIRO */
2545 /*-------------------------------------------------------------------------*/
2547 #ifndef HAVE_CAIRO
2548 void UDrawPolygon(polyptr thepoly, float passwidth)
2550 XPoint *tmppoints = (pointlist) malloc(thepoly->number * sizeof(XPoint));
2551 float scaledwidth;
2553 if (!areawin->redraw_ongoing) {
2554 areawin->redraw_needed = True;
2555 return;
2558 scaledwidth = thepoly->width * passwidth;
2560 UTransformbyCTM(DCTM, thepoly->points, tmppoints, thepoly->number);
2561 strokepath(tmppoints, thepoly->number, thepoly->style, scaledwidth);
2562 free(tmppoints);
2564 #endif /* HAVE_CAIRO */
2566 /*-------------------------------------------------------------------------*/
2568 #ifndef HAVE_CAIRO
2569 void UDrawArc(arcptr thearc, float passwidth)
2571 XPoint tmppoints[RSTEPS + 2];
2572 float scaledwidth;
2574 if (!areawin->redraw_ongoing) {
2575 areawin->redraw_needed = True;
2576 return;
2579 scaledwidth = thearc->width * passwidth;
2581 UfTransformbyCTM(DCTM, thearc->points, tmppoints, thearc->number);
2582 strokepath(tmppoints, thearc->number, thearc->style, scaledwidth);
2584 #endif /* HAVE_CAIRO */
2586 /*-------------------------------------------------------------------------*/
2588 #ifndef HAVE_CAIRO
2589 void UDrawPath(pathptr thepath, float passwidth)
2591 XPoint *tmppoints = (pointlist) malloc(sizeof(XPoint));
2592 genericptr *genpath;
2593 polyptr thepoly;
2594 splineptr thespline;
2595 int pathsegs = 0, curseg = 0;
2596 float scaledwidth;
2598 if (!areawin->redraw_ongoing) {
2599 areawin->redraw_needed = True;
2600 return;
2603 for (genpath = thepath->plist; genpath < thepath->plist + thepath->parts;
2604 genpath++) {
2605 switch(ELEMENTTYPE(*genpath)) {
2606 case POLYGON:
2607 thepoly = TOPOLY(genpath);
2608 pathsegs += thepoly->number;
2609 tmppoints = (pointlist) realloc(tmppoints, pathsegs * sizeof(XPoint));
2610 UTransformbyCTM(DCTM, thepoly->points, tmppoints + curseg, thepoly->number);
2611 curseg = pathsegs;
2612 break;
2613 case SPLINE:
2614 thespline = TOSPLINE(genpath);
2615 pathsegs += SPLINESEGS;
2616 tmppoints = (pointlist) realloc(tmppoints, pathsegs * sizeof(XPoint));
2617 makesplinepath(thespline, tmppoints + curseg);
2618 curseg = pathsegs;
2619 break;
2622 scaledwidth = thepath->width * passwidth;
2624 strokepath(tmppoints, pathsegs, thepath->style, scaledwidth);
2625 free(tmppoints);
2627 #endif /* HAVE_CAIRO */
2629 /*----------------------------------------------------------------------*/
2630 /* Main recursive object instance drawing routine. */
2631 /* context is the instance information passed down from above */
2632 /* theinstance is the object instance to be drawn */
2633 /* level is the level of recursion */
2634 /* passcolor is the inherited color value passed to object */
2635 /* passwidth is the inherited linewidth value passed to the object */
2636 /* stack contains graphics context information */
2637 /*----------------------------------------------------------------------*/
2639 #ifndef HAVE_CAIRO
2640 void UDrawObject(objinstptr theinstance, short level, int passcolor,
2641 float passwidth, pushlistptr *stack)
2643 genericptr *areagen;
2644 float tmpwidth;
2645 int defaultcolor = passcolor;
2646 int curcolor = passcolor;
2647 int thispart;
2648 short savesel;
2649 XPoint bboxin[2], bboxout[2];
2650 u_char xm, ym;
2651 objectptr theobject = theinstance->thisobject;
2653 if (!areawin->redraw_ongoing) {
2654 areawin->redraw_needed = True;
2655 return;
2658 /* Save the number of selections and set it to zero while we do the */
2659 /* object drawing. */
2661 savesel = areawin->selects;
2662 areawin->selects = 0;
2664 /* All parts are given in the coordinate system of the object, unless */
2665 /* this is the top-level object, in which they will be interpreted as */
2666 /* relative to the screen. */
2668 UPushCTM();
2670 if (stack) {
2671 /* Save the current clipping mask and push it on the stack */
2672 if (areawin->clipped > 0) {
2673 push_stack((pushlistptr *)stack, theinstance, (char *)areawin->clipmask);
2674 areawin->clipmask = XCreatePixmap(dpy, areawin->window, areawin->width,
2675 areawin->height, 1);
2676 XCopyArea(dpy, (Pixmap)(*stack)->clientdata, areawin->clipmask, areawin->cmgc,
2677 0, 0, areawin->width, areawin->height, 0, 0);
2679 else
2680 push_stack((pushlistptr *)stack, theinstance, (char *)NULL);
2682 if (level != 0)
2683 UPreMultCTM(DCTM, theinstance->position, theinstance->scale,
2684 theinstance->rotation);
2686 if (theinstance->style & LINE_INVARIANT)
2687 passwidth /= fabs(theinstance->scale);
2689 /* do a quick test for intersection with the display window */
2691 bboxin[0].x = theobject->bbox.lowerleft.x;
2692 bboxin[0].y = theobject->bbox.lowerleft.y;
2693 bboxin[1].x = theobject->bbox.lowerleft.x + theobject->bbox.width;
2694 bboxin[1].y = theobject->bbox.lowerleft.y + theobject->bbox.height;
2695 if (level == 0)
2696 extendschembbox(theinstance, &(bboxin[0]), &(bboxin[1]));
2697 UTransformbyCTM(DCTM, bboxin, bboxout, 2);
2699 xm = (bboxout[0].x < bboxout[1].x) ? 0 : 1;
2700 ym = (bboxout[0].y < bboxout[1].y) ? 0 : 1;
2702 if (bboxout[xm].x < areawin->width && bboxout[ym].y < areawin->height &&
2703 bboxout[1 - xm].x > 0 && bboxout[1 - ym].y > 0) {
2705 /* make parameter substitutions */
2706 psubstitute(theinstance);
2708 /* draw all of the elements */
2710 tmpwidth = UTopTransScale(passwidth);
2711 SetLineAttributes(dpy, areawin->gc, tmpwidth, LineSolid, CapRound,
2712 JoinBevel);
2714 /* guard against plist being regenerated during a redraw by the */
2715 /* expression parameter mechanism (should that be prohibited?) */
2717 for (thispart = 0; thispart < theobject->parts; thispart++) {
2718 areagen = theobject->plist + thispart;
2719 if ((*areagen)->type & DRAW_HIDE) continue;
2721 if (defaultcolor != DOFORALL) {
2722 Boolean clipcolor = FALSE;
2723 switch(ELEMENTTYPE(*areagen)) {
2724 case(POLYGON): case(SPLINE): case(ARC): case(PATH):
2725 if (TOPOLY(areagen)->style & CLIPMASK)
2726 clipcolor = TRUE;
2727 break;
2729 if (((*areagen)->color != curcolor) || (clipcolor == TRUE)) {
2730 if (clipcolor)
2731 curcolor = CLIPMASKCOLOR;
2732 else if ((*areagen)->color == DEFAULTCOLOR)
2733 curcolor = defaultcolor;
2734 else
2735 curcolor = (*areagen)->color;
2737 XcTopSetForeground(curcolor);
2741 switch(ELEMENTTYPE(*areagen)) {
2742 case(POLYGON):
2743 if (level == 0 || !((TOPOLY(areagen))->style & BBOX))
2744 UDrawPolygon(TOPOLY(areagen), passwidth);
2745 break;
2747 case(SPLINE):
2748 UDrawSpline(TOSPLINE(areagen), passwidth);
2749 break;
2751 case(ARC):
2752 UDrawArc(TOARC(areagen), passwidth);
2753 break;
2755 case(PATH):
2756 UDrawPath(TOPATH(areagen), passwidth);
2757 break;
2759 case(GRAPHIC):
2760 UDrawGraphic(TOGRAPHIC(areagen));
2761 break;
2763 case(OBJINST):
2764 if (areawin->editinplace && stack && (TOOBJINST(areagen)
2765 == areawin->topinstance)) {
2766 /* If stack matches areawin->stack, then don't draw */
2767 /* because it would be redundant. */
2768 pushlistptr alist = *stack, blist = areawin->stack;
2769 while (alist && blist) {
2770 if (alist->thisinst != blist->thisinst) break;
2771 alist = alist->next;
2772 blist = blist->next;
2774 if ((!alist) || (!blist)) break;
2776 if (areawin->clipped > 0) areawin->clipped += 2;
2777 UDrawObject(TOOBJINST(areagen), level + 1, curcolor, passwidth, stack);
2778 if (areawin->clipped > 0) areawin->clipped -= 2;
2779 break;
2781 case(LABEL):
2782 if (level == 0 || TOLABEL(areagen)->pin == False)
2783 UDrawString(TOLABEL(areagen), curcolor, theinstance);
2784 else if ((TOLABEL(areagen)->anchor & PINVISIBLE) && areawin->pinpointon)
2785 UDrawString(TOLABEL(areagen), curcolor, theinstance);
2786 else if (TOLABEL(areagen)->anchor & PINVISIBLE)
2787 UDrawStringNoX(TOLABEL(areagen), curcolor, theinstance);
2788 else if (level == 1 && TOLABEL(areagen)->pin &&
2789 TOLABEL(areagen)->pin != INFO && areawin->pinpointon)
2790 UDrawXDown(TOLABEL(areagen));
2791 break;
2793 if (areawin->clipped > 0) {
2794 if ((areawin->clipped & 3) == 1) {
2795 areawin->clipped++;
2797 else if ((areawin->clipped & 3) == 2) {
2798 areawin->clipped -= 2;
2799 if ((!stack) || ((*stack)->clientdata == (char *)NULL)) {
2800 XSetClipMask(dpy, areawin->gc, None);
2801 // printf("1: Clear clipmask\n"); // Diagnostic
2803 else {
2804 XSetClipMask(dpy, areawin->gc, (Pixmap)((*stack)->clientdata));
2805 // printf("1: Set to pushed clipmask\n"); // Diagnostic
2811 /* restore the color passed to the object, if different from current color */
2813 if ((defaultcolor != DOFORALL) && (passcolor != curcolor)) {
2814 XTopSetForeground(passcolor);
2816 if (areawin->clipped > 0) {
2817 if ((areawin->clipped & 3) != 3) {
2818 if ((!stack) || ((*stack)->clientdata == (char *)NULL)) {
2819 XSetClipMask(dpy, areawin->gc, None);
2820 // printf("2: Clear clipmask\n"); // Diagnostic
2822 else {
2823 XSetClipMask(dpy, areawin->gc, (Pixmap)((*stack)->clientdata));
2824 // printf("2: Set to pushed clipmask\n"); // Diagnostic
2827 areawin->clipped &= ~3;
2831 /* restore the selection list (if any) */
2832 areawin->selects = savesel;
2833 UPopCTM();
2834 if (stack) {
2835 if ((*stack) != NULL) {
2836 if ((*stack)->clientdata != (char *)NULL) {
2837 XFreePixmap(dpy, areawin->clipmask);
2838 areawin->clipmask = (Pixmap)(*stack)->clientdata;
2839 // printf("3: Restore clipmask\n"); // Diagnostic
2842 pop_stack(stack);
2845 #endif /* HAVE_CAIRO */
2847 /*----------------------------------------------------------------------*/
2848 /* Recursively run through the current page and find any labels which */
2849 /* are declared to be style LATEX. If "checkonly" is present, we set */
2850 /* it to TRUE or FALSE depending on whether or not LATEX labels have */
2851 /* been encountered. If NULL, then we write LATEX output appropriately */
2852 /* to a file named with the page filename + suffix ".tex". */
2853 /*----------------------------------------------------------------------*/
2855 void UDoLatex(objinstptr theinstance, short level, FILE *f,
2856 float scale, float scale2, int tx, int ty, Boolean *checkonly)
2858 XPoint lpos, xlpos;
2859 XfPoint xfpos;
2860 labelptr thislabel;
2861 genericptr *areagen;
2862 objectptr theobject = theinstance->thisobject;
2863 char *ltext;
2864 int lranchor, tbanchor;
2866 UPushCTM();
2867 if (level != 0)
2868 UPreMultCTM(DCTM, theinstance->position, theinstance->scale,
2869 theinstance->rotation);
2871 /* make parameter substitutions */
2872 psubstitute(theinstance);
2874 /* find all of the elements */
2876 for (areagen = theobject->plist; areagen < theobject->plist +
2877 theobject->parts; areagen++) {
2879 switch(ELEMENTTYPE(*areagen)) {
2880 case(OBJINST):
2881 UDoLatex(TOOBJINST(areagen), level + 1, f, scale, scale2, tx, ty, checkonly);
2882 break;
2884 case(LABEL):
2885 thislabel = TOLABEL(areagen);
2886 if (level == 0 || thislabel->pin == False ||
2887 (thislabel->anchor & PINVISIBLE))
2888 if (thislabel->anchor & LATEXLABEL) {
2889 if (checkonly) {
2890 *checkonly = TRUE;
2891 return;
2893 else {
2894 lpos.x = thislabel->position.x;
2895 lpos.y = thislabel->position.y;
2896 UTransformbyCTM(DCTM, &lpos, &xlpos, 1);
2897 xlpos.x += tx;
2898 xlpos.y += ty;
2899 xfpos.x = (float)xlpos.x * scale;
2900 xfpos.y = (float)xlpos.y * scale;
2901 xfpos.x /= 72.0;
2902 xfpos.y /= 72.0;
2903 xfpos.x -= 1.0;
2904 xfpos.y -= 1.0;
2905 xfpos.x += 0.056;
2906 xfpos.y += 0.056;
2907 xfpos.x /= scale2;
2908 xfpos.y /= scale2;
2909 ltext = textprinttex(thislabel->string, theinstance);
2910 tbanchor = thislabel->anchor & (NOTBOTTOM | TOP);
2911 lranchor = thislabel->anchor & (NOTLEFT | RIGHT);
2913 /* The 1.2 factor accounts for the difference between */
2914 /* Xcircuit's label scale of "1" and LaTeX's "normalsize" */
2916 fprintf(f, " \\putbox{%3.2fin}{%3.2fin}{%3.2f}{",
2917 xfpos.x, xfpos.y, 1.2 * thislabel->scale);
2918 if (thislabel->rotation != 0)
2919 fprintf(f, "\\rotatebox{-%d}{", thislabel->rotation);
2920 if (lranchor == (NOTLEFT | RIGHT)) fprintf(f, "\\rightbox{");
2921 else if (lranchor == NOTLEFT) fprintf(f, "\\centbox{");
2922 if (tbanchor == (NOTBOTTOM | TOP)) fprintf(f, "\\topbox{");
2923 else if (tbanchor == NOTBOTTOM) fprintf(f, "\\midbox{");
2924 fprintf(f, "%s", ltext);
2925 if (lranchor != NORMAL) fprintf(f, "}");
2926 if (tbanchor != NORMAL) fprintf(f, "}");
2927 if (thislabel->rotation != 0) fprintf(f, "}");
2928 fprintf(f, "}%%\n");
2929 free(ltext);
2932 break;
2935 UPopCTM();
2938 /*----------------------------------------------------------------------*/
2939 /* Top level routine for writing LATEX output. */
2940 /*----------------------------------------------------------------------*/
2942 void TopDoLatex()
2944 FILE *f;
2945 float psscale, outscale;
2946 int tx, ty, width, height;
2947 polyptr framebox;
2948 XPoint origin;
2949 Boolean checklatex = FALSE;
2950 char filename[100], extend[10], *dotptr;
2952 UDoLatex(areawin->topinstance, 0, NULL, 1.0, 1.0, 0, 0, &checklatex);
2954 if (checklatex == FALSE) return; /* No LaTeX labels to write */
2956 /* Handle cases where the file might have a ".eps" extension. */
2957 /* Thanks to Graham Sheward for pointing this out. */
2959 if (xobjs.pagelist[areawin->page]->filename)
2960 sprintf(filename, "%s", xobjs.pagelist[areawin->page]->filename);
2961 else
2962 sprintf(filename, "%s",
2963 xobjs.pagelist[areawin->page]->pageinst->thisobject->name);
2965 if ((dotptr = strchr(filename + strlen(filename) - 4, '.')) == NULL) {
2966 dotptr = filename + strlen(filename);
2967 sprintf(dotptr, ".ps");
2969 strcpy(extend, dotptr);
2970 strcpy(dotptr, ".tex");
2972 f = fopen(filename, "w");
2974 *dotptr = '\0';
2976 fprintf(f, "%% XCircuit output \"%s.tex\" for LaTeX input from %s%s\n",
2977 filename, filename, extend);
2978 fprintf(f, "\\def\\putbox#1#2#3#4{\\makebox[0in][l]{\\makebox[#1][l]{}"
2979 "\\raisebox{\\baselineskip}[0in][0in]"
2980 "{\\raisebox{#2}[0in][0in]{\\scalebox{#3}{#4}}}}}\n");
2981 fprintf(f, "\\def\\rightbox#1{\\makebox[0in][r]{#1}}\n");
2982 fprintf(f, "\\def\\centbox#1{\\makebox[0in]{#1}}\n");
2983 fprintf(f, "\\def\\topbox#1{\\raisebox{-0.60\\baselineskip}[0in][0in]{#1}}\n");
2984 fprintf(f, "\\def\\midbox#1{\\raisebox{-0.20\\baselineskip}[0in][0in]{#1}}\n");
2986 /* Modified to use \scalebox and \parbox by Alex Tercete, June 2008 */
2988 // fprintf(f, "\\begin{center}\n");
2990 outscale = xobjs.pagelist[areawin->page]->outscale;
2991 psscale = getpsscale(outscale, areawin->page);
2993 width = toplevelwidth(areawin->topinstance, &origin.x);
2994 height = toplevelheight(areawin->topinstance, &origin.y);
2996 /* Added 10/19/10: If there is a specified bounding box, let it */
2997 /* determine the figure origin; otherwise, the labels will be */
2998 /* mismatched to the bounding box. */
3000 if ((framebox = checkforbbox(topobject)) != NULL) {
3001 int i, maxx, maxy;
3003 origin.x = maxx = framebox->points[0].x;
3004 origin.y = maxy = framebox->points[0].y;
3005 for (i = 1; i < framebox->number; i++) {
3006 if (framebox->points[i].x < origin.x) origin.x = framebox->points[i].x;
3007 if (framebox->points[i].x > maxx) maxx = framebox->points[i].x;
3008 if (framebox->points[i].y < origin.y) origin.y = framebox->points[i].y;
3009 if (framebox->points[i].y > maxy) maxy = framebox->points[i].y;
3011 origin.x -= ((width - maxx + origin.x) / 2);
3012 origin.y -= ((height - maxy + origin.y) / 2);
3015 tx = (int)(72 / psscale) - origin.x,
3016 ty = (int)(72 / psscale) - origin.y;
3018 fprintf(f, " \\scalebox{%g}{\n", outscale);
3019 fprintf(f, " \\normalsize\n");
3020 fprintf(f, " \\parbox{%gin}{\n", (((float)width * psscale) / 72.0) / outscale);
3021 fprintf(f, " \\includegraphics[scale=%g]{%s}\\\\\n", 1.0 / outscale,
3022 filename);
3023 fprintf(f, " %% translate x=%d y=%d scale %3.2f\n", tx, ty, psscale);
3025 UPushCTM(); /* Save current state */
3026 UResetCTM(DCTM); /* Set to identity matrix */
3027 UDoLatex(areawin->topinstance, 0, f, psscale, outscale, tx, ty, NULL);
3028 UPopCTM(); /* Restore state */
3030 fprintf(f, " } %% close \'parbox\'\n");
3031 fprintf(f, " } %% close \'scalebox\'\n");
3032 fprintf(f, " \\vspace{-\\baselineskip} %% this is not"
3033 " necessary, but looks better\n");
3034 // fprintf(f, "\\end{center}\n");
3035 fclose(f);
3037 Wprintf("Wrote auxiliary file %s.tex", filename);