blockgen.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include "pic.h"
  12. #include "y.tab.h"
  13. #define NBRACK 20 /* depth of [...] */
  14. #define NBRACE 20 /* depth of {...} */
  15. struct pushstack stack[NBRACK];
  16. int nstack = 0;
  17. struct pushstack bracestack[NBRACE];
  18. int nbstack = 0;
  19. void blockadj(obj *);
  20. obj *leftthing(int c) /* called for {... or [... */
  21. /* really ought to be separate functions */
  22. {
  23. obj *p;
  24. if (c == '[') {
  25. if (nstack >= NBRACK)
  26. ERROR "[...] nested too deep" FATAL;
  27. stack[nstack].p_x = curx;
  28. stack[nstack].p_y = cury;
  29. stack[nstack].p_hvmode = hvmode;
  30. curx = cury = 0;
  31. stack[nstack].p_xmin = xmin;
  32. stack[nstack].p_xmax = xmax;
  33. stack[nstack].p_ymin = ymin;
  34. stack[nstack].p_ymax = ymax;
  35. nstack++;
  36. xmin = ymin = 30000;
  37. xmax = ymax = -30000;
  38. p = makenode(BLOCK, 7);
  39. p->o_val[4] = nobj; /* 1st item within [...] */
  40. if (p->o_nobj != nobj-1)
  41. fprintf(stderr, "nobjs wrong%d %d\n", p->o_nobj, nobj);
  42. } else {
  43. if (nbstack >= NBRACK)
  44. ERROR "{...} nested too deep" FATAL;
  45. bracestack[nbstack].p_x = curx;
  46. bracestack[nbstack].p_y = cury;
  47. bracestack[nbstack].p_hvmode = hvmode;
  48. nbstack++;
  49. p = NULL;
  50. }
  51. return(p);
  52. }
  53. obj *rightthing(obj *p, int c) /* called for ... ] or ... } */
  54. {
  55. obj *q;
  56. if (c == '}') {
  57. nbstack--;
  58. curx = bracestack[nbstack].p_x;
  59. cury = bracestack[nbstack].p_y;
  60. hvmode = bracestack[nbstack].p_hvmode;
  61. q = makenode(MOVE, 0);
  62. dprintf("M %g %g\n", curx, cury);
  63. } else {
  64. nstack--;
  65. curx = stack[nstack].p_x;
  66. cury = stack[nstack].p_y;
  67. hvmode = stack[nstack].p_hvmode;
  68. q = makenode(BLOCKEND, 7);
  69. q->o_val[4] = p->o_nobj + 1; /* back pointer */
  70. p->o_val[5] = q->o_nobj - 1; /* forward pointer */
  71. if (xmin > xmax) /* nothing happened */
  72. xmin = xmax;
  73. if (ymin > ymax)
  74. ymin = ymax;
  75. p->o_val[0] = xmin; p->o_val[1] = ymin;
  76. p->o_val[2] = xmax; p->o_val[3] = ymax;
  77. p->o_symtab = q->o_symtab = stack[nstack+1].p_symtab;
  78. xmin = stack[nstack].p_xmin;
  79. ymin = stack[nstack].p_ymin;
  80. xmax = stack[nstack].p_xmax;
  81. ymax = stack[nstack].p_ymax;
  82. }
  83. return(q);
  84. }
  85. obj *blockgen(obj *p, obj *q) /* handles [...] */
  86. {
  87. int i, invis, at, with;
  88. double ddval, h, w, xwith, ywith;
  89. double x0, y0, x1, y1, cx, cy;
  90. obj *ppos;
  91. Attr *ap;
  92. invis = at = 0;
  93. with = xwith = ywith = 0;
  94. ddval = 0;
  95. w = p->o_val[2] - p->o_val[0];
  96. h = p->o_val[3] - p->o_val[1];
  97. cx = (p->o_val[2] + p->o_val[0]) / 2; /* geom ctr of [] wrt local orogin */
  98. cy = (p->o_val[3] + p->o_val[1]) / 2;
  99. dprintf("cx,cy=%g,%g\n", cx, cy);
  100. for (i = 0; i < nattr; i++) {
  101. ap = &attr[i];
  102. switch (ap->a_type) {
  103. case HEIGHT:
  104. h = ap->a_val.f;
  105. break;
  106. case WIDTH:
  107. w = ap->a_val.f;
  108. break;
  109. case WITH:
  110. with = ap->a_val.i; /* corner */
  111. break;
  112. case PLACE: /* actually with position ... */
  113. ppos = ap->a_val.o;
  114. xwith = cx - ppos->o_x;
  115. ywith = cy - ppos->o_y;
  116. with = PLACE;
  117. break;
  118. case AT:
  119. case FROM:
  120. ppos = ap->a_val.o;
  121. curx = ppos->o_x;
  122. cury = ppos->o_y;
  123. at++;
  124. break;
  125. case INVIS:
  126. invis = INVIS;
  127. break;
  128. case TEXTATTR:
  129. savetext(ap->a_sub, ap->a_val.p);
  130. break;
  131. }
  132. }
  133. if (with) {
  134. switch (with) {
  135. case NORTH: ywith = -h / 2; break;
  136. case SOUTH: ywith = h / 2; break;
  137. case EAST: xwith = -w / 2; break;
  138. case WEST: xwith = w / 2; break;
  139. case NE: xwith = -w / 2; ywith = -h / 2; break;
  140. case SE: xwith = -w / 2; ywith = h / 2; break;
  141. case NW: xwith = w / 2; ywith = -h / 2; break;
  142. case SW: xwith = w / 2; ywith = h / 2; break;
  143. }
  144. curx += xwith;
  145. cury += ywith;
  146. }
  147. if (!at) {
  148. if (isright(hvmode))
  149. curx += w / 2;
  150. else if (isleft(hvmode))
  151. curx -= w / 2;
  152. else if (isup(hvmode))
  153. cury += h / 2;
  154. else
  155. cury -= h / 2;
  156. }
  157. x0 = curx - w / 2;
  158. y0 = cury - h / 2;
  159. x1 = curx + w / 2;
  160. y1 = cury + h / 2;
  161. extreme(x0, y0);
  162. extreme(x1, y1);
  163. p->o_x = curx;
  164. p->o_y = cury;
  165. p->o_nt1 = ntext1;
  166. p->o_nt2 = ntext;
  167. ntext1 = ntext;
  168. p->o_val[0] = w;
  169. p->o_val[1] = h;
  170. p->o_val[2] = cx;
  171. p->o_val[3] = cy;
  172. p->o_val[5] = q->o_nobj - 1; /* last item in [...] */
  173. p->o_ddval = ddval;
  174. p->o_attr = invis;
  175. dprintf("[] %g %g %g %g at %g %g, h=%g, w=%g\n", x0, y0, x1, y1, curx, cury, h, w);
  176. if (isright(hvmode))
  177. curx = x1;
  178. else if (isleft(hvmode))
  179. curx = x0;
  180. else if (isup(hvmode))
  181. cury = y1;
  182. else
  183. cury = y0;
  184. for (i = 0; i <= 5; i++)
  185. q->o_val[i] = p->o_val[i];
  186. stack[nstack+1].p_symtab = NULL; /* so won't be found again */
  187. blockadj(p); /* fix up coords for enclosed blocks */
  188. return(p);
  189. }
  190. void blockadj(obj *p) /* adjust coords in block starting at p */
  191. {
  192. double dx, dy;
  193. int n, lev;
  194. dx = p->o_x - p->o_val[2];
  195. dy = p->o_y - p->o_val[3];
  196. n = p->o_nobj + 1;
  197. dprintf("into blockadj: dx,dy=%g,%g\n", dx, dy);
  198. for (lev = 1; lev > 0; n++) {
  199. p = objlist[n];
  200. if (p->o_type == BLOCK)
  201. lev++;
  202. else if (p->o_type == BLOCKEND)
  203. lev--;
  204. dprintf("blockadj: type=%d o_x,y=%g,%g;", p->o_type, p->o_x, p->o_y);
  205. p->o_x += dx;
  206. p->o_y += dy;
  207. dprintf(" becomes %g,%g\n", p->o_x, p->o_y);
  208. switch (p->o_type) { /* other absolute coords */
  209. case LINE:
  210. case ARROW:
  211. case SPLINE:
  212. p->o_val[0] += dx;
  213. p->o_val[1] += dy;
  214. break;
  215. case ARC:
  216. p->o_val[0] += dx;
  217. p->o_val[1] += dy;
  218. p->o_val[2] += dx;
  219. p->o_val[3] += dy;
  220. break;
  221. }
  222. }
  223. }