From 45ec77973be85627589e540d55e69de5847ad7da Mon Sep 17 00:00:00 2001 From: Dav999 Date: Wed, 3 Jan 2024 18:20:25 +0100 Subject: [PATCH] Implement bidi_should_transform() This now returns true if any of the characters in the text belong to the Arabic or Hebrew alphabet, or are one of the Unicode directional formatting characters. This is just so the bidi machinery doesn't have to run 100% of the time for 100% of the languages. I will also make it so the Arabic language pack, as well as custom levels, have an RTL attribute that always enables bidi (and does things like right-alignment in textboxes and other design-flipping) --- desktop_version/src/FontBidi.cpp | 35 ++++++++++++++++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) diff --git a/desktop_version/src/FontBidi.cpp b/desktop_version/src/FontBidi.cpp index dce282d5..b56a013d 100644 --- a/desktop_version/src/FontBidi.cpp +++ b/desktop_version/src/FontBidi.cpp @@ -265,8 +265,39 @@ void bidi_destroy(void) bool bidi_should_transform(const char* text) { - // TODO - return true; + /* Just as an optimization, only run the whole bidi machinery if the + * language is actually an RTL one, _or_ if an RTL character is found. */ + + const char* text_ptr = text; + uint32_t ch; + while ((ch = UTF8_next(&text_ptr))) + { + // The standard Hebrew and Arabic blocks + if (ch >= 0x590 && ch <= 0x77F) return true; + + // Extended Arabic B and A + if (ch >= 0x870 && ch <= 0x8FF) return true; + + // LEFT-TO-RIGHT MARK and RIGHT-TO-LEFT MARK + if (ch == 0x200E || ch == 0x200F) return true; + + // Some other directional formatting: LRE, RLE, PDF, RLO, LRO + if (ch >= 0x202A && ch <= 0x202E) return true; + + // The more recent isolates: LRI, RLI, FSI, PDI + if (ch >= 0x2066 && ch <= 0x2069) return true; + + // Hebrew presentation forms + if (ch >= 0xFB1D && ch <= 0xFB4F) return true; + + // Arabic presentation forms A + if (ch >= 0xFB50 && ch <= 0xFDFF) return true; + + // Arabic presentation forms B + if (ch >= 0xFE70 && ch <= 0xFEFE) return true; + } + + return false; } const char* bidi_transform(const char* text)