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