]>
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.
27 #include <X11/Xft/Xft.h>
33 #define ELIPSES_LENGTH(font) \
34 (font->elipses_length + (font->shadow ? font->offset : 0))
36 #define OB_SHADOW "shadow"
37 #define OB_SHADOW_OFFSET "shadowoffset"
38 #define OB_SHADOW_ALPHA "shadowtint"
40 FcObjectType objs
[] = {
41 { OB_SHADOW
, FcTypeBool
},
42 { OB_SHADOW_OFFSET
, FcTypeInteger
},
43 { OB_SHADOW_ALPHA
, FcTypeInteger
}
46 static PangoContext
*context
;
47 static gboolean started
= FALSE
;
49 static void font_startup(void)
52 g_warning(_("Couldn't initialize Xft."));
58 /* these will never be freed, but we will need
59 * them until we shut down anyway */
60 context
= pango_xft_get_context(RrDisplay(NULL
), RrScreen(NULL
));
61 #endif /* USE_PANGO */
62 /* Here we are teaching xft about the shadow, shadowoffset & shadowtint */
63 FcNameRegisterObjectTypes(objs
, (sizeof(objs
) / sizeof(objs
[0])));
66 static void measure_font(RrFont
*f
)
68 /* xOff, yOff is the normal spacing to the next glyph. */
71 /* measure an elipses */
72 XftTextExtentsUtf8(RrDisplay(f
->inst
), f
->xftfont
,
73 (FcChar8
*)ELIPSES
, strlen(ELIPSES
), &info
);
74 f
->elipses_length
= (signed) info
.xOff
;
77 static RrFont
*openfont(const RrInstance
*inst
, gchar
*fontstring
)
79 /* This function is called for each font in the theme file. */
80 /* It returns a pointer to a RrFont struct after filling it. */
82 FcPattern
*pat
, *match
;
87 guchar
*tmp_string
= NULL
;
89 #endif /* USE_PANGO */
91 if (!(pat
= XftNameParse(fontstring
)))
94 match
= XftFontMatch(RrDisplay(inst
), RrScreen(inst
), pat
, &res
);
95 FcPatternDestroy(pat
);
99 out
= g_new(RrFont
, 1);
102 out
->pango_font_description
= pango_font_description_new();
104 if (FcPatternGetString(match
, "family", 0, &tmp_string
) !=
105 FcResultTypeMismatch
) {
106 pango_font_description_set_family(out
->pango_font_description
,
107 (gchar
*)tmp_string
);
110 if (FcPatternGetString(match
, "style", 0, &tmp_string
) !=
111 FcResultTypeMismatch
) {
113 if (!strcasecmp("bold", (gchar
*)tmp_string
)) {
114 pango_font_description_set_weight(out
->pango_font_description
,
118 else if (!strcasecmp("italic", (gchar
*)tmp_string
)) {
119 pango_font_description_set_style(out
->pango_font_description
,
125 if (FcPatternGetInteger(match
, "pixelsize", 0, &tmp_int
) !=
126 FcResultTypeMismatch
) {
127 /* TODO: is PANGO_SCALE correct ?? */
128 pango_font_description_set_size(out
->pango_font_description
,
129 tmp_int
*PANGO_SCALE
);
132 /* based on gtkmain.c gtk_get_default_language() */
135 locale
= g_strdup(setlocale(LC_CTYPE
, NULL
));
136 if ((p
= strchr(locale
, '.')))
138 if ((p
= strchr(locale
, '@')))
140 PangoFontMetrics
*metrics
=
141 pango_context_get_metrics(context
, out
->pango_font_description
,
142 ln
= pango_language_from_string(locale
));
143 out
->pango_ascent
= pango_font_metrics_get_ascent(metrics
);
144 out
->pango_descent
= pango_font_metrics_get_descent(metrics
);
146 pango_font_metrics_unref(metrics
);
147 #endif /* USE_PANGO */
149 if (FcPatternGetBool(match
, OB_SHADOW
, 0, &out
->shadow
) != FcResultMatch
)
152 if (FcPatternGetInteger(match
, OB_SHADOW_OFFSET
, 0, &out
->offset
) !=
156 if (FcPatternGetInteger(match
, OB_SHADOW_ALPHA
, 0, &tint
) != FcResultMatch
)
158 if (tint
> 100) tint
= 100;
159 else if (tint
< -100) tint
= -100;
162 font
= XftFontOpenPattern(RrDisplay(inst
), match
);
164 FcPatternDestroy(match
);
171 /* FcPatternDestroy(match); */
172 #endif /* USE_PANGO */
178 RrFont
*RrFontOpen(const RrInstance
*inst
, gchar
*fontstring
)
187 if ((out
= openfont(inst
, fontstring
)))
189 g_warning(_("Unable to load font: %s\n"), fontstring
);
190 g_warning(_("Trying fallback font: %s\n"), "sans");
192 if ((out
= openfont(inst
, "sans")))
194 g_warning(_("Unable to load font: %s\n"), "sans");
199 void RrFontClose(RrFont
*f
)
202 XftFontClose(RrDisplay(f
->inst
), f
->xftfont
);
206 pango_font_description_free(f
->pango_font_description
);
210 static void font_measure_full(const RrFont
*f
, const gchar
*str
,
216 pl
= pango_layout_new (context
);
217 pango_layout_set_text(pl
, str
, -1);
218 pango_layout_set_font_description(pl
, f
->pango_font_description
);
219 pango_layout_set_single_paragraph_mode(pl
, TRUE
);
220 pango_layout_get_pixel_extents(pl
, NULL
, &rect
);
221 *x
= rect
.width
+ (f
->shadow
? ABS(f
->offset
) : 0);
222 *y
= rect
.height
+ (f
->shadow
? ABS(f
->offset
) : 0);
228 XftTextExtentsUtf8(RrDisplay(f
->inst
), f
->xftfont
,
229 (const FcChar8
*)str
, strlen(str
), &info
);
231 *x
= (signed) info
.xOff
+ (f
->shadow
? ABS(f
->offset
) : 0);
232 *y
= info
.height
+ (f
->shadow
? ABS(f
->offset
) : 0);
233 #endif /* USE_PANGO */
236 RrSize
*RrFontMeasureString(const RrFont
*f
, const gchar
*str
)
239 size
= g_new(RrSize
, 1);
240 font_measure_full (f
, str
, &size
->width
, &size
->height
);
244 gint
RrFontHeight(const RrFont
*f
)
247 return (f
->pango_ascent
250 (f
->shadow
? f
->offset
: 0);
252 return f
->xftfont
->ascent
+ f
->xftfont
->descent
+
253 (f
->shadow
? f
->offset
: 0);
257 gint
RrFontMaxCharWidth(const RrFont
*f
)
259 return (signed) f
->xftfont
->max_advance_width
;
263 static inline int font_calculate_baseline(RrFont
*f
, gint height
)
265 /* For my own reference:
267 * ^space/2 ^height ^baseline
270 * | | | | |_ _____ _| |_ _ _
271 * | | | | _/ -_) \ / _| || |
272 * | v_________v \__\___/_\_\\__|\_, |
278 int asc
= f
->pango_ascent
;
279 int ascdesc
= asc
+ f
->pango_descent
;
280 int space
= height
* PANGO_SCALE
- ascdesc
;
281 int baseline
= space
/ 2 + asc
;
282 return baseline
/ PANGO_SCALE
;
286 void RrFontDraw(XftDraw
*d
, RrTextureText
*t
, RrRect
*area
)
294 gboolean shortened
= FALSE
;
299 pl
= pango_layout_new (context
);
300 #endif /* USE_PANGO */
303 * for xft we pass the top edge of the text for positioning... */
306 (area
->height
- RrFontHeight(t
->font
)) / 2;
308 /* but for pango we pass the baseline, since different fonts have
309 * different top edges. It looks stupid when the baseline of "normal"
310 * text jumps up and down when a "strange" character is just added
311 * to the end of the text */
313 font_calculate_baseline(t
->font
, area
->height
);
315 /* the +2 and -4 leave a small blank edge on the sides */
320 text
= g_string_new(t
->string
);
322 l
= g_utf8_strlen(text
->str
, -1);
323 font_measure_full(t
->font
, text
->str
, &mw
, &mh
);
324 while (l
&& mw
> area
->width
) {
326 /* remove a character from the middle */
327 text
= g_string_erase(text
, l
-- / 2, 1);
328 /* if the elipses are too large, don't show them at all */
329 if (ELIPSES_LENGTH(t
->font
) > area
->width
)
331 font_measure_full(t
->font
, text
->str
, &mw
, &mh
);
332 mw
+= ELIPSES_LENGTH(t
->font
);
335 text
= g_string_insert(text
, (l
+ 1) / 2, ELIPSES
);
340 l
= strlen(text
->str
); /* number of bytes */
343 pango_layout_set_text(pl
, text
->str
, -1);
344 pango_layout_set_font_description(pl
, t
->font
->pango_font_description
);
345 pango_layout_set_single_paragraph_mode(pl
, TRUE
);
346 pango_layout_set_width(pl
, w
* PANGO_SCALE
);
347 pango_layout_set_ellipsize(pl
, PANGO_ELLIPSIZE_MIDDLE
);
348 /* This doesn't work with layout_line() of course */
349 /* pango_layout_set_alignment(pl, (PangoAlignment)(t->justify)); */
350 pango_layout_get_pixel_extents(pl
, NULL
, &rect
);
353 #endif /* USE_PANGO */
355 switch (t
->justify
) {
356 case RR_JUSTIFY_LEFT
:
358 case RR_JUSTIFY_RIGHT
:
361 case RR_JUSTIFY_CENTER
:
366 if (t
->font
->shadow
) {
367 if (t
->font
->tint
>= 0) {
371 c
.color
.alpha
= 0xffff * t
->font
->tint
/ 100;
372 c
.pixel
= BlackPixel(RrDisplay(t
->font
->inst
),
373 RrScreen(t
->font
->inst
));
375 c
.color
.red
= 0xffff;
376 c
.color
.green
= 0xffff;
377 c
.color
.blue
= 0xffff;
378 c
.color
.alpha
= 0xffff * -t
->font
->tint
/ 100;
379 c
.pixel
= WhitePixel(RrDisplay(t
->font
->inst
),
380 RrScreen(t
->font
->inst
));
383 XftDrawStringUtf8(d
, &c
, t
->font
->xftfont
, x
+ t
->font
->offset
,
384 t
->font
->xftfont
->ascent
+ y
+ t
->font
->offset
,
385 (FcChar8
*)text
->str
, l
);
386 #else /* USE_PANGO */
388 pango_xft_render_layout_line(d
, &c
, pango_layout_get_line(pl
, 0),
389 (x
+ t
->font
->offset
) * PANGO_SCALE
,
390 (y
+ t
->font
->offset
) * PANGO_SCALE
);
391 #endif /* USE_PANGO */
393 c
.color
.red
= t
->color
->r
| t
->color
->r
<< 8;
394 c
.color
.green
= t
->color
->g
| t
->color
->g
<< 8;
395 c
.color
.blue
= t
->color
->b
| t
->color
->b
<< 8;
396 c
.color
.alpha
= 0xff | 0xff << 8; /* fully opaque text */
397 c
.pixel
= t
->color
->pixel
;
400 XftDrawStringUtf8(d
, &c
, t
->font
->xftfont
, x
,
401 t
->font
->xftfont
->ascent
+ y
,
402 (FcChar8
*)text
->str
, l
);
403 #else /* USE_PANGO */
404 /* This looks retarded, but layout_line() bases y on the baseline, while
405 * layout() bases y on the top of the ink layout shit ass fucking crap.
406 * We want the baseline to always be in the same place, thusly, we use
408 * The actual line doesn't need to be freed */
409 pango_xft_render_layout_line(d
, &c
, pango_layout_get_line(pl
, 0),
410 x
* PANGO_SCALE
, y
* PANGO_SCALE
);
414 g_string_free(text
, TRUE
);
This page took 0.062122 seconds and 5 git commands to generate.