8 #include <X11/Xlocale.h>
10 static char *makeFontSetOfSize(char *fontset
, int size
);
14 /* XLFD pattern matching */
16 xlfd_get_element (const char *xlfd
, int index
)
20 if (*p
== '-' && --index
== 0) {
21 const char *end
= strchr(p
+ 1, '-');
24 if (end
== 0) end
= p
+ strlen(p
);
27 memcpy(buf
, p
+ 1, len
);
36 /* XLFD pattern matching */
38 generalize_xlfd (const char *xlfd
)
42 char *weight
= xlfd_get_element(xlfd
, 3);
43 char *slant
= xlfd_get_element(xlfd
, 4);
44 char *pxlsz
= xlfd_get_element(xlfd
, 7);
46 #define Xstrlen(A) ((A)?strlen(A):0)
47 len
= Xstrlen(xlfd
)+Xstrlen(weight
)+Xstrlen(slant
)+Xstrlen(pxlsz
)*2+60;
50 buf
= wmalloc(len
+ 1);
51 snprintf(buf
, len
+ 1, "%s,-*-*-%s-%s-*-*-%s-*-*-*-*-*-*-*,"
52 "-*-*-*-*-*-*-%s-*-*-*-*-*-*-*,*",
53 xlfd
, weight
, slant
, pxlsz
, pxlsz
);
62 /* XLFD pattern matching */
64 W_CreateFontSetWithGuess(Display
*dpy
, char *xlfd
, char ***missing
,
65 int *nmissing
, char **def_string
)
67 XFontSet fs
= XCreateFontSet(dpy
, xlfd
, missing
, nmissing
, def_string
);
69 if (fs
!= NULL
&& *nmissing
== 0) return fs
;
71 /* for non-iso8859-1 language and iso8859-1 specification
72 (this fontset is only for pattern analysis) */
74 if (*nmissing
!= 0) XFreeStringList(*missing
);
75 setlocale(LC_CTYPE
, "C");
76 fs
= XCreateFontSet(dpy
, xlfd
, missing
, nmissing
, def_string
);
77 setlocale(LC_CTYPE
, "");
80 /* make XLFD font name for pattern analysis */
82 XFontStruct
**fontstructs
;
84 if (XFontsOfFontSet(fs
, &fontstructs
, &fontnames
) > 0)
88 xlfd
= generalize_xlfd (xlfd
);
90 if (*nmissing
!= 0) XFreeStringList(*missing
);
91 if (fs
!= NULL
) XFreeFontSet(dpy
, fs
);
93 fs
= XCreateFontSet(dpy
, xlfd
, missing
, nmissing
, def_string
);
100 WMCreateFontSet(WMScreen
*scrPtr
, char *fontName
)
103 Display
*display
= scrPtr
->display
;
107 XFontSetExtents
*extents
;
109 font
= WMHashGet(scrPtr
->fontSetCache
, fontName
);
115 font
= malloc(sizeof(WMFont
));
118 memset(font
, 0, sizeof(WMFont
));
120 font
->notFontSet
= 0;
122 font
->screen
= scrPtr
;
124 font
->font
.set
= W_CreateFontSetWithGuess(display
, fontName
, &missing
,
125 &nmissing
, &defaultString
);
126 if (nmissing
> 0 && font
->font
.set
) {
129 wwarning(_("the following character sets are missing in %s:"),
131 for (i
= 0; i
< nmissing
; i
++) {
132 wwarning(missing
[i
]);
134 XFreeStringList(missing
);
136 wwarning(_("the string \"%s\" will be used in place of any characters from those sets."),
139 if (!font
->font
.set
) {
144 extents
= XExtentsOfFontSet(font
->font
.set
);
146 font
->height
= extents
->max_logical_extent
.height
;
147 font
->y
= font
->height
- (font
->height
+ extents
->max_logical_extent
.y
);
151 font
->name
= wstrdup(fontName
);
153 assert(WMHashInsert(scrPtr
->fontSetCache
, font
->name
, font
)==NULL
);
161 WMCreateNormalFont(WMScreen
*scrPtr
, char *fontName
)
164 Display
*display
= scrPtr
->display
;
167 if ((ptr
= strchr(fontName
, ','))) {
168 fname
= wmalloc(ptr
- fontName
+ 1);
169 strncpy(fname
, fontName
, ptr
- fontName
);
170 fname
[ptr
- fontName
] = 0;
172 fname
= wstrdup(fontName
);
175 font
= WMHashGet(scrPtr
->fontCache
, fname
);
182 font
= malloc(sizeof(WMFont
));
187 memset(font
, 0, sizeof(WMFont
));
189 font
->notFontSet
= 1;
191 font
->screen
= scrPtr
;
193 font
->font
.normal
= XLoadQueryFont(display
, fname
);
194 if (!font
->font
.normal
) {
200 font
->height
= font
->font
.normal
->ascent
+font
->font
.normal
->descent
;
201 font
->y
= font
->font
.normal
->ascent
;
207 assert(WMHashInsert(scrPtr
->fontCache
, font
->name
, font
)==NULL
);
215 WMCreateFont(WMScreen
*scrPtr
, char *fontName
)
217 if (scrPtr
->useMultiByte
)
218 return WMCreateFontSet(scrPtr
, fontName
);
220 return WMCreateNormalFont(scrPtr
, fontName
);
226 WMRetainFont(WMFont
*font
)
228 wassertrv(font
!=NULL
, NULL
);
237 WMReleaseFont(WMFont
*font
)
239 wassertr(font
!=NULL
);
242 if (font
->refCount
< 1) {
243 if (font
->notFontSet
)
244 XFreeFont(font
->screen
->display
, font
->font
.normal
);
246 XFreeFontSet(font
->screen
->display
, font
->font
.set
);
249 if (font
->notFontSet
) {
250 WMHashRemove(font
->screen
->fontCache
, font
->name
);
252 WMHashRemove(font
->screen
->fontSetCache
, font
->name
);
263 WMFontHeight(WMFont
*font
)
265 wassertrv(font
!=NULL
, 0);
273 WMSystemFontOfSize(WMScreen
*scrPtr
, int size
)
278 fontSpec
= makeFontSetOfSize(WINGsConfiguration
.systemFont
, size
);
280 if (scrPtr
->useMultiByte
)
281 font
= WMCreateFontSet(scrPtr
, fontSpec
);
283 font
= WMCreateNormalFont(scrPtr
, fontSpec
);
286 if (scrPtr
->useMultiByte
) {
287 wwarning(_("could not load font set %s. Trying fixed."), fontSpec
);
288 font
= WMCreateFontSet(scrPtr
, "fixed");
290 font
= WMCreateFontSet(scrPtr
, "-*-fixed-medium-r-normal-*-14-*-*-*-*-*-*-*");
293 wwarning(_("could not load font %s. Trying fixed."), fontSpec
);
294 font
= WMCreateNormalFont(scrPtr
, "fixed");
297 wwarning(_("could not load fixed font!"));
309 WMBoldSystemFontOfSize(WMScreen
*scrPtr
, int size
)
314 fontSpec
= makeFontSetOfSize(WINGsConfiguration
.boldSystemFont
, size
);
316 if (scrPtr
->useMultiByte
)
317 font
= WMCreateFontSet(scrPtr
, fontSpec
);
319 font
= WMCreateNormalFont(scrPtr
, fontSpec
);
322 if (scrPtr
->useMultiByte
) {
323 wwarning(_("could not load font set %s. Trying fixed."), fontSpec
);
324 font
= WMCreateFontSet(scrPtr
, "fixed");
326 font
= WMCreateFontSet(scrPtr
, "-*-fixed-medium-r-normal-*-14-*-*-*-*-*-*-*");
329 wwarning(_("could not load font %s. Trying fixed."), fontSpec
);
330 font
= WMCreateNormalFont(scrPtr
, "fixed");
333 wwarning(_("could not load fixed font!"));
345 WMGetFontFontSet(WMFont
*font
)
347 wassertrv(font
!=NULL
, NULL
);
349 if (font
->notFontSet
)
352 return font
->font
.set
;
357 WMWidthOfString(WMFont
*font
, char *text
, int length
)
359 wassertrv(font
!=NULL
, 0);
360 wassertrv(text
!=NULL
, 0);
362 if (font
->notFontSet
)
363 return XTextWidth(font
->font
.normal
, text
, length
);
368 XmbTextExtents(font
->font
.set
, text
, length
, &AIXsucks
, &rect
);
377 WMDrawString(WMScreen
*scr
, Drawable d
, GC gc
, WMFont
*font
, int x
, int y
,
378 char *text
, int length
)
380 wassertr(font
!=NULL
);
382 if (font
->notFontSet
) {
383 XSetFont(scr
->display
, gc
, font
->font
.normal
->fid
);
384 XDrawString(scr
->display
, d
, gc
, x
, y
+ font
->y
, text
, length
);
386 XmbDrawString(scr
->display
, d
, font
->font
.set
, gc
, x
, y
+ font
->y
,
393 WMDrawImageString(WMScreen
*scr
, Drawable d
, GC gc
, WMFont
*font
, int x
, int y
,
394 char *text
, int length
)
396 wassertr(font
!= NULL
);
398 if (font
->notFontSet
) {
399 XSetFont(scr
->display
, gc
, font
->font
.normal
->fid
);
400 XDrawImageString(scr
->display
, d
, gc
, x
, y
+ font
->y
, text
, length
);
402 XmbDrawImageString(scr
->display
, d
, font
->font
.set
, gc
, x
, y
+ font
->y
,
411 makeFontSetOfSize(char *fontset
, int size
)
423 ptr
= strchr(fontset
, ',');
425 int count
= ptr
-fontset
;
428 wwarning(_("font description %s is too large."), fontset
);
430 memcpy(font
, fontset
, count
);
441 tmp
= wmalloc(end
+ strlen(f
) + 8);
443 sprintf(tmp
, "%s,", newfs
);
444 sprintf(tmp
+ end
+ 1, f
, size
);
446 sprintf(tmp
+ end
, f
, size
);
461 changeFontProp(char *fname
, char *newprop
, int which
)
463 char before
[128], prop
[128], after
[128];
477 else if(part
==which
+1)
487 snprintf(fname
, 255, "%s-%s%s", before
, newprop
, after
);
492 WMNormalizeFont(WMScreen
*scr
, WMFont
*font
)
494 WMFont
*newfont
=NULL
;
500 snprintf(fname
, 255, font
->name
);
501 changeFontProp(fname
, "medium", 2);
502 changeFontProp(fname
, "r", 3);
503 newfont
= WMCreateNormalFont(scr
, fname
);
513 WMStrengthenFont(WMScreen
*scr
, WMFont
*font
)
515 WMFont
*newfont
=NULL
;
521 snprintf(fname
, 255, font
->name
);
522 changeFontProp(fname
, "bold", 2);
523 newfont
= WMCreateNormalFont(scr
, fname
);
533 WMUnstrengthenFont(WMScreen
*scr
, WMFont
*font
)
535 WMFont
*newfont
=NULL
;
541 snprintf(fname
, 255, font
->name
);
542 changeFontProp(fname
, "medium", 2);
543 newfont
= WMCreateNormalFont(scr
, fname
);
553 WMEmphasizeFont(WMScreen
*scr
, WMFont
*font
)
555 WMFont
*newfont
=NULL
;
561 snprintf(fname
, 255, font
->name
);
562 changeFontProp(fname
, "o", 3);
563 newfont
= WMCreateNormalFont(scr
, fname
);
573 WMUnemphasizeFont(WMScreen
*scr
, WMFont
*font
)
575 WMFont
*newfont
=NULL
;
581 snprintf(fname
, 255, font
->name
);
582 changeFontProp(fname
, "r", 3);
583 newfont
= WMCreateNormalFont(scr
, fname
);
593 WMGetFontOfSize(WMScreen
*scr
, WMFont
*font
, int size
)
595 if(!scr
|| !font
|| size
<1)