show workspace name when changing workspace
[wmaker-crm.git] / WINGs / wfont.c
blob6084f4997b29652d14401edbecab227ddbbebfaa
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 = malloc(sizeof(WMFont));
79 if (!font)
80 return NULL;
81 memset(font, 0, sizeof(WMFont));
83 font->notFontSet = 1;
85 font->screen = scrPtr;
87 font->font.normal = XLoadQueryFont(display, fontName);
88 if (!font->font.normal) {
89 free(font);
90 return NULL;
93 font->height = font->font.normal->ascent+font->font.normal->descent;
94 font->y = font->font.normal->ascent;
96 font->refCount = 1;
98 return font;
103 WMFont*
104 WMRetainFont(WMFont *font)
106 wassertrv(font!=NULL, NULL);
108 font->refCount++;
110 return font;
114 void
115 WMReleaseFont(WMFont *font)
117 wassertr(font!=NULL);
119 font->refCount--;
120 if (font->refCount < 1) {
121 if (font->notFontSet)
122 XFreeFont(font->screen->display, font->font.normal);
123 else {
124 XFreeFontSet(font->screen->display, font->font.set);
126 if (font->name) {
127 WMHashRemove(font->screen->fontCache, font->name);
128 free(font->name);
130 free(font);
136 unsigned int
137 WMFontHeight(WMFont *font)
139 wassertrv(font!=NULL, 0);
141 return font->height;
147 WMFont*
148 WMSystemFontOfSize(WMScreen *scrPtr, int size)
150 WMFont *font;
151 char *fontSpec;
153 fontSpec = makeFontSetOfSize(WINGsConfiguration.systemFont, size);
155 font = WMCreateFont(scrPtr, fontSpec);
157 if (!font) {
158 wwarning("could not load font set %s. Trying fixed.", fontSpec);
159 font = WMCreateFont(scrPtr, "fixed");
160 if (!font) {
161 font = WMCreateFont(scrPtr, "-*-fixed-medium-r-normal-*-14-*-*-*-*-*-*-*");
163 if (!font) {
164 wwarning("could not load fixed font!");
165 free(fontSpec);
166 return NULL;
169 free(fontSpec);
171 return font;
175 WMFont*
176 WMBoldSystemFontOfSize(WMScreen *scrPtr, int size)
178 WMFont *font;
179 char *fontSpec;
181 fontSpec = makeFontSetOfSize(WINGsConfiguration.boldSystemFont, size);
183 font = WMCreateFont(scrPtr, fontSpec);
185 if (!font) {
186 wwarning("could not load font set %s. Trying fixed.", fontSpec);
187 font = WMCreateFont(scrPtr, "fixed");
188 if (!font) {
189 font = WMCreateFont(scrPtr, "-*-fixed-medium-r-normal-*-14-*-*-*-*-*-*-*");
191 if (!font) {
192 wwarning("could not load fixed font!");
193 free(fontSpec);
194 return NULL;
197 free(fontSpec);
199 return font;
203 XFontSet
204 WMGetFontFontSet(WMFont *font)
206 wassertrv(font!=NULL, NULL);
208 if (font->notFontSet)
209 return NULL;
210 else
211 return font->font.set;
216 WMWidthOfString(WMFont *font, char *text, int length)
218 wassertrv(font!=NULL, 0);
219 wassertrv(text!=NULL, 0);
221 if (font->notFontSet)
222 return XTextWidth(font->font.normal, text, length);
223 else {
224 XRectangle rect;
225 XRectangle AIXsucks;
227 XmbTextExtents(font->font.set, text, length, &AIXsucks, &rect);
229 return rect.width;
235 void
236 WMDrawString(WMScreen *scr, Drawable d, GC gc, WMFont *font, int x, int y,
237 char *text, int length)
239 wassertr(font!=NULL);
241 if (font->notFontSet) {
242 XSetFont(scr->display, gc, font->font.normal->fid);
243 XDrawString(scr->display, d, gc, x, y + font->y, text, length);
244 } else {
245 XmbDrawString(scr->display, d, font->font.set, gc, x, y + font->y,
246 text, length);
251 void
252 WMDrawImageString(WMScreen *scr, Drawable d, GC gc, WMFont *font, int x, int y,
253 char *text, int length)
255 wassertr(font != NULL);
257 if (font->notFontSet) {
258 XSetFont(scr->display, gc, font->font.normal->fid);
259 XDrawImageString(scr->display, d, gc, x, y + font->y, text, length);
260 } else {
261 XmbDrawImageString(scr->display, d, font->font.set, gc, x, y + font->y,
262 text, length);
269 static char*
270 makeFontSetOfSize(char *fontset, int size)
272 char font[300];
273 char *newfs = NULL;
274 char *ptr;
275 char *tmp;
277 do {
278 int hold = ' ';
280 ptr = strchr(fontset, ',');
281 if (ptr) {
282 hold = *(ptr+1);
283 *(ptr+1) = 0;
285 if (strlen(fontset)>255) {
286 wwarning("font description %s is too large.", fontset);
287 } else {
288 sprintf(font, fontset, size);
289 tmp = wstrappend(newfs, font);
290 if (newfs)
291 free(newfs);
292 newfs = tmp;
294 if (ptr) {
295 *(ptr+1) = hold;
297 fontset = ptr+1;
298 } while (ptr!=NULL);
300 return newfs;