[gnumeric] Fix byte offset to character offset alignment



commit 043e2e62a17acac37a7c85dbe186977b9c5d183a
Author: Andreas J Guelzow <aguelzow pyrshep ca>
Date:   Thu Jun 2 11:05:18 2011 -0600

    Fix byte offset to character offset alignment
    
    2011-06-02  Andreas J. Guelzow <aguelzow pyrshep ca>
    
    	* src/gui-util.h (gnm_load_pango_attributes_into_buffer): add
    	argument, change all callers
    	* src/gui-util.c (gnm_load_pango_attributes_into_buffer): add
    	argument, correctly align pango offsets with buffer char-offsets
    	(gnm_load_pango_byte_to_char): new

 ChangeLog                        |    8 +++++++
 src/gui-util.c                   |   39 +++++++++++++++++++++++++++++++++----
 src/gui-util.h                   |    3 +-
 src/sheet-control-gui.c          |    3 +-
 src/widgets/gnumeric-text-view.c |    2 +-
 5 files changed, 47 insertions(+), 8 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index 6a51db0..e2a6b78 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,11 @@
+2011-06-02  Andreas J. Guelzow <aguelzow pyrshep ca>
+
+	* src/gui-util.h (gnm_load_pango_attributes_into_buffer): add
+	argument, change all callers
+	* src/gui-util.c (gnm_load_pango_attributes_into_buffer): add
+	argument, correctly align pango offsets with buffer char-offsets
+	(gnm_load_pango_byte_to_char): new
+
 2011-05-30  Morten Welinder  <terra gnome org>
 
 	* src/gui-clipboard.c (gnm_x_claim_clipboard): Rename from
diff --git a/src/gui-util.c b/src/gui-util.c
index a1bdc09..17790e8 100644
--- a/src/gui-util.c
+++ b/src/gui-util.c
@@ -766,16 +766,34 @@ gnm_create_std_tags_for_buffer (GtkTextBuffer *buffer)
 }
 
 
+static gint
+gnm_load_pango_byte_to_char (gchar const *str, gint byte)
+{
+	if (byte >= strlen (str))
+		return g_utf8_strlen (str, -1);
+	return g_utf8_pointer_to_offset (str, 
+					 g_utf8_prev_char (str + byte + 1));
+}
+
 void
-gnm_load_pango_attributes_into_buffer (PangoAttrList  *markup, GtkTextBuffer *buffer)
+gnm_load_pango_attributes_into_buffer (PangoAttrList  *markup, GtkTextBuffer *buffer, gchar const *str)
 {
 	PangoAttrIterator * iter;
 	PangoAttrList  *copied_markup;
 	PangoAttrList  *our_markup;
+	char *str_retrieved = NULL;
 
 	if (markup == NULL)
 		return;
 
+	if (str == NULL) {
+		GtkTextIter start, end;
+		gtk_text_buffer_get_start_iter (buffer, &start);
+		gtk_text_buffer_get_end_iter (buffer, &end);
+		str = str_retrieved = gtk_text_buffer_get_slice 
+			(buffer, &start, &end, TRUE);
+	}
+
 /* For some styles we create named tags. The names are taken from the Pango enums */
 
 	copied_markup = pango_attr_list_copy (markup);
@@ -795,8 +813,13 @@ gnm_load_pango_attributes_into_buffer (PangoAttrList  *markup, GtkTextBuffer *bu
 				char const *name;
 
 				pango_attr_iterator_range (iter, &start, &end);
-				gtk_text_buffer_get_iter_at_offset (buffer, &start_iter, start);
-				gtk_text_buffer_get_iter_at_offset (buffer, &end_iter, end);
+				start = gnm_load_pango_byte_to_char 
+					(str, start);
+				end = gnm_load_pango_byte_to_char (str, end);
+				gtk_text_buffer_get_iter_at_offset 
+					(buffer, &start_iter, start);
+				gtk_text_buffer_get_iter_at_offset 
+					(buffer, &end_iter, end);
 
 				for (ptr = attr; ptr != NULL; ptr = ptr->next) {
 					PangoAttribute *attribute = ptr->data;
@@ -920,8 +943,13 @@ gnm_load_pango_attributes_into_buffer (PangoAttrList  *markup, GtkTextBuffer *bu
 					}
 				}
 				pango_attr_iterator_range (iter, &start, &end);
-				gtk_text_buffer_get_iter_at_offset (buffer, &start_iter, start);
-				gtk_text_buffer_get_iter_at_offset (buffer, &end_iter, end);
+				start = gnm_load_pango_byte_to_char 
+					(str, start);
+				end = gnm_load_pango_byte_to_char (str, end);
+				gtk_text_buffer_get_iter_at_offset 
+					(buffer, &start_iter, start);
+				gtk_text_buffer_get_iter_at_offset 
+					(buffer, &end_iter, end);
 				gtk_text_buffer_apply_tag (buffer, tag, &start_iter, &end_iter);
 				go_slist_free_custom (attr, (GFreeFunc)pango_attribute_destroy);
 			}
@@ -929,6 +957,7 @@ gnm_load_pango_attributes_into_buffer (PangoAttrList  *markup, GtkTextBuffer *bu
 		pango_attr_iterator_destroy (iter);
 		pango_attr_list_unref (our_markup);
 	}
+	g_free (str_retrieved);
 }
 
 #define gnmstoretexttagattrinpangoint(nameset, name, gnm_pango_attr_new)  \
diff --git a/src/gui-util.h b/src/gui-util.h
index 1fb803a..705befe 100644
--- a/src/gui-util.h
+++ b/src/gui-util.h
@@ -76,7 +76,8 @@ char *gnumeric_textbuffer_get_text (GtkTextBuffer *buf);
 char *gnumeric_textview_get_text (GtkTextView *text_view);
 void  gnumeric_textview_set_text (GtkTextView *text_view, char const *txt);
 void  gnm_load_pango_attributes_into_buffer (PangoAttrList  *markup,
-					     GtkTextBuffer *buffer);
+					     GtkTextBuffer *buffer,
+					     gchar const *str);
 PangoAttrList *gnm_get_pango_attributes_from_buffer (GtkTextBuffer *buffer);
 void gnm_create_std_tags_for_buffer (GtkTextBuffer *buffer);
 
diff --git a/src/sheet-control-gui.c b/src/sheet-control-gui.c
index 32e4cf7..6ece4ad 100644
--- a/src/sheet-control-gui.c
+++ b/src/sheet-control-gui.c
@@ -2935,8 +2935,9 @@ scg_comment_display (SheetControlGUI *scg, GnmComment *cc,
 
 		if (comment_text != NULL) {
 			gtk_text_buffer_set_text (buffer, comment_text, -1);
+			gnm_load_pango_attributes_into_buffer 
+				(comment_markup, buffer, comment_text);
 			g_free (comment_text);
-			gnm_load_pango_attributes_into_buffer (comment_markup, buffer);
 		}
 
 		comment_author = cell_comment_author_get (cc);
diff --git a/src/widgets/gnumeric-text-view.c b/src/widgets/gnumeric-text-view.c
index 3efed3e..b8542b2 100644
--- a/src/widgets/gnumeric-text-view.c
+++ b/src/widgets/gnumeric-text-view.c
@@ -298,7 +298,7 @@ gtv_set_property (GObject      *object,
 		break;
 	case PROP_ATTR:
 		gnm_load_pango_attributes_into_buffer (g_value_get_boxed (value),
-						       gtv->buffer);
+						       gtv->buffer, NULL);
 	break;
 	default:
 		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);



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