fmt: new hyphenation support with penalties
[neatroff.git] / out.c
blob68c25368a2ce4b5dcffff188aab0d0a880266ca3
1 /* generating troff output */
2 #include <ctype.h>
3 #include <stdarg.h>
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <string.h>
7 #include "roff.h"
9 static int out_nl = 1;
11 /* output troff code; newlines may appear only at the end of s */
12 static void out_out(char *s, va_list ap)
14 out_nl = strchr(s, '\n') != NULL;
15 vfprintf(stdout, s, ap);
18 /* output troff code; no preceding newline is necessary */
19 static void outnn(char *s, ...)
21 va_list ap;
22 va_start(ap, s);
23 out_out(s, ap);
24 va_end(ap);
27 /* output troff cmd; should appear after a newline */
28 void out(char *s, ...)
30 va_list ap;
31 if (!out_nl)
32 outnn("\n");
33 va_start(ap, s);
34 out_out(s, ap);
35 va_end(ap);
38 static int o_s = 10;
39 static int o_f = 1;
40 static int o_m = 0;
42 static void out_ps(int n)
44 if (o_s != n) {
45 o_s = n;
46 out("s%d\n", o_s);
50 static void out_ft(int n)
52 if (n >= 0 && o_f != n) {
53 o_f = n;
54 out("f%d\n", o_f);
58 static void out_clr(int n)
60 if (n >= 0 && o_m != n) {
61 o_m = n;
62 out("m%s\n", clr_str(o_m));
66 static int tok_num(char **s, int scale)
68 char tok[ILNLEN];
69 char *d = tok;
70 while (isspace(**s))
71 (*s)++;
72 while (**s && !isspace(**s))
73 *d++ = *(*s)++;
74 *d = '\0';
75 return eval(tok, scale);
78 static void out_draw(char *s)
80 int c = *s++;
81 out("D%c", c);
82 switch (tolower(c)) {
83 case 'l':
84 outnn(" %d", tok_num(&s, 'm'));
85 outnn(" %d", tok_num(&s, 'v'));
86 outnn(" ."); /* dpost requires this */
87 break;
88 case 'c':
89 outnn(" %d", tok_num(&s, 'm'));
90 break;
91 case 'e':
92 outnn(" %d", tok_num(&s, 'm'));
93 outnn(" %d", tok_num(&s, 'v'));
94 break;
95 case 'a':
96 outnn(" %d", tok_num(&s, 'm'));
97 outnn(" %d", tok_num(&s, 'v'));
98 outnn(" %d", tok_num(&s, 'm'));
99 outnn(" %d", tok_num(&s, 'v'));
100 break;
101 case '~':
102 case 'p':
103 outnn(" %d", tok_num(&s, 'm'));
104 outnn(" %d", tok_num(&s, 'v'));
105 while (*s) {
106 outnn(" %d", tok_num(&s, 'm'));
107 outnn(" %d", tok_num(&s, 'v'));
109 break;
111 outnn("\n");
114 static void outg(char *c, int fn)
116 int ofn = o_f;
117 out_ft(fn);
118 if (utf8one(c))
119 outnn("c%s%s", c, c[1] ? "\n" : "");
120 else
121 out("C%s\n", c[0] == c_ec && c[1] == '(' ? c + 2 : c);
122 out_ft(ofn);
125 static void outc(char *c)
127 struct glyph *g = dev_glyph(c, o_f);
128 int cwid, bwid;
129 if (!g)
130 return;
131 cwid = charwid(o_f, o_s, g->wid);
132 bwid = DEVWID(o_s, g->wid);
133 if (font_mapped(g->font, c))
134 c = g->name;
135 if (dev_getcs(o_f))
136 outnn("h%d", (cwid - bwid) / 2);
137 outg(c, dev_fontpos(g->font));
138 if (dev_getbd(o_f)) {
139 outnn("h%d", dev_getbd(o_f) - 1);
140 outg(c, dev_fontpos(g->font));
141 outnn("h%d", -dev_getbd(o_f) + 1);
143 if (dev_getcs(o_f))
144 outnn("h%d", -(cwid - bwid) / 2);
145 outnn("h%d", cwid);
148 void out_line(char *s)
150 char c[ILNLEN + GNLEN * 4];
151 int t;
152 while ((t = escread(&s, c)) >= 0) {
153 if (!t) {
154 if (c[0] == c_ni || (c[0] == '\\' && c[1] == '\\')) {
155 c[0] = c[1];
156 c[1] = '\0';
158 if (c[0] == '\t' || c[0] == '\x01' ||
159 !strcmp(c_hc, c) || !strcmp(c_bp, c))
160 continue;
161 outc(cmap_map(c));
162 continue;
164 switch (t) {
165 case 'D':
166 out_draw(c);
167 break;
168 case 'f':
169 out_ft(dev_pos(c));
170 break;
171 case 'h':
172 outnn("h%d", eval(c, 'm'));
173 break;
174 case 'm':
175 if (!n_cp)
176 out_clr(clr_get(c));
177 break;
178 case 's':
179 out_ps(eval_re(c, o_s, '\0'));
180 break;
181 case 'v':
182 outnn("v%d", eval(c, 'v'));
183 break;
184 case 'X':
185 out("x X %s\n", c);
186 break;