added a runtime disabling of multibyte text support
[wmaker-crm.git] / WINGs / wfont.c
bloba2393b48fb1428e62d5984f7e9888de6e9372124
2 #include "WINGsP.h"
5 #include <wraster.h>
6 #include <assert.h>
8 static char *makeFontSetOfSize(char *fontset, int size);
11 WMFont*
12 WMCreateFont(WMScreen *scrPtr, char *fontName)
14 WMFont *font;
15 Display *display = scrPtr->display;
16 char **missing;
17 int nmissing = 0;
18 char *defaultString;
19 XFontSetExtents *extents;
21 font = WMHashGet(scrPtr->fontCache, fontName);
22 if (font) {
23 WMRetainFont(font);
24 return font;
27 font = malloc(sizeof(WMFont));
28 if (!font)
29 return NULL;
30 memset(font, 0, sizeof(WMFont));
32 font->notFontSet = 0;
34 font->screen = scrPtr;
36 font->font.set = XCreateFontSet(display, fontName, &missing, &nmissing,
37 &defaultString);
38 if (nmissing > 0 && font->font.set) {
39 int i;
41 wwarning("the following character sets are missing in %s:",
42 fontName);
43 for (i = 0; i < nmissing; i++) {
44 wwarning(missing[i]);
46 XFreeStringList(missing);
47 if (defaultString)
48 wwarning("the string \"%s\" will be used in place of any characters from those sets.",
49 defaultString);
51 if (!font->font.set) {
52 free(font);
53 return NULL;
56 extents = XExtentsOfFontSet(font->font.set);
58 font->height = extents->max_logical_extent.height;
59 font->y = font->height - (font->height + extents->max_logical_extent.y);
61 font->refCount = 1;
63 font->name = wstrdup(fontName);
65 assert(WMHashInsert(scrPtr->fontCache, font->name, font)==NULL);
67 return font;
72 WMFont*
73 WMCreateFontInDefaultEncoding(WMScreen *scrPtr, char *fontName)
75 WMFont *font;
76 Display *display = scrPtr->display;
78 font = WMHashGet(scrPtr->fontCache, fontName);
79 if (font) {
80 WMRetainFont(font);
81 return font;
84 font = malloc(sizeof(WMFont));
85 if (!font)
86 return NULL;
87 memset(font, 0, sizeof(WMFont));
89 font->notFontSet = 1;
91 font->screen = scrPtr;
93 font->font.normal = XLoadQueryFont(display, fontName);
94 if (!font->font.normal) {
95 free(font);
96 return NULL;
99 font->height = font->font.normal->ascent+font->font.normal->descent;
100 font->y = font->font.normal->ascent;
102 font->refCount = 1;
104 font->name = wstrdup(fontName);
106 assert(WMHashInsert(scrPtr->fontCache, font->name, font)==NULL);
108 return font;
113 WMFont*
114 WMRetainFont(WMFont *font)
116 wassertrv(font!=NULL, NULL);
118 font->refCount++;
120 return font;
124 void
125 WMReleaseFont(WMFont *font)
127 wassertr(font!=NULL);
129 font->refCount--;
130 if (font->refCount < 1) {
131 if (font->notFontSet)
132 XFreeFont(font->screen->display, font->font.normal);
133 else {
134 XFreeFontSet(font->screen->display, font->font.set);
136 if (font->name) {
137 WMHashRemove(font->screen->fontCache, font->name);
138 free(font->name);
140 free(font);
146 unsigned int
147 WMFontHeight(WMFont *font)
149 wassertrv(font!=NULL, 0);
151 return font->height;
156 WMFont*
157 WMSystemFontOfSize(WMScreen *scrPtr, int size)
159 WMFont *font;
160 char *fontSpec;
162 fontSpec = makeFontSetOfSize(WINGsConfiguration.systemFont, size);
164 if (WINGsConfiguration.useMultiByte)
165 font = WMCreateFont(scrPtr, fontSpec);
166 else
167 font = WMCreateFontInDefaultEncoding(scrPtr, fontSpec);
169 if (!font) {
170 if (WINGsConfiguration.useMultiByte) {
171 wwarning("could not load font set %s. Trying fixed.", fontSpec);
172 font = WMCreateFont(scrPtr, "fixed");
173 if (!font) {
174 font = WMCreateFont(scrPtr, "-*-fixed-medium-r-normal-*-14-*-*-*-*-*-*-*");
176 } else {
177 wwarning("could not load font %s. Trying fixed.", fontSpec);
178 font = WMCreateFontInDefaultEncoding(scrPtr, "fixed");
180 if (!font) {
181 wwarning("could not load fixed font!");
182 free(fontSpec);
183 return NULL;
186 free(fontSpec);
188 return font;
192 WMFont*
193 WMBoldSystemFontOfSize(WMScreen *scrPtr, int size)
195 WMFont *font;
196 char *fontSpec;
198 fontSpec = makeFontSetOfSize(WINGsConfiguration.boldSystemFont, size);
200 if (WINGsConfiguration.useMultiByte)
201 font = WMCreateFont(scrPtr, fontSpec);
202 else
203 font = WMCreateFontInDefaultEncoding(scrPtr, fontSpec);
205 if (!font) {
206 if (WINGsConfiguration.useMultiByte) {
207 wwarning("could not load font set %s. Trying fixed.", fontSpec);
208 font = WMCreateFont(scrPtr, "fixed");
209 if (!font) {
210 font = WMCreateFont(scrPtr, "-*-fixed-medium-r-normal-*-14-*-*-*-*-*-*-*");
212 } else {
213 wwarning("could not load font %s. Trying fixed.", fontSpec);
214 font = WMCreateFontInDefaultEncoding(scrPtr, "fixed");
216 if (!font) {
217 wwarning("could not load fixed font!");
218 free(fontSpec);
219 return NULL;
222 free(fontSpec);
224 return font;
228 XFontSet
229 WMGetFontFontSet(WMFont *font)
231 wassertrv(font!=NULL, NULL);
233 if (font->notFontSet)
234 return NULL;
235 else
236 return font->font.set;
241 WMWidthOfString(WMFont *font, char *text, int length)
243 wassertrv(font!=NULL, 0);
244 wassertrv(text!=NULL, 0);
246 if (font->notFontSet)
247 return XTextWidth(font->font.normal, text, length);
248 else {
249 XRectangle rect;
250 XRectangle AIXsucks;
252 XmbTextExtents(font->font.set, text, length, &AIXsucks, &rect);
254 return rect.width;
260 void
261 WMDrawString(WMScreen *scr, Drawable d, GC gc, WMFont *font, int x, int y,
262 char *text, int length)
264 wassertr(font!=NULL);
266 if (font->notFontSet) {
267 XSetFont(scr->display, gc, font->font.normal->fid);
268 XDrawString(scr->display, d, gc, x, y + font->y, text, length);
269 } else {
270 XmbDrawString(scr->display, d, font->font.set, gc, x, y + font->y,
271 text, length);
276 void
277 WMDrawImageString(WMScreen *scr, Drawable d, GC gc, WMFont *font, int x, int y,
278 char *text, int length)
280 wassertr(font != NULL);
282 if (font->notFontSet) {
283 XSetFont(scr->display, gc, font->font.normal->fid);
284 XDrawImageString(scr->display, d, gc, x, y + font->y, text, length);
285 } else {
286 XmbDrawImageString(scr->display, d, font->font.set, gc, x, y + font->y,
287 text, length);
294 static char*
295 makeFontSetOfSize(char *fontset, int size)
297 char font[300];
298 char *newfs = NULL;
299 char *ptr;
300 char *tmp;
302 do {
303 int hold = ' ';
305 ptr = strchr(fontset, ',');
306 if (ptr) {
307 hold = *(ptr+1);
308 *(ptr+1) = 0;
310 if (strlen(fontset)>255) {
311 wwarning("font description %s is too large.", fontset);
312 } else {
313 sprintf(font, fontset, size);
314 tmp = wstrappend(newfs, font);
315 if (newfs)
316 free(newfs);
317 newfs = tmp;
319 if (ptr) {
320 *(ptr+1) = hold;
322 fontset = ptr+1;
323 } while (ptr!=NULL);
325 return newfs;