diff options
author | Connor Lane Smith <cls@lubutu.com> | 2010-06-30 01:36:15 +0100 |
---|---|---|
committer | Connor Lane Smith <cls@lubutu.com> | 2010-06-30 01:36:15 +0100 |
commit | 26b9f3de0ffe471e5335b07454147457e373c037 (patch) | |
tree | 5480fedbc318d490a4e90c2104d6ba8c26258698 /draw | |
parent | 7afd296c40a2e8af9477add1c3d02a7c4b444747 (diff) |
libdraw now has own repo
Diffstat (limited to 'draw')
-rw-r--r-- | draw/Makefile | 26 | ||||
-rw-r--r-- | draw/cleanupdraw.c | 13 | ||||
-rw-r--r-- | draw/draw.h | 34 | ||||
-rw-r--r-- | draw/drawsquare.c | 19 | ||||
-rw-r--r-- | draw/drawtext.c | 34 | ||||
-rw-r--r-- | draw/eprint.c | 18 | ||||
-rw-r--r-- | draw/getcolor.c | 13 | ||||
-rw-r--r-- | draw/initfont.c | 36 | ||||
-rw-r--r-- | draw/setupdraw.c | 16 | ||||
-rw-r--r-- | draw/textnw.c | 14 | ||||
-rw-r--r-- | draw/textw.c | 9 |
11 files changed, 0 insertions, 232 deletions
diff --git a/draw/Makefile b/draw/Makefile deleted file mode 100644 index 4b39490..0000000 --- a/draw/Makefile +++ /dev/null | |||
@@ -1,26 +0,0 @@ | |||
1 | # libdraw - dynamic drawing library | ||
2 | # See LICENSE file for copyright and license details. | ||
3 | |||
4 | include ../config.mk | ||
5 | |||
6 | SRC = cleanupdraw.c drawsquare.c drawtext.c eprint.c getcolor.c initfont.c \ | ||
7 | setupdraw.c textnw.c textw.c | ||
8 | OBJ = ${SRC:.c=.o} | ||
9 | |||
10 | all: libdraw.a | ||
11 | |||
12 | .c.o: | ||
13 | @echo CC $< | ||
14 | @${CC} -c ${CFLAGS} $< | ||
15 | |||
16 | ${OBJ}: ../config.mk draw.h | ||
17 | |||
18 | libdraw.a: ${OBJ} | ||
19 | @echo AR $@ | ||
20 | @ar cr $@ $+ | ||
21 | |||
22 | clean: | ||
23 | @echo cleaning libdraw | ||
24 | @rm -f libdraw.a ${OBJ} | ||
25 | |||
26 | .PHONY: all options clean | ||
diff --git a/draw/cleanupdraw.c b/draw/cleanupdraw.c deleted file mode 100644 index 28bce13..0000000 --- a/draw/cleanupdraw.c +++ /dev/null | |||
@@ -1,13 +0,0 @@ | |||
1 | /* See LICENSE file for copyright and license details. */ | ||
2 | #include <X11/Xlib.h> | ||
3 | #include "draw.h" | ||
4 | |||
5 | void | ||
6 | cleanupdraw(DC *dc) { | ||
7 | if(dc->font.set) | ||
8 | XFreeFontSet(dc->dpy, dc->font.set); | ||
9 | else | ||
10 | XFreeFont(dc->dpy, dc->font.xfont); | ||
11 | XFreePixmap(dc->dpy, dc->drawable); | ||
12 | XFreeGC(dc->dpy, dc->gc); | ||
13 | } | ||
diff --git a/draw/draw.h b/draw/draw.h deleted file mode 100644 index f282392..0000000 --- a/draw/draw.h +++ /dev/null | |||
@@ -1,34 +0,0 @@ | |||
1 | /* See LICENSE file for copyright and license details. */ | ||
2 | #include <X11/Xlib.h> | ||
3 | |||
4 | /* enums */ | ||
5 | enum { ColBorder, ColFG, ColBG, ColLast }; | ||
6 | |||
7 | /* typedefs */ | ||
8 | typedef struct { | ||
9 | int x, y, w, h; | ||
10 | Drawable drawable; | ||
11 | Display *dpy; | ||
12 | GC gc; | ||
13 | struct { | ||
14 | XFontStruct *xfont; | ||
15 | XFontSet set; | ||
16 | int ascent; | ||
17 | int descent; | ||
18 | int height; | ||
19 | } font; | ||
20 | } DC; /* draw context */ | ||
21 | |||
22 | /* forward declarations */ | ||
23 | void cleanupdraw(DC *dc); | ||
24 | void drawsquare(DC *dc, Bool filled, unsigned long col[ColLast], Bool invert); | ||
25 | void drawtext(DC *dc, const char *text, unsigned long col[ColLast], Bool invert); | ||
26 | void eprint(const char *fmt, ...); | ||
27 | unsigned long getcolor(DC *dc, const char *colstr); | ||
28 | void initfont(DC *dc, const char *fontstr); | ||
29 | void setupdraw(DC *dc, Window w); | ||
30 | int textnw(DC *dc, const char *text, unsigned int len); | ||
31 | int textw(DC *dc, const char *text); | ||
32 | |||
33 | /* variables */ | ||
34 | extern const char *progname; | ||
diff --git a/draw/drawsquare.c b/draw/drawsquare.c deleted file mode 100644 index 8899043..0000000 --- a/draw/drawsquare.c +++ /dev/null | |||
@@ -1,19 +0,0 @@ | |||
1 | /* See LICENSE file for copyright and license details. */ | ||
2 | #include <X11/Xlib.h> | ||
3 | #include "draw.h" | ||
4 | |||
5 | void | ||
6 | drawsquare(DC *dc, Bool filled, unsigned long col[ColLast], Bool invert) { | ||
7 | int n; | ||
8 | XRectangle r = { dc->x, dc->y, dc->w, dc->h }; | ||
9 | |||
10 | XSetForeground(dc->dpy, dc->gc, col[invert ? ColBG : ColFG]); | ||
11 | n = ((dc->font.ascent + dc->font.descent + 2) / 4) + (filled ? 1 : 0); | ||
12 | r.width = r.height = n; | ||
13 | r.x = dc->x + 1; | ||
14 | r.y = dc->y + 1; | ||
15 | if(filled) | ||
16 | XFillRectangles(dc->dpy, dc->drawable, dc->gc, &r, 1); | ||
17 | else | ||
18 | XDrawRectangles(dc->dpy, dc->drawable, dc->gc, &r, 1); | ||
19 | } | ||
diff --git a/draw/drawtext.c b/draw/drawtext.c deleted file mode 100644 index d347b36..0000000 --- a/draw/drawtext.c +++ /dev/null | |||
@@ -1,34 +0,0 @@ | |||
1 | /* See LICENSE file for copyright and license details. */ | ||
2 | #include <string.h> | ||
3 | #include <X11/Xlib.h> | ||
4 | #include "draw.h" | ||
5 | |||
6 | #define MIN(a, b) ((a) < (b) ? (a) : (b)) | ||
7 | |||
8 | void | ||
9 | drawtext(DC *dc, const char *text, unsigned long col[ColLast], Bool invert) { | ||
10 | char buf[256]; | ||
11 | int i, x, y, h, len, olen; | ||
12 | XRectangle r = { dc->x, dc->y, dc->w, dc->h }; | ||
13 | |||
14 | XSetForeground(dc->dpy, dc->gc, col[invert ? ColFG : ColBG]); | ||
15 | XFillRectangles(dc->dpy, dc->drawable, dc->gc, &r, 1); | ||
16 | if(!text) | ||
17 | return; | ||
18 | olen = strlen(text); | ||
19 | h = dc->font.height; | ||
20 | y = dc->y + ((h+2) / 2) - (h / 2) + dc->font.ascent; | ||
21 | x = dc->x + (h / 2); | ||
22 | /* shorten text if necessary */ | ||
23 | for(len = MIN(olen, sizeof buf); len && textnw(dc, text, len) > dc->w - h; len--); | ||
24 | if(!len) | ||
25 | return; | ||
26 | memcpy(buf, text, len); | ||
27 | if(len < olen) | ||
28 | for(i = len; i && i > len - 3; buf[--i] = '.'); | ||
29 | XSetForeground(dc->dpy, dc->gc, col[invert ? ColBG : ColFG]); | ||
30 | if(dc->font.set) | ||
31 | XmbDrawString(dc->dpy, dc->drawable, dc->font.set, dc->gc, x, y, buf, len); | ||
32 | else | ||
33 | XDrawString(dc->dpy, dc->drawable, dc->gc, x, y, buf, len); | ||
34 | } | ||
diff --git a/draw/eprint.c b/draw/eprint.c deleted file mode 100644 index 3094b61..0000000 --- a/draw/eprint.c +++ /dev/null | |||
@@ -1,18 +0,0 @@ | |||
1 | /* See LICENSE file for copyright and license details. */ | ||
2 | #include <stdarg.h> | ||
3 | #include <stdio.h> | ||
4 | #include <stdlib.h> | ||
5 | #include "draw.h" | ||
6 | |||
7 | const char *progname; | ||
8 | |||
9 | void | ||
10 | eprint(const char *fmt, ...) { | ||
11 | va_list ap; | ||
12 | |||
13 | fprintf(stderr, "%s: ", progname); | ||
14 | va_start(ap, fmt); | ||
15 | vfprintf(stderr, fmt, ap); | ||
16 | va_end(ap); | ||
17 | exit(EXIT_FAILURE); | ||
18 | } | ||
diff --git a/draw/getcolor.c b/draw/getcolor.c deleted file mode 100644 index c0e5d21..0000000 --- a/draw/getcolor.c +++ /dev/null | |||
@@ -1,13 +0,0 @@ | |||
1 | /* See LICENSE file for copyright and license details. */ | ||
2 | #include <X11/Xlib.h> | ||
3 | #include "draw.h" | ||
4 | |||
5 | unsigned long | ||
6 | getcolor(DC *dc, const char *colstr) { | ||
7 | Colormap cmap = DefaultColormap(dc->dpy, DefaultScreen(dc->dpy)); | ||
8 | XColor color; | ||
9 | |||
10 | if(!XAllocNamedColor(dc->dpy, cmap, colstr, &color, &color)) | ||
11 | eprint("cannot allocate color '%s'\n", colstr); | ||
12 | return color.pixel; | ||
13 | } | ||
diff --git a/draw/initfont.c b/draw/initfont.c deleted file mode 100644 index 77d3182..0000000 --- a/draw/initfont.c +++ /dev/null | |||
@@ -1,36 +0,0 @@ | |||
1 | /* See LICENSE file for copyright and license details. */ | ||
2 | #include <X11/Xlib.h> | ||
3 | #include "draw.h" | ||
4 | |||
5 | #define MAX(a, b) ((a) > (b) ? (a) : (b)) | ||
6 | |||
7 | void | ||
8 | initfont(DC *dc, const char *fontstr) { | ||
9 | char *def, **missing = NULL; | ||
10 | int i, n; | ||
11 | |||
12 | if(!fontstr || !*fontstr) | ||
13 | eprint("cannot load null font\n"); | ||
14 | dc->font.set = XCreateFontSet(dc->dpy, fontstr, &missing, &n, &def); | ||
15 | if(missing) | ||
16 | XFreeStringList(missing); | ||
17 | if(dc->font.set) { | ||
18 | XFontStruct **xfonts; | ||
19 | char **font_names; | ||
20 | dc->font.ascent = dc->font.descent = 0; | ||
21 | n = XFontsOfFontSet(dc->font.set, &xfonts, &font_names); | ||
22 | for(i = 0; i < n; i++) { | ||
23 | dc->font.ascent = MAX(dc->font.ascent, (*xfonts)->ascent); | ||
24 | dc->font.descent = MAX(dc->font.descent, (*xfonts)->descent); | ||
25 | xfonts++; | ||
26 | } | ||
27 | } | ||
28 | else { | ||
29 | if(!(dc->font.xfont = XLoadQueryFont(dc->dpy, fontstr)) | ||
30 | && !(dc->font.xfont = XLoadQueryFont(dc->dpy, "fixed"))) | ||
31 | eprint("cannot load font '%s'\n", fontstr); | ||
32 | dc->font.ascent = dc->font.xfont->ascent; | ||
33 | dc->font.descent = dc->font.xfont->descent; | ||
34 | } | ||
35 | dc->font.height = dc->font.ascent + dc->font.descent; | ||
36 | } | ||
diff --git a/draw/setupdraw.c b/draw/setupdraw.c deleted file mode 100644 index 7dd5012..0000000 --- a/draw/setupdraw.c +++ /dev/null | |||
@@ -1,16 +0,0 @@ | |||
1 | /* See LICENSE file for copyright and license details. */ | ||
2 | #include <X11/Xlib.h> | ||
3 | #include "draw.h" | ||
4 | |||
5 | void | ||
6 | setupdraw(DC *dc, Window w) { | ||
7 | XWindowAttributes wa; | ||
8 | |||
9 | XGetWindowAttributes(dc->dpy, w, &wa); | ||
10 | dc->drawable = XCreatePixmap(dc->dpy, w, wa.width, wa.height, | ||
11 | DefaultDepth(dc->dpy, DefaultScreen(dc->dpy))); | ||
12 | dc->gc = XCreateGC(dc->dpy, w, 0, NULL); | ||
13 | XSetLineAttributes(dc->dpy, dc->gc, 1, LineSolid, CapButt, JoinMiter); | ||
14 | if(!dc->font.set) | ||
15 | XSetFont(dc->dpy, dc->gc, dc->font.xfont->fid); | ||
16 | } | ||
diff --git a/draw/textnw.c b/draw/textnw.c deleted file mode 100644 index 9c0c122..0000000 --- a/draw/textnw.c +++ /dev/null | |||
@@ -1,14 +0,0 @@ | |||
1 | /* See LICENSE file for copyright and license details. */ | ||
2 | #include <X11/Xlib.h> | ||
3 | #include "draw.h" | ||
4 | |||
5 | int | ||
6 | textnw(DC *dc, const char *text, unsigned int len) { | ||
7 | XRectangle r; | ||
8 | |||
9 | if(dc->font.set) { | ||
10 | XmbTextExtents(dc->font.set, text, len, NULL, &r); | ||
11 | return r.width; | ||
12 | } | ||
13 | return XTextWidth(dc->font.xfont, text, len); | ||
14 | } | ||
diff --git a/draw/textw.c b/draw/textw.c deleted file mode 100644 index a8407f6..0000000 --- a/draw/textw.c +++ /dev/null | |||
@@ -1,9 +0,0 @@ | |||
1 | /* See LICENSE file for copyright and license details. */ | ||
2 | #include <string.h> | ||
3 | #include <X11/Xlib.h> | ||
4 | #include "draw.h" | ||
5 | |||
6 | int | ||
7 | textw(DC *dc, const char *text) { | ||
8 | return textnw(dc, text, strlen(text)) + dc->font.height; | ||
9 | } | ||