0.51.1 pre snapshot. Be careful, it may be buggy. It fixes some bugs though.
[wmaker-crm.git] / WINGs / wfont.c
blob91c9feb79ee6a3da9c4b4099d2fbef9f30670eb6
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 = malloc(sizeof(WMFont));
22 if (!font)
23 return NULL;
25 font->notFontSet = 0;
27 font->screen = scrPtr;
29 font->font.set = XCreateFontSet(display, fontName, &missing, &nmissing,
30 &defaultString);
31 if (nmissing > 0 && font->font.set) {
32 int i;
34 wwarning("the following character sets are missing in %s:",
35 fontName);
36 for (i = 0; i < nmissing; i++) {
37 wwarning(missing[i]);
39 XFreeStringList(missing);
40 if (defaultString)
41 wwarning("the string \"%s\" will be used in place of any characters from those sets.",
42 defaultString);
44 if (!font->font.set) {
45 free(font);
46 return NULL;
49 extents = XExtentsOfFontSet(font->font.set);
51 font->height = extents->max_logical_extent.height;
52 font->y = font->height - (font->height + extents->max_logical_extent.y);
54 font->refCount = 1;
56 return font;
61 WMFont*
62 WMCreateFontInDefaultEncoding(WMScreen *scrPtr, char *fontName)
64 WMFont *font;
65 Display *display = scrPtr->display;
67 font = malloc(sizeof(WMFont));
68 if (!font)
69 return NULL;
71 font->notFontSet = 1;
73 font->screen = scrPtr;
75 font->font.normal = XLoadQueryFont(display, fontName);
76 if (!font->font.normal) {
77 free(font);
78 return NULL;
81 font->height = font->font.normal->ascent+font->font.normal->descent;
82 font->y = font->font.normal->ascent;
84 font->refCount = 1;
86 return font;
91 WMFont*
92 WMRetainFont(WMFont *font)
94 wassertrv(font!=NULL, NULL);
96 font->refCount++;
98 return font;
102 void
103 WMReleaseFont(WMFont *font)
105 wassertr(font!=NULL);
107 font->refCount--;
108 if (font->refCount < 1) {
109 if (font->notFontSet)
110 XFreeFont(font->screen->display, font->font.normal);
111 else
112 XFreeFontSet(font->screen->display, font->font.set);
113 free(font);
119 unsigned int
120 WMFontHeight(WMFont *font)
122 wassertrv(font!=NULL, 0);
124 return font->height;
130 WMFont*
131 WMSystemFontOfSize(WMScreen *scrPtr, int size)
133 WMFont *font;
134 char *fontSpec;
136 fontSpec = makeFontSetOfSize(WINGsConfiguration.systemFont, size);
138 font = WMCreateFont(scrPtr, fontSpec);
140 if (!font) {
141 wwarning("could not load font set %s. Trying fixed.", fontSpec);
142 font = WMCreateFont(scrPtr, "fixed");
143 if (!font) {
144 font = WMCreateFont(scrPtr, "-*-fixed-medium-r-normal-*-14-*-*-*-*-*-*-*");
146 if (!font) {
147 wwarning("could not load fixed font!");
148 free(fontSpec);
149 return NULL;
152 free(fontSpec);
154 return font;
158 WMFont*
159 WMBoldSystemFontOfSize(WMScreen *scrPtr, int size)
161 WMFont *font;
162 char *fontSpec;
164 fontSpec = makeFontSetOfSize(WINGsConfiguration.boldSystemFont, size);
166 font = WMCreateFont(scrPtr, fontSpec);
168 if (!font) {
169 wwarning("could not load font set %s. Trying fixed.", fontSpec);
170 font = WMCreateFont(scrPtr, "fixed");
171 if (!font) {
172 font = WMCreateFont(scrPtr, "-*-fixed-medium-r-normal-*-14-*-*-*-*-*-*-*");
174 if (!font) {
175 wwarning("could not load fixed font!");
176 free(fontSpec);
177 return NULL;
180 free(fontSpec);
182 return font;
186 XFontSet
187 WMGetFontFontSet(WMFont *font)
189 wassertrv(font!=NULL, NULL);
191 if (font->notFontSet)
192 return NULL;
193 else
194 return font->font.set;
199 WMWidthOfString(WMFont *font, char *text, int length)
201 wassertrv(font!=NULL, 0);
202 wassertrv(text!=NULL, 0);
204 if (font->notFontSet)
205 return XTextWidth(font->font.normal, text, length);
206 else {
207 XRectangle rect;
208 XRectangle AIXsucks;
210 XmbTextExtents(font->font.set, text, length, &AIXsucks, &rect);
212 return rect.width;
218 void
219 WMDrawString(WMScreen *scr, Drawable d, GC gc, WMFont *font, int x, int y,
220 char *text, int length)
222 wassertr(font!=NULL);
224 if (font->notFontSet) {
225 XSetFont(scr->display, gc, font->font.normal->fid);
226 XDrawString(scr->display, d, gc, x, y + font->y, text, length);
227 } else {
228 XmbDrawString(scr->display, d, font->font.set, gc, x, y + font->y,
229 text, length);
234 void
235 WMDrawImageString(WMScreen *scr, Drawable d, GC gc, WMFont *font, int x, int y,
236 char *text, int length)
238 wassertr(font != NULL);
240 if (font->notFontSet) {
241 XSetFont(scr->display, gc, font->font.normal->fid);
242 XDrawImageString(scr->display, d, gc, x, y + font->y, text, length);
243 } else {
244 XmbDrawImageString(scr->display, d, font->font.set, gc, x, y + font->y,
245 text, length);
252 static char*
253 makeFontSetOfSize(char *fontset, int size)
255 char font[300];
256 char *newfs = NULL;
257 char *ptr;
258 char *tmp;
260 do {
261 int hold = ' ';
263 ptr = strchr(fontset, ',');
264 if (ptr) {
265 hold = *(ptr+1);
266 *(ptr+1) = 0;
268 if (strlen(fontset)>255) {
269 wwarning("font description %s is too large.", fontset);
270 } else {
271 sprintf(font, fontset, size);
272 tmp = wstrappend(newfs, font);
273 if (newfs)
274 free(newfs);
275 newfs = tmp;
277 if (ptr) {
278 *(ptr+1) = hold;
280 fontset = ptr+1;
281 } while (ptr!=NULL);
283 return newfs;