many bug fixes, finished some delegate code, updated menu file bug from EXEC
[wmaker-crm.git] / WINGs / wfont.c
blob86883a8449bc3a310f8d6c4c9147033ff61bdeaa
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 assert(WMHashInsert(scrPtr->fontCache, font->name, font)==NULL);
106 return font;
111 WMFont*
112 WMRetainFont(WMFont *font)
114 wassertrv(font!=NULL, NULL);
116 font->refCount++;
118 return font;
122 void
123 WMReleaseFont(WMFont *font)
125 wassertr(font!=NULL);
127 font->refCount--;
128 if (font->refCount < 1) {
129 if (font->notFontSet)
130 XFreeFont(font->screen->display, font->font.normal);
131 else {
132 XFreeFontSet(font->screen->display, font->font.set);
134 if (font->name) {
135 WMHashRemove(font->screen->fontCache, font->name);
136 free(font->name);
138 free(font);
144 unsigned int
145 WMFontHeight(WMFont *font)
147 wassertrv(font!=NULL, 0);
149 return font->height;
154 WMFont*
155 WMSystemFontOfSize(WMScreen *scrPtr, int size)
157 WMFont *font;
158 char *fontSpec;
160 fontSpec = makeFontSetOfSize(WINGsConfiguration.systemFont, size);
162 font = WMCreateFont(scrPtr, fontSpec);
164 if (!font) {
165 wwarning("could not load font set %s. Trying fixed.", fontSpec);
166 font = WMCreateFont(scrPtr, "fixed");
167 if (!font) {
168 font = WMCreateFont(scrPtr, "-*-fixed-medium-r-normal-*-14-*-*-*-*-*-*-*");
170 if (!font) {
171 wwarning("could not load fixed font!");
172 free(fontSpec);
173 return NULL;
176 free(fontSpec);
178 return font;
182 WMFont*
183 WMBoldSystemFontOfSize(WMScreen *scrPtr, int size)
185 WMFont *font;
186 char *fontSpec;
188 fontSpec = makeFontSetOfSize(WINGsConfiguration.boldSystemFont, size);
190 font = WMCreateFont(scrPtr, fontSpec);
192 if (!font) {
193 wwarning("could not load font set %s. Trying fixed.", fontSpec);
194 font = WMCreateFont(scrPtr, "fixed");
195 if (!font) {
196 font = WMCreateFont(scrPtr, "-*-fixed-medium-r-normal-*-14-*-*-*-*-*-*-*");
198 if (!font) {
199 wwarning("could not load fixed font!");
200 free(fontSpec);
201 return NULL;
204 free(fontSpec);
206 return font;
210 XFontSet
211 WMGetFontFontSet(WMFont *font)
213 wassertrv(font!=NULL, NULL);
215 if (font->notFontSet)
216 return NULL;
217 else
218 return font->font.set;
223 WMWidthOfString(WMFont *font, char *text, int length)
225 wassertrv(font!=NULL, 0);
226 wassertrv(text!=NULL, 0);
228 if (font->notFontSet)
229 return XTextWidth(font->font.normal, text, length);
230 else {
231 XRectangle rect;
232 XRectangle AIXsucks;
234 XmbTextExtents(font->font.set, text, length, &AIXsucks, &rect);
236 return rect.width;
242 void
243 WMDrawString(WMScreen *scr, Drawable d, GC gc, WMFont *font, int x, int y,
244 char *text, int length)
246 wassertr(font!=NULL);
248 if (font->notFontSet) {
249 XSetFont(scr->display, gc, font->font.normal->fid);
250 XDrawString(scr->display, d, gc, x, y + font->y, text, length);
251 } else {
252 XmbDrawString(scr->display, d, font->font.set, gc, x, y + font->y,
253 text, length);
258 void
259 WMDrawImageString(WMScreen *scr, Drawable d, GC gc, WMFont *font, int x, int y,
260 char *text, int length)
262 wassertr(font != NULL);
264 if (font->notFontSet) {
265 XSetFont(scr->display, gc, font->font.normal->fid);
266 XDrawImageString(scr->display, d, gc, x, y + font->y, text, length);
267 } else {
268 XmbDrawImageString(scr->display, d, font->font.set, gc, x, y + font->y,
269 text, length);
276 static char*
277 makeFontSetOfSize(char *fontset, int size)
279 char font[300];
280 char *newfs = NULL;
281 char *ptr;
282 char *tmp;
284 do {
285 int hold = ' ';
287 ptr = strchr(fontset, ',');
288 if (ptr) {
289 hold = *(ptr+1);
290 *(ptr+1) = 0;
292 if (strlen(fontset)>255) {
293 wwarning("font description %s is too large.", fontset);
294 } else {
295 sprintf(font, fontset, size);
296 tmp = wstrappend(newfs, font);
297 if (newfs)
298 free(newfs);
299 newfs = tmp;
301 if (ptr) {
302 *(ptr+1) = hold;
304 fontset = ptr+1;
305 } while (ptr!=NULL);
307 return newfs;