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