2015-05-04 20:54:46 +01:00
|
|
|
|
/* See LICENSE file for copyright and license details. */
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <X11/Xlib.h>
|
|
|
|
|
#include <X11/Xft/Xft.h>
|
|
|
|
|
|
|
|
|
|
#include "drw.h"
|
|
|
|
|
#include "util.h"
|
|
|
|
|
|
|
|
|
|
#define UTF_INVALID 0xFFFD
|
|
|
|
|
|
2024-07-04 22:25:37 +01:00
|
|
|
|
static int
|
|
|
|
|
utf8decode(const char *s_in, long *u, int *err)
|
2015-09-27 22:56:02 +01:00
|
|
|
|
{
|
2024-07-04 22:25:37 +01:00
|
|
|
|
static const unsigned char lens[] = {
|
|
|
|
|
/* 0XXXX */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
|
|
|
|
|
/* 10XXX */ 0, 0, 0, 0, 0, 0, 0, 0, /* invalid */
|
|
|
|
|
/* 110XX */ 2, 2, 2, 2,
|
|
|
|
|
/* 1110X */ 3, 3,
|
|
|
|
|
/* 11110 */ 4,
|
|
|
|
|
/* 11111 */ 0, /* invalid */
|
|
|
|
|
};
|
|
|
|
|
static const unsigned char leading_mask[] = { 0x7F, 0x1F, 0x0F, 0x07 };
|
|
|
|
|
static const unsigned int overlong[] = { 0x0, 0x80, 0x0800, 0x10000 };
|
|
|
|
|
|
|
|
|
|
const unsigned char *s = (const unsigned char *)s_in;
|
|
|
|
|
int len = lens[*s >> 3];
|
2015-05-04 20:54:46 +01:00
|
|
|
|
*u = UTF_INVALID;
|
2024-07-04 22:25:37 +01:00
|
|
|
|
*err = 1;
|
|
|
|
|
if (len == 0)
|
2015-05-04 20:54:46 +01:00
|
|
|
|
return 1;
|
2024-07-04 22:25:37 +01:00
|
|
|
|
|
|
|
|
|
long cp = s[0] & leading_mask[len - 1];
|
|
|
|
|
for (int i = 1; i < len; ++i) {
|
|
|
|
|
if (s[i] == '\0' || (s[i] & 0xC0) != 0x80)
|
|
|
|
|
return i;
|
|
|
|
|
cp = (cp << 6) | (s[i] & 0x3F);
|
2015-05-04 20:54:46 +01:00
|
|
|
|
}
|
2024-07-04 22:25:37 +01:00
|
|
|
|
/* out of range, surrogate, overlong encoding */
|
|
|
|
|
if (cp > 0x10FFFF || (cp >> 11) == 0x1B || cp < overlong[len - 1])
|
|
|
|
|
return len;
|
2015-09-27 22:56:02 +01:00
|
|
|
|
|
2024-07-04 22:25:37 +01:00
|
|
|
|
*err = 0;
|
|
|
|
|
*u = cp;
|
2015-05-04 20:54:46 +01:00
|
|
|
|
return len;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Drw *
|
2015-09-27 22:56:02 +01:00
|
|
|
|
drw_create(Display *dpy, int screen, Window root, unsigned int w, unsigned int h)
|
|
|
|
|
{
|
2016-05-21 20:51:14 +01:00
|
|
|
|
Drw *drw = ecalloc(1, sizeof(Drw));
|
2015-09-27 22:56:02 +01:00
|
|
|
|
|
2015-05-04 20:54:46 +01:00
|
|
|
|
drw->dpy = dpy;
|
|
|
|
|
drw->screen = screen;
|
|
|
|
|
drw->root = root;
|
|
|
|
|
drw->w = w;
|
|
|
|
|
drw->h = h;
|
|
|
|
|
drw->drawable = XCreatePixmap(dpy, root, w, h, DefaultDepth(dpy, screen));
|
|
|
|
|
drw->gc = XCreateGC(dpy, root, 0, NULL);
|
|
|
|
|
XSetLineAttributes(dpy, drw->gc, 1, LineSolid, CapButt, JoinMiter);
|
2015-09-27 22:56:02 +01:00
|
|
|
|
|
2015-05-04 20:54:46 +01:00
|
|
|
|
return drw;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
2015-09-27 22:56:02 +01:00
|
|
|
|
drw_resize(Drw *drw, unsigned int w, unsigned int h)
|
|
|
|
|
{
|
2016-05-21 20:51:14 +01:00
|
|
|
|
if (!drw)
|
|
|
|
|
return;
|
|
|
|
|
|
2015-05-04 20:54:46 +01:00
|
|
|
|
drw->w = w;
|
|
|
|
|
drw->h = h;
|
2015-09-27 22:56:02 +01:00
|
|
|
|
if (drw->drawable)
|
2015-05-04 20:54:46 +01:00
|
|
|
|
XFreePixmap(drw->dpy, drw->drawable);
|
|
|
|
|
drw->drawable = XCreatePixmap(drw->dpy, drw->root, w, h, DefaultDepth(drw->dpy, drw->screen));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
2015-09-27 22:56:02 +01:00
|
|
|
|
drw_free(Drw *drw)
|
|
|
|
|
{
|
2015-05-04 20:54:46 +01:00
|
|
|
|
XFreePixmap(drw->dpy, drw->drawable);
|
|
|
|
|
XFreeGC(drw->dpy, drw->gc);
|
2020-06-11 17:45:33 +01:00
|
|
|
|
drw_fontset_free(drw->fonts);
|
2015-05-04 20:54:46 +01:00
|
|
|
|
free(drw);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* This function is an implementation detail. Library users should use
|
2016-05-21 20:51:14 +01:00
|
|
|
|
* drw_fontset_create instead.
|
2015-05-04 20:54:46 +01:00
|
|
|
|
*/
|
|
|
|
|
static Fnt *
|
2016-05-21 20:51:14 +01:00
|
|
|
|
xfont_create(Drw *drw, const char *fontname, FcPattern *fontpattern)
|
2015-09-27 22:56:02 +01:00
|
|
|
|
{
|
2015-05-04 20:54:46 +01:00
|
|
|
|
Fnt *font;
|
2015-10-20 21:55:39 +01:00
|
|
|
|
XftFont *xfont = NULL;
|
|
|
|
|
FcPattern *pattern = NULL;
|
2015-05-04 20:54:46 +01:00
|
|
|
|
|
|
|
|
|
if (fontname) {
|
2016-05-21 20:51:14 +01:00
|
|
|
|
/* Using the pattern found at font->xfont->pattern does not yield the
|
|
|
|
|
* same substitution results as using the pattern returned by
|
2015-05-04 20:54:46 +01:00
|
|
|
|
* FcNameParse; using the latter results in the desired fallback
|
2016-05-21 20:51:14 +01:00
|
|
|
|
* behaviour whereas the former just results in missing-character
|
|
|
|
|
* rectangles being drawn, at least with some fonts. */
|
2015-10-20 21:55:39 +01:00
|
|
|
|
if (!(xfont = XftFontOpenName(drw->dpy, drw->screen, fontname))) {
|
2016-05-21 20:51:14 +01:00
|
|
|
|
fprintf(stderr, "error, cannot load font from name: '%s'\n", fontname);
|
2015-10-20 21:55:39 +01:00
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
if (!(pattern = FcNameParse((FcChar8 *) fontname))) {
|
2016-05-21 20:51:14 +01:00
|
|
|
|
fprintf(stderr, "error, cannot parse font name to pattern: '%s'\n", fontname);
|
2015-10-20 21:55:39 +01:00
|
|
|
|
XftFontClose(drw->dpy, xfont);
|
|
|
|
|
return NULL;
|
2015-05-04 20:54:46 +01:00
|
|
|
|
}
|
|
|
|
|
} else if (fontpattern) {
|
2015-10-20 21:55:39 +01:00
|
|
|
|
if (!(xfont = XftFontOpenPattern(drw->dpy, fontpattern))) {
|
2016-05-21 20:51:14 +01:00
|
|
|
|
fprintf(stderr, "error, cannot load font from pattern.\n");
|
2015-10-20 21:55:39 +01:00
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2016-08-12 13:39:30 +01:00
|
|
|
|
die("no font specified.");
|
2015-05-04 20:54:46 +01:00
|
|
|
|
}
|
|
|
|
|
|
2015-10-20 21:55:39 +01:00
|
|
|
|
font = ecalloc(1, sizeof(Fnt));
|
|
|
|
|
font->xfont = xfont;
|
|
|
|
|
font->pattern = pattern;
|
2016-05-21 20:51:14 +01:00
|
|
|
|
font->h = xfont->ascent + xfont->descent;
|
2015-05-04 20:54:46 +01:00
|
|
|
|
font->dpy = drw->dpy;
|
2015-09-27 22:56:02 +01:00
|
|
|
|
|
2015-05-04 20:54:46 +01:00
|
|
|
|
return font;
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-21 20:51:14 +01:00
|
|
|
|
static void
|
|
|
|
|
xfont_free(Fnt *font)
|
2015-09-27 22:56:02 +01:00
|
|
|
|
{
|
2016-05-21 20:51:14 +01:00
|
|
|
|
if (!font)
|
|
|
|
|
return;
|
|
|
|
|
if (font->pattern)
|
|
|
|
|
FcPatternDestroy(font->pattern);
|
|
|
|
|
XftFontClose(font->dpy, font->xfont);
|
|
|
|
|
free(font);
|
2015-05-04 20:54:46 +01:00
|
|
|
|
}
|
|
|
|
|
|
2016-05-21 20:51:14 +01:00
|
|
|
|
Fnt*
|
|
|
|
|
drw_fontset_create(Drw* drw, const char *fonts[], size_t fontcount)
|
2015-09-27 22:56:02 +01:00
|
|
|
|
{
|
2016-05-21 20:51:14 +01:00
|
|
|
|
Fnt *cur, *ret = NULL;
|
2015-05-04 20:54:46 +01:00
|
|
|
|
size_t i;
|
|
|
|
|
|
2016-05-21 20:51:14 +01:00
|
|
|
|
if (!drw || !fonts)
|
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
|
|
for (i = 1; i <= fontcount; i++) {
|
|
|
|
|
if ((cur = xfont_create(drw, fonts[fontcount - i], NULL))) {
|
|
|
|
|
cur->next = ret;
|
|
|
|
|
ret = cur;
|
2015-05-04 20:54:46 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
2016-05-21 20:51:14 +01:00
|
|
|
|
return (drw->fonts = ret);
|
2015-05-04 20:54:46 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
2016-05-21 20:51:14 +01:00
|
|
|
|
drw_fontset_free(Fnt *font)
|
2015-09-27 22:56:02 +01:00
|
|
|
|
{
|
2016-05-21 20:51:14 +01:00
|
|
|
|
if (font) {
|
|
|
|
|
drw_fontset_free(font->next);
|
|
|
|
|
xfont_free(font);
|
|
|
|
|
}
|
2015-05-04 20:54:46 +01:00
|
|
|
|
}
|
|
|
|
|
|
2016-05-21 20:51:14 +01:00
|
|
|
|
void
|
|
|
|
|
drw_clr_create(Drw *drw, Clr *dest, const char *clrname)
|
2015-09-27 22:56:02 +01:00
|
|
|
|
{
|
2016-05-21 20:51:14 +01:00
|
|
|
|
if (!drw || !dest || !clrname)
|
|
|
|
|
return;
|
2015-10-20 21:51:57 +01:00
|
|
|
|
|
|
|
|
|
if (!XftColorAllocName(drw->dpy, DefaultVisual(drw->dpy, drw->screen),
|
|
|
|
|
DefaultColormap(drw->dpy, drw->screen),
|
2016-05-21 20:51:14 +01:00
|
|
|
|
clrname, dest))
|
2016-08-12 13:39:30 +01:00
|
|
|
|
die("error, cannot allocate color '%s'", clrname);
|
2016-05-21 20:51:14 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Wrapper to create color schemes. The caller has to call free(3) on the
|
|
|
|
|
* returned color scheme when done using it. */
|
|
|
|
|
Clr *
|
|
|
|
|
drw_scm_create(Drw *drw, const char *clrnames[], size_t clrcount)
|
|
|
|
|
{
|
|
|
|
|
size_t i;
|
|
|
|
|
Clr *ret;
|
2015-09-27 22:56:02 +01:00
|
|
|
|
|
2016-05-21 20:51:14 +01:00
|
|
|
|
/* need at least two colors for a scheme */
|
2019-02-02 12:54:15 +00:00
|
|
|
|
if (!drw || !clrnames || clrcount < 2 || !(ret = ecalloc(clrcount, sizeof(XftColor))))
|
2016-05-21 20:51:14 +01:00
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
|
|
for (i = 0; i < clrcount; i++)
|
|
|
|
|
drw_clr_create(drw, &ret[i], clrnames[i]);
|
|
|
|
|
return ret;
|
2015-05-04 20:54:46 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
2016-05-21 20:51:14 +01:00
|
|
|
|
drw_setfontset(Drw *drw, Fnt *set)
|
2015-09-27 22:56:02 +01:00
|
|
|
|
{
|
2016-05-21 20:51:14 +01:00
|
|
|
|
if (drw)
|
|
|
|
|
drw->fonts = set;
|
2015-05-04 20:54:46 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
2016-05-21 20:51:14 +01:00
|
|
|
|
drw_setscheme(Drw *drw, Clr *scm)
|
2015-09-27 22:56:02 +01:00
|
|
|
|
{
|
2016-05-21 20:51:14 +01:00
|
|
|
|
if (drw)
|
|
|
|
|
drw->scheme = scm;
|
2015-05-04 20:54:46 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
2016-05-21 20:51:14 +01:00
|
|
|
|
drw_rect(Drw *drw, int x, int y, unsigned int w, unsigned int h, int filled, int invert)
|
2015-09-27 22:56:02 +01:00
|
|
|
|
{
|
2016-05-21 20:51:14 +01:00
|
|
|
|
if (!drw || !drw->scheme)
|
2015-05-04 20:54:46 +01:00
|
|
|
|
return;
|
2016-05-21 20:51:14 +01:00
|
|
|
|
XSetForeground(drw->dpy, drw->gc, invert ? drw->scheme[ColBg].pixel : drw->scheme[ColFg].pixel);
|
2015-09-27 22:56:02 +01:00
|
|
|
|
if (filled)
|
2016-05-21 20:51:14 +01:00
|
|
|
|
XFillRectangle(drw->dpy, drw->drawable, drw->gc, x, y, w, h);
|
|
|
|
|
else
|
|
|
|
|
XDrawRectangle(drw->dpy, drw->drawable, drw->gc, x, y, w - 1, h - 1);
|
2015-05-04 20:54:46 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int
|
2016-05-21 20:51:14 +01:00
|
|
|
|
drw_text(Drw *drw, int x, int y, unsigned int w, unsigned int h, unsigned int lpad, const char *text, int invert)
|
2015-09-27 22:56:02 +01:00
|
|
|
|
{
|
2023-07-07 12:00:42 +01:00
|
|
|
|
int ty, ellipsis_x = 0;
|
|
|
|
|
unsigned int tmpw, ew, ellipsis_w = 0, ellipsis_len, hash, h0, h1;
|
2015-10-20 21:56:57 +01:00
|
|
|
|
XftDraw *d = NULL;
|
2016-05-21 20:51:14 +01:00
|
|
|
|
Fnt *usedfont, *curfont, *nextfont;
|
2024-07-04 22:25:37 +01:00
|
|
|
|
int utf8strlen, utf8charlen, utf8err, render = x || y || w || h;
|
2015-05-04 20:54:46 +01:00
|
|
|
|
long utf8codepoint = 0;
|
|
|
|
|
const char *utf8str;
|
|
|
|
|
FcCharSet *fccharset;
|
|
|
|
|
FcPattern *fcpattern;
|
|
|
|
|
FcPattern *match;
|
|
|
|
|
XftResult result;
|
drw_text: improve both performance and correctness
this patch makes some non-trivial changes, which significantly improves
the performance of drawing large strings as well as fixes any issues
regarding the printing of the ellipsis when string gets truncated.
* performance:
before there were two O(n) loops, one which finds how long we can go
without changing font, and the second loop would (incorrectly) truncate
the string if it's too big.
this patch merges the overflow calculation into the first loop and exits
out when overflow is detected. when dumping lots of emojies into dmenu,
i see some noticeable startup time improvement:
before -> after
460ms -> 360ms
input latency when scrolling up/down is also noticeably better and can
be tested with the following:
for _ in $(seq 20); do
cat /dev/urandom | base64 | tr -d '\n' | head -c 1000000
echo
done | ./dmenu -l 10
* correctness:
the previous version would incorrectly assumed single byte chars and
would overwrite them with '.' , this caused a whole bunch of obvious
problems, including the ellipsis not getting rendered if then font
changed.
in addition to exiting out when we detect overflow, this patch also
keeps track of the last x-position where the ellipsis would fit. if we
detect overflow, we simply make a recursing call to drw_text() at the
ellipsis_x position and overwrite what was there.
so now the ellipsis will always be printed properly, regardless of
weather the font changes or if the string is single byte char or not.
the idea of rendering the ellipsis on top incase of overflow was
from Bakkeby <bakkeby@gmail.com>, thanks! however the original patch had
some issues incorrectly truncating the prompt (-p flag) and cutting off
emojies. those have been fixed in here.
2022-03-23 18:37:55 +00:00
|
|
|
|
int charexists = 0, overflow = 0;
|
2022-03-23 18:37:55 +00:00
|
|
|
|
/* keep track of a couple codepoints for which we have no match. */
|
2024-07-04 22:27:47 +01:00
|
|
|
|
static unsigned int nomatches[128], ellipsis_width, invalid_width;
|
|
|
|
|
static const char invalid[] = "<EFBFBD>";
|
2015-05-04 20:54:46 +01:00
|
|
|
|
|
2022-03-27 20:02:52 +01:00
|
|
|
|
if (!drw || (render && (!drw->scheme || !w)) || !text || !drw->fonts)
|
2015-05-04 20:54:46 +01:00
|
|
|
|
return 0;
|
|
|
|
|
|
2016-05-21 20:51:14 +01:00
|
|
|
|
if (!render) {
|
2022-03-23 20:00:00 +00:00
|
|
|
|
w = invert ? invert : ~invert;
|
2015-10-20 21:56:57 +01:00
|
|
|
|
} else {
|
2016-05-21 20:51:14 +01:00
|
|
|
|
XSetForeground(drw->dpy, drw->gc, drw->scheme[invert ? ColFg : ColBg].pixel);
|
2015-10-20 21:56:57 +01:00
|
|
|
|
XFillRectangle(drw->dpy, drw->drawable, drw->gc, x, y, w, h);
|
2024-10-30 12:02:57 +00:00
|
|
|
|
if (w < lpad)
|
|
|
|
|
return x + w;
|
2015-10-20 21:56:57 +01:00
|
|
|
|
d = XftDrawCreate(drw->dpy, drw->drawable,
|
|
|
|
|
DefaultVisual(drw->dpy, drw->screen),
|
|
|
|
|
DefaultColormap(drw->dpy, drw->screen));
|
2016-05-21 20:51:14 +01:00
|
|
|
|
x += lpad;
|
|
|
|
|
w -= lpad;
|
2015-05-04 20:54:46 +01:00
|
|
|
|
}
|
|
|
|
|
|
2016-05-21 20:51:14 +01:00
|
|
|
|
usedfont = drw->fonts;
|
2022-03-28 16:38:49 +01:00
|
|
|
|
if (!ellipsis_width && render)
|
|
|
|
|
ellipsis_width = drw_fontset_getwidth(drw, "...");
|
2024-07-14 10:40:20 +01:00
|
|
|
|
if (!invalid_width && render)
|
2024-07-04 22:27:47 +01:00
|
|
|
|
invalid_width = drw_fontset_getwidth(drw, invalid);
|
2015-05-04 20:54:46 +01:00
|
|
|
|
while (1) {
|
2024-07-04 22:25:37 +01:00
|
|
|
|
ew = ellipsis_len = utf8err = utf8charlen = utf8strlen = 0;
|
2015-05-04 20:54:46 +01:00
|
|
|
|
utf8str = text;
|
|
|
|
|
nextfont = NULL;
|
|
|
|
|
while (*text) {
|
2024-07-04 22:25:37 +01:00
|
|
|
|
utf8charlen = utf8decode(text, &utf8codepoint, &utf8err);
|
2016-05-21 20:51:14 +01:00
|
|
|
|
for (curfont = drw->fonts; curfont; curfont = curfont->next) {
|
|
|
|
|
charexists = charexists || XftCharExists(drw->dpy, curfont->xfont, utf8codepoint);
|
2015-05-04 20:54:46 +01:00
|
|
|
|
if (charexists) {
|
drw_text: improve both performance and correctness
this patch makes some non-trivial changes, which significantly improves
the performance of drawing large strings as well as fixes any issues
regarding the printing of the ellipsis when string gets truncated.
* performance:
before there were two O(n) loops, one which finds how long we can go
without changing font, and the second loop would (incorrectly) truncate
the string if it's too big.
this patch merges the overflow calculation into the first loop and exits
out when overflow is detected. when dumping lots of emojies into dmenu,
i see some noticeable startup time improvement:
before -> after
460ms -> 360ms
input latency when scrolling up/down is also noticeably better and can
be tested with the following:
for _ in $(seq 20); do
cat /dev/urandom | base64 | tr -d '\n' | head -c 1000000
echo
done | ./dmenu -l 10
* correctness:
the previous version would incorrectly assumed single byte chars and
would overwrite them with '.' , this caused a whole bunch of obvious
problems, including the ellipsis not getting rendered if then font
changed.
in addition to exiting out when we detect overflow, this patch also
keeps track of the last x-position where the ellipsis would fit. if we
detect overflow, we simply make a recursing call to drw_text() at the
ellipsis_x position and overwrite what was there.
so now the ellipsis will always be printed properly, regardless of
weather the font changes or if the string is single byte char or not.
the idea of rendering the ellipsis on top incase of overflow was
from Bakkeby <bakkeby@gmail.com>, thanks! however the original patch had
some issues incorrectly truncating the prompt (-p flag) and cutting off
emojies. those have been fixed in here.
2022-03-23 18:37:55 +00:00
|
|
|
|
drw_font_getexts(curfont, text, utf8charlen, &tmpw, NULL);
|
|
|
|
|
if (ew + ellipsis_width <= w) {
|
|
|
|
|
/* keep track where the ellipsis still fits */
|
|
|
|
|
ellipsis_x = x + ew;
|
|
|
|
|
ellipsis_w = w - ew;
|
|
|
|
|
ellipsis_len = utf8strlen;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (ew + tmpw > w) {
|
|
|
|
|
overflow = 1;
|
2022-03-23 20:00:00 +00:00
|
|
|
|
/* called from drw_fontset_getwidth_clamp():
|
|
|
|
|
* it wants the width AFTER the overflow
|
|
|
|
|
*/
|
|
|
|
|
if (!render)
|
|
|
|
|
x += tmpw;
|
|
|
|
|
else
|
|
|
|
|
utf8strlen = ellipsis_len;
|
drw_text: improve both performance and correctness
this patch makes some non-trivial changes, which significantly improves
the performance of drawing large strings as well as fixes any issues
regarding the printing of the ellipsis when string gets truncated.
* performance:
before there were two O(n) loops, one which finds how long we can go
without changing font, and the second loop would (incorrectly) truncate
the string if it's too big.
this patch merges the overflow calculation into the first loop and exits
out when overflow is detected. when dumping lots of emojies into dmenu,
i see some noticeable startup time improvement:
before -> after
460ms -> 360ms
input latency when scrolling up/down is also noticeably better and can
be tested with the following:
for _ in $(seq 20); do
cat /dev/urandom | base64 | tr -d '\n' | head -c 1000000
echo
done | ./dmenu -l 10
* correctness:
the previous version would incorrectly assumed single byte chars and
would overwrite them with '.' , this caused a whole bunch of obvious
problems, including the ellipsis not getting rendered if then font
changed.
in addition to exiting out when we detect overflow, this patch also
keeps track of the last x-position where the ellipsis would fit. if we
detect overflow, we simply make a recursing call to drw_text() at the
ellipsis_x position and overwrite what was there.
so now the ellipsis will always be printed properly, regardless of
weather the font changes or if the string is single byte char or not.
the idea of rendering the ellipsis on top incase of overflow was
from Bakkeby <bakkeby@gmail.com>, thanks! however the original patch had
some issues incorrectly truncating the prompt (-p flag) and cutting off
emojies. those have been fixed in here.
2022-03-23 18:37:55 +00:00
|
|
|
|
} else if (curfont == usedfont) {
|
2015-05-04 20:54:46 +01:00
|
|
|
|
text += utf8charlen;
|
2024-07-04 22:27:47 +01:00
|
|
|
|
utf8strlen += utf8err ? 0 : utf8charlen;
|
|
|
|
|
ew += utf8err ? 0 : tmpw;
|
2015-05-04 20:54:46 +01:00
|
|
|
|
} else {
|
2016-05-21 20:51:14 +01:00
|
|
|
|
nextfont = curfont;
|
2015-05-04 20:54:46 +01:00
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-04 22:27:47 +01:00
|
|
|
|
if (overflow || !charexists || nextfont || utf8err)
|
2015-05-04 20:54:46 +01:00
|
|
|
|
break;
|
2015-09-27 22:56:02 +01:00
|
|
|
|
else
|
2015-05-04 20:54:46 +01:00
|
|
|
|
charexists = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (utf8strlen) {
|
drw_text: improve both performance and correctness
this patch makes some non-trivial changes, which significantly improves
the performance of drawing large strings as well as fixes any issues
regarding the printing of the ellipsis when string gets truncated.
* performance:
before there were two O(n) loops, one which finds how long we can go
without changing font, and the second loop would (incorrectly) truncate
the string if it's too big.
this patch merges the overflow calculation into the first loop and exits
out when overflow is detected. when dumping lots of emojies into dmenu,
i see some noticeable startup time improvement:
before -> after
460ms -> 360ms
input latency when scrolling up/down is also noticeably better and can
be tested with the following:
for _ in $(seq 20); do
cat /dev/urandom | base64 | tr -d '\n' | head -c 1000000
echo
done | ./dmenu -l 10
* correctness:
the previous version would incorrectly assumed single byte chars and
would overwrite them with '.' , this caused a whole bunch of obvious
problems, including the ellipsis not getting rendered if then font
changed.
in addition to exiting out when we detect overflow, this patch also
keeps track of the last x-position where the ellipsis would fit. if we
detect overflow, we simply make a recursing call to drw_text() at the
ellipsis_x position and overwrite what was there.
so now the ellipsis will always be printed properly, regardless of
weather the font changes or if the string is single byte char or not.
the idea of rendering the ellipsis on top incase of overflow was
from Bakkeby <bakkeby@gmail.com>, thanks! however the original patch had
some issues incorrectly truncating the prompt (-p flag) and cutting off
emojies. those have been fixed in here.
2022-03-23 18:37:55 +00:00
|
|
|
|
if (render) {
|
|
|
|
|
ty = y + (h - usedfont->h) / 2 + usedfont->xfont->ascent;
|
|
|
|
|
XftDrawStringUtf8(d, &drw->scheme[invert ? ColBg : ColFg],
|
|
|
|
|
usedfont->xfont, x, ty, (XftChar8 *)utf8str, utf8strlen);
|
2015-05-04 20:54:46 +01:00
|
|
|
|
}
|
drw_text: improve both performance and correctness
this patch makes some non-trivial changes, which significantly improves
the performance of drawing large strings as well as fixes any issues
regarding the printing of the ellipsis when string gets truncated.
* performance:
before there were two O(n) loops, one which finds how long we can go
without changing font, and the second loop would (incorrectly) truncate
the string if it's too big.
this patch merges the overflow calculation into the first loop and exits
out when overflow is detected. when dumping lots of emojies into dmenu,
i see some noticeable startup time improvement:
before -> after
460ms -> 360ms
input latency when scrolling up/down is also noticeably better and can
be tested with the following:
for _ in $(seq 20); do
cat /dev/urandom | base64 | tr -d '\n' | head -c 1000000
echo
done | ./dmenu -l 10
* correctness:
the previous version would incorrectly assumed single byte chars and
would overwrite them with '.' , this caused a whole bunch of obvious
problems, including the ellipsis not getting rendered if then font
changed.
in addition to exiting out when we detect overflow, this patch also
keeps track of the last x-position where the ellipsis would fit. if we
detect overflow, we simply make a recursing call to drw_text() at the
ellipsis_x position and overwrite what was there.
so now the ellipsis will always be printed properly, regardless of
weather the font changes or if the string is single byte char or not.
the idea of rendering the ellipsis on top incase of overflow was
from Bakkeby <bakkeby@gmail.com>, thanks! however the original patch had
some issues incorrectly truncating the prompt (-p flag) and cutting off
emojies. those have been fixed in here.
2022-03-23 18:37:55 +00:00
|
|
|
|
x += ew;
|
|
|
|
|
w -= ew;
|
2015-05-04 20:54:46 +01:00
|
|
|
|
}
|
2024-07-04 22:27:47 +01:00
|
|
|
|
if (utf8err && (!render || invalid_width < w)) {
|
|
|
|
|
if (render)
|
|
|
|
|
drw_text(drw, x, y, w, h, 0, invalid, invert);
|
|
|
|
|
x += invalid_width;
|
|
|
|
|
w -= invalid_width;
|
|
|
|
|
}
|
drw_text: improve both performance and correctness
this patch makes some non-trivial changes, which significantly improves
the performance of drawing large strings as well as fixes any issues
regarding the printing of the ellipsis when string gets truncated.
* performance:
before there were two O(n) loops, one which finds how long we can go
without changing font, and the second loop would (incorrectly) truncate
the string if it's too big.
this patch merges the overflow calculation into the first loop and exits
out when overflow is detected. when dumping lots of emojies into dmenu,
i see some noticeable startup time improvement:
before -> after
460ms -> 360ms
input latency when scrolling up/down is also noticeably better and can
be tested with the following:
for _ in $(seq 20); do
cat /dev/urandom | base64 | tr -d '\n' | head -c 1000000
echo
done | ./dmenu -l 10
* correctness:
the previous version would incorrectly assumed single byte chars and
would overwrite them with '.' , this caused a whole bunch of obvious
problems, including the ellipsis not getting rendered if then font
changed.
in addition to exiting out when we detect overflow, this patch also
keeps track of the last x-position where the ellipsis would fit. if we
detect overflow, we simply make a recursing call to drw_text() at the
ellipsis_x position and overwrite what was there.
so now the ellipsis will always be printed properly, regardless of
weather the font changes or if the string is single byte char or not.
the idea of rendering the ellipsis on top incase of overflow was
from Bakkeby <bakkeby@gmail.com>, thanks! however the original patch had
some issues incorrectly truncating the prompt (-p flag) and cutting off
emojies. those have been fixed in here.
2022-03-23 18:37:55 +00:00
|
|
|
|
if (render && overflow)
|
|
|
|
|
drw_text(drw, ellipsis_x, y, ellipsis_w, h, 0, "...", invert);
|
2015-05-04 20:54:46 +01:00
|
|
|
|
|
drw_text: improve both performance and correctness
this patch makes some non-trivial changes, which significantly improves
the performance of drawing large strings as well as fixes any issues
regarding the printing of the ellipsis when string gets truncated.
* performance:
before there were two O(n) loops, one which finds how long we can go
without changing font, and the second loop would (incorrectly) truncate
the string if it's too big.
this patch merges the overflow calculation into the first loop and exits
out when overflow is detected. when dumping lots of emojies into dmenu,
i see some noticeable startup time improvement:
before -> after
460ms -> 360ms
input latency when scrolling up/down is also noticeably better and can
be tested with the following:
for _ in $(seq 20); do
cat /dev/urandom | base64 | tr -d '\n' | head -c 1000000
echo
done | ./dmenu -l 10
* correctness:
the previous version would incorrectly assumed single byte chars and
would overwrite them with '.' , this caused a whole bunch of obvious
problems, including the ellipsis not getting rendered if then font
changed.
in addition to exiting out when we detect overflow, this patch also
keeps track of the last x-position where the ellipsis would fit. if we
detect overflow, we simply make a recursing call to drw_text() at the
ellipsis_x position and overwrite what was there.
so now the ellipsis will always be printed properly, regardless of
weather the font changes or if the string is single byte char or not.
the idea of rendering the ellipsis on top incase of overflow was
from Bakkeby <bakkeby@gmail.com>, thanks! however the original patch had
some issues incorrectly truncating the prompt (-p flag) and cutting off
emojies. those have been fixed in here.
2022-03-23 18:37:55 +00:00
|
|
|
|
if (!*text || overflow) {
|
2015-05-04 20:54:46 +01:00
|
|
|
|
break;
|
|
|
|
|
} else if (nextfont) {
|
|
|
|
|
charexists = 0;
|
2016-05-21 20:51:14 +01:00
|
|
|
|
usedfont = nextfont;
|
2015-05-04 20:54:46 +01:00
|
|
|
|
} else {
|
|
|
|
|
/* Regardless of whether or not a fallback font is found, the
|
2016-05-21 20:51:14 +01:00
|
|
|
|
* character must be drawn. */
|
2015-05-04 20:54:46 +01:00
|
|
|
|
charexists = 1;
|
|
|
|
|
|
2023-07-07 12:00:42 +01:00
|
|
|
|
hash = (unsigned int)utf8codepoint;
|
|
|
|
|
hash = ((hash >> 16) ^ hash) * 0x21F0AAAD;
|
|
|
|
|
hash = ((hash >> 15) ^ hash) * 0xD35A2D97;
|
|
|
|
|
h0 = ((hash >> 15) ^ hash) % LENGTH(nomatches);
|
|
|
|
|
h1 = (hash >> 17) % LENGTH(nomatches);
|
|
|
|
|
/* avoid expensive XftFontMatch call when we know we won't find a match */
|
|
|
|
|
if (nomatches[h0] == utf8codepoint || nomatches[h1] == utf8codepoint)
|
|
|
|
|
goto no_match;
|
2022-03-23 18:37:55 +00:00
|
|
|
|
|
2015-05-04 20:54:46 +01:00
|
|
|
|
fccharset = FcCharSetCreate();
|
|
|
|
|
FcCharSetAddChar(fccharset, utf8codepoint);
|
|
|
|
|
|
2016-05-21 20:51:14 +01:00
|
|
|
|
if (!drw->fonts->pattern) {
|
|
|
|
|
/* Refer to the comment in xfont_create for more information. */
|
2016-08-12 13:39:30 +01:00
|
|
|
|
die("the first font in the cache must be loaded from a font string.");
|
2015-05-04 20:54:46 +01:00
|
|
|
|
}
|
|
|
|
|
|
2016-05-21 20:51:14 +01:00
|
|
|
|
fcpattern = FcPatternDuplicate(drw->fonts->pattern);
|
2015-05-04 20:54:46 +01:00
|
|
|
|
FcPatternAddCharSet(fcpattern, FC_CHARSET, fccharset);
|
|
|
|
|
FcPatternAddBool(fcpattern, FC_SCALABLE, FcTrue);
|
|
|
|
|
|
|
|
|
|
FcConfigSubstitute(NULL, fcpattern, FcMatchPattern);
|
|
|
|
|
FcDefaultSubstitute(fcpattern);
|
|
|
|
|
match = XftFontMatch(drw->dpy, drw->screen, fcpattern, &result);
|
|
|
|
|
|
|
|
|
|
FcCharSetDestroy(fccharset);
|
|
|
|
|
FcPatternDestroy(fcpattern);
|
|
|
|
|
|
|
|
|
|
if (match) {
|
2016-05-21 20:51:14 +01:00
|
|
|
|
usedfont = xfont_create(drw, NULL, match);
|
|
|
|
|
if (usedfont && XftCharExists(drw->dpy, usedfont->xfont, utf8codepoint)) {
|
|
|
|
|
for (curfont = drw->fonts; curfont->next; curfont = curfont->next)
|
|
|
|
|
; /* NOP */
|
|
|
|
|
curfont->next = usedfont;
|
2015-05-04 20:54:46 +01:00
|
|
|
|
} else {
|
2016-05-21 20:51:14 +01:00
|
|
|
|
xfont_free(usedfont);
|
2023-07-07 12:00:42 +01:00
|
|
|
|
nomatches[nomatches[h0] ? h1 : h0] = utf8codepoint;
|
2022-03-23 18:37:55 +00:00
|
|
|
|
no_match:
|
2016-05-21 20:51:14 +01:00
|
|
|
|
usedfont = drw->fonts;
|
2015-05-04 20:54:46 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-10-20 21:56:57 +01:00
|
|
|
|
if (d)
|
2015-05-04 20:54:46 +01:00
|
|
|
|
XftDrawDestroy(d);
|
|
|
|
|
|
2016-05-21 20:51:14 +01:00
|
|
|
|
return x + (render ? w : 0);
|
2015-05-04 20:54:46 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
2015-09-27 22:56:02 +01:00
|
|
|
|
drw_map(Drw *drw, Window win, int x, int y, unsigned int w, unsigned int h)
|
|
|
|
|
{
|
2016-05-21 20:51:14 +01:00
|
|
|
|
if (!drw)
|
|
|
|
|
return;
|
|
|
|
|
|
2015-05-04 20:54:46 +01:00
|
|
|
|
XCopyArea(drw->dpy, drw->drawable, win, drw->gc, x, y, w, h, x, y);
|
|
|
|
|
XSync(drw->dpy, False);
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-21 20:51:14 +01:00
|
|
|
|
unsigned int
|
|
|
|
|
drw_fontset_getwidth(Drw *drw, const char *text)
|
2015-09-27 22:56:02 +01:00
|
|
|
|
{
|
2016-05-21 20:51:14 +01:00
|
|
|
|
if (!drw || !drw->fonts || !text)
|
|
|
|
|
return 0;
|
|
|
|
|
return drw_text(drw, 0, 0, 0, 0, 0, text, 0);
|
2015-05-04 20:54:46 +01:00
|
|
|
|
}
|
|
|
|
|
|
2022-03-23 20:00:00 +00:00
|
|
|
|
unsigned int
|
|
|
|
|
drw_fontset_getwidth_clamp(Drw *drw, const char *text, unsigned int n)
|
|
|
|
|
{
|
|
|
|
|
unsigned int tmp = 0;
|
|
|
|
|
if (drw && drw->fonts && text && n)
|
|
|
|
|
tmp = drw_text(drw, 0, 0, 0, 0, 0, text, n);
|
|
|
|
|
return MIN(n, tmp);
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-21 20:51:14 +01:00
|
|
|
|
void
|
|
|
|
|
drw_font_getexts(Fnt *font, const char *text, unsigned int len, unsigned int *w, unsigned int *h)
|
2015-09-27 22:56:02 +01:00
|
|
|
|
{
|
2016-05-21 20:51:14 +01:00
|
|
|
|
XGlyphInfo ext;
|
2015-05-04 20:54:46 +01:00
|
|
|
|
|
2016-05-21 20:51:14 +01:00
|
|
|
|
if (!font || !text)
|
|
|
|
|
return;
|
2015-10-20 21:53:55 +01:00
|
|
|
|
|
2016-05-21 20:51:14 +01:00
|
|
|
|
XftTextExtentsUtf8(font->dpy, font->xfont, (XftChar8 *)text, len, &ext);
|
|
|
|
|
if (w)
|
|
|
|
|
*w = ext.xOff;
|
|
|
|
|
if (h)
|
|
|
|
|
*h = font->h;
|
2015-05-04 20:54:46 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Cur *
|
2015-09-27 22:56:02 +01:00
|
|
|
|
drw_cur_create(Drw *drw, int shape)
|
|
|
|
|
{
|
2015-05-04 20:54:46 +01:00
|
|
|
|
Cur *cur;
|
|
|
|
|
|
2016-05-21 20:51:14 +01:00
|
|
|
|
if (!drw || !(cur = ecalloc(1, sizeof(Cur))))
|
|
|
|
|
return NULL;
|
|
|
|
|
|
2015-05-04 20:54:46 +01:00
|
|
|
|
cur->cursor = XCreateFontCursor(drw->dpy, shape);
|
2015-09-27 22:56:02 +01:00
|
|
|
|
|
2015-05-04 20:54:46 +01:00
|
|
|
|
return cur;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
2015-09-27 22:56:02 +01:00
|
|
|
|
drw_cur_free(Drw *drw, Cur *cursor)
|
|
|
|
|
{
|
2015-10-20 21:53:55 +01:00
|
|
|
|
if (!cursor)
|
2015-05-04 20:54:46 +01:00
|
|
|
|
return;
|
2016-05-21 20:51:14 +01:00
|
|
|
|
|
2015-05-04 20:54:46 +01:00
|
|
|
|
XFreeCursor(drw->dpy, cursor->cursor);
|
|
|
|
|
free(cursor);
|
|
|
|
|
}
|