]> git.cameronkatri.com Git - mandoc.git/blob - out.c
4b30ad40e43418df7bbf578f18a4a746f27acb4b
[mandoc.git] / out.c
1 /* $Id: out.c,v 1.3 2009/10/07 12:35:24 kristaps Exp $ */
2 /*
3 * Copyright (c) 2009 Kristaps Dzonsons <kristaps@kth.se>
4 *
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.
8 *
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.
16 */
17 #include <sys/types.h>
18
19 #include <ctype.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22
23 #include "out.h"
24
25
26 /*
27 * Convert a `scaling unit' to a consistent form, or fail. Scaling
28 * units are documented in groff.7, which stipulates the following:
29 *
30 * (1) A scaling unit is a signed/unsigned integer/float with or
31 * without a unit.
32 *
33 * (2) The following units exist:
34 * c Centimeter
35 * i Inch
36 * P Pica = 1/6 inch
37 * p Point = 1/72 inch
38 * m Em = the font size in points (width of letter m)
39 * M 100th of an Em
40 * n En = Em/2
41 * u Basic unit for actual output device
42 * v Vertical line space in basic units scaled point =
43 * 1/sizescale of a point (defined in font DESC file)
44 * f Scale by 65536.
45 */
46 int
47 a2roffsu(const char *src, struct roffsu *dst)
48 {
49 char buf[BUFSIZ], *p;
50 int i;
51 enum roffscale unit;
52
53 for (p = buf, i = 0; i < BUFSIZ && isdigit((u_char)*src); i++)
54 *p++ = *src++;
55
56 if (BUFSIZ == i || (*src && *(src + 1)))
57 return(0);
58
59 *p = '\0';
60
61 switch (*src) {
62 case ('c'):
63 unit = SCALE_CM;
64 break;
65 case ('i'):
66 unit = SCALE_IN;
67 break;
68 case ('P'):
69 unit = SCALE_PC;
70 break;
71 case ('p'):
72 unit = SCALE_PT;
73 break;
74 case ('f'):
75 unit = SCALE_FS;
76 break;
77 case ('v'):
78 unit = SCALE_VS;
79 break;
80 case ('m'):
81 unit = SCALE_EM;
82 break;
83 case ('\0'):
84 /* FALLTHROUGH */
85 case ('u'):
86 unit = SCALE_BU;
87 break;
88 case ('M'):
89 unit = SCALE_MM;
90 break;
91 case ('n'):
92 unit = SCALE_EN;
93 break;
94 default:
95 return(0);
96 }
97
98 if ((dst->scale = atoi(buf)) < 0)
99 dst->scale = 0;
100 dst->unit = unit;
101 return(1);
102 }