draw.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  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. #ifndef _PLAN9_SOURCE
  10. This header file is an extension to ANSI/POSIX
  11. #endif
  12. #ifndef __DRAW_H_
  13. #define __DRAW_H_
  14. #pragma src "/sys/src/ape/lib/draw"
  15. #pragma lib "/$M/lib/ape/libdraw.a"
  16. #include <u.h>
  17. #include <fmt.h>
  18. #include <qlock.h>
  19. typedef struct Cachefont Cachefont;
  20. typedef struct Cacheinfo Cacheinfo;
  21. typedef struct Cachesubf Cachesubf;
  22. typedef struct Display Display;
  23. typedef struct Font Font;
  24. typedef struct Fontchar Fontchar;
  25. typedef struct Image Image;
  26. typedef struct Mouse Mouse;
  27. typedef struct Point Point;
  28. typedef struct Rectangle Rectangle;
  29. typedef struct RGB RGB;
  30. typedef struct Screen Screen;
  31. typedef struct Subfont Subfont;
  32. #pragma varargck type "R" Rectangle
  33. #pragma varargck type "P" Point
  34. extern int Rfmt(Fmt*);
  35. extern int Pfmt(Fmt*);
  36. enum
  37. {
  38. DOpaque = 0xFFFFFFFF,
  39. DTransparent = 0x00000000, /* only useful for allocimage, memfillcolor */
  40. DBlack = 0x000000FF,
  41. DWhite = 0xFFFFFFFF,
  42. DRed = 0xFF0000FF,
  43. DGreen = 0x00FF00FF,
  44. DBlue = 0x0000FFFF,
  45. DCyan = 0x00FFFFFF,
  46. DMagenta = 0xFF00FFFF,
  47. DYellow = 0xFFFF00FF,
  48. DPaleyellow = 0xFFFFAAFF,
  49. DDarkyellow = 0xEEEE9EFF,
  50. DDarkgreen = 0x448844FF,
  51. DPalegreen = 0xAAFFAAFF,
  52. DMedgreen = 0x88CC88FF,
  53. DDarkblue = 0x000055FF,
  54. DPalebluegreen= 0xAAFFFFFF,
  55. DPaleblue = 0x0000BBFF,
  56. DBluegreen = 0x008888FF,
  57. DGreygreen = 0x55AAAAFF,
  58. DPalegreygreen = 0x9EEEEEFF,
  59. DYellowgreen = 0x99994CFF,
  60. DMedblue = 0x000099FF,
  61. DGreyblue = 0x005DBBFF,
  62. DPalegreyblue = 0x4993DDFF,
  63. DPurpleblue = 0x8888CCFF,
  64. DNotacolor = 0xFFFFFF00,
  65. DNofill = DNotacolor,
  66. };
  67. enum
  68. {
  69. Displaybufsize = 8000,
  70. ICOSSCALE = 1024,
  71. Borderwidth = 4,
  72. };
  73. enum
  74. {
  75. /* refresh methods */
  76. Refbackup = 0,
  77. Refnone = 1,
  78. Refmesg = 2
  79. };
  80. #define NOREFRESH ((void*)-1)
  81. enum
  82. {
  83. /* line ends */
  84. Endsquare = 0,
  85. Enddisc = 1,
  86. Endarrow = 2,
  87. Endmask = 0x1F
  88. };
  89. #define ARROW(a, b, c) (Endarrow|((a)<<5)|((b)<<14)|((c)<<23))
  90. typedef enum
  91. {
  92. /* Porter-Duff compositing operators */
  93. Clear = 0,
  94. SinD = 8,
  95. DinS = 4,
  96. SoutD = 2,
  97. DoutS = 1,
  98. S = SinD|SoutD,
  99. SoverD = SinD|SoutD|DoutS,
  100. SatopD = SinD|DoutS,
  101. SxorD = SoutD|DoutS,
  102. D = DinS|DoutS,
  103. DoverS = DinS|DoutS|SoutD,
  104. DatopS = DinS|SoutD,
  105. DxorS = DoutS|SoutD, /* == SxorD */
  106. Ncomp = 12,
  107. } Drawop;
  108. /*
  109. * image channel descriptors
  110. */
  111. enum {
  112. CRed = 0,
  113. CGreen,
  114. CBlue,
  115. CGrey,
  116. CAlpha,
  117. CMap,
  118. CIgnore,
  119. NChan,
  120. };
  121. #define __DC(type, nbits) ((((type)&15)<<4)|((nbits)&15))
  122. #define CHAN1(a,b) __DC(a,b)
  123. #define CHAN2(a,b,c,d) (CHAN1((a),(b))<<8|__DC((c),(d)))
  124. #define CHAN3(a,b,c,d,e,f) (CHAN2((a),(b),(c),(d))<<8|__DC((e),(f)))
  125. #define CHAN4(a,b,c,d,e,f,g,h) (CHAN3((a),(b),(c),(d),(e),(f))<<8|__DC((g),(h)))
  126. #define NBITS(c) ((c)&15)
  127. #define TYPE(c) (((c)>>4)&15)
  128. enum {
  129. GREY1 = CHAN1(CGrey, 1),
  130. GREY2 = CHAN1(CGrey, 2),
  131. GREY4 = CHAN1(CGrey, 4),
  132. GREY8 = CHAN1(CGrey, 8),
  133. CMAP8 = CHAN1(CMap, 8),
  134. RGB15 = CHAN4(CIgnore, 1, CRed, 5, CGreen, 5, CBlue, 5),
  135. RGB16 = CHAN3(CRed, 5, CGreen, 6, CBlue, 5),
  136. RGB24 = CHAN3(CRed, 8, CGreen, 8, CBlue, 8),
  137. RGBA32 = CHAN4(CRed, 8, CGreen, 8, CBlue, 8, CAlpha, 8),
  138. ARGB32 = CHAN4(CAlpha, 8, CRed, 8, CGreen, 8, CBlue, 8), /* stupid VGAs */
  139. XRGB32 = CHAN4(CIgnore, 8, CRed, 8, CGreen, 8, CBlue, 8),
  140. BGR24 = CHAN3(CBlue, 8, CGreen, 8, CRed, 8),
  141. ABGR32 = CHAN4(CAlpha, 8, CBlue, 8, CGreen, 8, CRed, 8),
  142. XBGR32 = CHAN4(CIgnore, 8, CBlue, 8, CGreen, 8, CRed, 8),
  143. };
  144. extern char* chantostr(char*, ulong);
  145. extern ulong strtochan(char*);
  146. extern int chantodepth(ulong);
  147. struct Point
  148. {
  149. int x;
  150. int y;
  151. };
  152. struct Rectangle
  153. {
  154. Point min;
  155. Point max;
  156. };
  157. typedef void (*Reffn)(Image*, Rectangle, void*);
  158. struct Screen
  159. {
  160. Display *display; /* display holding data */
  161. int id; /* id of system-held Screen */
  162. Image *image; /* unused; for reference only */
  163. Image *fill; /* color to paint behind windows */
  164. };
  165. struct Display
  166. {
  167. QLock qlock;
  168. int locking; /*program is using lockdisplay */
  169. int dirno;
  170. int fd;
  171. int reffd;
  172. int ctlfd;
  173. int imageid;
  174. int local;
  175. void (*error)(Display*, char*);
  176. char *devdir;
  177. char *windir;
  178. char oldlabel[64];
  179. ulong dataqid;
  180. Image *white;
  181. Image *black;
  182. Image *opaque;
  183. Image *transparent;
  184. Image *image;
  185. uchar *buf;
  186. int bufsize;
  187. uchar *bufp;
  188. Font *defaultfont;
  189. Subfont *defaultsubfont;
  190. Image *windows;
  191. Image *screenimage;
  192. int _isnewdisplay;
  193. };
  194. struct Image
  195. {
  196. Display *display; /* display holding data */
  197. int id; /* id of system-held Image */
  198. Rectangle r; /* rectangle in data area, local coords */
  199. Rectangle clipr; /* clipping region */
  200. int depth; /* number of bits per pixel */
  201. ulong chan;
  202. int repl; /* flag: data replicates to tile clipr */
  203. Screen *screen; /* 0 if not a window */
  204. Image *next; /* next in list of windows */
  205. };
  206. struct RGB
  207. {
  208. ulong red;
  209. ulong green;
  210. ulong blue;
  211. };
  212. /*
  213. * Subfonts
  214. *
  215. * given char c, Subfont *f, Fontchar *i, and Point p, one says
  216. * i = f->info+c;
  217. * draw(b, Rect(p.x+i->left, p.y+i->top,
  218. * p.x+i->left+((i+1)->x-i->x), p.y+i->bottom),
  219. * color, f->bits, Pt(i->x, i->top));
  220. * p.x += i->width;
  221. * to draw characters in the specified color (itself an Image) in Image b.
  222. */
  223. struct Fontchar
  224. {
  225. int x; /* left edge of bits */
  226. uchar top; /* first non-zero scan-line */
  227. uchar bottom; /* last non-zero scan-line + 1 */
  228. char left; /* offset of baseline */
  229. uchar width; /* width of baseline */
  230. };
  231. struct Subfont
  232. {
  233. char *name;
  234. short n; /* number of chars in font */
  235. uchar height; /* height of image */
  236. char ascent; /* top of image to baseline */
  237. Fontchar *info; /* n+1 character descriptors */
  238. Image *bits; /* of font */
  239. int ref;
  240. };
  241. enum
  242. {
  243. /* starting values */
  244. LOG2NFCACHE = 6,
  245. NFCACHE = (1<<LOG2NFCACHE), /* #chars cached */
  246. NFLOOK = 5, /* #chars to scan in cache */
  247. NFSUBF = 2, /* #subfonts to cache */
  248. /* max value */
  249. MAXFCACHE = 1024+NFLOOK, /* upper limit */
  250. MAXSUBF = 50, /* generous upper limit */
  251. /* deltas */
  252. DSUBF = 4,
  253. /* expiry ages */
  254. SUBFAGE = 10000,
  255. CACHEAGE = 10000
  256. };
  257. struct Cachefont
  258. {
  259. Rune min; /* lowest rune value to be taken from subfont */
  260. Rune max; /* highest rune value+1 to be taken from subfont */
  261. int offset; /* position in subfont of character at min */
  262. char *name; /* stored in font */
  263. char *subfontname; /* to access subfont */
  264. };
  265. struct Cacheinfo
  266. {
  267. ushort x; /* left edge of bits */
  268. uchar width; /* width of baseline */
  269. schar left; /* offset of baseline */
  270. Rune value; /* value of character at this slot in cache */
  271. ushort age;
  272. };
  273. struct Cachesubf
  274. {
  275. ulong age; /* for replacement */
  276. Cachefont *cf; /* font info that owns us */
  277. Subfont *f; /* attached subfont */
  278. };
  279. struct Font
  280. {
  281. char *name;
  282. Display *display;
  283. short height; /* max height of image, interline spacing */
  284. short ascent; /* top of image to baseline */
  285. short width; /* widest so far; used in caching only */
  286. short nsub; /* number of subfonts */
  287. ulong age; /* increasing counter; used for LRU */
  288. int maxdepth; /* maximum depth of all loaded subfonts */
  289. int ncache; /* size of cache */
  290. int nsubf; /* size of subfont list */
  291. Cacheinfo *cache;
  292. Cachesubf *subf;
  293. Cachefont **sub; /* as read from file */
  294. Image *cacheimage;
  295. };
  296. #define Dx(r) ((r).max.x-(r).min.x)
  297. #define Dy(r) ((r).max.y-(r).min.y)
  298. #ifdef __cplusplus
  299. extern "C" {
  300. #endif
  301. /*
  302. * Image management
  303. */
  304. extern Image* _allocimage(Image*, Display*, Rectangle, ulong, int, ulong, int, int);
  305. extern Image* allocimage(Display*, Rectangle, ulong, int, ulong);
  306. extern uchar* bufimage(Display*, int);
  307. extern int bytesperline(Rectangle, int);
  308. extern void closedisplay(Display*);
  309. extern void drawerror(Display*, char*);
  310. extern int flushimage(Display*, int);
  311. extern int freeimage(Image*);
  312. extern int _freeimage1(Image*);
  313. extern int geninitdraw(char*, void(*)(Display*, char*), char*, char*, char*, int);
  314. extern int initdraw(void(*)(Display*, char*), char*, char*);
  315. extern int newwindow(char*);
  316. extern Display* initdisplay(char*, char*, void(*)(Display*, char*));
  317. extern int loadimage(Image*, Rectangle, uchar*, int);
  318. extern int cloadimage(Image*, Rectangle, uchar*, int);
  319. extern int getwindow(Display*, int);
  320. extern int gengetwindow(Display*, char*, Image**, Screen**, int);
  321. extern Image* readimage(Display*, int, int);
  322. extern Image* creadimage(Display*, int, int);
  323. extern int unloadimage(Image*, Rectangle, uchar*, int);
  324. extern int wordsperline(Rectangle, int);
  325. extern int writeimage(int, Image*, int);
  326. extern Image* namedimage(Display*, char*);
  327. extern int nameimage(Image*, char*, int);
  328. extern Image* allocimagemix(Display*, ulong, ulong);
  329. /*
  330. * Colors
  331. */
  332. extern void readcolmap(Display*, RGB*);
  333. extern void writecolmap(Display*, RGB*);
  334. extern ulong setalpha(ulong, uchar);
  335. /*
  336. * Windows
  337. */
  338. extern Screen* allocscreen(Image*, Image*, int);
  339. extern Image* _allocwindow(Image*, Screen*, Rectangle, int, ulong);
  340. extern Image* allocwindow(Screen*, Rectangle, int, ulong);
  341. extern void bottomnwindows(Image**, int);
  342. extern void bottomwindow(Image*);
  343. extern int freescreen(Screen*);
  344. extern Screen* publicscreen(Display*, int, ulong);
  345. extern void topnwindows(Image**, int);
  346. extern void topwindow(Image*);
  347. extern int originwindow(Image*, Point, Point);
  348. /*
  349. * Geometry
  350. */
  351. extern Point Pt(int, int);
  352. extern Rectangle Rect(int, int, int, int);
  353. extern Rectangle Rpt(Point, Point);
  354. extern Point addpt(Point, Point);
  355. extern Point subpt(Point, Point);
  356. extern Point divpt(Point, int);
  357. extern Point mulpt(Point, int);
  358. extern int eqpt(Point, Point);
  359. extern int eqrect(Rectangle, Rectangle);
  360. extern Rectangle insetrect(Rectangle, int);
  361. extern Rectangle rectaddpt(Rectangle, Point);
  362. extern Rectangle rectsubpt(Rectangle, Point);
  363. extern Rectangle canonrect(Rectangle);
  364. extern int rectXrect(Rectangle, Rectangle);
  365. extern int rectinrect(Rectangle, Rectangle);
  366. extern void combinerect(Rectangle*, Rectangle);
  367. extern int rectclip(Rectangle*, Rectangle);
  368. extern int ptinrect(Point, Rectangle);
  369. extern void replclipr(Image*, int, Rectangle);
  370. extern int drawreplxy(int, int, int); /* used to be drawsetxy */
  371. extern Point drawrepl(Rectangle, Point);
  372. extern int rgb2cmap(int, int, int);
  373. extern int cmap2rgb(int);
  374. extern int cmap2rgba(int);
  375. extern void icossin(int, int*, int*);
  376. extern void icossin2(int, int, int*, int*);
  377. /*
  378. * Graphics
  379. */
  380. extern void draw(Image*, Rectangle, Image*, Image*, Point);
  381. extern void drawop(Image*, Rectangle, Image*, Image*, Point, Drawop);
  382. extern void gendraw(Image*, Rectangle, Image*, Point, Image*, Point);
  383. extern void gendrawop(Image*, Rectangle, Image*, Point, Image*, Point, Drawop);
  384. extern void line(Image*, Point, Point, int, int, int, Image*, Point);
  385. extern void lineop(Image*, Point, Point, int, int, int, Image*, Point, Drawop);
  386. extern void poly(Image*, Point*, int, int, int, int, Image*, Point);
  387. extern void polyop(Image*, Point*, int, int, int, int, Image*, Point, Drawop);
  388. extern void fillpoly(Image*, Point*, int, int, Image*, Point);
  389. extern void fillpolyop(Image*, Point*, int, int, Image*, Point, Drawop);
  390. extern Point string(Image*, Point, Image*, Point, Font*, char*);
  391. extern Point stringop(Image*, Point, Image*, Point, Font*, char*, Drawop);
  392. extern Point stringn(Image*, Point, Image*, Point, Font*, char*, int);
  393. extern Point stringnop(Image*, Point, Image*, Point, Font*, char*, int, Drawop);
  394. extern Point runestring(Image*, Point, Image*, Point, Font*, Rune*);
  395. extern Point runestringop(Image*, Point, Image*, Point, Font*, Rune*, Drawop);
  396. extern Point runestringn(Image*, Point, Image*, Point, Font*, Rune*, int);
  397. extern Point runestringnop(Image*, Point, Image*, Point, Font*, Rune*, int, Drawop);
  398. extern Point stringbg(Image*, Point, Image*, Point, Font*, char*, Image*, Point);
  399. extern Point stringbgop(Image*, Point, Image*, Point, Font*, char*, Image*, Point, Drawop);
  400. extern Point stringnbg(Image*, Point, Image*, Point, Font*, char*, int, Image*, Point);
  401. extern Point stringnbgop(Image*, Point, Image*, Point, Font*, char*, int, Image*, Point, Drawop);
  402. extern Point runestringbg(Image*, Point, Image*, Point, Font*, Rune*, Image*, Point);
  403. extern Point runestringbgop(Image*, Point, Image*, Point, Font*, Rune*, Image*, Point, Drawop);
  404. extern Point runestringnbg(Image*, Point, Image*, Point, Font*, Rune*, int, Image*, Point);
  405. extern Point runestringnbgop(Image*, Point, Image*, Point, Font*, Rune*, int, Image*, Point, Drawop);
  406. extern Point _string(Image*, Point, Image*, Point, Font*, char*, Rune*, int, Rectangle, Image*, Point, Drawop);
  407. extern Point stringsubfont(Image*, Point, Image*, Subfont*, char*);
  408. extern int bezier(Image*, Point, Point, Point, Point, int, int, int, Image*, Point);
  409. extern int bezierop(Image*, Point, Point, Point, Point, int, int, int, Image*, Point, Drawop);
  410. extern int bezspline(Image*, Point*, int, int, int, int, Image*, Point);
  411. extern int bezsplineop(Image*, Point*, int, int, int, int, Image*, Point, Drawop);
  412. extern int bezsplinepts(Point*, int, Point**);
  413. extern int fillbezier(Image*, Point, Point, Point, Point, int, Image*, Point);
  414. extern int fillbezierop(Image*, Point, Point, Point, Point, int, Image*, Point, Drawop);
  415. extern int fillbezspline(Image*, Point*, int, int, Image*, Point);
  416. extern int fillbezsplineop(Image*, Point*, int, int, Image*, Point, Drawop);
  417. extern void ellipse(Image*, Point, int, int, int, Image*, Point);
  418. extern void ellipseop(Image*, Point, int, int, int, Image*, Point, Drawop);
  419. extern void fillellipse(Image*, Point, int, int, Image*, Point);
  420. extern void fillellipseop(Image*, Point, int, int, Image*, Point, Drawop);
  421. extern void arc(Image*, Point, int, int, int, Image*, Point, int, int);
  422. extern void arcop(Image*, Point, int, int, int, Image*, Point, int, int, Drawop);
  423. extern void fillarc(Image*, Point, int, int, Image*, Point, int, int);
  424. extern void fillarcop(Image*, Point, int, int, Image*, Point, int, int, Drawop);
  425. extern void border(Image*, Rectangle, int, Image*, Point);
  426. extern void borderop(Image*, Rectangle, int, Image*, Point, Drawop);
  427. /*
  428. * Font management
  429. */
  430. extern Font* openfont(Display*, char*);
  431. extern Font* buildfont(Display*, char*, char*);
  432. extern void freefont(Font*);
  433. extern Font* mkfont(Subfont*, Rune);
  434. extern int cachechars(Font*, char**, Rune**, ushort*, int, int*, char**);
  435. extern void agefont(Font*);
  436. extern Subfont* allocsubfont(char*, int, int, int, Fontchar*, Image*);
  437. extern Subfont* lookupsubfont(Display*, char*);
  438. extern void installsubfont(char*, Subfont*);
  439. extern void uninstallsubfont(Subfont*);
  440. extern void freesubfont(Subfont*);
  441. extern Subfont* readsubfont(Display*, char*, int, int);
  442. extern Subfont* readsubfonti(Display*, char*, int, Image*, int);
  443. extern int writesubfont(int, Subfont*);
  444. extern void _unpackinfo(Fontchar*, uchar*, int);
  445. extern Point stringsize(Font*, char*);
  446. extern int stringwidth(Font*, char*);
  447. extern int stringnwidth(Font*, char*, int);
  448. extern Point runestringsize(Font*, Rune*);
  449. extern int runestringwidth(Font*, Rune*);
  450. extern int runestringnwidth(Font*, Rune*, int);
  451. extern Point strsubfontwidth(Subfont*, char*);
  452. extern int loadchar(Font*, Rune, Cacheinfo*, int, int, char**);
  453. extern char* subfontname(char*, char*, int);
  454. extern Subfont* _getsubfont(Display*, char*);
  455. extern Subfont* getdefont(Display*);
  456. extern void lockdisplay(Display*);
  457. extern void unlockdisplay(Display*);
  458. extern int drawlsetrefresh(ulong, int, void*, void*);
  459. /*
  460. * Predefined
  461. */
  462. extern uchar defontdata[];
  463. extern int sizeofdefont;
  464. extern Point ZP;
  465. extern Rectangle ZR;
  466. /*
  467. * Set up by initdraw()
  468. */
  469. extern Display *display;
  470. extern Font *font;
  471. extern Image *screen;
  472. extern Screen *_screen;
  473. extern int _cursorfd;
  474. extern int _drawdebug; /* set to 1 to see errors from flushimage */
  475. extern void _setdrawop(Display*, Drawop);
  476. #define BGSHORT(p) (((p)[0]<<0) | ((p)[1]<<8))
  477. #define BGLONG(p) ((BGSHORT(p)<<0) | (BGSHORT(p+2)<<16))
  478. #define BPSHORT(p, v) ((p)[0]=(v), (p)[1]=((v)>>8))
  479. #define BPLONG(p, v) (BPSHORT(p, (v)), BPSHORT(p+2, (v)>>16))
  480. /*
  481. * Compressed image file parameters and helper routines
  482. */
  483. #define NMATCH 3 /* shortest match possible */
  484. #define NRUN (NMATCH+31) /* longest match possible */
  485. #define NMEM 1024 /* window size */
  486. #define NDUMP 128 /* maximum length of dump */
  487. #define NCBLOCK 6000 /* size of compressed blocks */
  488. extern void _twiddlecompressed(uchar*, int);
  489. extern int _compblocksize(Rectangle, int);
  490. /* XXX backwards helps; should go */
  491. extern ulong drawld2chan[];
  492. extern void drawsetdebug(int);
  493. #ifdef __cplusplus
  494. }
  495. #endif
  496. #endif