From 91eeae07bd6a4d4db2ec81c4237819c73d79b0de Mon Sep 17 00:00:00 2001 From: Thomas White Date: Sat, 23 Mar 2019 11:34:48 +0100 Subject: Combine slide text strings and layouts in one structure --- libstorycode/gtk/gtkslideview.c | 39 ++++++++++++++++++------------------- libstorycode/slide.c | 41 +++++++++++++-------------------------- libstorycode/slide_priv.h | 16 +++++++++++---- libstorycode/slide_render_cairo.c | 26 +++++++++---------------- 4 files changed, 53 insertions(+), 69 deletions(-) diff --git a/libstorycode/gtk/gtkslideview.c b/libstorycode/gtk/gtkslideview.c index f8975d4..f60ccec 100644 --- a/libstorycode/gtk/gtkslideview.c +++ b/libstorycode/gtk/gtkslideview.c @@ -163,11 +163,11 @@ static size_t pos_trail_to_offset(SlideItem *item, int para, glong char_offs; char *ptr; - char_offs = g_utf8_pointer_to_offset(item->paragraphs[para], - item->paragraphs[para]+offs); + char_offs = g_utf8_pointer_to_offset(item->paras[para].text, + item->paras[para].text+offs); char_offs += trail; - ptr = g_utf8_offset_to_pointer(item->paragraphs[para], char_offs); - return ptr - item->paragraphs[para]; + ptr = g_utf8_offset_to_pointer(item->paras[para].text, char_offs); + return ptr - item->paras[para].text; } @@ -177,7 +177,7 @@ static double para_top(SlideItem *item, int pnum) double py = 0.0; for ( i=0; ilayouts[i], NULL, &rect); + pango_layout_get_extents(item->paras[i].layout, NULL, &rect); py += pango_units_to_double(rect.height); } return py; @@ -192,7 +192,7 @@ static int get_cursor_pos(SlideItem *item, Stylesheet *stylesheet, PangoRectangle rect; double padl, padr, padt, padb; - if ( item->layouts[cpos.para] == NULL ) { + if ( item->paras[cpos.para].layout == NULL ) { fprintf(stderr, "get_cursor_pos: No layout\n"); return 1; } @@ -201,7 +201,7 @@ static int get_cursor_pos(SlideItem *item, Stylesheet *stylesheet, slide_w, slide_h); offs = pos_trail_to_offset(item, cpos.para, cpos.pos, cpos.trail); - pango_layout_get_cursor_pos(item->layouts[cpos.para], offs, &rect, NULL); + pango_layout_get_cursor_pos(item->paras[cpos.para].layout, offs, &rect, NULL); *x = pango_units_to_double(rect.x) + padl; *y = pango_units_to_double(rect.y) + para_top(item, cpos.para) + padt; *h = pango_units_to_double(rect.height); @@ -547,14 +547,14 @@ static int find_cursor(SlideItem *item, Stylesheet *stylesheet, do { PangoRectangle rect; - pango_layout_get_extents(item->layouts[i++], NULL, &rect); + pango_layout_get_extents(item->paras[i++].layout, NULL, &rect); top = cur_y; cur_y += pango_units_to_double(rect.height); } while ( (cur_y < y) && (in_paras) ); pos->para = i-1; - pango_layout_xy_to_index(item->layouts[i-1], + pango_layout_xy_to_index(item->paras[i-1].layout, pango_units_from_double(x), pango_units_from_double(y - top), &pos->pos, &pos->trail); @@ -883,7 +883,7 @@ static size_t end_offset_of_para(SlideItem *item, int pnum) { assert(pnum >= 0); if ( is_text(item->type) ) return 0; - return strlen(item->paragraphs[pnum]); + return strlen(item->paras[pnum].text); } @@ -892,10 +892,9 @@ static void cursor_moveh(GtkSlideView *e, struct slide_pos *cp, signed int dir) int np = cp->pos; if ( !is_text(e->cursor_frame->type) ) return; - if ( e->cursor_frame->layouts == NULL ) return; - if ( e->cursor_frame->layouts[e->cpos.para] == NULL ) return; + if ( e->cursor_frame->paras[e->cpos.para].layout == NULL ) return; - pango_layout_move_cursor_visually(e->cursor_frame->layouts[e->cpos.para], + pango_layout_move_cursor_visually(e->cursor_frame->paras[e->cpos.para].layout, 1, cp->pos, cp->trail, dir, &np, &cp->trail); @@ -948,14 +947,14 @@ static int slide_positions_equal(struct slide_pos a, struct slide_pos b) static void insert_text_in_paragraph(SlideItem *item, int para, size_t offs, char *t) { - char *n = malloc(strlen(t) + strlen(item->paragraphs[para]) + 1); + char *n = malloc(strlen(t) + strlen(item->paras[para].text) + 1); if ( n == NULL ) return; - strncpy(n, item->paragraphs[para], offs); + strncpy(n, item->paras[para].text, offs); n[offs] = '\0'; strcat(n, t); - strcat(n, item->paragraphs[para]+offs); - free(item->paragraphs[para]); - item->paragraphs[para] = n; + strcat(n, item->paras[para].text+offs); + free(item->paras[para].text); + item->paras[para].text = n; } @@ -985,8 +984,8 @@ static void insert_text(char *t, GtkSlideView *e) off = pos_trail_to_offset(e->cursor_frame, e->cpos.para, e->cpos.pos, e->cpos.trail); insert_text_in_paragraph(e->cursor_frame, e->cpos.para, off, t); - pango_layout_set_text(e->cursor_frame->layouts[e->cpos.para], - e->cursor_frame->paragraphs[e->cpos.para], -1); + pango_layout_set_text(e->cursor_frame->paras[e->cpos.para].layout, + e->cursor_frame->paras[e->cpos.para].text, -1); cursor_moveh(e, &e->cpos, +1); emit_change_sig(e); redraw(e); diff --git a/libstorycode/slide.c b/libstorycode/slide.c index 0702f52..cb2a5a1 100644 --- a/libstorycode/slide.c +++ b/libstorycode/slide.c @@ -62,7 +62,7 @@ static SlideItem *add_item(Slide *s) s->items = new_items; item = &s->items[s->n_items++]; - item->layouts = NULL; + item->paras = NULL; return item; } @@ -94,16 +94,16 @@ int add_text_item(Slide *s, char **text, int n_text, struct frame_geom geom, if ( item == NULL ) return 1; item->type = slide_item; - item->layouts = NULL; - item->paragraphs = malloc(n_text*sizeof(char *)); - if ( item->paragraphs == NULL ) { + item->paras = malloc(n_text*sizeof(struct slide_text_paragraph)); + if ( item->paras == NULL ) { s->n_items--; return 1; } for ( i=0; in_paras = n_text; - item->paragraphs[i] = text[i]; + item->paras[i].text = text[i]; + item->paras[i].layout = NULL; } item->n_paras = n_text; @@ -311,33 +311,18 @@ void slide_item_get_padding(SlideItem *item, Stylesheet *ss, void slide_item_split_text_paragraph(SlideItem *item, int para, size_t off) { - char **np; + struct slide_text_paragraph *np; - np = realloc(item->paragraphs, (item->n_paras+1)*sizeof(char *)); + np = realloc(item->paras, (item->n_paras+1)*sizeof(struct slide_text_paragraph)); if ( np == NULL ) return; -#ifdef HAVE_PANGO - PangoLayout **nl; - nl = realloc(item->layouts, (item->n_paras+1)*sizeof(PangoLayout *)); - if ( nl == NULL ) { - free(np); - return; - } - item->layouts = nl; -#endif - - item->paragraphs = np; + item->paras = np; item->n_paras++; - memmove(&item->paragraphs[para+1], &item->paragraphs[para], - (item->n_paras - para - 1)*sizeof(char *)); - -#ifdef HAVE_PANGO - memmove(&item->layouts[para+1], &item->layouts[para], - (item->n_paras - para - 1)*sizeof(PangoLayout *)); - item->layouts[para+1] = NULL; -#endif + memmove(&item->paras[para+1], &item->paras[para], + (item->n_paras - para - 1)*sizeof(struct slide_text_paragraph)); - item->paragraphs[para+1] = strdup(&item->paragraphs[para][off]); - item->paragraphs[para][off] = '\0'; + item->paras[para+1].text = strdup(&item->paras[para].text[off]); + item->paras[para+1].layout = NULL; + item->paras[para].text[off] = '\0'; } diff --git a/libstorycode/slide_priv.h b/libstorycode/slide_priv.h index 0d80663..75a2ac5 100644 --- a/libstorycode/slide_priv.h +++ b/libstorycode/slide_priv.h @@ -39,17 +39,25 @@ enum slide_item_type }; +struct slide_text_paragraph +{ + char *text; +#ifdef HAVE_PANGO + PangoLayout *layout; +#else + void *layout; +#endif +}; + + struct _slideitem { enum slide_item_type type; /* For TEXT, SLIDETITLE, PRESTITLE */ - char **paragraphs; int n_paras; + struct slide_text_paragraph *paras; enum alignment align; -#ifdef HAVE_PANGO - PangoLayout **layouts; -#endif /* For IMAGE */ char *filename; diff --git a/libstorycode/slide_render_cairo.c b/libstorycode/slide_render_cairo.c index 5b6bfc7..16be90f 100644 --- a/libstorycode/slide_render_cairo.c +++ b/libstorycode/slide_render_cairo.c @@ -92,14 +92,6 @@ static void render_text(SlideItem *item, cairo_t *cr, PangoContext *pc, fontdesc = pango_font_description_from_string(font); - if ( item->layouts == NULL ) { - item->layouts = malloc(item->n_paras*sizeof(PangoLayout *)); - if ( item->layouts == NULL ) return; - for ( i=0; in_paras; i++ ) { - item->layouts[i] = NULL; - } - } - if ( item->align == ALIGN_INHERIT ) { /* Use value from stylesheet */ palignment = to_pangoalignment(align); @@ -116,16 +108,16 @@ static void render_text(SlideItem *item, cairo_t *cr, PangoContext *pc, for ( i=0; in_paras; i++ ) { - if ( item->layouts[i] == NULL ) { - item->layouts[i] = pango_layout_new(pc); + if ( item->paras[i].layout == NULL ) { + item->paras[i].layout = pango_layout_new(pc); } - pango_layout_set_width(item->layouts[i], + pango_layout_set_width(item->paras[i].layout, pango_units_from_double(w-pad_l-pad_r)); - pango_layout_set_text(item->layouts[i], item->paragraphs[i], -1); + pango_layout_set_text(item->paras[i].layout, item->paras[i].text, -1); - pango_layout_set_alignment(item->layouts[i], palignment); + pango_layout_set_alignment(item->paras[i].layout, palignment); - pango_layout_set_font_description(item->layouts[i], fontdesc); + pango_layout_set_font_description(item->paras[i].layout, fontdesc); /* FIXME: Handle *bold*, _underline_, /italic/ etc. */ //pango_layout_set_attributes(item->layouts[i], attrs); @@ -135,9 +127,9 @@ static void render_text(SlideItem *item, cairo_t *cr, PangoContext *pc, cairo_set_source_rgba(cr, fgcol[0], fgcol[1], fgcol[2], fgcol[3]); cairo_move_to(cr, 0.0, 0.0); - pango_cairo_update_layout(cr, item->layouts[i]); - pango_cairo_show_layout(cr, item->layouts[i]); - pango_layout_get_extents(item->layouts[i], NULL, &rect); + pango_cairo_update_layout(cr, item->paras[i].layout); + pango_cairo_show_layout(cr, item->paras[i].layout); + pango_layout_get_extents(item->paras[i].layout, NULL, &rect); cairo_translate(cr, 0.0, pango_units_to_double(rect.height)); cairo_fill(cr); -- cgit v1.2.3