From f2bfe89481217625f74224289947f7bcd839c55f Mon Sep 17 00:00:00 2001 From: Thomas White Date: Fri, 1 Mar 2019 23:28:27 +0100 Subject: Debug narrative rendering machinery --- libstorycode/gtk/gtknarrativeview.c | 8 +-- libstorycode/narrative.c | 21 +++++++- libstorycode/narrative.h | 2 +- libstorycode/narrative_priv.h | 13 ++--- libstorycode/narrative_render_cairo.c | 96 ++++++++++++----------------------- libstorycode/narrative_render_cairo.h | 3 +- 6 files changed, 64 insertions(+), 79 deletions(-) diff --git a/libstorycode/gtk/gtknarrativeview.c b/libstorycode/gtk/gtknarrativeview.c index 9040d55..55565d8 100644 --- a/libstorycode/gtk/gtknarrativeview.c +++ b/libstorycode/gtk/gtknarrativeview.c @@ -132,14 +132,14 @@ static gboolean resize_sig(GtkWidget *widget, GdkEventConfigure *event, e->visible_height = event->height; e->visible_width = event->width; - e->w = e->visible_width; - e->h = narrative_get_height(presentation_get_narrative(e->p)); - /* Wrap everything with the current width, to get the total height */ narrative_wrap(presentation_get_narrative(e->p), presentation_get_stylesheet(e->p), pango_language_get_default(), pc, e->w); + e->w = e->visible_width; + e->h = narrative_get_height(presentation_get_narrative(e->p)); + g_object_unref(pc); return FALSE; @@ -528,6 +528,8 @@ static gboolean draw_sig(GtkWidget *da, cairo_t *cr, GtkNarrativeView *e) cairo_fill(cr); /* Contents */ + narrative_render_cairo(presentation_get_narrative(e->p), cr, + presentation_get_stylesheet(e->p)); /* Editing overlay */ draw_overlay(cr, e); diff --git a/libstorycode/narrative.c b/libstorycode/narrative.c index 85f5410..81c69b1 100644 --- a/libstorycode/narrative.c +++ b/libstorycode/narrative.c @@ -48,6 +48,16 @@ void narrative_free(Narrative *n) } +static struct narrative_item *add_item(Narrative *n) +{ + struct narrative_item *new_items; + new_items = realloc(n->items, (n->n_items+1)*sizeof(struct narrative_item)); + if ( new_items == NULL ) return NULL; + n->items = new_items; + return &n->items[n->n_items++]; +} + + void narrative_add_prestitle(Narrative *n, const char *text) { } @@ -58,8 +68,17 @@ void narrative_add_bp(Narrative *n, const char *text) } -void narrative_add_text(Narrative *n, const char *text) +void narrative_add_text(Narrative *n, char *text) { + struct narrative_item *item; + + item = add_item(n); + if ( item == NULL ) return; + + item->type = NARRATIVE_ITEM_TEXT; + item->text = text; + item->align = ALIGN_LEFT; + item->layout = NULL; } diff --git a/libstorycode/narrative.h b/libstorycode/narrative.h index 8f9727f..ef757c0 100644 --- a/libstorycode/narrative.h +++ b/libstorycode/narrative.h @@ -36,7 +36,7 @@ extern void narrative_free(Narrative *n); extern void narrative_add_prestitle(Narrative *n, const char *text); extern void narrative_add_bp(Narrative *n, const char *text); -extern void narrative_add_text(Narrative *n, const char *text); +extern void narrative_add_text(Narrative *n, char *text); extern void narrative_add_slide(Narrative *n, Slide *slide); diff --git a/libstorycode/narrative_priv.h b/libstorycode/narrative_priv.h index 059b015..c1695e3 100644 --- a/libstorycode/narrative_priv.h +++ b/libstorycode/narrative_priv.h @@ -43,18 +43,13 @@ struct narrative_item enum narrative_item_type type; /* For TEXT, SLIDETITLE, PRESTITLE */ - char **paragraphs; - int n_paras; + char *text; enum alignment align; #ifdef HAVE_PANGO - PangoLayout **layouts; + PangoLayout *layout; +#else + void *layout; #endif - - /* For IMAGE */ - char *filename; - - /* For TEXT and IMAGE */ - struct frame_geom geom; }; diff --git a/libstorycode/narrative_render_cairo.c b/libstorycode/narrative_render_cairo.c index 746ce62..afc8f16 100644 --- a/libstorycode/narrative_render_cairo.c +++ b/libstorycode/narrative_render_cairo.c @@ -65,17 +65,8 @@ static double wrap_text(struct narrative_item *item, PangoContext *pc, Stylesheet *ss, enum style_element el, double wrap_w, PangoFontDescription *fontdesc, enum alignment align) { - int i; PangoAlignment palignment; - double total_h = 0.0; - - if ( item->layouts == NULL ) { - item->layouts = malloc(item->n_paras*sizeof(PangoLayout *)); - if ( item->layouts == NULL ) return 0.0; - for ( i=0; in_paras; i++ ) { - item->layouts[i] = NULL; - } - } + PangoRectangle rect; if ( item->align == ALIGN_INHERIT ) { /* Use value from stylesheet */ @@ -85,62 +76,36 @@ static double wrap_text(struct narrative_item *item, PangoContext *pc, palignment = to_pangoalignment(item->align); } - for ( i=0; in_paras; i++ ) { - - PangoRectangle rect; - - if ( item->layouts[i] == NULL ) { - item->layouts[i] = pango_layout_new(pc); - } - pango_layout_set_width(item->layouts[i], pango_units_from_double(wrap_w)); - pango_layout_set_text(item->layouts[i], item->paragraphs[i], -1); - pango_layout_set_alignment(item->layouts[i], palignment); - pango_layout_set_font_description(item->layouts[i], fontdesc); - - /* FIXME: Handle *bold*, _underline_, /italic/ etc. */ - //pango_layout_set_attributes(item->layouts[i], attrs); - //pango_attr_list_unref(attrs); - - pango_layout_get_extents(item->layouts[i], NULL, &rect); - total_h += pango_units_to_double(rect.height); - + if ( item->layout == NULL ) { + item->layout = pango_layout_new(pc); } + pango_layout_set_width(item->layout, pango_units_from_double(wrap_w)); + pango_layout_set_text(item->layout, item->text, -1); + pango_layout_set_alignment(item->layout, palignment); + pango_layout_set_font_description(item->layout, fontdesc); - return total_h; + /* FIXME: Handle *bold*, _underline_, /italic/ etc. */ + //pango_layout_set_attributes(item->layout, attrs); + //pango_attr_list_unref(attrs); + + pango_layout_get_extents(item->layout, NULL, &rect); + return pango_units_to_double(rect.height); } -static void draw_text(struct narrative_item *item, cairo_t *cr) +static double draw_text(struct narrative_item *item, cairo_t *cr) { - int i; - double hpos = 0.0; - - cairo_save(cr); + PangoRectangle rect; - for ( i=0; in_paras; i++ ) { + //if ( (hpos + cur_h > min_y) && (hpos < max_y) ) { + cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 1.0); + pango_cairo_update_layout(cr, item->layout); + pango_cairo_show_layout(cr, item->layout); + cairo_fill(cr); + //} /* else paragraph is not visible */ - PangoRectangle rect; - double cur_h; - - pango_layout_get_extents(item->layouts[i], NULL, &rect); - cur_h = rect.height; - - cairo_save(cr); - cairo_translate(cr, 0.0, hpos); - - //if ( (hpos + cur_h > min_y) && (hpos < max_y) ) { - cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 1.0); - pango_cairo_update_layout(cr, item->layouts[i]); - pango_cairo_show_layout(cr, item->layouts[i]); - cairo_fill(cr); - //} /* else paragraph is not visible */ - - hpos += cur_h; - cairo_restore(cr); - - } - - cairo_restore(cr); + pango_layout_get_extents(item->layout, NULL, &rect); + return pango_units_to_double(rect.height); } @@ -204,16 +169,16 @@ double narrative_get_height(Narrative *n) } -int narrative_render_cairo(Narrative *n, cairo_t *cr, Stylesheet *stylesheet, - PangoLanguage *lang, PangoContext *pc, double w) +int narrative_render_cairo(Narrative *n, cairo_t *cr, Stylesheet *stylesheet) { int i, r; enum gradient bg; double bgcol[4]; double bgcol2[4]; cairo_pattern_t *patt = NULL; + double vpos = 0.0; - r = stylesheet_get_background(stylesheet, STYEL_SLIDE, &bg, bgcol, bgcol2); + r = stylesheet_get_background(stylesheet, STYEL_NARRATIVE, &bg, bgcol, bgcol2); if ( r ) return 1; /* Overall background */ @@ -243,14 +208,17 @@ int narrative_render_cairo(Narrative *n, cairo_t *cr, Stylesheet *stylesheet, for ( i=0; in_items; i++ ) { + cairo_save(cr); + cairo_translate(cr, 0.0, vpos); + switch ( n->items[i].type ) { case NARRATIVE_ITEM_TEXT : - draw_text(&n->items[i], cr); + vpos += draw_text(&n->items[i], cr); break; case NARRATIVE_ITEM_BP : - draw_text(&n->items[i], cr); + vpos += draw_text(&n->items[i], cr); break; case NARRATIVE_ITEM_SLIDE : @@ -260,6 +228,8 @@ int narrative_render_cairo(Narrative *n, cairo_t *cr, Stylesheet *stylesheet, break; } + + cairo_restore(cr); } return 0; diff --git a/libstorycode/narrative_render_cairo.h b/libstorycode/narrative_render_cairo.h index 1f16aae..ebabb4d 100644 --- a/libstorycode/narrative_render_cairo.h +++ b/libstorycode/narrative_render_cairo.h @@ -37,7 +37,6 @@ extern int narrative_wrap(Narrative *n, Stylesheet *stylesheet, extern double narrative_get_height(Narrative *n); extern int narrative_render_cairo(Narrative *n, cairo_t *cr, - Stylesheet *stylesheet, PangoLanguage *lang, - PangoContext *pc); + Stylesheet *stylesheet); #endif /* NARRATIVE_RENDER_CAIRO_H */ -- cgit v1.2.3