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
, "pixelsize");
93 FcPatternAddDouble(pattern
, "pixelsize", (double)size
);
94 } else if (size
==0 && !hasProperty(pattern
, "size") &&
95 !hasProperty(pattern
, "pixelsize")) {
96 FcPatternAddDouble(pattern
, "pixelsize", (double)DEFAULT_SIZE
);
99 if (fallback
&& !hasPropertyWithStringValue(pattern
, "family", fallback
)) {
100 FcPatternAddString(pattern
, "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
;
140 printf("WMCreateFont: %s\n", fname
);
142 if (fname
[0] == '-') {
143 // Backward compat thing. Remove in a later version
144 font
->font
= XftFontOpenXlfd(display
, scrPtr
->screen
, fname
);
146 font
->font
= XftFontOpenName(display
, scrPtr
->screen
, fname
);
153 font
->height
= font
->font
->ascent
+font
->font
->descent
;
154 font
->y
= font
->font
->ascent
;
160 assert(WMHashInsert(scrPtr
->fontCache
, font
->name
, font
)==NULL
);
167 WMRetainFont(WMFont
*font
)
169 wassertrv(font
!=NULL
, NULL
);
178 WMReleaseFont(WMFont
*font
)
180 wassertr(font
!=NULL
);
183 if (font
->refCount
< 1) {
184 XftFontClose(font
->screen
->display
, font
->font
);
186 WMHashRemove(font
->screen
->fontCache
, font
->name
);
195 WMIsAntialiasingEnabled(WMScreen
*scrPtr
)
197 return scrPtr
->antialiasedText
;
202 WMFontHeight(WMFont
*font
)
204 wassertrv(font
!=NULL
, 0);
211 WMGetFontName(WMFont
*font
)
213 wassertrv(font
!=NULL
, NULL
);
220 WMDefaultSystemFont(WMScreen
*scrPtr
)
222 return WMRetainFont(scrPtr
->normalFont
);
227 WMDefaultBoldSystemFont(WMScreen
*scrPtr
)
229 return WMRetainFont(scrPtr
->boldFont
);
234 WMSystemFontOfSize(WMScreen
*scrPtr
, int size
)
239 fontSpec
= makeFontOfSize(WINGsConfiguration
.systemFont
, size
, "sans");
241 font
= WMCreateFont(scrPtr
, fontSpec
);
244 wwarning(_("could not load font %s."), fontSpec
);
254 WMBoldSystemFontOfSize(WMScreen
*scrPtr
, int size
)
259 fontSpec
= makeFontOfSize(WINGsConfiguration
.boldSystemFont
, size
, "sans");
261 font
= WMCreateFont(scrPtr
, fontSpec
);
264 wwarning(_("could not load font %s."), fontSpec
);
274 WMWidthOfString(WMFont
*font
, char *text
, int length
)
278 wassertrv(font
!=NULL
&& text
!=NULL
, 0);
280 XftTextExtentsUtf8(font
->screen
->display
, font
->font
,
281 (XftChar8
*)text
, length
, &extents
);
283 return extents
.xOff
; /* don't ask :P */
289 WMDrawString(WMScreen
*scr
, Drawable d
, WMColor
*color
, WMFont
*font
,
290 int x
, int y
, char *text
, int length
)
294 wassertr(font
!=NULL
);
296 xftcolor
.color
.red
= color
->color
.red
;
297 xftcolor
.color
.green
= color
->color
.green
;
298 xftcolor
.color
.blue
= color
->color
.blue
;
299 xftcolor
.color
.alpha
= color
->alpha
;;
300 xftcolor
.pixel
= W_PIXEL(color
);
302 XftDrawChange(scr
->xftdraw
, d
);
304 XftDrawStringUtf8(scr
->xftdraw
, &xftcolor
, font
->font
,
305 x
, y
+ font
->y
, (XftChar8
*)text
, length
);
310 WMDrawImageString(WMScreen
*scr
, Drawable d
, WMColor
*color
, WMColor
*background
,
311 WMFont
*font
, int x
, int y
, char *text
, int length
)
316 wassertr(font
!=NULL
);
318 textColor
.color
.red
= color
->color
.red
;
319 textColor
.color
.green
= color
->color
.green
;
320 textColor
.color
.blue
= color
->color
.blue
;
321 textColor
.color
.alpha
= color
->alpha
;;
322 textColor
.pixel
= W_PIXEL(color
);
324 bgColor
.color
.red
= background
->color
.red
;
325 bgColor
.color
.green
= background
->color
.green
;
326 bgColor
.color
.blue
= background
->color
.blue
;
327 bgColor
.color
.alpha
= background
->alpha
;;
328 bgColor
.pixel
= W_PIXEL(background
);
330 XftDrawChange(scr
->xftdraw
, d
);
332 XftDrawRect(scr
->xftdraw
, &bgColor
, x
, y
,
333 WMWidthOfString(font
, text
, length
),
336 XftDrawStringUtf8(scr
->xftdraw
, &textColor
, font
->font
,
337 x
, y
+ font
->y
, (XftChar8
*)text
, length
);
342 WMCopyFontWithStyle(WMScreen
*scrPtr
, WMFont
*font
, WMFontStyle style
)
351 pattern
= FcNameParse(WMGetFontName(font
));
354 FcPatternDel(pattern
, "weight");
355 FcPatternDel(pattern
, "slant");
356 FcPatternAddString(pattern
, "weight", "regular");
357 FcPatternAddString(pattern
, "weight", "medium");
358 FcPatternAddString(pattern
, "slant", "roman");
361 FcPatternDel(pattern
, "weight");
362 FcPatternAddString(pattern
, "weight", "bold");
365 FcPatternDel(pattern
, "slant");
366 FcPatternAddString(pattern
, "slant", "italic");
367 FcPatternAddString(pattern
, "slant", "oblique");
369 case WFSBoldEmphasized
:
370 FcPatternDel(pattern
, "weight");
371 FcPatternDel(pattern
, "slant");
372 FcPatternAddString(pattern
, "weight", "bold");
373 FcPatternAddString(pattern
, "slant", "italic");
374 FcPatternAddString(pattern
, "slant", "oblique");
378 name
= FcNameUnparse(pattern
);
379 copy
= WMCreateFont(scrPtr
, name
);
380 FcPatternDestroy(pattern
);