/* xscreensaver, Copyright (c) 2014-2018 Jamie Zawinski <jwz@jwz.org>
* Permission to use, copy, modify, distribute, and sell this software and its
* documentation for any purpose is hereby granted without fee, provided that
* the above copyright notice appear in all copies and that both that
* copyright notice and this permission notice appear in supporting
* documentation. No representations are made about the suitability of this
* software for any purpose. It is provided "as is" without express or
/* Compatibility layer using XDrawString, XDrawString16() or Xutf8DrawString().
This layer is used by X11 systems without Xft, and by MacOS / iOS.
extern const char *progname
;
XftFontOpenXlfd (Display
*dpy
, int screen
, _Xconst
char *xlfd
)
XftFont
*ff
= (XftFont
*) calloc (1, sizeof(*ff
));
if (!dpy
|| !xlfd
) abort();
ff
->xfont
= XLoadQueryFont (dpy
, xlfd
);
ff
->name
= strdup (xlfd
);
ff
->ascent
= ff
->xfont
->ascent
;
ff
->descent
= ff
->xfont
->descent
;
ff
->height
= ff
->ascent
+ ff
->descent
;
# ifdef HAVE_XUTF8DRAWSTRING
// In the event of -*-random-* (under JWXYZ), get the actual XLFD,
// otherwise we'll get another random font that doesn't match ff->xfont.
char *xlfd_resolved
= NULL
;
char **missing_charset_list_return
;
int missing_charset_count_return
;
for (i
= 0; i
!= ff
->xfont
->n_properties
; ++i
) {
if (ff
->xfont
->properties
[i
].name
== XA_FONT
) {
xlfd_resolved
= XGetAtomName (dpy
, ff
->xfont
->properties
[i
].card32
);
ss
= (char *) malloc (strlen(xlfd
) + 10);
ff
->fontset
= XCreateFontSet (dpy
, ss
,
&missing_charset_list_return
,
&missing_charset_count_return
,
for (i
= 0; i
< missing_charset_count_return
; i
++)
fprintf (stderr
, "%s: missing charset: %s\n",
ss
, missing_charset_list_return
[i
]);
/* Apparently this is not to be freed. */
/* if (def_string_return) XFree (def_string_return); */
if (missing_charset_list_return
)
XFreeStringList (missing_charset_list_return
);
XftFontClose (Display
*dpy
, XftFont
*font
)
if (!dpy
|| !font
) abort();
XFreeFont (dpy
, font
->xfont
);
# ifdef HAVE_XUTF8DRAWSTRING
XFreeFontSet (dpy
, font
->fontset
);
XftColorAllocName (Display
*dpy
,
if (!dpy
|| !visual
|| !name
|| !result
) abort();
if (! XParseColor (dpy
, cmap
, name
, &color
))
fprintf (stderr
, "%s: can't parse color %s", progname
, name
);
else if (! XAllocColor (dpy
, cmap
, &color
))
fprintf (stderr
, "%s: couldn't allocate color %s", progname
, name
);
color2
.green
= color
.green
;
color2
.blue
= color
.blue
;
XftColorAllocValue (dpy
, visual
, cmap
, &color2
, result
);
result
->pixel
= color
.pixel
;
maskbase (unsigned long m
)
masklen (unsigned long m
)
y
= (m
>> 1) & 033333333333;
y
= m
- y
- ((y
>>1) & 033333333333);
return (short) (((y
+ (y
>> 3)) & 030707070707) % 077);
XftColorAllocValue (Display
*dpy
,
_Xconst XRenderColor
*color
,
if (!dpy
|| !visual
|| !color
|| !result
) abort();
if (visual
->class == TrueColor
)
int red_shift
= maskbase (visual
->red_mask
);
int red_len
= masklen (visual
->red_mask
);
int green_shift
= maskbase (visual
->green_mask
);
int green_len
= masklen (visual
->green_mask
);
int blue_shift
= maskbase (visual
->blue_mask
);
int blue_len
= masklen (visual
->blue_mask
);
result
->pixel
= (((color
->red
>> (16 - red_len
)) << red_shift
) |
((color
->green
>> (16 - green_len
)) << green_shift
) |
((color
->blue
>> (16 - blue_len
)) << blue_shift
));
result
->pixel
|= BlackPixel(dpy
, 0); /* alpha */
xcolor
.green
= color
->green
;
xcolor
.blue
= color
->blue
;
if (!XAllocColor (dpy
, cmap
, &xcolor
))
result
->pixel
= xcolor
.pixel
;
result
->color
.red
= color
->red
;
result
->color
.green
= color
->green
;
result
->color
.blue
= color
->blue
;
result
->color
.alpha
= color
->alpha
;
XftColorFree (Display
*dpy
,
if (!dpy
|| !visual
|| !color
) abort();
if (visual
->class != TrueColor
)
XFreeColors (dpy
, cmap
, &color
->pixel
, 1, 0);
XftDrawCreate (Display
*dpy
,
XftDraw
*dd
= (XftDraw
*) calloc (1, sizeof(*dd
));
if (!dpy
|| !drawable
|| !visual
) abort();
dd
->gc
= XCreateGC (dpy
, drawable
, 0, 0);
XftDrawDestroy (XftDraw
*draw
)
XFreeGC (draw
->dpy
, draw
->gc
);
XftTextExtentsUtf8 (Display
*dpy
,
if (!dpy
|| !font
|| !string
|| !extents
) abort();
# ifdef HAVE_XUTF8DRAWSTRING
Xutf8TextExtents (font
->fontset
, (const char *) string
, len
, &ink
, 0);
XmbRectangle_to_XCharStruct (ink
, overall
, advancement
);
# else /* !HAVE_XUTF8DRAWSTRING */
char *s2
= (char *) malloc (len
+ 1);
int direction
, ascent
, descent
;
strncpy (s2
, (char *) string
, len
);
s16
= utf8_to_XChar2b (s2
, &s16_len
);
XTextExtents16 (font
->xfont
, s16
, s16_len
,
&direction
, &ascent
, &descent
, &overall
);
# endif /* !HAVE_XUTF8DRAWSTRING */
XCharStruct_to_XGlyphInfo (overall
, *extents
);
XftDrawStringUtf8 (XftDraw
*draw
,
if (!draw
|| !color
|| !font
|| !string
) abort();
if (color
->pixel
!= draw
->pixel
)
XSetForeground (draw
->dpy
, draw
->gc
, color
->pixel
);
draw
->pixel
= color
->pixel
;
if (font
->xfont
->fid
!= draw
->fid
)
XSetFont (draw
->dpy
, draw
->gc
, font
->xfont
->fid
);
draw
->fid
= font
->xfont
->fid
;
# ifdef HAVE_XUTF8DRAWSTRING
/* If we have Xutf8DrawString, use it instead of XDrawString16 because
there is some chance it will handle characters of more than 16 bits
(beyond the Basic Multilingual Plane).
/* #### I guess I don't really understand how FontSet works, because when
using the real X11 implementation of Xutf8DrawString, this seems
to just truncate the text at the first non-ASCII character.
The XDrawString16() path works, however, at the expense of losing
everything above Basic Multilingual. However, that path is only
taken on X11 systems that are old enough to not have libXft,
which means that the chance of Unicode working was already slim.
Xutf8DrawString (draw
->dpy
, draw
->drawable
, font
->fontset
, draw
->gc
, x
, y
,
(const char *) string
, len
);
char *s2
= (char *) malloc (len
+ 1);
strncpy (s2
, (char *) string
, len
);
s16
= utf8_to_XChar2b (s2
, &s16_len
);
XDrawString16 (draw
->dpy
, draw
->drawable
, draw
->gc
, x
, y
, s16
, s16_len
);
const int Wempty_translation_unit_is_a_dumb_warning
= 0;