Xinqi Bao's Git
688bd69d87ce8649a070a59723db26adf30ea3fd
1 /* See LICENSE file for copyright and license details. */
13 #define MIN(a, b) ((a) < (b) ? (a) : (b))
14 #define MAX(a, b) ((a) > (b) ? (a) : (b))
19 XFreeFontSet(dpy
, dc
.font
.set
);
21 XFreeFont(dpy
, dc
.font
.xfont
);
22 XFreePixmap(dpy
, dc
.drawable
);
29 dc
.norm
[ColBG
] = getcolor(normbgcolor
);
30 dc
.norm
[ColFG
] = getcolor(normfgcolor
);
31 dc
.sel
[ColBG
] = getcolor(selbgcolor
);
32 dc
.sel
[ColFG
] = getcolor(selfgcolor
);
35 dc
.drawable
= XCreatePixmap(dpy
, parent
, mw
, mh
, DefaultDepth(dpy
, screen
));
36 dc
.gc
= XCreateGC(dpy
, parent
, 0, NULL
);
37 XSetLineAttributes(dpy
, dc
.gc
, 1, LineSolid
, CapButt
, JoinMiter
);
39 XSetFont(dpy
, dc
.gc
, dc
.font
.xfont
->fid
);
43 drawtext(const char *text
, unsigned long col
[ColLast
]) {
45 int i
, x
, y
, h
, len
, olen
;
46 XRectangle r
= { dc
.x
, dc
.y
, dc
.w
, dc
.h
};
48 XSetForeground(dpy
, dc
.gc
, col
[ColBG
]);
49 XFillRectangles(dpy
, dc
.drawable
, dc
.gc
, &r
, 1);
54 y
= dc
.y
+ ((h
+2) / 2) - (h
/ 2) + dc
.font
.ascent
;
56 /* shorten text if necessary */
57 for(len
= MIN(olen
, sizeof buf
); len
&& textnw(text
, len
) > dc
.w
- h
; len
--);
60 memcpy(buf
, text
, len
);
62 for(i
= len
; i
&& i
> len
- 3; buf
[--i
] = '.');
63 XSetForeground(dpy
, dc
.gc
, col
[ColFG
]);
65 XmbDrawString(dpy
, dc
.drawable
, dc
.font
.set
, dc
.gc
, x
, y
, buf
, len
);
67 XDrawString(dpy
, dc
.drawable
, dc
.gc
, x
, y
, buf
, len
);
71 eprint(const char *errstr
, ...) {
75 vfprintf(stderr
, errstr
, ap
);
81 getcolor(const char *colstr
) {
82 Colormap cmap
= DefaultColormap(dpy
, screen
);
85 if(!XAllocNamedColor(dpy
, cmap
, colstr
, &color
, &color
))
86 eprint("drawtext: cannot allocate color '%s'\n", colstr
);
91 initfont(const char *fontstr
) {
92 char *def
, **missing
= NULL
;
95 if(!fontstr
|| fontstr
[0] == '\0')
96 eprint("drawtext: cannot load font: '%s'\n", fontstr
);
97 dc
.font
.set
= XCreateFontSet(dpy
, fontstr
, &missing
, &n
, &def
);
99 XFreeStringList(missing
);
101 XFontStruct
**xfonts
;
103 dc
.font
.ascent
= dc
.font
.descent
= 0;
104 n
= XFontsOfFontSet(dc
.font
.set
, &xfonts
, &font_names
);
105 for(i
= 0; i
< n
; i
++) {
106 dc
.font
.ascent
= MAX(dc
.font
.ascent
, (*xfonts
)->ascent
);
107 dc
.font
.descent
= MAX(dc
.font
.descent
, (*xfonts
)->descent
);
112 if(!(dc
.font
.xfont
= XLoadQueryFont(dpy
, fontstr
))
113 && !(dc
.font
.xfont
= XLoadQueryFont(dpy
, "fixed")))
114 eprint("drawtext: cannot load font: '%s'\n", fontstr
);
115 dc
.font
.ascent
= dc
.font
.xfont
->ascent
;
116 dc
.font
.descent
= dc
.font
.xfont
->descent
;
118 dc
.font
.height
= dc
.font
.ascent
+ dc
.font
.descent
;
122 textnw(const char *text
, unsigned int len
) {
126 XmbTextExtents(dc
.font
.set
, text
, len
, NULL
, &r
);
129 return XTextWidth(dc
.font
.xfont
, text
, len
);
133 textw(const char *text
) {
134 return textnw(text
, strlen(text
)) + dc
.font
.height
;