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