draw.h 14 KB

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