]>
Dogcows Code - chaz/openbox/blob - render/font.c
7 #include <X11/Xft/Xft.h>
13 #define ELIPSES_LENGTH(font) \
14 (font->elipses_length + (font->shadow ? font->offset : 0))
16 #define OB_SHADOW "shadow"
17 #define OB_SHADOW_OFFSET "shadowoffset"
18 #define OB_SHADOW_ALPHA "shadowtint"
20 FcObjectType objs
[] = {
21 { OB_SHADOW
, FcTypeBool
},
22 { OB_SHADOW_OFFSET
, FcTypeInteger
},
23 { OB_SHADOW_ALPHA
, FcTypeInteger
}
26 static gboolean started
= FALSE
;
28 static void font_startup(void)
31 g_warning(_("Couldn't initialize Xft."));
34 FcNameRegisterObjectTypes(objs
, (sizeof(objs
) / sizeof(objs
[0])));
37 static void measure_font(RrFont
*f
)
41 /* measure an elipses */
42 XftTextExtentsUtf8(RrDisplay(f
->inst
), f
->xftfont
,
43 (FcChar8
*)ELIPSES
, strlen(ELIPSES
), &info
);
44 f
->elipses_length
= (signed) info
.xOff
;
47 static RrFont
*openfont(const RrInstance
*inst
, char *fontstring
)
50 FcPattern
*pat
, *match
;
55 if (!(pat
= XftNameParse(fontstring
)))
58 match
= XftFontMatch(RrDisplay(inst
), RrScreen(inst
), pat
, &res
);
59 FcPatternDestroy(pat
);
63 out
= g_new(RrFont
, 1);
66 if (FcPatternGetBool(match
, OB_SHADOW
, 0, &out
->shadow
) != FcResultMatch
)
69 if (FcPatternGetInteger(match
, OB_SHADOW_OFFSET
, 0, &out
->offset
) !=
73 if (FcPatternGetInteger(match
, OB_SHADOW_ALPHA
, 0, &tint
) != FcResultMatch
)
75 if (tint
> 100) tint
= 100;
76 else if (tint
< -100) tint
= -100;
79 font
= XftFontOpenPattern(RrDisplay(inst
), match
);
81 FcPatternDestroy(match
);
92 RrFont
*RrFontOpen(const RrInstance
*inst
, char *fontstring
)
101 if ((out
= openfont(inst
, fontstring
)))
103 g_warning(_("Unable to load font: %s\n"), fontstring
);
104 g_warning(_("Trying fallback font: %s\n"), "sans");
106 if ((out
= openfont(inst
, "sans")))
108 g_warning(_("Unable to load font: %s\n"), "sans");
113 void RrFontClose(RrFont
*f
)
116 XftFontClose(RrDisplay(f
->inst
), f
->xftfont
);
121 static void font_measure_full(const RrFont
*f
, const gchar
*str
,
126 XftTextExtentsUtf8(RrDisplay(f
->inst
), f
->xftfont
,
127 (const FcChar8
*)str
, strlen(str
), &info
);
129 *x
= (signed) info
.xOff
+ (f
->shadow
? ABS(f
->offset
) : 0);
130 *y
= info
.height
+ (f
->shadow
? ABS(f
->offset
) : 0);
133 int RrFontMeasureString(const RrFont
*f
, const gchar
*str
)
136 font_measure_full (f
, str
, &x
, &y
);
140 int RrFontHeight(const RrFont
*f
)
142 return f
->xftfont
->ascent
+ f
->xftfont
->descent
+
143 (f
->shadow
? f
->offset
: 0);
146 int RrFontMaxCharWidth(const RrFont
*f
)
148 return (signed) f
->xftfont
->max_advance_width
;
151 void RrFontDraw(XftDraw
*d
, RrTextureText
*t
, RrRect
*area
)
158 gboolean shortened
= FALSE
;
160 /* center vertically */
162 (area
->height
- RrFontHeight(t
->font
)) / 2;
163 /* the +2 and -4 leave a small blank edge on the sides */
168 text
= g_string_new(t
->string
);
169 l
= g_utf8_strlen(text
->str
, -1);
170 font_measure_full(t
->font
, text
->str
, &mw
, &mh
);
171 while (l
&& mw
> area
->width
) {
173 /* remove a character from the middle */
174 text
= g_string_erase(text
, l
-- / 2, 1);
175 /* if the elipses are too large, don't show them at all */
176 if (ELIPSES_LENGTH(t
->font
) > area
->width
)
178 font_measure_full(t
->font
, text
->str
, &mw
, &mh
);
179 mw
+= ELIPSES_LENGTH(t
->font
);
182 text
= g_string_insert(text
, (l
+ 1) / 2, ELIPSES
);
187 switch (t
->justify
) {
188 case RR_JUSTIFY_LEFT
:
190 case RR_JUSTIFY_RIGHT
:
193 case RR_JUSTIFY_CENTER
:
198 l
= strlen(text
->str
); /* number of bytes */
200 if (t
->font
->shadow
) {
201 if (t
->font
->tint
>= 0) {
205 c
.color
.alpha
= 0xffff * t
->font
->tint
/ 100;
206 c
.pixel
= BlackPixel(RrDisplay(t
->font
->inst
),
207 RrScreen(t
->font
->inst
));
209 c
.color
.red
= 0xffff;
210 c
.color
.green
= 0xffff;
211 c
.color
.blue
= 0xffff;
212 c
.color
.alpha
= 0xffff * -t
->font
->tint
/ 100;
213 c
.pixel
= WhitePixel(RrDisplay(t
->font
->inst
),
214 RrScreen(t
->font
->inst
));
216 XftDrawStringUtf8(d
, &c
, t
->font
->xftfont
, x
+ t
->font
->offset
,
217 t
->font
->xftfont
->ascent
+ y
+ t
->font
->offset
,
218 (FcChar8
*)text
->str
, l
);
220 c
.color
.red
= t
->color
->r
| t
->color
->r
<< 8;
221 c
.color
.green
= t
->color
->g
| t
->color
->g
<< 8;
222 c
.color
.blue
= t
->color
->b
| t
->color
->b
<< 8;
223 c
.color
.alpha
= 0xff | 0xff << 8; /* fully opaque text */
224 c
.pixel
= t
->color
->pixel
;
226 XftDrawStringUtf8(d
, &c
, t
->font
->xftfont
, x
,
227 t
->font
->xftfont
->ascent
+ y
,
228 (FcChar8
*)text
->str
, l
);
230 g_string_free(text
, TRUE
);
This page took 0.046326 seconds and 4 git commands to generate.