[gspell/wip/entry] Entry utils: get list of words



commit 080e32cb49a3b9d5d78d5dbc1d09f7f58b05ba10
Author: Sébastien Wilmet <swilmet gnome org>
Date:   Fri Oct 28 12:30:46 2016 +0200

    Entry utils: get list of words

 docs/reference/Makefile.am  |    1 +
 gspell/Makefile.am          |    2 +
 gspell/gspell-entry-utils.c |  116 +++++++++++++++++++++++++++++++++++++++++++
 gspell/gspell-entry-utils.h |   47 +++++++++++++++++
 4 files changed, 166 insertions(+), 0 deletions(-)
---
diff --git a/docs/reference/Makefile.am b/docs/reference/Makefile.am
index 33b30b1..088c496 100644
--- a/docs/reference/Makefile.am
+++ b/docs/reference/Makefile.am
@@ -31,6 +31,7 @@ EXTRA_HFILES =                                                \
 IGNORE_HFILES =                                        \
        gspell.h                                \
        gspellregion.h                          \
+       gspell-entry-utils.h                    \
        gspell-init.h                           \
        gspell-inline-checker-text-buffer.h     \
        gspell-osx.h                            \
diff --git a/gspell/Makefile.am b/gspell/Makefile.am
index 3cfd0da..253698f 100644
--- a/gspell/Makefile.am
+++ b/gspell/Makefile.am
@@ -45,6 +45,7 @@ gspell_public_c_files =                               \
 gspell_private_headers =                       \
        gconstructor.h                          \
        gspellregion.h                          \
+       gspell-entry-utils.h                    \
        gspell-init.h                           \
        gspell-inline-checker-text-buffer.h     \
        gspell-text-iter.h                      \
@@ -52,6 +53,7 @@ gspell_private_headers =                      \
 
 gspell_private_c_files =                       \
        gspellregion.c                          \
+       gspell-entry-utils.c                    \
        gspell-init.c                           \
        gspell-inline-checker-text-buffer.c     \
        gspell-text-iter.c                      \
diff --git a/gspell/gspell-entry-utils.c b/gspell/gspell-entry-utils.c
new file mode 100644
index 0000000..b88192d
--- /dev/null
+++ b/gspell/gspell-entry-utils.c
@@ -0,0 +1,116 @@
+/*
+ * This file is part of gspell, a spell-checking library.
+ *
+ * Copyright 2016 - Sébastien Wilmet
+ *
+ * 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.1 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/>.
+ */
+
+#include "gspell-entry-utils.h"
+
+static GspellEntryWord *
+entry_word_new (void)
+{
+       return g_new0 (GspellEntryWord, 1);
+}
+
+void
+_gspell_entry_word_free (gpointer data)
+{
+       GspellEntryWord *word = data;
+
+       if (word != NULL)
+       {
+               g_free (word->word_str);
+               g_free (word);
+       }
+}
+
+/* List elements: GspellEntryWord*.
+ * Free with g_slist_free_full (words, _gspell_entry_word_free);
+ */
+GSList *
+_gspell_entry_utils_get_words (GtkEntry *entry)
+{
+       PangoLayout *layout;
+       const gchar *text;
+       const gchar *cur_text_pos;
+       const gchar *word_start_str = NULL;
+       gint word_start_pos;
+       const PangoLogAttr *attrs;
+       const PangoLogAttr *cur_attr;
+       gint n_attrs = 0;
+       gint attr_num;
+       GSList *list = NULL;
+
+       g_return_val_if_fail (GTK_IS_ENTRY (entry), NULL);
+
+       layout = gtk_entry_get_layout (entry);
+       text = gtk_entry_get_text (entry);
+       attrs = pango_layout_get_log_attrs_readonly (layout, &n_attrs);
+
+       for (attr_num = 0,
+            cur_attr = attrs,
+            cur_text_pos = text;
+
+            attr_num < n_attrs;
+
+            attr_num++,
+            cur_attr++,
+            cur_text_pos = g_utf8_find_next_char (cur_text_pos, NULL))
+       {
+               const gchar *word_end_str;
+               gint word_end_pos;
+               GspellEntryWord *word;
+
+               if (word_start_str == NULL)
+               {
+                       if (cur_attr->is_word_start)
+                       {
+                               word_start_str = cur_text_pos;
+                               word_start_pos = attr_num;
+                       }
+
+                       continue;
+               }
+
+               if (!cur_attr->is_word_end)
+               {
+                       continue;
+               }
+
+               word_end_str = cur_text_pos;
+               word_end_pos = attr_num;
+
+               word = entry_word_new ();
+               word->start = word_start_pos;
+               word->end = word_end_pos;
+               word->word_str = g_strndup (word_start_str, word_end_str - word_start_str);
+               list = g_slist_prepend (list, word);
+
+               /* Find next word start. */
+               word_start_str = NULL;
+
+               if (cur_text_pos == NULL &&
+                   attr_num != n_attrs - 1)
+               {
+                       g_warning ("%s(): problem in loop iteration.", G_STRFUNC);
+                       break;
+               }
+       }
+
+       return g_slist_reverse (list);
+}
+
+/* ex:set ts=8 noet: */
diff --git a/gspell/gspell-entry-utils.h b/gspell/gspell-entry-utils.h
new file mode 100644
index 0000000..b3c2768
--- /dev/null
+++ b/gspell/gspell-entry-utils.h
@@ -0,0 +1,47 @@
+/*
+ * This file is part of gspell, a spell-checking library.
+ *
+ * Copyright 2016 - Sébastien Wilmet
+ *
+ * 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.1 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/>.
+ */
+
+#ifndef GSPELL_ENTRY_UTILS_H
+#define GSPELL_ENTRY_UTILS_H
+
+#include <gtk/gtk.h>
+
+G_BEGIN_DECLS
+
+typedef struct _GspellEntryWord GspellEntryWord;
+struct _GspellEntryWord
+{
+       gchar *word_str;
+
+       /* UTF-8 character offsets in the GtkEntryBuffer. */
+       gint start;
+       gint end;
+};
+
+G_GNUC_INTERNAL
+void           _gspell_entry_word_free         (gpointer data);
+
+G_GNUC_INTERNAL
+GSList *       _gspell_entry_utils_get_words   (GtkEntry *entry);
+
+G_END_DECLS
+
+#endif /* GSPELL_ENTRY_UTILS_H */
+
+/* ex:set ts=8 noet: */


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