4 #include <X11/Xft/Xft.h>
5 #include <fontconfig/fontconfig.h>
13 #include <X11/Xlocale.h>
16 #define DEFAULT_SIZE WINGsConfiguration.defaultFontSize
19 fixXLFD(char *xlfd
, int size
)
23 fname
= wmalloc(strlen(xlfd
) + 20);
24 if (strstr(xlfd
, "%d")!=NULL
)
25 sprintf(fname
, xlfd
, size
? size
: DEFAULT_SIZE
);
29 if ((ptr
= strchr(fname
, ','))) {
38 hasProperty(FcPattern
*pattern
, const char *property
)
42 if (FcPatternGet(pattern
, property
, 0, &val
)==FcResultMatch
) {
51 hasPropertyWithStringValue(FcPattern
*pattern
, const char *object
, char *value
)
56 if (!value
|| value
[0]==0)
60 while (FcPatternGetString(pattern
, object
, id
, &str
)==FcResultMatch
) {
61 if (strcasecmp(value
, (char*)str
) == 0) {
71 // also handle an xlfd with %d for size?
73 makeFontOfSize(char *font
, int size
, char *fallback
)
81 fname
= fixXLFD(font
, size
);
82 pattern
= XftXlfdParse(fname
, False
, False
);
85 pattern
= FcNameParse(font
);
88 //FcPatternPrint(pattern);
90 FcPatternDel(pattern
, FC_PIXEL_SIZE
);
91 FcPatternAddDouble(pattern
, FC_PIXEL_SIZE
, (double)size
);
92 } else if (size
==0 && !hasProperty(pattern
, "size") &&
93 !hasProperty(pattern
, FC_PIXEL_SIZE
)) {
94 FcPatternAddDouble(pattern
, FC_PIXEL_SIZE
, (double)DEFAULT_SIZE
);
97 if (fallback
&& !hasPropertyWithStringValue(pattern
, FC_FAMILY
, fallback
)) {
98 FcPatternAddString(pattern
, FC_FAMILY
, fallback
);
101 result
= FcNameUnparse(pattern
);
102 FcPatternDestroy(pattern
);
109 WMCreateFont(WMScreen
*scrPtr
, char *fontName
)
112 Display
*display
= scrPtr
->display
;
115 /* This is for back-compat (to allow reading of old xlfd descriptions) */
116 if (fontName
[0]=='-' && (ptr
= strchr(fontName
, ','))) {
117 // warn for deprecation
118 fname
= wmalloc(ptr
- fontName
+ 1);
119 strncpy(fname
, fontName
, ptr
- fontName
);
120 fname
[ptr
- fontName
] = 0;
122 fname
= wstrdup(fontName
);
125 font
= WMHashGet(scrPtr
->fontCache
, fname
);
132 font
= wmalloc(sizeof(WMFont
));
133 memset(font
, 0, sizeof(WMFont
));
135 font
->screen
= scrPtr
;
137 if (fname
[0] == '-') {
138 // Backward compat thing. Remove in a later version
139 font
->font
= XftFontOpenXlfd(display
, scrPtr
->screen
, fname
);
141 font
->font
= XftFontOpenName(display
, scrPtr
->screen
, fname
);
148 font
->height
= font
->font
->ascent
+font
->font
->descent
;
149 font
->y
= font
->font
->ascent
;
155 assert(WMHashInsert(scrPtr
->fontCache
, font
->name
, font
)==NULL
);
162 WMRetainFont(WMFont
*font
)
164 wassertrv(font
!=NULL
, NULL
);
173 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
);
190 WMIsAntialiasingEnabled(WMScreen
*scrPtr
)
192 return scrPtr
->antialiasedText
;
197 WMFontHeight(WMFont
*font
)
199 wassertrv(font
!=NULL
, 0);
206 WMGetFontName(WMFont
*font
)
208 wassertrv(font
!=NULL
, NULL
);
215 WMDefaultSystemFont(WMScreen
*scrPtr
)
217 return WMRetainFont(scrPtr
->normalFont
);
222 WMDefaultBoldSystemFont(WMScreen
*scrPtr
)
224 return WMRetainFont(scrPtr
->boldFont
);
229 WMSystemFontOfSize(WMScreen
*scrPtr
, int size
)
234 fontSpec
= makeFontOfSize(WINGsConfiguration
.systemFont
, size
, "sans");
236 font
= WMCreateFont(scrPtr
, fontSpec
);
239 wwarning(_("could not load font %s."), fontSpec
);
249 WMBoldSystemFontOfSize(WMScreen
*scrPtr
, int size
)
254 fontSpec
= makeFontOfSize(WINGsConfiguration
.boldSystemFont
, size
, "sans");
256 font
= WMCreateFont(scrPtr
, fontSpec
);
259 wwarning(_("could not load font %s."), fontSpec
);
269 WMWidthOfString(WMFont
*font
, char *text
, int length
)
273 wassertrv(font
!=NULL
&& text
!=NULL
, 0);
275 XftTextExtentsUtf8(font
->screen
->display
, font
->font
,
276 (XftChar8
*)text
, length
, &extents
);
278 return extents
.xOff
; /* don't ask :P */
284 WMDrawString(WMScreen
*scr
, Drawable d
, WMColor
*color
, WMFont
*font
,
285 int x
, int y
, char *text
, int length
)
289 wassertr(font
!=NULL
);
291 xftcolor
.color
.red
= color
->color
.red
;
292 xftcolor
.color
.green
= color
->color
.green
;
293 xftcolor
.color
.blue
= color
->color
.blue
;
294 xftcolor
.color
.alpha
= color
->alpha
;;
295 xftcolor
.pixel
= W_PIXEL(color
);
297 XftDrawChange(scr
->xftdraw
, d
);
299 XftDrawStringUtf8(scr
->xftdraw
, &xftcolor
, font
->font
,
300 x
, y
+ font
->y
, (XftChar8
*)text
, length
);
305 WMDrawImageString(WMScreen
*scr
, Drawable d
, WMColor
*color
, WMColor
*background
,
306 WMFont
*font
, int x
, int y
, char *text
, int length
)
311 wassertr(font
!=NULL
);
313 textColor
.color
.red
= color
->color
.red
;
314 textColor
.color
.green
= color
->color
.green
;
315 textColor
.color
.blue
= color
->color
.blue
;
316 textColor
.color
.alpha
= color
->alpha
;;
317 textColor
.pixel
= W_PIXEL(color
);
319 bgColor
.color
.red
= background
->color
.red
;
320 bgColor
.color
.green
= background
->color
.green
;
321 bgColor
.color
.blue
= background
->color
.blue
;
322 bgColor
.color
.alpha
= background
->alpha
;;
323 bgColor
.pixel
= W_PIXEL(background
);
325 XftDrawChange(scr
->xftdraw
, d
);
327 XftDrawRect(scr
->xftdraw
, &bgColor
, x
, y
,
328 WMWidthOfString(font
, text
, length
),
331 XftDrawStringUtf8(scr
->xftdraw
, &textColor
, font
->font
,
332 x
, y
+ font
->y
, (XftChar8
*)text
, length
);
337 WMCopyFontWithStyle(WMScreen
*scrPtr
, WMFont
*font
, WMFontStyle style
)
346 pattern
= FcNameParse(WMGetFontName(font
));
349 FcPatternDel(pattern
, FC_WEIGHT
);
350 FcPatternDel(pattern
, FC_SLANT
);
351 FcPatternAddString(pattern
, FC_WEIGHT
, "regular");
352 FcPatternAddString(pattern
, FC_WEIGHT
, "medium");
353 FcPatternAddString(pattern
, FC_SLANT
, "roman");
356 FcPatternDel(pattern
, FC_WEIGHT
);
357 FcPatternAddString(pattern
, FC_WEIGHT
, "bold");
360 FcPatternDel(pattern
, FC_SLANT
);
361 FcPatternAddString(pattern
, FC_SLANT
, "italic");
362 FcPatternAddString(pattern
, FC_SLANT
, "oblique");
364 case WFSBoldEmphasized
:
365 FcPatternDel(pattern
, FC_WEIGHT
);
366 FcPatternDel(pattern
, FC_SLANT
);
367 FcPatternAddString(pattern
, FC_WEIGHT
, "bold");
368 FcPatternAddString(pattern
, FC_SLANT
, "italic");
369 FcPatternAddString(pattern
, FC_SLANT
, "oblique");
373 name
= FcNameUnparse(pattern
);
374 copy
= WMCreateFont(scrPtr
, name
);
375 FcPatternDestroy(pattern
);