xref: /openbsd/usr.bin/mandoc/eqn_term.c (revision d4c8d4a3)
1 /*	$OpenBSD: eqn_term.c,v 1.15 2018/12/13 05:13:15 schwarze Exp $ */
2 /*
3  * Copyright (c) 2011 Kristaps Dzonsons <kristaps@bsd.lv>
4  * Copyright (c) 2014, 2015, 2017 Ingo Schwarze <schwarze@openbsd.org>
5  *
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.
9  *
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.
17  */
18 #include <sys/types.h>
19 
20 #include <assert.h>
21 #include <ctype.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 
26 #include "eqn.h"
27 #include "out.h"
28 #include "term.h"
29 
30 static	const enum termfont fontmap[EQNFONT__MAX] = {
31 	TERMFONT_NONE, /* EQNFONT_NONE */
32 	TERMFONT_NONE, /* EQNFONT_ROMAN */
33 	TERMFONT_BOLD, /* EQNFONT_BOLD */
34 	TERMFONT_BOLD, /* EQNFONT_FAT */
35 	TERMFONT_UNDER /* EQNFONT_ITALIC */
36 };
37 
38 static void	eqn_box(struct termp *, const struct eqn_box *);
39 
40 
41 void
term_eqn(struct termp * p,const struct eqn_box * bp)42 term_eqn(struct termp *p, const struct eqn_box *bp)
43 {
44 
45 	eqn_box(p, bp);
46 	p->flags &= ~TERMP_NOSPACE;
47 }
48 
49 static void
eqn_box(struct termp * p,const struct eqn_box * bp)50 eqn_box(struct termp *p, const struct eqn_box *bp)
51 {
52 	const struct eqn_box *child;
53 	const char *cp;
54 	int delim;
55 
56 	/* Delimiters around this box? */
57 
58 	if ((bp->type == EQN_LIST && bp->expectargs > 1) ||
59 	    (bp->type == EQN_PILE && (bp->prev || bp->next)) ||
60 	    (bp->parent != NULL && (bp->parent->pos == EQNPOS_SQRT ||
61 	    /* Diacritic followed by ^ or _. */
62 	    ((bp->top != NULL || bp->bottom != NULL) &&
63 	     bp->parent->type == EQN_SUBEXPR &&
64 	     bp->parent->pos != EQNPOS_OVER && bp->next != NULL) ||
65 	    /* Nested over, sub, sup, from, to. */
66 	    (bp->type == EQN_SUBEXPR && bp->pos != EQNPOS_SQRT &&
67 	     ((bp->parent->type == EQN_LIST && bp->expectargs == 1) ||
68 	      (bp->parent->type == EQN_SUBEXPR &&
69 	       bp->pos != EQNPOS_SQRT)))))) {
70 		if ((bp->parent->type == EQN_SUBEXPR && bp->prev != NULL) ||
71 		    (bp->type == EQN_LIST &&
72 		     bp->first != NULL &&
73 		     bp->first->type != EQN_PILE &&
74 		     bp->first->type != EQN_MATRIX &&
75 		     bp->prev != NULL &&
76 		     (bp->prev->type == EQN_LIST ||
77 		      (bp->prev->type == EQN_TEXT &&
78 		       (*bp->prev->text == '\\' ||
79 		        isalpha((unsigned char)*bp->prev->text))))))
80 			p->flags |= TERMP_NOSPACE;
81 		term_word(p, bp->left != NULL ? bp->left : "(");
82 		p->flags |= TERMP_NOSPACE;
83 		delim = 1;
84 	} else
85 		delim = 0;
86 
87 	/* Handle Fonts and text. */
88 
89 	if (bp->font != EQNFONT_NONE)
90 		term_fontpush(p, fontmap[(int)bp->font]);
91 
92 	if (bp->text != NULL) {
93 		if (strchr("!\"'),.:;?]}", *bp->text) != NULL)
94 			p->flags |= TERMP_NOSPACE;
95 		term_word(p, bp->text);
96 		if ((cp = strchr(bp->text, '\0')) > bp->text &&
97 		    (strchr("\"'([{", cp[-1]) != NULL ||
98 		     (bp->prev == NULL && (cp[-1] == '-' ||
99 		      (cp >= bp->text + 5 &&
100 		       strcmp(cp - 5, "\\[mi]") == 0)))))
101 			p->flags |= TERMP_NOSPACE;
102 	}
103 
104 	/* Special box types. */
105 
106 	if (bp->pos == EQNPOS_SQRT) {
107 		term_word(p, "\\(sr");
108 		if (bp->first != NULL) {
109 			p->flags |= TERMP_NOSPACE;
110 			eqn_box(p, bp->first);
111 		}
112 	} else if (bp->type == EQN_SUBEXPR) {
113 		child = bp->first;
114 		eqn_box(p, child);
115 		p->flags |= TERMP_NOSPACE;
116 		term_word(p, bp->pos == EQNPOS_OVER ? "/" :
117 		    (bp->pos == EQNPOS_SUP ||
118 		     bp->pos == EQNPOS_TO) ? "^" : "_");
119 		child = child->next;
120 		if (child != NULL) {
121 			p->flags |= TERMP_NOSPACE;
122 			eqn_box(p, child);
123 			if (bp->pos == EQNPOS_FROMTO ||
124 			    bp->pos == EQNPOS_SUBSUP) {
125 				p->flags |= TERMP_NOSPACE;
126 				term_word(p, "^");
127 				p->flags |= TERMP_NOSPACE;
128 				child = child->next;
129 				if (child != NULL)
130 					eqn_box(p, child);
131 			}
132 		}
133 	} else {
134 		child = bp->first;
135 		if (bp->type == EQN_MATRIX &&
136 		    child != NULL &&
137 		    child->type == EQN_LIST &&
138 		    child->expectargs > 1)
139 			child = child->first;
140 		while (child != NULL) {
141 			eqn_box(p,
142 			    bp->type == EQN_PILE &&
143 			    child->type == EQN_LIST &&
144 			    child->expectargs > 1 &&
145 			    child->args == 1 ?
146 			    child->first : child);
147 			child = child->next;
148 		}
149 	}
150 
151 	/* Handle Fonts and diacritics. */
152 
153 	if (bp->font != EQNFONT_NONE)
154 		term_fontpop(p);
155 	if (bp->top != NULL) {
156 		p->flags |= TERMP_NOSPACE;
157 		term_word(p, bp->top);
158 	}
159 	if (bp->bottom != NULL) {
160 		p->flags |= TERMP_NOSPACE;
161 		term_word(p, "_");
162 	}
163 
164 	/* Right delimiter after this box? */
165 
166 	if (delim) {
167 		p->flags |= TERMP_NOSPACE;
168 		term_word(p, bp->right != NULL ? bp->right : ")");
169 		if (bp->parent->type == EQN_SUBEXPR && bp->next != NULL)
170 			p->flags |= TERMP_NOSPACE;
171 	}
172 }
173