aboutsummaryrefslogtreecommitdiff
path: root/draw
diff options
context:
space:
mode:
authorConnor Lane Smith <cls@lubutu.com>2010-06-24 17:44:35 +0100
committerConnor Lane Smith <cls@lubutu.com>2010-06-24 17:44:35 +0100
commit4508fd2c4ee3171bdc1bffb7e53ecda8290292ef (patch)
treee86bbdfc4e1cc93fe5906ef204bf970d389c3df2 /draw
parent723361fa124aa666d637e3fc9f5df1210a9e02b4 (diff)
moved draw.c to libdraw.a
Diffstat (limited to 'draw')
-rw-r--r--draw/Makefile26
-rw-r--r--draw/cleanupdraw.c13
-rw-r--r--draw/draw.h33
-rw-r--r--draw/drawtext.c34
-rw-r--r--draw/eprint.c18
-rw-r--r--draw/getcolor.c13
-rw-r--r--draw/initfont.c36
-rw-r--r--draw/setupdraw.c16
-rw-r--r--draw/textnw.c14
-rw-r--r--draw/textw.c9
10 files changed, 212 insertions, 0 deletions
diff --git a/draw/Makefile b/draw/Makefile
new file mode 100644
index 0000000..1f72b61
--- /dev/null
+++ b/draw/Makefile
@@ -0,0 +1,26 @@
1# libdraw - dynamic drawing library
2# See LICENSE file for copyright and license details.
3
4include ../config.mk
5
6SRC = cleanupdraw.c setupdraw.c drawtext.c eprint.c getcolor.c initfont.c \
7textnw.c textw.c
8OBJ = ${SRC:.c=.o}
9
10all: libdraw.a
11
12.c.o:
13 @echo CC $<
14 @${CC} -c ${CFLAGS} $<
15
16${OBJ}: ../config.mk draw.h
17
18libdraw.a: ${OBJ}
19 @echo AR $@
20 @ar cr $@ $+
21
22clean:
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
new file mode 100644
index 0000000..28bce13
--- /dev/null
+++ b/draw/cleanupdraw.c
@@ -0,0 +1,13 @@
1/* See LICENSE file for copyright and license details. */
2#include <X11/Xlib.h>
3#include "draw.h"
4
5void
6cleanupdraw(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
new file mode 100644
index 0000000..4646a18
--- /dev/null
+++ b/draw/draw.h
@@ -0,0 +1,33 @@
1/* See LICENSE file for copyright and license details. */
2#include <X11/Xlib.h>
3
4/* enums */
5enum { ColFG, ColBG, ColLast };
6
7/* typedefs */
8typedef 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 */
23void cleanupdraw(DC *dc);
24void drawtext(DC *dc, const char *text, unsigned long col[ColLast]);
25void eprint(const char *fmt, ...);
26unsigned long getcolor(DC *dc, const char *colstr);
27void initfont(DC *dc, const char *fontstr);
28void setupdraw(DC *dc, Window w);
29int textnw(DC *dc, const char *text, unsigned int len);
30int textw(DC *dc, const char *text);
31
32/* variables */
33extern const char *progname;
diff --git a/draw/drawtext.c b/draw/drawtext.c
new file mode 100644
index 0000000..cf7b015
--- /dev/null
+++ b/draw/drawtext.c
@@ -0,0 +1,34 @@
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
8void
9drawtext(DC *dc, const char *text, unsigned long col[ColLast]) {
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[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[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
new file mode 100644
index 0000000..3094b61
--- /dev/null
+++ b/draw/eprint.c
@@ -0,0 +1,18 @@
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
7const char *progname;
8
9void
10eprint(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
new file mode 100644
index 0000000..c0e5d21
--- /dev/null
+++ b/draw/getcolor.c
@@ -0,0 +1,13 @@
1/* See LICENSE file for copyright and license details. */
2#include <X11/Xlib.h>
3#include "draw.h"
4
5unsigned long
6getcolor(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
new file mode 100644
index 0000000..77d3182
--- /dev/null
+++ b/draw/initfont.c
@@ -0,0 +1,36 @@
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
7void
8initfont(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
new file mode 100644
index 0000000..7dd5012
--- /dev/null
+++ b/draw/setupdraw.c
@@ -0,0 +1,16 @@
1/* See LICENSE file for copyright and license details. */
2#include <X11/Xlib.h>
3#include "draw.h"
4
5void
6setupdraw(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
new file mode 100644
index 0000000..9c0c122
--- /dev/null
+++ b/draw/textnw.c
@@ -0,0 +1,14 @@
1/* See LICENSE file for copyright and license details. */
2#include <X11/Xlib.h>
3#include "draw.h"
4
5int
6textnw(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
new file mode 100644
index 0000000..a8407f6
--- /dev/null
+++ b/draw/textw.c
@@ -0,0 +1,9 @@
1/* See LICENSE file for copyright and license details. */
2#include <string.h>
3#include <X11/Xlib.h>
4#include "draw.h"
5
6int
7textw(DC *dc, const char *text) {
8 return textnw(dc, text, strlen(text)) + dc->font.height;
9}