draw.h 16 KB

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