all repos — openbox @ 43d0f79057c1c4dfa1999db8fd6d9f48e5b47951

openbox fork - make it a bit more like ryudo

fonts are no longer loaded from the theme file. instead, they are created by the application and passed in while creating/loading a theme
Dana Jansens danakj@orodu.net
commit

43d0f79057c1c4dfa1999db8fd6d9f48e5b47951

parent

aeda86f46056a10126f85ad43fb51a92075bcefd

5 files changed, 134 insertions(+), 45 deletions(-)

jump to
M render/font.crender/font.c

@@ -75,10 +75,8 @@

g_free(locale); } -static RrFont *openfont(const RrInstance *inst, gchar *fontstring) +static RrFont *openfontstring(const RrInstance *inst, gchar *fontstring) { - /* This function is called for each font in the theme file. */ - /* It returns a pointer to a RrFont struct after filling it. */ RrFont *out; FcPattern *pat; gint tint;

@@ -90,6 +88,7 @@ return NULL;

out = g_new(RrFont, 1); out->inst = inst; + out->ref = 1; out->font_desc = pango_font_description_new(); out->layout = pango_layout_new(inst->pango);

@@ -156,10 +155,12 @@

/* get the ascent and descent */ measure_font(inst, out); + FcPatternDestroy(pat); + return out; } -RrFont *RrFontOpen(const RrInstance *inst, gchar *fontstring) +RrFont *RrFontOpenByString(const RrInstance *inst, gchar *fontstring) { RrFont *out;

@@ -168,24 +169,98 @@ font_startup();

started = TRUE; } - if ((out = openfont(inst, fontstring))) + if ((out = openfontstring(inst, fontstring))) return out; g_warning(_("Unable to load font: %s\n"), fontstring); g_warning(_("Trying fallback font: %s\n"), "sans"); - if ((out = openfont(inst, "sans"))) + if ((out = openfontstring(inst, "sans"))) return out; g_warning(_("Unable to load font: %s\n"), "sans"); return NULL; } +RrFont *RrFontOpen(const RrInstance *inst, gchar *name, gint size, + RrFontWeight weight, RrFontSlant slant, gboolean shadow, + gint shadowoffset, gchar shadowtint) +{ + RrFont *out; + PangoWeight pweight; + PangoStyle pstyle; + + if (!started) { + font_startup(); + started = TRUE; + } + + g_assert(shadowtint <= 100 && shadowtint >= -100); + + out = g_new(RrFont, 1); + out->inst = inst; + out->ref = 1; + out->font_desc = pango_font_description_new(); + out->layout = pango_layout_new(inst->pango); + + switch (weight) { + case RR_FONTWEIGHT_LIGHT: pweight = PANGO_WEIGHT_LIGHT; break; + case RR_FONTWEIGHT_NORMAL: pweight = PANGO_WEIGHT_NORMAL; break; + case RR_FONTWEIGHT_SEMIBOLD: pweight = PANGO_WEIGHT_SEMIBOLD; break; + case RR_FONTWEIGHT_BOLD: pweight = PANGO_WEIGHT_BOLD; break; + case RR_FONTWEIGHT_ULTRABOLD: pweight = PANGO_WEIGHT_ULTRABOLD; break; + default: g_assert_not_reached(); + } + + switch (slant) { + case RR_FONTSLANT_NORMAL: pstyle = PANGO_STYLE_NORMAL; break; + case RR_FONTSLANT_ITALIC: pstyle = PANGO_STYLE_ITALIC; break; + case RR_FONTSLANT_OBLIQUE: pstyle = PANGO_STYLE_OBLIQUE; break; + default: g_assert_not_reached(); + } + + /* setup the font */ + pango_font_description_set_family(out->font_desc, name); + pango_font_description_set_weight(out->font_desc, pweight); + pango_font_description_set_style(out->font_desc, pstyle); + pango_font_description_set_size(out->font_desc, size * PANGO_SCALE); + + /* setup the shadow */ + out->shadow = shadow; + out->offset = shadowoffset; + out->tint = shadowtint; + + /* setup the layout */ + pango_layout_set_font_description(out->layout, out->font_desc); + pango_layout_set_single_paragraph_mode(out->layout, TRUE); + pango_layout_set_ellipsize(out->layout, PANGO_ELLIPSIZE_MIDDLE); + + /* get the ascent and descent */ + measure_font(inst, out); + + return out; +} + +RrFont *RrFontOpenDefault(const RrInstance *inst) +{ + return RrFontOpen(inst, RrDefaultFontFamily, RrDefaultFontSize, + RrDefaultFontWeight, RrDefaultFontSlant, + RrDefaultFontShadow, RrDefaultFontShadowOffset, + RrDefaultFontShadowTint); +} + +void RrFontRef(RrFont *f) +{ + ++f->ref; +} + void RrFontClose(RrFont *f) { if (f) { - g_object_unref(f->layout); - pango_font_description_free(f->font_desc); - g_free(f); + if (--f->ref < 1) { + g_object_unref(f->layout); + pango_font_description_free(f->font_desc); + g_free(f); + } } }
M render/font.hrender/font.h

