attempt to fix compilation on solaris with openwin
[wmaker-crm.git] / WINGs / wfont.c
blob03c6d002ce75959805b0697edbc7b3fdcb651f7a
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(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(DEFAULT_FONT);
44 return pattern;
48 static char*
49 xlfdToFcName(char *xlfd)
51 FcPattern *pattern;
52 char *fname;
54 pattern = xlfdToFcPattern(xlfd);
55 fname = 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(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, fallback);
122 /*FcPatternPrint(pattern);*/
124 result = 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 wfree(font);
163 wfree(fname);
164 return NULL;
166 font->height = font->font->ascent+font->font->descent;
167 font->y = font->font->ascent;
169 font->refCount = 1;
171 font->name = fname;
173 assert(WMHashInsert(scrPtr->fontCache, font->name, font)==NULL);
175 return font;
179 WMFont*
180 WMRetainFont(WMFont *font)
182 wassertrv(font!=NULL, NULL);
184 font->refCount++;
186 return font;
190 void
191 WMReleaseFont(WMFont *font)
193 wassertr(font!=NULL);
195 font->refCount--;
196 if (font->refCount < 1) {
197 XftFontClose(font->screen->display, font->font);
198 if (font->name) {
199 WMHashRemove(font->screen->fontCache, font->name);
200 wfree(font->name);
202 wfree(font);
207 Bool
208 WMIsAntialiasingEnabled(WMScreen *scrPtr)
210 return scrPtr->antialiasedText;
214 unsigned int
215 WMFontHeight(WMFont *font)
217 wassertrv(font!=NULL, 0);
219 return font->height;
223 char*
224 WMGetFontName(WMFont *font)
226 wassertrv(font!=NULL, NULL);
228 return font->name;
232 WMFont*
233 WMDefaultSystemFont(WMScreen *scrPtr)
235 return WMRetainFont(scrPtr->normalFont);
239 WMFont*
240 WMDefaultBoldSystemFont(WMScreen *scrPtr)
242 return WMRetainFont(scrPtr->boldFont);
246 WMFont*
247 WMSystemFontOfSize(WMScreen *scrPtr, int size)
249 WMFont *font;
250 char *fontSpec;
252 fontSpec = makeFontOfSize(WINGsConfiguration.systemFont, size, NULL);
254 font = WMCreateFont(scrPtr, fontSpec);
256 if (!font) {
257 wwarning(_("could not load font: %s."), fontSpec);
260 wfree(fontSpec);
262 return font;
266 WMFont*
267 WMBoldSystemFontOfSize(WMScreen *scrPtr, int size)
269 WMFont *font;
270 char *fontSpec;
272 fontSpec = makeFontOfSize(WINGsConfiguration.boldSystemFont, size, NULL);
274 font = WMCreateFont(scrPtr, fontSpec);
276 if (!font) {
277 wwarning(_("could not load font: %s."), fontSpec);
280 wfree(fontSpec);
282 return font;
287 WMWidthOfString(WMFont *font, char *text, int length)
289 XGlyphInfo extents;
291 wassertrv(font!=NULL && text!=NULL, 0);
293 XftTextExtentsUtf8(font->screen->display, font->font,
294 (XftChar8 *)text, length, &extents);
296 return extents.xOff; /* don't ask :P */
301 void
302 WMDrawString(WMScreen *scr, Drawable d, WMColor *color, WMFont *font,
303 int x, int y, char *text, int length)
305 XftColor xftcolor;
307 wassertr(font!=NULL);
309 xftcolor.color.red = color->color.red;
310 xftcolor.color.green = color->color.green;
311 xftcolor.color.blue = color->color.blue;
312 xftcolor.color.alpha = color->alpha;;
313 xftcolor.pixel = W_PIXEL(color);
315 XftDrawChange(scr->xftdraw, d);
317 XftDrawStringUtf8(scr->xftdraw, &xftcolor, font->font,
318 x, y + font->y, (XftChar8*)text, length);
322 void
323 WMDrawImageString(WMScreen *scr, Drawable d, WMColor *color, WMColor *background,
324 WMFont *font, int x, int y, char *text, int length)
326 XftColor textColor;
327 XftColor bgColor;
329 wassertr(font!=NULL);
331 textColor.color.red = color->color.red;
332 textColor.color.green = color->color.green;
333 textColor.color.blue = color->color.blue;
334 textColor.color.alpha = color->alpha;;
335 textColor.pixel = W_PIXEL(color);
337 bgColor.color.red = background->color.red;
338 bgColor.color.green = background->color.green;
339 bgColor.color.blue = background->color.blue;
340 bgColor.color.alpha = background->alpha;;
341 bgColor.pixel = W_PIXEL(background);
343 XftDrawChange(scr->xftdraw, d);
345 XftDrawRect(scr->xftdraw, &bgColor, x, y,
346 WMWidthOfString(font, text, length),
347 font->height);
349 XftDrawStringUtf8(scr->xftdraw, &textColor, font->font,
350 x, y + font->y, (XftChar8*)text, length);
354 WMFont*
355 WMCopyFontWithStyle(WMScreen *scrPtr, WMFont *font, WMFontStyle style)
357 FcPattern *pattern;
358 WMFont *copy;
359 char *name;
361 if (!font)
362 return NULL;
364 /* It's enough to add italic to slant, even if the font has no italic
365 * variant, but only oblique. This is because fontconfig will actually
366 * return the closest match font to what we requested which is the
367 * oblique font. Same goes for using bold for weight.
369 pattern = FcNameParse(WMGetFontName(font));
370 switch (style) {
371 case WFSNormal:
372 FcPatternDel(pattern, FC_WEIGHT);
373 FcPatternDel(pattern, FC_SLANT);
374 break;
375 case WFSBold:
376 FcPatternDel(pattern, FC_WEIGHT);
377 FcPatternAddString(pattern, FC_WEIGHT, "bold");
378 break;
379 case WFSItalic:
380 FcPatternDel(pattern, FC_SLANT);
381 FcPatternAddString(pattern, FC_SLANT, "italic");
382 break;
383 case WFSBoldItalic:
384 FcPatternDel(pattern, FC_WEIGHT);
385 FcPatternDel(pattern, FC_SLANT);
386 FcPatternAddString(pattern, FC_WEIGHT, "bold");
387 FcPatternAddString(pattern, FC_SLANT, "italic");
388 break;
391 name = FcNameUnparse(pattern);
392 copy = WMCreateFont(scrPtr, name);
393 FcPatternDestroy(pattern);
394 wfree(name);
396 return copy;