]>
Dogcows Code - chaz/openbox/blob - render/font.c
1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
3 font.c for the Openbox window manager
4 Copyright (c) 2003 Ben Jansens
5 Copyright (c) 2003 Derek Foreman
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 See the COPYING file for a copy of the GNU General Public License.
26 #include <X11/Xft/Xft.h>
32 #define ELIPSES_LENGTH(font) \
33 (font->elipses_length + (font->shadow ? font->offset : 0))
35 #define OB_SHADOW "shadow"
36 #define OB_SHADOW_OFFSET "shadowoffset"
37 #define OB_SHADOW_ALPHA "shadowtint"
39 FcObjectType objs
[] = {
40 { OB_SHADOW
, FcTypeBool
},
41 { OB_SHADOW_OFFSET
, FcTypeInteger
},
42 { OB_SHADOW_ALPHA
, FcTypeInteger
}
45 static gboolean started
= FALSE
;
47 static void font_startup(void)
50 g_warning(_("Couldn't initialize Xft."));
56 #endif /* USE_PANGO */
57 /* Here we are teaching xft about the shadow, shadowoffset & shadowtint */
58 FcNameRegisterObjectTypes(objs
, (sizeof(objs
) / sizeof(objs
[0])));
61 static void measure_font(RrFont
*f
)
63 /* xOff, yOff is the normal spacing to the next glyph. */
66 /* measure an elipses */
67 XftTextExtentsUtf8(RrDisplay(f
->inst
), f
->xftfont
,
68 (FcChar8
*)ELIPSES
, strlen(ELIPSES
), &info
);
69 f
->elipses_length
= (signed) info
.xOff
;
72 static RrFont
*openfont(const RrInstance
*inst
, gchar
*fontstring
)
74 /* This function is called for each font in the theme file. */
75 /* It returns a pointer to a RrFont struct after filling it. */
77 FcPattern
*pat
, *match
;
82 guchar
*tmp_string
= NULL
;
84 #endif /* USE_PANGO */
86 if (!(pat
= XftNameParse(fontstring
)))
89 match
= XftFontMatch(RrDisplay(inst
), RrScreen(inst
), pat
, &res
);
90 FcPatternDestroy(pat
);
94 out
= g_new(RrFont
, 1);
97 /* printf("\n\n%s\n\n",fontstring);
98 FcPatternPrint(match); */
100 out
->pango_font_description
= pango_font_description_new();
102 if (FcPatternGetString(match
, "family", 0, &tmp_string
) != FcResultTypeMismatch
) {
103 pango_font_description_set_family(out
->pango_font_description
, (gchar
*)tmp_string
);
106 if (FcPatternGetString(match
, "style", 0, &tmp_string
) != FcResultTypeMismatch
) {
108 if (!strcasecmp("bold", (gchar
*)tmp_string
)) {
109 pango_font_description_set_weight(out
->pango_font_description
, PANGO_WEIGHT_BOLD
);
112 else if (!strcasecmp("italic", (gchar
*)tmp_string
)) {
113 pango_font_description_set_style(out
->pango_font_description
, PANGO_STYLE_ITALIC
);
118 if (FcPatternGetInteger(match
, "pixelsize", 0, &tmp_int
) != FcResultTypeMismatch
) {
119 /* TODO: is PANGO_SCALE correct ?? */
120 pango_font_description_set_size(out
->pango_font_description
, tmp_int
*PANGO_SCALE
);
122 #endif /* USE_PANGO */
124 if (FcPatternGetBool(match
, OB_SHADOW
, 0, &out
->shadow
) != FcResultMatch
)
127 if (FcPatternGetInteger(match
, OB_SHADOW_OFFSET
, 0, &out
->offset
) !=
131 if (FcPatternGetInteger(match
, OB_SHADOW_ALPHA
, 0, &tint
) != FcResultMatch
)
133 if (tint
> 100) tint
= 100;
134 else if (tint
< -100) tint
= -100;
137 font
= XftFontOpenPattern(RrDisplay(inst
), match
);
139 FcPatternDestroy(match
);
146 /* FcPatternDestroy(match); */
147 #endif /* USE_PANGO */
153 RrFont
*RrFontOpen(const RrInstance
*inst
, gchar
*fontstring
)
162 if ((out
= openfont(inst
, fontstring
)))
164 g_warning(_("Unable to load font: %s\n"), fontstring
);
165 g_warning(_("Trying fallback font: %s\n"), "sans");
167 if ((out
= openfont(inst
, "sans")))
169 g_warning(_("Unable to load font: %s\n"), "sans");
174 void RrFontClose(RrFont
*f
)
177 XftFontClose(RrDisplay(f
->inst
), f
->xftfont
);
182 static void font_measure_full(const RrFont
*f
, const gchar
*str
,
186 PangoContext
*context
;
189 context
= pango_xft_get_context (RrDisplay(f
->inst
), RrScreen(f
->inst
));
190 pl
= pango_layout_new (context
);
191 pango_layout_set_text(pl
, str
, -1);
192 pango_layout_set_font_description(pl
, f
->pango_font_description
);
193 pango_layout_set_single_paragraph_mode(pl
, TRUE
);
194 pango_layout_get_pixel_extents(pl
, NULL
, &rect
);
195 *x
= rect
.width
+ (f
->shadow
? ABS(f
->offset
) : 0);
196 *y
= rect
.height
+ (f
->shadow
? ABS(f
->offset
) : 0);
198 g_object_unref(context
);
203 XftTextExtentsUtf8(RrDisplay(f
->inst
), f
->xftfont
,
204 (const FcChar8
*)str
, strlen(str
), &info
);
206 *x
= (signed) info
.xOff
+ (f
->shadow
? ABS(f
->offset
) : 0);
207 *y
= info
.height
+ (f
->shadow
? ABS(f
->offset
) : 0);
208 #endif /* USE_PANGO */
211 gint
RrFontMeasureString(const RrFont
*f
, const gchar
*str
)
214 font_measure_full (f
, str
, &x
, &y
);
218 gint
RrFontHeight(const RrFont
*f
)
221 return f
->xftfont
->ascent
+ f
->xftfont
->descent
+
222 (f
->shadow
? f
->offset
: 0);
223 #else /* USE_PANGO */
225 PangoContext *context = pango_context_new ();
227 PangoFontMetrics *metrics = pango_context_get_metrics(context, f->pango_font, NULL);
229 gint result = pango_font_metrics_get_ascent (metrics) +
230 pango_font_metrics_get_descent(metrics) +
231 (f->shadow ? f->offset : 0);
233 pango_font_metrics_unref(metrics);
234 g_object_unref(context);
237 #ifndef ANNOYING_QUESTION
238 // Obviously you either remove this or pass -DANNOYING_QUESTION to actually
240 #error XXX Does anyone have any idea how the above is supposed to work?
242 #warning XXX Using very ugly workaround in the meantime.
246 font_measure_full(f
, " ", &x
, &y
);
249 #endif /* USE_PANGO */
252 gint
RrFontMaxCharWidth(const RrFont
*f
)
254 return (signed) f
->xftfont
->max_advance_width
;
257 void RrFontDraw(XftDraw
*d
, RrTextureText
*t
, RrRect
*area
)
265 gboolean shortened
= FALSE
;
268 PangoContext
*context
;
270 context
= pango_xft_get_context (RrDisplay(t
->font
->inst
), RrScreen(t
->font
->inst
));
271 pl
= pango_layout_new (context
);
272 #endif /* USE_PANGO */
274 /* center vertically */
276 (area
->height
- RrFontHeight(t
->font
)) / 2;
277 /* the +2 and -4 leave a small blank edge on the sides */
282 text
= g_string_new(t
->string
);
284 l
= g_utf8_strlen(text
->str
, -1);
285 font_measure_full(t
->font
, text
->str
, &mw
, &mh
);
286 while (l
&& mw
> area
->width
) {
288 /* remove a character from the middle */
289 text
= g_string_erase(text
, l
-- / 2, 1);
290 /* if the elipses are too large, don't show them at all */
291 if (ELIPSES_LENGTH(t
->font
) > area
->width
)
293 font_measure_full(t
->font
, text
->str
, &mw
, &mh
);
294 mw
+= ELIPSES_LENGTH(t
->font
);
297 text
= g_string_insert(text
, (l
+ 1) / 2, ELIPSES
);
302 switch (t
->justify
) {
303 case RR_JUSTIFY_LEFT
:
305 case RR_JUSTIFY_RIGHT
:
308 case RR_JUSTIFY_CENTER
:
313 l
= strlen(text
->str
); /* number of bytes */
316 pango_layout_set_text(pl
, text
->str
, -1);
317 pango_layout_set_font_description(pl
, t
->font
->pango_font_description
);
318 pango_layout_set_single_paragraph_mode(pl
, TRUE
);
319 pango_layout_set_width(pl
, w
* PANGO_SCALE
);
320 pango_layout_set_ellipsize(pl
, PANGO_ELLIPSIZE_MIDDLE
);
321 pango_layout_set_alignment(pl
, (PangoAlignment
)(t
->justify
));
322 #endif /* USE_PANGO */
324 if (t
->font
->shadow
) {
325 if (t
->font
->tint
>= 0) {
329 c
.color
.alpha
= 0xffff * t
->font
->tint
/ 100;
330 c
.pixel
= BlackPixel(RrDisplay(t
->font
->inst
),
331 RrScreen(t
->font
->inst
));
333 c
.color
.red
= 0xffff;
334 c
.color
.green
= 0xffff;
335 c
.color
.blue
= 0xffff;
336 c
.color
.alpha
= 0xffff * -t
->font
->tint
/ 100;
337 c
.pixel
= WhitePixel(RrDisplay(t
->font
->inst
),
338 RrScreen(t
->font
->inst
));
341 XftDrawStringUtf8(d
, &c
, t
->font
->xftfont
, x
+ t
->font
->offset
,
342 t
->font
->xftfont
->ascent
+ y
+ t
->font
->offset
,
343 (FcChar8
*)text
->str
, l
);
345 #else /* USE_PANGO */
347 pango_xft_render_layout(d
, &c
, pl
, (x
+ t
->font
->offset
) * PANGO_SCALE
,
348 (y
+ t
->font
->offset
) * PANGO_SCALE
);
350 #endif /* USE_PANGO */
351 c
.color
.red
= t
->color
->r
| t
->color
->r
<< 8;
352 c
.color
.green
= t
->color
->g
| t
->color
->g
<< 8;
353 c
.color
.blue
= t
->color
->b
| t
->color
->b
<< 8;
354 c
.color
.alpha
= 0xff | 0xff << 8; /* fully opaque text */
355 c
.pixel
= t
->color
->pixel
;
358 XftDrawStringUtf8(d
, &c
, t
->font
->xftfont
, x
,
359 t
->font
->xftfont
->ascent
+ y
,
360 (FcChar8
*)text
->str
, l
);
361 #else /* USE_PANGO */
362 pango_xft_render_layout(d
, &c
, pl
, x
* PANGO_SCALE
, y
* PANGO_SCALE
);
364 g_object_unref(context
);
367 g_string_free(text
, TRUE
);
This page took 0.057481 seconds and 5 git commands to generate.