Code update for Window Maker version 0.50.0
[wmaker-crm.git] / WINGs / wfont.c
blob7671f42e80c9d96cdcae505999b5d84efb03d09d
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 wwarning("could not load fixed font!");
145 free(fontSpec);
146 return NULL;
149 free(fontSpec);
151 return font;
155 WMFont*
156 WMBoldSystemFontOfSize(WMScreen *scrPtr, int size)
158 WMFont *font;
159 char *fontSpec;
161 fontSpec = makeFontSetOfSize(WINGsConfiguration.boldSystemFont, size);
163 font = WMCreateFont(scrPtr, fontSpec);
165 if (!font) {
166 wwarning("could not load font set %s. Trying fixed.", fontSpec);
167 font = WMCreateFont(scrPtr, "fixed");
168 if (!font) {
169 wwarning("could not load fixed font!");
170 free(fontSpec);
171 return NULL;
174 free(fontSpec);
176 return font;
180 XFontSet
181 WMGetFontFontSet(WMFont *font)
183 wassertrv(font!=NULL, NULL);
185 if (font->notFontSet)
186 return NULL;
187 else
188 return font->font.set;
193 WMWidthOfString(WMFont *font, char *text, int length)
195 wassertrv(font!=NULL, 0);
196 wassertrv(text!=NULL, 0);
198 if (font->notFontSet)
199 return XTextWidth(font->font.normal, text, length);
200 else {
201 XRectangle rect;
202 XRectangle AIXsucks;
204 XmbTextExtents(font->font.set, text, length, &AIXsucks, &rect);
206 return rect.width;
212 void
213 WMDrawString(WMScreen *scr, Drawable d, GC gc, WMFont *font, int x, int y,
214 char *text, int length)
216 wassertr(font!=NULL);
218 if (font->notFontSet) {
219 XSetFont(scr->display, gc, font->font.normal->fid);
220 XDrawString(scr->display, d, gc, x, y + font->y, text, length);
221 } else {
222 XmbDrawString(scr->display, d, font->font.set, gc, x, y + font->y,
223 text, length);
228 void
229 WMDrawImageString(WMScreen *scr, Drawable d, GC gc, WMFont *font, int x, int y,
230 char *text, int length)
232 wassertr(font != NULL);
234 if (font->notFontSet) {
235 XSetFont(scr->display, gc, font->font.normal->fid);
236 XDrawImageString(scr->display, d, gc, x, y + font->y, text, length);
237 } else {
238 XmbDrawImageString(scr->display, d, font->font.set, gc, x, y + font->y,
239 text, length);
246 static char*
247 makeFontSetOfSize(char *fontset, int size)
249 char font[300];
250 char *newfs = NULL;
251 char *ptr;
252 char *tmp;
254 do {
255 int hold = ' ';
257 ptr = strchr(fontset, ',');
258 if (ptr) {
259 hold = *(ptr+1);
260 *(ptr+1) = 0;
262 if (strlen(fontset)>255) {
263 wwarning("font description %s is too large.", fontset);
264 } else {
265 sprintf(font, fontset, size);
266 tmp = wstrappend(newfs, font);
267 if (newfs)
268 free(newfs);
269 newfs = tmp;
271 if (ptr) {
272 *(ptr+1) = hold;
274 fontset = ptr+1;
275 } while (ptr!=NULL);
277 return newfs;