aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas White <taw@bitwiz.me.uk>2019-08-25 22:30:08 +0200
committerThomas White <taw@bitwiz.me.uk>2019-08-25 22:30:08 +0200
commitbef894ac4fc76a7c163fba44a8127162a6c40d2b (patch)
tree1b035ee4b0e9e56f9ffff8134ca16341497f9f04
parent16daa2c43091242b142074405e0beeff16d2b133 (diff)
Fix *bold* etc
-rw-r--r--libstorycode/gtk/gtknarrativeview.c17
-rw-r--r--libstorycode/gtk/gtkslideview.c4
-rw-r--r--libstorycode/narrative_priv.h4
-rw-r--r--libstorycode/narrative_render_cairo.c26
4 files changed, 30 insertions, 21 deletions
diff --git a/libstorycode/gtk/gtknarrativeview.c b/libstorycode/gtk/gtknarrativeview.c
index 83d6d23..0fe2c59 100644
--- a/libstorycode/gtk/gtknarrativeview.c
+++ b/libstorycode/gtk/gtknarrativeview.c
@@ -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);
diff --git a/libstorycode/gtk/gtkslideview.c b/libstorycode/gtk/gtkslideview.c
index 9766572..0ec1c5f 100644
--- a/libstorycode/gtk/gtkslideview.c
+++ b/libstorycode/gtk/gtkslideview.c
@@ -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;
}
diff --git a/libstorycode/narrative_priv.h b/libstorycode/narrative_priv.h
index ec3ffb5..86aa612 100644
--- a/libstorycode/narrative_priv.h
+++ b/libstorycode/narrative_priv.h
@@ -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 */
diff --git a/libstorycode/narrative_render_cairo.c b/libstorycode/narrative_render_cairo.c
index 17f22aa..aaa05ee 100644
--- a/libstorycode/narrative_render_cairo.c
+++ b/libstorycode/narrative_render_cairo.c
@@ -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);