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 WMCreateFontSet(WMScreen
*scrPtr
, char *fontName
)
111 Display
*display
= scrPtr
->display
;
115 XFontSetExtents
*extents
;
117 font
= WMHashGet(scrPtr
->fontSetCache
, fontName
);
123 font
= malloc(sizeof(WMFont
));
126 memset(font
, 0, sizeof(WMFont
));
128 font
->notFontSet
= 0;
129 font
->antialiased
= 0;
131 font
->screen
= scrPtr
;
133 font
->font
.set
= W_CreateFontSetWithGuess(display
, fontName
, &missing
,
134 &nmissing
, &defaultString
);
135 if (nmissing
> 0 && font
->font
.set
) {
138 wwarning(_("the following character sets are missing in %s:"),
140 for (i
= 0; i
< nmissing
; i
++) {
141 wwarning(missing
[i
]);
143 XFreeStringList(missing
);
145 wwarning(_("the string \"%s\" will be used in place of any characters from those sets."),
148 if (!font
->font
.set
) {
153 extents
= XExtentsOfFontSet(font
->font
.set
);
155 font
->height
= extents
->max_logical_extent
.height
;
156 font
->y
= font
->height
- (font
->height
+ extents
->max_logical_extent
.y
);
160 font
->name
= wstrdup(fontName
);
162 assert(WMHashInsert(scrPtr
->fontSetCache
, font
->name
, font
)==NULL
);
170 WMCreateNormalFont(WMScreen
*scrPtr
, char *fontName
)
173 Display
*display
= scrPtr
->display
;
176 if ((ptr
= strchr(fontName
, ','))) {
177 fname
= wmalloc(ptr
- fontName
+ 1);
178 strncpy(fname
, fontName
, ptr
- fontName
);
179 fname
[ptr
- fontName
] = 0;
181 fname
= wstrdup(fontName
);
184 font
= WMHashGet(scrPtr
->fontCache
, fname
);
191 font
= malloc(sizeof(WMFont
));
196 memset(font
, 0, sizeof(WMFont
));
198 font
->notFontSet
= 1;
199 font
->antialiased
= 0;
201 font
->screen
= scrPtr
;
203 font
->font
.normal
= XLoadQueryFont(display
, fname
);
204 if (!font
->font
.normal
) {
209 font
->height
= font
->font
.normal
->ascent
+font
->font
.normal
->descent
;
210 font
->y
= font
->font
.normal
->ascent
;
216 assert(WMHashInsert(scrPtr
->fontCache
, font
->name
, font
)==NULL
);
223 WMCreateAAFont(WMScreen
*scrPtr
, char *fontName
)
227 Display
*display
= scrPtr
->display
;
230 if ((ptr
= strchr(fontName
, ','))) {
231 fname
= wmalloc(ptr
- fontName
+ 1);
232 strncpy(fname
, fontName
, ptr
- fontName
);
233 fname
[ptr
- fontName
] = 0;
235 fname
= wstrdup(fontName
);
238 font
= WMHashGet(scrPtr
->fontCache
, fname
);
245 font
= malloc(sizeof(WMFont
));
250 memset(font
, 0, sizeof(WMFont
));
252 font
->notFontSet
= 1;
253 font
->antialiased
= 1;
255 font
->screen
= scrPtr
;
258 font
->font
.normal
= XLoadQueryFont(display
, fname
);
259 if (!font
->font
.normal
) {
264 XFreeFont(display
, font
->font
.normal
);
266 font
->font
.xft
= XftFontOpenXlfd(display
, scrPtr
->screen
, fname
);
267 if (!font
->font
.xft
) {
272 font
->height
= font
->font
.xft
->ascent
+font
->font
.xft
->descent
;
273 font
->y
= font
->font
.xft
->ascent
;
279 assert(WMHashInsert(scrPtr
->fontCache
, font
->name
, font
)==NULL
);
289 WMCreateFont(WMScreen
*scrPtr
, char *fontName
)
291 if (scrPtr
->useMultiByte
) {
292 return WMCreateFontSet(scrPtr
, fontName
);
293 } else if (scrPtr
->antialiasedText
) {
294 /*// should revert to normal if this fails? */
295 return WMCreateAAFont(scrPtr
, fontName
);
297 return WMCreateNormalFont(scrPtr
, fontName
);
303 WMRetainFont(WMFont
*font
)
305 wassertrv(font
!=NULL
, NULL
);
314 WMReleaseFont(WMFont
*font
)
316 wassertr(font
!=NULL
);
319 if (font
->refCount
< 1) {
320 if (font
->notFontSet
) {
321 if (font
->antialiased
) {
323 XftFontClose(font
->screen
->display
, font
->font
.xft
);
328 XFreeFont(font
->screen
->display
, font
->font
.normal
);
331 XFreeFontSet(font
->screen
->display
, font
->font
.set
);
334 if (font
->notFontSet
) {
335 WMHashRemove(font
->screen
->fontCache
, font
->name
);
337 WMHashRemove(font
->screen
->fontSetCache
, font
->name
);
348 WMFontHeight(WMFont
*font
)
350 wassertrv(font
!=NULL
, 0);
357 WMDefaultSystemFont(WMScreen
*scrPtr
)
359 return WMRetainFont(scrPtr
->normalFont
);
364 WMDefaultBoldSystemFont(WMScreen
*scrPtr
)
366 return WMRetainFont(scrPtr
->boldFont
);
371 makeSystemFontOfSize(WMScreen
*scrPtr
, int size
, Bool bold
)
374 char *fontSpec
, *aaFontSpec
;
377 fontSpec
= makeFontSetOfSize(WINGsConfiguration
.boldSystemFont
, size
);
378 aaFontSpec
= makeFontSetOfSize(WINGsConfiguration
.aaBoldSystemFont
, size
);
380 fontSpec
= makeFontSetOfSize(WINGsConfiguration
.systemFont
, size
);
381 aaFontSpec
= makeFontSetOfSize(WINGsConfiguration
.aaSystemFont
, size
);
384 if (scrPtr
->useMultiByte
) {
385 font
= WMCreateFontSet(scrPtr
, fontSpec
);
386 } else if (scrPtr
->antialiasedText
) {
387 font
= WMCreateAAFont(scrPtr
, aaFontSpec
);
389 font
= WMCreateNormalFont(scrPtr
, fontSpec
);
393 if (scrPtr
->useMultiByte
) {
394 wwarning(_("could not load font set %s. Trying fixed."), fontSpec
);
395 font
= WMCreateFontSet(scrPtr
, "fixed");
397 font
= WMCreateFontSet(scrPtr
, "-*-fixed-medium-r-normal-*-14-*-*-*-*-*-*-*");
399 } else if (scrPtr
->antialiasedText
) {
400 wwarning(_("could not load font %s. Trying arial."), aaFontSpec
);
402 font
= WMCreateAAFont(scrPtr
, "-*-arial-bold-r-normal-*-12-*-*-*-*-*-*-*");
404 font
= WMCreateAAFont(scrPtr
, "-*-arial-medium-r-normal-*-12-*-*-*-*-*-*-*");
407 wwarning(_("could not load antialiased fonts. Reverting to normal fonts."));
408 font
= WMCreateNormalFont(scrPtr
, fontSpec
);
410 wwarning(_("could not load font %s. Trying fixed."), fontSpec
);
411 font
= WMCreateNormalFont(scrPtr
, "fixed");
415 wwarning(_("could not load font %s. Trying fixed."), fontSpec
);
416 font
= WMCreateNormalFont(scrPtr
, "fixed");
419 wwarning(_("could not load fixed font!"));
433 WMSystemFontOfSize(WMScreen
*scrPtr
, int size
)
435 return makeSystemFontOfSize(scrPtr
, size
, False
);
440 WMBoldSystemFontOfSize(WMScreen
*scrPtr
, int size
)
442 return makeSystemFontOfSize(scrPtr
, size
, True
);
447 WMGetFontFontSet(WMFont
*font
)
449 wassertrv(font
!=NULL
, NULL
);
451 if (font
->notFontSet
)
454 return font
->font
.set
;
459 WMWidthOfString(WMFont
*font
, char *text
, int length
)
461 wassertrv(font
!=NULL
, 0);
462 wassertrv(text
!=NULL
, 0);
464 if (font
->notFontSet
) {
465 if (font
->antialiased
) {
469 XftTextExtents8(font
->screen
->display
, font
->font
.xft
,
470 (XftChar8
*)text
, length
, &extents
);
471 return extents
.xOff
; /* don't ask :P */
476 return XTextWidth(font
->font
.normal
, text
, length
);
482 XmbTextExtents(font
->font
.set
, text
, length
, &AIXsucks
, &rect
);
491 WMDrawString(WMScreen
*scr
, Drawable d
, WMColor
*color
, WMFont
*font
,
492 int x
, int y
, char *text
, int length
)
494 wassertr(font
!=NULL
);
496 if (font
->notFontSet
) {
497 if (font
->antialiased
) {
502 xftcolor
.color
.red
= color
->color
.red
;
503 xftcolor
.color
.green
= color
->color
.green
;
504 xftcolor
.color
.blue
= color
->color
.blue
;
505 xftcolor
.color
.alpha
= color
->alpha
;;
506 xftcolor
.pixel
= W_PIXEL(color
);
508 /* //share if possible */
509 xftdraw
= XftDrawCreate(scr
->display
, d
, scr
->visual
, scr
->colormap
);
511 XftDrawString8(xftdraw
, &xftcolor
, font
->font
.xft
,
512 x
, y
+ font
->y
, text
, length
);
514 XftDrawDestroy(xftdraw
);
519 XSetFont(scr
->display
, scr
->drawStringGC
, font
->font
.normal
->fid
);
520 XSetForeground(scr
->display
, scr
->drawStringGC
, W_PIXEL(color
));
521 XDrawString(scr
->display
, d
, scr
->drawStringGC
, x
, y
+ font
->y
,
525 XSetForeground(scr
->display
, scr
->drawStringGC
, W_PIXEL(color
));
526 XmbDrawString(scr
->display
, d
, font
->font
.set
, scr
->drawStringGC
,
527 x
, y
+ font
->y
, text
, length
);
533 WMDrawImageString(WMScreen
*scr
, Drawable d
, WMColor
*color
, WMColor
*background
,
534 WMFont
*font
, int x
, int y
, char *text
, int length
)
536 wassertr(font
!= NULL
);
538 if (font
->notFontSet
) {
539 if (font
->antialiased
) {
545 textColor
.color
.red
= color
->color
.red
;
546 textColor
.color
.green
= color
->color
.green
;
547 textColor
.color
.blue
= color
->color
.blue
;
548 textColor
.color
.alpha
= color
->alpha
;;
549 textColor
.pixel
= W_PIXEL(color
);
551 bgColor
.color
.red
= background
->color
.red
;
552 bgColor
.color
.green
= background
->color
.green
;
553 bgColor
.color
.blue
= background
->color
.blue
;
554 bgColor
.color
.alpha
= background
->alpha
;;
555 bgColor
.pixel
= W_PIXEL(background
);
557 /* //share if possible */
558 xftdraw
= XftDrawCreate(scr
->display
, d
, scr
->visual
, scr
->colormap
);
560 XftDrawRect(xftdraw
, &bgColor
, x
, y
,
561 WMWidthOfString(font
, text
, length
), font
->height
);
563 XftDrawString8(xftdraw
, &textColor
, font
->font
.xft
, x
, y
+ font
->y
,
566 XftDrawDestroy(xftdraw
);
571 XSetForeground(scr
->display
, scr
->drawImStringGC
, W_PIXEL(color
));
572 XSetBackground(scr
->display
, scr
->drawImStringGC
, W_PIXEL(background
));
573 XSetFont(scr
->display
, scr
->drawImStringGC
, font
->font
.normal
->fid
);
574 XDrawImageString(scr
->display
, d
, scr
->drawImStringGC
,
575 x
, y
+ font
->y
, text
, length
);
578 XSetForeground(scr
->display
, scr
->drawImStringGC
, W_PIXEL(color
));
579 XSetBackground(scr
->display
, scr
->drawImStringGC
, W_PIXEL(background
));
580 XmbDrawImageString(scr
->display
, d
, font
->font
.set
, scr
->drawImStringGC
,
581 x
, y
+ font
->y
, text
, length
);
589 makeFontSetOfSize(char *fontset
, int size
)
601 ptr
= strchr(fontset
, ',');
603 int count
= ptr
-fontset
;
606 wwarning(_("font description %s is too large."), fontset
);
608 memcpy(font
, fontset
, count
);
619 tmp
= wmalloc(end
+ strlen(f
) + 8);
621 sprintf(tmp
, "%s,", newfs
);
622 sprintf(tmp
+ end
+ 1, f
, size
);
624 sprintf(tmp
+ end
, f
, size
);
639 changeFontProp(char *fname
, char *newprop
, int which
)
641 char before
[128], prop
[128], after
[128];
655 else if (part
==which
+1)
665 snprintf(fname
, 255, "%s-%s%s", before
, newprop
, after
);
670 WMNormalizeFont(WMScreen
*scr
, WMFont
*font
)
672 WMFont
*newfont
=NULL
;
678 snprintf(fname
, 255, "%s", font
->name
);
679 changeFontProp(fname
, "medium", 2);
680 changeFontProp(fname
, "r", 3);
681 newfont
= WMCreateNormalFont(scr
, fname
);
691 WMStrengthenFont(WMScreen
*scr
, WMFont
*font
)
693 WMFont
*newfont
=NULL
;
699 snprintf(fname
, 255, "%s", font
->name
);
700 changeFontProp(fname
, "bold", 2);
701 newfont
= WMCreateNormalFont(scr
, fname
);
711 WMUnstrengthenFont(WMScreen
*scr
, WMFont
*font
)
713 WMFont
*newfont
=NULL
;
719 snprintf(fname
, 255, "%s", font
->name
);
720 changeFontProp(fname
, "medium", 2);
721 newfont
= WMCreateNormalFont(scr
, fname
);
731 WMEmphasizeFont(WMScreen
*scr
, WMFont
*font
)
733 WMFont
*newfont
=NULL
;
739 snprintf(fname
, 255, "%s", font
->name
);
740 changeFontProp(fname
, "o", 3);
741 newfont
= WMCreateNormalFont(scr
, fname
);
751 WMUnemphasizeFont(WMScreen
*scr
, WMFont
*font
)
753 WMFont
*newfont
=NULL
;
759 snprintf(fname
, 255, "%s", font
->name
);
760 changeFontProp(fname
, "r", 3);
761 newfont
= WMCreateNormalFont(scr
, fname
);
771 WMGetFontOfSize(WMScreen
*scr
, WMFont
*font
, int size
)
773 if(!scr
|| !font
|| size
<1)