summaryrefslogtreecommitdiffstats
path: root/util/gapcalc.c
blob: 4a4c3dbf227fedba936a06ded820d92690d2a304 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/*  gapcalc - calculate height of given font
 *  Copyright (C) 2007 by Robert Manea  <rob.manea@gmail.com>
 *
 *  Compile with: cc -lX11 -o gapcalc gapcalc.c
 */

#include<stdio.h>
#include<stdlib.h>
#include<stdarg.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>

void 
eprint(const char *errstr, ...) {
    va_list ap;

    va_start(ap, errstr);
    vfprintf(stderr, errstr, ap);
    va_end(ap);
    exit(EXIT_FAILURE);
}


int 
main(int argc, char *argv[]) {
    Display *dpy;    
    XFontStruct *xfont;
    XFontSet set;
    char *def, **missing;
    char *fontstr;
    int i, n, ascent, descent;

    if(argc < 2)
        eprint("Usage: gapcalc <font>\n");

    if(!(dpy = XOpenDisplay(0)))
        eprint("fatal: cannot open display\n");

    fontstr = argv[1];
    missing = NULL;

    set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
    if(missing)
        XFreeStringList(missing);
    if(set) {
        XFontSetExtents *font_extents;
        XFontStruct **xfonts;
        char **font_names;
        ascent = descent = 0;
        font_extents = XExtentsOfFontSet(set);
        n = XFontsOfFontSet(set, &xfonts, &font_names);
        for(i = 0, ascent = 0, descent = 0; i < n; i++) {
            if(ascent < (*xfonts)->ascent)
                ascent = (*xfonts)->ascent;
            if(descent < (*xfonts)->descent)
                descent = (*xfonts)->descent;
            xfonts++;
        }
    } else if(!set && (xfont = XLoadQueryFont(dpy, fontstr))) {
        ascent = xfont->ascent;
        descent = xfont->descent;
    } else 
        eprint("fatal: cannot find specified font\n");

    printf("%d\n", ascent + descent + 2);

  
  return EXIT_SUCCESS;
}