2021-11-15 15:35:39 +00:00
|
|
|
/* $Id: term.c,v 1.283 2021/08/10 12:55:04 schwarze Exp $ */
|
2012-10-18 09:55:16 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
|
2021-11-15 15:35:39 +00:00
|
|
|
* Copyright (c) 2010-2020 Ingo Schwarze <schwarze@openbsd.org>
|
2012-10-18 09:55:16 +00:00
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
2016-01-15 23:08:59 +00:00
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES
|
2012-10-18 09:55:16 +00:00
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
2016-01-15 23:08:59 +00:00
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
|
2012-10-18 09:55:16 +00:00
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*/
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <ctype.h>
|
2019-03-29 13:13:30 +00:00
|
|
|
#include <stdint.h>
|
2012-10-18 09:55:16 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "mandoc.h"
|
2014-11-22 18:08:25 +00:00
|
|
|
#include "mandoc_aux.h"
|
2012-10-18 09:55:16 +00:00
|
|
|
#include "out.h"
|
|
|
|
#include "term.h"
|
|
|
|
#include "main.h"
|
|
|
|
|
2014-01-31 19:59:03 +00:00
|
|
|
static size_t cond_width(const struct termp *, int, int *);
|
2017-06-08 19:29:07 +00:00
|
|
|
static void adjbuf(struct termp_col *, size_t);
|
2012-10-18 09:55:16 +00:00
|
|
|
static void bufferc(struct termp *, char);
|
|
|
|
static void encode(struct termp *, const char *, size_t);
|
|
|
|
static void encode1(struct termp *, int);
|
2017-06-08 19:29:07 +00:00
|
|
|
static void endline(struct termp *);
|
2021-11-15 15:35:39 +00:00
|
|
|
static void term_field(struct termp *, size_t, size_t);
|
2019-03-29 13:13:30 +00:00
|
|
|
static void term_fill(struct termp *, size_t *, size_t *,
|
|
|
|
size_t);
|
2012-10-18 09:55:16 +00:00
|
|
|
|
2014-11-22 18:08:25 +00:00
|
|
|
|
2017-07-31 19:17:54 +00:00
|
|
|
void
|
|
|
|
term_setcol(struct termp *p, size_t maxtcol)
|
|
|
|
{
|
|
|
|
if (maxtcol > p->maxtcol) {
|
|
|
|
p->tcols = mandoc_recallocarray(p->tcols,
|
|
|
|
p->maxtcol, maxtcol, sizeof(*p->tcols));
|
|
|
|
p->maxtcol = maxtcol;
|
|
|
|
}
|
|
|
|
p->lasttcol = maxtcol - 1;
|
|
|
|
p->tcol = p->tcols;
|
|
|
|
}
|
|
|
|
|
2012-10-18 09:55:16 +00:00
|
|
|
void
|
|
|
|
term_free(struct termp *p)
|
|
|
|
{
|
2017-06-08 19:29:07 +00:00
|
|
|
for (p->tcol = p->tcols; p->tcol < p->tcols + p->maxtcol; p->tcol++)
|
|
|
|
free(p->tcol->buf);
|
|
|
|
free(p->tcols);
|
2015-03-02 16:45:41 +00:00
|
|
|
free(p->fontq);
|
2012-10-18 09:55:16 +00:00
|
|
|
free(p);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-11-22 18:08:25 +00:00
|
|
|
term_begin(struct termp *p, term_margin head,
|
2016-01-15 23:08:59 +00:00
|
|
|
term_margin foot, const struct roff_meta *arg)
|
2012-10-18 09:55:16 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
p->headf = head;
|
|
|
|
p->footf = foot;
|
|
|
|
p->argf = arg;
|
|
|
|
(*p->begin)(p);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
term_end(struct termp *p)
|
|
|
|
{
|
|
|
|
|
|
|
|
(*p->end)(p);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2014-11-22 18:08:25 +00:00
|
|
|
* Flush a chunk of text. By default, break the output line each time
|
|
|
|
* the right margin is reached, and continue output on the next line
|
|
|
|
* at the same offset as the chunk itself. By default, also break the
|
2019-03-29 13:13:30 +00:00
|
|
|
* output line at the end of the chunk. There are many flags modifying
|
|
|
|
* this behaviour, see the comments in the body of the function.
|
2012-10-18 09:55:16 +00:00
|
|
|
*/
|
|
|
|
void
|
|
|
|
term_flushln(struct termp *p)
|
|
|
|
{
|
2019-03-29 13:13:30 +00:00
|
|
|
size_t vbl; /* Number of blanks to prepend to the output. */
|
|
|
|
size_t vbr; /* Actual visual position of the end of field. */
|
|
|
|
size_t vfield; /* Desired visual field width. */
|
|
|
|
size_t vtarget; /* Desired visual position of the right margin. */
|
|
|
|
size_t ic; /* Character position in the input buffer. */
|
|
|
|
size_t nbr; /* Number of characters to print in this field. */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Normally, start writing at the left margin, but with the
|
|
|
|
* NOPAD flag, start writing at the current position instead.
|
|
|
|
*/
|
2012-10-18 09:55:16 +00:00
|
|
|
|
2017-06-08 19:29:07 +00:00
|
|
|
vbl = (p->flags & TERMP_NOPAD) || p->tcol->offset < p->viscol ?
|
|
|
|
0 : p->tcol->offset - p->viscol;
|
|
|
|
if (p->minbl && vbl < p->minbl)
|
|
|
|
vbl = p->minbl;
|
2012-10-18 09:55:16 +00:00
|
|
|
|
2017-07-31 19:17:54 +00:00
|
|
|
if ((p->flags & TERMP_MULTICOL) == 0)
|
2017-06-08 19:29:07 +00:00
|
|
|
p->tcol->col = 0;
|
2019-03-29 13:13:30 +00:00
|
|
|
|
|
|
|
/* Loop over output lines. */
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
vfield = p->tcol->rmargin > p->viscol + vbl ?
|
|
|
|
p->tcol->rmargin - p->viscol - vbl : 0;
|
2017-06-08 19:29:07 +00:00
|
|
|
|
2012-10-18 09:55:16 +00:00
|
|
|
/*
|
2019-03-29 13:13:30 +00:00
|
|
|
* Normally, break the line at the the right margin
|
|
|
|
* of the field, but with the NOBREAK flag, only
|
|
|
|
* break it at the max right margin of the screen,
|
|
|
|
* and with the BRNEVER flag, never break it at all.
|
2012-10-18 09:55:16 +00:00
|
|
|
*/
|
2017-06-08 19:29:07 +00:00
|
|
|
|
2021-11-15 15:35:39 +00:00
|
|
|
vtarget = (p->flags & TERMP_NOBREAK) == 0 ? vfield :
|
2019-03-29 13:13:30 +00:00
|
|
|
p->maxrmargin > p->viscol + vbl ?
|
|
|
|
p->maxrmargin - p->viscol - vbl : 0;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Figure out how much text will fit in the field.
|
|
|
|
* If there is whitespace only, print nothing.
|
|
|
|
*/
|
|
|
|
|
2021-11-15 15:35:39 +00:00
|
|
|
term_fill(p, &nbr, &vbr,
|
|
|
|
p->flags & TERMP_BRNEVER ? SIZE_MAX : vtarget);
|
2019-03-29 13:13:30 +00:00
|
|
|
if (nbr == 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* With the CENTER or RIGHT flag, increase the indentation
|
|
|
|
* to center the text between the left and right margins
|
|
|
|
* or to adjust it to the right margin, respectively.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (vbr < vtarget) {
|
|
|
|
if (p->flags & TERMP_CENTER)
|
|
|
|
vbl += (vtarget - vbr) / 2;
|
|
|
|
else if (p->flags & TERMP_RIGHT)
|
|
|
|
vbl += vtarget - vbr;
|
2012-10-18 09:55:16 +00:00
|
|
|
}
|
|
|
|
|
2019-03-29 13:13:30 +00:00
|
|
|
/* Finally, print the field content. */
|
|
|
|
|
2021-11-15 15:35:39 +00:00
|
|
|
term_field(p, vbl, nbr);
|
2019-03-29 13:13:30 +00:00
|
|
|
|
2012-10-18 09:55:16 +00:00
|
|
|
/*
|
2019-03-29 13:13:30 +00:00
|
|
|
* If there is no text left in the field, exit the loop.
|
|
|
|
* If the BRTRSP flag is set, consider trailing
|
|
|
|
* whitespace significant when deciding whether
|
|
|
|
* the field fits or not.
|
2012-10-18 09:55:16 +00:00
|
|
|
*/
|
|
|
|
|
2019-03-29 13:13:30 +00:00
|
|
|
for (ic = p->tcol->col; ic < p->tcol->lastcol; ic++) {
|
|
|
|
switch (p->tcol->buf[ic]) {
|
|
|
|
case '\t':
|
|
|
|
if (p->flags & TERMP_BRTRSP)
|
|
|
|
vbr = term_tab_next(vbr);
|
2017-07-31 19:17:54 +00:00
|
|
|
continue;
|
2019-03-29 13:13:30 +00:00
|
|
|
case ' ':
|
|
|
|
if (p->flags & TERMP_BRTRSP)
|
|
|
|
vbr += (*p->width)(p, ' ');
|
|
|
|
continue;
|
|
|
|
case '\n':
|
|
|
|
case ASCII_BREAK:
|
2012-10-18 09:55:16 +00:00
|
|
|
continue;
|
2019-03-29 13:13:30 +00:00
|
|
|
default:
|
|
|
|
break;
|
2012-10-18 09:55:16 +00:00
|
|
|
}
|
2019-03-29 13:13:30 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (ic == p->tcol->lastcol)
|
|
|
|
break;
|
2012-10-18 09:55:16 +00:00
|
|
|
|
2019-03-29 13:13:30 +00:00
|
|
|
/*
|
|
|
|
* At the location of an automtic line break, input
|
|
|
|
* space characters are consumed by the line break.
|
|
|
|
*/
|
2012-10-18 09:55:16 +00:00
|
|
|
|
2019-03-29 13:13:30 +00:00
|
|
|
while (p->tcol->col < p->tcol->lastcol &&
|
|
|
|
p->tcol->buf[p->tcol->col] == ' ')
|
|
|
|
p->tcol->col++;
|
2014-11-22 18:08:25 +00:00
|
|
|
|
2019-03-29 13:13:30 +00:00
|
|
|
/*
|
|
|
|
* In multi-column mode, leave the rest of the text
|
|
|
|
* in the buffer to be handled by a subsequent
|
|
|
|
* invocation, such that the other columns of the
|
|
|
|
* table can be handled first.
|
|
|
|
* In single-column mode, simply break the line.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (p->flags & TERMP_MULTICOL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
endline(p);
|
|
|
|
p->viscol = 0;
|
2012-10-18 09:55:16 +00:00
|
|
|
|
|
|
|
/*
|
2019-03-29 13:13:30 +00:00
|
|
|
* Normally, start the next line at the same indentation
|
|
|
|
* as this one, but with the BRIND flag, start it at the
|
|
|
|
* right margin instead. This is used together with
|
|
|
|
* NOBREAK for the tags in various kinds of tagged lists.
|
2012-10-18 09:55:16 +00:00
|
|
|
*/
|
2017-06-08 19:29:07 +00:00
|
|
|
|
2019-03-29 13:13:30 +00:00
|
|
|
vbl = p->flags & TERMP_BRIND ?
|
|
|
|
p->tcol->rmargin : p->tcol->offset;
|
|
|
|
}
|
2017-06-08 19:29:07 +00:00
|
|
|
|
2019-03-29 13:13:30 +00:00
|
|
|
/* Reset output state in preparation for the next field. */
|
2012-10-18 09:55:16 +00:00
|
|
|
|
2019-03-29 13:13:30 +00:00
|
|
|
p->col = p->tcol->col = p->tcol->lastcol = 0;
|
|
|
|
p->minbl = p->trailspace;
|
|
|
|
p->flags &= ~(TERMP_BACKAFTER | TERMP_BACKBEFORE | TERMP_NOPAD);
|
2014-01-31 19:59:03 +00:00
|
|
|
|
2019-03-29 13:13:30 +00:00
|
|
|
if (p->flags & TERMP_MULTICOL)
|
|
|
|
return;
|
2014-01-31 19:59:03 +00:00
|
|
|
|
2019-03-29 13:13:30 +00:00
|
|
|
/*
|
|
|
|
* The HANG flag means that the next field
|
|
|
|
* always follows on the same line.
|
|
|
|
* The NOBREAK flag means that the next field
|
|
|
|
* follows on the same line unless the field was overrun.
|
|
|
|
* Normally, break the line at the end of each field.
|
|
|
|
*/
|
2012-10-18 09:55:16 +00:00
|
|
|
|
2019-03-29 13:13:30 +00:00
|
|
|
if ((p->flags & TERMP_HANG) == 0 &&
|
|
|
|
((p->flags & TERMP_NOBREAK) == 0 ||
|
|
|
|
vbr + term_len(p, p->trailspace) > vfield))
|
|
|
|
endline(p);
|
|
|
|
}
|
2012-10-18 09:55:16 +00:00
|
|
|
|
2019-03-29 13:13:30 +00:00
|
|
|
/*
|
|
|
|
* Store the number of input characters to print in this field in *nbr
|
|
|
|
* and their total visual width to print in *vbr.
|
|
|
|
* If there is only whitespace in the field, both remain zero.
|
|
|
|
* The desired visual width of the field is provided by vtarget.
|
|
|
|
* If the first word is longer, the field will be overrun.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
term_fill(struct termp *p, size_t *nbr, size_t *vbr, size_t vtarget)
|
|
|
|
{
|
|
|
|
size_t ic; /* Character position in the input buffer. */
|
|
|
|
size_t vis; /* Visual position of the current character. */
|
|
|
|
size_t vn; /* Visual position of the next character. */
|
|
|
|
int breakline; /* Break at the end of this word. */
|
|
|
|
int graph; /* Last character was non-blank. */
|
|
|
|
|
|
|
|
*nbr = *vbr = vis = 0;
|
|
|
|
breakline = graph = 0;
|
|
|
|
for (ic = p->tcol->col; ic < p->tcol->lastcol; ic++) {
|
|
|
|
switch (p->tcol->buf[ic]) {
|
|
|
|
case '\b': /* Escape \o (overstrike) or backspace markup. */
|
|
|
|
assert(ic > 0);
|
|
|
|
vis -= (*p->width)(p, p->tcol->buf[ic - 1]);
|
|
|
|
continue;
|
2017-06-08 19:29:07 +00:00
|
|
|
|
2019-03-29 13:13:30 +00:00
|
|
|
case '\t': /* Normal ASCII whitespace. */
|
|
|
|
case ' ':
|
|
|
|
case ASCII_BREAK: /* Escape \: (breakpoint). */
|
|
|
|
switch (p->tcol->buf[ic]) {
|
|
|
|
case '\t':
|
|
|
|
vn = term_tab_next(vis);
|
2012-10-18 09:55:16 +00:00
|
|
|
break;
|
2019-03-29 13:13:30 +00:00
|
|
|
case ' ':
|
|
|
|
vn = vis + (*p->width)(p, ' ');
|
2012-10-18 09:55:16 +00:00
|
|
|
break;
|
2019-03-29 13:13:30 +00:00
|
|
|
case ASCII_BREAK:
|
|
|
|
vn = vis;
|
2012-10-18 09:55:16 +00:00
|
|
|
break;
|
2019-07-26 09:51:35 +00:00
|
|
|
default:
|
|
|
|
abort();
|
2012-10-18 09:55:16 +00:00
|
|
|
}
|
2019-03-29 13:13:30 +00:00
|
|
|
/* Can break at the end of a word. */
|
|
|
|
if (breakline || vn > vtarget)
|
|
|
|
break;
|
|
|
|
if (graph) {
|
|
|
|
*nbr = ic;
|
|
|
|
*vbr = vis;
|
|
|
|
graph = 0;
|
2012-10-18 09:55:16 +00:00
|
|
|
}
|
2019-03-29 13:13:30 +00:00
|
|
|
vis = vn;
|
|
|
|
continue;
|
2012-10-18 09:55:16 +00:00
|
|
|
|
2019-03-29 13:13:30 +00:00
|
|
|
case '\n': /* Escape \p (break at the end of the word). */
|
|
|
|
breakline = 1;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case ASCII_HYPH: /* Breakable hyphen. */
|
|
|
|
graph = 1;
|
2012-10-18 09:55:16 +00:00
|
|
|
/*
|
2019-03-29 13:13:30 +00:00
|
|
|
* We are about to decide whether to break the
|
|
|
|
* line or not, so we no longer need this hyphen
|
|
|
|
* to be marked as breakable. Put back a real
|
|
|
|
* hyphen such that we get the correct width.
|
2012-10-18 09:55:16 +00:00
|
|
|
*/
|
2019-03-29 13:13:30 +00:00
|
|
|
p->tcol->buf[ic] = '-';
|
|
|
|
vis += (*p->width)(p, '-');
|
|
|
|
if (vis > vtarget) {
|
|
|
|
ic++;
|
|
|
|
break;
|
2012-10-18 09:55:16 +00:00
|
|
|
}
|
2019-03-29 13:13:30 +00:00
|
|
|
*nbr = ic + 1;
|
|
|
|
*vbr = vis;
|
2017-07-31 19:17:54 +00:00
|
|
|
continue;
|
|
|
|
|
2019-03-29 13:13:30 +00:00
|
|
|
case ASCII_NBRSP: /* Non-breakable space. */
|
|
|
|
p->tcol->buf[ic] = ' ';
|
|
|
|
/* FALLTHROUGH */
|
|
|
|
default: /* Printable character. */
|
|
|
|
graph = 1;
|
|
|
|
vis += (*p->width)(p, p->tcol->buf[ic]);
|
|
|
|
if (vis > vtarget && *nbr > 0)
|
|
|
|
return;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
break;
|
2012-10-18 09:55:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2019-03-29 13:13:30 +00:00
|
|
|
* If the last word extends to the end of the field without any
|
|
|
|
* trailing whitespace, the loop could not check yet whether it
|
|
|
|
* can remain on this line. So do the check now.
|
2012-10-18 09:55:16 +00:00
|
|
|
*/
|
2017-06-08 19:29:07 +00:00
|
|
|
|
2019-03-29 13:13:30 +00:00
|
|
|
if (graph && (vis <= vtarget || *nbr == 0)) {
|
|
|
|
*nbr = ic;
|
|
|
|
*vbr = vis;
|
|
|
|
}
|
|
|
|
}
|
2012-10-18 09:55:16 +00:00
|
|
|
|
2019-03-29 13:13:30 +00:00
|
|
|
/*
|
|
|
|
* Print the contents of one field
|
|
|
|
* with an indentation of vbl visual columns,
|
2021-11-15 15:35:39 +00:00
|
|
|
* and an input string length of nbr characters.
|
2019-03-29 13:13:30 +00:00
|
|
|
*/
|
|
|
|
static void
|
2021-11-15 15:35:39 +00:00
|
|
|
term_field(struct termp *p, size_t vbl, size_t nbr)
|
2019-03-29 13:13:30 +00:00
|
|
|
{
|
|
|
|
size_t ic; /* Character position in the input buffer. */
|
|
|
|
size_t vis; /* Visual position of the current character. */
|
|
|
|
size_t dv; /* Visual width of the current character. */
|
|
|
|
size_t vn; /* Visual position of the next character. */
|
2012-10-18 09:55:16 +00:00
|
|
|
|
2019-03-29 13:13:30 +00:00
|
|
|
vis = 0;
|
|
|
|
for (ic = p->tcol->col; ic < nbr; ic++) {
|
|
|
|
|
|
|
|
/*
|
|
|
|
* To avoid the printing of trailing whitespace,
|
|
|
|
* do not print whitespace right away, only count it.
|
|
|
|
*/
|
2017-07-31 19:17:54 +00:00
|
|
|
|
2019-03-29 13:13:30 +00:00
|
|
|
switch (p->tcol->buf[ic]) {
|
|
|
|
case '\n':
|
|
|
|
case ASCII_BREAK:
|
|
|
|
continue;
|
|
|
|
case '\t':
|
|
|
|
vn = term_tab_next(vis);
|
|
|
|
vbl += vn - vis;
|
|
|
|
vis = vn;
|
|
|
|
continue;
|
|
|
|
case ' ':
|
|
|
|
case ASCII_NBRSP:
|
|
|
|
dv = (*p->width)(p, ' ');
|
|
|
|
vbl += dv;
|
|
|
|
vis += dv;
|
|
|
|
continue;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2017-06-08 19:29:07 +00:00
|
|
|
|
2019-03-29 13:13:30 +00:00
|
|
|
/*
|
|
|
|
* We found a non-blank character to print,
|
|
|
|
* so write preceding white space now.
|
|
|
|
*/
|
2016-01-15 23:08:59 +00:00
|
|
|
|
2019-03-29 13:13:30 +00:00
|
|
|
if (vbl > 0) {
|
|
|
|
(*p->advance)(p, vbl);
|
|
|
|
p->viscol += vbl;
|
|
|
|
vbl = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Print the character and adjust the visual position. */
|
|
|
|
|
|
|
|
(*p->letter)(p, p->tcol->buf[ic]);
|
|
|
|
if (p->tcol->buf[ic] == '\b') {
|
|
|
|
dv = (*p->width)(p, p->tcol->buf[ic - 1]);
|
|
|
|
p->viscol -= dv;
|
|
|
|
vis -= dv;
|
|
|
|
} else {
|
|
|
|
dv = (*p->width)(p, p->tcol->buf[ic]);
|
|
|
|
p->viscol += dv;
|
|
|
|
vis += dv;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
p->tcol->col = nbr;
|
2017-06-08 19:29:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
endline(struct termp *p)
|
|
|
|
{
|
|
|
|
if ((p->flags & (TERMP_NEWMC | TERMP_ENDMC)) == TERMP_ENDMC) {
|
|
|
|
p->mc = NULL;
|
|
|
|
p->flags &= ~TERMP_ENDMC;
|
|
|
|
}
|
|
|
|
if (p->mc != NULL) {
|
|
|
|
if (p->viscol && p->maxrmargin >= p->viscol)
|
|
|
|
(*p->advance)(p, p->maxrmargin - p->viscol + 1);
|
|
|
|
p->flags |= TERMP_NOBUF | TERMP_NOSPACE;
|
|
|
|
term_word(p, p->mc);
|
|
|
|
p->flags &= ~(TERMP_NOBUF | TERMP_NEWMC);
|
2012-10-18 09:55:16 +00:00
|
|
|
}
|
2017-06-08 19:29:07 +00:00
|
|
|
p->viscol = 0;
|
|
|
|
p->minbl = 0;
|
|
|
|
(*p->endline)(p);
|
2012-10-18 09:55:16 +00:00
|
|
|
}
|
|
|
|
|
2014-11-22 18:08:25 +00:00
|
|
|
/*
|
2012-10-18 09:55:16 +00:00
|
|
|
* A newline only breaks an existing line; it won't assert vertical
|
|
|
|
* space. All data in the output buffer is flushed prior to the newline
|
|
|
|
* assertion.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
term_newln(struct termp *p)
|
|
|
|
{
|
|
|
|
|
|
|
|
p->flags |= TERMP_NOSPACE;
|
2017-07-31 19:17:54 +00:00
|
|
|
if (p->tcol->lastcol || p->viscol)
|
2012-10-18 09:55:16 +00:00
|
|
|
term_flushln(p);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Asserts a vertical space (a full, empty line-break between lines).
|
|
|
|
* Note that if used twice, this will cause two blank spaces and so on.
|
|
|
|
* All data in the output buffer is flushed prior to the newline
|
|
|
|
* assertion.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
term_vspace(struct termp *p)
|
|
|
|
{
|
|
|
|
|
|
|
|
term_newln(p);
|
|
|
|
p->viscol = 0;
|
2017-06-08 19:29:07 +00:00
|
|
|
p->minbl = 0;
|
2014-01-31 19:59:03 +00:00
|
|
|
if (0 < p->skipvsp)
|
|
|
|
p->skipvsp--;
|
|
|
|
else
|
|
|
|
(*p->endline)(p);
|
2012-10-18 09:55:16 +00:00
|
|
|
}
|
|
|
|
|
2015-03-02 16:45:41 +00:00
|
|
|
/* Swap current and previous font; for \fP and .ft P */
|
2012-10-18 09:55:16 +00:00
|
|
|
void
|
|
|
|
term_fontlast(struct termp *p)
|
|
|
|
{
|
|
|
|
enum termfont f;
|
|
|
|
|
|
|
|
f = p->fontl;
|
|
|
|
p->fontl = p->fontq[p->fonti];
|
|
|
|
p->fontq[p->fonti] = f;
|
|
|
|
}
|
|
|
|
|
2015-03-02 16:45:41 +00:00
|
|
|
/* Set font, save current, discard previous; for \f, .ft, .B etc. */
|
2012-10-18 09:55:16 +00:00
|
|
|
void
|
|
|
|
term_fontrepl(struct termp *p, enum termfont f)
|
|
|
|
{
|
|
|
|
|
|
|
|
p->fontl = p->fontq[p->fonti];
|
|
|
|
p->fontq[p->fonti] = f;
|
|
|
|
}
|
|
|
|
|
2015-03-02 16:45:41 +00:00
|
|
|
/* Set font, save previous. */
|
2012-10-18 09:55:16 +00:00
|
|
|
void
|
|
|
|
term_fontpush(struct termp *p, enum termfont f)
|
|
|
|
{
|
|
|
|
|
|
|
|
p->fontl = p->fontq[p->fonti];
|
2015-03-02 16:45:41 +00:00
|
|
|
if (++p->fonti == p->fontsz) {
|
|
|
|
p->fontsz += 8;
|
|
|
|
p->fontq = mandoc_reallocarray(p->fontq,
|
2016-01-15 23:08:59 +00:00
|
|
|
p->fontsz, sizeof(*p->fontq));
|
2015-03-02 16:45:41 +00:00
|
|
|
}
|
|
|
|
p->fontq[p->fonti] = f;
|
2012-10-18 09:55:16 +00:00
|
|
|
}
|
|
|
|
|
2015-03-02 16:45:41 +00:00
|
|
|
/* Flush to make the saved pointer current again. */
|
2012-10-18 09:55:16 +00:00
|
|
|
void
|
2015-03-02 16:45:41 +00:00
|
|
|
term_fontpopq(struct termp *p, int i)
|
2012-10-18 09:55:16 +00:00
|
|
|
{
|
|
|
|
|
2015-03-02 16:45:41 +00:00
|
|
|
assert(i >= 0);
|
|
|
|
if (p->fonti > i)
|
|
|
|
p->fonti = i;
|
2012-10-18 09:55:16 +00:00
|
|
|
}
|
|
|
|
|
2015-03-02 16:45:41 +00:00
|
|
|
/* Pop one font off the stack. */
|
2012-10-18 09:55:16 +00:00
|
|
|
void
|
|
|
|
term_fontpop(struct termp *p)
|
|
|
|
{
|
|
|
|
|
|
|
|
assert(p->fonti);
|
|
|
|
p->fonti--;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Handle pwords, partial words, which may be either a single word or a
|
|
|
|
* phrase that cannot be broken down (such as a literal string). This
|
|
|
|
* handles word styling.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
term_word(struct termp *p, const char *word)
|
|
|
|
{
|
2017-06-08 19:29:07 +00:00
|
|
|
struct roffsu su;
|
2014-01-31 19:59:03 +00:00
|
|
|
const char nbrsp[2] = { ASCII_NBRSP, 0 };
|
2012-10-18 09:55:16 +00:00
|
|
|
const char *seq, *cp;
|
|
|
|
int sz, uc;
|
2017-06-08 19:29:07 +00:00
|
|
|
size_t csz, lsz, ssz;
|
2012-10-18 09:55:16 +00:00
|
|
|
enum mandoc_esc esc;
|
|
|
|
|
2017-06-08 19:29:07 +00:00
|
|
|
if ((p->flags & TERMP_NOBUF) == 0) {
|
|
|
|
if ((p->flags & TERMP_NOSPACE) == 0) {
|
|
|
|
if ((p->flags & TERMP_KEEP) == 0) {
|
2012-10-18 09:55:16 +00:00
|
|
|
bufferc(p, ' ');
|
2017-06-08 19:29:07 +00:00
|
|
|
if (p->flags & TERMP_SENTENCE)
|
|
|
|
bufferc(p, ' ');
|
|
|
|
} else
|
|
|
|
bufferc(p, ASCII_NBRSP);
|
|
|
|
}
|
|
|
|
if (p->flags & TERMP_PREKEEP)
|
|
|
|
p->flags |= TERMP_KEEP;
|
|
|
|
if (p->flags & TERMP_NONOSPACE)
|
|
|
|
p->flags |= TERMP_NOSPACE;
|
|
|
|
else
|
|
|
|
p->flags &= ~TERMP_NOSPACE;
|
|
|
|
p->flags &= ~(TERMP_SENTENCE | TERMP_NONEWLINE);
|
|
|
|
p->skipvsp = 0;
|
2012-10-18 09:55:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
while ('\0' != *word) {
|
2014-01-31 19:59:03 +00:00
|
|
|
if ('\\' != *word) {
|
|
|
|
if (TERMP_NBRWORD & p->flags) {
|
|
|
|
if (' ' == *word) {
|
|
|
|
encode(p, nbrsp, 1);
|
|
|
|
word++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
ssz = strcspn(word, "\\ ");
|
|
|
|
} else
|
|
|
|
ssz = strcspn(word, "\\");
|
2012-10-18 09:55:16 +00:00
|
|
|
encode(p, word, ssz);
|
2014-01-31 19:59:03 +00:00
|
|
|
word += (int)ssz;
|
2012-10-18 09:55:16 +00:00
|
|
|
continue;
|
2014-01-31 19:59:03 +00:00
|
|
|
}
|
2012-10-18 09:55:16 +00:00
|
|
|
|
|
|
|
word++;
|
|
|
|
esc = mandoc_escape(&word, &seq, &sz);
|
|
|
|
switch (esc) {
|
2014-11-22 18:08:25 +00:00
|
|
|
case ESCAPE_UNICODE:
|
2014-12-02 07:34:06 +00:00
|
|
|
uc = mchars_num2uc(seq + 1, sz - 1);
|
2012-10-18 09:55:16 +00:00
|
|
|
break;
|
2014-11-22 18:08:25 +00:00
|
|
|
case ESCAPE_NUMBERED:
|
2014-12-02 07:34:06 +00:00
|
|
|
uc = mchars_num2char(seq, sz);
|
|
|
|
if (uc < 0)
|
|
|
|
continue;
|
2012-10-18 09:55:16 +00:00
|
|
|
break;
|
2014-11-22 18:08:25 +00:00
|
|
|
case ESCAPE_SPECIAL:
|
2014-12-02 07:34:06 +00:00
|
|
|
if (p->enc == TERMENC_ASCII) {
|
2016-01-15 23:08:59 +00:00
|
|
|
cp = mchars_spec2str(seq, sz, &ssz);
|
2014-12-02 07:34:06 +00:00
|
|
|
if (cp != NULL)
|
|
|
|
encode(p, cp, ssz);
|
|
|
|
} else {
|
2016-01-15 23:08:59 +00:00
|
|
|
uc = mchars_spec2cp(seq, sz);
|
2014-12-02 07:34:06 +00:00
|
|
|
if (uc > 0)
|
|
|
|
encode1(p, uc);
|
|
|
|
}
|
|
|
|
continue;
|
2019-03-29 13:13:30 +00:00
|
|
|
case ESCAPE_UNDEF:
|
|
|
|
uc = *seq;
|
|
|
|
break;
|
2014-11-22 18:08:25 +00:00
|
|
|
case ESCAPE_FONTBOLD:
|
2021-11-15 15:35:39 +00:00
|
|
|
case ESCAPE_FONTCB:
|
2012-10-18 09:55:16 +00:00
|
|
|
term_fontrepl(p, TERMFONT_BOLD);
|
2014-12-02 07:34:06 +00:00
|
|
|
continue;
|
2014-11-22 18:08:25 +00:00
|
|
|
case ESCAPE_FONTITALIC:
|
2021-11-15 15:35:39 +00:00
|
|
|
case ESCAPE_FONTCI:
|
2012-10-18 09:55:16 +00:00
|
|
|
term_fontrepl(p, TERMFONT_UNDER);
|
2014-12-02 07:34:06 +00:00
|
|
|
continue;
|
2014-11-22 18:08:25 +00:00
|
|
|
case ESCAPE_FONTBI:
|
2014-01-31 19:59:03 +00:00
|
|
|
term_fontrepl(p, TERMFONT_BI);
|
2014-12-02 07:34:06 +00:00
|
|
|
continue;
|
2014-11-22 18:08:25 +00:00
|
|
|
case ESCAPE_FONT:
|
2021-11-15 15:35:39 +00:00
|
|
|
case ESCAPE_FONTCR:
|
2014-11-22 18:08:25 +00:00
|
|
|
case ESCAPE_FONTROMAN:
|
2012-10-18 09:55:16 +00:00
|
|
|
term_fontrepl(p, TERMFONT_NONE);
|
2014-12-02 07:34:06 +00:00
|
|
|
continue;
|
2014-11-22 18:08:25 +00:00
|
|
|
case ESCAPE_FONTPREV:
|
2012-10-18 09:55:16 +00:00
|
|
|
term_fontlast(p);
|
2014-12-02 07:34:06 +00:00
|
|
|
continue;
|
2017-07-31 19:17:54 +00:00
|
|
|
case ESCAPE_BREAK:
|
|
|
|
bufferc(p, '\n');
|
|
|
|
continue;
|
2014-11-22 18:08:25 +00:00
|
|
|
case ESCAPE_NOSPACE:
|
2016-01-15 23:08:59 +00:00
|
|
|
if (p->flags & TERMP_BACKAFTER)
|
|
|
|
p->flags &= ~TERMP_BACKAFTER;
|
|
|
|
else if (*word == '\0')
|
2014-12-25 21:33:25 +00:00
|
|
|
p->flags |= (TERMP_NOSPACE | TERMP_NONEWLINE);
|
2014-12-02 07:34:06 +00:00
|
|
|
continue;
|
2019-03-29 13:13:30 +00:00
|
|
|
case ESCAPE_DEVICE:
|
|
|
|
if (p->type == TERMTYPE_PDF)
|
|
|
|
encode(p, "pdf", 3);
|
|
|
|
else if (p->type == TERMTYPE_PS)
|
|
|
|
encode(p, "ps", 2);
|
|
|
|
else if (p->enc == TERMENC_ASCII)
|
|
|
|
encode(p, "ascii", 5);
|
|
|
|
else
|
|
|
|
encode(p, "utf8", 4);
|
|
|
|
continue;
|
2017-06-08 19:29:07 +00:00
|
|
|
case ESCAPE_HORIZ:
|
2017-07-31 19:17:54 +00:00
|
|
|
if (*seq == '|') {
|
|
|
|
seq++;
|
|
|
|
uc = -p->col;
|
|
|
|
} else
|
|
|
|
uc = 0;
|
2017-06-08 19:29:07 +00:00
|
|
|
if (a2roffsu(seq, &su, SCALE_EM) == NULL)
|
|
|
|
continue;
|
2017-07-31 19:17:54 +00:00
|
|
|
uc += term_hen(p, &su);
|
2017-06-08 19:29:07 +00:00
|
|
|
if (uc > 0)
|
|
|
|
while (uc-- > 0)
|
|
|
|
bufferc(p, ASCII_NBRSP);
|
|
|
|
else if (p->col > (size_t)(-uc))
|
|
|
|
p->col += uc;
|
|
|
|
else {
|
|
|
|
uc += p->col;
|
|
|
|
p->col = 0;
|
|
|
|
if (p->tcol->offset > (size_t)(-uc)) {
|
|
|
|
p->ti += uc;
|
|
|
|
p->tcol->offset += uc;
|
|
|
|
} else {
|
|
|
|
p->ti -= p->tcol->offset;
|
|
|
|
p->tcol->offset = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
case ESCAPE_HLINE:
|
2017-07-31 19:17:54 +00:00
|
|
|
if ((cp = a2roffsu(seq, &su, SCALE_EM)) == NULL)
|
2017-06-08 19:29:07 +00:00
|
|
|
continue;
|
2017-07-31 19:17:54 +00:00
|
|
|
uc = term_hen(p, &su);
|
2017-06-08 19:29:07 +00:00
|
|
|
if (uc <= 0) {
|
|
|
|
if (p->tcol->rmargin <= p->tcol->offset)
|
|
|
|
continue;
|
|
|
|
lsz = p->tcol->rmargin - p->tcol->offset;
|
|
|
|
} else
|
|
|
|
lsz = uc;
|
2017-07-31 19:17:54 +00:00
|
|
|
if (*cp == seq[-1])
|
2017-06-08 19:29:07 +00:00
|
|
|
uc = -1;
|
2017-07-31 19:17:54 +00:00
|
|
|
else if (*cp == '\\') {
|
|
|
|
seq = cp + 1;
|
2017-06-08 19:29:07 +00:00
|
|
|
esc = mandoc_escape(&seq, &cp, &sz);
|
|
|
|
switch (esc) {
|
|
|
|
case ESCAPE_UNICODE:
|
|
|
|
uc = mchars_num2uc(cp + 1, sz - 1);
|
|
|
|
break;
|
|
|
|
case ESCAPE_NUMBERED:
|
|
|
|
uc = mchars_num2char(cp, sz);
|
|
|
|
break;
|
|
|
|
case ESCAPE_SPECIAL:
|
|
|
|
uc = mchars_spec2cp(cp, sz);
|
|
|
|
break;
|
2019-03-29 13:13:30 +00:00
|
|
|
case ESCAPE_UNDEF:
|
|
|
|
uc = *seq;
|
|
|
|
break;
|
2017-06-08 19:29:07 +00:00
|
|
|
default:
|
|
|
|
uc = -1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else
|
2017-07-31 19:17:54 +00:00
|
|
|
uc = *cp;
|
2017-06-08 19:29:07 +00:00
|
|
|
if (uc < 0x20 || (uc > 0x7E && uc < 0xA0))
|
|
|
|
uc = '_';
|
|
|
|
if (p->enc == TERMENC_ASCII) {
|
|
|
|
cp = ascii_uc2str(uc);
|
|
|
|
csz = term_strlen(p, cp);
|
|
|
|
ssz = strlen(cp);
|
|
|
|
} else
|
|
|
|
csz = (*p->width)(p, uc);
|
|
|
|
while (lsz >= csz) {
|
|
|
|
if (p->enc == TERMENC_ASCII)
|
|
|
|
encode(p, cp, ssz);
|
|
|
|
else
|
|
|
|
encode1(p, uc);
|
|
|
|
lsz -= csz;
|
|
|
|
}
|
|
|
|
continue;
|
2014-11-22 18:08:25 +00:00
|
|
|
case ESCAPE_SKIPCHAR:
|
2016-01-15 23:08:59 +00:00
|
|
|
p->flags |= TERMP_BACKAFTER;
|
2014-12-02 07:34:06 +00:00
|
|
|
continue;
|
2015-03-02 16:45:41 +00:00
|
|
|
case ESCAPE_OVERSTRIKE:
|
|
|
|
cp = seq + sz;
|
|
|
|
while (seq < cp) {
|
|
|
|
if (*seq == '\\') {
|
|
|
|
mandoc_escape(&seq, NULL, NULL);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
encode1(p, *seq++);
|
2016-01-15 23:08:59 +00:00
|
|
|
if (seq < cp) {
|
|
|
|
if (p->flags & TERMP_BACKBEFORE)
|
|
|
|
p->flags |= TERMP_BACKAFTER;
|
|
|
|
else
|
|
|
|
p->flags |= TERMP_BACKBEFORE;
|
|
|
|
}
|
2015-03-02 16:45:41 +00:00
|
|
|
}
|
2016-01-15 23:08:59 +00:00
|
|
|
/* Trim trailing backspace/blank pair. */
|
2017-07-31 19:17:54 +00:00
|
|
|
if (p->tcol->lastcol > 2 &&
|
|
|
|
(p->tcol->buf[p->tcol->lastcol - 1] == ' ' ||
|
|
|
|
p->tcol->buf[p->tcol->lastcol - 1] == '\t'))
|
|
|
|
p->tcol->lastcol -= 2;
|
|
|
|
if (p->col > p->tcol->lastcol)
|
|
|
|
p->col = p->tcol->lastcol;
|
2016-01-15 23:08:59 +00:00
|
|
|
continue;
|
2012-10-18 09:55:16 +00:00
|
|
|
default:
|
2014-12-02 07:34:06 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Common handling for Unicode and numbered
|
|
|
|
* character escape sequences.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (p->enc == TERMENC_ASCII) {
|
|
|
|
cp = ascii_uc2str(uc);
|
|
|
|
encode(p, cp, strlen(cp));
|
|
|
|
} else {
|
|
|
|
if ((uc < 0x20 && uc != 0x09) ||
|
|
|
|
(uc > 0x7E && uc < 0xA0))
|
|
|
|
uc = 0xFFFD;
|
|
|
|
encode1(p, uc);
|
2012-10-18 09:55:16 +00:00
|
|
|
}
|
|
|
|
}
|
2014-01-31 19:59:03 +00:00
|
|
|
p->flags &= ~TERMP_NBRWORD;
|
2012-10-18 09:55:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2017-06-08 19:29:07 +00:00
|
|
|
adjbuf(struct termp_col *c, size_t sz)
|
2012-10-18 09:55:16 +00:00
|
|
|
{
|
2017-06-08 19:29:07 +00:00
|
|
|
if (c->maxcols == 0)
|
|
|
|
c->maxcols = 1024;
|
|
|
|
while (c->maxcols <= sz)
|
|
|
|
c->maxcols <<= 2;
|
|
|
|
c->buf = mandoc_reallocarray(c->buf, c->maxcols, sizeof(*c->buf));
|
2012-10-18 09:55:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
bufferc(struct termp *p, char c)
|
|
|
|
{
|
2017-06-08 19:29:07 +00:00
|
|
|
if (p->flags & TERMP_NOBUF) {
|
|
|
|
(*p->letter)(p, c);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (p->col + 1 >= p->tcol->maxcols)
|
|
|
|
adjbuf(p->tcol, p->col + 1);
|
2017-07-31 19:17:54 +00:00
|
|
|
if (p->tcol->lastcol <= p->col || (c != ' ' && c != ASCII_NBRSP))
|
2017-06-08 19:29:07 +00:00
|
|
|
p->tcol->buf[p->col] = c;
|
2017-07-31 19:17:54 +00:00
|
|
|
if (p->tcol->lastcol < ++p->col)
|
|
|
|
p->tcol->lastcol = p->col;
|
2012-10-18 09:55:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* See encode().
|
|
|
|
* Do this for a single (probably unicode) value.
|
|
|
|
* Does not check for non-decorated glyphs.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
encode1(struct termp *p, int c)
|
|
|
|
{
|
|
|
|
enum termfont f;
|
|
|
|
|
2017-06-08 19:29:07 +00:00
|
|
|
if (p->flags & TERMP_NOBUF) {
|
|
|
|
(*p->letter)(p, c);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (p->col + 7 >= p->tcol->maxcols)
|
|
|
|
adjbuf(p->tcol, p->col + 7);
|
2012-10-18 09:55:16 +00:00
|
|
|
|
2016-01-15 23:08:59 +00:00
|
|
|
f = (c == ASCII_HYPH || c > 127 || isgraph(c)) ?
|
|
|
|
p->fontq[p->fonti] : TERMFONT_NONE;
|
2012-10-18 09:55:16 +00:00
|
|
|
|
2016-01-15 23:08:59 +00:00
|
|
|
if (p->flags & TERMP_BACKBEFORE) {
|
2017-06-08 19:29:07 +00:00
|
|
|
if (p->tcol->buf[p->col - 1] == ' ' ||
|
|
|
|
p->tcol->buf[p->col - 1] == '\t')
|
2016-01-15 23:08:59 +00:00
|
|
|
p->col--;
|
|
|
|
else
|
2017-06-08 19:29:07 +00:00
|
|
|
p->tcol->buf[p->col++] = '\b';
|
2016-01-15 23:08:59 +00:00
|
|
|
p->flags &= ~TERMP_BACKBEFORE;
|
|
|
|
}
|
2017-06-08 19:29:07 +00:00
|
|
|
if (f == TERMFONT_UNDER || f == TERMFONT_BI) {
|
|
|
|
p->tcol->buf[p->col++] = '_';
|
|
|
|
p->tcol->buf[p->col++] = '\b';
|
2014-01-31 19:59:03 +00:00
|
|
|
}
|
2017-06-08 19:29:07 +00:00
|
|
|
if (f == TERMFONT_BOLD || f == TERMFONT_BI) {
|
|
|
|
if (c == ASCII_HYPH)
|
|
|
|
p->tcol->buf[p->col++] = '-';
|
2014-01-31 19:59:03 +00:00
|
|
|
else
|
2017-06-08 19:29:07 +00:00
|
|
|
p->tcol->buf[p->col++] = c;
|
|
|
|
p->tcol->buf[p->col++] = '\b';
|
2014-01-31 19:59:03 +00:00
|
|
|
}
|
2017-07-31 19:17:54 +00:00
|
|
|
if (p->tcol->lastcol <= p->col || (c != ' ' && c != ASCII_NBRSP))
|
2017-06-08 19:29:07 +00:00
|
|
|
p->tcol->buf[p->col] = c;
|
2017-07-31 19:17:54 +00:00
|
|
|
if (p->tcol->lastcol < ++p->col)
|
|
|
|
p->tcol->lastcol = p->col;
|
2016-01-15 23:08:59 +00:00
|
|
|
if (p->flags & TERMP_BACKAFTER) {
|
|
|
|
p->flags |= TERMP_BACKBEFORE;
|
|
|
|
p->flags &= ~TERMP_BACKAFTER;
|
|
|
|
}
|
2012-10-18 09:55:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
encode(struct termp *p, const char *word, size_t sz)
|
|
|
|
{
|
2014-01-31 19:59:03 +00:00
|
|
|
size_t i;
|
2012-10-18 09:55:16 +00:00
|
|
|
|
2017-06-08 19:29:07 +00:00
|
|
|
if (p->flags & TERMP_NOBUF) {
|
|
|
|
for (i = 0; i < sz; i++)
|
|
|
|
(*p->letter)(p, word[i]);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (p->col + 2 + (sz * 5) >= p->tcol->maxcols)
|
|
|
|
adjbuf(p->tcol, p->col + 2 + (sz * 5));
|
2012-10-18 09:55:16 +00:00
|
|
|
|
2014-01-31 19:59:03 +00:00
|
|
|
for (i = 0; i < sz; i++) {
|
|
|
|
if (ASCII_HYPH == word[i] ||
|
|
|
|
isgraph((unsigned char)word[i]))
|
|
|
|
encode1(p, word[i]);
|
2017-01-21 13:01:39 +00:00
|
|
|
else {
|
2017-07-31 19:17:54 +00:00
|
|
|
if (p->tcol->lastcol <= p->col ||
|
2017-06-08 19:29:07 +00:00
|
|
|
(word[i] != ' ' && word[i] != ASCII_NBRSP))
|
|
|
|
p->tcol->buf[p->col] = word[i];
|
|
|
|
p->col++;
|
2017-01-21 13:01:39 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Postpone the effect of \z while handling
|
|
|
|
* an overstrike sequence from ascii_uc2str().
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (word[i] == '\b' &&
|
|
|
|
(p->flags & TERMP_BACKBEFORE)) {
|
|
|
|
p->flags &= ~TERMP_BACKBEFORE;
|
|
|
|
p->flags |= TERMP_BACKAFTER;
|
|
|
|
}
|
|
|
|
}
|
2012-10-18 09:55:16 +00:00
|
|
|
}
|
2017-07-31 19:17:54 +00:00
|
|
|
if (p->tcol->lastcol < p->col)
|
|
|
|
p->tcol->lastcol = p->col;
|
2012-10-18 09:55:16 +00:00
|
|
|
}
|
|
|
|
|
2014-11-22 18:08:25 +00:00
|
|
|
void
|
|
|
|
term_setwidth(struct termp *p, const char *wstr)
|
|
|
|
{
|
|
|
|
struct roffsu su;
|
2016-01-15 23:08:59 +00:00
|
|
|
int iop, width;
|
2014-11-22 18:08:25 +00:00
|
|
|
|
|
|
|
iop = 0;
|
|
|
|
width = 0;
|
|
|
|
if (NULL != wstr) {
|
|
|
|
switch (*wstr) {
|
|
|
|
case '+':
|
|
|
|
iop = 1;
|
|
|
|
wstr++;
|
|
|
|
break;
|
|
|
|
case '-':
|
|
|
|
iop = -1;
|
|
|
|
wstr++;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2017-06-08 19:29:07 +00:00
|
|
|
if (a2roffsu(wstr, &su, SCALE_MAX) != NULL)
|
2014-11-22 18:08:25 +00:00
|
|
|
width = term_hspan(p, &su);
|
|
|
|
else
|
|
|
|
iop = 0;
|
|
|
|
}
|
|
|
|
(*p->setwidth)(p, iop, width);
|
|
|
|
}
|
|
|
|
|
2012-10-18 09:55:16 +00:00
|
|
|
size_t
|
|
|
|
term_len(const struct termp *p, size_t sz)
|
|
|
|
{
|
|
|
|
|
2016-01-15 23:08:59 +00:00
|
|
|
return (*p->width)(p, ' ') * sz;
|
2012-10-18 09:55:16 +00:00
|
|
|
}
|
|
|
|
|
2014-01-31 19:59:03 +00:00
|
|
|
static size_t
|
|
|
|
cond_width(const struct termp *p, int c, int *skip)
|
|
|
|
{
|
|
|
|
|
|
|
|
if (*skip) {
|
|
|
|
(*skip) = 0;
|
2016-01-15 23:08:59 +00:00
|
|
|
return 0;
|
2014-01-31 19:59:03 +00:00
|
|
|
} else
|
2016-01-15 23:08:59 +00:00
|
|
|
return (*p->width)(p, c);
|
2014-01-31 19:59:03 +00:00
|
|
|
}
|
2012-10-18 09:55:16 +00:00
|
|
|
|
|
|
|
size_t
|
|
|
|
term_strlen(const struct termp *p, const char *cp)
|
|
|
|
{
|
|
|
|
size_t sz, rsz, i;
|
2014-12-02 07:34:06 +00:00
|
|
|
int ssz, skip, uc;
|
2012-10-18 09:55:16 +00:00
|
|
|
const char *seq, *rhs;
|
|
|
|
enum mandoc_esc esc;
|
2014-11-22 18:08:25 +00:00
|
|
|
static const char rej[] = { '\\', ASCII_NBRSP, ASCII_HYPH,
|
|
|
|
ASCII_BREAK, '\0' };
|
2012-10-18 09:55:16 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Account for escaped sequences within string length
|
|
|
|
* calculations. This follows the logic in term_word() as we
|
|
|
|
* must calculate the width of produced strings.
|
|
|
|
*/
|
|
|
|
|
|
|
|
sz = 0;
|
2014-01-31 19:59:03 +00:00
|
|
|
skip = 0;
|
2012-10-18 09:55:16 +00:00
|
|
|
while ('\0' != *cp) {
|
|
|
|
rsz = strcspn(cp, rej);
|
|
|
|
for (i = 0; i < rsz; i++)
|
2014-01-31 19:59:03 +00:00
|
|
|
sz += cond_width(p, *cp++, &skip);
|
2012-10-18 09:55:16 +00:00
|
|
|
|
|
|
|
switch (*cp) {
|
2014-11-22 18:08:25 +00:00
|
|
|
case '\\':
|
2012-10-18 09:55:16 +00:00
|
|
|
cp++;
|
|
|
|
rhs = NULL;
|
2019-03-29 13:13:30 +00:00
|
|
|
esc = mandoc_escape(&cp, &seq, &ssz);
|
2012-10-18 09:55:16 +00:00
|
|
|
switch (esc) {
|
2014-11-22 18:08:25 +00:00
|
|
|
case ESCAPE_UNICODE:
|
2014-12-02 07:34:06 +00:00
|
|
|
uc = mchars_num2uc(seq + 1, ssz - 1);
|
2012-10-18 09:55:16 +00:00
|
|
|
break;
|
2014-11-22 18:08:25 +00:00
|
|
|
case ESCAPE_NUMBERED:
|
2014-12-02 07:34:06 +00:00
|
|
|
uc = mchars_num2char(seq, ssz);
|
|
|
|
if (uc < 0)
|
|
|
|
continue;
|
2012-10-18 09:55:16 +00:00
|
|
|
break;
|
2014-11-22 18:08:25 +00:00
|
|
|
case ESCAPE_SPECIAL:
|
2014-12-02 07:34:06 +00:00
|
|
|
if (p->enc == TERMENC_ASCII) {
|
2016-01-15 23:08:59 +00:00
|
|
|
rhs = mchars_spec2str(seq, ssz, &rsz);
|
2014-12-02 07:34:06 +00:00
|
|
|
if (rhs != NULL)
|
|
|
|
break;
|
|
|
|
} else {
|
2016-01-15 23:08:59 +00:00
|
|
|
uc = mchars_spec2cp(seq, ssz);
|
2014-12-02 07:34:06 +00:00
|
|
|
if (uc > 0)
|
|
|
|
sz += cond_width(p, uc, &skip);
|
|
|
|
}
|
|
|
|
continue;
|
2019-03-29 13:13:30 +00:00
|
|
|
case ESCAPE_UNDEF:
|
|
|
|
uc = *seq;
|
|
|
|
break;
|
|
|
|
case ESCAPE_DEVICE:
|
|
|
|
if (p->type == TERMTYPE_PDF) {
|
|
|
|
rhs = "pdf";
|
|
|
|
rsz = 3;
|
|
|
|
} else if (p->type == TERMTYPE_PS) {
|
|
|
|
rhs = "ps";
|
|
|
|
rsz = 2;
|
|
|
|
} else if (p->enc == TERMENC_ASCII) {
|
|
|
|
rhs = "ascii";
|
|
|
|
rsz = 5;
|
|
|
|
} else {
|
|
|
|
rhs = "utf8";
|
|
|
|
rsz = 4;
|
|
|
|
}
|
|
|
|
break;
|
2014-11-22 18:08:25 +00:00
|
|
|
case ESCAPE_SKIPCHAR:
|
2014-01-31 19:59:03 +00:00
|
|
|
skip = 1;
|
2014-12-02 07:34:06 +00:00
|
|
|
continue;
|
2015-03-02 16:45:41 +00:00
|
|
|
case ESCAPE_OVERSTRIKE:
|
|
|
|
rsz = 0;
|
|
|
|
rhs = seq + ssz;
|
|
|
|
while (seq < rhs) {
|
|
|
|
if (*seq == '\\') {
|
|
|
|
mandoc_escape(&seq, NULL, NULL);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
i = (*p->width)(p, *seq++);
|
|
|
|
if (rsz < i)
|
|
|
|
rsz = i;
|
|
|
|
}
|
|
|
|
sz += rsz;
|
|
|
|
continue;
|
2012-10-18 09:55:16 +00:00
|
|
|
default:
|
2014-12-02 07:34:06 +00:00
|
|
|
continue;
|
2012-10-18 09:55:16 +00:00
|
|
|
}
|
|
|
|
|
2014-12-02 07:34:06 +00:00
|
|
|
/*
|
|
|
|
* Common handling for Unicode and numbered
|
|
|
|
* character escape sequences.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (rhs == NULL) {
|
|
|
|
if (p->enc == TERMENC_ASCII) {
|
|
|
|
rhs = ascii_uc2str(uc);
|
|
|
|
rsz = strlen(rhs);
|
|
|
|
} else {
|
|
|
|
if ((uc < 0x20 && uc != 0x09) ||
|
|
|
|
(uc > 0x7E && uc < 0xA0))
|
|
|
|
uc = 0xFFFD;
|
|
|
|
sz += cond_width(p, uc, &skip);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2012-10-18 09:55:16 +00:00
|
|
|
|
2014-01-31 19:59:03 +00:00
|
|
|
if (skip) {
|
|
|
|
skip = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2014-12-02 07:34:06 +00:00
|
|
|
/*
|
|
|
|
* Common handling for all escape sequences
|
|
|
|
* printing more than one character.
|
|
|
|
*/
|
|
|
|
|
2012-10-18 09:55:16 +00:00
|
|
|
for (i = 0; i < rsz; i++)
|
|
|
|
sz += (*p->width)(p, *rhs++);
|
|
|
|
break;
|
2014-11-22 18:08:25 +00:00
|
|
|
case ASCII_NBRSP:
|
2014-01-31 19:59:03 +00:00
|
|
|
sz += cond_width(p, ' ', &skip);
|
2012-10-18 09:55:16 +00:00
|
|
|
cp++;
|
|
|
|
break;
|
2014-11-22 18:08:25 +00:00
|
|
|
case ASCII_HYPH:
|
2014-01-31 19:59:03 +00:00
|
|
|
sz += cond_width(p, '-', &skip);
|
2012-10-18 09:55:16 +00:00
|
|
|
cp++;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-15 23:08:59 +00:00
|
|
|
return sz;
|
2012-10-18 09:55:16 +00:00
|
|
|
}
|
|
|
|
|
2015-03-02 16:45:41 +00:00
|
|
|
int
|
2012-10-18 09:55:16 +00:00
|
|
|
term_vspan(const struct termp *p, const struct roffsu *su)
|
|
|
|
{
|
|
|
|
double r;
|
2015-03-02 16:45:41 +00:00
|
|
|
int ri;
|
2012-10-18 09:55:16 +00:00
|
|
|
|
|
|
|
switch (su->unit) {
|
2015-03-02 16:45:41 +00:00
|
|
|
case SCALE_BU:
|
|
|
|
r = su->scale / 40.0;
|
|
|
|
break;
|
2014-11-22 18:08:25 +00:00
|
|
|
case SCALE_CM:
|
2015-03-02 16:45:41 +00:00
|
|
|
r = su->scale * 6.0 / 2.54;
|
|
|
|
break;
|
|
|
|
case SCALE_FS:
|
|
|
|
r = su->scale * 65536.0 / 40.0;
|
2012-10-18 09:55:16 +00:00
|
|
|
break;
|
2014-11-22 18:08:25 +00:00
|
|
|
case SCALE_IN:
|
|
|
|
r = su->scale * 6.0;
|
2012-10-18 09:55:16 +00:00
|
|
|
break;
|
2015-03-02 16:45:41 +00:00
|
|
|
case SCALE_MM:
|
|
|
|
r = su->scale * 0.006;
|
|
|
|
break;
|
2014-11-22 18:08:25 +00:00
|
|
|
case SCALE_PC:
|
2012-10-18 09:55:16 +00:00
|
|
|
r = su->scale;
|
|
|
|
break;
|
2014-11-22 18:08:25 +00:00
|
|
|
case SCALE_PT:
|
2015-03-02 16:45:41 +00:00
|
|
|
r = su->scale / 12.0;
|
2012-10-18 09:55:16 +00:00
|
|
|
break;
|
2015-03-02 16:45:41 +00:00
|
|
|
case SCALE_EN:
|
|
|
|
case SCALE_EM:
|
|
|
|
r = su->scale * 0.6;
|
2012-10-18 09:55:16 +00:00
|
|
|
break;
|
2014-11-22 18:08:25 +00:00
|
|
|
case SCALE_VS:
|
2012-10-18 09:55:16 +00:00
|
|
|
r = su->scale;
|
|
|
|
break;
|
|
|
|
default:
|
2015-03-02 16:45:41 +00:00
|
|
|
abort();
|
2012-10-18 09:55:16 +00:00
|
|
|
}
|
2015-03-02 16:45:41 +00:00
|
|
|
ri = r > 0.0 ? r + 0.4995 : r - 0.4995;
|
2016-01-15 23:08:59 +00:00
|
|
|
return ri < 66 ? ri : 1;
|
2012-10-18 09:55:16 +00:00
|
|
|
}
|
|
|
|
|
2016-01-15 23:08:59 +00:00
|
|
|
/*
|
2017-07-31 19:17:54 +00:00
|
|
|
* Convert a scaling width to basic units, rounding towards 0.
|
2016-01-15 23:08:59 +00:00
|
|
|
*/
|
2015-03-02 16:45:41 +00:00
|
|
|
int
|
2012-10-18 09:55:16 +00:00
|
|
|
term_hspan(const struct termp *p, const struct roffsu *su)
|
|
|
|
{
|
|
|
|
|
2016-01-15 23:08:59 +00:00
|
|
|
return (*p->hspan)(p, su);
|
2012-10-18 09:55:16 +00:00
|
|
|
}
|
2017-07-31 19:17:54 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Convert a scaling width to basic units, rounding to closest.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
term_hen(const struct termp *p, const struct roffsu *su)
|
|
|
|
{
|
|
|
|
int bu;
|
|
|
|
|
|
|
|
if ((bu = (*p->hspan)(p, su)) >= 0)
|
|
|
|
return (bu + 11) / 24;
|
|
|
|
else
|
|
|
|
return -((-bu + 11) / 24);
|
|
|
|
}
|