aboutsummaryrefslogtreecommitdiff
path: root/wayland/imv/patches/0001-support-libgrapheme.patch
blob: 693a89211dee5a3f4467f3bc8d79120bb9b9ea09 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
From a83304d4d673aae6efed51da1986bd7315a4d642 Mon Sep 17 00:00:00 2001
From: Cem Keylan <cem@ckyln.com>
Date: Tue, 7 Dec 2021 14:40:57 +0100
Subject: [PATCH] Add support for libgrapheme as an icu replacement

---
 meson.build       | 11 ++++++++++-
 meson_options.txt |  8 ++++++++
 src/console.c     | 28 ++++++++++++++++++++++++++++
 3 files changed, 46 insertions(+), 1 deletion(-)

diff --git a/meson.build b/meson.build
index 7cf64b5..26ee0a1 100644
--- a/meson.build
+++ b/meson.build
@@ -38,6 +38,15 @@ else
   target_single_ws = false
 endif
 
+_unicode = get_option('unicode')
+if _unicode == 'icu'
+  unicode_lib = dependency('icu-io')
+  add_project_arguments('-DIMV_USE_ICU', language: 'c')
+elif _unicode == 'grapheme'
+  unicode_lib = cc.find_library('grapheme')
+  add_project_arguments('-DIMV_USE_GRAPHEME', language: 'c')
+endif
+
 gl_dep = dependency('gl', required: false)
 if not gl_dep.found()
   # libglvnd fallback for pure-wayland systems
@@ -49,7 +58,7 @@ deps_for_imv = [
   gl_dep,
   dependency('threads'),
   dependency('xkbcommon'),
-  dependency('icu-io'),
+  unicode_lib,
   dependency('inih', fallback : ['inih', 'inih_dep']),
   m_dep,
 ]
diff --git a/meson_options.txt b/meson_options.txt
index 389b7fd..c13ef7a 100644
--- a/meson_options.txt
+++ b/meson_options.txt
@@ -8,6 +8,14 @@ option('windows',
   description : 'window system to use'
 )
 
+# Unicode backend - default is ICU
+option('unicode',
+  type: 'combo',
+  value: 'icu',
+  choices : ['icu', 'grapheme'],
+  description : 'unicode library to use'
+)
+
 option('test',
   type : 'feature',
   description : 'enable tests'
diff --git a/src/console.c b/src/console.c
index 073274f..323383f 100644
--- a/src/console.c
+++ b/src/console.c
@@ -6,8 +6,15 @@
 #include <ctype.h>
 #include <stdlib.h>
 #include <string.h>
+
+#ifdef IMV_USE_ICU
 #include <unicode/utext.h>
 #include <unicode/ubrk.h>
+#endif
+
+#ifdef IMV_USE_GRAPHEME
+#include <grapheme.h>
+#endif
 
 struct imv_console {
   char *buffer;
@@ -25,6 +32,7 @@ struct imv_console {
 /* Iterates forwards over characters in a UTF-8 string */
 static size_t next_char(char *buffer, size_t position)
 {
+  #if defined(IMV_USE_ICU)
   size_t result = position;
   UErrorCode status = U_ZERO_ERROR;
   UText *ut = utext_openUTF8(NULL, buffer, -1, &status);
@@ -42,11 +50,19 @@ static size_t next_char(char *buffer, size_t position)
   utext_close(ut);
   assert(U_SUCCESS(status));
   return result;
+  #elif defined(IMV_USE_GRAPHEME)
+  if (buffer[position] != 0) {
+    return position + grapheme_bytelen(buffer + position);
+  } else {
+    return position;
+  }
+  #endif
 }
 
 /* Iterates backwards over characters in a UTF-8 string */
 static size_t prev_char(char *buffer, size_t position)
 {
+  #if defined(IMV_USE_ICU)
   size_t result = position;
   UErrorCode status = U_ZERO_ERROR;
   UText *ut = utext_openUTF8(NULL, buffer, -1, &status);
@@ -64,6 +80,18 @@ static size_t prev_char(char *buffer, size_t position)
   utext_close(ut);
   assert(U_SUCCESS(status));
   return result;
+
+  #elif defined(IMV_USE_GRAPHEME)
+  size_t result = 0;
+  size_t step;
+  do {
+    step = grapheme_bytelen(buffer + result);
+    if (result + step >= position)
+      break;
+    result += step;
+  } while (step > 0);
+  return result;
+  #endif
 }
 
 static void add_to_history(struct list *history, const char *line)
-- 
2.32.0