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