]>
git.cameronkatri.com Git - mandoc.git/blob - out.c
1 /* $Id: out.c,v 1.37 2011/01/30 16:05:37 schwarze Exp $ */
3 * Copyright (c) 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2011 Ingo Schwarze <schwarze@openbsd.org>
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22 #include <sys/types.h>
34 static void tblcalc_data(struct rofftbl
*, struct roffcol
*,
35 const struct tbl
*, const struct tbl_dat
*);
36 static void tblcalc_literal(struct rofftbl
*, struct roffcol
*,
37 const struct tbl_dat
*);
38 static void tblcalc_number(struct rofftbl
*, struct roffcol
*,
39 const struct tbl
*, const struct tbl_dat
*);
42 * Convert a `scaling unit' to a consistent form, or fail. Scaling
43 * units are documented in groff.7, mdoc.7, man.7.
46 a2roffsu(const char *src
, struct roffsu
*dst
, enum roffscale def
)
48 char buf
[BUFSIZ
], hasd
;
72 if ( ! isdigit((u_char
)*src
)) {
83 if (BUFSIZ
== i
|| (*src
&& *(src
+ 1)))
111 if (SCALE_MAX
== def
)
128 /* FIXME: do this in the caller. */
129 if ((dst
->scale
= atof(buf
)) < 0)
137 * Correctly writes the time in nroff form, which differs from standard
138 * form in that a space isn't printed in lieu of the extra %e field for
139 * single-digit dates.
142 time2a(time_t t
, char *dst
, size_t sz
)
150 localtime_r(&t
, &tm
);
157 if (0 == (nsz
= strftime(p
, sz
, "%B ", &tm
)))
163 if (0 == strftime(buf
, sizeof(buf
), "%e, ", &tm
))
166 nsz
= strlcat(p
, buf
+ (' ' == buf
[0] ? 1 : 0), sz
);
174 (void)strftime(p
, sz
, "%Y", &tm
);
179 a2roffdeco(enum roffdeco
*d
, const char **word
, size_t *sz
)
191 switch ((c
= wp
[i
++])) {
199 *d
= 'F' == c
? DECO_FFONT
: DECO_FONT
;
259 * Sequence of characters: backslash, 'N' (i = 0),
260 * starting delimiter (i = 1), character number (i = 2).
267 * Cannot use a digit as a starting delimiter;
268 * but skip the digit anyway.
271 if (isdigit((int)wp
[1]))
275 * Any non-digit terminates the character number.
276 * That is, the terminating delimiter need not
277 * match the starting delimiter.
280 for (i
= 2; isdigit((int)wp
[i
]); i
++)
284 * This is only a numbered character
285 * if the character number has at least one digit.
292 * Skip the terminating delimiter, even if it does not
293 * match, and even if there is no character number.
304 if ('+' == wp
[i
] || '-' == wp
[i
]) {
328 if ('+' == wp
[i
] || '-' == wp
[i
]) {
334 /* Handle embedded numerical subexp or escape. */
337 while (wp
[i
] && ')' != wp
[i
])
338 if ('\\' == wp
[i
++]) {
339 /* Handle embedded escape. */
341 i
+= a2roffdeco(&dd
, word
, sz
);
349 } else if ('\\' == wp
[i
]) {
351 i
+= a2roffdeco(&dd
, word
, sz
);
366 return(i
+ a2roffdeco(&dd
, word
, sz
));
373 if ('\'' == wp
[i
++]) {
390 while (wp
[i
] && wp
[i
] != term
)
398 *sz
= (size_t)(i
- j
);
406 for (j
= 0; wp
[i
] && j
< lim
; j
++)
415 * Calculate the abstract widths and decimal positions of columns in a
416 * table. This routine allocates the columns structures then runs over
417 * all rows and cells in the table. The function pointers in "tbl" are
418 * used for the actual width calculations.
421 tblcalc(struct rofftbl
*tbl
, const struct tbl_span
*sp
)
423 const struct tbl_dat
*dp
;
424 const struct tbl_head
*hp
;
428 * Allocate the master column specifiers. These will hold the
429 * widths and decimal positions for all cells in the column. It
430 * must be freed and nullified by the caller.
433 assert(NULL
== tbl
->cols
);
434 tbl
->cols
= calloc(sp
->tbl
->cols
, sizeof(struct roffcol
));
438 for ( ; sp
; sp
= sp
->next
) {
439 if (TBL_SPAN_DATA
!= sp
->pos
)
442 * Account for the data cells in the layout, matching it
443 * to data cells in the data section.
445 for (dp
= sp
->first
; dp
; dp
= dp
->next
) {
447 col
= &tbl
->cols
[dp
->layout
->head
->ident
];
448 tblcalc_data(tbl
, col
, sp
->tbl
, dp
);
453 * Calculate width of the spanners. These get one space for a
454 * vertical line, two for a double-vertical line.
457 for ( ; hp
; hp
= hp
->next
) {
458 col
= &tbl
->cols
[hp
->ident
];
460 case (TBL_HEAD_VERT
):
461 col
->width
= (*tbl
->len
)(1, tbl
->arg
);
463 case (TBL_HEAD_DVERT
):
464 col
->width
= (*tbl
->len
)(2, tbl
->arg
);
473 tblcalc_data(struct rofftbl
*tbl
, struct roffcol
*col
,
474 const struct tbl
*tp
, const struct tbl_dat
*dp
)
478 /* Branch down into data sub-types. */
480 switch (dp
->layout
->pos
) {
481 case (TBL_CELL_HORIZ
):
483 case (TBL_CELL_DHORIZ
):
484 sz
= (*tbl
->len
)(1, tbl
->arg
);
488 case (TBL_CELL_LONG
):
490 case (TBL_CELL_CENTRE
):
492 case (TBL_CELL_LEFT
):
494 case (TBL_CELL_RIGHT
):
495 tblcalc_literal(tbl
, col
, dp
);
497 case (TBL_CELL_NUMBER
):
498 tblcalc_number(tbl
, col
, tp
, dp
);
500 case (TBL_CELL_DOWN
):
509 tblcalc_literal(struct rofftbl
*tbl
, struct roffcol
*col
,
510 const struct tbl_dat
*dp
)
512 size_t sz
, bufsz
, spsz
;
516 * Calculate our width and use the spacing, with a minimum
517 * spacing dictated by position (centre, e.g,. gets a space on
518 * either side, while right/left get a single adjacent space).
522 str
= dp
->string
? dp
->string
: "";
523 sz
= (*tbl
->slen
)(str
, tbl
->arg
);
525 /* FIXME: TBL_DATA_HORIZ et al.? */
528 switch (dp
->layout
->pos
) {
529 case (TBL_CELL_LONG
):
531 case (TBL_CELL_CENTRE
):
532 bufsz
= (*tbl
->len
)(1, tbl
->arg
);
535 bufsz
= (*tbl
->len
)(1, tbl
->arg
);
539 if (dp
->layout
->spacing
) {
540 spsz
= (*tbl
->len
)(dp
->layout
->spacing
, tbl
->arg
);
541 bufsz
= bufsz
> spsz
? bufsz
: spsz
;
550 tblcalc_number(struct rofftbl
*tbl
, struct roffcol
*col
,
551 const struct tbl
*tp
, const struct tbl_dat
*dp
)
554 size_t sz
, psz
, ssz
, d
;
560 * First calculate number width and decimal place (last + 1 for
561 * no-decimal numbers). If the stored decimal is subsequent
562 * ours, make our size longer by that difference
563 * (right-"shifting"); similarly, if ours is subsequent the
564 * stored, then extend the stored size by the difference.
565 * Finally, re-assign the stored values.
568 str
= dp
->string
? dp
->string
: "";
569 sz
= (*tbl
->slen
)(str
, tbl
->arg
);
571 /* FIXME: TBL_DATA_HORIZ et al.? */
573 buf
[0] = tp
->decimal
;
576 psz
= (*tbl
->slen
)(buf
, tbl
->arg
);
578 if (NULL
!= (cp
= strrchr(str
, tp
->decimal
))) {
580 for (ssz
= 0, i
= 0; cp
!= &str
[i
]; i
++) {
582 ssz
+= (*tbl
->slen
)(buf
, tbl
->arg
);
590 sz
+= (*tbl
->len
)(2, tbl
->arg
);
591 d
+= (*tbl
->len
)(1, tbl
->arg
);
593 /* Adjust the settings for this column. */
595 if (col
->decimal
> d
) {
596 sz
+= col
->decimal
- d
;
599 col
->width
+= d
- col
->decimal
;
603 if (d
> col
->decimal
)
606 /* Adjust for stipulated width. */
608 if (col
->width
< dp
->layout
->spacing
)
609 col
->width
= dp
->layout
->spacing
;