6 #include <X11/Xft/Xft.h>
7 #include <fontconfig/fontconfig.h>
15 #include <X11/Xlocale.h>
18 #define DEFAULT_SIZE WINGsConfiguration.defaultFontSize
21 fixXLFD(char *xlfd
, int size
)
25 fname
= wmalloc(strlen(xlfd
) + 20);
26 if (strstr(xlfd
, "%d")!=NULL
)
27 sprintf(fname
, xlfd
, size
? size
: DEFAULT_SIZE
);
31 if ((ptr
= strchr(fname
, ','))) {
40 hasProperty(FcPattern
*pattern
, const char *property
)
44 if (FcPatternGet(pattern
, property
, 0, &val
)==FcResultMatch
) {
53 hasPropertyWithStringValue(FcPattern
*pattern
, const char *object
, char *value
)
58 if (!value
|| value
[0]==0)
62 while (FcPatternGetString(pattern
, object
, id
, &str
)==FcResultMatch
) {
63 if (strcasecmp(value
, (char*)str
) == 0) {
73 // also handle an xlfd with %d for size?
75 makeFontOfSize(char *font
, int size
, char *fallback
)
83 fname
= fixXLFD(font
, size
);
84 pattern
= XftXlfdParse(fname
, False
, False
);
87 pattern
= FcNameParse(font
);
90 //FcPatternPrint(pattern);
92 FcPatternDel(pattern
, FC_PIXEL_SIZE
);
93 FcPatternAddDouble(pattern
, FC_PIXEL_SIZE
, (double)size
);
94 } else if (size
==0 && !hasProperty(pattern
, "size") &&
95 !hasProperty(pattern
, FC_PIXEL_SIZE
)) {
96 FcPatternAddDouble(pattern
, FC_PIXEL_SIZE
, (double)DEFAULT_SIZE
);
99 if (fallback
&& !hasPropertyWithStringValue(pattern
, FC_FAMILY
, fallback
)) {
100 FcPatternAddString(pattern
, FC_FAMILY
, fallback
);
103 result
= FcNameUnparse(pattern
);
104 FcPatternDestroy(pattern
);
111 WMCreateFont(WMScreen
*scrPtr
, char *fontName
)
114 Display
*display
= scrPtr
->display
;
117 /* This is for back-compat (to allow reading of old xlfd descriptions) */
118 if (fontName
[0]=='-' && (ptr
= strchr(fontName
, ','))) {
119 // warn for deprecation
120 fname
= wmalloc(ptr
- fontName
+ 1);
121 strncpy(fname
, fontName
, ptr
- fontName
);
122 fname
[ptr
- fontName
] = 0;
124 fname
= wstrdup(fontName
);
127 font
= WMHashGet(scrPtr
->fontCache
, fname
);
134 font
= wmalloc(sizeof(WMFont
));
135 memset(font
, 0, sizeof(WMFont
));
137 font
->screen
= scrPtr
;
139 if (fname
[0] == '-') {
140 // Backward compat thing. Remove in a later version
141 font
->font
= XftFontOpenXlfd(display
, scrPtr
->screen
, fname
);
143 font
->font
= XftFontOpenName(display
, scrPtr
->screen
, fname
);
150 font
->height
= font
->font
->ascent
+font
->font
->descent
;
151 font
->y
= font
->font
->ascent
;
157 assert(WMHashInsert(scrPtr
->fontCache
, font
->name
, font
)==NULL
);
164 WMRetainFont(WMFont
*font
)
166 wassertrv(font
!=NULL
, NULL
);
175 WMReleaseFont(WMFont
*font
)
177 wassertr(font
!=NULL
);
180 if (font
->refCount
< 1) {
181 XftFontClose(font
->screen
->display
, font
->font
);
183 WMHashRemove(font
->screen
->fontCache
, font
->name
);
192 WMIsAntialiasingEnabled(WMScreen
*scrPtr
)
194 return scrPtr
->antialiasedText
;
199 WMFontHeight(WMFont
*font
)
201 wassertrv(font
!=NULL
, 0);
208 WMGetFontName(WMFont
*font
)
210 wassertrv(font
!=NULL
, NULL
);
217 WMDefaultSystemFont(WMScreen
*scrPtr
)
219 return WMRetainFont(scrPtr
->normalFont
);
224 WMDefaultBoldSystemFont(WMScreen
*scrPtr
)
226 return WMRetainFont(scrPtr
->boldFont
);
231 WMSystemFontOfSize(WMScreen
*scrPtr
, int size
)
236 fontSpec
= makeFontOfSize(WINGsConfiguration
.systemFont
, size
, "sans");
238 font
= WMCreateFont(scrPtr
, fontSpec
);
241 wwarning(_("could not load font %s."), fontSpec
);
251 WMBoldSystemFontOfSize(WMScreen
*scrPtr
, int size
)
256 fontSpec
= makeFontOfSize(WINGsConfiguration
.boldSystemFont
, size
, "sans");
258 font
= WMCreateFont(scrPtr
, fontSpec
);
261 wwarning(_("could not load font %s."), fontSpec
);
271 WMWidthOfString(WMFont
*font
, char *text
, int length
)
275 wassertrv(font
!=NULL
&& text
!=NULL
, 0);
277 XftTextExtentsUtf8(font
->screen
->display
, font
->font
,
278 (XftChar8
*)text
, length
, &extents
);
280 return extents
.xOff
; /* don't ask :P */
286 WMDrawString(WMScreen
*scr
, Drawable d
, WMColor
*color
, WMFont
*font
,
287 int x
, int y
, char *text
, int length
)
291 wassertr(font
!=NULL
);
293 xftcolor
.color
.red
= color
->color
.red
;
294 xftcolor
.color
.green
= color
->color
.green
;
295 xftcolor
.color
.blue
= color
->color
.blue
;
296 xftcolor
.color
.alpha
= color
->alpha
;;
297 xftcolor
.pixel
= W_PIXEL(color
);
299 XftDrawChange(scr
->xftdraw
, d
);
301 XftDrawStringUtf8(scr
->xftdraw
, &xftcolor
, font
->font
,
302 x
, y
+ font
->y
, (XftChar8
*)text
, length
);
307 WMDrawImageString(WMScreen
*scr
, Drawable d
, WMColor
*color
, WMColor
*background
,
308 WMFont
*font
, int x
, int y
, char *text
, int length
)
313 wassertr(font
!=NULL
);
315 textColor
.color
.red
= color
->color
.red
;
316 textColor
.color
.green
= color
->color
.green
;
317 textColor
.color
.blue
= color
->color
.blue
;
318 textColor
.color
.alpha
= color
->alpha
;;
319 textColor
.pixel
= W_PIXEL(color
);
321 bgColor
.color
.red
= background
->color
.red
;
322 bgColor
.color
.green
= background
->color
.green
;
323 bgColor
.color
.blue
= background
->color
.blue
;
324 bgColor
.color
.alpha
= background
->alpha
;;
325 bgColor
.pixel
= W_PIXEL(background
);
327 XftDrawChange(scr
->xftdraw
, d
);
329 XftDrawRect(scr
->xftdraw
, &bgColor
, x
, y
,
330 WMWidthOfString(font
, text
, length
),
333 XftDrawStringUtf8(scr
->xftdraw
, &textColor
, font
->font
,
334 x
, y
+ font
->y
, (XftChar8
*)text
, length
);
339 WMCopyFontWithStyle(WMScreen
*scrPtr
, WMFont
*font
, WMFontStyle style
)
348 pattern
= FcNameParse(WMGetFontName(font
));
351 FcPatternDel(pattern
, FC_WEIGHT
);
352 FcPatternDel(pattern
, FC_SLANT
);
353 FcPatternAddString(pattern
, FC_WEIGHT
, "regular");
354 FcPatternAddString(pattern
, FC_WEIGHT
, "medium");
355 FcPatternAddString(pattern
, FC_SLANT
, "roman");
358 FcPatternDel(pattern
, FC_WEIGHT
);
359 FcPatternAddString(pattern
, FC_WEIGHT
, "bold");
362 FcPatternDel(pattern
, FC_SLANT
);
363 FcPatternAddString(pattern
, FC_SLANT
, "italic");
364 FcPatternAddString(pattern
, FC_SLANT
, "oblique");
366 case WFSBoldEmphasized
:
367 FcPatternDel(pattern
, FC_WEIGHT
);
368 FcPatternDel(pattern
, FC_SLANT
);
369 FcPatternAddString(pattern
, FC_WEIGHT
, "bold");
370 FcPatternAddString(pattern
, FC_SLANT
, "italic");
371 FcPatternAddString(pattern
, FC_SLANT
, "oblique");
375 name
= FcNameUnparse(pattern
);
376 copy
= WMCreateFont(scrPtr
, name
);
377 FcPatternDestroy(pattern
);