line.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  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 <u.h>
  10. #include <libc.h>
  11. #include <draw.h>
  12. #include <memdraw.h>
  13. #include <memlayer.h>
  14. enum
  15. {
  16. Arrow1 = 8,
  17. Arrow2 = 10,
  18. Arrow3 = 3,
  19. };
  20. static
  21. int
  22. lmin(int a, int b)
  23. {
  24. if(a < b)
  25. return a;
  26. return b;
  27. }
  28. static
  29. int
  30. lmax(int a, int b)
  31. {
  32. if(a > b)
  33. return a;
  34. return b;
  35. }
  36. #ifdef NOTUSED
  37. /*
  38. * Rather than line clip, we run the Bresenham loop over the full line,
  39. * and clip on each pixel. This is more expensive but means that
  40. * lines look the same regardless of how the windowing has tiled them.
  41. * For speed, we check for clipping outside the loop and make the
  42. * test easy when possible.
  43. */
  44. static
  45. void
  46. horline1(Memimage *dst, Point p0, Point p1, int srcval, Rectangle clipr)
  47. {
  48. int x, y, dy, deltay, deltax, maxx;
  49. int dd, easy, e, bpp, m, m0;
  50. uint8_t *d;
  51. deltax = p1.x - p0.x;
  52. deltay = p1.y - p0.y;
  53. dd = dst->width*sizeof(ulong);
  54. dy = 1;
  55. if(deltay < 0){
  56. dd = -dd;
  57. deltay = -deltay;
  58. dy = -1;
  59. }
  60. maxx = lmin(p1.x, clipr.max.x-1);
  61. bpp = dst->depth;
  62. m0 = 0xFF^(0xFF>>bpp);
  63. m = m0 >> (p0.x&(7/dst->depth))*bpp;
  64. easy = ptinrect(p0, clipr) && ptinrect(p1, clipr);
  65. e = 2*deltay - deltax;
  66. y = p0.y;
  67. d = byteaddr(dst, p0);
  68. deltay *= 2;
  69. deltax = deltay - 2*deltax;
  70. for(x=p0.x; x<=maxx; x++){
  71. if(easy || (clipr.min.x<=x && clipr.min.y<=y && y<clipr.max.y))
  72. *d ^= (*d^srcval) & m;
  73. if(e > 0){
  74. y += dy;
  75. d += dd;
  76. e += deltax;
  77. }else
  78. e += deltay;
  79. d++;
  80. m >>= bpp;
  81. if(m == 0)
  82. m = m0;
  83. }
  84. }
  85. static
  86. void
  87. verline1(Memimage *dst, Point p0, Point p1, int srcval, Rectangle clipr)
  88. {
  89. int x, y, deltay, deltax, maxy;
  90. int easy, e, bpp, m, m0, dd;
  91. uint8_t *d;
  92. deltax = p1.x - p0.x;
  93. deltay = p1.y - p0.y;
  94. dd = 1;
  95. if(deltax < 0){
  96. dd = -1;
  97. deltax = -deltax;
  98. }
  99. maxy = lmin(p1.y, clipr.max.y-1);
  100. bpp = dst->depth;
  101. m0 = 0xFF^(0xFF>>bpp);
  102. m = m0 >> (p0.x&(7/dst->depth))*bpp;
  103. easy = ptinrect(p0, clipr) && ptinrect(p1, clipr);
  104. e = 2*deltax - deltay;
  105. x = p0.x;
  106. d = byteaddr(dst, p0);
  107. deltax *= 2;
  108. deltay = deltax - 2*deltay;
  109. for(y=p0.y; y<=maxy; y++){
  110. if(easy || (clipr.min.y<=y && clipr.min.x<=x && x<clipr.max.x))
  111. *d ^= (*d^srcval) & m;
  112. if(e > 0){
  113. x += dd;
  114. d += dd;
  115. e += deltay;
  116. }else
  117. e += deltax;
  118. d += dst->width*sizeof(ulong);
  119. m >>= bpp;
  120. if(m == 0)
  121. m = m0;
  122. }
  123. }
  124. static
  125. void
  126. horliner(Memimage *dst, Point p0, Point p1, Memimage *src, Point dsrc, Rectangle clipr)
  127. {
  128. int x, y, sx, sy, deltay, deltax, minx, maxx;
  129. int bpp, m, m0;
  130. uint8_t *d, *s;
  131. deltax = p1.x - p0.x;
  132. deltay = p1.y - p0.y;
  133. sx = drawreplxy(src->r.min.x, src->r.max.x, p0.x+dsrc.x);
  134. minx = lmax(p0.x, clipr.min.x);
  135. maxx = lmin(p1.x, clipr.max.x-1);
  136. bpp = dst->depth;
  137. m0 = 0xFF^(0xFF>>bpp);
  138. m = m0 >> (minx&(7/dst->depth))*bpp;
  139. for(x=minx; x<=maxx; x++){
  140. y = p0.y + (deltay*(x-p0.x)+deltax/2)/deltax;
  141. if(clipr.min.y<=y && y<clipr.max.y){
  142. d = byteaddr(dst, Pt(x, y));
  143. sy = drawreplxy(src->r.min.y, src->r.max.y, y+dsrc.y);
  144. s = byteaddr(src, Pt(sx, sy));
  145. *d ^= (*d^*s) & m;
  146. }
  147. if(++sx >= src->r.max.x)
  148. sx = src->r.min.x;
  149. m >>= bpp;
  150. if(m == 0)
  151. m = m0;
  152. }
  153. }
  154. static
  155. void
  156. verliner(Memimage *dst, Point p0, Point p1, Memimage *src, Point dsrc, Rectangle clipr)
  157. {
  158. int x, y, sx, sy, deltay, deltax, miny, maxy;
  159. int bpp, m, m0;
  160. uint8_t *d, *s;
  161. deltax = p1.x - p0.x;
  162. deltay = p1.y - p0.y;
  163. sy = drawreplxy(src->r.min.y, src->r.max.y, p0.y+dsrc.y);
  164. miny = lmax(p0.y, clipr.min.y);
  165. maxy = lmin(p1.y, clipr.max.y-1);
  166. bpp = dst->depth;
  167. m0 = 0xFF^(0xFF>>bpp);
  168. for(y=miny; y<=maxy; y++){
  169. if(deltay == 0) /* degenerate line */
  170. x = p0.x;
  171. else
  172. x = p0.x + (deltax*(y-p0.y)+deltay/2)/deltay;
  173. if(clipr.min.x<=x && x<clipr.max.x){
  174. m = m0 >> (x&(7/dst->depth))*bpp;
  175. d = byteaddr(dst, Pt(x, y));
  176. sx = drawreplxy(src->r.min.x, src->r.max.x, x+dsrc.x);
  177. s = byteaddr(src, Pt(sx, sy));
  178. *d ^= (*d^*s) & m;
  179. }
  180. if(++sy >= src->r.max.y)
  181. sy = src->r.min.y;
  182. }
  183. }
  184. static
  185. void
  186. horline(Memimage *dst, Point p0, Point p1, Memimage *src, Point dsrc, Rectangle clipr)
  187. {
  188. int x, y, deltay, deltax, minx, maxx;
  189. int bpp, m, m0;
  190. uint8_t *d, *s;
  191. deltax = p1.x - p0.x;
  192. deltay = p1.y - p0.y;
  193. minx = lmax(p0.x, clipr.min.x);
  194. maxx = lmin(p1.x, clipr.max.x-1);
  195. bpp = dst->depth;
  196. m0 = 0xFF^(0xFF>>bpp);
  197. m = m0 >> (minx&(7/dst->depth))*bpp;
  198. for(x=minx; x<=maxx; x++){
  199. y = p0.y + (deltay*(x-p0.x)+deltay/2)/deltax;
  200. if(clipr.min.y<=y && y<clipr.max.y){
  201. d = byteaddr(dst, Pt(x, y));
  202. s = byteaddr(src, addpt(dsrc, Pt(x, y)));
  203. *d ^= (*d^*s) & m;
  204. }
  205. m >>= bpp;
  206. if(m == 0)
  207. m = m0;
  208. }
  209. }
  210. static
  211. void
  212. verline(Memimage *dst, Point p0, Point p1, Memimage *src, Point dsrc, Rectangle clipr)
  213. {
  214. int x, y, deltay, deltax, miny, maxy;
  215. int bpp, m, m0;
  216. uint8_t *d, *s;
  217. deltax = p1.x - p0.x;
  218. deltay = p1.y - p0.y;
  219. miny = lmax(p0.y, clipr.min.y);
  220. maxy = lmin(p1.y, clipr.max.y-1);
  221. bpp = dst->depth;
  222. m0 = 0xFF^(0xFF>>bpp);
  223. for(y=miny; y<=maxy; y++){
  224. if(deltay == 0) /* degenerate line */
  225. x = p0.x;
  226. else
  227. x = p0.x + deltax*(y-p0.y)/deltay;
  228. if(clipr.min.x<=x && x<clipr.max.x){
  229. m = m0 >> (x&(7/dst->depth))*bpp;
  230. d = byteaddr(dst, Pt(x, y));
  231. s = byteaddr(src, addpt(dsrc, Pt(x, y)));
  232. *d ^= (*d^*s) & m;
  233. }
  234. }
  235. }
  236. #endif /* NOTUSED */
  237. static Memimage*
  238. membrush(int radius)
  239. {
  240. static Memimage *brush;
  241. static int brushradius;
  242. if(brush==nil || brushradius!=radius){
  243. freememimage(brush);
  244. brush = allocmemimage(Rect(0, 0, 2*radius+1, 2*radius+1), memopaque->chan);
  245. if(brush != nil){
  246. memfillcolor(brush, DTransparent); /* zeros */
  247. memellipse(brush, Pt(radius, radius), radius, radius, -1, memopaque, Pt(radius, radius), S);
  248. }
  249. brushradius = radius;
  250. }
  251. return brush;
  252. }
  253. static
  254. void
  255. discend(Point p, int radius, Memimage *dst, Memimage *src, Point dsrc, int op)
  256. {
  257. Memimage *disc;
  258. Rectangle r;
  259. disc = membrush(radius);
  260. if(disc != nil){
  261. r.min.x = p.x - radius;
  262. r.min.y = p.y - radius;
  263. r.max.x = p.x + radius+1;
  264. r.max.y = p.y + radius+1;
  265. memdraw(dst, r, src, addpt(r.min, dsrc), disc, Pt(0,0), op);
  266. }
  267. }
  268. static
  269. void
  270. arrowend(Point tip, Point *pp, int end, int sin, int cos, int radius)
  271. {
  272. int x1, x2, x3;
  273. /* before rotation */
  274. if(end == Endarrow){
  275. x1 = Arrow1;
  276. x2 = Arrow2;
  277. x3 = Arrow3;
  278. }else{
  279. x1 = (end>>5) & 0x1FF; /* distance along line from end of line to tip */
  280. x2 = (end>>14) & 0x1FF; /* distance along line from barb to tip */
  281. x3 = (end>>23) & 0x1FF; /* distance perpendicular from edge of line to barb */
  282. }
  283. /* comments follow track of right-facing arrowhead */
  284. pp->x = tip.x+((2*radius+1)*sin/2-x1*cos); /* upper side of shaft */
  285. pp->y = tip.y-((2*radius+1)*cos/2+x1*sin);
  286. pp++;
  287. pp->x = tip.x+((2*radius+2*x3+1)*sin/2-x2*cos); /* upper barb */
  288. pp->y = tip.y-((2*radius+2*x3+1)*cos/2+x2*sin);
  289. pp++;
  290. pp->x = tip.x;
  291. pp->y = tip.y;
  292. pp++;
  293. pp->x = tip.x+(-(2*radius+2*x3+1)*sin/2-x2*cos); /* lower barb */
  294. pp->y = tip.y-(-(2*radius+2*x3+1)*cos/2+x2*sin);
  295. pp++;
  296. pp->x = tip.x+(-(2*radius+1)*sin/2-x1*cos); /* lower side of shaft */
  297. pp->y = tip.y+((2*radius+1)*cos/2-x1*sin);
  298. }
  299. void
  300. _memimageline(Memimage *dst, Point p0, Point p1, int end0, int end1, int radius, Memimage *src, Point sp, Rectangle clipr, int op)
  301. {
  302. /*
  303. * BUG: We should really really pick off purely horizontal and purely
  304. * vertical lines and handle them separately with calls to memimagedraw
  305. * on rectangles.
  306. */
  307. int hor;
  308. int sin, cos, dx, dy, t;
  309. Rectangle oclipr, r;
  310. Point q, pts[10], *pp, d;
  311. if(radius < 0)
  312. return;
  313. if(rectclip(&clipr, dst->r) == 0)
  314. return;
  315. if(rectclip(&clipr, dst->clipr) == 0)
  316. return;
  317. d = subpt(sp, p0);
  318. if(rectclip(&clipr, rectsubpt(src->clipr, d)) == 0)
  319. return;
  320. if((src->flags&Frepl)==0 && rectclip(&clipr, rectsubpt(src->r, d))==0)
  321. return;
  322. /* this means that only verline() handles degenerate lines (p0==p1) */
  323. hor = (abs(p1.x-p0.x) > abs(p1.y-p0.y));
  324. /*
  325. * Clipping is a little peculiar. We can't use Sutherland-Cohen
  326. * clipping because lines are wide. But this is probably just fine:
  327. * we do all math with the original p0 and p1, but clip when deciding
  328. * what pixels to draw. This means the layer code can call this routine,
  329. * using clipr to define the region being written, and get the same set
  330. * of pixels regardless of the dicing.
  331. */
  332. if((hor && p0.x>p1.x) || (!hor && p0.y>p1.y)){
  333. q = p0;
  334. p0 = p1;
  335. p1 = q;
  336. t = end0;
  337. end0 = end1;
  338. end1 = t;
  339. }
  340. if((p0.x == p1.x || p0.y == p1.y) && (end0&0x1F) == Endsquare && (end1&0x1F) == Endsquare){
  341. r.min = p0;
  342. r.max = p1;
  343. if(p0.x == p1.x){
  344. r.min.x -= radius;
  345. r.max.x += radius+1;
  346. }
  347. else{
  348. r.min.y -= radius;
  349. r.max.y += radius+1;
  350. }
  351. oclipr = dst->clipr;
  352. sp = addpt(r.min, d);
  353. dst->clipr = clipr;
  354. memimagedraw(dst, r, src, sp, memopaque, sp, op);
  355. dst->clipr = oclipr;
  356. return;
  357. }
  358. /* Hard: */
  359. /* draw thick line using polygon fill */
  360. icossin2(p1.x-p0.x, p1.y-p0.y, &cos, &sin);
  361. dx = (sin*(2*radius+1))/2;
  362. dy = (cos*(2*radius+1))/2;
  363. pp = pts;
  364. oclipr = dst->clipr;
  365. dst->clipr = clipr;
  366. q.x = ICOSSCALE*p0.x+ICOSSCALE/2-cos/2;
  367. q.y = ICOSSCALE*p0.y+ICOSSCALE/2-sin/2;
  368. switch(end0 & 0x1F){
  369. case Enddisc:
  370. discend(p0, radius, dst, src, d, op);
  371. /* fall through */
  372. case Endsquare:
  373. default:
  374. pp->x = q.x-dx;
  375. pp->y = q.y+dy;
  376. pp++;
  377. pp->x = q.x+dx;
  378. pp->y = q.y-dy;
  379. pp++;
  380. break;
  381. case Endarrow:
  382. arrowend(q, pp, end0, -sin, -cos, radius);
  383. _memfillpolysc(dst, pts, 5, ~0, src, addpt(pts[0], mulpt(d, ICOSSCALE)), 1, 10, 1, op);
  384. pp[1] = pp[4];
  385. pp += 2;
  386. }
  387. q.x = ICOSSCALE*p1.x+ICOSSCALE/2+cos/2;
  388. q.y = ICOSSCALE*p1.y+ICOSSCALE/2+sin/2;
  389. switch(end1 & 0x1F){
  390. case Enddisc:
  391. discend(p1, radius, dst, src, d, op);
  392. /* fall through */
  393. case Endsquare:
  394. default:
  395. pp->x = q.x+dx;
  396. pp->y = q.y-dy;
  397. pp++;
  398. pp->x = q.x-dx;
  399. pp->y = q.y+dy;
  400. pp++;
  401. break;
  402. case Endarrow:
  403. arrowend(q, pp, end1, sin, cos, radius);
  404. _memfillpolysc(dst, pp, 5, ~0, src, addpt(pp[0], mulpt(d, ICOSSCALE)), 1, 10, 1, op);
  405. pp[1] = pp[4];
  406. pp += 2;
  407. }
  408. _memfillpolysc(dst, pts, pp-pts, ~0, src, addpt(pts[0], mulpt(d, ICOSSCALE)), 0, 10, 1, op);
  409. dst->clipr = oclipr;
  410. return;
  411. }
  412. void
  413. memimageline(Memimage *dst, Point p0, Point p1, int end0, int end1, int radius, Memimage *src, Point sp, int op)
  414. {
  415. _memimageline(dst, p0, p1, end0, end1, radius, src, sp, dst->clipr, op);
  416. }
  417. /*
  418. * Simple-minded conservative code to compute bounding box of line.
  419. * Result is probably a little larger than it needs to be.
  420. */
  421. static
  422. void
  423. addbbox(Rectangle *r, Point p)
  424. {
  425. if(r->min.x > p.x)
  426. r->min.x = p.x;
  427. if(r->min.y > p.y)
  428. r->min.y = p.y;
  429. if(r->max.x < p.x+1)
  430. r->max.x = p.x+1;
  431. if(r->max.y < p.y+1)
  432. r->max.y = p.y+1;
  433. }
  434. int
  435. memlineendsize(int end)
  436. {
  437. int x3;
  438. if((end&0x3F) != Endarrow)
  439. return 0;
  440. if(end == Endarrow)
  441. x3 = Arrow3;
  442. else
  443. x3 = (end>>23) & 0x1FF;
  444. return x3;
  445. }
  446. Rectangle
  447. memlinebbox(Point p0, Point p1, int end0, int end1, int radius)
  448. {
  449. Rectangle r, r1;
  450. int extra;
  451. r.min.x = 10000000;
  452. r.min.y = 10000000;
  453. r.max.x = -10000000;
  454. r.max.y = -10000000;
  455. extra = lmax(memlineendsize(end0), memlineendsize(end1));
  456. r1 = insetrect(canonrect(Rpt(p0, p1)), -(radius+extra));
  457. addbbox(&r, r1.min);
  458. addbbox(&r, r1.max);
  459. return r;
  460. }