summaryrefslogtreecommitdiff
path: root/src/fl_utf8.cxx
diff options
context:
space:
mode:
authorManoloFLTK <41016272+ManoloFLTK@users.noreply.github.com>2026-01-19 16:34:50 +0100
committerManoloFLTK <41016272+ManoloFLTK@users.noreply.github.com>2026-01-19 16:34:50 +0100
commit61e75e4a12f199368f28dd274d83fc4ecd9cfec3 (patch)
tree94c71a1dffe9c6d9ac4a51dbec84561d38828b2c /src/fl_utf8.cxx
parent59d93554b3aff9614276fec99792807d77b24ee5 (diff)
All platforms use same code to remove context-dependent codepoints from text input.
This commit introduces function fl_utf8_remove_context_dependent() that removes from an UTF-8 string its context-dependent codepoints. Platforms macOS, Wayland and X11 call this function to process UTF-8 text received from a character palette as input to FLTK text. This makes sure FLTK text-editing widgets process textual input equally and consistently across platforms, especially emojis entered via a palette. Platform Windows creates a series of separate system events to input an emoji via the character palette. For this reason, function fl_utf8_remove_context_dependent() is not used by this platform which does internally the same filtering of context- dependent codepoints.
Diffstat (limited to 'src/fl_utf8.cxx')
-rw-r--r--src/fl_utf8.cxx29
1 files changed, 28 insertions, 1 deletions
diff --git a/src/fl_utf8.cxx b/src/fl_utf8.cxx
index 7cc53f13b..05e45ded3 100644
--- a/src/fl_utf8.cxx
+++ b/src/fl_utf8.cxx
@@ -3,7 +3,7 @@
//
// Author: Jean-Marc Lienher ( http://oksid.ch )
// Copyright 2000-2010 by O'ksi'D.
-// Copyright 2016-2022 by Bill Spitzak and others.
+// Copyright 2016-2026 by Bill Spitzak and others.
//
// This library is free software. Distribution and use rights are outlined in
// the file "COPYING" which should have been included with this file. If this
@@ -1630,3 +1630,30 @@ unsigned fl_utf8from_mb(char* dst, unsigned dstlen, const char* src, unsigned sr
}
/** @} */
+
+#ifndef FL_DOXYGEN
+
+/* This function removes from an UTF-8 string its context-dependent codepoints
+ when there are any, and returns the length of the possibly shortened string.
+ */
+int fl_utf8_remove_context_dependent(char *text, int len) {
+ if (len > 1 && fl_utf_nb_char((const uchar*)text, len) > 1) {
+ // Some emojis are expressed by a series of Unicode points
+ char *p = text, *end = text + len;
+ while (p < end) { // loop over all unicode points of the series
+ int l_point;
+ unsigned u = fl_utf8decode(p, end, &l_point); // extract one such unicode point
+ if ((u >= 0xFE00 && u <= 0xFE0F) // variation selectors
+ || u == 0x200D // zero-width joiner
+ || (u >= 0x1F3FB && u <= 0x1F3FF) // EMOJI MODIFIERS FITZPATRICK TYPE
+ ) { // remove context-dependent unicode points
+ memmove((void*)p, p + l_point, (end - (p+l_point)) + 1);
+ end -= l_point;
+ len -= l_point;
+ } else p += l_point; // keep other unicode points
+ }
+ }
+ return len;
+}
+
+#endif // ! FL_DOXYGEN