Fix *bold* etc master
authorThomas White <taw@bitwiz.me.uk>
Sun, 25 Aug 2019 20:30:08 +0000 (22:30 +0200)
committerThomas White <taw@bitwiz.me.uk>
Sun, 25 Aug 2019 20:30:08 +0000 (22:30 +0200)
libstorycode/gtk/gtknarrativeview.c
libstorycode/gtk/gtkslideview.c
libstorycode/narrative_priv.h
libstorycode/narrative_render_cairo.c

index 83d6d23..0fe2c59 100644 (file)
@@ -505,10 +505,10 @@ static size_t pos_trail_to_offset(struct narrative_item *item, int offs, int tra
                return offs;
        }
 
-       char_offs = g_utf8_pointer_to_offset(item->text, item->text+offs);
+       char_offs = g_utf8_pointer_to_offset(item->layout_text, item->layout_text+offs);
        char_offs += trail;
-       ptr = g_utf8_offset_to_pointer(item->text, char_offs);
-       return ptr - item->text;
+       ptr = g_utf8_offset_to_pointer(item->layout_text, char_offs);
+       return ptr - item->layout_text;
 }
 
 
@@ -773,6 +773,8 @@ static void do_backspace(GtkNarrativeView *e, signed int dir)
 {
        struct edit_pos p1, p2;
        size_t o1, o2;
+       struct narrative_item *item1;
+       struct narrative_item *item2;
 
        if ( !positions_equal(e->sel_start, e->sel_end) ) {
 
@@ -789,8 +791,12 @@ static void do_backspace(GtkNarrativeView *e, signed int dir)
        }
 
        sort_positions(&p1, &p2);
-       o1 = pos_trail_to_offset(&e->n->items[p1.para], p1.pos, p1.trail);
-       o2 = pos_trail_to_offset(&e->n->items[p2.para], p2.pos, p2.trail);
+       item1 = &e->n->items[p1.para];
+       item2 = &e->n->items[p2.para];
+       o1 = pos_trail_to_offset(item1, p1.pos, p1.trail);
+       o2 = pos_trail_to_offset(item2, p2.pos, p2.trail);
+       o1 = layout_index_to_text(item1, o1);
+       o2 = layout_index_to_text(item2, o2);
        narrative_delete_block(e->n, p1.para, o1, p2.para, o2);
        e->cpos = p1;
        unset_selection(e);
@@ -833,6 +839,7 @@ static void insert_text(char *t, GtkNarrativeView *e)
        if ( narrative_item_is_text(e->n, e->cpos.para) ) {
 
                size_t off = pos_trail_to_offset(item, e->cpos.pos, e->cpos.trail);
+               off = layout_index_to_text(item, off);
 
                if ( strcmp(t, "\n") == 0 ) {
                        narrative_split_item(e->n, e->cpos.para, off);
index 9766572..0ec1c5f 100644 (file)
@@ -570,8 +570,8 @@ static int find_cursor(SlideItem *item, Stylesheet *stylesheet,
        pos->para = i-1;
 
        pango_layout_xy_to_index(item->paras[i-1].layout,
-                                pango_units_from_double(x),
-                                pango_units_from_double(y - top),
+                                pango_units_from_double(x - padl),
+                                pango_units_from_double(y - top - padt),
                                 &pos->pos, &pos->trail);
        return 0;
 }
index ec3ffb5..86aa612 100644 (file)
@@ -99,5 +99,9 @@ struct _narrative
        double space_b;
 };
 
+extern int text_index_to_layout(struct narrative_item *item, int idx);
+
+extern int layout_index_to_text(struct narrative_item *item, int idx);
+
 
 #endif /* NARRATIVE_PRIV_H */
index 17f22aa..aaa05ee 100644 (file)
@@ -118,8 +118,9 @@ static int add_range(struct narrative_item *item, int *max_chars_removed,
                if ( *add == NULL ) return 1;
        }
 
-       (*add)[*n_add].start = start;
-       (*add)[*n_add].end = end;
+       /* Indices NOT including the markers */
+       (*add)[*n_add].start = start+1;
+       (*add)[*n_add].end = end-1;
        (*add)[*n_add].type = type;
        (*n_add)++;
 
@@ -128,12 +129,12 @@ static int add_range(struct narrative_item *item, int *max_chars_removed,
 
 
 /* How many bytes were removed up to idx? */
-int index_before_removal(int *chars_removed, int n_chars_removed, int idx)
+int layout_index_to_text(struct narrative_item *item, int idx)
 {
        int i;
 
-       for ( i=0; i<n_chars_removed; i++ ) {
-               if ( chars_removed[i] > idx ) break;
+       for ( i=0; i<item->n_chars_removed; i++ ) {
+               if ( item->chars_removed[i] > idx ) break;
        }
 
        return idx + i;
@@ -141,12 +142,13 @@ int index_before_removal(int *chars_removed, int n_chars_removed, int idx)
 
 
 /* How many bytes were removed up to idx? */
-int index_with_removal(int *chars_removed, int n_chars_removed, int idx)
+int text_index_to_layout(struct narrative_item *item, int idx)
 {
        int i;
 
-       for ( i=0; i<n_chars_removed; i++ ) {
-               if ( chars_removed[i] > idx ) break;
+       for ( i=0; i<item->n_chars_removed; i++ ) {
+               assert(item->chars_removed[i] != idx);
+               if ( item->chars_removed[i] > idx ) break;
        }
 
        return idx - i;
@@ -229,13 +231,9 @@ static void process_tags(struct narrative_item *item, PangoAttrList *attrs)
 
                }
 
-               attr->start_index = index_with_removal(item->chars_removed,
-                                                      item->n_chars_removed,
-                                                      add[i].start);
+               attr->start_index = text_index_to_layout(item, add[i].start);
 
-               attr->end_index = index_with_removal(item->chars_removed,
-                                                    item->n_chars_removed,
-                                                    add[i].end) + 1;
+               attr->end_index = text_index_to_layout(item, add[i].end)+1;
 
                pango_attr_list_insert(attrs, attr);