[go: up one dir, main page]

Menu

[1d9733]: / x11font.c  Maximize  Restore  History

Download this file

112 lines (93 with data), 2.6 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
/*
* Copyright 2010 Johan Veenhuizen
*/
#include <X11/Xlib.h>
#include <stdlib.h>
#include <string.h>
#include "wind.h"
#define DEFAULT "-*-helvetica-medium-r-*-*-12-*-*-*-*-*-*-*"
struct fontcolor {
GC gc;
};
struct font *ftload(const char *name)
{
XFontSet fontset = NULL;
char **missingcharsetlist;
int missingcharsetcount;
char *defstring;
if (name != NULL) {
fontset = XCreateFontSet(dpy, name, &missingcharsetlist,
&missingcharsetcount, &defstring);
if (fontset == NULL)
errorf("cannot not load fontset %s", name);
}
if (fontset == NULL)
fontset = XCreateFontSet(dpy, DEFAULT, &missingcharsetlist,
&missingcharsetcount, &defstring);
if (fontset == NULL)
fontset = XCreateFontSet(dpy, "fixed", &missingcharsetlist,
&missingcharsetcount, &defstring);
if (fontset == NULL)
return NULL;
XFontSetExtents *extents = XExtentsOfFontSet(fontset);
struct font *f = xmalloc(sizeof *f);
f->data = fontset;
f->ascent = -extents->max_logical_extent.y;
f->descent = extents->max_logical_extent.height - f->ascent;
f->size = f->ascent + f->descent;
return f;
}
void ftfree(struct font *f)
{
XFreeFontSet(dpy, f->data);
free(f);
}
struct fontcolor *ftloadcolor(const char *name)
{
unsigned long pixel = getpixel(name);
struct fontcolor *c = xmalloc(sizeof *c);
c->gc = XCreateGC(dpy, root, GCForeground,
&(XGCValues){ .foreground = pixel });
return c;
}
void ftfreecolor(struct fontcolor *c)
{
XFreeGC(dpy, c->gc);
free(c);
}
void ftdrawstring(Drawable d, struct font *f, struct fontcolor *c,
int x, int y, const char *s)
{
XFontSet fontset = f->data;
XmbDrawString(dpy, d, fontset, c->gc, x, y, s, strlen(s));
}
void ftdrawstring_utf8(Drawable d, struct font *f, struct fontcolor *c,
int x, int y, const char *s)
{
#ifdef X_HAVE_UTF8_STRING
XFontSet fontset = f->data;
Xutf8DrawString(dpy, d, fontset, c->gc, x, y, s, strlen(s));
#else
// This is not correct, but might be better than doing nothing.
ftdrawstring(d, f, c, x, y, s);
#endif
}
int fttextwidth(struct font *f, const char *s)
{
XRectangle r = { .x = 0, .y = 0, .width = 0, .height = 0 };
XFontSet fontset = f->data;
XmbTextExtents(fontset, s, strlen(s), &r, NULL);
return r.x + r.width;
}
int fttextwidth_utf8(struct font *f, const char *s)
{
#ifdef X_HAVE_UTF8_STRING
XRectangle r = { .x = 0, .y = 0, .width = 0, .height = 0 };
XFontSet fontset = f->data;
Xutf8TextExtents(fontset, s, strlen(s), &r, NULL);
return r.x + r.width;
#else
// This is not correct, but might be better than doing nothing.
return fttextwidth(f, s);
#endif
}