10 #include <X11/Xlocale.h>
12 #include <X11/Xft/Xft.h>
13 #include <fontconfig/fontconfig.h>
16 #define DEFAULT_FONT "sans serif:pixelsize=12"
18 #define DEFAULT_SIZE WINGsConfiguration.defaultFontSize
22 xlfdToFcPattern(char *xlfd
)
27 /* Just skip old font names that contain %d in them.
28 * We don't support that anymore. */
29 if (strchr(xlfd
, '%')!=NULL
)
30 return FcNameParse(DEFAULT_FONT
);
33 if ((ptr
= strchr(fname
, ','))) {
36 pattern
= XftXlfdParse(fname
, False
, False
);
40 wwarning(_("invalid font: %s. Trying '%s'"), xlfd
, DEFAULT_FONT
);
41 pattern
= FcNameParse(DEFAULT_FONT
);
49 xlfdToFcName(char *xlfd
)
54 pattern
= xlfdToFcPattern(xlfd
);
55 fname
= FcNameUnparse(pattern
);
56 FcPatternDestroy(pattern
);
63 hasProperty(FcPattern
*pattern
, const char *property
)
67 if (FcPatternGet(pattern
, property
, 0, &val
)==FcResultMatch
) {
76 hasPropertyWithStringValue(FcPattern
*pattern
, const char *object
, char *value
)
81 if (!value
|| value
[0]==0)
85 while (FcPatternGetString(pattern
, object
, id
, &str
)==FcResultMatch
) {
86 if (strcasecmp(value
, (char*)str
) == 0) {
97 makeFontOfSize(char *font
, int size
, char *fallback
)
103 pattern
= xlfdToFcPattern(font
);
105 pattern
= FcNameParse(font
);
108 /*FcPatternPrint(pattern);*/
111 FcPatternDel(pattern
, FC_PIXEL_SIZE
);
112 FcPatternAddDouble(pattern
, FC_PIXEL_SIZE
, (double)size
);
113 } else if (size
==0 && !hasProperty(pattern
, "size") &&
114 !hasProperty(pattern
, FC_PIXEL_SIZE
)) {
115 FcPatternAddDouble(pattern
, FC_PIXEL_SIZE
, (double)DEFAULT_SIZE
);
118 if (fallback
&& !hasPropertyWithStringValue(pattern
, FC_FAMILY
, fallback
)) {
119 FcPatternAddString(pattern
, FC_FAMILY
, fallback
);
122 /*FcPatternPrint(pattern);*/
124 result
= FcNameUnparse(pattern
);
125 FcPatternDestroy(pattern
);
132 WMCreateFont(WMScreen
*scrPtr
, char *fontName
)
134 Display
*display
= scrPtr
->display
;
138 if (fontName
[0]=='-'){
139 fname
= xlfdToFcName(fontName
);
141 fname
= wstrdup(fontName
);
144 if (!WINGsConfiguration
.antialiasedText
&& !strstr(fname
, ":antialias=")) {
145 fname
= wstrappend(fname
, ":antialias=false");
148 font
= WMHashGet(scrPtr
->fontCache
, fname
);
155 font
= wmalloc(sizeof(WMFont
));
156 memset(font
, 0, sizeof(WMFont
));
158 font
->screen
= scrPtr
;
160 font
->font
= XftFontOpenName(display
, scrPtr
->screen
, fname
);
166 font
->height
= font
->font
->ascent
+font
->font
->descent
;
167 font
->y
= font
->font
->ascent
;
173 assert(WMHashInsert(scrPtr
->fontCache
, font
->name
, font
)==NULL
);
180 WMRetainFont(WMFont
*font
)
182 wassertrv(font
!=NULL
, NULL
);
191 WMReleaseFont(WMFont
*font
)
193 wassertr(font
!=NULL
);
196 if (font
->refCount
< 1) {
197 XftFontClose(font
->screen
->display
, font
->font
);
199 WMHashRemove(font
->screen
->fontCache
, font
->name
);
208 WMIsAntialiasingEnabled(WMScreen
*scrPtr
)
210 return scrPtr
->antialiasedText
;
215 WMFontHeight(WMFont
*font
)
217 wassertrv(font
!=NULL
, 0);
224 WMGetFontName(WMFont
*font
)
226 wassertrv(font
!=NULL
, NULL
);
233 WMDefaultSystemFont(WMScreen
*scrPtr
)
235 return WMRetainFont(scrPtr
->normalFont
);
240 WMDefaultBoldSystemFont(WMScreen
*scrPtr
)
242 return WMRetainFont(scrPtr
->boldFont
);
247 WMSystemFontOfSize(WMScreen
*scrPtr
, int size
)
252 fontSpec
= makeFontOfSize(WINGsConfiguration
.systemFont
, size
, NULL
);
254 font
= WMCreateFont(scrPtr
, fontSpec
);
257 wwarning(_("could not load font: %s."), fontSpec
);
267 WMBoldSystemFontOfSize(WMScreen
*scrPtr
, int size
)
272 fontSpec
= makeFontOfSize(WINGsConfiguration
.boldSystemFont
, size
, NULL
);
274 font
= WMCreateFont(scrPtr
, fontSpec
);
277 wwarning(_("could not load font: %s."), fontSpec
);
287 WMWidthOfString(WMFont
*font
, char *text
, int length
)
291 wassertrv(font
!=NULL
&& text
!=NULL
, 0);
293 XftTextExtentsUtf8(font
->screen
->display
, font
->font
,
294 (XftChar8
*)text
, length
, &extents
);
296 return extents
.xOff
; /* don't ask :P */
302 WMDrawString(WMScreen
*scr
, Drawable d
, WMColor
*color
, WMFont
*font
,
303 int x
, int y
, char *text
, int length
)
307 wassertr(font
!=NULL
);
309 xftcolor
.color
.red
= color
->color
.red
;
310 xftcolor
.color
.green
= color
->color
.green
;
311 xftcolor
.color
.blue
= color
->color
.blue
;
312 xftcolor
.color
.alpha
= color
->alpha
;;
313 xftcolor
.pixel
= W_PIXEL(color
);
315 XftDrawChange(scr
->xftdraw
, d
);
317 XftDrawStringUtf8(scr
->xftdraw
, &xftcolor
, font
->font
,
318 x
, y
+ font
->y
, (XftChar8
*)text
, length
);
323 WMDrawImageString(WMScreen
*scr
, Drawable d
, WMColor
*color
, WMColor
*background
,
324 WMFont
*font
, int x
, int y
, char *text
, int length
)
329 wassertr(font
!=NULL
);
331 textColor
.color
.red
= color
->color
.red
;
332 textColor
.color
.green
= color
->color
.green
;
333 textColor
.color
.blue
= color
->color
.blue
;
334 textColor
.color
.alpha
= color
->alpha
;;
335 textColor
.pixel
= W_PIXEL(color
);
337 bgColor
.color
.red
= background
->color
.red
;
338 bgColor
.color
.green
= background
->color
.green
;
339 bgColor
.color
.blue
= background
->color
.blue
;
340 bgColor
.color
.alpha
= background
->alpha
;;
341 bgColor
.pixel
= W_PIXEL(background
);
343 XftDrawChange(scr
->xftdraw
, d
);
345 XftDrawRect(scr
->xftdraw
, &bgColor
, x
, y
,
346 WMWidthOfString(font
, text
, length
),
349 XftDrawStringUtf8(scr
->xftdraw
, &textColor
, font
->font
,
350 x
, y
+ font
->y
, (XftChar8
*)text
, length
);
355 WMCopyFontWithStyle(WMScreen
*scrPtr
, WMFont
*font
, WMFontStyle style
)
364 /* It's enough to add italic to slant, even if the font has no italic
365 * variant, but only oblique. This is because fontconfig will actually
366 * return the closest match font to what we requested which is the
367 * oblique font. Same goes for using bold for weight.
369 pattern
= FcNameParse(WMGetFontName(font
));
372 FcPatternDel(pattern
, FC_WEIGHT
);
373 FcPatternDel(pattern
, FC_SLANT
);
376 FcPatternDel(pattern
, FC_WEIGHT
);
377 FcPatternAddString(pattern
, FC_WEIGHT
, "bold");
380 FcPatternDel(pattern
, FC_SLANT
);
381 FcPatternAddString(pattern
, FC_SLANT
, "italic");
384 FcPatternDel(pattern
, FC_WEIGHT
);
385 FcPatternDel(pattern
, FC_SLANT
);
386 FcPatternAddString(pattern
, FC_WEIGHT
, "bold");
387 FcPatternAddString(pattern
, FC_SLANT
, "italic");
391 name
= FcNameUnparse(pattern
);
392 copy
= WMCreateFont(scrPtr
, name
);
393 FcPatternDestroy(pattern
);