5 # include <X11/Xft/Xft.h>
13 #include <X11/Xlocale.h>
16 static char *makeFontSetOfSize(char *fontset
, int size
);
20 /* XLFD pattern matching */
22 getElementFromXLFD(const char *xlfd
, int index
)
26 if (*p
== '-' && --index
== 0) {
27 const char *end
= strchr(p
+ 1, '-');
30 if (end
== 0) end
= p
+ strlen(p
);
33 memcpy(buf
, p
+ 1, len
);
43 /* XLFD pattern matching */
45 generalizeXLFD(const char *xlfd
)
49 char *weight
= getElementFromXLFD(xlfd
, 3);
50 char *slant
= getElementFromXLFD(xlfd
, 4);
51 char *pxlsz
= getElementFromXLFD(xlfd
, 7);
53 #define Xstrlen(A) ((A)?strlen(A):0)
54 len
= Xstrlen(xlfd
)+Xstrlen(weight
)+Xstrlen(slant
)+Xstrlen(pxlsz
)*2+60;
57 buf
= wmalloc(len
+ 1);
58 snprintf(buf
, len
+ 1, "%s,-*-*-%s-%s-*-*-%s-*-*-*-*-*-*-*,"
59 "-*-*-*-*-*-*-%s-*-*-*-*-*-*-*,*",
60 xlfd
, weight
, slant
, pxlsz
, pxlsz
);
69 /* XLFD pattern matching */
71 W_CreateFontSetWithGuess(Display
*dpy
, char *xlfd
, char ***missing
,
72 int *nmissing
, char **def_string
)
74 XFontSet fs
= XCreateFontSet(dpy
, xlfd
, missing
, nmissing
, def_string
);
76 if (fs
!= NULL
&& *nmissing
== 0) return fs
;
78 /* for non-iso8859-1 language and iso8859-1 specification
79 (this fontset is only for pattern analysis) */
81 if (*nmissing
!= 0) XFreeStringList(*missing
);
82 setlocale(LC_CTYPE
, "C");
83 fs
= XCreateFontSet(dpy
, xlfd
, missing
, nmissing
, def_string
);
84 setlocale(LC_CTYPE
, "");
87 /* make XLFD font name for pattern analysis */
89 XFontStruct
**fontstructs
;
91 if (XFontsOfFontSet(fs
, &fontstructs
, &fontnames
) > 0)
95 xlfd
= generalizeXLFD(xlfd
);
97 if (*nmissing
!= 0) XFreeStringList(*missing
);
98 if (fs
!= NULL
) XFreeFontSet(dpy
, fs
);
100 fs
= XCreateFontSet(dpy
, xlfd
, missing
, nmissing
, def_string
);
108 xlfdFromFontName(char *fontName
, Bool antialiased
)
110 char *systemFont
, *boldSystemFont
;
115 systemFont
= WINGsConfiguration
.antialiasedSystemFont
;
116 boldSystemFont
= WINGsConfiguration
.antialiasedBoldSystemFont
;
118 systemFont
= WINGsConfiguration
.systemFont
;
119 boldSystemFont
= WINGsConfiguration
.boldSystemFont
;
122 size
= WINGsConfiguration
.defaultFontSize
;
124 if (strcmp(fontName
, "SystemFont")==0) {
126 size
= WINGsConfiguration
.defaultFontSize
;
127 } else if (strncmp(fontName
, "SystemFont-", 11)==0) {
129 if (sscanf(&fontName
[11], "%i", &size
)!=1) {
130 size
= WINGsConfiguration
.defaultFontSize
;
131 wwarning(_("Invalid size specification '%s' in %s. "
132 "Using default %d\n"), &fontName
[11], fontName
, size
);
134 } else if (strcmp(fontName
, "BoldSystemFont")==0) {
135 font
= boldSystemFont
;
136 size
= WINGsConfiguration
.defaultFontSize
;
137 } else if (strncmp(fontName
, "BoldSystemFont-", 15)==0) {
138 font
= boldSystemFont
;
139 if (sscanf(&fontName
[15], "%i", &size
)!=1) {
140 size
= WINGsConfiguration
.defaultFontSize
;
141 wwarning(_("Invalid size specification '%s' in %s. "
142 "Using default %d\n"), &fontName
[15], fontName
, size
);
148 return (font
!=NULL
? makeFontSetOfSize(font
, size
) : wstrdup(fontName
));
153 WMCreateFontSet(WMScreen
*scrPtr
, char *fontName
)
156 Display
*display
= scrPtr
->display
;
161 XFontSetExtents
*extents
;
163 fname
= xlfdFromFontName(fontName
, False
);
165 font
= WMHashGet(scrPtr
->fontSetCache
, fname
);
172 font
= wmalloc(sizeof(WMFont
));
173 memset(font
, 0, sizeof(WMFont
));
175 font
->notFontSet
= 0;
176 font
->antialiased
= 0;
178 font
->screen
= scrPtr
;
180 font
->font
.set
= W_CreateFontSetWithGuess(display
, fname
, &missing
,
181 &nmissing
, &defaultString
);
182 if (nmissing
> 0 && font
->font
.set
) {
185 wwarning(_("the following character sets are missing in %s:"), fname
);
186 for (i
= 0; i
< nmissing
; i
++) {
187 wwarning(missing
[i
]);
189 XFreeStringList(missing
);
191 wwarning(_("the string \"%s\" will be used in place of any characters from those sets."),
194 if (!font
->font
.set
) {
200 extents
= XExtentsOfFontSet(font
->font
.set
);
202 font
->height
= extents
->max_logical_extent
.height
;
203 font
->y
= font
->height
- (font
->height
+ extents
->max_logical_extent
.y
);
209 assert(WMHashInsert(scrPtr
->fontSetCache
, font
->name
, font
)==NULL
);
217 WMCreateNormalFont(WMScreen
*scrPtr
, char *fontName
)
220 Display
*display
= scrPtr
->display
;
223 fontName
= xlfdFromFontName(fontName
, False
);
225 if ((ptr
= strchr(fontName
, ','))) {
226 fname
= wmalloc(ptr
- fontName
+ 1);
227 strncpy(fname
, fontName
, ptr
- fontName
);
228 fname
[ptr
- fontName
] = 0;
230 fname
= wstrdup(fontName
);
235 font
= WMHashGet(scrPtr
->fontCache
, fname
);
242 font
= wmalloc(sizeof(WMFont
));
243 memset(font
, 0, sizeof(WMFont
));
245 font
->notFontSet
= 1;
246 font
->antialiased
= 0;
248 font
->screen
= scrPtr
;
250 font
->font
.normal
= XLoadQueryFont(display
, fname
);
251 if (!font
->font
.normal
) {
256 font
->height
= font
->font
.normal
->ascent
+font
->font
.normal
->descent
;
257 font
->y
= font
->font
.normal
->ascent
;
263 assert(WMHashInsert(scrPtr
->fontCache
, font
->name
, font
)==NULL
);
270 WMCreateAntialiasedFont(WMScreen
*scrPtr
, char *fontName
)
274 Display
*display
= scrPtr
->display
;
277 if (!scrPtr
->hasXftSupport
)
280 fontName
= xlfdFromFontName(fontName
, True
);
282 if ((ptr
= strchr(fontName
, ','))) {
283 fname
= wmalloc(ptr
- fontName
+ 1);
284 strncpy(fname
, fontName
, ptr
- fontName
);
285 fname
[ptr
- fontName
] = 0;
287 fname
= wstrdup(fontName
);
292 font
= WMHashGet(scrPtr
->xftFontCache
, fname
);
299 font
= wmalloc(sizeof(WMFont
));
300 memset(font
, 0, sizeof(WMFont
));
302 font
->notFontSet
= 1;
303 font
->antialiased
= 1;
305 font
->screen
= scrPtr
;
308 /* // Xft sux. Loading a font that doesn't exist will load the default
309 * defined in XftConfig without any warning or error */
310 font
->font
.normal
= XLoadQueryFont(display
, fname
);
311 if (!font
->font
.normal
) {
316 XFreeFont(display
, font
->font
.normal
);
319 font
->font
.xft
= XftFontOpenXlfd(display
, scrPtr
->screen
, fname
);
320 if (!font
->font
.xft
) {
325 font
->height
= font
->font
.xft
->ascent
+font
->font
.xft
->descent
;
326 font
->y
= font
->font
.xft
->ascent
;
332 assert(WMHashInsert(scrPtr
->xftFontCache
, font
->name
, font
)==NULL
);
342 WMCreateFont(WMScreen
*scrPtr
, char *fontName
)
344 return WMCreateFontWithFlags(scrPtr
, fontName
, WFDefaultFont
);
349 WMCreateFontWithFlags(WMScreen
*scrPtr
, char *fontName
, WMFontFlags flags
)
351 Bool multiByte
= scrPtr
->useMultiByte
;
352 Bool antialiased
= scrPtr
->antialiasedText
;
355 if (flags
& WFFontSet
) {
357 } else if (flags
& WFNormalFont
) {
360 if (flags
& WFAntialiased
) {
362 } else if (flags
& WFNotAntialiased
) {
366 /* Multibyte with antialiasing is not implemented. To avoid problems,
367 * if both multiByte and antialiasing are enabled, ignore antialiasing
368 * and return a FontSet.
371 font
= WMCreateFontSet(scrPtr
, fontName
);
372 } else if (antialiased
) {
373 font
= WMCreateAntialiasedFont(scrPtr
, fontName
);
374 /* If we cannot create an antialiased font and antialiasing is
375 * not explicitly requested in flags, fallback to normal font */
376 if (!font
&& (flags
& WFAntialiased
)==0) {
377 font
= WMCreateNormalFont(scrPtr
, fontName
);
380 font
= WMCreateNormalFont(scrPtr
, fontName
);
388 WMRetainFont(WMFont
*font
)
390 wassertrv(font
!=NULL
, NULL
);
399 WMReleaseFont(WMFont
*font
)
401 wassertr(font
!=NULL
);
404 if (font
->refCount
< 1) {
405 if (font
->notFontSet
) {
406 if (font
->antialiased
) {
408 XftFontClose(font
->screen
->display
, font
->font
.xft
);
413 XFreeFont(font
->screen
->display
, font
->font
.normal
);
416 XFreeFontSet(font
->screen
->display
, font
->font
.set
);
419 if (font
->antialiased
) {
420 WMHashRemove(font
->screen
->xftFontCache
, font
->name
);
421 } else if (font
->notFontSet
) {
422 WMHashRemove(font
->screen
->fontCache
, font
->name
);
424 WMHashRemove(font
->screen
->fontSetCache
, font
->name
);
434 WMHasAntialiasingSupport(WMScreen
*scrPtr
)
436 return scrPtr
->hasXftSupport
;
441 WMIsAntialiasingEnabled(WMScreen
*scrPtr
)
443 return scrPtr
->antialiasedText
;
448 WMIsAntialiasedFont(WMFont
*font
)
450 return font
->antialiased
;
455 WMFontHeight(WMFont
*font
)
457 wassertrv(font
!=NULL
, 0);
464 WMDefaultSystemFont(WMScreen
*scrPtr
)
466 return WMRetainFont(scrPtr
->normalFont
);
471 WMDefaultBoldSystemFont(WMScreen
*scrPtr
)
473 return WMRetainFont(scrPtr
->boldFont
);
478 makeSystemFontOfSize(WMScreen
*scrPtr
, int size
, Bool bold
)
481 char *fontSpec
, *xftFontSpec
;
483 #define WConf WINGsConfiguration
485 fontSpec
= makeFontSetOfSize(WConf
.boldSystemFont
, size
);
486 xftFontSpec
= makeFontSetOfSize(WConf
.antialiasedBoldSystemFont
, size
);
488 fontSpec
= makeFontSetOfSize(WConf
.systemFont
, size
);
489 xftFontSpec
= makeFontSetOfSize(WConf
.antialiasedSystemFont
, size
);
493 if (scrPtr
->useMultiByte
) {
494 font
= WMCreateFontSet(scrPtr
, fontSpec
);
495 } else if (scrPtr
->antialiasedText
) {
496 font
= WMCreateAntialiasedFont(scrPtr
, xftFontSpec
);
498 font
= WMCreateNormalFont(scrPtr
, fontSpec
);
502 if (scrPtr
->useMultiByte
) {
503 wwarning(_("could not load font set %s. Trying fixed."), fontSpec
);
504 font
= WMCreateFontSet(scrPtr
, "fixed");
506 font
= WMCreateFontSet(scrPtr
, "-*-fixed-medium-r-normal-*-14-*-*-*-*-*-*-*");
508 } else if (scrPtr
->antialiasedText
) {
509 wwarning(_("could not load font %s. Trying arial."), xftFontSpec
);
511 font
= WMCreateAntialiasedFont(scrPtr
, "-*-arial-bold-r-normal-*-12-*-*-*-*-*-*-*");
513 font
= WMCreateAntialiasedFont(scrPtr
, "-*-arial-medium-r-normal-*-12-*-*-*-*-*-*-*");
516 wwarning(_("could not load antialiased fonts. Reverting to normal fonts."));
517 font
= WMCreateNormalFont(scrPtr
, fontSpec
);
519 wwarning(_("could not load font %s. Trying fixed."), fontSpec
);
520 font
= WMCreateNormalFont(scrPtr
, "fixed");
524 wwarning(_("could not load font %s. Trying fixed."), fontSpec
);
525 font
= WMCreateNormalFont(scrPtr
, "fixed");
528 wwarning(_("could not load fixed font!"));
542 WMSystemFontOfSize(WMScreen
*scrPtr
, int size
)
544 return makeSystemFontOfSize(scrPtr
, size
, False
);
549 WMBoldSystemFontOfSize(WMScreen
*scrPtr
, int size
)
551 return makeSystemFontOfSize(scrPtr
, size
, True
);
556 WMGetFontFontSet(WMFont
*font
)
558 wassertrv(font
!=NULL
, NULL
);
560 if (font
->notFontSet
)
563 return font
->font
.set
;
568 WMWidthOfString(WMFont
*font
, char *text
, int length
)
570 wassertrv(font
!=NULL
, 0);
571 wassertrv(text
!=NULL
, 0);
573 if (font
->notFontSet
) {
574 if (font
->antialiased
) {
578 XftTextExtents8(font
->screen
->display
, font
->font
.xft
,
579 (XftChar8
*)text
, length
, &extents
);
580 return extents
.xOff
; /* don't ask :P */
585 return XTextWidth(font
->font
.normal
, text
, length
);
591 XmbTextExtents(font
->font
.set
, text
, length
, &AIXsucks
, &rect
);
600 WMDrawString(WMScreen
*scr
, Drawable d
, WMColor
*color
, WMFont
*font
,
601 int x
, int y
, char *text
, int length
)
603 wassertr(font
!=NULL
);
605 if (font
->notFontSet
) {
606 if (font
->antialiased
) {
610 xftcolor
.color
.red
= color
->color
.red
;
611 xftcolor
.color
.green
= color
->color
.green
;
612 xftcolor
.color
.blue
= color
->color
.blue
;
613 xftcolor
.color
.alpha
= color
->alpha
;;
614 xftcolor
.pixel
= W_PIXEL(color
);
616 XftDrawChange(scr
->xftdraw
, d
);
618 XftDrawString8(scr
->xftdraw
, &xftcolor
, font
->font
.xft
,
619 x
, y
+ font
->y
, text
, length
);
624 XSetFont(scr
->display
, scr
->drawStringGC
, font
->font
.normal
->fid
);
625 XSetForeground(scr
->display
, scr
->drawStringGC
, W_PIXEL(color
));
626 XDrawString(scr
->display
, d
, scr
->drawStringGC
, x
, y
+ font
->y
,
630 XSetForeground(scr
->display
, scr
->drawStringGC
, W_PIXEL(color
));
631 XmbDrawString(scr
->display
, d
, font
->font
.set
, scr
->drawStringGC
,
632 x
, y
+ font
->y
, text
, length
);
638 WMDrawImageString(WMScreen
*scr
, Drawable d
, WMColor
*color
, WMColor
*background
,
639 WMFont
*font
, int x
, int y
, char *text
, int length
)
641 wassertr(font
!= NULL
);
643 if (font
->notFontSet
) {
644 if (font
->antialiased
) {
649 textColor
.color
.red
= color
->color
.red
;
650 textColor
.color
.green
= color
->color
.green
;
651 textColor
.color
.blue
= color
->color
.blue
;
652 textColor
.color
.alpha
= color
->alpha
;;
653 textColor
.pixel
= W_PIXEL(color
);
655 bgColor
.color
.red
= background
->color
.red
;
656 bgColor
.color
.green
= background
->color
.green
;
657 bgColor
.color
.blue
= background
->color
.blue
;
658 bgColor
.color
.alpha
= background
->alpha
;;
659 bgColor
.pixel
= W_PIXEL(background
);
662 XftDrawChange(scr
->xftdraw
, d
);
664 XftDrawRect(scr
->xftdraw
, &bgColor
, x
, y
,
665 WMWidthOfString(font
, text
, length
), font
->height
);
667 XftDrawString8(scr
->xftdraw
, &textColor
, font
->font
.xft
,
668 x
, y
+ font
->y
, text
, length
);
673 XSetForeground(scr
->display
, scr
->drawImStringGC
, W_PIXEL(color
));
674 XSetBackground(scr
->display
, scr
->drawImStringGC
, W_PIXEL(background
));
675 XSetFont(scr
->display
, scr
->drawImStringGC
, font
->font
.normal
->fid
);
676 XDrawImageString(scr
->display
, d
, scr
->drawImStringGC
,
677 x
, y
+ font
->y
, text
, length
);
680 XSetForeground(scr
->display
, scr
->drawImStringGC
, W_PIXEL(color
));
681 XSetBackground(scr
->display
, scr
->drawImStringGC
, W_PIXEL(background
));
682 XmbDrawImageString(scr
->display
, d
, font
->font
.set
, scr
->drawImStringGC
,
683 x
, y
+ font
->y
, text
, length
);
691 makeFontSetOfSize(char *fontset
, int size
)
703 ptr
= strchr(fontset
, ',');
705 int count
= ptr
-fontset
;
708 wwarning(_("font description %s is too large."), fontset
);
710 memcpy(font
, fontset
, count
);
721 tmp
= wmalloc(end
+ strlen(f
) + 8);
723 sprintf(tmp
, "%s,", newfs
);
724 sprintf(tmp
+ end
+ 1, f
, size
);
726 sprintf(tmp
+ end
, f
, size
);
741 changeFontProp(char *fname
, char *newprop
, int which
)
743 char before
[128], prop
[128], after
[128];
757 else if (part
==which
+1)
767 snprintf(fname
, 255, "%s-%s%s", before
, newprop
, after
);
772 WMNormalizeFont(WMScreen
*scr
, WMFont
*font
)
780 snprintf(fname
, 255, "%s", font
->name
);
781 changeFontProp(fname
, "medium", 2);
782 changeFontProp(fname
, "r", 3);
783 flag
= (font
->antialiased
? WFAntialiased
: WFNotAntialiased
);
784 return WMCreateFontWithFlags(scr
, fname
, flag
);
789 WMStrengthenFont(WMScreen
*scr
, WMFont
*font
)
797 snprintf(fname
, 255, "%s", font
->name
);
798 changeFontProp(fname
, "bold", 2);
799 flag
= (font
->antialiased
? WFAntialiased
: WFNotAntialiased
);
800 return WMCreateFontWithFlags(scr
, fname
, flag
);
805 WMUnstrengthenFont(WMScreen
*scr
, WMFont
*font
)
813 snprintf(fname
, 255, "%s", font
->name
);
814 changeFontProp(fname
, "medium", 2);
815 flag
= (font
->antialiased
? WFAntialiased
: WFNotAntialiased
);
816 return WMCreateFontWithFlags(scr
, fname
, flag
);
821 WMEmphasizeFont(WMScreen
*scr
, WMFont
*font
)
829 snprintf(fname
, 255, "%s", font
->name
);
830 if (font
->antialiased
)
831 changeFontProp(fname
, "i", 3);
833 changeFontProp(fname
, "o", 3);
835 flag
= (font
->antialiased
? WFAntialiased
: WFNotAntialiased
);
836 return WMCreateFontWithFlags(scr
, fname
, flag
);
841 WMUnemphasizeFont(WMScreen
*scr
, WMFont
*font
)
849 snprintf(fname
, 255, "%s", font
->name
);
850 changeFontProp(fname
, "r", 3);
851 flag
= (font
->antialiased
? WFAntialiased
: WFNotAntialiased
);
852 return WMCreateFontWithFlags(scr
, fname
, flag
);