Initial revision
[wmaker-crm.git] / WINGs / wfont.c
blobc6759e1d17ce439960943e819f7b135fc2b5732e
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 assert(font!=NULL);
96 font->refCount++;
98 return font;
102 void
103 WMReleaseFont(WMFont *font)
105 assert(font!=NULL);
106 font->refCount--;
107 if (font->refCount < 1) {
108 if (font->notFontSet)
109 XFreeFont(font->screen->display, font->font.normal);
110 else
111 XFreeFontSet(font->screen->display, font->font.set);
112 free(font);
118 unsigned int
119 WMFontHeight(WMFont *font)
121 assert(font!=NULL);
123 return font->height;
129 WMFont*
130 WMSystemFontOfSize(WMScreen *scrPtr, int size)
132 WMFont *font;
133 char *fontSpec;
135 fontSpec = makeFontSetOfSize(WINGsConfiguration.systemFont, size);
137 font = WMCreateFont(scrPtr, fontSpec);
139 if (!font) {
140 wwarning("could not load font set %s. Trying fixed.", fontSpec);
141 font = WMCreateFont(scrPtr, "fixed");
142 if (!font) {
143 wwarning("could not load fixed font!");
144 free(fontSpec);
145 return NULL;
148 free(fontSpec);
150 return font;
154 WMFont*
155 WMBoldSystemFontOfSize(WMScreen *scrPtr, int size)
157 WMFont *font;
158 char *fontSpec;
160 fontSpec = makeFontSetOfSize(WINGsConfiguration.boldSystemFont, 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 wwarning("could not load fixed font!");
169 free(fontSpec);
170 return NULL;
173 free(fontSpec);
175 return font;
179 XFontSet
180 WMGetFontFontSet(WMFont *font)
182 if (font->notFontSet)
183 return NULL;
184 else
185 return font->font.set;
190 WMWidthOfString(WMFont *font, char *text, int length)
192 assert(font!=NULL);
193 assert(text!=NULL);
195 if (font->notFontSet)
196 return XTextWidth(font->font.normal, text, length);
197 else {
198 XRectangle rect;
199 XRectangle AIXsucks;
201 XmbTextExtents(font->font.set, text, length, &AIXsucks, &rect);
203 return rect.width;
209 void
210 WMDrawString(WMScreen *scr, Drawable d, GC gc, WMFont *font, int x, int y,
211 char *text, int length)
213 if (font->notFontSet) {
214 XSetFont(scr->display, gc, font->font.normal->fid);
215 XDrawString(scr->display, d, gc, x, y + font->y, text, length);
216 } else {
217 XmbDrawString(scr->display, d, font->font.set, gc, x, y + font->y,
218 text, length);
223 void
224 WMDrawImageString(WMScreen *scr, Drawable d, GC gc, WMFont *font, int x, int y,
225 char *text, int length)
227 if (font->notFontSet) {
228 XSetFont(scr->display, gc, font->font.normal->fid);
229 XDrawImageString(scr->display, d, gc, x, y + font->y, text, length);
230 } else {
231 XmbDrawImageString(scr->display, d, font->font.set, gc, x, y + font->y,
232 text, length);
239 static char*
240 makeFontSetOfSize(char *fontset, int size)
242 char font[300];
243 char *newfs = NULL;
244 char *ptr;
245 char *tmp;
247 do {
248 int hold = ' ';
250 ptr = strchr(fontset, ',');
251 if (ptr) {
252 hold = *(ptr+1);
253 *(ptr+1) = 0;
255 if (strlen(fontset)>255) {
256 wwarning("font description %s is too large.", fontset);
257 } else {
258 sprintf(font, fontset, size);
259 tmp = wstrappend(newfs, font);
260 if (newfs)
261 free(newfs);
262 newfs = tmp;
264 if (ptr) {
265 *(ptr+1) = hold;
267 fontset = ptr+1;
268 } while (ptr!=NULL);
270 return newfs;