Blob


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