]> git.cameronkatri.com Git - mandoc.git/blob - out.c
Moved output definitions into main.h.
[mandoc.git] / out.c
1 /* $Id: out.c,v 1.4 2009/10/09 06:54:11 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], hasd;
50 int i;
51 enum roffscale unit;
52
53 i = hasd = 0;
54
55 switch (*src) {
56 case ('+'):
57 src++;
58 break;
59 case ('-'):
60 buf[i++] = *src++;
61 break;
62 default:
63 break;
64 }
65
66 while (i < BUFSIZ) {
67 if ( ! isdigit((u_char)*src)) {
68 if ('.' != *src)
69 break;
70 else if (hasd)
71 break;
72 else
73 hasd = 1;
74 }
75 buf[i++] = *src++;
76 }
77
78 if (BUFSIZ == i || (*src && *(src + 1)))
79 return(0);
80
81 buf[i] = '\0';
82
83 switch (*src) {
84 case ('c'):
85 unit = SCALE_CM;
86 break;
87 case ('i'):
88 unit = SCALE_IN;
89 break;
90 case ('P'):
91 unit = SCALE_PC;
92 break;
93 case ('p'):
94 unit = SCALE_PT;
95 break;
96 case ('f'):
97 unit = SCALE_FS;
98 break;
99 case ('v'):
100 unit = SCALE_VS;
101 break;
102 case ('m'):
103 unit = SCALE_EM;
104 break;
105 case ('\0'):
106 /* FALLTHROUGH */
107 case ('u'):
108 unit = SCALE_BU;
109 break;
110 case ('M'):
111 unit = SCALE_MM;
112 break;
113 case ('n'):
114 unit = SCALE_EN;
115 break;
116 default:
117 return(0);
118 }
119
120 if ((dst->scale = atof(buf)) < 0)
121 dst->scale = 0;
122 dst->unit = unit;
123 dst->pt = hasd;
124
125 return(1);
126 }