Blob


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