]> git.lizzy.rs Git - plan9front.git/blob - sys/include/draw.h
libc, seconds: new time and date apis (try 2)
[plan9front.git] / sys / include / draw.h
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 extern int              badrect(Rectangle);
405
406 /*
407  * Graphics
408  */
409 extern void     draw(Image*, Rectangle, Image*, Image*, Point);
410 extern void     drawop(Image*, Rectangle, Image*, Image*, Point, Drawop);
411 extern void     gendraw(Image*, Rectangle, Image*, Point, Image*, Point);
412 extern void     gendrawop(Image*, Rectangle, Image*, Point, Image*, Point, Drawop);
413 extern void     line(Image*, Point, Point, int, int, int, Image*, Point);
414 extern void     lineop(Image*, Point, Point, int, int, int, Image*, Point, Drawop);
415 extern void     poly(Image*, Point*, int, int, int, int, Image*, Point);
416 extern void     polyop(Image*, Point*, int, int, int, int, Image*, Point, Drawop);
417 extern void     fillpoly(Image*, Point*, int, int, Image*, Point);
418 extern void     fillpolyop(Image*, Point*, int, int, Image*, Point, Drawop);
419 extern Point    string(Image*, Point, Image*, Point, Font*, char*);
420 extern Point    stringop(Image*, Point, Image*, Point, Font*, char*, Drawop);
421 extern Point    stringn(Image*, Point, Image*, Point, Font*, char*, int);
422 extern Point    stringnop(Image*, Point, Image*, Point, Font*, char*, int, Drawop);
423 extern Point    runestring(Image*, Point, Image*, Point, Font*, Rune*);
424 extern Point    runestringop(Image*, Point, Image*, Point, Font*, Rune*, Drawop);
425 extern Point    runestringn(Image*, Point, Image*, Point, Font*, Rune*, int);
426 extern Point    runestringnop(Image*, Point, Image*, Point, Font*, Rune*, int, Drawop);
427 extern Point    stringbg(Image*, Point, Image*, Point, Font*, char*, Image*, Point);
428 extern Point    stringbgop(Image*, Point, Image*, Point, Font*, char*, Image*, Point, Drawop);
429 extern Point    stringnbg(Image*, Point, Image*, Point, Font*, char*, int, Image*, Point);
430 extern Point    stringnbgop(Image*, Point, Image*, Point, Font*, char*, int, Image*, Point, Drawop);
431 extern Point    runestringbg(Image*, Point, Image*, Point, Font*, Rune*, Image*, Point);
432 extern Point    runestringbgop(Image*, Point, Image*, Point, Font*, Rune*, Image*, Point, Drawop);
433 extern Point    runestringnbg(Image*, Point, Image*, Point, Font*, Rune*, int, Image*, Point);
434 extern Point    runestringnbgop(Image*, Point, Image*, Point, Font*, Rune*, int, Image*, Point, Drawop);
435 extern Point    _string(Image*, Point, Image*, Point, Font*, char*, Rune*, int, Rectangle, Image*, Point, Drawop);
436 extern Point    stringsubfont(Image*, Point, Image*, Subfont*, char*);
437 extern int              bezier(Image*, Point, Point, Point, Point, int, int, int, Image*, Point);
438 extern int              bezierop(Image*, Point, Point, Point, Point, int, int, int, Image*, Point, Drawop);
439 extern int              bezspline(Image*, Point*, int, int, int, int, Image*, Point);
440 extern int              bezsplineop(Image*, Point*, int, int, int, int, Image*, Point, Drawop);
441 extern int              bezsplinepts(Point*, int, Point**);
442 extern int              fillbezier(Image*, Point, Point, Point, Point, int, Image*, Point);
443 extern int              fillbezierop(Image*, Point, Point, Point, Point, int, Image*, Point, Drawop);
444 extern int              fillbezspline(Image*, Point*, int, int, Image*, Point);
445 extern int              fillbezsplineop(Image*, Point*, int, int, Image*, Point, Drawop);
446 extern void     ellipse(Image*, Point, int, int, int, Image*, Point);
447 extern void     ellipseop(Image*, Point, int, int, int, Image*, Point, Drawop);
448 extern void     fillellipse(Image*, Point, int, int, Image*, Point);
449 extern void     fillellipseop(Image*, Point, int, int, Image*, Point, Drawop);
450 extern void     arc(Image*, Point, int, int, int, Image*, Point, int, int);
451 extern void     arcop(Image*, Point, int, int, int, Image*, Point, int, int, Drawop);
452 extern void     fillarc(Image*, Point, int, int, Image*, Point, int, int);
453 extern void     fillarcop(Image*, Point, int, int, Image*, Point, int, int, Drawop);
454 extern void     border(Image*, Rectangle, int, Image*, Point);
455 extern void     borderop(Image*, Rectangle, int, Image*, Point, Drawop);
456
457 /*
458  * Font management
459  */
460 extern Font*    openfont(Display*, char*);
461 extern Font*    buildfont(Display*, char*, char*);
462 extern void     freefont(Font*);
463 extern Font*    mkfont(Subfont*, Rune);
464 extern int      cachechars(Font*, char**, Rune**, ushort*, int, int*, char**);
465 extern void     agefont(Font*);
466 extern Subfont* allocsubfont(char*, int, int, int, Fontchar*, Image*);
467 extern Subfont* lookupsubfont(Display*, char*);
468 extern void     installsubfont(char*, Subfont*);
469 extern void     uninstallsubfont(Subfont*);
470 extern void     freesubfont(Subfont*);
471 extern Subfont* readsubfont(Display*, char*, int, int);
472 extern Subfont* readsubfonti(Display*, char*, int, Image*, int);
473 extern int      writesubfont(int, Subfont*);
474 extern void     _unpackinfo(Fontchar*, uchar*, int);
475 extern Point    stringsize(Font*, char*);
476 extern int      stringwidth(Font*, char*);
477 extern int      stringnwidth(Font*, char*, int);
478 extern Point    runestringsize(Font*, Rune*);
479 extern int      runestringwidth(Font*, Rune*);
480 extern int      runestringnwidth(Font*, Rune*, int);
481 extern Point    strsubfontwidth(Subfont*, char*);
482 extern int      loadchar(Font*, Rune, Cacheinfo*, int, int, char**);
483 extern char*    subfontname(char*, char*, int);
484 extern Subfont* _getsubfont(Display*, char*);
485 extern Subfont* getdefont(Display*);
486 extern void             lockdisplay(Display*);
487 extern void     unlockdisplay(Display*);
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  void    _setdrawop(Display*, Drawop);
506
507 #define BGSHORT(p)      ((p)[0]|((p)[1]<<8))
508 #define BGLONG(p)       ((p)[0]|((p)[1]<<8)|((p)[2]<<16)|((p)[3]<<24))
509 #define BPSHORT(p,v)    do{ushort _v_=(v);(p)[0]=_v_;(p)[1]=_v_>>8;}while(0)
510 #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)
511
512 /*
513  * Compressed image file parameters and helper routines
514  */
515 #define NMATCH  3               /* shortest match possible */
516 #define NRUN    (NMATCH+31)     /* longest match possible */
517 #define NMEM    1024            /* window size */
518 #define NDUMP   128             /* maximum length of dump */
519 #define NCBLOCK 6000            /* size of compressed blocks */
520 extern  void    _twiddlecompressed(uchar*, int);
521 extern  int     _compblocksize(Rectangle, int);
522
523 extern  ulong   drawld2chan[];
524 extern  void    drawsetdebug(int);