+ exit(EXIT_FAILURE);
+}
+
+void
+freedc(DC *dc) {
+ if(dc->font.set)
+ XFreeFontSet(dc->dpy, dc->font.set);
+ if(dc->font.xfont)
+ XFreeFont(dc->dpy, dc->font.xfont);
+ if(dc->canvas)
+ XFreePixmap(dc->dpy, dc->canvas);
+ XFreeGC(dc->dpy, dc->gc);
+ XCloseDisplay(dc->dpy);
+ free(dc);
+}
+
+unsigned long
+getcolor(DC *dc, const char *colstr) {
+ Colormap cmap = DefaultColormap(dc->dpy, DefaultScreen(dc->dpy));
+ XColor color;
+
+ if(!XAllocNamedColor(dc->dpy, cmap, colstr, &color, &color))
+ eprintf("cannot allocate color '%s'\n", colstr);
+ return color.pixel;
+}
+
+DC *
+initdc(void) {
+ DC *dc;
+
+ if(!setlocale(LC_CTYPE, "") || !XSupportsLocale())
+ fprintf(stderr, "no locale support\n");
+ if(!(dc = calloc(1, sizeof *dc)))
+ eprintf("cannot malloc %u bytes:", sizeof *dc);
+ if(!(dc->dpy = XOpenDisplay(NULL)))
+ eprintf("cannot open display\n");
+
+ dc->gc = XCreateGC(dc->dpy, DefaultRootWindow(dc->dpy), 0, NULL);
+ XSetLineAttributes(dc->dpy, dc->gc, 1, LineSolid, CapButt, JoinMiter);
+ return dc;
+}
+
+void
+initfont(DC *dc, const char *fontstr) {
+ if(!loadfont(dc, fontstr ? fontstr : DEFFONT)) {
+ if(fontstr != NULL)
+ fprintf(stderr, "cannot load font '%s'\n", fontstr);
+ if(fontstr == NULL || !loadfont(dc, DEFFONT))
+ eprintf("cannot load font '%s'\n", DEFFONT);
+ }
+ dc->font.height = dc->font.ascent + dc->font.descent;
+}
+
+Bool
+loadfont(DC *dc, const char *fontstr) {
+ char *def, **missing;
+ int i, n;
+
+ if(!*fontstr)
+ return False;
+ if((dc->font.set = XCreateFontSet(dc->dpy, fontstr, &missing, &n, &def))) {
+ char **names;
+ XFontStruct **xfonts;
+
+ n = XFontsOfFontSet(dc->font.set, &xfonts, &names);
+ for(i = dc->font.ascent = dc->font.descent = 0; i < n; i++) {
+ dc->font.ascent = MAX(dc->font.ascent, xfonts[i]->ascent);
+ dc->font.descent = MAX(dc->font.descent, xfonts[i]->descent);
+ }