@@ -25,18 +25,20 @@ #include <pango/pango.h>

struct _RrFont { const RrInstance *inst; + gint ref; PangoFontDescription *font_desc; PangoLayout *layout; /*!< Used for measuring and rendering strings */ gint ascent; /*!< The font's ascent in pango-units */ gint descent; /*!< The font's descent in pango-units */ - gint elipses_length; /*!< This one is in pixels, yay */ gint shadow; gchar tint; gint offset; }; -RrFont *RrFontOpen(const RrInstance *inst, gchar *fontstring); -void RrFontClose(RrFont *f); void RrFontDraw(XftDraw *d, RrTextureText *t, RrRect *position); + +/*! Increment the references for this font, RrFontClose will decrement until 0 + and then really close it */ +void RrFontRef(RrFont *f); #endif /* __font_h */
M render/render.hrender/render.h

@@ -186,6 +186,14 @@ #define RrDefaultRedOffset 16

#define RrDefaultGreenOffset 8 #define RrDefaultBlueOffset 0 +#define RrDefaultFontFamily "arial,sans" +#define RrDefaultFontSize 8 +#define RrDefaultFontWeight RR_FONTWEIGHT_NORMAL +#define RrDefaultFontSlant RR_FONTSLANT_NORMAL +#define RrDefaultFontShadow FALSE +#define RrDefaultFontShadowOffset 1 +#define RrDefaultFontShadowTint 50 + RrInstance* RrInstanceNew (Display *display, gint screen); void RrInstanceFree (RrInstance *inst);

@@ -219,9 +227,16 @@ RrAppearance *RrAppearanceNew (const RrInstance *inst, gint numtex);

RrAppearance *RrAppearanceCopy (RrAppearance *a); void RrAppearanceFree (RrAppearance *a); +RrFont *RrFontOpenByString (const RrInstance *inst, gchar *fontstring); +RrFont *RrFontOpen (const RrInstance *inst, gchar *name, gint size, + RrFontWeight weight, RrFontSlant slant, + gboolean shadow, gint shadowoffset, + gchar shadowtint); +RrFont *RrFontOpenDefault (const RrInstance *inst); +void RrFontClose (RrFont *f); RrSize *RrFontMeasureString (const RrFont *f, const gchar *str); -gint RrFontHeight (const RrFont *f); -gint RrFontMaxCharWidth (const RrFont *f); +gint RrFontHeight (const RrFont *f); +gint RrFontMaxCharWidth (const RrFont *f); void RrPaint (RrAppearance *a, Window win, gint w, gint h); void RrMinsize (RrAppearance *a, gint *w, gint *h);
M render/theme.crender/theme.c

