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