convert to ASCII, fixing a markup bug

This commit is contained in:
speedie 2023-05-10 19:43:41 +02:00
parent 6dc84051aa
commit 8da0dba45b
3 changed files with 48 additions and 22 deletions

View file

@ -314,6 +314,9 @@ issues!</p>
MD5() is deprecated as of OpenSSL 3.0, but this would also make it very
easy to have LibreSSL compatibility.</li>
<li>Image support: Ability to display icons, similar to rofi</li>
<li>Image support: Images take a long time to load sometimes,
particularly when items are selected using the cursor so what we really
need is a way to skip over images after a set time limit.</li>
<li>Text drawing: Use cairo for text drawing over Xft.</li>
<li>Lines: Rofi-like newlines in the same entry
<ul>
@ -347,17 +350,7 @@ state.</li>
</ul></li>
</ul>
<h3 id="bugs">Bugs</h3>
<ul>
<li>Text drawing: Pango will sometimes spit out errors for invalid
markup. Silencing this would be a good idea.
<ul>
<li>Simply make sure characters are valid UTF-8 characters. Remove
anything else in the drw_text function.</li>
</ul></li>
<li>Image support: Images take a long time to load sometimes,
particularly when items are selected using the cursor so what we really
need is a way to skip over images after a set time limit.</li>
</ul>
<p>None discovered yet! :)</p>
<h2 id="scripts">Scripts</h2>
<p>Theres a page dedicated to user scripts <a
href="https://spmenu.speedie.site/index.php/User+scripts">over on the

View file

@ -122,6 +122,9 @@ Pull requests would be greatly appreciated for any of these issues!
is deprecated as of OpenSSL 3.0, but this would also make it very easy to
have LibreSSL compatibility.
- Image support: Ability to display icons, similar to rofi
- Image support: Images take a long time to load sometimes, particularly when
items are selected using the cursor so what we really need is a way to
skip over images after a set time limit.
- Text drawing: Use cairo for text drawing over Xft.
- Lines: Rofi-like newlines in the same entry
- Just need to `XMoveResizeWindow()` as well as `mh += bh` and `y += bh`
@ -146,13 +149,7 @@ like you will be unable to use spmenu in its current state.
### Bugs
- Text drawing: Pango will sometimes spit out errors for invalid markup.
Silencing this would be a good idea.
- Simply make sure characters are valid UTF-8 characters. Remove anything else
in the drw_text function.
- Image support: Images take a long time to load sometimes, particularly when
items are selected using the cursor so what we really need is a way to
skip over images after a set time limit.
None discovered yet! :)
## Scripts

View file

@ -14,6 +14,7 @@
#if USEPANGO
#include <pango/pango.h>
#include <pango/pangoxft.h>
#include <iconv.h>
#endif
#include "drw.h"
@ -65,6 +66,8 @@ static size_t utf8decode(const char *c, long *u, size_t clen) {
return len;
}
#else
static char *parse_utf(char *str, size_t clen);
#endif
Clr transcheme[3];
@ -308,6 +311,29 @@ int xerrordummy(Display *dpy, XErrorEvent *ee) {
return 0;
}
#if USEPANGO
char *parse_utf(char *str, size_t clen) {
char *ostr = str;
char *cnstr = calloc(clen + 1, sizeof(char));
char *cstr = cnstr;
size_t olen = clen;
iconv_t cd = iconv_open("UTF-8//IGNORE", "ASCII");
if (cd == (iconv_t) - 1) {
die("spmenu: iconv_open failed");
}
if (iconv(cd, &ostr, &olen, &cstr, &clen)) {
; // should be ok
}
iconv_close(cd);
return cnstr;
}
#endif
int drw_text(Drw *drw, int x, int y, unsigned int w, unsigned int h, unsigned int lpad, const char *text, int invert, Bool markup) {
XSetErrorHandler(xerrordummy);
@ -336,13 +362,14 @@ int drw_text(Drw *drw, int x, int y, unsigned int w, unsigned int h, unsigned in
len = strlen(text);
t = strdup(text);
t = parse_utf(t, len);
if (len) {
drw_font_getexts(drw->font, t, len, &ew, NULL, markup);
/* shorten text if necessary */
for (len = MIN(len, sizeof(buf) - 1); len && ew > w; drw_font_getexts(drw->font, t, len, &ew, NULL, markup))
len--;
if (len) {
memcpy(buf, t, len);
buf[len] = '\0';
@ -350,6 +377,9 @@ int drw_text(Drw *drw, int x, int y, unsigned int w, unsigned int h, unsigned in
for (i = len; i && i > len - 3; buf[--i] = '.')
; /* NOP */
if (!strstr(buf, "</"))
markup = 0;
if (render) {
ty = y + (h - drw->font->h) / 2;
if(markup)
@ -523,17 +553,23 @@ void drw_font_getexts(Fnt *font, const char *text, unsigned int len, unsigned in
if (!font || !text)
return;
char *t = strdup(text);
#if USEPANGO
t = parse_utf(t, len);
if (!strstr(t, "</"))
markup = 0;
PangoRectangle r;
if(markup)
pango_layout_set_markup(font->layout, text, len);
pango_layout_set_markup(font->layout, t, len);
else
pango_layout_set_text(font->layout, text, len);
pango_layout_set_text(font->layout, t, len);
pango_layout_get_extents(font->layout, 0, &r);
if(markup) // clear markup attributes
pango_layout_set_attributes(font->layout, NULL);
#else
XftTextExtentsUtf8(font->dpy, font->xfont, (XftChar8 *)text, len, &ext);
XftTextExtentsUtf8(font->dpy, font->xfont, (XftChar8 *)t, len, &ext);
#endif
if (w)
#if USEPANGO