From 93956372d317fc90f8751e6b319e0055507c2519 Mon Sep 17 00:00:00 2001 From: Thomas White Date: Fri, 17 Jan 2014 10:13:46 +0100 Subject: Unwrapped text belongs to frame, not interpreter --- src/frame.c | 16 ++++++++++++++-- src/frame.h | 1 + src/render.c | 11 +++++++++-- src/sc_interp.c | 28 ++++++++++++---------------- src/sc_interp.h | 2 -- src/wrap.c | 7 ++----- src/wrap.h | 2 +- tests/render_test.c | 6 ++---- tests/render_test_sc1.c | 4 +--- 9 files changed, 42 insertions(+), 35 deletions(-) diff --git a/src/frame.c b/src/frame.c index 40daa86..2906c40 100644 --- a/src/frame.c +++ b/src/frame.c @@ -31,6 +31,7 @@ #include "sc_parse.h" #include "frame.h" +#include "wrap.h" static int alloc_ro(struct frame *fr) @@ -56,12 +57,23 @@ struct frame *frame_new() n->children = NULL; n->max_children = 32; - alloc_ro(n); - + if ( alloc_ro(n) ) { + fprintf(stderr, "Couldn't allocate children\n"); + free(n); + return NULL; + } n->num_children = 0; n->scblocks = NULL; + n->boxes = malloc(sizeof(struct wrap_line)); + if ( n->boxes == NULL ) { + fprintf(stderr, "Failed to allocate boxes.\n"); + free(n); + return NULL; + } + initialise_line(n->boxes); + return n; } diff --git a/src/frame.h b/src/frame.h index f71794b..e1cfccd 100644 --- a/src/frame.h +++ b/src/frame.h @@ -47,6 +47,7 @@ struct frame int max_children; SCBlock *scblocks; + struct wrap_line *boxes; /* The unwrapped boxes */ int n_lines; int max_lines; diff --git a/src/render.c b/src/render.c index 4509152..8c28536 100644 --- a/src/render.c +++ b/src/render.c @@ -297,18 +297,25 @@ static int render_frame(cairo_t *cr, struct frame *fr, ImageStore *is, } for ( i=0; in_lines; i++ ) { - wrap_line_free(&fr->lines[i]); + // wrap_line_free(&fr->lines[i]); } free(fr->lines); fr->lines = NULL; fr->n_lines = 0; fr->max_lines = 0; + if ( fr->boxes != NULL ) { + free(fr->boxes->boxes); + free(fr->boxes); + } + fr->boxes = malloc(sizeof(struct wrap_line)); + initialise_line(fr->boxes); + /* SCBlocks -> frames and wrap boxes (preferably re-using frames) */ sc_interp_add_blocks(scin, bl); /* Wrap boxes -> wrap lines */ - wrap_contents(fr, sc_interp_get_boxes(scin)); + wrap_contents(fr); /* Actually draw the lines */ draw_frame(cr, fr, is, isz); diff --git a/src/sc_interp.c b/src/sc_interp.c index 9fd6b24..5e1be23 100644 --- a/src/sc_interp.c +++ b/src/sc_interp.c @@ -60,8 +60,6 @@ struct _scinterp struct sc_state *state; int j; /* Index of the current state */ int max_state; - - struct wrap_line *boxes; }; @@ -249,13 +247,6 @@ SCInterpreter *sc_interp_new(PangoContext *pc, struct frame *top) /* FIXME: Determine proper language (somehow...) */ scin->lang = pango_language_from_string("en_GB"); - scin->boxes = malloc(sizeof(struct wrap_line)); - if ( scin->boxes == NULL ) { - fprintf(stderr, "Failed to allocate boxes.\n"); - return NULL; - } - initialise_line(scin->boxes); - /* The "ultimate" default font */ set_font(scin, "Sans 12"); set_colour(scin, "#000000"); @@ -411,8 +402,8 @@ int sc_interp_add_blocks(SCInterpreter *scin, SCBlock *bl) } if ( name == NULL ) { - split_words(scin->boxes, scin->pc, contents, - scin->lang, 1, scin); + split_words(sc_interp_get_frame(scin)->boxes, scin->pc, + contents, scin->lang, 1, scin); } else if ( strcmp(name, "font") == 0 ) { set_font(scin, options); @@ -444,7 +435,16 @@ int sc_interp_add_blocks(SCInterpreter *scin, SCBlock *bl) #endif } else if ( strcmp(name, "f")==0 ) { + struct frame *fr = sc_block_frame(bl); + + if ( fr != NULL ) { + free(fr->boxes->boxes); + free(fr->boxes); + fr->boxes = malloc(sizeof(struct wrap_line)); + initialise_line(fr->boxes); + } + if ( fr == NULL ) { fr = add_subframe(sc_interp_get_frame(scin)); sc_block_set_frame(bl, fr); @@ -454,6 +454,7 @@ int sc_interp_add_blocks(SCInterpreter *scin, SCBlock *bl) fprintf(stderr, "Failed to add frame.\n"); goto next; } + parse_frame_options(fr, sc_interp_get_frame(scin), options); set_frame(scin, fr); @@ -477,8 +478,3 @@ next: return 0; } - -struct wrap_line *sc_interp_get_boxes(SCInterpreter *scin) -{ - return scin->boxes; -} diff --git a/src/sc_interp.h b/src/sc_interp.h index 78f32d9..80b1e70 100644 --- a/src/sc_interp.h +++ b/src/sc_interp.h @@ -39,8 +39,6 @@ extern void sc_interp_restore(SCInterpreter *scin); extern int sc_interp_add_blocks(SCInterpreter *scin, SCBlock *bl); -extern struct wrap_line *sc_interp_get_boxes(SCInterpreter *scin); - /* Get the current state of the interpreter */ extern struct frame *sc_interp_get_frame(SCInterpreter *scin); extern PangoFont *sc_interp_get_font(SCInterpreter *scin); diff --git a/src/wrap.c b/src/wrap.c index 25f5d4d..782828a 100644 --- a/src/wrap.c +++ b/src/wrap.c @@ -803,7 +803,7 @@ void show_boxes(struct wrap_line *boxes) /* Wrap the StoryCode inside "fr->sc" so that it fits within width "fr->w", * and generate fr->lines */ -int wrap_contents(struct frame *fr, struct wrap_line *boxes) +int wrap_contents(struct frame *fr) { struct wrap_line *para; int i; @@ -821,7 +821,7 @@ int wrap_contents(struct frame *fr, struct wrap_line *boxes) i = 0; do { - para = split_paragraph(boxes, &i); + para = split_paragraph(fr->boxes, &i); /* Split paragraphs into lines */ if ( para != NULL ) { @@ -835,9 +835,6 @@ int wrap_contents(struct frame *fr, struct wrap_line *boxes) } while ( para != NULL ); - free(boxes->boxes); - free(boxes); - for ( i=0; in_lines; i++ ) { distribute_spaces(&fr->lines[i], wrap_w, rho); calc_line_geometry(&fr->lines[i]); diff --git a/src/wrap.h b/src/wrap.h index f29e0dc..8fc057a 100644 --- a/src/wrap.h +++ b/src/wrap.h @@ -99,7 +99,7 @@ struct wrap_line }; -extern int wrap_contents(struct frame *fr, struct wrap_line *boxes); +extern int wrap_contents(struct frame *fr); extern void get_cursor_pos(struct frame *fr, size_t pos, double *xposd, double *yposd, double *line_height); diff --git a/tests/render_test.c b/tests/render_test.c index 79d1110..88bcf6a 100644 --- a/tests/render_test.c +++ b/tests/render_test.c @@ -3,7 +3,7 @@ * * Rendering test * - * Copyright © 2012-2013 Thomas White + * Copyright © 2012-2014 Thomas White * * This file is part of Colloquium. * @@ -79,10 +79,8 @@ int main(int argc, char *argv[]) gtk_init(&argc, &argv); - fr = calloc(1, sizeof(struct frame)); + fr = frame_new(); if ( fr == NULL ) return 1; - fr->children = NULL; - fr->num_children = 0; fr->scblocks = sc_parse(sc); if ( fr->scblocks == NULL ) { fprintf(stderr, "SC parse failed.\n"); diff --git a/tests/render_test_sc1.c b/tests/render_test_sc1.c index f84c13a..63c59d7 100644 --- a/tests/render_test_sc1.c +++ b/tests/render_test_sc1.c @@ -77,10 +77,8 @@ int main(int argc, char *argv[]) gtk_init(&argc, &argv); - fr = calloc(1, sizeof(struct frame)); + fr = frame_new(); if ( fr == NULL ) return 1; - fr->children = NULL; - fr->num_children = 0; fr->scblocks = sc_parse(sc); if ( fr->scblocks == NULL ) { fprintf(stderr, "SC parse failed.\n"); -- cgit v1.2.3