]>
git.cameronkatri.com Git - mandoc.git/blob - term.c
1 /* $Id: term.c,v 1.144 2010/06/08 09:20:08 kristaps Exp $ */
3 * Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@kth.se>
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21 #include <sys/types.h>
40 #define PS_CHAR_WIDTH 6
41 #define PS_CHAR_HEIGHT 12
42 #define PS_CHAR_TOPMARG (792 - 24)
43 #define PS_CHAR_TOP (PS_CHAR_TOPMARG - 36)
44 #define PS_CHAR_LEFT 36
45 #define PS_CHAR_BOTMARG 24
46 #define PS_CHAR_BOT (PS_CHAR_BOTMARG + 36)
48 static struct termp
*alloc(char *, enum termenc
, enum termtype
);
49 static void term_free(struct termp
*);
50 static void spec(struct termp
*, const char *, size_t);
51 static void res(struct termp
*, const char *, size_t);
52 static void buffera(struct termp
*, const char *, size_t);
53 static void bufferc(struct termp
*, char);
54 static void adjbuf(struct termp
*p
, size_t);
55 static void encode(struct termp
*, const char *, size_t);
56 static void advance(struct termp
*, size_t);
57 static void endline(struct termp
*);
58 static void letter(struct termp
*, char);
59 static void pageopen(struct termp
*);
63 ascii_alloc(char *outopts
)
66 return(alloc(outopts
, TERMENC_ASCII
, TERMTYPE_CHAR
));
74 return(alloc(NULL
, TERMENC_ASCII
, TERMTYPE_PS
));
79 terminal_free(void *arg
)
82 term_free((struct termp
*)arg
);
87 term_free(struct termp
*p
)
93 chars_free(p
->symtab
);
99 * Push a single letter into our output engine.
102 letter(struct termp
*p
, char c
)
105 if (TERMTYPE_CHAR
== p
->type
) {
107 * If using the terminal device, just push the letter
108 * out into the screen.
114 if ( ! (PS_INLINE
& p
->psstate
)) {
116 * If we're not in a PostScript "word" context, then
117 * open one now at the current cursor.
119 printf("%zu %zu moveto\n", p
->pscol
, p
->psrow
);
121 p
->psstate
|= PS_INLINE
;
125 * We need to escape these characters as per the PostScript
126 * specification. We would also escape non-graphable characters
127 * (like tabs), but none of them would get to this point and
128 * it's superfluous to abort() on them.
143 /* Write the character and adjust where we are on the page. */
145 p
->pscol
+= PS_CHAR_WIDTH
;
150 * Begin a "terminal" context. Since terminal encompasses PostScript,
151 * the actual terminal, etc., there are a few things we can do here.
154 term_begin(struct termp
*p
, term_margin head
,
155 term_margin foot
, const void *arg
)
162 if (TERMTYPE_CHAR
== p
->type
) {
163 /* Emit the header and be done. */
164 (*p
->headf
)(p
, p
->argf
);
169 * Emit the standard PostScript prologue, set our initial page
170 * position, then run pageopen() on the initial page.
173 printf("%s\n", "%!PS");
174 printf("%s\n", "/Courier");
175 printf("%s\n", "10 selectfont");
184 * Open a page. This is only used for -Tps at the moment. It opens a
185 * page context, printing the header and the footer. THE OUTPUT BUFFER
186 * MUST BE EMPTY. If it is not, output will ghost on the next line and
187 * we'll be all gross and out of state.
190 pageopen(struct termp
*p
)
193 assert(TERMTYPE_PS
== p
->type
);
194 assert(0 == p
->psstate
);
196 p
->pscol
= PS_CHAR_LEFT
;
197 p
->psrow
= PS_CHAR_TOPMARG
;
198 p
->psstate
|= PS_MARGINS
;
200 (*p
->headf
)(p
, p
->argf
);
203 p
->psstate
&= ~PS_MARGINS
;
204 assert(0 == p
->psstate
);
206 p
->pscol
= PS_CHAR_LEFT
;
207 p
->psrow
= PS_CHAR_BOTMARG
;
208 p
->psstate
|= PS_MARGINS
;
210 (*p
->footf
)(p
, p
->argf
);
213 p
->psstate
&= ~PS_MARGINS
;
214 assert(0 == p
->psstate
);
216 p
->pscol
= PS_CHAR_LEFT
;
217 p
->psrow
= PS_CHAR_TOP
;
223 term_end(struct termp
*p
)
226 if (TERMTYPE_CHAR
== p
->type
) {
227 (*p
->footf
)(p
, p
->argf
);
231 printf("%s\n", "%%END");
236 endline(struct termp
*p
)
239 if (TERMTYPE_CHAR
== p
->type
) {
244 if (PS_INLINE
& p
->psstate
) {
246 p
->psstate
&= ~PS_INLINE
;
249 if (PS_MARGINS
& p
->psstate
)
252 p
->pscol
= PS_CHAR_LEFT
;
253 if (p
->psrow
>= PS_CHAR_HEIGHT
+ PS_CHAR_BOT
) {
254 p
->psrow
-= PS_CHAR_HEIGHT
;
259 * XXX: can't run pageopen() until we're certain a flushln() has
260 * occured, else the buf will reopen in an awkward state on the
263 printf("showpage\n");
264 p
->psrow
= PS_CHAR_TOP
;
269 * Advance the output engine by a certain amount of whitespace.
272 advance(struct termp
*p
, size_t len
)
276 if (TERMTYPE_CHAR
== p
->type
) {
277 /* Just print whitespace on the terminal. */
278 for (i
= 0; i
< len
; i
++)
283 if (PS_INLINE
& p
->psstate
) {
284 /* Dump out any existing line scope. */
286 p
->psstate
&= ~PS_INLINE
;
289 p
->pscol
+= len
? len
* PS_CHAR_WIDTH
: 0;
293 static struct termp
*
294 alloc(char *outopts
, enum termenc enc
, enum termtype type
)
304 p
= calloc(1, sizeof(struct termp
));
316 while (outopts
&& *outopts
)
317 switch (getsubopt(&outopts
, UNCONST(toks
), &v
)) {
319 width
= (size_t)atoi(v
);
325 /* Enforce some lower boundary. */
328 p
->defrmargin
= width
- 2;
334 * Flush a line of text. A "line" is loosely defined as being something
335 * that should be followed by a newline, regardless of whether it's
336 * broken apart by newlines getting there. A line can also be a
337 * fragment of a columnar list (`Bl -tag' or `Bl -column'), which does
338 * not have a trailing newline.
340 * The following flags may be specified:
342 * - TERMP_NOLPAD: when beginning to write the line, don't left-pad the
343 * offset value. This is useful when doing columnar lists where the
344 * prior column has right-padded.
346 * - TERMP_NOBREAK: this is the most important and is used when making
347 * columns. In short: don't print a newline and instead pad to the
348 * right margin. Used in conjunction with TERMP_NOLPAD.
350 * - TERMP_TWOSPACE: when padding, make sure there are at least two
351 * space characters of padding. Otherwise, rather break the line.
353 * - TERMP_DANGLE: don't newline when TERMP_NOBREAK is specified and
354 * the line is overrun, and don't pad-right if it's underrun.
356 * - TERMP_HANG: like TERMP_DANGLE, but doesn't newline when
357 * overruning, instead save the position and continue at that point
358 * when the next invocation.
360 * In-line line breaking:
362 * If TERMP_NOBREAK is specified and the line overruns the right
363 * margin, it will break and pad-right to the right margin after
364 * writing. If maxrmargin is violated, it will break and continue
365 * writing from the right-margin, which will lead to the above scenario
366 * upon exit. Otherwise, the line will break at the right margin.
369 term_flushln(struct termp
*p
)
371 int i
; /* current input position in p->buf */
372 size_t vis
; /* current visual position on output */
373 size_t vbl
; /* number of blanks to prepend to output */
374 size_t vend
; /* end of word visual position on output */
375 size_t bp
; /* visual right border position */
376 int j
; /* temporary loop index */
377 int jhy
; /* last hyphen before line overflow */
381 * First, establish the maximum columns of "visible" content.
382 * This is usually the difference between the right-margin and
383 * an indentation, but can be, for tagged lists or columns, a
384 * small set of values.
387 assert(p
->offset
< p
->rmargin
);
389 maxvis
= (int)(p
->rmargin
- p
->offset
) - p
->overstep
< 0 ?
391 0 : p
->rmargin
- p
->offset
- p
->overstep
;
392 mmax
= (int)(p
->maxrmargin
- p
->offset
) - p
->overstep
< 0 ?
394 0 : p
->maxrmargin
- p
->offset
- p
->overstep
;
396 bp
= TERMP_NOBREAK
& p
->flags
? mmax
: maxvis
;
399 * Indent the first line of a paragraph.
401 vbl
= p
->flags
& TERMP_NOLPAD
? 0 : p
->offset
;
404 * FIXME: if bp is zero, we still output the first word before
409 while (i
< (int)p
->col
) {
412 * Handle literal tab characters.
414 for (j
= i
; j
< (int)p
->col
; j
++) {
415 if ('\t' != p
->buf
[j
])
417 vend
= (vis
/p
->tabwidth
+1)*p
->tabwidth
;
423 * Count up visible word characters. Control sequences
424 * (starting with the CSI) aren't counted. A space
425 * generates a non-printing word, which is valid (the
426 * space is printed according to regular spacing rules).
430 for (jhy
= 0; j
< (int)p
->col
; j
++) {
431 if ((j
&& ' ' == p
->buf
[j
]) || '\t' == p
->buf
[j
])
433 if (8 != p
->buf
[j
]) {
434 if (vend
> vis
&& vend
< bp
&&
435 ASCII_HYPH
== p
->buf
[j
])
443 * Find out whether we would exceed the right margin.
444 * If so, break to the next line.
446 if (vend
> bp
&& 0 == jhy
&& vis
> 0) {
449 if (TERMP_NOBREAK
& p
->flags
) {
450 p
->viscol
= p
->rmargin
;
451 advance(p
, p
->rmargin
);
452 vend
+= p
->rmargin
- p
->offset
;
458 /* Remove the p->overstep width. */
460 bp
+= (int)/* LINTED */
466 * Skip leading tabs, they were handled above.
468 while (i
< (int)p
->col
&& '\t' == p
->buf
[i
])
471 /* Write out the [remaining] word. */
472 for ( ; i
< (int)p
->col
; i
++) {
473 if (vend
> bp
&& jhy
> 0 && i
> jhy
)
475 if ('\t' == p
->buf
[i
])
477 if (' ' == p
->buf
[i
]) {
478 while (' ' == p
->buf
[i
]) {
484 if (ASCII_NBRSP
== p
->buf
[i
]) {
490 * Now we definitely know there will be
491 * printable characters to output,
492 * so write preceding white space now.
500 if (ASCII_HYPH
== p
->buf
[i
])
503 letter(p
, p
->buf
[i
]);
514 if ( ! (TERMP_NOBREAK
& p
->flags
)) {
520 if (TERMP_HANG
& p
->flags
) {
521 /* We need one blank after the tag. */
522 p
->overstep
= /* LINTED */
526 * Behave exactly the same way as groff:
527 * If we have overstepped the margin, temporarily move
528 * it to the right and flag the rest of the line to be
530 * If we landed right at the margin, be happy.
531 * If we are one step before the margin, temporarily
532 * move it one step LEFT and flag the rest of the line
535 if (p
->overstep
>= -1) {
536 assert((int)maxvis
+ p
->overstep
>= 0);
538 maxvis
+= p
->overstep
;
542 } else if (TERMP_DANGLE
& p
->flags
)
546 if (maxvis
> vis
+ /* LINTED */
547 ((TERMP_TWOSPACE
& p
->flags
) ? 1 : 0)) {
548 p
->viscol
+= maxvis
- vis
;
549 advance(p
, maxvis
- vis
);
550 vis
+= (maxvis
- vis
);
551 } else { /* ...or newline break. */
553 p
->viscol
= p
->rmargin
;
554 advance(p
, p
->rmargin
);
560 * A newline only breaks an existing line; it won't assert vertical
561 * space. All data in the output buffer is flushed prior to the newline
565 term_newln(struct termp
*p
)
568 p
->flags
|= TERMP_NOSPACE
;
569 if (0 == p
->col
&& 0 == p
->viscol
) {
570 p
->flags
&= ~TERMP_NOLPAD
;
574 p
->flags
&= ~TERMP_NOLPAD
;
579 * Asserts a vertical space (a full, empty line-break between lines).
580 * Note that if used twice, this will cause two blank spaces and so on.
581 * All data in the output buffer is flushed prior to the newline
585 term_vspace(struct termp
*p
)
595 spec(struct termp
*p
, const char *word
, size_t len
)
600 rhs
= chars_a2ascii(p
->symtab
, word
, len
, &sz
);
607 res(struct termp
*p
, const char *word
, size_t len
)
612 rhs
= chars_a2res(p
->symtab
, word
, len
, &sz
);
619 term_fontlast(struct termp
*p
)
624 p
->fontl
= p
->fontq
[p
->fonti
];
625 p
->fontq
[p
->fonti
] = f
;
630 term_fontrepl(struct termp
*p
, enum termfont f
)
633 p
->fontl
= p
->fontq
[p
->fonti
];
634 p
->fontq
[p
->fonti
] = f
;
639 term_fontpush(struct termp
*p
, enum termfont f
)
642 assert(p
->fonti
+ 1 < 10);
643 p
->fontl
= p
->fontq
[p
->fonti
];
644 p
->fontq
[++p
->fonti
] = f
;
649 term_fontq(struct termp
*p
)
652 return(&p
->fontq
[p
->fonti
]);
657 term_fonttop(struct termp
*p
)
660 return(p
->fontq
[p
->fonti
]);
665 term_fontpopq(struct termp
*p
, const void *key
)
668 while (p
->fonti
>= 0 && key
!= &p
->fontq
[p
->fonti
])
670 assert(p
->fonti
>= 0);
675 term_fontpop(struct termp
*p
)
684 * Handle pwords, partial words, which may be either a single word or a
685 * phrase that cannot be broken down (such as a literal string). This
686 * handles word styling.
689 term_word(struct termp
*p
, const char *word
)
691 const char *sv
, *seq
;
698 if (word
[0] && '\0' == word
[1])
715 if ( ! (TERMP_IGNDELIM
& p
->flags
))
716 p
->flags
|= TERMP_NOSPACE
;
722 if ( ! (TERMP_NOSPACE
& p
->flags
)) {
724 if (TERMP_SENTENCE
& p
->flags
)
728 if ( ! (p
->flags
& TERMP_NONOSPACE
))
729 p
->flags
&= ~TERMP_NOSPACE
;
731 p
->flags
&= ~TERMP_SENTENCE
;
733 /* FIXME: use strcspn. */
743 sz
= a2roffdeco(&deco
, &seq
, &ssz
);
746 case (DECO_RESERVED
):
753 term_fontrepl(p
, TERMFONT_BOLD
);
756 term_fontrepl(p
, TERMFONT_UNDER
);
759 term_fontrepl(p
, TERMFONT_NONE
);
761 case (DECO_PREVIOUS
):
769 if (DECO_NOSPACE
== deco
&& '\0' == *word
)
770 p
->flags
|= TERMP_NOSPACE
;
774 * Note that we don't process the pipe: the parser sees it as
775 * punctuation, but we don't in terms of typography.
777 if (sv
[0] && 0 == sv
[1])
782 p
->flags
|= TERMP_NOSPACE
;
791 adjbuf(struct termp
*p
, size_t sz
)
796 while (sz
>= p
->maxcols
)
799 p
->buf
= realloc(p
->buf
, p
->maxcols
);
800 if (NULL
== p
->buf
) {
808 buffera(struct termp
*p
, const char *word
, size_t sz
)
811 if (p
->col
+ sz
>= p
->maxcols
)
812 adjbuf(p
, p
->col
+ sz
);
814 memcpy(&p
->buf
[(int)p
->col
], word
, sz
);
820 bufferc(struct termp
*p
, char c
)
823 if (p
->col
+ 1 >= p
->maxcols
)
824 adjbuf(p
, p
->col
+ 1);
826 p
->buf
[(int)p
->col
++] = c
;
831 encode(struct termp
*p
, const char *word
, size_t sz
)
837 * Encode and buffer a string of characters. If the current
838 * font mode is unset, buffer directly, else encode then buffer
839 * character by character.
842 if (TERMTYPE_PS
== p
->type
) {
843 buffera(p
, word
, sz
);
845 } else if (TERMFONT_NONE
== (f
= term_fonttop(p
))) {
846 buffera(p
, word
, sz
);
850 for (i
= 0; i
< (int)sz
; i
++) {
851 if ( ! isgraph((u_char
)word
[i
])) {
856 if (TERMFONT_UNDER
== f
)
868 term_vspan(const struct roffsu
*su
)
886 r
= su
->scale
/ 1000;
898 return(/* LINTED */(size_t)
904 term_hspan(const struct roffsu
*su
)
908 /* XXX: CM, IN, and PT are approximations. */
915 /* XXX: this is an approximation. */
919 r
= (10 * su
->scale
) / 6;
922 r
= (10 * su
->scale
) / 72;
925 r
= su
->scale
/ 1000; /* FIXME: double-check. */
928 r
= su
->scale
* 2 - 1; /* FIXME: double-check. */
937 return((size_t)/* LINTED */