]> icculus.org git repositories - mikachu/openbox.git/blob - util/xftlsfonts.cc
make what is output optional
[mikachu/openbox.git] / util / xftlsfonts.cc
1 extern "C" {
2 #include <X11/Xlib.h>
3 #include <X11/Xft/Xft.h>
4 }
5
6 #include <iostream>
7 #include <string>
8 #include <vector>
9
10 const char *NAME = "xftlsfonts";
11 const char *VERSION = "1.0";
12
13 using std::string;
14 using std::cout;
15 using std::endl;
16
17 int main(int argc, char **argv) {
18   if (argc > 1)
19     for (int i = 1; i < argc; ++i)
20       if (string(argv[i]) == "-help" ||
21           string(argv[i]) == "--help" ||
22           string(argv[i]) == "-version" ||
23           string(argv[i]) == "--version") {
24         cout << NAME << " version " << VERSION << endl;
25         cout << "Copyright (c) 2002, Ben Jansens <ben@orodu.net>" << endl;
26         cout << endl;
27         cout << "Usage: " << argv[0] << " [options]" << endl;
28         cout << "    -style     Show possible styles for each font" << endl;
29         cout << "    -slant     Show the slant for each font" << endl;
30         cout << "    -weight    Show the weight for each font" << endl;
31         cout << "    -file      Show which files contain each font" << endl;
32         cout << endl;
33         return 1;
34       }
35
36   Display *display = XOpenDisplay(NULL);
37
38   XftObjectSet *obj = XftObjectSetCreate();
39   if (! obj) {
40     cout << "Failed to create an XftObjectSet\n";
41     exit(2);
42   }
43
44   XftObjectSetAdd(obj, XFT_FAMILY);
45
46   if (argc > 1)
47     for (int i = 1; i < argc; ++i) {
48       if (string(argv[i]) == "-style") XftObjectSetAdd(obj, XFT_STYLE);
49       else if (string(argv[i]) == "-file") XftObjectSetAdd(obj, XFT_FILE);
50       else if (string(argv[i]) == "-slant") XftObjectSetAdd(obj, XFT_SLANT);
51       else if (string(argv[i]) == "-weight") XftObjectSetAdd(obj, XFT_WEIGHT);
52     }
53
54   XftPattern *pat = XftPatternCreate();
55   if (! pat) {
56     cout << "Failed to create an XftPattern\n";
57     exit(2);
58   }
59
60   XftFontSet *set = XftListFontsPatternObjects(display, DefaultScreen(display),
61                                                pat, obj);
62   if (! set) {
63     cout << "Failed to find a matching XftFontSet\n";
64     exit(2);
65   }
66  
67   XFree(pat);
68   XFree(obj);
69
70   for (int i = 0; i < set->nfont; ++i) {
71     for (int e = 0; e < set->fonts[i]->num; ++e) {
72 //      if (string(set->fonts[i]->elts[e].object) != "family")
73 //        continue; // i just want font family names
74
75       if (e > 0)
76         cout << "  "; // indent after the first element
77       cout << set->fonts[i]->elts[e].object << ": ";
78
79       XftValueList *vallist = set->fonts[i]->elts[e].values;
80       bool f = true;
81       do {
82         if (f)
83           f = false;
84         else
85           cout << ", ";
86
87         XftValue val = vallist->value;
88         switch (val.type) {
89         case XftTypeVoid:
90           cout << "(void)";
91           break;
92
93         case XftTypeInteger:
94           cout << val.u.i;
95           break;
96
97         case XftTypeDouble:
98           cout << val.u.d;
99           break;
100
101         case XftTypeString:
102           cout << val.u.s;
103           break;
104
105         case XftTypeBool:
106           cout << val.u.b;
107           break;
108
109         case XftTypeMatrix:
110           cout << "xx(" << val.u.m->xx << ") ";
111           cout << "xy(" << val.u.m->xy << ") ";
112           cout << "yx(" << val.u.m->yx << ") ";
113           cout << "yy(" << val.u.m->yy << ")";
114           break;
115         }
116       } while ((vallist = vallist->next));
117       cout << endl;
118     }
119   }
120   
121   cout << endl << "Found " << set->nfont << " matches." << endl;
122
123   XFree(set);
124   
125   XCloseDisplay(display);
126   return 0;
127 }