draw.h 16 KB

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