From 8da0dba45ba061816784d4c8915c5a53d970befb Mon Sep 17 00:00:00 2001 From: speedie Date: Wed, 10 May 2023 19:43:41 +0200 Subject: [PATCH] convert to ASCII, fixing a markup bug --- README.html | 15 ++++----------- README.md | 11 ++++------- libs/libdrw/drw.c | 44 ++++++++++++++++++++++++++++++++++++++++---- 3 files changed, 48 insertions(+), 22 deletions(-) diff --git a/README.html b/README.html index d7d47b4..3a5ca09 100644 --- a/README.html +++ b/README.html @@ -314,6 +314,9 @@ issues!

MD5() 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
  • Bugs

    - +

    None discovered yet! :)

    Scripts

    There’s a page dedicated to user scripts over on the diff --git a/README.md b/README.md index 7dfd778..3f9ed64 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/libs/libdrw/drw.c b/libs/libdrw/drw.c index 737ba60..d1f5c9f 100644 --- a/libs/libdrw/drw.c +++ b/libs/libdrw/drw.c @@ -14,6 +14,7 @@ #if USEPANGO #include #include +#include #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, "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, "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