openbox/render/font.c

78 lines
1.7 KiB
C
Raw Normal View History

2003-03-16 22:34:05 +00:00
#include <stdlib.h>
#include <X11/Xft/Xft.h>
#include "../kernel/openbox.h"
2003-03-16 21:11:39 +00:00
#include "font.h"
#include "../src/gettext.h"
#define _(str) gettext(str)
2003-03-16 22:34:05 +00:00
void font_startup(void)
2003-03-16 21:11:39 +00:00
{
2003-03-16 22:34:05 +00:00
#ifdef DEBUG
int version;
#endif /* DEBUG */
2003-03-16 21:11:39 +00:00
if (!XftInit(0)) {
2003-03-16 22:34:05 +00:00
g_warning(_("Couldn't initialize Xft.\n\n"));
exit(3);
2003-03-16 21:11:39 +00:00
}
#ifdef DEBUG
2003-03-16 22:34:05 +00:00
version = XftGetVersion();
g_message("Using Xft %d.%d.%d (Built against %d.%d.%d).\n",
version / 10000 % 100, version / 100 % 100, version % 100,
XFT_MAJOR, XFT_MINOR, XFT_REVISION);
2003-03-16 21:11:39 +00:00
#endif
2003-03-16 22:34:05 +00:00
}
2003-03-16 21:11:39 +00:00
2003-03-16 22:34:05 +00:00
ObFont *font_open(char *fontstring)
{
ObFont *out;
XftFont *xf;
if ((xf = XftFontOpenName(ob_display, ob_screen, fontstring))) {
out = malloc(sizeof(ObFont));
out->xftfont = xf;
return out;
}
g_warning(_("Unable to load font: %s\n"), fontstring);
g_warning(_("Trying fallback font: %s\n"), "fixed");
2003-03-16 21:11:39 +00:00
2003-03-16 22:34:05 +00:00
if ((xf = XftFontOpenName(ob_display, ob_screen, "fixed"))) {
out = malloc(sizeof(ObFont));
out->xftfont = xf;
return out;
}
g_warning(_("Unable to load font: %s\n"), "fixed");
g_warning(_("Aborting!.\n"));
2003-03-16 21:11:39 +00:00
2003-03-16 22:34:05 +00:00
exit(3); // can't continue without a font
2003-03-16 21:11:39 +00:00
}
2003-03-16 22:34:05 +00:00
void font_close(ObFont *f)
2003-03-16 21:11:39 +00:00
{
2003-03-16 22:34:05 +00:00
XftFontClose(ob_display, f->xftfont);
2003-03-16 21:11:39 +00:00
}
2003-03-16 22:34:05 +00:00
int font_measure_string(ObFont *f, const char *str, int shadow, int offset)
2003-03-16 21:11:39 +00:00
{
2003-03-16 22:34:05 +00:00
XGlyphInfo info;
2003-03-16 21:11:39 +00:00
2003-03-16 22:34:05 +00:00
XftTextExtentsUtf8(ob_display, f->xftfont,
(FcChar8*)str, strlen(str), &info);
2003-03-16 21:11:39 +00:00
2003-03-16 22:34:05 +00:00
return (signed) info.xOff + (shadow ? offset : 0);
2003-03-16 21:11:39 +00:00
}
2003-03-16 22:34:05 +00:00
int font_height(ObFont *f, int shadow, int offset)
2003-03-16 21:11:39 +00:00
{
2003-03-16 22:34:05 +00:00
return (signed) f->xftfont->height + (shadow ? offset : 0);
2003-03-16 21:11:39 +00:00
}
2003-03-16 22:34:05 +00:00
int font_max_char_width(ObFont *f)
2003-03-16 21:11:39 +00:00
{
2003-03-16 22:34:05 +00:00
return (signed) f->xftfont->max_advance_width;
2003-03-16 21:11:39 +00:00
}