Change to the linux kernel coding style
[wmaker-crm.git] / WINGs / wfont_noxft.c
1
2 #include "wconfig.h"
3
4 #ifndef XFT
5
6 #include "WINGsP.h"
7
8 #include <wraster.h>
9 #include <assert.h>
10 #include <X11/Xlocale.h>
11
12 static char *makeFontSetOfSize(char *fontset, int size);
13
14 /* XLFD pattern matching */
15 static char *getElementFromXLFD(const char *xlfd, int index)
16 {
17 const char *p = xlfd;
18 while (*p != 0) {
19 if (*p == '-' && --index == 0) {
20 const char *end = strchr(p + 1, '-');
21 char *buf;
22 size_t len;
23 if (end == 0)
24 end = p + strlen(p);
25 len = end - (p + 1);
26 buf = wmalloc(len);
27 memcpy(buf, p + 1, len);
28 buf[len] = 0;
29 return buf;
30 }
31 p++;
32 }
33 return strdup("*");
34 }
35
36 /* XLFD pattern matching */
37 static char *generalizeXLFD(const char *xlfd)
38 {
39 char *buf;
40 int len;
41 char *weight = getElementFromXLFD(xlfd, 3);
42 char *slant = getElementFromXLFD(xlfd, 4);
43 char *pxlsz = getElementFromXLFD(xlfd, 7);
44
45 #define Xstrlen(A) ((A)?strlen(A):0)
46 len = Xstrlen(xlfd) + Xstrlen(weight) + Xstrlen(slant) + Xstrlen(pxlsz) * 2 + 60;
47 #undef Xstrlen
48
49 buf = wmalloc(len + 1);
50 snprintf(buf, len + 1, "%s,-*-*-%s-%s-*-*-%s-*-*-*-*-*-*-*,"
51 "-*-*-*-*-*-*-%s-*-*-*-*-*-*-*,*", xlfd, weight, slant, pxlsz, pxlsz);
52
53 wfree(pxlsz);
54 wfree(slant);
55 wfree(weight);
56
57 return buf;
58 }
59
60 /* XLFD pattern matching */
61 static XFontSet
62 W_CreateFontSetWithGuess(Display * dpy, char *xlfd, char ***missing, int *nmissing, char **def_string)
63 {
64 XFontSet fs = XCreateFontSet(dpy, xlfd, missing, nmissing, def_string);
65
66 if (fs != NULL && *nmissing == 0)
67 return fs;
68
69 /* for non-iso8859-1 language and iso8859-1 specification
70 (this fontset is only for pattern analysis) */
71 if (fs == NULL) {
72 if (*nmissing != 0)
73 XFreeStringList(*missing);
74 setlocale(LC_CTYPE, "C");
75 fs = XCreateFontSet(dpy, xlfd, missing, nmissing, def_string);
76 setlocale(LC_CTYPE, "");
77 }
78
79 /* make XLFD font name for pattern analysis */
80 if (fs != NULL) {
81 XFontStruct **fontstructs;
82 char **fontnames;
83 if (XFontsOfFontSet(fs, &fontstructs, &fontnames) > 0)
84 xlfd = fontnames[0];
85 }
86
87 xlfd = generalizeXLFD(xlfd);
88
89 if (*nmissing != 0)
90 XFreeStringList(*missing);
91 if (fs != NULL)
92 XFreeFontSet(dpy, fs);
93
94 fs = XCreateFontSet(dpy, xlfd, missing, nmissing, def_string);
95
96 wfree(xlfd);
97 return fs;
98 }
99
100 static char *xlfdFromFontName(char *fontName)
101 {
102 char *systemFont, *boldSystemFont;
103 char *font;
104 int size;
105
106 systemFont = WINGsConfiguration.systemFont;
107 boldSystemFont = WINGsConfiguration.boldSystemFont;
108
109 size = WINGsConfiguration.defaultFontSize;
110
111 if (strcmp(fontName, "SystemFont") == 0) {
112 font = systemFont;
113 } else if (strncmp(fontName, "SystemFont-", 11) == 0) {
114 font = systemFont;
115 if (sscanf(&fontName[11], "%i", &size) != 1) {
116 size = WINGsConfiguration.defaultFontSize;
117 wwarning(_("Invalid size specification '%s' in %s. "
118 "Using default %d\n"), &fontName[11], fontName, size);
119 }
120 } else if (strcmp(fontName, "BoldSystemFont") == 0) {
121 font = boldSystemFont;
122 } else if (strncmp(fontName, "BoldSystemFont-", 15) == 0) {
123 font = boldSystemFont;
124 if (sscanf(&fontName[15], "%i", &size) != 1) {
125 size = WINGsConfiguration.defaultFontSize;
126 wwarning(_("Invalid size specification '%s' in %s. "
127 "Using default %d\n"), &fontName[15], fontName, size);
128 }
129 } else {
130 font = NULL;
131 }
132
133 return (font != NULL ? makeFontSetOfSize(font, size) : wstrdup(fontName));
134 }
135
136 WMFont *WMCreateFontSet(WMScreen * scrPtr, char *fontName)
137 {
138 WMFont *font;
139 Display *display = scrPtr->display;
140 char **missing;
141 int nmissing = 0;
142 char *defaultString;
143 char *fname;
144 XFontSetExtents *extents;
145
146 fname = xlfdFromFontName(fontName);
147
148 font = WMHashGet(scrPtr->fontSetCache, fname);
149 if (font) {
150 WMRetainFont(font);
151 wfree(fname);
152 return font;
153 }
154
155 font = wmalloc(sizeof(WMFont));
156 memset(font, 0, sizeof(WMFont));
157
158 font->notFontSet = 0;
159
160 font->screen = scrPtr;
161
162 font->font.set = W_CreateFontSetWithGuess(display, fname, &missing, &nmissing, &defaultString);
163 if (nmissing > 0 && font->font.set) {
164 int i;
165
166 wwarning(_("the following character sets are missing in %s:"), fname);
167 for (i = 0; i < nmissing; i++) {
168 wwarning(missing[i]);
169 }
170 XFreeStringList(missing);
171 if (defaultString)
172 wwarning(_("the string \"%s\" will be used in place of any characters from those sets."),
173 defaultString);
174 }
175 if (!font->font.set) {
176 wfree(font);
177 wfree(fname);
178 return NULL;
179 }
180
181 extents = XExtentsOfFontSet(font->font.set);
182
183 font->height = extents->max_logical_extent.height;
184 font->y = font->height - (font->height + extents->max_logical_extent.y);
185
186 font->refCount = 1;
187
188 font->name = fname;
189
190 assert(WMHashInsert(scrPtr->fontSetCache, font->name, font) == NULL);
191
192 return font;
193 }
194
195 WMFont *WMCreateNormalFont(WMScreen * scrPtr, char *fontName)
196 {
197 WMFont *font;
198 Display *display = scrPtr->display;
199 char *fname, *ptr;
200
201 fontName = xlfdFromFontName(fontName);
202
203 if ((ptr = strchr(fontName, ','))) {
204 fname = wmalloc(ptr - fontName + 1);
205 strncpy(fname, fontName, ptr - fontName);
206 fname[ptr - fontName] = 0;
207 } else {
208 fname = wstrdup(fontName);
209 }
210
211 wfree(fontName);
212
213 font = WMHashGet(scrPtr->fontCache, fname);
214 if (font) {
215 WMRetainFont(font);
216 wfree(fname);
217 return font;
218 }
219
220 font = wmalloc(sizeof(WMFont));
221 memset(font, 0, sizeof(WMFont));
222
223 font->notFontSet = 1;
224
225 font->screen = scrPtr;
226
227 font->font.normal = XLoadQueryFont(display, fname);
228 if (!font->font.normal) {
229 wfree(font);
230 wfree(fname);
231 return NULL;
232 }
233 font->height = font->font.normal->ascent + font->font.normal->descent;
234 font->y = font->font.normal->ascent;
235
236 font->refCount = 1;
237
238 font->name = fname;
239
240 assert(WMHashInsert(scrPtr->fontCache, font->name, font) == NULL);
241
242 return font;
243 }
244
245 WMFont *WMCreateFont(WMScreen * scrPtr, char *fontName)
246 {
247 return WMCreateFontWithFlags(scrPtr, fontName, WFDefaultFont);
248 }
249
250 WMFont *WMCreateFontWithFlags(WMScreen * scrPtr, char *fontName, WMFontFlags flags)
251 {
252 Bool multiByte = scrPtr->useMultiByte;
253 WMFont *font;
254
255 if (flags & WFFontSet) {
256 multiByte = True;
257 } else if (flags & WFNormalFont) {
258 multiByte = False;
259 }
260
261 if (multiByte) {
262 font = WMCreateFontSet(scrPtr, fontName);
263 } else {
264 font = WMCreateNormalFont(scrPtr, fontName);
265 }
266
267 return font;
268 }
269
270 WMFont *WMRetainFont(WMFont * font)
271 {
272 wassertrv(font != NULL, NULL);
273
274 font->refCount++;
275
276 return font;
277 }
278
279 void WMReleaseFont(WMFont * font)
280 {
281 wassertr(font != NULL);
282
283 font->refCount--;
284 if (font->refCount < 1) {
285 if (font->notFontSet) {
286 XFreeFont(font->screen->display, font->font.normal);
287 } else {
288 XFreeFontSet(font->screen->display, font->font.set);
289 }
290
291 if (font->name) {
292 if (font->notFontSet) {
293 WMHashRemove(font->screen->fontCache, font->name);
294 } else {
295 WMHashRemove(font->screen->fontSetCache, font->name);
296 }
297 wfree(font->name);
298 }
299 wfree(font);
300 }
301 }
302
303 Bool WMIsAntialiasingEnabled(WMScreen * scrPtr)
304 {
305 return False;
306 }
307
308 unsigned int WMFontHeight(WMFont * font)
309 {
310 wassertrv(font != NULL, 0);
311
312 return font->height;
313 }
314
315 char *WMGetFontName(WMFont * font)
316 {
317 wassertrv(font != NULL, NULL);
318
319 return font->name;
320 }
321
322 WMFont *WMDefaultSystemFont(WMScreen * scrPtr)
323 {
324 return WMRetainFont(scrPtr->normalFont);
325 }
326
327 WMFont *WMDefaultBoldSystemFont(WMScreen * scrPtr)
328 {
329 return WMRetainFont(scrPtr->boldFont);
330 }
331
332 static WMFont *makeSystemFontOfSize(WMScreen * scrPtr, int size, Bool bold)
333 {
334 WMFont *font;
335 char *fontSpec;
336
337 #define WConf WINGsConfiguration
338 if (bold) {
339 fontSpec = makeFontSetOfSize(WConf.boldSystemFont, size);
340 } else {
341 fontSpec = makeFontSetOfSize(WConf.systemFont, size);
342 }
343 #undef WConf
344
345 if (scrPtr->useMultiByte) {
346 font = WMCreateFontSet(scrPtr, fontSpec);
347 } else {
348 font = WMCreateNormalFont(scrPtr, fontSpec);
349 }
350
351 if (!font) {
352 if (scrPtr->useMultiByte) {
353 wwarning(_("could not load font set %s. Trying fixed."), fontSpec);
354 font = WMCreateFontSet(scrPtr, "fixed");
355 if (!font) {
356 font = WMCreateFontSet(scrPtr, "-*-fixed-medium-r-normal-*-14-*-*-*-*-*-*-*");
357 }
358 } else {
359 wwarning(_("could not load font %s. Trying fixed."), fontSpec);
360 font = WMCreateNormalFont(scrPtr, "fixed");
361 }
362 if (!font) {
363 wwarning(_("could not load fixed font!"));
364 wfree(fontSpec);
365 return NULL;
366 }
367 }
368 wfree(fontSpec);
369
370 return font;
371 }
372
373 WMFont *WMSystemFontOfSize(WMScreen * scrPtr, int size)
374 {
375 return makeSystemFontOfSize(scrPtr, size, False);
376 }
377
378 WMFont *WMBoldSystemFontOfSize(WMScreen * scrPtr, int size)
379 {
380 return makeSystemFontOfSize(scrPtr, size, True);
381 }
382
383 XFontSet WMGetFontFontSet(WMFont * font)
384 {
385 wassertrv(font != NULL, NULL);
386
387 if (!font->notFontSet && !font->antialiased)
388 return font->font.set;
389
390 return NULL;
391 }
392
393 int WMWidthOfString(WMFont * font, char *text, int length)
394 {
395 wassertrv(font != NULL, 0);
396 wassertrv(text != NULL, 0);
397
398 if (font->notFontSet) {
399 return XTextWidth(font->font.normal, text, length);
400 } else {
401 XRectangle rect;
402 XRectangle AIXsucks;
403
404 XmbTextExtents(font->font.set, text, length, &AIXsucks, &rect);
405
406 return rect.width;
407 }
408 }
409
410 void WMDrawString(WMScreen * scr, Drawable d, WMColor * color, WMFont * font, int x, int y, char *text, int length)
411 {
412 wassertr(font != NULL);
413
414 if (font->notFontSet) {
415 XSetFont(scr->display, scr->drawStringGC, font->font.normal->fid);
416 XSetForeground(scr->display, scr->drawStringGC, W_PIXEL(color));
417 XDrawString(scr->display, d, scr->drawStringGC, x, y + font->y, text, length);
418 } else {
419 XSetForeground(scr->display, scr->drawStringGC, W_PIXEL(color));
420 XmbDrawString(scr->display, d, font->font.set, scr->drawStringGC, x, y + font->y, text, length);
421 }
422 }
423
424 void
425 WMDrawImageString(WMScreen * scr, Drawable d, WMColor * color, WMColor * background,
426 WMFont * font, int x, int y, char *text, int length)
427 {
428 wassertr(font != NULL);
429
430 if (font->notFontSet) {
431 XSetForeground(scr->display, scr->drawImStringGC, W_PIXEL(color));
432 XSetBackground(scr->display, scr->drawImStringGC, W_PIXEL(background));
433 XSetFont(scr->display, scr->drawImStringGC, font->font.normal->fid);
434 XDrawImageString(scr->display, d, scr->drawImStringGC, x, y + font->y, text, length);
435 } else {
436 XSetForeground(scr->display, scr->drawImStringGC, W_PIXEL(color));
437 XSetBackground(scr->display, scr->drawImStringGC, W_PIXEL(background));
438 XmbDrawImageString(scr->display, d, font->font.set, scr->drawImStringGC,
439 x, y + font->y, text, length);
440 }
441 }
442
443 static char *makeFontSetOfSize(char *fontset, int size)
444 {
445 char font[300], *f;
446 char *newfs = NULL;
447 char *ptr;
448
449 do {
450 char *tmp;
451 int end;
452
453 f = fontset;
454 ptr = strchr(fontset, ',');
455 if (ptr) {
456 int count = ptr - fontset;
457
458 if (count > 255) {
459 wwarning(_("font description %s is too large."), fontset);
460 } else {
461 memcpy(font, fontset, count);
462 font[count] = 0;
463 f = (char *)font;
464 }
465 }
466
467 if (newfs)
468 end = strlen(newfs);
469 else
470 end = 0;
471
472 tmp = wmalloc(end + strlen(f) + 8);
473 if (end != 0) {
474 sprintf(tmp, "%s,", newfs);
475 sprintf(tmp + end + 1, f, size);
476 } else {
477 sprintf(tmp + end, f, size);
478 }
479
480 if (newfs)
481 wfree(newfs);
482 newfs = tmp;
483
484 fontset = ptr + 1;
485 } while (ptr != NULL);
486
487 return newfs;
488 }
489
490 #define FONT_PROPS 14
491
492 typedef struct {
493 char *props[FONT_PROPS];
494 } W_FontAttributes;
495
496 static void changeFontProp(char *buf, char *newprop, int position)
497 {
498 char buf2[512];
499 char *ptr, *pptr, *rptr;
500 int count;
501
502 if (buf[0] != '-') {
503 /* // remove warning later. or maybe not */
504 wwarning(_("Invalid font specification: '%s'\n"), buf);
505 return;
506 }
507
508 ptr = pptr = rptr = buf;
509 count = 0;
510 while (*ptr && *ptr != ',') {
511 if (*ptr == '-') {
512 count++;
513 if (count - 1 == position + 1) {
514 rptr = ptr;
515 break;
516 }
517 if (count - 1 == position) {
518 pptr = ptr + 1;
519 }
520 }
521 ptr++;
522 }
523 if (position == FONT_PROPS - 1) {
524 rptr = ptr;
525 }
526
527 *pptr = 0;
528 snprintf(buf2, 512, "%s%s%s", buf, newprop, rptr);
529 strcpy(buf, buf2);
530 }
531
532 static WMArray *getOptions(char *options)
533 {
534 char *ptr, *ptr2, *str;
535 WMArray *result;
536 int count;
537
538 result = WMCreateArrayWithDestructor(2, (WMFreeDataProc *) wfree);
539
540 ptr = options;
541 while (1) {
542 ptr2 = strchr(ptr, ',');
543 if (!ptr2) {
544 WMAddToArray(result, wstrdup(ptr));
545 break;
546 } else {
547 count = ptr2 - ptr;
548 str = wmalloc(count + 1);
549 memcpy(str, ptr, count);
550 str[count] = 0;
551 WMAddToArray(result, str);
552 ptr = ptr2 + 1;
553 }
554 }
555
556 return result;
557 }
558
559 #define WFAUnchanged (NULL)
560 /* Struct for font change operations */
561 typedef struct WMFontAttributes {
562 char *foundry;
563 char *family;
564 char *weight;
565 char *slant;
566 char *setWidth;
567 char *addStyle;
568 char *pixelSize;
569 char *pointSize;
570 char *resolutionX;
571 char *resolutionY;
572 char *spacing;
573 char *averageWidth;
574 char *registry;
575 char *encoding;
576 } WMFontAttributes;
577
578 WMFont *WMCopyFontWithChanges(WMScreen * scrPtr, WMFont * font, const WMFontAttributes * changes)
579 {
580 int index[FONT_PROPS], count[FONT_PROPS];
581 int totalProps, i, j, carry;
582 char fname[512];
583 WMFontFlags fFlags;
584 WMBag *props;
585 WMArray *options;
586 WMFont *result;
587 char *prop;
588
589 snprintf(fname, 512, "%s", font->name);
590
591 fFlags = (font->antialiased ? WFAntialiased : WFNotAntialiased);
592 fFlags |= (font->notFontSet ? WFNormalFont : WFFontSet);
593
594 props = WMCreateBagWithDestructor(1, (WMFreeDataProc *) WMFreeArray);
595
596 totalProps = 0;
597 for (i = 0; i < FONT_PROPS; i++) {
598 prop = ((W_FontAttributes *) changes)->props[i];
599 count[i] = index[i] = 0;
600 if (!prop) {
601 /* No change for this property */
602 continue;
603 } else if (strchr(prop, ',') == NULL) {
604 /* Simple option */
605 changeFontProp(fname, prop, i);
606 } else {
607 /* Option with fallback alternatives */
608 if ((changes == WFAEmphasized || changes == WFABoldEmphasized) &&
609 font->antialiased && strcmp(prop, "o,i") == 0) {
610 options = getOptions("i,o");
611 } else {
612 options = getOptions(prop);
613 }
614 WMInsertInBag(props, i, options);
615 count[i] = WMGetArrayItemCount(options);
616 if (totalProps == 0)
617 totalProps = 1;
618 totalProps = totalProps * count[i];
619 }
620 }
621
622 if (totalProps == 0) {
623 /* No options with fallback alternatives at all */
624 WMFreeBag(props);
625 return WMCreateFontWithFlags(scrPtr, fname, fFlags);
626 }
627
628 for (i = 0; i < totalProps; i++) {
629 for (j = 0; j < FONT_PROPS; j++) {
630 if (count[j] != 0) {
631 options = WMGetFromBag(props, j);
632 prop = WMGetFromArray(options, index[j]);
633 if (prop) {
634 changeFontProp(fname, prop, j);
635 }
636 }
637 }
638 result = WMCreateFontWithFlags(scrPtr, fname, fFlags);
639 if (result) {
640 WMFreeBag(props);
641 return result;
642 }
643 for (j = FONT_PROPS - 1, carry = 1; j >= 0; j--) {
644 if (count[j] != 0) {
645 index[j] += carry;
646 carry = (index[j] == count[j]);
647 index[j] %= count[j];
648 }
649 }
650 }
651
652 WMFreeBag(props);
653
654 return NULL;
655 }
656
657 // should WFANormal also set "normal" or leave it alone?
658 static const WMFontAttributes W_FANormal = {
659 WFAUnchanged, WFAUnchanged, "medium,normal,regular", "r", "normal",
660 WFAUnchanged, WFAUnchanged, WFAUnchanged, WFAUnchanged, WFAUnchanged,
661 WFAUnchanged, WFAUnchanged, WFAUnchanged, WFAUnchanged
662 };
663
664 static const WMFontAttributes W_FABold = {
665 WFAUnchanged, WFAUnchanged, "bold", WFAUnchanged,
666 WFAUnchanged, WFAUnchanged, WFAUnchanged, WFAUnchanged, WFAUnchanged,
667 WFAUnchanged, WFAUnchanged, WFAUnchanged, WFAUnchanged, WFAUnchanged
668 };
669
670 static const WMFontAttributes W_FANotBold = {
671 WFAUnchanged, WFAUnchanged, "medium,normal,regular", WFAUnchanged,
672 WFAUnchanged, WFAUnchanged, WFAUnchanged, WFAUnchanged, WFAUnchanged,
673 WFAUnchanged, WFAUnchanged, WFAUnchanged, WFAUnchanged, WFAUnchanged
674 };
675
676 static const WMFontAttributes W_FAEmphasized = {
677 WFAUnchanged, WFAUnchanged, WFAUnchanged, "o,i",
678 WFAUnchanged, WFAUnchanged, WFAUnchanged, WFAUnchanged, WFAUnchanged,
679 WFAUnchanged, WFAUnchanged, WFAUnchanged, WFAUnchanged, WFAUnchanged
680 };
681
682 static const WMFontAttributes W_FANotEmphasized = {
683 WFAUnchanged, WFAUnchanged, WFAUnchanged, "r",
684 WFAUnchanged, WFAUnchanged, WFAUnchanged, WFAUnchanged, WFAUnchanged,
685 WFAUnchanged, WFAUnchanged, WFAUnchanged, WFAUnchanged, WFAUnchanged
686 };
687
688 static const WMFontAttributes W_FABoldEmphasized = {
689 WFAUnchanged, WFAUnchanged, "bold", "o,i",
690 WFAUnchanged, WFAUnchanged, WFAUnchanged, WFAUnchanged, WFAUnchanged,
691 WFAUnchanged, WFAUnchanged, WFAUnchanged, WFAUnchanged, WFAUnchanged
692 };
693
694 const WMFontAttributes *WFANormal = &W_FANormal;
695 const WMFontAttributes *WFABold = &W_FABold;
696 const WMFontAttributes *WFANotBold = &W_FANotBold;
697 const WMFontAttributes *WFAEmphasized = &W_FAEmphasized;
698 const WMFontAttributes *WFANotEmphasized = &W_FANotEmphasized;
699 const WMFontAttributes *WFABoldEmphasized = &W_FABoldEmphasized;
700
701 #endif