]>
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 them until we shut down anyway */
59 context
= pango_xft_get_context(RrDisplay(NULL
), RrScreen(NULL
));
60 #endif /* USE_PANGO */
61 /* Here we are teaching xft about the shadow, shadowoffset & shadowtint */
62 FcNameRegisterObjectTypes(objs
, (sizeof(objs
) / sizeof(objs
[0])));
65 static void measure_font(RrFont
*f
)
67 /* xOff, yOff is the normal spacing to the next glyph. */
70 /* measure an elipses */
71 XftTextExtentsUtf8(RrDisplay(f
->inst
), f
->xftfont
,
72 (FcChar8
*)ELIPSES
, strlen(ELIPSES
), &info
);
73 f
->elipses_length
= (signed) info
.xOff
;
76 static RrFont
*openfont(const RrInstance
*inst
, gchar
*fontstring
)
78 /* This function is called for each font in the theme file. */
79 /* It returns a pointer to a RrFont struct after filling it. */
81 FcPattern
*pat
, *match
;
86 guchar
*tmp_string
= NULL
;
88 #endif /* USE_PANGO */
90 if (!(pat
= XftNameParse(fontstring
)))
93 match
= XftFontMatch(RrDisplay(inst
), RrScreen(inst
), pat
, &res
);
94 FcPatternDestroy(pat
);
98 out
= g_new(RrFont
, 1);
101 /* printf("\n\n%s\n\n",fontstring);
102 FcPatternPrint(match); */
104 out
->pango_font_description
= pango_font_description_new();
106 if (FcPatternGetString(match
, "family", 0, &tmp_string
) != FcResultTypeMismatch
) {
107 pango_font_description_set_family(out
->pango_font_description
, (gchar
*)tmp_string
);
110 if (FcPatternGetString(match
, "style", 0, &tmp_string
) != FcResultTypeMismatch
) {
112 if (!strcasecmp("bold", (gchar
*)tmp_string
)) {
113 pango_font_description_set_weight(out
->pango_font_description
, PANGO_WEIGHT_BOLD
);
116 else if (!strcasecmp("italic", (gchar
*)tmp_string
)) {
117 pango_font_description_set_style(out
->pango_font_description
, PANGO_STYLE_ITALIC
);
122 if (FcPatternGetInteger(match
, "pixelsize", 0, &tmp_int
) != FcResultTypeMismatch
) {
123 /* TODO: is PANGO_SCALE correct ?? */
124 pango_font_description_set_size(out
->pango_font_description
, tmp_int
*PANGO_SCALE
);
127 /* based on gtkmain.c gtk_get_default_language() */
130 locale
= g_strdup(setlocale(LC_CTYPE
, NULL
));
131 if ((p
= strchr(locale
, '.')))
133 if ((p
= strchr(locale
, '@')))
135 printf("%s\n", locale
);
136 PangoFontMetrics
*metrics
= pango_context_get_metrics(context
, out
->pango_font_description
, ln
= pango_language_from_string(locale
));
137 out
->pango_ascent
= pango_font_metrics_get_ascent(metrics
);
138 out
->pango_descent
= pango_font_metrics_get_descent(metrics
);
140 pango_font_metrics_unref(metrics
);
141 #endif /* USE_PANGO */
143 if (FcPatternGetBool(match
, OB_SHADOW
, 0, &out
->shadow
) != FcResultMatch
)
146 if (FcPatternGetInteger(match
, OB_SHADOW_OFFSET
, 0, &out
->offset
) !=
150 if (FcPatternGetInteger(match
, OB_SHADOW_ALPHA
, 0, &tint
) != FcResultMatch
)
152 if (tint
> 100) tint
= 100;
153 else if (tint
< -100) tint
= -100;
156 font
= XftFontOpenPattern(RrDisplay(inst
), match
);
158 FcPatternDestroy(match
);
165 /* FcPatternDestroy(match); */
166 #endif /* USE_PANGO */
172 RrFont
*RrFontOpen(const RrInstance
*inst
, gchar
*fontstring
)
181 if ((out
= openfont(inst
, fontstring
)))
183 g_warning(_("Unable to load font: %s\n"), fontstring
);
184 g_warning(_("Trying fallback font: %s\n"), "sans");
186 if ((out
= openfont(inst
, "sans")))
188 g_warning(_("Unable to load font: %s\n"), "sans");
193 void RrFontClose(RrFont
*f
)
196 XftFontClose(RrDisplay(f
->inst
), f
->xftfont
);
200 pango_font_description_free(f
->pango_font_description
);
204 static void font_measure_full(const RrFont
*f
, const gchar
*str
,
210 pl
= pango_layout_new (context
);
211 pango_layout_set_text(pl
, str
, -1);
212 pango_layout_set_font_description(pl
, f
->pango_font_description
);
213 pango_layout_set_single_paragraph_mode(pl
, TRUE
);
214 pango_layout_get_pixel_extents(pl
, NULL
, &rect
);
215 *x
= rect
.width
+ (f
->shadow
? ABS(f
->offset
) : 0);
216 *y
= rect
.height
+ (f
->shadow
? ABS(f
->offset
) : 0);
222 XftTextExtentsUtf8(RrDisplay(f
->inst
), f
->xftfont
,
223 (const FcChar8
*)str
, strlen(str
), &info
);
225 *x
= (signed) info
.xOff
+ (f
->shadow
? ABS(f
->offset
) : 0);
226 *y
= info
.height
+ (f
->shadow
? ABS(f
->offset
) : 0);
227 #endif /* USE_PANGO */
230 RrSize
*RrFontMeasureString(const RrFont
*f
, const gchar
*str
)
233 size
= g_new(RrSize
, 1);
234 font_measure_full (f
, str
, &size
->width
, &size
->height
);
238 gint
RrFontHeight(const RrFont
*f
)
241 return (f
->pango_ascent
244 (f
->shadow
? f
->offset
: 0);
246 return f
->xftfont
->ascent
+ f
->xftfont
->descent
+
247 (f
->shadow
? f
->offset
: 0);
251 gint
RrFontMaxCharWidth(const RrFont
*f
)
253 return (signed) f
->xftfont
->max_advance_width
;
257 static inline int font_calculate_baseline(RrFont
*f
, gint height
)
259 /* For my own reference:
261 * ^space/2 ^height ^baseline
264 * | | | | |_ _____ _| |_ _ _
265 * | | | | _/ -_) \ / _| || |
266 * | v_________v \__\___/_\_\\__|\_, |
272 int asc
= f
->pango_ascent
;
273 int ascdesc
= asc
+ f
->pango_descent
;
274 int space
= height
* PANGO_SCALE
- ascdesc
;
275 int baseline
= space
/ 2 + asc
;
276 return baseline
/ PANGO_SCALE
;
280 void RrFontDraw(XftDraw
*d
, RrTextureText
*t
, RrRect
*area
)
288 gboolean shortened
= FALSE
;
293 pl
= pango_layout_new (context
);
294 #endif /* USE_PANGO */
297 * for xft we pass the top edge of the text for positioning... */
300 (area
->height
- RrFontHeight(t
->font
)) / 2;
302 /* but for pango we pass the baseline, since different fonts have
303 * different top edges. It looks stupid when the baseline of "normal"
304 * text jumps up and down when a "strange" character is just added
305 * to the end of the text */
307 font_calculate_baseline(t
->font
, area
->height
);
309 /* the +2 and -4 leave a small blank edge on the sides */
314 text
= g_string_new(t
->string
);
316 l
= g_utf8_strlen(text
->str
, -1);
317 font_measure_full(t
->font
, text
->str
, &mw
, &mh
);
318 while (l
&& mw
> area
->width
) {
320 /* remove a character from the middle */
321 text
= g_string_erase(text
, l
-- / 2, 1);
322 /* if the elipses are too large, don't show them at all */
323 if (ELIPSES_LENGTH(t
->font
) > area
->width
)
325 font_measure_full(t
->font
, text
->str
, &mw
, &mh
);
326 mw
+= ELIPSES_LENGTH(t
->font
);
329 text
= g_string_insert(text
, (l
+ 1) / 2, ELIPSES
);
334 l
= strlen(text
->str
); /* number of bytes */
337 pango_layout_set_text(pl
, text
->str
, -1);
338 pango_layout_set_font_description(pl
, t
->font
->pango_font_description
);
339 pango_layout_set_single_paragraph_mode(pl
, TRUE
);
340 pango_layout_set_width(pl
, w
* PANGO_SCALE
);
341 pango_layout_set_ellipsize(pl
, PANGO_ELLIPSIZE_MIDDLE
);
342 /* This doesn't work with layout_line() of course */
343 /* pango_layout_set_alignment(pl, (PangoAlignment)(t->justify)); */
344 pango_layout_get_pixel_extents(pl
, NULL
, &rect
);
347 #endif /* USE_PANGO */
349 switch (t
->justify
) {
350 case RR_JUSTIFY_LEFT
:
352 case RR_JUSTIFY_RIGHT
:
355 case RR_JUSTIFY_CENTER
:
360 if (t
->font
->shadow
) {
361 if (t
->font
->tint
>= 0) {
365 c
.color
.alpha
= 0xffff * t
->font
->tint
/ 100;
366 c
.pixel
= BlackPixel(RrDisplay(t
->font
->inst
),
367 RrScreen(t
->font
->inst
));
369 c
.color
.red
= 0xffff;
370 c
.color
.green
= 0xffff;
371 c
.color
.blue
= 0xffff;
372 c
.color
.alpha
= 0xffff * -t
->font
->tint
/ 100;
373 c
.pixel
= WhitePixel(RrDisplay(t
->font
->inst
),
374 RrScreen(t
->font
->inst
));
377 XftDrawStringUtf8(d
, &c
, t
->font
->xftfont
, x
+ t
->font
->offset
,
378 t
->font
->xftfont
->ascent
+ y
+ t
->font
->offset
,
379 (FcChar8
*)text
->str
, l
);
380 #else /* USE_PANGO */
382 pango_xft_render_layout_line(d
, &c
, pango_layout_get_line(pl
, 0),
383 (x
+ t
->font
->offset
) * PANGO_SCALE
,
384 (y
+ t
->font
->offset
) * PANGO_SCALE
);
385 #endif /* USE_PANGO */
387 c
.color
.red
= t
->color
->r
| t
->color
->r
<< 8;
388 c
.color
.green
= t
->color
->g
| t
->color
->g
<< 8;
389 c
.color
.blue
= t
->color
->b
| t
->color
->b
<< 8;
390 c
.color
.alpha
= 0xff | 0xff << 8; /* fully opaque text */
391 c
.pixel
= t
->color
->pixel
;
394 XftDrawStringUtf8(d
, &c
, t
->font
->xftfont
, x
,
395 t
->font
->xftfont
->ascent
+ y
,
396 (FcChar8
*)text
->str
, l
);
397 #else /* USE_PANGO */
398 /* This looks retarded, but layout_line() bases y on the baseline, while
399 * layout() bases y on the top of the ink layout shit ass fucking crap.
400 * We want the baseline to always be in the same place, thusly, we use
402 * The actual line doesn't need to be freed */
403 pango_xft_render_layout_line(d
, &c
, pango_layout_get_line(pl
, 0),
404 x
* PANGO_SCALE
, y
* PANGO_SCALE
);
408 g_string_free(text
, TRUE
);
This page took 0.057182 seconds and 4 git commands to generate.