/*
* Copyright 2010 Johan Veenhuizen
*/
#include <X11/Xlib.h>
#include <X11/Xft/Xft.h>
#include <string.h>
#include "wind.h"
#define DEFAULT "sans-serif:size=10"
struct font *ftload(const char *name)
{
XftFont *font = NULL;
if (name != NULL) {
font = XftFontOpenXlfd(dpy, scr, name);
if (font == NULL)
font = XftFontOpenName(dpy, scr, name);
if (font == NULL)
errorf("cannot not load font %s", name);
}
if (font == NULL)
font = XftFontOpenName(dpy, scr, DEFAULT);
if (font == NULL)
return NULL;
struct font *f = xmalloc(sizeof *f);
f->size = font->ascent + font->descent;
f->ascent = font->ascent;
f->descent = font->descent;
f->data = font;
return f;
}
struct fontcolor {
XftColor color;
XftDraw *draw;
Visual *visual;
Colormap colormap;
};
struct fontcolor *ftloadcolor(const char *name)
{
XftDraw *draw;
XftColor color;
Visual *visual = DefaultVisual(dpy, scr);
Colormap colormap = DefaultColormap(dpy, scr);
if ((draw = XftDrawCreate(dpy, root, visual, colormap)) == NULL)
return NULL;
if (!XftColorAllocName(dpy, visual, colormap, name, &color))
return NULL;
struct fontcolor *c = xmalloc(sizeof *c);
c->draw = draw;
c->color = color;
c->visual = visual;
c->colormap = colormap;
return c;
}
void ftfree(struct font *f)
{
free(f);
}
void ftfreecolor(struct fontcolor *fc)
{
XftColorFree(dpy, fc->visual, fc->colormap, &fc->color);
XftDrawDestroy(fc->draw);
free(fc);
}
void ftdrawstring(Drawable d, struct font *f, struct fontcolor *c,
int x, int y, const char *s)
{
XftFont *font = f->data;
XftDrawChange(c->draw, d);
XftDrawString8(c->draw, &c->color, font, x, y,
(unsigned char *)s, strlen(s));
}
void ftdrawstring_utf8(Drawable d, struct font *f, struct fontcolor *c,
int x, int y, const char *s)
{
XftFont *font = f->data;
XftDrawChange(c->draw, d);
XftDrawStringUtf8(c->draw, &c->color, font, x, y,
(FcChar8 *)s, strlen(s));
}
int fttextwidth(struct font *f, const char *s)
{
XftFont *font = f->data;
XGlyphInfo info;
XftTextExtents8(dpy, font, (unsigned char *)s, strlen(s), &info);
return info.width - info.x; // [sic]
}
int fttextwidth_utf8(struct font *f, const char *s)
{
XftFont *font = f->data;
XGlyphInfo info;
XftTextExtentsUtf8(dpy, font, (FcChar8 *)s, strlen(s), &info);
return info.width - info.x; // [sic]
}