From 38afcc6942dfb281c03792e024e9f96c7a4196c1 Mon Sep 17 00:00:00 2001 From: speedie Date: Sun, 22 Jan 2023 11:22:33 +0100 Subject: [PATCH] add rtl language support (im going to use this for dboard) --- host.mk | 4 ++-- spmenu.c | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++----- toggle.mk | 5 +++++ 3 files changed, 67 insertions(+), 8 deletions(-) diff --git a/host.mk b/host.mk index bce2622..79f50c4 100644 --- a/host.mk +++ b/host.mk @@ -34,5 +34,5 @@ XRENDERLIBS = -lXrender CPPFLAGS = -D_DEFAULT_SOURCE -D_BSD_SOURCE -D_XOPEN_SOURCE=700 -D_POSIX_C_SOURCE=200809L -DVERSION=\"$(VERSION)\" $(XINERAMAFLAGS) CFLAGS = -std=c99 -pedantic -Wall -Ofast $(INCS) $(CPPFLAGS) LDFLAGS = $(LIBS) -INCS = -I$(X11INC) -I$(FREETYPEINC) `pkg-config --cflags $(XFT_CONF) $(PANGO_CONF) $(PANGOXFT_CONF)` -LIBS = -L$(X11LIB) $(X11LIBS) $(XINERAMALIBS) $(FREETYPELIBS) $(XRENDERLIBS) -lm `pkg-config --libs $(XFT_CONF) $(PANGO_CONF) $(PANGOXFT_CONF)` +INCS = -I$(X11INC) -I$(FREETYPEINC) `pkg-config --cflags $(XFT_CONF) $(PANGO_CONF) $(PANGOXFT_CONF)` $(BDINC) +LIBS = -L$(X11LIB) $(X11LIBS) $(XINERAMALIBS) $(FREETYPELIBS) $(XRENDERLIBS) -lm `pkg-config --libs $(XFT_CONF) $(PANGO_CONF) $(PANGOXFT_CONF)` $(BDLIBS) diff --git a/spmenu.c b/spmenu.c index 5a23a75..a3ef66c 100644 --- a/spmenu.c +++ b/spmenu.c @@ -9,6 +9,17 @@ #include #include +#ifdef FRIBIDI +#define USERTL 1 +#else +#define USERTL 0 +#endif + +#if USERTL +#include +static char fribidi_text[BUFSIZ] = ""; +#endif + #include #include #include @@ -148,6 +159,9 @@ static void selectitem(const Arg *arg); static void quit(const Arg *arg); static void complete(const Arg *arg); +#if USERTL +static void apply_fribidi(char *str); +#endif #include "options.h" /* Include user configuration */ #include "keybinds.h" /* Include keybinds */ @@ -209,7 +223,7 @@ calcoffsets(void) break; } -static int +int max_textw(void) { int len = 0; @@ -231,7 +245,7 @@ cleanup(void) XCloseDisplay(dpy); } -static char * +char * cistrstr(const char *h, const char *n) { size_t i; @@ -286,7 +300,7 @@ drawhighlights(struct item *item, int x, int y, int maxw) } } -static int +int drawitem(struct item *item, int x, int y, int w) { if (item == sel) @@ -393,13 +407,19 @@ drawitem(struct item *item, int x, int y, int w) } buffer[wr] = '\0'; + + #if USERTL + apply_fribidi(buffer); + int r = drw_text(drw, x, y, w, bh, lrpad / 2, fribidi_text, 0, False); + #else int r = drw_text(drw, x, y, w, bh, lrpad / 2, buffer, 0, False); + #endif drawhighlights(item, x, y, w); return r; } -unsigned char +char sixd_to_8bit(int x) { return x == 0 ? 0 : 0x37 + 0x28 * x; @@ -429,9 +449,21 @@ drawmenu(void) if (passwd) { censort = ecalloc(1, sizeof(text)); memset(censort, '.', strlen(text)); + #if USERTL + apply_fribidi(censort); + drw_text(drw, x, 0, w, bh, lrpad / 2, fribidi_text, 0, True); + #else drw_text(drw, x, 0, w, bh, lrpad / 2, censort, 0, True); + #endif free(censort); - } else drw_text(drw, x, 0, w, bh, lrpad / 2, text, 0, True); + } else { + #if USERTL + apply_fribidi(text); + drw_text(drw, x, 0, w, bh, lrpad / 2, fribidi_text, 0, True); + #else + drw_text(drw, x, 0, w, bh, lrpad / 2, text, 0, True); + #endif + } curpos = TEXTW(text) - TEXTW(&text[cursor]); if ((curpos += lrpad / 2 - 1) < w) { @@ -692,7 +724,7 @@ insert(const char *str, ssize_t n) match(); } -static size_t +size_t nextrune(int inc) { ssize_t n; @@ -1291,6 +1323,28 @@ buttonpress(XEvent *e) } } +#if USERTL +void +apply_fribidi(char *str) +{ + FriBidiStrIndex len = strlen(str); + FriBidiChar logical[BUFSIZ]; + FriBidiChar visual[BUFSIZ]; + FriBidiParType base = FRIBIDI_PAR_ON; + FriBidiCharSet charset; + fribidi_boolean result; + + fribidi_text[0] = 0; + if (len>0) + { + charset = fribidi_parse_charset("UTF-8"); + len = fribidi_charset_to_unicode(charset, str, len, logical); + result = fribidi_log2vis(logical, len, &base, visual, NULL, NULL, NULL); + len = fribidi_unicode_to_charset(charset, visual, len, fribidi_text); + } +} +#endif + void pastesel(void) { diff --git a/toggle.mk b/toggle.mk index ef09042..a677da5 100644 --- a/toggle.mk +++ b/toggle.mk @@ -6,3 +6,8 @@ # Comment these lines if you don't need it. XINERAMALIBS = -lXinerama XINERAMAFLAGS = -DXINERAMA + +# Right to left language support +# Comment these lines if you don't need it. +BDLIBS = -lfribidi +BDINC = /usr/include/fribidi