draw.h 16 KB

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