chiark / gitweb /
Imported Upstream version 1.0.0
[e16] / src / text_pango.c
1 /*
2  * Copyright (C) 2007-2008 Kim Woelders
3  *
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:
10  *
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.
15  *
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.
22  */
23 #include "E.h"
24 #include "tclass.h"
25
26 #ifdef USE_PANGO
27 #include "xwin.h"
28 #include <X11/Xft/Xft.h>
29 #include <pango/pangoxft.h>
30
31 /*
32  * Pango-Xft
33  */
34 __EXPORT__ extern const FontOps FontOps_pango;
35
36 static PangoContext *_pango_ctx = NULL;
37
38 /* Beware! The layout of FontCtxPangoXft must match FontCtxXft
39  * in order to reuse the _xft_Fdc... functions. */
40 typedef struct {
41    PangoFontDescription *font;
42    Win                 win;
43    Drawable            draw;
44    XftDraw            *xftd;
45    XftColor            xftc;
46 } FontCtxPangoXft;
47
48 static int
49 _pango_xft_Load(TextState * ts, const char *name)
50 {
51    FontCtxPangoXft    *fdc;
52    PangoFontDescription *font;
53    PangoFontMask       flags;
54
55    if (!_pango_ctx)
56       _pango_ctx = pango_xft_get_context(disp, Dpy.screen);
57    if (!_pango_ctx)
58       return -1;
59
60    font = pango_font_description_from_string(name);
61    if (!font)
62       return -1;
63
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);
69
70    fdc = EMALLOC(FontCtxPangoXft, 1);
71    if (!fdc)
72       return -1;
73    fdc->font = font;
74    ts->fdc = fdc;
75    ts->need_utf8 = 1;
76    ts->type = FONT_TYPE_PANGO_XFT;
77    ts->ops = &FontOps_pango;
78    return 0;
79 }
80
81 static void
82 _pango_xft_Unload(TextState * ts)
83 {
84    FontCtxPangoXft    *fdc = (FontCtxPangoXft *) ts->fdc;
85
86    pango_font_description_free(fdc->font);
87 }
88
89 static void
90 _pango_xft_TextSize(TextState * ts, const char *text, int len __UNUSED__,
91                     int *width, int *height, int *ascent)
92 {
93    FontCtxPangoXft    *fdc = (FontCtxPangoXft *) ts->fdc;
94    PangoLayout        *layout;
95    PangoRectangle      logical_rect;
96
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);
101
102    *width = PANGO_PIXELS(logical_rect.x + logical_rect.width);
103    *height = PANGO_PIXELS(logical_rect.height);
104    *ascent = PANGO_PIXELS(-logical_rect.y);
105
106    g_object_unref(layout);
107 }
108
109 static void
110 _pango_xft_TextDraw(TextState * ts, int x, int y, const char *text,
111                     int len __UNUSED__)
112 {
113    FontCtxPangoXft    *fdc = (FontCtxPangoXft *) ts->fdc;
114    PangoLayout        *layout;
115
116    layout = pango_layout_new(_pango_ctx);
117    pango_layout_set_text(layout, text, -1);
118    pango_layout_set_font_description(layout, fdc->font);
119
120    pango_xft_render_layout(fdc->xftd, &(fdc->xftc), layout,
121                            x * PANGO_SCALE, y * PANGO_SCALE);
122
123    g_object_unref(layout);
124 }
125
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
130 };
131
132 #endif /* USE_PANGO */