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 MD5() is deprecated as of OpenSSL 3.0, but this would also make it very
easy to have LibreSSL compatibility.</li> easy to have LibreSSL compatibility.</li>
<li>Image support: Ability to display icons, similar to rofi</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>Text drawing: Use cairo for text drawing over Xft.</li>
<li>Lines: Rofi-like newlines in the same entry <li>Lines: Rofi-like newlines in the same entry
<ul> <ul>
@ -347,17 +350,7 @@ state.</li>
</ul></li> </ul></li>
</ul> </ul>
<h3 id="bugs">Bugs</h3> <h3 id="bugs">Bugs</h3>
<ul> <p>None discovered yet! :)</p>
<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>
<h2 id="scripts">Scripts</h2> <h2 id="scripts">Scripts</h2>
<p>Theres a page dedicated to user scripts <a <p>Theres a page dedicated to user scripts <a
href="https://spmenu.speedie.site/index.php/User+scripts">over on the 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 is deprecated as of OpenSSL 3.0, but this would also make it very easy to
have LibreSSL compatibility. have LibreSSL compatibility.
- Image support: Ability to display icons, similar to rofi - 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. - Text drawing: Use cairo for text drawing over Xft.
- Lines: Rofi-like newlines in the same entry - Lines: Rofi-like newlines in the same entry
- Just need to `XMoveResizeWindow()` as well as `mh += bh` and `y += bh` - 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 ### Bugs
- Text drawing: Pango will sometimes spit out errors for invalid markup. None discovered yet! :)
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.
## Scripts ## Scripts

View file

@ -14,6 +14,7 @@
#if USEPANGO #if USEPANGO
#include <pango/pango.h> #include <pango/pango.h>
#include <pango/pangoxft.h> #include <pango/pangoxft.h>
#include <iconv.h>
#endif #endif
#include "drw.h" #include "drw.h"
@ -65,6 +66,8 @@ static size_t utf8decode(const char *c, long *u, size_t clen) {
return len; return len;
} }
#else
static char *parse_utf(char *str, size_t clen);
#endif #endif
Clr transcheme[3]; Clr transcheme[3];
@ -308,6 +311,29 @@ int xerrordummy(Display *dpy, XErrorEvent *ee) {
return 0; 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) { 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); 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); len = strlen(text);
t = strdup(text); t = strdup(text);
t = parse_utf(t, len);
if (len) { if (len) {
drw_font_getexts(drw->font, t, len, &ew, NULL, markup); drw_font_getexts(drw->font, t, len, &ew, NULL, markup);
/* shorten text if necessary */ /* shorten text if necessary */
for (len = MIN(len, sizeof(buf) - 1); len && ew > w; drw_font_getexts(drw->font, t, len, &ew, NULL, markup)) for (len = MIN(len, sizeof(buf) - 1); len && ew > w; drw_font_getexts(drw->font, t, len, &ew, NULL, markup))
len--; len--;
if (len) { if (len) {
memcpy(buf, t, len); memcpy(buf, t, len);
buf[len] = '\0'; 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] = '.') for (i = len; i && i > len - 3; buf[--i] = '.')
; /* NOP */ ; /* NOP */
if (!strstr(buf, "</"))
markup = 0;
if (render) { if (render) {
ty = y + (h - drw->font->h) / 2; ty = y + (h - drw->font->h) / 2;
if(markup) if(markup)
@ -523,17 +553,23 @@ void drw_font_getexts(Fnt *font, const char *text, unsigned int len, unsigned in
if (!font || !text) if (!font || !text)
return; return;
char *t = strdup(text);
#if USEPANGO #if USEPANGO
t = parse_utf(t, len);
if (!strstr(t, "</"))
markup = 0;
PangoRectangle r; PangoRectangle r;
if(markup) if(markup)
pango_layout_set_markup(font->layout, text, len); pango_layout_set_markup(font->layout, t, len);
else 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); pango_layout_get_extents(font->layout, 0, &r);
if(markup) // clear markup attributes if(markup) // clear markup attributes
pango_layout_set_attributes(font->layout, NULL); pango_layout_set_attributes(font->layout, NULL);
#else #else
XftTextExtentsUtf8(font->dpy, font->xfont, (XftChar8 *)text, len, &ext); XftTextExtentsUtf8(font->dpy, font->xfont, (XftChar8 *)t, len, &ext);
#endif #endif
if (w) if (w)
#if USEPANGO #if USEPANGO