no need for font_{ascent,descent,height} wrappers; limit font_width to

just requiring xftfont.
This commit is contained in:
okan 2013-05-02 17:25:15 +00:00
parent 5ab3b373d3
commit 5acf6c147f
4 changed files with 28 additions and 44 deletions

View File

@ -450,14 +450,11 @@ void conf_ignore(struct conf *, char *);
void conf_mousebind(struct conf *, char *, char *); void conf_mousebind(struct conf *, char *, char *);
void conf_ungrab(struct conf *, struct keybinding *); void conf_ungrab(struct conf *, struct keybinding *);
int font_ascent(struct screen_ctx *);
int font_descent(struct screen_ctx *);
void font_draw(struct screen_ctx *, const char *, int, void font_draw(struct screen_ctx *, const char *, int,
Drawable, int, int, int); Drawable, int, int, int);
u_int font_height(struct screen_ctx *);
void font_init(struct screen_ctx *, const char *, void font_init(struct screen_ctx *, const char *,
const char **); const char **);
int font_width(struct screen_ctx *, const char *, int); int font_width(XftFont *, const char *, int);
void xev_loop(void); void xev_loop(void);

22
font.c
View File

@ -30,24 +30,6 @@
#include "calmwm.h" #include "calmwm.h"
int
font_ascent(struct screen_ctx *sc)
{
return (sc->xftfont->ascent);
}
int
font_descent(struct screen_ctx *sc)
{
return (sc->xftfont->descent);
}
u_int
font_height(struct screen_ctx *sc)
{
return (sc->xftfont->height + 1);
}
void void
font_init(struct screen_ctx *sc, const char *name, const char **color) font_init(struct screen_ctx *sc, const char *name, const char **color)
{ {
@ -82,11 +64,11 @@ font_init(struct screen_ctx *sc, const char *name, const char **color)
} }
int int
font_width(struct screen_ctx *sc, const char *text, int len) font_width(XftFont *xftfont, const char *text, int len)
{ {
XGlyphInfo extents; XGlyphInfo extents;
XftTextExtentsUtf8(X_Dpy, sc->xftfont, (const FcChar8*)text, XftTextExtentsUtf8(X_Dpy, xftfont, (const FcChar8*)text,
len, &extents); len, &extents);
return (extents.xOff); return (extents.xOff);

32
menu.c
View File

@ -122,9 +122,11 @@ menu_filter(struct screen_ctx *sc, struct menu_q *menuq, char *prompt,
prompt, PROMPT_SCHAR); prompt, PROMPT_SCHAR);
(void)snprintf(mc.dispstr, sizeof(mc.dispstr), "%s%s%s", (void)snprintf(mc.dispstr, sizeof(mc.dispstr), "%s%s%s",
mc.promptstr, mc.searchstr, PROMPT_ECHAR); mc.promptstr, mc.searchstr, PROMPT_ECHAR);
mc.width = font_width(sc, mc.dispstr, strlen(mc.dispstr)); mc.width = font_width(sc->xftfont, mc.dispstr,
strlen(mc.dispstr));
mc.hasprompt = 1; mc.hasprompt = 1;
} }
mc.height = sc->xftfont->height + 1;
if (initial != NULL) if (initial != NULL)
(void)strlcpy(mc.searchstr, initial, sizeof(mc.searchstr)); (void)strlcpy(mc.searchstr, initial, sizeof(mc.searchstr));
@ -135,8 +137,7 @@ menu_filter(struct screen_ctx *sc, struct menu_q *menuq, char *prompt,
mc.print = print; mc.print = print;
mc.entry = mc.prev = -1; mc.entry = mc.prev = -1;
XMoveResizeWindow(X_Dpy, sc->menuwin, mc.x, mc.y, mc.width, XMoveResizeWindow(X_Dpy, sc->menuwin, mc.x, mc.y, mc.width, mc.height);
font_height(sc));
XSelectInput(X_Dpy, sc->menuwin, evmask); XSelectInput(X_Dpy, sc->menuwin, evmask);
XMapRaised(X_Dpy, sc->menuwin); XMapRaised(X_Dpy, sc->menuwin);
@ -164,6 +165,7 @@ menu_filter(struct screen_ctx *sc, struct menu_q *menuq, char *prompt,
goto out; goto out;
/* FALLTHROUGH */ /* FALLTHROUGH */
case Expose: case Expose:
fprintf(stderr, "expose\n");
menu_draw(sc, &mc, menuq, &resultq); menu_draw(sc, &mc, menuq, &resultq);
break; break;
case MotionNotify: case MotionNotify:
@ -373,8 +375,9 @@ menu_draw(struct screen_ctx *sc, struct menu_ctx *mc, struct menu_q *menuq,
if (mc->hasprompt) { if (mc->hasprompt) {
(void)snprintf(mc->dispstr, sizeof(mc->dispstr), "%s%s%s", (void)snprintf(mc->dispstr, sizeof(mc->dispstr), "%s%s%s",
mc->promptstr, mc->searchstr, PROMPT_ECHAR); mc->promptstr, mc->searchstr, PROMPT_ECHAR);
mc->width = font_width(sc, mc->dispstr, strlen(mc->dispstr)); mc->width = font_width(sc->xftfont, mc->dispstr,
mc->height = font_height(sc); strlen(mc->dispstr));
mc->height = sc->xftfont->height + 1;
mc->num = 1; mc->num = 1;
} }
@ -389,9 +392,9 @@ menu_draw(struct screen_ctx *sc, struct menu_ctx *mc, struct menu_q *menuq,
text = mi->text; text = mi->text;
} }
mc->width = MAX(mc->width, font_width(sc, text, mc->width = MAX(mc->width, font_width(sc->xftfont, text,
MIN(strlen(text), MENU_MAXENTRY))); MIN(strlen(text), MENU_MAXENTRY)));
mc->height += font_height(sc); mc->height += sc->xftfont->height + 1;
mc->num++; mc->num++;
} }
@ -425,7 +428,7 @@ menu_draw(struct screen_ctx *sc, struct menu_ctx *mc, struct menu_q *menuq,
if (mc->hasprompt) { if (mc->hasprompt) {
font_draw(sc, mc->dispstr, strlen(mc->dispstr), sc->menuwin, 0, font_draw(sc, mc->dispstr, strlen(mc->dispstr), sc->menuwin, 0,
0, font_ascent(sc)); 0, sc->xftfont->ascent);
n = 1; n = 1;
} else } else
n = 0; n = 0;
@ -433,7 +436,7 @@ menu_draw(struct screen_ctx *sc, struct menu_ctx *mc, struct menu_q *menuq,
TAILQ_FOREACH(mi, resultq, resultentry) { TAILQ_FOREACH(mi, resultq, resultentry) {
char *text = mi->print[0] != '\0' ? char *text = mi->print[0] != '\0' ?
mi->print : mi->text; mi->print : mi->text;
int y = n * font_height(sc) + font_ascent(sc) + 1; int y = n * (sc->xftfont->height + 1) + sc->xftfont->ascent + 1;
/* Stop drawing when menu doesn't fit inside the screen. */ /* Stop drawing when menu doesn't fit inside the screen. */
if (mc->y + y > xine.h) if (mc->y + y > xine.h)
@ -469,10 +472,10 @@ menu_draw_entry(struct screen_ctx *sc, struct menu_ctx *mc,
color = active ? CWM_COLOR_MENU_FG : CWM_COLOR_MENU_BG; color = active ? CWM_COLOR_MENU_FG : CWM_COLOR_MENU_BG;
text = mi->print[0] != '\0' ? mi->print : mi->text; text = mi->print[0] != '\0' ? mi->print : mi->text;
XftDrawRect(sc->xftdraw, &sc->xftcolor[color], 0, XftDrawRect(sc->xftdraw, &sc->xftcolor[color], 0,
font_height(sc) * entry, mc->width, (sc->xftfont->height + 1) * entry, mc->width,
font_height(sc) + font_descent(sc)); (sc->xftfont->height + 1) + sc->xftfont->descent);
font_draw(sc, text, strlen(text), sc->menuwin, active, font_draw(sc, text, strlen(text), sc->menuwin, active,
0, font_height(sc) * entry + font_ascent(sc) + 1); 0, (sc->xftfont->height + 1) * entry + sc->xftfont->ascent + 1);
} }
static void static void
@ -525,11 +528,12 @@ menu_calc_entry(struct screen_ctx *sc, struct menu_ctx *mc, int x, int y)
{ {
int entry; int entry;
entry = y / font_height(sc); entry = y / (sc->xftfont->height + 1);
/* in bounds? */ /* in bounds? */
if (x < 0 || x > mc->width || y < 0 || if (x < 0 || x > mc->width || y < 0 ||
y > font_height(sc) * mc->num || entry < 0 || entry >= mc->num) y > (sc->xftfont->height + 1) * mc->num ||
entry < 0 || entry >= mc->num)
entry = -1; entry = -1;
if (mc->hasprompt && entry == 0) if (mc->hasprompt && entry == 0)

View File

@ -55,23 +55,24 @@ mousefunc_sweep_draw(struct client_ctx *cc)
{ {
struct screen_ctx *sc = cc->sc; struct screen_ctx *sc = cc->sc;
char asize[10]; /* fits "nnnnxnnnn\0" */ char asize[10]; /* fits "nnnnxnnnn\0" */
int width, width_size, width_name; int width, width_size, width_name, height;
(void)snprintf(asize, sizeof(asize), "%dx%d", (void)snprintf(asize, sizeof(asize), "%dx%d",
(cc->geom.w - cc->hint.basew) / cc->hint.incw, (cc->geom.w - cc->hint.basew) / cc->hint.incw,
(cc->geom.h - cc->hint.baseh) / cc->hint.inch); (cc->geom.h - cc->hint.baseh) / cc->hint.inch);
width_size = font_width(sc, asize, strlen(asize)) + 4; width_size = font_width(sc->xftfont, asize, strlen(asize)) + 4;
width_name = font_width(sc, cc->name, strlen(cc->name)) + 4; width_name = font_width(sc->xftfont, cc->name, strlen(cc->name)) + 4;
width = MAX(width_size, width_name); width = MAX(width_size, width_name);
height = sc->xftfont->height + 1;
XReparentWindow(X_Dpy, sc->menuwin, cc->win, 0, 0); XReparentWindow(X_Dpy, sc->menuwin, cc->win, 0, 0);
XMoveResizeWindow(X_Dpy, sc->menuwin, 0, 0, width, font_height(sc) * 2); XMoveResizeWindow(X_Dpy, sc->menuwin, 0, 0, width, height * 2);
XMapWindow(X_Dpy, sc->menuwin); XMapWindow(X_Dpy, sc->menuwin);
XClearWindow(X_Dpy, sc->menuwin); XClearWindow(X_Dpy, sc->menuwin);
font_draw(sc, cc->name, strlen(cc->name), sc->menuwin, 0, font_draw(sc, cc->name, strlen(cc->name), sc->menuwin, 0,
2, font_ascent(sc) + 1); 2, sc->xftfont->ascent + 1);
font_draw(sc, asize, strlen(asize), sc->menuwin, 0, font_draw(sc, asize, strlen(asize), sc->menuwin, 0,
width / 2 - width_size / 2, font_height(sc) + font_ascent(sc) + 1); width / 2 - width_size / 2, height + sc->xftfont->ascent + 1);
} }
void void