wmgenmenu: Add French and Spanish translations
[wmaker-crm.git] / WINGs / wfont.c
blob245cdccdc29eed65f7c177d61b75dfb2b4be358c
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>
15 #define DEFAULT_FONT "sans serif:pixelsize=12"
17 #define DEFAULT_SIZE WINGsConfiguration.defaultFontSize
19 static FcPattern *xlfdToFcPattern(char *xlfd)
21 FcPattern *pattern;
22 char *fname, *ptr;
24 /* Just skip old font names that contain %d in them.
25 * We don't support that anymore. */
26 if (strchr(xlfd, '%') != NULL)
27 return FcNameParse((FcChar8 *) DEFAULT_FONT);
29 fname = wstrdup(xlfd);
30 if ((ptr = strchr(fname, ','))) {
31 *ptr = 0;
33 pattern = XftXlfdParse(fname, False, False);
34 wfree(fname);
36 if (!pattern) {
37 wwarning(_("invalid font: %s. Trying '%s'"), xlfd, DEFAULT_FONT);
38 pattern = FcNameParse((FcChar8 *) DEFAULT_FONT);
41 return pattern;
44 static char *xlfdToFcName(char *xlfd)
46 FcPattern *pattern;
47 char *fname;
49 pattern = xlfdToFcPattern(xlfd);
50 fname = (char *)FcNameUnparse(pattern);
51 FcPatternDestroy(pattern);
53 return fname;
56 static Bool hasProperty(FcPattern * pattern, const char *property)
58 FcValue val;
60 if (FcPatternGet(pattern, property, 0, &val) == FcResultMatch) {
61 return True;
64 return False;
67 static Bool hasPropertyWithStringValue(FcPattern * pattern, const char *object, char *value)
69 FcChar8 *str;
70 int id;
72 if (!value || value[0] == 0)
73 return True;
75 id = 0;
76 while (FcPatternGetString(pattern, object, id, &str) == FcResultMatch) {
77 if (strcasecmp(value, (char *)str) == 0) {
78 return True;
80 id++;
83 return False;
86 static char *makeFontOfSize(char *font, int size, char *fallback)
88 FcPattern *pattern;
89 char *result;
91 if (font[0] == '-') {
92 pattern = xlfdToFcPattern(font);
93 } else {
94 pattern = FcNameParse((FcChar8 *) font);
97 /*FcPatternPrint(pattern); */
99 if (size > 0) {
100 FcPatternDel(pattern, FC_PIXEL_SIZE);
101 FcPatternAddDouble(pattern, FC_PIXEL_SIZE, (double)size);
102 } else if (size == 0 && !hasProperty(pattern, "size") && !hasProperty(pattern, FC_PIXEL_SIZE)) {
103 FcPatternAddDouble(pattern, FC_PIXEL_SIZE, (double)DEFAULT_SIZE);
106 if (fallback && !hasPropertyWithStringValue(pattern, FC_FAMILY, fallback)) {
107 FcPatternAddString(pattern, FC_FAMILY, (FcChar8 *) fallback);
110 /*FcPatternPrint(pattern); */
112 result = (char *)FcNameUnparse(pattern);
113 FcPatternDestroy(pattern);
115 return result;
118 WMFont *WMCreateFont(WMScreen * scrPtr, char *fontName)
120 Display *display = scrPtr->display;
121 WMFont *font;
122 char *fname;
124 if (fontName[0] == '-') {
125 fname = xlfdToFcName(fontName);
126 } else {
127 fname = wstrdup(fontName);
130 if (!WINGsConfiguration.antialiasedText && !strstr(fname, ":antialias=")) {
131 fname = wstrappend(fname, ":antialias=false");
134 font = WMHashGet(scrPtr->fontCache, fname);
135 if (font) {
136 WMRetainFont(font);
137 wfree(fname);
138 return font;
141 font = wmalloc(sizeof(WMFont));
143 font->screen = scrPtr;
145 font->font = XftFontOpenName(display, scrPtr->screen, fname);
146 if (!font->font) {
147 printf("Font named %s doesn't exist.\n", fname);
148 printf("Please check your system configuration.\n");
149 printf("Will try default font %s.\n", DEFAULT_FONT);
150 font->font = XftFontOpenName(display, scrPtr->screen, DEFAULT_FONT);
151 if (!font->font) {
152 printf("Unrecoverable font error! I must die!\n");
153 wfree(font);
154 wfree(fname);
155 exit(1);
156 } else
157 printf("Default font loading succeded.\n");
160 font->height = font->font->ascent + font->font->descent;
161 font->y = font->font->ascent;
163 font->refCount = 1;
165 font->name = fname;
167 assert(WMHashInsert(scrPtr->fontCache, font->name, font) == NULL);
169 return font;
172 WMFont *WMRetainFont(WMFont * font)
174 wassertrv(font != NULL, NULL);
176 font->refCount++;
178 return font;
181 void WMReleaseFont(WMFont * font)
183 wassertr(font != NULL);
185 font->refCount--;
186 if (font->refCount < 1) {
187 XftFontClose(font->screen->display, font->font);
188 if (font->name) {
189 WMHashRemove(font->screen->fontCache, font->name);
190 wfree(font->name);
192 wfree(font);
196 Bool WMIsAntialiasingEnabled(WMScreen * scrPtr)
198 return scrPtr->antialiasedText;
201 unsigned int WMFontHeight(WMFont * font)
203 wassertrv(font != NULL, 0);
205 return font->height;
208 char *WMGetFontName(WMFont * font)
210 wassertrv(font != NULL, NULL);
212 return font->name;
215 WMFont *WMDefaultSystemFont(WMScreen * scrPtr)
217 return WMRetainFont(scrPtr->normalFont);
220 WMFont *WMDefaultBoldSystemFont(WMScreen * scrPtr)
222 return WMRetainFont(scrPtr->boldFont);
225 WMFont *WMSystemFontOfSize(WMScreen * scrPtr, int size)
227 WMFont *font;
228 char *fontSpec;
230 fontSpec = makeFontOfSize(WINGsConfiguration.systemFont, size, NULL);
232 font = WMCreateFont(scrPtr, fontSpec);
234 if (!font) {
235 wwarning(_("could not load font: %s."), fontSpec);
238 wfree(fontSpec);
240 return font;
243 WMFont *WMBoldSystemFontOfSize(WMScreen * scrPtr, int size)
245 WMFont *font;
246 char *fontSpec;
248 fontSpec = makeFontOfSize(WINGsConfiguration.boldSystemFont, size, NULL);
250 font = WMCreateFont(scrPtr, fontSpec);
252 if (!font) {
253 wwarning(_("could not load font: %s."), fontSpec);
256 wfree(fontSpec);
258 return font;
261 int WMWidthOfString(WMFont * font, char *text, int length)
263 XGlyphInfo extents;
265 wassertrv(font != NULL && text != NULL, 0);
267 XftTextExtentsUtf8(font->screen->display, font->font, (XftChar8 *) text, length, &extents);
269 return extents.xOff; /* don't ask :P */
272 void WMDrawString(WMScreen * scr, Drawable d, WMColor * color, WMFont * font, int x, int y, char *text, int length)
274 XftColor xftcolor;
276 wassertr(font != NULL);
278 xftcolor.color.red = color->color.red;
279 xftcolor.color.green = color->color.green;
280 xftcolor.color.blue = color->color.blue;
281 xftcolor.color.alpha = color->alpha;;
282 xftcolor.pixel = W_PIXEL(color);
284 XftDrawChange(scr->xftdraw, d);
286 XftDrawStringUtf8(scr->xftdraw, &xftcolor, font->font, x, y + font->y, (XftChar8 *) text, length);
289 void
290 WMDrawImageString(WMScreen * scr, Drawable d, WMColor * color, WMColor * background,
291 WMFont * font, int x, int y, char *text, int length)
293 XftColor textColor;
294 XftColor bgColor;
296 wassertr(font != NULL);
298 textColor.color.red = color->color.red;
299 textColor.color.green = color->color.green;
300 textColor.color.blue = color->color.blue;
301 textColor.color.alpha = color->alpha;;
302 textColor.pixel = W_PIXEL(color);
304 bgColor.color.red = background->color.red;
305 bgColor.color.green = background->color.green;
306 bgColor.color.blue = background->color.blue;
307 bgColor.color.alpha = background->alpha;;
308 bgColor.pixel = W_PIXEL(background);
310 XftDrawChange(scr->xftdraw, d);
312 XftDrawRect(scr->xftdraw, &bgColor, x, y, WMWidthOfString(font, text, length), font->height);
314 XftDrawStringUtf8(scr->xftdraw, &textColor, font->font, x, y + font->y, (XftChar8 *) text, length);
317 WMFont *WMCopyFontWithStyle(WMScreen * scrPtr, WMFont * font, WMFontStyle style)
319 FcPattern *pattern;
320 WMFont *copy;
321 char *name;
323 if (!font)
324 return NULL;
326 /* It's enough to add italic to slant, even if the font has no italic
327 * variant, but only oblique. This is because fontconfig will actually
328 * return the closest match font to what we requested which is the
329 * oblique font. Same goes for using bold for weight.
331 pattern = FcNameParse((FcChar8 *) WMGetFontName(font));
332 switch (style) {
333 case WFSNormal:
334 FcPatternDel(pattern, FC_WEIGHT);
335 FcPatternDel(pattern, FC_SLANT);
336 break;
337 case WFSBold:
338 FcPatternDel(pattern, FC_WEIGHT);
339 FcPatternAddString(pattern, FC_WEIGHT, (FcChar8 *) "bold");
340 break;
341 case WFSItalic:
342 FcPatternDel(pattern, FC_SLANT);
343 FcPatternAddString(pattern, FC_SLANT, (FcChar8 *) "italic");
344 break;
345 case WFSBoldItalic:
346 FcPatternDel(pattern, FC_WEIGHT);
347 FcPatternDel(pattern, FC_SLANT);
348 FcPatternAddString(pattern, FC_WEIGHT, (FcChar8 *) "bold");
349 FcPatternAddString(pattern, FC_SLANT, (FcChar8 *) "italic");
350 break;
353 name = (char *)FcNameUnparse(pattern);
354 copy = WMCreateFont(scrPtr, name);
355 FcPatternDestroy(pattern);
356 wfree(name);
358 return copy;