/* $Id: term.c,v 1.257 2016/04/12 15:30:00 schwarze Exp $ */
/*
* Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
* Copyright (c) 2010-2015 Ingo Schwarze <schwarze@openbsd.org>
*
* 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.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
* 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 <assert.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "mandoc.h"
#include "mandoc_aux.h"
#include "out.h"
#include "term.h"
#include "main.h"
void
{
free(p);
}
void
{
(*p->begin)(p);
}
void
{
(*p->end)(p);
}
/*
* 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
* output line at the end of the chunk.
* The following flags may be specified:
*
* - TERMP_NOBREAK: Do not break the output line at the right margin,
* but only at the max right margin. Also, do not break the output
* line at the end of the chunk, such that the next call can pad to
* the next column. However, if less than p->trailspace blanks,
* which can be 0, 1, or 2, remain to the right margin, the line
* will be broken.
* - TERMP_BRTRSP: Consider trailing whitespace significant
* when deciding whether the chunk fits or not.
* - TERMP_BRIND: If the chunk does not fit and the output line has
* to be broken, start the next line at the right margin instead
* of at the offset. Used together with TERMP_NOBREAK for the tags
* in various kinds of tagged lists.
* - TERMP_DANGLE: Do not break the output line at the right margin,
* append the next chunk after it even if this one is too long.
* To be used together with TERMP_NOBREAK.
* - TERMP_HANG: Like TERMP_DANGLE, and also suppress padding before
* the next chunk if this column is not full.
*/
void
{
size_t i; /* current input position in p->buf */
size_t j; /* temporary loop index for p->buf */
/*
* First, establish the maximum columns of "visible" content.
* This is usually the difference between the right-margin and
* an indentation, but can be, for tagged lists or columns, a
* small set of values.
*
* The following unsigned-signed subtractions look strange,
* but they are actually correct. If the int p->overstep
* is negative, it gets sign extended. Subtracting that
* very large size_t effectively adds a small number to dv.
*/
if (p->flags & TERMP_NOBREAK) {
p->maxrmargin - p->offset : 0;
} else
/*
* Calculate the required amount of padding.
*/
i = 0;
while (i < p->col) {
/*
* Handle literal tab characters: collapse all
* subsequent tabs into a single huge set of spaces.
*/
ntab = 0;
ntab++;
i++;
}
/*
* Count up visible word characters. Control sequences
* (starting with the CSI) aren't counted. A space
* generates a non-printing word, which is valid (the
* space is printed according to regular spacing rules).
*/
break;
/* Back over the last printed character. */
if (8 == p->buf[j]) {
assert(j);
continue;
}
/* Regular word. */
/* Break at the hyphen point if we overrun. */
(ASCII_HYPH == p->buf[j] ||
ASCII_BREAK == p->buf[j]))
jhy = j;
/*
* Hyphenation now decided, put back a real
* hyphen such that we get the correct width.
*/
if (ASCII_HYPH == p->buf[j])
p->buf[j] = '-';
}
/*
* Find out whether we would exceed the right margin.
* If so, break to the next line.
*/
(*p->endline)(p);
p->viscol = 0;
if (TERMP_BRIND & p->flags) {
} else
/* use pending tabs on the new line */
if (0 < ntab)
/*
* Remove the p->overstep width.
* Again, if p->overstep is negative,
* sign extension does the right thing.
*/
p->overstep = 0;
}
/* Write out the [remaining] word. */
for ( ; i < p->col; i++) {
break;
if ('\t' == p->buf[i])
break;
if (' ' == p->buf[i]) {
j = i;
i++;
break;
}
if (ASCII_NBRSP == p->buf[i]) {
continue;
}
if (ASCII_BREAK == p->buf[i])
continue;
/*
* Now we definitely know there will be
* printable characters to output,
* so write preceding white space now.
*/
if (vbl) {
vbl = 0;
}
if (8 == p->buf[i])
else
}
}
/*
* If there was trailing white space, it was not printed;
* so reset the cursor position accordingly.
*/
else
vis = 0;
p->col = 0;
p->overstep = 0;
if ( ! (TERMP_NOBREAK & p->flags)) {
p->viscol = 0;
(*p->endline)(p);
return;
}
if (TERMP_HANG & p->flags) {
/*
* If we have overstepped the margin, temporarily move
* it to the right and flag the rest of the line to be
* shorter.
* If there is a request to keep the columns together,
* allow negative overstep when the column is not full.
*/
if (p->trailspace && p->overstep < 0)
p->overstep = 0;
return;
} else if (TERMP_DANGLE & p->flags)
return;
/* Trailing whitespace is significant in some columns. */
/* If the column was overrun, break the line. */
(*p->endline)(p);
p->viscol = 0;
}
}
/*
* 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
{
p->flags |= TERMP_NOSPACE;
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_newln(p);
p->viscol = 0;
if (0 < p->skipvsp)
p->skipvsp--;
else
(*p->endline)(p);
}
/* Swap current and previous font; for \fP and .ft P */
void
{
enum termfont f;
f = p->fontl;
}
/* Set font, save current, discard previous; for \f, .ft, .B etc. */
void
{
}
/* Set font, save previous. */
void
{
p->fontsz += 8;
}
}
/* Flush to make the saved pointer current again. */
void
{
assert(i >= 0);
if (p->fonti > i)
p->fonti = i;
}
/* Pop one font off the stack. */
void
{
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
{
if ( ! (TERMP_NOSPACE & p->flags)) {
if ( ! (TERMP_KEEP & p->flags)) {
bufferc(p, ' ');
if (TERMP_SENTENCE & p->flags)
bufferc(p, ' ');
} else
bufferc(p, ASCII_NBRSP);
}
if (TERMP_PREKEEP & p->flags)
p->flags |= TERMP_KEEP;
if ( ! (p->flags & TERMP_NONOSPACE))
p->flags &= ~TERMP_NOSPACE;
else
p->flags |= TERMP_NOSPACE;
p->skipvsp = 0;
while ('\0' != *word) {
if ('\\' != *word) {
if (TERMP_NBRWORD & p->flags) {
if (' ' == *word) {
word++;
continue;
}
} else
continue;
}
word++;
if (ESCAPE_ERROR == esc)
continue;
switch (esc) {
case ESCAPE_UNICODE:
break;
case ESCAPE_NUMBERED:
if (uc < 0)
continue;
break;
case ESCAPE_SPECIAL:
if (p->enc == TERMENC_ASCII) {
} else {
if (uc > 0)
}
continue;
case ESCAPE_FONTBOLD:
continue;
case ESCAPE_FONTITALIC:
continue;
case ESCAPE_FONTBI:
term_fontrepl(p, TERMFONT_BI);
continue;
case ESCAPE_FONT:
case ESCAPE_FONTROMAN:
continue;
case ESCAPE_FONTPREV:
term_fontlast(p);
continue;
case ESCAPE_NOSPACE:
if (p->flags & TERMP_BACKAFTER)
p->flags &= ~TERMP_BACKAFTER;
else if (*word == '\0')
continue;
case ESCAPE_SKIPCHAR:
p->flags |= TERMP_BACKAFTER;
continue;
case ESCAPE_OVERSTRIKE:
if (*seq == '\\') {
continue;
}
if (p->flags & TERMP_BACKBEFORE)
p->flags |= TERMP_BACKAFTER;
else
p->flags |= TERMP_BACKBEFORE;
}
}
p->col -= 2;
continue;
default:
continue;
}
/*
* Common handling for Unicode and numbered
* character escape sequences.
*/
if (p->enc == TERMENC_ASCII) {
} else {
uc = 0xFFFD;
}
}
p->flags &= ~TERMP_NBRWORD;
}
static void
{
if (0 == p->maxcols)
p->maxcols = 1024;
p->maxcols <<= 2;
}
static void
{
}
/*
* See encode().
* Do this for a single (probably unicode) value.
* Does not check for non-decorated glyphs.
*/
static void
{
enum termfont f;
if (p->flags & TERMP_BACKBEFORE) {
p->col--;
else
p->flags &= ~TERMP_BACKBEFORE;
}
if (TERMFONT_UNDER == f || TERMFONT_BI == f) {
}
if (TERMFONT_BOLD == f || TERMFONT_BI == f) {
if (ASCII_HYPH == c)
else
}
if (p->flags & TERMP_BACKAFTER) {
p->flags |= TERMP_BACKBEFORE;
p->flags &= ~TERMP_BACKAFTER;
}
}
static void
{
size_t i;
for (i = 0; i < sz; i++) {
if (ASCII_HYPH == word[i] ||
else
}
}
void
{
iop = 0;
width = 0;
switch (*wstr) {
case '+':
iop = 1;
wstr++;
break;
case '-':
iop = -1;
wstr++;
break;
default:
break;
}
else
iop = 0;
}
}
{
}
static size_t
{
if (*skip) {
(*skip) = 0;
return 0;
} else
return (*p->width)(p, c);
}
{
ASCII_BREAK, '\0' };
/*
* 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;
skip = 0;
while ('\0' != *cp) {
for (i = 0; i < rsz; i++)
switch (*cp) {
case '\\':
cp++;
if (ESCAPE_ERROR == esc)
continue;
switch (esc) {
case ESCAPE_UNICODE:
break;
case ESCAPE_NUMBERED:
if (uc < 0)
continue;
break;
case ESCAPE_SPECIAL:
if (p->enc == TERMENC_ASCII) {
break;
} else {
if (uc > 0)
}
continue;
case ESCAPE_SKIPCHAR:
skip = 1;
continue;
case ESCAPE_OVERSTRIKE:
rsz = 0;
if (*seq == '\\') {
continue;
}
if (rsz < i)
rsz = i;
}
continue;
default:
continue;
}
/*
* Common handling for Unicode and numbered
* character escape sequences.
*/
if (p->enc == TERMENC_ASCII) {
} else {
uc = 0xFFFD;
continue;
}
}
if (skip) {
skip = 0;
break;
}
/*
* Common handling for all escape sequences
* printing more than one character.
*/
for (i = 0; i < rsz; i++)
break;
case ASCII_NBRSP:
cp++;
break;
case ASCII_HYPH:
cp++;
break;
default:
break;
}
}
return sz;
}
int
{
double r;
int ri;
case SCALE_BU:
break;
case SCALE_CM:
break;
case SCALE_FS:
break;
case SCALE_IN:
break;
case SCALE_MM:
break;
case SCALE_PC:
break;
case SCALE_PT:
break;
case SCALE_EN:
case SCALE_EM:
break;
case SCALE_VS:
break;
default:
abort();
}
}
/*
* Convert a scaling width to basic units, rounding down.
*/
int
{
}