1
0

draw.h 16 KB

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