diff options
author | Thomas White <taw@physics.org> | 2019-10-04 21:34:41 +0200 |
---|---|---|
committer | Thomas White <taw@physics.org> | 2019-10-05 17:03:43 +0200 |
commit | 309ca67853b35f3f652686dbb52ca5182b81d0a7 (patch) | |
tree | 496e3be89df38182f37cba39291ec77517d93a30 | |
parent | 91242b8dc8ae39add836c730d6e77d44222bdde3 (diff) |
Implement which_run()
-rw-r--r-- | libstorycode/gtk/gtknarrativeview.c | 16 | ||||
-rw-r--r-- | libstorycode/narrative.c | 17 | ||||
-rw-r--r-- | 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; run<item->n_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; run<item->n_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 */ |