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+50;
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
->fontCache
, 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
->fontCache
, 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
) {
199 font
->height
= font
->font
.normal
->ascent
+font
->font
.normal
->descent
;
200 font
->y
= font
->font
.normal
->ascent
;
206 assert(WMHashInsert(scrPtr
->fontCache
, font
->name
, font
)==NULL
);
214 WMCreateFont(WMScreen
*scrPtr
, char *fontName
)
216 if (scrPtr
->useMultiByte
)
217 return WMCreateFontSet(scrPtr
, fontName
);
219 return WMCreateNormalFont(scrPtr
, fontName
);
225 WMRetainFont(WMFont
*font
)
227 wassertrv(font
!=NULL
, NULL
);
236 WMReleaseFont(WMFont
*font
)
238 wassertr(font
!=NULL
);
241 if (font
->refCount
< 1) {
242 if (font
->notFontSet
)
243 XFreeFont(font
->screen
->display
, font
->font
.normal
);
245 XFreeFontSet(font
->screen
->display
, font
->font
.set
);
248 WMHashRemove(font
->screen
->fontCache
, font
->name
);
258 WMFontHeight(WMFont
*font
)
260 wassertrv(font
!=NULL
, 0);
268 WMSystemFontOfSize(WMScreen
*scrPtr
, int size
)
273 fontSpec
= makeFontSetOfSize(WINGsConfiguration
.systemFont
, size
);
275 if (scrPtr
->useMultiByte
)
276 font
= WMCreateFontSet(scrPtr
, fontSpec
);
278 font
= WMCreateNormalFont(scrPtr
, fontSpec
);
281 if (scrPtr
->useMultiByte
) {
282 wwarning(_("could not load font set %s. Trying fixed."), fontSpec
);
283 font
= WMCreateFontSet(scrPtr
, "fixed");
285 font
= WMCreateFontSet(scrPtr
, "-*-fixed-medium-r-normal-*-14-*-*-*-*-*-*-*");
288 wwarning(_("could not load font %s. Trying fixed."), fontSpec
);
289 font
= WMCreateNormalFont(scrPtr
, "fixed");
292 wwarning(_("could not load fixed font!"));
304 WMBoldSystemFontOfSize(WMScreen
*scrPtr
, int size
)
309 fontSpec
= makeFontSetOfSize(WINGsConfiguration
.boldSystemFont
, size
);
311 if (scrPtr
->useMultiByte
)
312 font
= WMCreateFontSet(scrPtr
, fontSpec
);
314 font
= WMCreateNormalFont(scrPtr
, fontSpec
);
317 if (scrPtr
->useMultiByte
) {
318 wwarning(_("could not load font set %s. Trying fixed."), fontSpec
);
319 font
= WMCreateFontSet(scrPtr
, "fixed");
321 font
= WMCreateFontSet(scrPtr
, "-*-fixed-medium-r-normal-*-14-*-*-*-*-*-*-*");
324 wwarning(_("could not load font %s. Trying fixed."), fontSpec
);
325 font
= WMCreateNormalFont(scrPtr
, "fixed");
328 wwarning(_("could not load fixed font!"));
340 WMGetFontFontSet(WMFont
*font
)
342 wassertrv(font
!=NULL
, NULL
);
344 if (font
->notFontSet
)
347 return font
->font
.set
;
352 WMWidthOfString(WMFont
*font
, char *text
, int length
)
354 wassertrv(font
!=NULL
, 0);
355 wassertrv(text
!=NULL
, 0);
357 if (font
->notFontSet
)
358 return XTextWidth(font
->font
.normal
, text
, length
);
363 XmbTextExtents(font
->font
.set
, text
, length
, &AIXsucks
, &rect
);
372 WMDrawString(WMScreen
*scr
, Drawable d
, GC gc
, WMFont
*font
, int x
, int y
,
373 char *text
, int length
)
375 wassertr(font
!=NULL
);
377 if (font
->notFontSet
) {
378 XSetFont(scr
->display
, gc
, font
->font
.normal
->fid
);
379 XDrawString(scr
->display
, d
, gc
, x
, y
+ font
->y
, text
, length
);
381 XmbDrawString(scr
->display
, d
, font
->font
.set
, gc
, x
, y
+ font
->y
,
388 WMDrawImageString(WMScreen
*scr
, Drawable d
, GC gc
, WMFont
*font
, int x
, int y
,
389 char *text
, int length
)
391 wassertr(font
!= NULL
);
393 if (font
->notFontSet
) {
394 XSetFont(scr
->display
, gc
, font
->font
.normal
->fid
);
395 XDrawImageString(scr
->display
, d
, gc
, x
, y
+ font
->y
, text
, length
);
397 XmbDrawImageString(scr
->display
, d
, font
->font
.set
, gc
, x
, y
+ font
->y
,
406 makeFontSetOfSize(char *fontset
, int size
)
418 ptr
= strchr(fontset
, ',');
420 int count
= ptr
-fontset
;
423 wwarning(_("font description %s is too large."), fontset
);
425 memcpy(font
, fontset
, count
);
436 tmp
= wmalloc(end
+ strlen(f
) + 8);
438 sprintf(tmp
, "%s,", newfs
);
439 sprintf(tmp
+ end
+ 1, f
, size
);
441 sprintf(tmp
+ end
, f
, size
);
456 changeFontProp(char *fname
, char *newprop
, int which
)
458 char before
[128], prop
[128], after
[128];
470 if(part
==which
) bptr
= prop
;
471 else if(part
==which
+1) bptr
= after
;
478 snprintf(fname
, 255, "%s-%s%s", before
, newprop
, after
);
483 WMNormalizeFont(WMScreen
*scr
, WMFont
*font
)
485 WMFont
*newfont
=NULL
;
491 snprintf(fname
, 255, font
->name
);
492 changeFontProp(fname
, "medium", 2);
493 changeFontProp(fname
, "r", 3);
494 newfont
= WMCreateNormalFont(scr
, fname
);
504 WMStrengthenFont(WMScreen
*scr
, WMFont
*font
)
506 WMFont
*newfont
=NULL
;
512 snprintf(fname
, 255, font
->name
);
513 changeFontProp(fname
, "bold", 2);
514 newfont
= WMCreateNormalFont(scr
, fname
);
524 WMUnstrengthenFont(WMScreen
*scr
, WMFont
*font
)
526 WMFont
*newfont
=NULL
;
532 snprintf(fname
, 255, font
->name
);
533 changeFontProp(fname
, "medium", 2);
534 newfont
= WMCreateNormalFont(scr
, fname
);
544 WMEmphasizeFont(WMScreen
*scr
, WMFont
*font
)
546 WMFont
*newfont
=NULL
;
552 snprintf(fname
, 255, font
->name
);
553 changeFontProp(fname
, "o", 3);
554 newfont
= WMCreateNormalFont(scr
, fname
);
564 WMUnemphasizeFont(WMScreen
*scr
, WMFont
*font
)
566 WMFont
*newfont
=NULL
;
572 snprintf(fname
, 255, font
->name
);
573 changeFontProp(fname
, "r", 3);
574 newfont
= WMCreateNormalFont(scr
, fname
);
583 WMGetFontOfSize(WMScreen
*scr
, WMFont
*font
, int size
)
585 if(!scr
|| !font
|| size
<1)