2 * Copyright (C) 2007-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/Xft/Xft.h>
29 #include <pango/pangoxft.h>
34 __EXPORT__ extern const FontOps FontOps_pango;
36 static PangoContext *_pango_ctx = NULL;
38 /* Beware! The layout of FontCtxPangoXft must match FontCtxXft
39 * in order to reuse the _xft_Fdc... functions. */
41 PangoFontDescription *font;
49 _pango_xft_Load(TextState * ts, const char *name)
52 PangoFontDescription *font;
56 _pango_ctx = pango_xft_get_context(disp, Dpy.screen);
60 font = pango_font_description_from_string(name);
64 flags = pango_font_description_get_set_fields(font);
65 if ((flags & PANGO_FONT_MASK_FAMILY) == 0)
66 pango_font_description_set_family(font, "sans");
67 if ((flags & PANGO_FONT_MASK_SIZE) == 0)
68 pango_font_description_set_size(font, 10 * PANGO_SCALE);
70 fdc = EMALLOC(FontCtxPangoXft, 1);
76 ts->type = FONT_TYPE_PANGO_XFT;
77 ts->ops = &FontOps_pango;
82 _pango_xft_Unload(TextState * ts)
84 FontCtxPangoXft *fdc = (FontCtxPangoXft *) ts->fdc;
86 pango_font_description_free(fdc->font);
90 _pango_xft_TextSize(TextState * ts, const char *text, int len __UNUSED__,
91 int *width, int *height, int *ascent)
93 FontCtxPangoXft *fdc = (FontCtxPangoXft *) ts->fdc;
95 PangoRectangle logical_rect;
97 layout = pango_layout_new(_pango_ctx);
98 pango_layout_set_text(layout, text, -1);
99 pango_layout_set_font_description(layout, fdc->font);
100 pango_layout_get_extents(layout, NULL, &logical_rect);
102 *width = PANGO_PIXELS(logical_rect.x + logical_rect.width);
103 *height = PANGO_PIXELS(logical_rect.height);
104 *ascent = PANGO_PIXELS(-logical_rect.y);
106 g_object_unref(layout);
110 _pango_xft_TextDraw(TextState * ts, int x, int y, const char *text,
113 FontCtxPangoXft *fdc = (FontCtxPangoXft *) ts->fdc;
116 layout = pango_layout_new(_pango_ctx);
117 pango_layout_set_text(layout, text, -1);
118 pango_layout_set_font_description(layout, fdc->font);
120 pango_xft_render_layout(fdc->xftd, &(fdc->xftc), layout,
121 x * PANGO_SCALE, y * PANGO_SCALE);
123 g_object_unref(layout);
126 const FontOps FontOps_pango = {
127 _pango_xft_Load, _pango_xft_Unload,
128 _pango_xft_TextSize, TextstateTextFit, _pango_xft_TextDraw,
129 _xft_FdcInit, _xft_FdcFini, _xft_FdcSetDrawable, _xft_FdcSetColor
132 #endif /* USE_PANGO */