From: Dana Jansens Date: Thu, 26 Jun 2003 01:29:45 +0000 (+0000) Subject: read fonts and titlebar layout from theme files X-Git-Url: https://git.brokenzipper.com/gitweb?a=commitdiff_plain;h=a12e73cf3741f91d9188bacbcc7733d5948c8156;p=chaz%2Fopenbox read fonts and titlebar layout from theme files --- diff --git a/render/font.c b/render/font.c index 9f1f5ef8..2fce1dd4 100644 --- a/render/font.c +++ b/render/font.c @@ -11,26 +11,28 @@ #include #define ELIPSES "..." -#define ELIPSES_LENGTH(font, shadow, offset) \ - (font->elipses_length + (shadow ? offset : 0)) +#define ELIPSES_LENGTH(font) \ + (font->elipses_length + (font->shadow ? font->offset : 0)) + +#define OB_SHADOW "shadow" +#define OB_SHADOW_OFFSET "shadowoffset" +#define OB_SHADOW_ALPHA "shadowtint" + +FcObjectType objs[] = { + { OB_SHADOW, FcTypeBool }, + { OB_SHADOW_OFFSET, FcTypeInteger }, + { OB_SHADOW_ALPHA, FcTypeInteger } +}; static gboolean started = FALSE; static void font_startup(void) { -#ifdef DEBUG - int version; -#endif /* DEBUG */ if (!XftInit(0)) { g_warning(_("Couldn't initialize Xft.\n")); exit(EXIT_FAILURE); } -#ifdef DEBUG - version = XftGetVersion(); - g_message("Using Xft %d.%d.%d (Built against %d.%d.%d).", - version / 10000 % 100, version / 100 % 100, version % 100, - XFT_MAJOR, XFT_MINOR, XFT_REVISION); -#endif + FcNameRegisterObjectTypes(objs, (sizeof(objs) / sizeof(objs[0]))); } static void measure_font(RrFont *f) @@ -43,33 +45,68 @@ static void measure_font(RrFont *f) f->elipses_length = (signed) info.xOff; } +static RrFont *openfont(const RrInstance *inst, char *fontstring) +{ + RrFont *out; + FcPattern *pat, *match; + XftFont *font; + FcResult res; + gint tint; + + if (!(pat = XftNameParse(fontstring))) + return NULL; + + match = XftFontMatch(RrDisplay(inst), RrScreen(inst), pat, &res); + FcPatternDestroy(pat); + if (!match) + return NULL; + + out = g_new(RrFont, 1); + out->inst = inst; + + if (FcPatternGetBool(match, OB_SHADOW, 0, &out->shadow) != FcResultMatch) + out->shadow = FALSE; + g_message("shadow %d", out->shadow); + + if (FcPatternGetInteger(match, OB_SHADOW_OFFSET, 0, &out->offset) != + FcResultMatch) + out->offset = 1; + + if (FcPatternGetInteger(match, OB_SHADOW_ALPHA, 0, &tint) != FcResultMatch) + tint = 25; + if (tint > 100) tint = 100; + else if (tint < -100) tint = -100; + out->tint = tint; + + font = XftFontOpenPattern(RrDisplay(inst), match); + FcPatternDestroy(match); + if (!font) { + g_free(out); + return NULL; + } else + out->xftfont = font; + + measure_font(out); + + return out; +} + RrFont *RrFontOpen(const RrInstance *inst, char *fontstring) { RrFont *out; - XftFont *xf; if (!started) { font_startup(); started = TRUE; } - - if ((xf = XftFontOpenName(RrDisplay(inst), RrScreen(inst), fontstring))) { - out = g_new(RrFont, 1); - out->inst = inst; - out->xftfont = xf; - measure_font(out); + + if ((out = openfont(inst, fontstring))) return out; - } g_warning(_("Unable to load font: %s\n"), fontstring); g_warning(_("Trying fallback font: %s\n"), "sans"); - if ((xf = XftFontOpenName(RrDisplay(inst), RrScreen(inst), "sans"))) { - out = g_new(RrFont, 1); - out->inst = inst; - out->xftfont = xf; - measure_font(out); + if ((out = openfont(inst, "sans"))) return out; - } g_warning(_("Unable to load font: %s\n"), "sans"); return NULL; @@ -84,28 +121,28 @@ void RrFontClose(RrFont *f) } static void font_measure_full(const RrFont *f, const gchar *str, - gint shadow, gint offset, gint *x, gint *y) + gint *x, gint *y) { XGlyphInfo info; XftTextExtentsUtf8(RrDisplay(f->inst), f->xftfont, (const FcChar8*)str, strlen(str), &info); - *x = (signed) info.xOff + (shadow ? ABS(offset) : 0); - *y = info.height + (shadow ? ABS(offset) : 0); + *x = (signed) info.xOff + (f->shadow ? ABS(f->offset) : 0); + *y = info.height + (f->shadow ? ABS(f->offset) : 0); } -int RrFontMeasureString(const RrFont *f, const gchar *str, - gint shadow, gint offset) +int RrFontMeasureString(const RrFont *f, const gchar *str) { gint x, y; - font_measure_full (f, str, shadow, offset, &x, &y); + font_measure_full (f, str, &x, &y); return x; } -int RrFontHeight(const RrFont *f, gint shadow, gint offset) +int RrFontHeight(const RrFont *f) { - return f->xftfont->ascent + f->xftfont->descent + (shadow ? offset : 0); + return f->xftfont->ascent + f->xftfont->descent + + (f->shadow ? f->offset : 0); } int RrFontMaxCharWidth(const RrFont *f) @@ -124,7 +161,7 @@ void RrFontDraw(XftDraw *d, RrTextureText *t, Rect *area) /* center vertically */ y = area->y + - (area->height - RrFontHeight(t->font, t->shadow, t->offset)) / 2; + (area->height - RrFontHeight(t->font)) / 2; /* the +2 and -4 leave a small blank edge on the sides */ x = area->x + 2; w = area->width - 4; @@ -132,16 +169,16 @@ void RrFontDraw(XftDraw *d, RrTextureText *t, Rect *area) text = g_string_new(t->string); l = g_utf8_strlen(text->str, -1); - font_measure_full(t->font, text->str, t->shadow, t->offset, &mw, &mh); + font_measure_full(t->font, text->str, &mw, &mh); while (l && mw > area->width) { shortened = TRUE; /* remove a character from the middle */ text = g_string_erase(text, l-- / 2, 1); - em = ELIPSES_LENGTH(t->font, t->shadow, t->offset); + em = ELIPSES_LENGTH(t->font); /* if the elipses are too large, don't show them at all */ if (em > area->width) shortened = FALSE; - font_measure_full(t->font, text->str, t->shadow, t->offset, &mw, &mh); + font_measure_full(t->font, text->str, &mw, &mh); mw += em; } if (shortened) { @@ -163,24 +200,24 @@ void RrFontDraw(XftDraw *d, RrTextureText *t, Rect *area) l = strlen(text->str); /* number of bytes */ - if (t->shadow) { - if (t->tint >= 0) { + if (t->font->shadow) { + if (t->font->tint >= 0) { c.color.red = 0; c.color.green = 0; c.color.blue = 0; - c.color.alpha = 0xffff * t->tint / 100; /* transparent shadow */ + c.color.alpha = 0xffff * t->font->tint / 100; c.pixel = BlackPixel(RrDisplay(t->font->inst), RrScreen(t->font->inst)); } else { - c.color.red = 0xffff * -t->tint / 100; - c.color.green = 0xffff * -t->tint / 100; - c.color.blue = 0xffff * -t->tint / 100; - c.color.alpha = 0xffff * -t->tint / 100; /* transparent shadow */ + c.color.red = 0xffff; + c.color.green = 0xffff; + c.color.blue = 0xffff; + c.color.alpha = 0xffff * -t->font->tint / 100; c.pixel = WhitePixel(RrDisplay(t->font->inst), RrScreen(t->font->inst)); } - XftDrawStringUtf8(d, &c, t->font->xftfont, x + t->offset, - t->font->xftfont->ascent + y + t->offset, + XftDrawStringUtf8(d, &c, t->font->xftfont, x + t->font->offset, + t->font->xftfont->ascent + y + t->font->offset, (FcChar8*)text->str, l); } c.color.red = t->color->r | t->color->r << 8; @@ -188,7 +225,7 @@ void RrFontDraw(XftDraw *d, RrTextureText *t, Rect *area) c.color.blue = t->color->b | t->color->b << 8; c.color.alpha = 0xff | 0xff << 8; /* fully opaque text */ c.pixel = t->color->pixel; - + XftDrawStringUtf8(d, &c, t->font->xftfont, x, t->font->xftfont->ascent + y, (FcChar8*)text->str, l); diff --git a/render/font.h b/render/font.h index 22aeaec0..85aa9f3e 100644 --- a/render/font.h +++ b/render/font.h @@ -9,6 +9,9 @@ struct _RrFont { const RrInstance *inst; XftFont *xftfont; gint elipses_length; + gint shadow; + gchar tint; + gint offset; }; RrFont *RrFontOpen(const RrInstance *inst, char *fontstring); diff --git a/render/render.c b/render/render.c index 1d5b705c..96866923 100644 --- a/render/render.c +++ b/render/render.c @@ -263,13 +263,9 @@ void RrMinsize(RrAppearance *l, gint *w, gint *h) break; case RR_TEXTURE_TEXT: m = RrFontMeasureString(l->texture[i].data.text.font, - l->texture[i].data.text.string, - l->texture[i].data.text.shadow, - l->texture[i].data.text.offset); + l->texture[i].data.text.string); *w = MAX(*w, m); - m = RrFontHeight(l->texture[i].data.text.font, - l->texture[i].data.text.shadow, - l->texture[i].data.text.offset); + m = RrFontHeight(l->texture[i].data.text.font); *h += MAX(*h, m); break; case RR_TEXTURE_RGBA: diff --git a/render/render.h b/render/render.h index 8469c69e..4fe7db1a 100644 --- a/render/render.h +++ b/render/render.h @@ -76,9 +76,6 @@ struct _RrSurface { struct _RrTextureText { RrFont *font; RrJustify justify; - gint shadow; - gchar tint; - guchar offset; RrColor *color; gchar *string; }; @@ -173,9 +170,8 @@ RrAppearance *RrAppearanceNew (const RrInstance *inst, gint numtex); RrAppearance *RrAppearanceCopy (RrAppearance *a); void RrAppearanceFree (RrAppearance *a); -int RrFontMeasureString (const RrFont *f, const gchar *str, - gint shadow, gint offset); -int RrFontHeight (const RrFont *f, gint shadow, gint offset); +int RrFontMeasureString (const RrFont *f, const gchar *str); +int RrFontHeight (const RrFont *f); int RrFontMaxCharWidth (const RrFont *f); void RrPaint (RrAppearance *l, Window win, gint w, gint h); diff --git a/render/theme.c b/render/theme.c index 4f86c8d0..3da99868 100644 --- a/render/theme.c +++ b/render/theme.c @@ -108,29 +108,14 @@ RrTheme* RrThemeNew(const RrInstance *inst, gchar *name) } /* load the font stuff */ - font_str = "arial:bold:pixelsize=10"; - - theme->winfont_shadow = FALSE; - if (read_string(db, "window.xft.flags", &str)) { - if (g_strrstr(str, "shadow")) - theme->winfont_shadow = TRUE; - g_free(str); - } - - if (!read_int(db, "window.xft.shadow.offset", - &theme->winfont_shadow_offset)) - theme->winfont_shadow_offset = 1; - if (!read_int(db, "window.xft.shadow.tint", - &theme->winfont_shadow_tint) || - theme->winfont_shadow_tint < 100 || theme->winfont_shadow_tint > 100) - theme->winfont_shadow_tint = 25; + if (!read_string(db, "window.title.xftfont", &font_str)) + font_str = "arial,sans:bold:pixelsize=10:shadow=y:shadowtint=50"; if (!(theme->winfont = RrFontOpen(inst, font_str))) { RrThemeFree(theme); return NULL; } - theme->winfont_height = RrFontHeight(theme->winfont, theme->winfont_shadow, - theme->winfont_shadow_offset); + theme->winfont_height = RrFontHeight(theme->winfont); winjust = RR_JUSTIFY_LEFT; if (read_string(db, "window.justify", &str)) { @@ -138,34 +123,16 @@ RrTheme* RrThemeNew(const RrInstance *inst, gchar *name) winjust = RR_JUSTIFY_RIGHT; else if (!g_ascii_strcasecmp(str, "center")) winjust = RR_JUSTIFY_CENTER; - g_free(str); } - font_str = "arial-10:bold"; - - theme->mtitlefont_shadow = FALSE; - if (read_string(db, "menu.title.xft.flags", &str)) { - if (g_strrstr(str, "shadow")) - theme->mtitlefont_shadow = TRUE; - g_free(str); - } - - if (!read_int(db, "menu.title.xft.shadow.offset", - &theme->mtitlefont_shadow_offset)) - theme->mtitlefont_shadow_offset = 1; - if (!read_int(db, "menu.title.xft.shadow.tint", - &theme->mtitlefont_shadow_tint) || - theme->mtitlefont_shadow_tint < 100 || - theme->mtitlefont_shadow_tint > 100) - theme->mtitlefont_shadow_tint = 25; + if (!read_string(db, "menu.title.xftfont", &font_str)) + font_str = "arial,sans:bold:pixelsize=12:shadow=y"; if (!(theme->mtitlefont = RrFontOpen(inst, font_str))) { RrThemeFree(theme); return NULL; } - theme->mtitlefont_height = RrFontHeight(theme->mtitlefont, - theme->mtitlefont_shadow, - theme->mtitlefont_shadow_offset); + theme->mtitlefont_height = RrFontHeight(theme->mtitlefont); mtitlejust = RR_JUSTIFY_LEFT; if (read_string(db, "menu.title.justify", &str)) { @@ -173,33 +140,16 @@ RrTheme* RrThemeNew(const RrInstance *inst, gchar *name) mtitlejust = RR_JUSTIFY_RIGHT; else if (!g_ascii_strcasecmp(str, "center")) mtitlejust = RR_JUSTIFY_CENTER; - g_free(str); } - font_str = "arial-8"; - - theme->mfont_shadow = FALSE; - if (read_string(db, "menu.frame.xft.flags", &str)) { - if (g_strrstr(str, "shadow")) - theme->mfont_shadow = TRUE; - g_free(str); - } - - if (!read_int(db, "menu.frame.xft.shadow.offset", - &theme->mfont_shadow_offset)) - theme->mfont_shadow_offset = 1; - if (!read_int(db, "menu.frame.xft.shadow.tint", - &theme->mfont_shadow_tint) || - theme->mfont_shadow_tint < 100 || - theme->mfont_shadow_tint > 100) - theme->mfont_shadow_tint = 25; + if (!read_string(db, "menu.frame.xftfont", &font_str)) + font_str = "arial,sans:bold:pixelsize=11:shadow=y"; if (!(theme->mfont = RrFontOpen(inst, font_str))) { RrThemeFree(theme); return NULL; } - theme->mfont_height = RrFontHeight(theme->mfont, theme->mfont_shadow, - theme->mfont_shadow_offset); + theme->mfont_height = RrFontHeight(theme->mfont); mjust = RR_JUSTIFY_LEFT; if (read_string(db, "menu.frame.justify", &str)) { @@ -207,11 +157,12 @@ RrTheme* RrThemeNew(const RrInstance *inst, gchar *name) mjust = RR_JUSTIFY_RIGHT; else if (!g_ascii_strcasecmp(str, "center")) mjust = RR_JUSTIFY_CENTER; - g_free(str); } /* load the title layout */ - theme->title_layout = g_strdup("NLIMC"); + if (!read_string(db, "window.title.layout", &font_str)) + font_str = "NLIMC"; + theme->title_layout = g_strdup(font_str); if (!read_int(db, "handleWidth", &theme->handle_height) || theme->handle_height < 0 || theme->handle_height > 100) @@ -458,15 +409,6 @@ RrTheme* RrThemeNew(const RrInstance *inst, gchar *name) theme->app_hilite_label->texture[0].data.text.justify = RR_JUSTIFY_LEFT; theme->a_focused_label->texture[0].data.text.font = theme->app_hilite_label->texture[0].data.text.font = theme->winfont; - theme->a_focused_label->texture[0].data.text.shadow = - theme->app_hilite_label->texture[0].data.text.shadow = - theme->winfont_shadow; - theme->a_focused_label->texture[0].data.text.offset = - theme->app_hilite_label->texture[0].data.text.offset = - theme->winfont_shadow_offset; - theme->a_focused_label->texture[0].data.text.tint = - theme->app_hilite_label->texture[0].data.text.tint = - theme->winfont_shadow_tint; theme->a_focused_label->texture[0].data.text.color = theme->app_hilite_label->texture[0].data.text.color = theme->title_focused_color; @@ -477,15 +419,6 @@ RrTheme* RrThemeNew(const RrInstance *inst, gchar *name) theme->app_unhilite_label->texture[0].data.text.justify = RR_JUSTIFY_LEFT; theme->a_unfocused_label->texture[0].data.text.font = theme->app_unhilite_label->texture[0].data.text.font = theme->winfont; - theme->a_unfocused_label->texture[0].data.text.shadow = - theme->app_unhilite_label->texture[0].data.text.shadow = - theme->winfont_shadow; - theme->a_unfocused_label->texture[0].data.text.offset = - theme->app_unhilite_label->texture[0].data.text.offset = - theme->winfont_shadow_offset; - theme->a_unfocused_label->texture[0].data.text.tint = - theme->app_unhilite_label->texture[0].data.text.tint = - theme->winfont_shadow_tint; theme->a_unfocused_label->texture[0].data.text.color = theme->app_unhilite_label->texture[0].data.text.color = theme->title_unfocused_color; @@ -493,11 +426,6 @@ RrTheme* RrThemeNew(const RrInstance *inst, gchar *name) theme->a_menu_title->texture[0].type = RR_TEXTURE_TEXT; theme->a_menu_title->texture[0].data.text.justify = mtitlejust; theme->a_menu_title->texture[0].data.text.font = theme->mtitlefont; - theme->a_menu_title->texture[0].data.text.shadow = theme->mtitlefont_shadow; - theme->a_menu_title->texture[0].data.text.offset = - theme->mtitlefont_shadow_offset; - theme->a_menu_title->texture[0].data.text.tint = - theme->mtitlefont_shadow_tint; theme->a_menu_title->texture[0].data.text.color = theme->menu_title_color; theme->a_menu_item->surface.grad = @@ -513,18 +441,6 @@ RrTheme* RrThemeNew(const RrInstance *inst, gchar *name) theme->a_menu_item->texture[0].data.text.font = theme->a_menu_disabled->texture[0].data.text.font = theme->a_menu_hilite->texture[0].data.text.font = theme->mfont; - theme->a_menu_item->texture[0].data.text.shadow = - theme->a_menu_disabled->texture[0].data.text.shadow = - theme->a_menu_hilite->texture[0].data.text.shadow = - theme->mfont_shadow; - theme->a_menu_item->texture[0].data.text.offset = - theme->a_menu_disabled->texture[0].data.text.offset = - theme->a_menu_hilite->texture[0].data.text.offset = - theme->mfont_shadow_offset; - theme->a_menu_item->texture[0].data.text.tint = - theme->a_menu_disabled->texture[0].data.text.tint = - theme->a_menu_hilite->texture[0].data.text.tint = - theme->mfont_shadow_tint; theme->a_menu_item->texture[0].data.text.color = theme->menu_color; theme->a_menu_disabled->texture[0].data.text.color = theme->menu_disabled_color; @@ -767,7 +683,7 @@ static gboolean read_string(XrmDatabase db, char *rname, char **value) if (XrmGetResource(db, rname, rclass, &rettype, &retvalue) && retvalue.addr != NULL) { - *value = g_strdup(retvalue.addr); + *value = retvalue.addr; ret = TRUE; } diff --git a/render/theme.h b/render/theme.h index 5c39b3df..b696d9e4 100644 --- a/render/theme.h +++ b/render/theme.h @@ -36,19 +36,10 @@ struct _RrTheme { /* style settings - fonts */ gint winfont_height; RrFont *winfont; - gboolean winfont_shadow; - gint winfont_shadow_offset; - gint winfont_shadow_tint; gint mtitlefont_height; RrFont *mtitlefont; - gboolean mtitlefont_shadow; - gint mtitlefont_shadow_offset; - gint mtitlefont_shadow_tint; gint mfont_height; RrFont *mfont; - gboolean mfont_shadow; - gint mfont_shadow_offset; - gint mfont_shadow_tint; /* style settings - title layout */ gchar *title_layout;