Blob


1 #ifndef _DRAW_H_
2 #define _DRAW_H_ 1
3 #if defined(__cplusplus)
4 extern "C" {
5 #endif
7 AUTOLIB(draw)
8 #ifdef __APPLE__
9 AUTOFRAMEWORK(Carbon)
10 #endif
12 typedef struct Cachefont Cachefont;
13 typedef struct Cacheinfo Cacheinfo;
14 typedef struct Cachesubf Cachesubf;
15 typedef struct Display Display;
16 typedef struct Font Font;
17 typedef struct Fontchar Fontchar;
18 typedef struct Image Image;
19 typedef struct Mouse Mouse;
20 typedef struct Point Point;
21 typedef struct Rectangle Rectangle;
22 typedef struct RGB RGB;
23 typedef struct Screen Screen;
24 typedef struct Subfont Subfont;
26 extern int Rfmt(Fmt*);
27 extern int Pfmt(Fmt*);
29 #define DOpaque 0xFFFFFFFF
30 #define DTransparent 0x00000000 /* only useful for allocimage memfillcolor */
31 #define DBlack 0x000000FF
32 #define DWhite 0xFFFFFFFF
33 #define DRed 0xFF0000FF
34 #define DGreen 0x00FF00FF
35 #define DBlue 0x0000FFFF
36 #define DCyan 0x00FFFFFF
37 #define DMagenta 0xFF00FFFF
38 #define DYellow 0xFFFF00FF
39 #define DPaleyellow 0xFFFFAAFF
40 #define DDarkyellow 0xEEEE9EFF
41 #define DDarkgreen 0x448844FF
42 #define DPalegreen 0xAAFFAAFF
43 #define DMedgreen 0x88CC88FF
44 #define DDarkblue 0x000055FF
45 #define DPalebluegreen 0xAAFFFFFF
46 #define DPaleblue 0x0000BBFF
47 #define DBluegreen 0x008888FF
48 #define DGreygreen 0x55AAAAFF
49 #define DPalegreygreen 0x9EEEEEFF
50 #define DYellowgreen 0x99994CFF
51 #define DMedblue 0x000099FF
52 #define DGreyblue 0x005DBBFF
53 #define DPalegreyblue 0x4993DDFF
54 #define DPurpleblue 0x8888CCFF
56 #define DNotacolor 0xFFFFFF00
57 #define DNofill DNotacolor
59 enum
60 {
61 Displaybufsize = 8000,
62 ICOSSCALE = 1024,
63 Borderwidth = 4
64 };
66 enum
67 {
68 /* refresh methods */
69 Refbackup = 0,
70 Refnone = 1,
71 Refmesg = 2
72 };
73 #define NOREFRESH ((void*)-1)
75 enum
76 {
77 /* line ends */
78 Endsquare = 0,
79 Enddisc = 1,
80 Endarrow = 2,
81 Endmask = 0x1F
82 };
84 #define ARROW(a, b, c) (Endarrow|((a)<<5)|((b)<<14)|((c)<<23))
86 typedef enum
87 {
88 /* Porter-Duff compositing operators */
89 Clear = 0,
91 SinD = 8,
92 DinS = 4,
93 SoutD = 2,
94 DoutS = 1,
96 S = SinD|SoutD,
97 SoverD = SinD|SoutD|DoutS,
98 SatopD = SinD|DoutS,
99 SxorD = SoutD|DoutS,
101 D = DinS|DoutS,
102 DoverS = DinS|DoutS|SoutD,
103 DatopS = DinS|SoutD,
104 DxorS = DoutS|SoutD, /* == SxorD */
106 Ncomp = 12
107 } Drawop;
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 };
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)))
129 #define NBITS(c) ((c)&15)
130 #define TYPE(c) (((c)>>4)&15)
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 BGR24 = CHAN3(CBlue, 8, CGreen, 8, CRed, 8),
142 RGBA32 = CHAN4(CRed, 8, CGreen, 8, CBlue, 8, CAlpha, 8),
143 ARGB32 = CHAN4(CAlpha, 8, CRed, 8, CGreen, 8, CBlue, 8), /* stupid VGAs */
144 ABGR32 = CHAN4(CAlpha, 8, CBlue, 8, CGreen, 8, CRed, 8),
145 XRGB32 = CHAN4(CIgnore, 8, CRed, 8, CGreen, 8, CBlue, 8),
146 XBGR32 = CHAN4(CIgnore, 8, CBlue, 8, CGreen, 8, CRed, 8)
147 };
149 extern char* chantostr(char*, u32int);
150 extern u32int strtochan(char*);
151 extern int chantodepth(u32int);
153 struct Point
155 int x;
156 int y;
157 };
159 struct Rectangle
161 Point min;
162 Point max;
163 };
165 typedef void (*Reffn)(Image*, Rectangle, void*);
167 struct Screen
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 };
175 struct Display
177 QLock qlock;
178 int locking; /*program is using lockdisplay */
179 int dirno;
180 int imageid;
181 int local;
182 void (*error)(Display*, char*);
183 char *devdir;
184 char *windir;
185 char oldlabel[64];
186 u32int dataqid;
187 Image *image;
188 Image *white;
189 Image *black;
190 Image *opaque;
191 Image *transparent;
192 uchar *buf;
193 int bufsize;
194 uchar *bufp;
195 uchar *obuf;
196 int obufsize;
197 uchar *obufp;
198 Font *defaultfont;
199 Subfont *defaultsubfont;
200 Image *windows;
201 Image *screenimage;
202 int _isnewdisplay;
203 };
205 struct Image
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 u32int 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 };
218 struct RGB
220 u32int red;
221 u32int green;
222 u32int blue;
223 };
225 /*
226 * Subfonts
228 * given char c, Subfont *f, Fontchar *i, and Point p, one says
229 * i = f->info+c;
230 * void(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 */
237 struct Fontchar
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 };
246 struct Subfont
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 };
257 enum
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 };
274 struct Cachefont
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 };
283 struct Cacheinfo
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 };
292 struct Cachesubf
294 u32int age; /* for replacement */
295 Cachefont *cf; /* font info that owns us */
296 Subfont *f; /* attached subfont */
297 };
299 struct Font
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 u32int 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 };
317 #define Dx(r) ((r).max.x-(r).min.x)
318 #define Dy(r) ((r).max.y-(r).min.y)
320 /*
321 * Image management
322 */
323 extern Image* _allocimage(Image*, Display*, Rectangle, u32int, int, u32int, int, int);
324 extern Image* allocimage(Display*, Rectangle, u32int, int, u32int);
325 extern uchar* bufimage(Display*, int);
326 extern int bytesperline(Rectangle, int);
327 extern void closedisplay(Display*);
328 extern void drawerror(Display*, char*);
329 extern int flushimage(Display*, int);
330 extern int freeimage(Image*);
331 extern int _freeimage1(Image*);
332 extern int geninitdraw(char*, void(*)(Display*, char*), char*, char*, char*, int);
333 extern int initdraw(void(*)(Display*, char*), char*, char*);
334 extern int newwindow(char*);
335 extern int loadimage(Image*, Rectangle, uchar*, int);
336 extern int cloadimage(Image*, Rectangle, uchar*, int);
337 extern int getwindow(Display*, int);
338 extern int gengetwindow(Display*, char*, Image**, Screen**, int);
339 extern Image* readimage(Display*, int, int);
340 extern Image* creadimage(Display*, int, int);
341 extern int unloadimage(Image*, Rectangle, uchar*, int);
342 extern int wordsperline(Rectangle, int);
343 extern int writeimage(int, Image*, int);
344 extern Image* namedimage(Display*, char*);
345 extern int nameimage(Image*, char*, int);
346 extern Image* allocimagemix(Display*, u32int, u32int);
347 extern int drawsetlabel(char*);
349 /*
350 * Colors
351 */
352 extern void readcolmap(Display*, RGB*);
353 extern void writecolmap(Display*, RGB*);
354 extern u32int setalpha(u32int, uchar);
356 /*
357 * Windows
358 */
359 extern Screen* allocscreen(Image*, Image*, int);
360 extern Image* _allocwindow(Image*, Screen*, Rectangle, int, u32int);
361 extern Image* allocwindow(Screen*, Rectangle, int, u32int);
362 extern void bottomnwindows(Image**, int);
363 extern void bottomwindow(Image*);
364 extern int freescreen(Screen*);
365 extern Screen* publicscreen(Display*, int, u32int);
366 extern void topnwindows(Image**, int);
367 extern void topwindow(Image*);
368 extern int originwindow(Image*, Point, Point);
370 /*
371 * Geometry
372 */
373 extern Point Pt(int, int);
374 extern Rectangle Rect(int, int, int, int);
375 extern Rectangle Rpt(Point, Point);
376 extern Point addpt(Point, Point);
377 extern Point subpt(Point, Point);
378 extern Point divpt(Point, int);
379 extern Point mulpt(Point, int);
380 extern int eqpt(Point, Point);
381 extern int eqrect(Rectangle, Rectangle);
382 extern Rectangle insetrect(Rectangle, int);
383 extern Rectangle rectaddpt(Rectangle, Point);
384 extern Rectangle rectsubpt(Rectangle, Point);
385 extern Rectangle canonrect(Rectangle);
386 extern int rectXrect(Rectangle, Rectangle);
387 extern int rectinrect(Rectangle, Rectangle);
388 extern void combinerect(Rectangle*, Rectangle);
389 extern int rectclip(Rectangle*, Rectangle);
390 extern int ptinrect(Point, Rectangle);
391 extern void replclipr(Image*, int, Rectangle);
392 extern int drawreplxy(int, int, int); /* used to be drawsetxy */
393 extern Point drawrepl(Rectangle, Point);
394 extern int rgb2cmap(int, int, int);
395 extern int cmap2rgb(int);
396 extern int cmap2rgba(int);
397 extern void icossin(int, int*, int*);
398 extern void icossin2(int, int, int*, int*);
400 /*
401 * Graphics
402 */
403 extern void draw(Image*, Rectangle, Image*, Image*, Point);
404 extern void drawop(Image*, Rectangle, Image*, Image*, Point, Drawop);
405 extern void gendraw(Image*, Rectangle, Image*, Point, Image*, Point);
406 extern void gendrawop(Image*, Rectangle, Image*, Point, Image*, Point, Drawop);
407 extern void line(Image*, Point, Point, int, int, int, Image*, Point);
408 extern void lineop(Image*, Point, Point, int, int, int, Image*, Point, Drawop);
409 extern void poly(Image*, Point*, int, int, int, int, Image*, Point);
410 extern void polyop(Image*, Point*, int, int, int, int, Image*, Point, Drawop);
411 extern void fillpoly(Image*, Point*, int, int, Image*, Point);
412 extern void fillpolyop(Image*, Point*, int, int, Image*, Point, Drawop);
413 extern Point string(Image*, Point, Image*, Point, Font*, char*);
414 extern Point stringop(Image*, Point, Image*, Point, Font*, char*, Drawop);
415 extern Point stringn(Image*, Point, Image*, Point, Font*, char*, int);
416 extern Point stringnop(Image*, Point, Image*, Point, Font*, char*, int, Drawop);
417 extern Point runestring(Image*, Point, Image*, Point, Font*, Rune*);
418 extern Point runestringop(Image*, Point, Image*, Point, Font*, Rune*, Drawop);
419 extern Point runestringn(Image*, Point, Image*, Point, Font*, Rune*, int);
420 extern Point runestringnop(Image*, Point, Image*, Point, Font*, Rune*, int, Drawop);
421 extern Point stringbg(Image*, Point, Image*, Point, Font*, char*, Image*, Point);
422 extern Point stringbgop(Image*, Point, Image*, Point, Font*, char*, Image*, Point, Drawop);
423 extern Point stringnbg(Image*, Point, Image*, Point, Font*, char*, int, Image*, Point);
424 extern Point stringnbgop(Image*, Point, Image*, Point, Font*, char*, int, Image*, Point, Drawop);
425 extern Point runestringbg(Image*, Point, Image*, Point, Font*, Rune*, Image*, Point);
426 extern Point runestringbgop(Image*, Point, Image*, Point, Font*, Rune*, Image*, Point, Drawop);
427 extern Point runestringnbg(Image*, Point, Image*, Point, Font*, Rune*, int, Image*, Point);
428 extern Point runestringnbgop(Image*, Point, Image*, Point, Font*, Rune*, int, Image*, Point, Drawop);
429 extern Point _string(Image*, Point, Image*, Point, Font*, char*, Rune*, int, Rectangle, Image*, Point, Drawop);
430 extern Point stringsubfont(Image*, Point, Image*, Subfont*, char*);
431 extern int bezier(Image*, Point, Point, Point, Point, int, int, int, Image*, Point);
432 extern int bezierop(Image*, Point, Point, Point, Point, int, int, int, Image*, Point, Drawop);
433 extern int bezspline(Image*, Point*, int, int, int, int, Image*, Point);
434 extern int bezsplineop(Image*, Point*, int, int, int, int, Image*, Point, Drawop);
435 extern int bezsplinepts(Point*, int, Point**);
436 extern int fillbezier(Image*, Point, Point, Point, Point, int, Image*, Point);
437 extern int fillbezierop(Image*, Point, Point, Point, Point, int, Image*, Point, Drawop);
438 extern int fillbezspline(Image*, Point*, int, int, Image*, Point);
439 extern int fillbezsplineop(Image*, Point*, int, int, Image*, Point, Drawop);
440 extern void ellipse(Image*, Point, int, int, int, Image*, Point);
441 extern void ellipseop(Image*, Point, int, int, int, Image*, Point, Drawop);
442 extern void fillellipse(Image*, Point, int, int, Image*, Point);
443 extern void fillellipseop(Image*, Point, int, int, Image*, Point, Drawop);
444 extern void arc(Image*, Point, int, int, int, Image*, Point, int, int);
445 extern void arcop(Image*, Point, int, int, int, Image*, Point, int, int, Drawop);
446 extern void fillarc(Image*, Point, int, int, Image*, Point, int, int);
447 extern void fillarcop(Image*, Point, int, int, Image*, Point, int, int, Drawop);
448 extern void border(Image*, Rectangle, int, Image*, Point);
449 extern void borderop(Image*, Rectangle, int, Image*, Point, Drawop);
451 /*
452 * Font management
453 */
454 extern Font* openfont(Display*, char*);
455 extern Font* buildfont(Display*, char*, char*);
456 extern void freefont(Font*);
457 extern Font* mkfont(Subfont*, Rune);
458 extern int cachechars(Font*, char**, Rune**, ushort*, int, int*, char**);
459 extern void agefont(Font*);
460 extern Subfont* allocsubfont(char*, int, int, int, Fontchar*, Image*);
461 extern Subfont* lookupsubfont(Display*, char*);
462 extern void installsubfont(char*, Subfont*);
463 extern void uninstallsubfont(Subfont*);
464 extern void freesubfont(Subfont*);
465 extern Subfont* readsubfont(Display*, char*, int, int);
466 extern Subfont* readsubfonti(Display*, char*, int, Image*, int);
467 extern int writesubfont(int, Subfont*);
468 extern void _unpackinfo(Fontchar*, uchar*, int);
469 extern Point stringsize(Font*, char*);
470 extern int stringwidth(Font*, char*);
471 extern int stringnwidth(Font*, char*, int);
472 extern Point runestringsize(Font*, Rune*);
473 extern int runestringwidth(Font*, Rune*);
474 extern int runestringnwidth(Font*, Rune*, int);
475 extern Point strsubfontwidth(Subfont*, char*);
476 extern int loadchar(Font*, Rune, Cacheinfo*, int, int, char**);
477 extern char* subfontname(char*, char*, int);
478 extern Subfont* _getsubfont(Display*, char*);
479 extern Subfont* getdefont(Display*);
480 extern void lockdisplay(Display*);
481 extern void unlockdisplay(Display*);
482 extern int drawlsetrefresh(u32int, int, void*, void*);
484 /*
485 * Predefined
486 */
487 extern uchar defontdata[];
488 extern int sizeofdefont;
489 extern Point ZP;
490 extern Rectangle ZR;
492 /*
493 * Set up by initdraw()
494 */
495 extern Display *display;
496 extern Font *font;
497 extern Image *screen;
498 extern Screen *_screen;
499 extern int _cursorfd;
500 extern int _drawdebug; /* set to 1 to see errors from flushimage */
501 extern void _setdrawop(Display*, Drawop);
502 extern Display *_initdisplay(void(*)(Display*,char*), char*);
504 #define BGSHORT(p) (((p)[0]<<0) | ((p)[1]<<8))
505 #define BGLONG(p) ((BGSHORT(p)<<0) | (BGSHORT(p+2)<<16))
506 #define BPSHORT(p, v) ((p)[0]=(v), (p)[1]=((v)>>8))
507 #define BPLONG(p, v) (BPSHORT(p, (v)), BPSHORT(p+2, (v)>>16))
509 /*
510 * Compressed image file parameters and helper routines
511 */
512 #define NMATCH 3 /* shortest match possible */
513 #define NRUN (NMATCH+31) /* longest match possible */
514 #define NMEM 1024 /* window size */
515 #define NDUMP 128 /* maximum length of dump */
516 #define NCBLOCK 6000 /* size of compressed blocks */
517 extern void _twiddlecompressed(uchar*, int);
518 extern int _compblocksize(Rectangle, int);
520 /* XXX backwards helps; should go */
521 extern u32int drawld2chan[];
522 extern void drawsetdebug(int);
524 /*
525 * Snarf buffer
526 */
527 enum
529 SnarfSize = 64*1024
530 };
531 char *getsnarf(void);
532 void putsnarf(char*);
534 void drawtopwindow(void);
535 void drawresizewindow(Rectangle);
536 extern char *winsize;
538 /*
539 * Port magic.
540 */
541 int _drawmsgread(Display*, void*, int);
542 int _drawmsgwrite(Display*, void*, int);
543 int _latin1(Rune*, int);
545 int mousescrollsize(int);
547 #if defined(__cplusplus)
549 #endif
550 #endif