Xinqi Bao's Git
2 * (C)opyright MMIV-MMVI Anselm R. Garbe <garbeam at gmail dot com>
3 * See LICENSE file for license details.
8 #include <X11/Xlocale.h>
13 textnw(const char *text
, unsigned int len
) {
17 XmbTextExtents(dc
.font
.set
, text
, len
, NULL
, &r
);
20 return XTextWidth(dc
.font
.xfont
, text
, len
);
26 drawtext(const char *text
, unsigned long col
[ColLast
]) {
29 unsigned int len
, olen
;
31 XRectangle r
= { dc
.x
, dc
.y
, dc
.w
, dc
.h
};
33 XSetForeground(dpy
, dc
.gc
, col
[ColBG
]);
34 XFillRectangles(dpy
, dc
.drawable
, dc
.gc
, &r
, 1);
40 olen
= len
= strlen(text
);
41 if(len
>= sizeof(buf
))
42 len
= sizeof(buf
) - 1;
43 memcpy(buf
, text
, len
);
46 h
= dc
.font
.ascent
+ dc
.font
.descent
;
47 y
= dc
.y
+ (dc
.h
/ 2) - (h
/ 2) + dc
.font
.ascent
;
50 /* shorten text if necessary */
51 while(len
&& (w
= textnw(buf
, len
)) > dc
.w
- h
)
63 return; /* too long */
65 gcv
.foreground
= col
[ColFG
];
67 XChangeGC(dpy
, dc
.gc
, GCForeground
, &gcv
);
68 XmbDrawString(dpy
, dc
.drawable
, dc
.font
.set
, dc
.gc
,
72 gcv
.font
= dc
.font
.xfont
->fid
;
73 XChangeGC(dpy
, dc
.gc
, GCForeground
| GCFont
, &gcv
);
74 XDrawString(dpy
, dc
.drawable
, dc
.gc
, x
, y
, buf
, len
);
79 getcolor(const char *colstr
, const char *alternate
) {
80 Colormap cmap
= DefaultColormap(dpy
, screen
);
83 if(XAllocNamedColor(dpy
, cmap
, colstr
, &color
, &color
) != Success
)
84 XAllocNamedColor(dpy
, cmap
, alternate
, &color
, &color
);
89 setfont(const char *fontstr
) {
94 setlocale(LC_ALL
, "");
96 XFreeFontSet(dpy
, dc
.font
.set
);
97 dc
.font
.set
= XCreateFontSet(dpy
, fontstr
, &missing
, &n
, &def
);
99 XFreeStringList(missing
);
101 XFreeFontSet(dpy
, dc
.font
.set
);
106 XFontSetExtents
*font_extents
;
107 XFontStruct
**xfonts
;
110 dc
.font
.ascent
= dc
.font
.descent
= 0;
111 font_extents
= XExtentsOfFontSet(dc
.font
.set
);
112 n
= XFontsOfFontSet(dc
.font
.set
, &xfonts
, &font_names
);
113 for(i
= 0, dc
.font
.ascent
= 0, dc
.font
.descent
= 0; i
< n
; i
++) {
114 if(dc
.font
.ascent
< (*xfonts
)->ascent
)
115 dc
.font
.ascent
= (*xfonts
)->ascent
;
116 if(dc
.font
.descent
< (*xfonts
)->descent
)
117 dc
.font
.descent
= (*xfonts
)->descent
;
123 XFreeFont(dpy
, dc
.font
.xfont
);
124 dc
.font
.xfont
= NULL
;
125 dc
.font
.xfont
= XLoadQueryFont(dpy
, fontstr
);
127 dc
.font
.xfont
= XLoadQueryFont(dpy
, "fixed");
129 eprint("error, cannot init 'fixed' font\n");
130 dc
.font
.ascent
= dc
.font
.xfont
->ascent
;
131 dc
.font
.descent
= dc
.font
.xfont
->descent
;
133 dc
.font
.height
= dc
.font
.ascent
+ dc
.font
.descent
;
137 textw(const char *text
) {
138 return textnw(text
, strlen(text
)) + dc
.font
.height
;