2 * Copyright (C) 2006-2008 Kim Woelders
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to
6 * deal in the Software without restriction, including without limitation the
7 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
8 * sell copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies of the Software, its documentation and marketing & publicity
13 * materials, and acknowledgment shall be given in the documentation, materials
14 * and software packages that this Software was used.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
20 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 #include <X11/extensions/Xrender.h>
29 #include <X11/Xft/Xft.h>
34 __EXPORT__ extern const FontOps FontOps_xft;
45 _xft_Load(TextState * ts, const char *name)
51 font = XftFontOpenXlfd(disp, Dpy.screen, name);
53 font = XftFontOpenName(disp, Dpy.screen, name);
60 FT_Face ftf = XftLockFace(font);
64 Eprintf("Font %s family_name=%s style_name=%s\n", name,
65 ftf->family_name, ftf->style_name);
70 fdc = EMALLOC(FontCtxXft, 1);
76 ts->type = FONT_TYPE_XFT;
77 ts->ops = &FontOps_xft;
82 _xft_Unload(TextState * ts)
84 FontCtxXft *fdc = (FontCtxXft *) ts->fdc;
86 XftFontClose(disp, fdc->font);
90 _xft_TextSize(TextState * ts, const char *text, int len,
91 int *width, int *height, int *ascent)
93 FontCtxXft *fdc = (FontCtxXft *) ts->fdc;
98 XftTextExtentsUtf8(disp, fdc->font, (const XftChar8 *)text, len, &gi);
100 *height = fdc->font->height;
101 if (*height < fdc->font->ascent + fdc->font->descent)
102 *height = fdc->font->ascent + fdc->font->descent;
103 *ascent = fdc->font->ascent;
105 Eprintf("asc/dsc/h=%d/%d/%d x,y=%2d,%d wxh=%dx%d ox,y=%3d,%d: (%d)%s\n",
106 fdc->font->ascent, fdc->font->descent, fdc->font->height, gi.x, gi.y,
107 gi.width, gi.height, gi.xOff, gi.yOff, len, text);
112 _xft_TextDraw(TextState * ts, int x, int y, const char *text, int len)
114 FontCtxXft *fdc = (FontCtxXft *) ts->fdc;
116 XftDrawStringUtf8(fdc->xftd, &(fdc->xftc), fdc->font, x, y,
117 (const XftChar8 *)text, len);
121 _xft_FdcInit(TextState * ts, Win win, Drawable draw)
123 FontCtxXft *fdc = (FontCtxXft *) ts->fdc;
128 fdc->xftd = XftDrawCreate(disp, draw, WinGetVisual(win), WinGetCmap(win));
135 _xft_FdcFini(TextState * ts)
137 FontCtxXft *fdc = (FontCtxXft *) ts->fdc;
139 XftDrawDestroy(fdc->xftd);
143 _xft_FdcSetDrawable(TextState * ts, unsigned long draw)
145 FontCtxXft *fdc = (FontCtxXft *) ts->fdc;
147 if (fdc->draw == draw)
150 XftDrawChange(fdc->xftd, draw);
154 _xft_FdcSetColor(TextState * ts, EColor * ec)
156 FontCtxXft *fdc = (FontCtxXft *) ts->fdc;
159 xrc.red = ec->red << 8;
160 xrc.green = ec->green << 8;
161 xrc.blue = ec->blue << 8;
164 XftColorAllocValue(disp, WinGetVisual(fdc->win), WinGetCmap(fdc->win),
168 const FontOps FontOps_xft = {
169 _xft_Load, _xft_Unload, _xft_TextSize, TextstateTextFit, _xft_TextDraw,
170 _xft_FdcInit, _xft_FdcFini, _xft_FdcSetDrawable, _xft_FdcSetColor
173 #endif /* FONT_TYPE_XFT */