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