1
0

draw.h 16 KB

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