From 9e0b5b1886f764c50906af66533f74c7654e59e1 Mon Sep 17 00:00:00 2001 From: Carlos Garnacho Date: Mon, 2 Jan 2023 23:11:37 +0100 Subject: [PATCH] clutter: Avoid string translation to find text direction Use Pango and Harfbuzz for the task, getting the default language, then its scripts, then their directions. We pick the first valid horizontal direction, resorting to LTR as a fallback. Related: https://gitlab.gnome.org/GNOME/gtk/-/merge_requests/5385 Part-of: --- clutter/clutter/clutter-main.c | 35 +++++++++++++++++++++------------- clutter/meson.build | 1 + meson.build | 2 ++ po/POTFILES.in | 1 - 4 files changed, 25 insertions(+), 14 deletions(-) diff --git a/clutter/clutter/clutter-main.c b/clutter/clutter/clutter-main.c index be9f74441..e758e170a 100644 --- a/clutter/clutter/clutter-main.c +++ b/clutter/clutter/clutter-main.c @@ -51,6 +51,7 @@ #include #include +#include #include "clutter-actor-private.h" #include "clutter-backend-private.h" @@ -221,20 +222,28 @@ clutter_get_text_direction (void) } else { - /* - * Translate to default:RTL if you want your widgets - * to be RTL, otherwise translate to default:LTR. - * Do *not* translate it to "predefinito:LTR", if it - * it isn't default:LTR or default:RTL it will not work - */ - const char *e = _("default:LTR"); + PangoLanguage *language; + const PangoScript *scripts; + int n_scripts, i; - if (strcmp (e, "default:RTL") == 0) - dir = CLUTTER_TEXT_DIRECTION_RTL; - else if (strcmp (e, "default:LTR") == 0) - dir = CLUTTER_TEXT_DIRECTION_LTR; - else - g_warning ("Whoever translated default:LTR did so wrongly."); + language = pango_language_get_default (); + scripts = pango_language_get_scripts (language, &n_scripts); + + for (i = 0; i < n_scripts; i++) + { + hb_script_t script; + hb_direction_t text_dir; + + script = hb_glib_script_to_script ((GUnicodeScript) scripts[i]); + text_dir = hb_script_get_horizontal_direction (script); + + if (text_dir == HB_DIRECTION_LTR) + dir = CLUTTER_TEXT_DIRECTION_LTR; + else if (text_dir == HB_DIRECTION_RTL) + dir = CLUTTER_TEXT_DIRECTION_RTL; + else + continue; + } } CLUTTER_NOTE (MISC, "Text direction: %s", diff --git a/clutter/meson.build b/clutter/meson.build index ac6db2fd2..918153ee4 100644 --- a/clutter/meson.build +++ b/clutter/meson.build @@ -36,6 +36,7 @@ clutter_pkg_deps = [ gio_dep, json_glib_dep, pango_dep, + harfbuzz_dep, ] clutter_pkg_private_deps = [ diff --git a/meson.build b/meson.build index af716b750..227725c94 100644 --- a/meson.build +++ b/meson.build @@ -38,6 +38,7 @@ libstartup_notification_req = '>= 0.7' libcanberra_req = '>= 0.26' libwacom_req = '>= 0.13' atk_req = '>= 2.5.3' +harfbuzz_req = '>= 2.6' # optional version requirements udev_req = '>= 228' @@ -128,6 +129,7 @@ atk_dep = dependency('atk', version: atk_req) dbus_dep = dependency('dbus-1') colord_dep = dependency('colord', version: colord_req) lcms2_dep = dependency('lcms2', version: lcms2_req) +harfbuzz_dep = dependency('harfbuzz', version: harfbuzz_req) have_wayland = get_option('wayland') # For now always require X11 support diff --git a/po/POTFILES.in b/po/POTFILES.in index d671ba4ab..ebafa966e 100644 --- a/po/POTFILES.in +++ b/po/POTFILES.in @@ -11,7 +11,6 @@ src/backends/meta-input-settings.c src/backends/meta-monitor.c src/backends/meta-monitor-manager.c src/backends/x11/meta-clutter-backend-x11.c -src/clutter/clutter/clutter-main.c src/compositor/compositor.c src/compositor/meta-background.c src/core/bell.c