(wrong string) élien Aptel
changeset: 224:0f0f3e2a0193
branch: xft
user: Aurélien Aptel <aurelien.aptel_AT_gmail.com>
date: Mon Jan 23 00:03:46 2012 +0100
files: st.c
description:
basic xft rendering working.
big thanks to Ondrej Martinek who sent a patch almost a year ago for
this. i finally got around it and learned a bit about all this font
stuff.
* cured of ifdef-itis, etc.
* dropped x11 core font
* re-arrange code & refactor
diff -r 3c2f9f2ab5e4 -r 0f0f3e2a0193 st.c
--- a/st.c Sat Jan 21 23:43:03 2012 +0100
+++ b/st.c Mon Jan 23 00:03:46 2012 +0100
_AT_@ -24,6 +24,9 @@
#include <X11/Xutil.h>
#include <X11/cursorfont.h>
#include <X11/keysym.h>
+#include <X11/Xft/Xft.h>
+#define Glyph Glyph_
+#define Font Font_
#if defined(__linux)
#include <pty.h>
_AT_@ -37,6 +40,9 @@
"st " VERSION " (c) 2010-2011 st engineers\n" \
"usage: st [-t title] [-c class] [-w windowid] [-v] [-e command...]\n"
+#define XFT_FONT "monospace-8"
+#define XFT_BFONT XFT_FONT ":bold"
+
/* XEMBED messages */
#define XEMBED_FOCUS_IN 4
#define XEMBED_FOCUS_OUT 5
_AT_@ -137,6 +143,8 @@
Atom xembed;
XIM xim;
XIC xic;
+ XftDraw* xft_draw;
+ Visual* vis;
int scr;
int w; /* window width */
int h; /* window height */
_AT_@ -154,17 +162,21 @@
char s[ESC_BUF_SIZ];
} Key;
+typedef struct {
+ int ascent;
+ int descent;
+ short lbearing;
+ short rbearing;
+ XFontSet set;
+ XftFont* xft_set;
+} Font;
+
/* Drawing Context */
typedef struct {
ulong col[256];
+ XftColor xft_col[256];
GC gc;
- struct {
- int ascent;
- int descent;
- short lbearing;
- short rbearing;
- XFontSet set;
- } font, bfont;
+ Font font, bfont;
} DC;
/* TODO: use better name for vars... */
_AT_@ -284,6 +296,7 @@
static char *opt_title = NULL;
static char *opt_embed = NULL;
static char *opt_class = NULL;
+static char *opt_font = NULL;
int
utf8decode(char *s, long *u) {
_AT_@ -1581,44 +1594,46 @@
xw.w-2*BORDER, xw.h-xw.bufh-BORDER,
False);
xw.buf = newbuf;
+ XftDrawChange(xw.xft_draw, xw.buf);
}
void
xloadcols(void) {
int i, r, g, b;
- XColor color;
ulong white = WhitePixel(xw.dpy, xw.scr);
-
+ XRenderColor xft_color = { .alpha = 0 };
+
for(i = 0; i < LEN(colorname); i++) {
- if(!XAllocNamedColor(xw.dpy, xw.cmap, colorname[i], &color, &color)) {
+ if(!XftColorAllocName(xw.dpy, xw.vis, xw.cmap, colorname[i], &dc.xft_col[i])) {
dc.col[i] = white;
fprintf(stderr, "Could not allocate color '%s'\n", colorname[i]);
} else
- dc.col[i] = color.pixel;
+ dc.col[i] = dc.xft_col[i].pixel;
}
/* same colors as xterm */
for(r = 0; r < 6; r++)
for(g = 0; g < 6; g++)
for(b = 0; b < 6; b++) {
- color.red = r == 0 ? 0 : 0x3737 + 0x2828 * r;
- color.green = g == 0 ? 0 : 0x3737 + 0x2828 * g;
- color.blue = b == 0 ? 0 : 0x3737 + 0x2828 * b;
- if(!XAllocColor(xw.dpy, xw.cmap, &color)) {
+ xft_color.red = r == 0 ? 0 : 0x3737 + 0x2828 * r;
+ xft_color.green = g == 0 ? 0 : 0x3737 + 0x2828 * g;
+ xft_color.blue = b == 0 ? 0 : 0x3737 + 0x2828 * b;
+
+ if(!XftColorAllocValue(xw.dpy, xw.vis, xw.cmap, &xft_color, &dc.xft_col[i])) {
dc.col[i] = white;
fprintf(stderr, "Could not allocate color %d\n", i);
} else
- dc.col[i] = color.pixel;
+ dc.col[i] = dc.xft_col[i].pixel;
i++;
}
for(r = 0; r < 24; r++, i++) {
- color.red = color.green = color.blue = 0x0808 + 0x0a0a * r;
- if (!XAllocColor(xw.dpy, xw.cmap, &color)) {
+ xft_color.red = xft_color.green = xft_color.blue = 0x0808 + 0x0a0a * r;
+ if(!XftColorAllocValue(xw.dpy, xw.vis, xw.cmap, &xft_color, &dc.xft_col[i])) {
dc.col[i] = white;
fprintf(stderr, "Could not allocate color %d\n", i);
} else
- dc.col[i] = color.pixel;
+ dc.col[i] = dc.xft_col[i].pixel;
}
}
_AT_@ -1646,48 +1661,17 @@
XSetWMProperties(xw.dpy, xw.win, NULL, NULL, NULL, 0, &size, &wm, &class);
}
-XFontSet
-xinitfont(char *fontstr) {
- XFontSet set;
- char *def, **missing;
- int n;
+void
+xinitfont(Font *f, char *fontstr) {
+ f->xft_set = XftFontOpenName(xw.dpy, xw.scr, fontstr);
- missing = NULL;
- set = XCreateFontSet(xw.dpy, fontstr, &missing, &n, &def);
- if(missing) {
- while(n--)
- fprintf(stderr, "st: missing fontset: %s\n", missing[n]);
- XFreeStringList(missing);
- }
- return set;
-}
+ if(!f->xft_set)
+ die("st: can't open font %s.\n", fontstr);
-void
-xgetfontinfo(XFontSet set, int *ascent, int *descent, short *lbearing, short *rbearing) {
- XFontStruct **xfonts;
- char **font_names;
- int i, n;
-
- *ascent = *descent = *lbearing = *rbearing = 0;
- n = XFontsOfFontSet(set, &xfonts, &font_names);
- for(i = 0; i < n; i++) {
- *ascent = MAX(*ascent, (*xfonts)->ascent);
- *descent = MAX(*descent, (*xfonts)->descent);
- *lbearing = MAX(*lbearing, (*xfonts)->min_bounds.lbearing);
- *rbearing = MAX(*rbearing, (*xfonts)->max_bounds.rbearing);
- xfonts++;
- }
-}
-
-void
-initfonts(char *fontstr, char *bfontstr) {
- if((dc.font.set = xinitfont(fontstr)) == NULL ||
- (dc.bfont.set = xinitfont(bfontstr)) == NULL)
- die("Can't load font %s\n", dc.font.set ? BOLDFONT : FONT);
- xgetfontinfo(dc.font.set, &dc.font.ascent, &dc.font.descent,
- &dc.font.lbearing, &dc.font.rbearing);
- xgetfontinfo(dc.bfont.set, &dc.bfont.ascent, &dc.bfont.descent,
- &dc.bfont.lbearing, &dc.bfont.rbearing);
+ f->ascent = f->xft_set->ascent;
+ f->descent = f->xft_set->descent;
+ f->lbearing = 0;
+ f->rbearing = f->xft_set->max_advance_width;
}
void
_AT_@ -1695,13 +1679,20 @@
XSetWindowAttributes attrs;
Cursor cursor;
Window parent;
+ char font[256];
if(!(xw.dpy = XOpenDisplay(NULL)))
die("Can't open display\n");
+
xw.scr = XDefaultScreen(xw.dpy);
-
- /* font */
- initfonts(FONT, BOLDFONT);
+ xw.vis = XDefaultVisual(xw.dpy, xw.scr);
+
+ /* fonts */
+ /* XXX: quick debug hack, needs fixing.. */
+ strncpy(font, opt_font ? opt_font : XFT_FONT, 255);
+ xinitfont(&dc.font, font);
+ strncat(font, ":bold", 255);
+ xinitfont(&dc.bfont, font);
/* XXX: Assuming same size for bold font */
xw.cw = dc.font.rbearing - dc.font.lbearing;
_AT_@ -1729,12 +1720,14 @@
parent = opt_embed ? strtol(opt_embed, NULL, 0) : XRootWindow(xw.dpy, xw.scr);
xw.win = XCreateWindow(xw.dpy, parent, 0, 0,
xw.w, xw.h, 0, XDefaultDepth(xw.dpy, xw.scr), InputOutput,
- XDefaultVisual(xw.dpy, xw.scr),
+ xw.vis,
CWBackPixel | CWBorderPixel | CWBitGravity | CWEventMask
| CWColormap,
&attrs);
xw.buf = XCreatePixmap(xw.dpy, xw.win, xw.bufw, xw.bufh, XDefaultDepth(xw.dpy, xw.scr));
+ /* Xft rendering context */
+ xw.xft_draw = XftDrawCreate(xw.dpy, xw.buf, xw.vis, xw.cmap);
/* input methods */
xw.xim = XOpenIM(xw.dpy, NULL, NULL, NULL);
_AT_@ -1761,23 +1754,25 @@
void
xdraws(char *s, Glyph base, int x, int y, int charlen, int bytelen) {
- ulong xfg = dc.col[base.fg], xbg = dc.col[base.bg], temp;
+ int fg = base.fg, bg = base.bg, temp;
int winx = x*xw.cw, winy = y*xw.ch + dc.font.ascent, width = charlen*xw.cw;
+ Font *font = base.mode & ATTR_BOLD ? &dc.bfont : &dc.font;
+ XGlyphInfo extents;
int i;
/* only switch default fg/bg if term is in RV mode */
if(IS_SET(MODE_REVERSE)) {
- if(base.fg == DefaultFG)
- xfg = dc.col[DefaultBG];
- if(base.bg == DefaultBG)
- xbg = dc.col[DefaultFG];
+ if(fg == DefaultFG)
+ fg = DefaultBG;
+ if(bg == DefaultBG)
+ bg = DefaultFG;
}
if(base.mode & ATTR_REVERSE)
- temp = xfg, xfg = xbg, xbg = temp;
+ temp = fg, fg = bg, bg = temp;
- XSetBackground(xw.dpy, dc.gc, xbg);
- XSetForeground(xw.dpy, dc.gc, xfg);
+ XSetBackground(xw.dpy, dc.gc, dc.col[bg]);
+ XSetForeground(xw.dpy, dc.gc, dc.col[fg]);
if(base.mode & ATTR_GFX) {
for(i = 0; i < bytelen; i++) {
_AT_@ -1789,9 +1784,11 @@
}
}
- XmbDrawImageString(xw.dpy, xw.buf, base.mode & ATTR_BOLD ? dc.bfont.set : dc.font.set,
- dc.gc, winx, winy, s, bytelen);
-
+ XftTextExtentsUtf8(xw.dpy, font->xft_set, (FcChar8 *)s, bytelen, &extents);
+ width = extents.xOff;
+ XftDrawRect(xw.xft_draw, &dc.xft_col[bg], winx, winy - font->ascent, width, xw.ch);
+ XftDrawStringUtf8(xw.xft_draw, &dc.xft_col[fg], font->xft_set, winx, winy, (FcChar8 *)s, bytelen);
+
if(base.mode & ATTR_UNDERLINE)
XDrawLine(xw.dpy, xw.buf, dc.gc, winx, winy+1, winx+width-1, winy+1);
}
_AT_@ -2081,6 +2078,8 @@
for(i = 1; i < argc; i++) {
switch(argv[i][0] != '-' || argv[i][2] ? -1 : argv[i][1]) {
+ case 'f':
+ if(++i < argc) opt_font = argv[i];
case 't':
if(++i < argc) opt_title = argv[i];
break;
Received on Mon Jan 23 2012 - 20:31:45 CET
This archive was generated by hypermail 2.3.0
: Mon Jan 23 2012 - 20:36:05 CET