From e6d1719915ce2f572e471fea82358f606b7fd586 Mon Sep 17 00:00:00 2001 From: Kristaps Dzonsons Date: Wed, 15 Sep 2010 14:36:16 +0000 Subject: Allow string lengths to account for escapes. Now all calls to calculate column width in -Tascii, -Tpdf, and -Tps will account for "more real" string lengths. Example: .Bl -tag -width \s[+123424]foo .It bar baz .El The size escape will be correctly tossed. .Bl -tag -width \(aqbar .It \(aqbar baz .El The \(aq will be correctly handled. --- term.c | 50 +++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 43 insertions(+), 7 deletions(-) (limited to 'term.c') diff --git a/term.c b/term.c index 475ee343..71ab3a35 100644 --- a/term.c +++ b/term.c @@ -1,4 +1,4 @@ -/* $Id: term.c,v 1.170 2010/09/04 20:18:53 kristaps Exp $ */ +/* $Id: term.c,v 1.171 2010/09/15 14:36:16 kristaps Exp $ */ /* * Copyright (c) 2008, 2009, 2010 Kristaps Dzonsons * Copyright (c) 2010 Ingo Schwarze @@ -458,7 +458,6 @@ void term_word(struct termp *p, const char *word) { const char *sv, *seq; - int sz; size_t ssz; enum roffdeco deco; @@ -515,7 +514,7 @@ term_word(struct termp *p, const char *word) continue; seq = ++word; - sz = a2roffdeco(&deco, &seq, &ssz); + word += a2roffdeco(&deco, &seq, &ssz); switch (deco) { case (DECO_RESERVED): @@ -542,7 +541,6 @@ term_word(struct termp *p, const char *word) break; } - word += sz; if (DECO_NOSPACE == deco && '\0' == *word) p->flags |= TERMP_NOSPACE; } @@ -645,10 +643,48 @@ term_len(const struct termp *p, size_t sz) size_t term_strlen(const struct termp *p, const char *cp) { - size_t sz; + size_t sz, ssz, rsz, i; + enum roffdeco d; + const char *seq, *rhs; - for (sz = 0; *cp; cp++) - sz += (*p->width)(p, *cp); + for (sz = 0; '\0' != *cp; ) + /* + * Account for escaped sequences within string length + * calculations. This follows the logic in term_word() + * as we must calculate the width of produced strings. + */ + if ('\\' == *cp) { + seq = ++cp; + cp += a2roffdeco(&d, &seq, &ssz); + + switch (d) { + case (DECO_RESERVED): + rhs = chars_res2str + (p->symtab, seq, ssz, &rsz); + break; + case (DECO_SPECIAL): + /* FALLTHROUGH */ + case (DECO_SSPECIAL): + rhs = chars_spec2str + (p->symtab, seq, ssz, &rsz); + + /* Allow for one-char escapes. */ + if (DECO_SSPECIAL != d || rhs) + break; + + rhs = seq; + rsz = ssz; + break; + default: + rhs = NULL; + break; + } + + if (rhs) + for (i = 0; i < rsz; i++) + sz += (*p->width)(p, *rhs++); + } else + sz += (*p->width)(p, *cp++); return(sz); } -- cgit v1.2.3-56-ge451