Blob


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