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