Fix periodic focus bug
[wmaker-crm.git] / WINGs / wfont.c
blob9de1c7e1af0987ce3353911c278c5b5e0a33ea47
2 #include <stdlib.h>
4 #include "wconfig.h"
6 #include "WINGsP.h"
8 #include <wraster.h>
9 #include <assert.h>
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
21 static FcPattern*
22 xlfdToFcPattern(char *xlfd)
24 FcPattern *pattern;
25 char *fname, *ptr;
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((FcChar8*)DEFAULT_FONT);
32 fname= wstrdup(xlfd);
33 if ((ptr = strchr(fname, ','))) {
34 *ptr = 0;
36 pattern = XftXlfdParse(fname, False, False);
37 wfree(fname);
39 if (!pattern) {
40 wwarning(_("invalid font: %s. Trying '%s'"), xlfd, DEFAULT_FONT);
41 pattern = FcNameParse((FcChar8*)DEFAULT_FONT);
44 return pattern;
48 static char*
49 xlfdToFcName(char *xlfd)
51 FcPattern *pattern;
52 char *fname;
54 pattern = xlfdToFcPattern(xlfd);
55 fname = (char*)FcNameUnparse(pattern);
56 FcPatternDestroy(pattern);
58 return fname;
62 static Bool
63 hasProperty(FcPattern *pattern, const char *property)
65 FcValue val;
67 if (FcPatternGet(pattern, property, 0, &val)==FcResultMatch) {
68 return True;
71 return False;
75 static Bool
76 hasPropertyWithStringValue(FcPattern *pattern, const char *object, char *value)
78 FcChar8 *str;
79 int id;
81 if (!value || value[0]==0)
82 return True;
84 id = 0;
85 while (FcPatternGetString(pattern, object, id, &str)==FcResultMatch) {
86 if (strcasecmp(value, (char*)str) == 0) {
87 return True;
89 id++;
92 return False;
96 static char*
97 makeFontOfSize(char *font, int size, char *fallback)
99 FcPattern *pattern;
100 char *result;
102 if (font[0]=='-') {
103 pattern = xlfdToFcPattern(font);
104 } else {
105 pattern = FcNameParse((FcChar8*)font);
108 /*FcPatternPrint(pattern);*/
110 if (size > 0) {
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, (FcChar8*)fallback);
122 /*FcPatternPrint(pattern);*/
124 result = (char*)FcNameUnparse(pattern);
125 FcPatternDestroy(pattern);
127 return result;
131 WMFont*
132 WMCreateFont(WMScreen *scrPtr, char *fontName)
134 Display *display = scrPtr->display;
135 WMFont *font;
136 char *fname;
138 if (fontName[0]=='-'){
139 fname = xlfdToFcName(fontName);
140 } else {
141 fname = wstrdup(fontName);
144 if (!WINGsConfiguration.antialiasedText && !strstr(fname, ":antialias=")) {
145 fname = wstrappend(fname, ":antialias=false");
148 font = WMHashGet(scrPtr->fontCache, fname);
149 if (font) {
150 WMRetainFont(font);
151 wfree(fname);
152 return font;
155 font = wmalloc(sizeof(WMFont));
156 memset(font, 0, sizeof(WMFont));
158 font->screen = scrPtr;
160 font->font = XftFontOpenName(display, scrPtr->screen, fname);
161 if (!font->font) {
162 printf("Font named %s doesn't exist.\n", fname);
163 printf("Please check your system configuration.\n");
164 printf("Will try default font %s.\n", DEFAULT_FONT);
165 font->font = XftFontOpenName(display, scrPtr->screen, DEFAULT_FONT);
166 if (!font->font) {
167 printf("Unrecoverable font error! I must die!\n");
168 exit(1);
169 } else
170 printf("Default font loading succeded.\n");
173 font->height = font->font->ascent+font->font->descent;
174 font->y = font->font->ascent;
176 font->refCount = 1;
178 font->name = fname;
180 assert(WMHashInsert(scrPtr->fontCache, font->name, font)==NULL);
182 return font;
186 WMFont*
187 WMRetainFont(WMFont *font)
189 wassertrv(font!=NULL, NULL);
191 font->refCount++;
193 return font;
197 void
198 WMReleaseFont(WMFont *font)
200 wassertr(font!=NULL);
202 font->refCount--;
203 if (font->refCount < 1) {
204 XftFontClose(font->screen->display, font->font);
205 if (font->name) {
206 WMHashRemove(font->screen->fontCache, font->name);
207 wfree(font->name);
209 wfree(font);
214 Bool
215 WMIsAntialiasingEnabled(WMScreen *scrPtr)
217 return scrPtr->antialiasedText;
221 unsigned int
222 WMFontHeight(WMFont *font)
224 wassertrv(font!=NULL, 0);
226 return font->height;
230 char*
231 WMGetFontName(WMFont *font)
233 wassertrv(font!=NULL, NULL);
235 return font->name;
239 WMFont*
240 WMDefaultSystemFont(WMScreen *scrPtr)
242 return WMRetainFont(scrPtr->normalFont);
246 WMFont*
247 WMDefaultBoldSystemFont(WMScreen *scrPtr)
249 return WMRetainFont(scrPtr->boldFont);
253 WMFont*
254 WMSystemFontOfSize(WMScreen *scrPtr, int size)
256 WMFont *font;
257 char *fontSpec;
259 fontSpec = makeFontOfSize(WINGsConfiguration.systemFont, size, NULL);
261 font = WMCreateFont(scrPtr, fontSpec);
263 if (!font) {
264 wwarning(_("could not load font: %s."), fontSpec);
267 wfree(fontSpec);
269 return font;
273 WMFont*
274 WMBoldSystemFontOfSize(WMScreen *scrPtr, int size)
276 WMFont *font;
277 char *fontSpec;
279 fontSpec = makeFontOfSize(WINGsConfiguration.boldSystemFont, size, NULL);
281 font = WMCreateFont(scrPtr, fontSpec);
283 if (!font) {
284 wwarning(_("could not load font: %s."), fontSpec);
287 wfree(fontSpec);
289 return font;
294 WMWidthOfString(WMFont *font, char *text, int length)
296 XGlyphInfo extents;
298 wassertrv(font!=NULL && text!=NULL, 0);
300 XftTextExtentsUtf8(font->screen->display, font->font,
301 (XftChar8 *)text, length, &extents);
303 return extents.xOff; /* don't ask :P */
308 void
309 WMDrawString(WMScreen *scr, Drawable d, WMColor *color, WMFont *font,
310 int x, int y, char *text, int length)
312 XftColor xftcolor;
314 wassertr(font!=NULL);
316 xftcolor.color.red = color->color.red;
317 xftcolor.color.green = color->color.green;
318 xftcolor.color.blue = color->color.blue;
319 xftcolor.color.alpha = color->alpha;;
320 xftcolor.pixel = W_PIXEL(color);
322 XftDrawChange(scr->xftdraw, d);
324 XftDrawStringUtf8(scr->xftdraw, &xftcolor, font->font,
325 x, y + font->y, (XftChar8*)text, length);
329 void
330 WMDrawImageString(WMScreen *scr, Drawable d, WMColor *color, WMColor *background,
331 WMFont *font, int x, int y, char *text, int length)
333 XftColor textColor;
334 XftColor bgColor;
336 wassertr(font!=NULL);
338 textColor.color.red = color->color.red;
339 textColor.color.green = color->color.green;
340 textColor.color.blue = color->color.blue;
341 textColor.color.alpha = color->alpha;;
342 textColor.pixel = W_PIXEL(color);
344 bgColor.color.red = background->color.red;
345 bgColor.color.green = background->color.green;
346 bgColor.color.blue = background->color.blue;
347 bgColor.color.alpha = background->alpha;;
348 bgColor.pixel = W_PIXEL(background);
350 XftDrawChange(scr->xftdraw, d);
352 XftDrawRect(scr->xftdraw, &bgColor, x, y,
353 WMWidthOfString(font, text, length),
354 font->height);
356 XftDrawStringUtf8(scr->xftdraw, &textColor, font->font,
357 x, y + font->y, (XftChar8*)text, length);
361 WMFont*
362 WMCopyFontWithStyle(WMScreen *scrPtr, WMFont *font, WMFontStyle style)
364 FcPattern *pattern;
365 WMFont *copy;
366 char *name;
368 if (!font)
369 return NULL;
371 /* It's enough to add italic to slant, even if the font has no italic
372 * variant, but only oblique. This is because fontconfig will actually
373 * return the closest match font to what we requested which is the
374 * oblique font. Same goes for using bold for weight.
376 pattern = FcNameParse((FcChar8*)WMGetFontName(font));
377 switch (style) {
378 case WFSNormal:
379 FcPatternDel(pattern, FC_WEIGHT);
380 FcPatternDel(pattern, FC_SLANT);
381 break;
382 case WFSBold:
383 FcPatternDel(pattern, FC_WEIGHT);
384 FcPatternAddString(pattern, FC_WEIGHT, (FcChar8*)"bold");
385 break;
386 case WFSItalic:
387 FcPatternDel(pattern, FC_SLANT);
388 FcPatternAddString(pattern, FC_SLANT, (FcChar8*)"italic");
389 break;
390 case WFSBoldItalic:
391 FcPatternDel(pattern, FC_WEIGHT);
392 FcPatternDel(pattern, FC_SLANT);
393 FcPatternAddString(pattern, FC_WEIGHT, (FcChar8*)"bold");
394 FcPatternAddString(pattern, FC_SLANT, (FcChar8*)"italic");
395 break;
398 name = (char*)FcNameUnparse(pattern);
399 copy = WMCreateFont(scrPtr, name);
400 FcPatternDestroy(pattern);
401 wfree(name);
403 return copy;