[libadwaita/wip/exalm/tabs: 81/88] Add adw-bidi




commit 6f27421e8208938a3bf7217a2a703d3fa7624f29
Author: Alexander Mikhaylenko <alexm gnome org>
Date:   Tue Jan 26 20:58:20 2021 +0500

    Add adw-bidi
    
    Copy a private replacement for pango_find_base_dir() from GTK4, since the
    pango function is deprecated. Add fribidi dependency for that.

 doc/meson.build        |  1 +
 src/adw-bidi-private.h | 21 +++++++++++++++
 src/adw-bidi.c         | 70 ++++++++++++++++++++++++++++++++++++++++++++++++++
 src/meson.build        |  2 ++
 4 files changed, 94 insertions(+)
---
diff --git a/doc/meson.build b/doc/meson.build
index bf8ab5f..468e0af 100644
--- a/doc/meson.build
+++ b/doc/meson.build
@@ -7,6 +7,7 @@ private_headers = [
     'gtkprogresstrackerprivate.h',
     'gtk-window-private.h',
     'adw-animation-private.h',
+    'adw-bidi-private.h',
     'adw-enums.h',
     'adw-enums-private.h',
     'adw-enum-value-object-private.h',
diff --git a/src/adw-bidi-private.h b/src/adw-bidi-private.h
new file mode 100644
index 0000000..667731d
--- /dev/null
+++ b/src/adw-bidi-private.h
@@ -0,0 +1,21 @@
+/*
+ * Copyright (C) 2021 Purism SPC
+ *
+ * SPDX-License-Identifier: LGPL-2.1+
+ */
+
+#pragma once
+
+#if !defined(_ADWAITA_INSIDE) && !defined(ADWAITA_COMPILATION)
+#error "Only <adwaita.h> can be included directly."
+#endif
+
+#include <glib.h>
+#include <pango/pango.h>
+
+G_BEGIN_DECLS
+
+PangoDirection adw_find_base_dir (const char *text,
+                                  int         length);
+
+G_END_DECLS
diff --git a/src/adw-bidi.c b/src/adw-bidi.c
new file mode 100644
index 0000000..76d50be
--- /dev/null
+++ b/src/adw-bidi.c
@@ -0,0 +1,70 @@
+/* GDK - The GIMP Drawing Kit
+ * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+/*
+ * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
+ * file for a list of people on the GTK+ Team.  See the ChangeLog
+ * files for a list of changes.  These files are distributed with
+ * GTK+ at ftp://ftp.gtk.org/pub/gtk/..
+ */
+
+/* Bits taken from GTK 4.0.2 and tweaked to be used by libadwaita. */
+
+#include "adw-bidi-private.h"
+
+#include <fribidi.h>
+
+static PangoDirection
+adw_unichar_direction (gunichar ch)
+{
+  FriBidiCharType fribidi_ch_type;
+
+  G_STATIC_ASSERT (sizeof (FriBidiChar) == sizeof (gunichar));
+
+  fribidi_ch_type = fribidi_get_bidi_type (ch);
+
+  if (!FRIBIDI_IS_STRONG (fribidi_ch_type))
+    return PANGO_DIRECTION_NEUTRAL;
+  else if (FRIBIDI_IS_RTL (fribidi_ch_type))
+    return PANGO_DIRECTION_RTL;
+  else
+    return PANGO_DIRECTION_LTR;
+}
+
+PangoDirection
+adw_find_base_dir (const char *text,
+                   int         length)
+{
+  PangoDirection dir = PANGO_DIRECTION_NEUTRAL;
+  const char *p;
+
+  g_return_val_if_fail (text != NULL || length == 0, PANGO_DIRECTION_NEUTRAL);
+
+  p = text;
+  while ((length < 0 || p < text + length) && *p) {
+    gunichar wc = g_utf8_get_char (p);
+
+    dir = adw_unichar_direction (wc);
+
+    if (dir != PANGO_DIRECTION_NEUTRAL)
+      break;
+
+    p = g_utf8_next_char (p);
+  }
+
+  return dir;
+}
diff --git a/src/meson.build b/src/meson.build
index eed6438..08eced1 100644
--- a/src/meson.build
+++ b/src/meson.build
@@ -117,6 +117,7 @@ src_sources = [
   'adw-animation.c',
   'adw-application-window.c',
   'adw-avatar.c',
+  'adw-bidi.c',
   'adw-bin.c',
   'adw-carousel.c',
   'adw-carousel-indicator-dots.c',
@@ -178,6 +179,7 @@ gtk_dep = dependency('gtk4')
 libadwaita_deps = [
   dependency('glib-2.0', version: glib_min_version),
   dependency('gmodule-2.0', version: glib_min_version),
+  dependency('fribidi'),
   gio_dep,
   gtk_dep,
   cc.find_library('m', required: false),


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]