@@ -45,12 +45,13 @@ gboolean allow_trans);

static RrPixel32* read_c_image(gint width, gint height, const guint8 *data); static void set_default_appearance(RrAppearance *a); -RrTheme* RrThemeNew(const RrInstance *inst, gchar *name) +RrTheme* RrThemeNew(const RrInstance *inst, gchar *name, + RrFont *active_window_font, RrFont *inactive_window_font, + RrFont *menu_title_font, RrFont *menu_item_font) { XrmDatabase db = NULL; RrJustify winjust, mtitlejust; gchar *str; - gchar *font_str; RrTheme *theme; theme = g_new0(RrTheme, 1);

@@ -110,22 +111,18 @@ theme->name = g_path_get_basename(DEFAULT_THEME);

} /* load the font stuff */ - if (!read_string(db, "window.active.label.text.font", &font_str)) - font_str = "arial,sans:bold:pixelsize=10:shadow=y:shadowtint=50"; - - if (!(theme->win_font_focused = RrFontOpen(inst, font_str))) { - RrThemeFree(theme); - return NULL; - } + if (active_window_font) { + theme->win_font_focused = active_window_font; + RrFontRef(active_window_font); + } else + theme->win_font_focused = RrFontOpenDefault(inst); theme->win_font_height = RrFontHeight(theme->win_font_focused); - if (!read_string(db, "window.inactive.label.text.font", &font_str)) - /* font_str will already be set to the last one */; - - if (!(theme->win_font_unfocused = RrFontOpen(inst, font_str))) { - RrThemeFree(theme); - return NULL; - } + if (inactive_window_font) { + theme->win_font_unfocused = inactive_window_font; + RrFontRef(inactive_window_font); + } else + theme->win_font_unfocused = RrFontOpenDefault(inst); theme->win_font_height = MAX(theme->win_font_height, RrFontHeight(theme->win_font_unfocused));

@@ -137,13 +134,11 @@ else if (!g_ascii_strcasecmp(str, "center"))

winjust = RR_JUSTIFY_CENTER; } - if (!read_string(db, "menu.title.text.font", &font_str)) - font_str = "arial,sans:bold:pixelsize=12:shadow=y"; - - if (!(theme->menu_title_font = RrFontOpen(inst, font_str))) { - RrThemeFree(theme); - return NULL; - } + if (menu_title_font) { + theme->menu_title_font = menu_title_font; + RrFontRef(menu_title_font); + } else + theme->menu_title_font = RrFontOpenDefault(inst); theme->menu_title_font_height = RrFontHeight(theme->menu_title_font); mtitlejust = RR_JUSTIFY_LEFT;

@@ -154,13 +149,11 @@ else if (!g_ascii_strcasecmp(str, "center"))

mtitlejust = RR_JUSTIFY_CENTER; } - if (!read_string(db, "menu.items.font", &font_str)) - font_str = "arial,sans:bold:pixelsize=11:shadow=y"; - - if (!(theme->menu_font = RrFontOpen(inst, font_str))) { - RrThemeFree(theme); - return NULL; - } + if (menu_item_font) { + theme->menu_font = menu_item_font; + RrFontRef(menu_item_font); + } else + theme->menu_font = RrFontOpenDefault(inst); theme->menu_font_height = RrFontHeight(theme->menu_font); /* load direct dimensions */
M render/theme.hrender/theme.h

@@ -187,7 +187,11 @@ RrAppearance *app_unhilite_label; /* can be parent relative */

}; -RrTheme* RrThemeNew(const RrInstance *inst, gchar *theme); +/*! The font values are all optional. If a NULL is used for any of them, then + the default font will be used. */ +RrTheme* RrThemeNew(const RrInstance *inst, gchar *theme, + RrFont *active_window_font, RrFont *inactive_window_font, + RrFont *menu_title_font, RrFont *menu_item_font); void RrThemeFree(RrTheme *theme); G_END_DECLS