From 723d65d543161171f01ca6d28129bb70b7e6ec99 Mon Sep 17 00:00:00 2001 From: Simon Tatham Date: Thu, 25 Oct 2001 12:48:13 +0000 Subject: Fixes to allow the text back end to wrap long headings. (These sound like a typographical Bad Plan, but they work OK in some types of document such as a FAQ.) [originally from svn r1324] --- bk_text.c | 60 ++++++++++++++++++++++++++++++++++++++---------------------- buttress.h | 2 ++ misc.c | 3 +++ 3 files changed, 43 insertions(+), 22 deletions(-) diff --git a/bk_text.c b/bk_text.c index fb66dc6..73c4cea 100644 --- a/bk_text.c +++ b/bk_text.c @@ -458,42 +458,58 @@ static void text_heading(FILE *fp, word *tprefix, word *nprefix, word *text, alignstruct align, int indent, int width) { rdstringc t = { 0, 0, NULL }; int margin, length; + int firstlinewidth, wrapwidth; + wrappedline *wrapping, *p; if (align.just_numbers && nprefix) { text_rdaddwc(&t, nprefix, NULL); rdaddc(&t, ' '); /* FIXME: as below */ - margin = strlen(t.text); } else if (!align.just_numbers && tprefix) { text_rdaddwc(&t, tprefix, NULL); rdaddsc(&t, ": "); /* FIXME: configurability */ - margin = strlen(t.text); } - text_rdaddwc(&t, text, NULL); - - length = strlen(t.text); + margin = length = (t.text ? strlen(t.text) : 0); - if (align.align == LEFTPLUS) + if (align.align == LEFTPLUS) { margin = indent - margin; - else if (align.align == LEFT) - margin = 0; - else if (align.align == CENTRE) { - margin = (indent + width - length)/2; if (margin < 0) margin = 0; + firstlinewidth = indent + width - margin - length; + wrapwidth = width; + } else if (align.align == LEFT || align.align == CENTRE) { + margin = 0; + firstlinewidth = indent + width - length; + wrapwidth = indent + width; } - fprintf(fp, "%*s%s\n", margin, "", t.text); - if (align.underline != L'\0') { - char *u, uc; - wchar_t uw[2]; - uw[0] = align.underline; uw[1] = L'\0'; - text_convert(uw, &u); - uc = u[0]; - sfree(u); - fprintf(fp, "%*s", margin, ""); - while (length--) - putc(uc, fp); - putc('\n', fp); + wrapping = wrap_para(text, firstlinewidth, wrapwidth, text_width); + for (p = wrapping; p; p = p->next) { + text_rdaddwc(&t, p->begin, p->end); + length = (t.text ? strlen(t.text) : 0); + if (align.align == CENTRE) { + margin = (indent + width - length)/2; + if (margin < 0) margin = 0; + } + fprintf(fp, "%*s%s\n", margin, "", t.text); + if (align.underline != L'\0') { + char *u, uc; + wchar_t uw[2]; + uw[0] = align.underline; uw[1] = L'\0'; + text_convert(uw, &u); + uc = u[0]; + sfree(u); + fprintf(fp, "%*s", margin, ""); + while (length--) + putc(uc, fp); + putc('\n', fp); + } + if (align.align == LEFTPLUS) + margin = indent; + else + margin = 0; + sfree(t.text); + t = empty_rdstringc; } + wrap_free(wrapping); putc('\n', fp); sfree(t.text); diff --git a/buttress.h b/buttress.h index 934cca1..b07cf0a 100644 --- a/buttress.h +++ b/buttress.h @@ -279,6 +279,8 @@ struct tagRdstringc { int pos, size; char *text; }; +extern const rdstring empty_rdstring; +extern const rdstringc empty_rdstringc; void rdadd(rdstring *rs, wchar_t c); void rdadds(rdstring *rs, wchar_t *p); wchar_t *rdtrim(rdstring *rs); diff --git a/misc.c b/misc.c index 7a1c59d..4243954 100644 --- a/misc.c +++ b/misc.c @@ -44,6 +44,9 @@ void *stk_pop(stack s) { /* * Small routines to amalgamate a string from an input source. */ +const rdstring empty_rdstring = {0, 0, NULL}; +const rdstringc empty_rdstringc = {0, 0, NULL}; + void rdadd(rdstring *rs, wchar_t c) { if (rs->pos >= rs->size-1) { rs->size = rs->pos + 128; -- cgit v1.1