From 309ca67853b35f3f652686dbb52ca5182b81d0a7 Mon Sep 17 00:00:00 2001 From: Thomas White Date: Fri, 4 Oct 2019 21:34:41 +0200 Subject: Implement which_run() --- libstorycode/gtk/gtknarrativeview.c | 16 +++++----------- libstorycode/narrative.c | 17 +++++++++++++++++ libstorycode/narrative_priv.h | 4 +--- 3 files changed, 23 insertions(+), 14 deletions(-) diff --git a/libstorycode/gtk/gtknarrativeview.c b/libstorycode/gtk/gtknarrativeview.c index b7571f1..e44d25b 100644 --- a/libstorycode/gtk/gtknarrativeview.c +++ b/libstorycode/gtk/gtknarrativeview.c @@ -800,22 +800,16 @@ static void insert_text_in_paragraph(struct narrative_item *item, size_t offs, { char *n; int run; - size_t pos; + size_t run_offs; - pos = 0; - for ( run=0; runn_runs; run++ ) { - size_t npos = pos + strlen(item->runs[run].text); - if ( npos >= offs ) break; - pos = npos; - } - offs -= pos; + run = which_run(item, offs, &run_offs); n = malloc(strlen(t) + strlen(item->runs[run].text) + 1); if ( n == NULL ) return; - strncpy(n, item->runs[run].text, offs); - n[offs] = '\0'; + strncpy(n, item->runs[run].text, run_offs); + n[run_offs] = '\0'; strcat(n, t); - strcat(n, item->runs[run].text+offs); + strcat(n, item->runs[run].text+run_offs); free(item->runs[run].text); item->runs[run].text = n; } diff --git a/libstorycode/narrative.c b/libstorycode/narrative.c index 3bf85f5..08c19d3 100644 --- a/libstorycode/narrative.c +++ b/libstorycode/narrative.c @@ -406,6 +406,23 @@ void narrative_delete_block(Narrative *n, int i1, size_t o1, int i2, size_t o2) } +int which_run(struct narrative_item *item, size_t item_offs, size_t *run_offs) +{ + int run; + size_t pos = 0; + + for ( run=0; runn_runs; run++ ) { + size_t npos = pos + strlen(item->runs[run].text); + if ( npos >= item_offs ) break; + pos = npos; + } + if ( run_offs != NULL ) { + *run_offs = item_offs - pos; + } + return run; +} + + void narrative_split_item(Narrative *n, int i1, size_t o1) { /* FIXME! */ diff --git a/libstorycode/narrative_priv.h b/libstorycode/narrative_priv.h index 4862455..dcf8831 100644 --- a/libstorycode/narrative_priv.h +++ b/libstorycode/narrative_priv.h @@ -97,9 +97,7 @@ 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); +extern int which_run(struct narrative_item *item, size_t item_offs, size_t *run_offs); #endif /* NARRATIVE_PRIV_H */ -- cgit v1.2.3