aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorThomas White <taw@bitwiz.org.uk>2018-03-07 20:10:41 +0100
committerThomas White <taw@bitwiz.org.uk>2018-03-07 20:10:41 +0100
commitf4c56706bc083f9792420fb1af2ae8cdf9651bb7 (patch)
tree8dbcdabec3f151d7d517827e04fac9047fe0081f /src
parent74ad51de1a6dc74b9c253c254cccfe543d0c69b0 (diff)
Don't create runs for \newpara
Removes a load more special cases
Diffstat (limited to 'src')
-rw-r--r--src/frame.c114
-rw-r--r--src/frame.h1
-rw-r--r--src/sc_interp.c12
-rw-r--r--src/sc_parse.c13
4 files changed, 59 insertions, 81 deletions
diff --git a/src/frame.c b/src/frame.c
index 151a545..ac3103a 100644
--- a/src/frame.c
+++ b/src/frame.c
@@ -281,11 +281,6 @@ static size_t run_text_len(const struct text_run *run)
}
if ( sc_block_contents(run->rscblock) == NULL ) {
- if ( sc_block_name(run->rscblock) != NULL ) {
- if ( strcmp("newpara", sc_block_name(run->rscblock)) == 0 ) {
- return 0;
- }
- }
fprintf(stderr, "NULL rscblock contents in run_text_len\n");
return 0;
}
@@ -342,8 +337,6 @@ void wrap_paragraph(Paragraph *para, PangoContext *pc, double w,
guint16 r, g, b;
run_text = sc_block_contents(para->runs[i].rscblock);
-
- if ( run_text == NULL ) continue; /* Could be \newpara */
run_len = strlen(run_text);
attr = pango_attr_font_desc_new(para->runs[i].fontdesc);
@@ -575,6 +568,20 @@ Paragraph *last_open_para(struct frame *fr)
}
+void add_newpara(struct frame *fr, SCBlock *bl)
+{
+ Paragraph *last_para;
+
+ if ( fr->paras == NULL ) return;
+ last_para = fr->paras[fr->n_paras-1];
+
+ if ( last_para->open ) {
+ set_newline_at_end(last_para, bl);
+ close_last_paragraph(fr);
+ } /* else do nothing */
+}
+
+
void close_last_paragraph(struct frame *fr)
{
if ( fr->paras == NULL ) return;
@@ -984,12 +991,6 @@ size_t pos_trail_to_offset(Paragraph *para, size_t offs, int trail)
return 0;
}
- if ( (sc_block_name(run->rscblock) != NULL)
- && (strcmp(sc_block_name(run->rscblock), "newpara") == 0) )
- {
- return 0;
- }
-
if ( sc_block_contents(run->rscblock) == NULL ) {
fprintf(stderr, "pos_trail_to_offset: No contents "
"(%p name=%s, options=%s)\n",
@@ -1023,8 +1024,6 @@ size_t pos_trail_to_offset(Paragraph *para, size_t offs, int trail)
void insert_text_in_paragraph(Paragraph *para, size_t offs, const char *t)
{
int nrun;
- struct text_run *run;
- size_t run_offs;
/* Find which run we are in */
nrun = which_run(para, offs);
@@ -1032,43 +1031,16 @@ void insert_text_in_paragraph(Paragraph *para, size_t offs, const char *t)
fprintf(stderr, "Couldn't find run to insert into.\n");
return;
}
- run = &para->runs[nrun];
-
- /* Translate paragraph offset for insertion into SCBlock offset */
- run_offs = offs - get_paragraph_offset(para, nrun);
-
- if ( (sc_block_name(run->rscblock) != NULL)
- && (strcmp(sc_block_name(run->rscblock), "newpara") == 0) )
- {
-
- if ( para->n_runs == 1 ) {
-
- SCBlock *nnp;
- printf("Inserting into newpara block...\n");
-
- /* The first \newpara block becomes a normal anonymous block */
- sc_block_set_name(run->rscblock, NULL);
- sc_block_set_contents(run->rscblock, strdup(t));
-
- /* Add a new \newpara block after this one */
- nnp = sc_block_append(run->rscblock, "newpara",
- NULL, NULL, NULL);
- add_run(para, nnp, nnp, 0, run->fontdesc, run->col);
-
- para->newline_at_end = nnp;
-
- return;
-
- } else {
-
- run = &para->runs[nrun-1];
- run_offs = strlen(sc_block_contents(run->rscblock));
-
- }
+ if ( para->n_runs == 0 ) {
+ printf("No runs in paragraph?\n");
+ } else {
+ struct text_run *run;
+ size_t run_offs;
+ run = &para->runs[nrun];
+ run_offs = offs - get_paragraph_offset(para, nrun);
+ sc_insert_text(run->rscblock, run_offs, t);
}
-
- sc_insert_text(run->rscblock, run_offs, t);
}
@@ -1287,8 +1259,6 @@ static signed int merge_paragraph_runs(Paragraph *p1, Paragraph *p2)
}
p1->runs = runs_new;
- assert(p1->runs[p1->n_runs-1].scblock == get_newline_at_end(p1));
- p1->n_runs--; /* Chop off the run corresponding to \newpara */
spos = p1->n_runs;
/* The end of the united paragraph should now be the end of the
@@ -1621,31 +1591,38 @@ static SCBlock *split_text_paragraph(struct frame *fr, int pn, size_t pos,
if ( run == para->n_runs-1 ) {
+ SCBlock *end;
+
printf("Simple new para\n");
- /* Right at the end of a paragraph:
- * - don't touch the current paragraph
- * - add a new paragraph after
- * - .. containing just a \newpara block and run */
-
- nnp = sc_block_append(rr->scblock, strdup("newpara"), NULL, NULL, NULL);
- pnew->runs[0].scblock = nnp;
- pnew->runs[0].rscblock = nnp;
- pnew->runs[0].fontdesc = pango_font_description_copy(rr->fontdesc);
- pnew->runs[0].col[0] = rr->col[0];
- pnew->runs[0].col[1] = rr->col[1];
- pnew->runs[0].col[2] = rr->col[2];
- pnew->runs[0].col[3] = rr->col[3];
- pnew->n_runs = 1;
+ if ( get_newline_at_end(para) == NULL ) {
+ /* The current paragraph doesn't have
+ * a \newpara yet */
+ end = sc_block_append(rr->scblock,
+ strdup("newpara"), NULL,
+ NULL, NULL);
+ set_newline_at_end(para, end);
+ } else {
+ /* If the current paragraph did have \newpara,
+ * then the new one needs one too */
+ end = sc_block_append(rr->scblock,
+ strdup("newpara"),
+ NULL, NULL, NULL);
+ set_newline_at_end(pnew, end);
+ }
- set_newline_at_end(pnew, nnp);
+ /* Add an empty run + SCBlock to type into */
+ end = sc_block_append(end, NULL,
+ NULL, strdup(""), NULL);
+ pnew->n_runs = 0;
+ add_run(pnew, end, end, 0, fr->fontdesc, fr->col);
pnew->open = para->open;
para->open = 0;
wrap_paragraph(pnew, pc, fr->w - fr->pad_l - fr->pad_r, 0, 0);
- return nnp;
+ return end;
} else {
@@ -1698,7 +1675,6 @@ static SCBlock *split_text_paragraph(struct frame *fr, int pn, size_t pos,
/* Add a \newpara after the end of the first paragraph's SC */
nnp = sc_block_append(rr->scblock, strdup("newpara"), NULL, NULL, NULL);
- add_run(para, nnp, nnp, 0, rr->fontdesc, rr->col);
set_newline_at_end(para, nnp);
pnew->open = para->open;
diff --git a/src/frame.h b/src/frame.h
index 7604090..7d9f741 100644
--- a/src/frame.h
+++ b/src/frame.h
@@ -121,6 +121,7 @@ extern struct frame *find_frame_with_scblocks(struct frame *top,
extern double total_height(struct frame *fr);
+extern void add_newpara(struct frame *fr, SCBlock *bl);
extern Paragraph *last_open_para(struct frame *fr);
extern Paragraph *current_para(struct frame *fr);
extern void close_last_paragraph(struct frame *fr);
diff --git a/src/sc_interp.c b/src/sc_interp.c
index 921bf94..006046d 100644
--- a/src/sc_interp.c
+++ b/src/sc_interp.c
@@ -954,7 +954,6 @@ static int add_text(struct frame *fr, PangoContext *pc, SCBlock *bl,
/* Empty block? */
if ( text == NULL ) return 1;
- if ( strlen(text) == 0 ) return 1;
fontdesc = sc_interp_get_fontdesc(scin);
col = sc_interp_get_fgcol(scin);
@@ -1021,8 +1020,7 @@ static int check_outputs(SCBlock *bl, SCInterpreter *scin)
set_frame_default_style(fr, scin);
- parse_frame_options(fr, sc_interp_get_frame(scin),
- options);
+ parse_frame_options(fr, sc_interp_get_frame(scin), options);
maybe_recurse_before(scin, child);
set_frame(scin, fr);
@@ -1031,13 +1029,7 @@ static int check_outputs(SCBlock *bl, SCInterpreter *scin)
} else if ( strcmp(name, "newpara")==0 ) {
struct frame *fr = sc_interp_get_frame(scin);
- Paragraph *para = last_open_para(fr);
-
- /* Add a dummy run which we can type into */
- add_run(para, bl, bl, 0, sc_interp_get_fontdesc(scin), fr->col);
-
- set_newline_at_end(para, bl);
- close_last_paragraph(fr);
+ add_newpara(fr, bl);
} else {
return 0;
diff --git a/src/sc_parse.c b/src/sc_parse.c
index b39d183..23bf2d0 100644
--- a/src/sc_parse.c
+++ b/src/sc_parse.c
@@ -541,10 +541,19 @@ static void separate_newlines(SCBlock *bl)
bl->contents = NULL;
nb = bl;
} else {
- sc_block_append(bl, strdup("newpara"), NULL, NULL, &nb);
+ sc_block_append(bl, strdup("newpara"),
+ NULL, NULL, &nb);
}
+
+ /* Follow \newpara with an empty block so that
+ * all paragraphs have at least one SCBlock */
+ sc_block_append(nb, NULL, NULL, strdup(""),
+ NULL);
+
+ /* Add any text after the \n */
if ( strlen(npos+1) > 0 ) {
- sc_block_append(nb, NULL, NULL, strdup(npos+1), &nb);
+ sc_block_append(nb, NULL, NULL,
+ strdup(npos+1), &nb);
}
npos[0] = '\0';
}