10 #include <X11/Xlocale.h>
12 #include <X11/Xft/Xft.h>
13 #include <fontconfig/fontconfig.h>
15 #define DEFAULT_FONT "sans serif:pixelsize=12"
17 #define DEFAULT_SIZE WINGsConfiguration.defaultFontSize
19 static FcPattern
*xlfdToFcPattern(const char *xlfd
)
24 /* Just skip old font names that contain %d in them.
25 * We don't support that anymore. */
26 if (strchr(xlfd
, '%') != NULL
)
27 return FcNameParse((FcChar8
*) DEFAULT_FONT
);
29 fname
= wstrdup(xlfd
);
30 if ((ptr
= strchr(fname
, ','))) {
33 pattern
= XftXlfdParse(fname
, False
, False
);
37 wwarning(_("invalid font: %s. Trying '%s'"), xlfd
, DEFAULT_FONT
);
38 pattern
= FcNameParse((FcChar8
*) DEFAULT_FONT
);
44 static char *xlfdToFcName(const char *xlfd
)
49 pattern
= xlfdToFcPattern(xlfd
);
50 fname
= (char *)FcNameUnparse(pattern
);
51 FcPatternDestroy(pattern
);
56 static Bool
hasProperty(FcPattern
* pattern
, const char *property
)
60 if (FcPatternGet(pattern
, property
, 0, &val
) == FcResultMatch
) {
67 static Bool
hasPropertyWithStringValue(FcPattern
* pattern
, const char *object
, const char *value
)
72 if (!value
|| value
[0] == 0)
76 while (FcPatternGetString(pattern
, object
, id
, &str
) == FcResultMatch
) {
77 if (strcasecmp(value
, (char *)str
) == 0) {
86 static char *makeFontOfSize(const char *font
, int size
, const char *fallback
)
92 pattern
= xlfdToFcPattern(font
);
94 pattern
= FcNameParse((const FcChar8
*) font
);
97 /*FcPatternPrint(pattern); */
100 FcPatternDel(pattern
, FC_PIXEL_SIZE
);
101 FcPatternAddDouble(pattern
, FC_PIXEL_SIZE
, (double)size
);
102 } else if (size
== 0 && !hasProperty(pattern
, "size") && !hasProperty(pattern
, FC_PIXEL_SIZE
)) {
103 FcPatternAddDouble(pattern
, FC_PIXEL_SIZE
, (double)DEFAULT_SIZE
);
106 if (fallback
&& !hasPropertyWithStringValue(pattern
, FC_FAMILY
, fallback
)) {
107 FcPatternAddString(pattern
, FC_FAMILY
, (const FcChar8
*) fallback
);
110 /*FcPatternPrint(pattern); */
112 result
= (char *)FcNameUnparse(pattern
);
113 FcPatternDestroy(pattern
);
118 WMFont
*WMCreateFont(WMScreen
* scrPtr
, const char *fontName
)
120 Display
*display
= scrPtr
->display
;
124 if (fontName
[0] == '-') {
125 fname
= xlfdToFcName(fontName
);
127 fname
= wstrdup(fontName
);
130 if (!WINGsConfiguration
.antialiasedText
&& !strstr(fname
, ":antialias=")) {
131 fname
= wstrappend(fname
, ":antialias=false");
134 font
= WMHashGet(scrPtr
->fontCache
, fname
);
141 font
= wmalloc(sizeof(WMFont
));
143 font
->screen
= scrPtr
;
145 font
->font
= XftFontOpenName(display
, scrPtr
->screen
, fname
);
152 font
->height
= font
->font
->ascent
+ font
->font
->descent
;
153 font
->y
= font
->font
->ascent
;
159 assert(WMHashInsert(scrPtr
->fontCache
, font
->name
, font
) == NULL
);
164 WMFont
*WMRetainFont(WMFont
* font
)
166 wassertrv(font
!= NULL
, NULL
);
173 void WMReleaseFont(WMFont
* font
)
175 wassertr(font
!= NULL
);
178 if (font
->refCount
< 1) {
179 XftFontClose(font
->screen
->display
, font
->font
);
181 WMHashRemove(font
->screen
->fontCache
, font
->name
);
188 Bool
WMIsAntialiasingEnabled(WMScreen
* scrPtr
)
190 return scrPtr
->antialiasedText
;
193 unsigned int WMFontHeight(WMFont
* font
)
195 wassertrv(font
!= NULL
, 0);
200 char *WMGetFontName(WMFont
* font
)
202 wassertrv(font
!= NULL
, NULL
);
207 WMFont
*WMDefaultSystemFont(WMScreen
* scrPtr
)
209 return WMRetainFont(scrPtr
->normalFont
);
212 WMFont
*WMDefaultBoldSystemFont(WMScreen
* scrPtr
)
214 return WMRetainFont(scrPtr
->boldFont
);
217 WMFont
*WMSystemFontOfSize(WMScreen
* scrPtr
, int size
)
222 fontSpec
= makeFontOfSize(WINGsConfiguration
.systemFont
, size
, NULL
);
224 font
= WMCreateFont(scrPtr
, fontSpec
);
227 wwarning(_("could not load font: %s."), fontSpec
);
235 WMFont
*WMBoldSystemFontOfSize(WMScreen
* scrPtr
, int size
)
240 fontSpec
= makeFontOfSize(WINGsConfiguration
.boldSystemFont
, size
, NULL
);
242 font
= WMCreateFont(scrPtr
, fontSpec
);
245 wwarning(_("could not load font: %s."), fontSpec
);
253 int WMWidthOfString(WMFont
* font
, const char *text
, int length
)
257 wassertrv(font
!= NULL
&& text
!= NULL
, 0);
259 XftTextExtentsUtf8(font
->screen
->display
, font
->font
, (XftChar8
*) text
, length
, &extents
);
261 return extents
.xOff
; /* don't ask :P */
264 void WMDrawString(WMScreen
* scr
, Drawable d
, WMColor
* color
, WMFont
* font
, int x
, int y
, const char *text
, int length
)
268 wassertr(font
!= NULL
);
270 xftcolor
.color
.red
= color
->color
.red
;
271 xftcolor
.color
.green
= color
->color
.green
;
272 xftcolor
.color
.blue
= color
->color
.blue
;
273 xftcolor
.color
.alpha
= color
->alpha
;;
274 xftcolor
.pixel
= W_PIXEL(color
);
276 XftDrawChange(scr
->xftdraw
, d
);
278 XftDrawStringUtf8(scr
->xftdraw
, &xftcolor
, font
->font
, x
, y
+ font
->y
, (XftChar8
*) text
, length
);
282 WMDrawImageString(WMScreen
* scr
, Drawable d
, WMColor
* color
, WMColor
* background
,
283 WMFont
* font
, int x
, int y
, const char *text
, int length
)
288 wassertr(font
!= NULL
);
290 textColor
.color
.red
= color
->color
.red
;
291 textColor
.color
.green
= color
->color
.green
;
292 textColor
.color
.blue
= color
->color
.blue
;
293 textColor
.color
.alpha
= color
->alpha
;;
294 textColor
.pixel
= W_PIXEL(color
);
296 bgColor
.color
.red
= background
->color
.red
;
297 bgColor
.color
.green
= background
->color
.green
;
298 bgColor
.color
.blue
= background
->color
.blue
;
299 bgColor
.color
.alpha
= background
->alpha
;;
300 bgColor
.pixel
= W_PIXEL(background
);
302 XftDrawChange(scr
->xftdraw
, d
);
304 XftDrawRect(scr
->xftdraw
, &bgColor
, x
, y
, WMWidthOfString(font
, text
, length
), font
->height
);
306 XftDrawStringUtf8(scr
->xftdraw
, &textColor
, font
->font
, x
, y
+ font
->y
, (XftChar8
*) text
, length
);
309 WMFont
*WMCopyFontWithStyle(WMScreen
* scrPtr
, WMFont
* font
, WMFontStyle style
)
318 /* It's enough to add italic to slant, even if the font has no italic
319 * variant, but only oblique. This is because fontconfig will actually
320 * return the closest match font to what we requested which is the
321 * oblique font. Same goes for using bold for weight.
323 pattern
= FcNameParse((FcChar8
*) WMGetFontName(font
));
326 FcPatternDel(pattern
, FC_WEIGHT
);
327 FcPatternDel(pattern
, FC_SLANT
);
330 FcPatternDel(pattern
, FC_WEIGHT
);
331 FcPatternAddString(pattern
, FC_WEIGHT
, (FcChar8
*) "bold");
334 FcPatternDel(pattern
, FC_SLANT
);
335 FcPatternAddString(pattern
, FC_SLANT
, (FcChar8
*) "italic");
338 FcPatternDel(pattern
, FC_WEIGHT
);
339 FcPatternDel(pattern
, FC_SLANT
);
340 FcPatternAddString(pattern
, FC_WEIGHT
, (FcChar8
*) "bold");
341 FcPatternAddString(pattern
, FC_SLANT
, (FcChar8
*) "italic");
345 name
= (char *)FcNameUnparse(pattern
);
346 copy
= WMCreateFont(scrPtr
, name
);
347 FcPatternDestroy(pattern
);