draw.h 16 KB

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