[go: up one dir, main page]

Menu

[dbd395]: / xftfont.c  Maximize  Restore  History

Download this file

114 lines (92 with data), 2.3 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/*
* 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]
}