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