1 /* $Id: eqn_html.c,v 1.11 2017/01/17 01:47:51 schwarze Exp $ */
3 * Copyright (c) 2011, 2014 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2017 Ingo Schwarze <schwarze@openbsd.org>
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.
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.
20 #include <sys/types.h>
32 eqn_box(struct html *p, const struct eqn_box *bp)
34 struct tag *post, *row, *cell, *t;
35 const struct eqn_box *child, *parent;
44 * Special handling for a matrix, which is presented to us in
45 * column order, but must be printed in row-order.
47 if (EQN_MATRIX == bp->type) {
48 if (NULL == bp->first)
50 if (EQN_LIST != bp->first->type) {
51 eqn_box(p, bp->first);
54 if (NULL == (parent = bp->first->first))
56 /* Estimate the number of rows, first. */
57 if (NULL == (child = parent->first))
59 for (rows = 0; NULL != child; rows++)
61 /* Print row-by-row. */
62 post = print_otag(p, TAG_MTABLE, "");
63 for (i = 0; i < rows; i++) {
64 parent = bp->first->first;
65 row = print_otag(p, TAG_MTR, "");
66 while (NULL != parent) {
67 child = parent->first;
68 for (j = 0; j < i; j++) {
73 cell = print_otag(p, TAG_MTD, "");
75 * If we have no data for this
76 * particular cell, then print a
77 * placeholder and continue--don't puke.
80 eqn_box(p, child->first);
82 parent = parent->next;
91 post = print_otag(p, TAG_MOVER, "");
94 post = print_otag(p, TAG_MSUP, "");
97 post = print_otag(p, TAG_MUNDER, "");
100 post = print_otag(p, TAG_MSUB, "");
103 post = print_otag(p, TAG_MFRAC, "");
105 case (EQNPOS_FROMTO):
106 post = print_otag(p, TAG_MUNDEROVER, "");
108 case (EQNPOS_SUBSUP):
109 post = print_otag(p, TAG_MSUBSUP, "");
112 post = print_otag(p, TAG_MSQRT, "");
118 if (bp->top || bp->bottom) {
119 assert(NULL == post);
120 if (bp->top && NULL == bp->bottom)
121 post = print_otag(p, TAG_MOVER, "");
122 else if (bp->top && bp->bottom)
123 post = print_otag(p, TAG_MUNDEROVER, "");
125 post = print_otag(p, TAG_MUNDER, "");
128 if (EQN_PILE == bp->type) {
129 assert(NULL == post);
130 if (bp->first != NULL && bp->first->type == EQN_LIST)
131 post = print_otag(p, TAG_MTABLE, "");
132 } else if (bp->type == EQN_LIST &&
133 bp->parent && bp->parent->type == EQN_PILE) {
134 assert(NULL == post);
135 post = print_otag(p, TAG_MTR, "");
136 print_otag(p, TAG_MTD, "");
139 if (NULL != bp->text) {
140 assert(NULL == post);
141 post = print_otag(p, TAG_MI, "");
142 print_text(p, bp->text);
143 } else if (NULL == post) {
144 if (NULL != bp->left || NULL != bp->right)
145 post = print_otag(p, TAG_MFENCED, "??",
146 "open", bp->left == NULL ? "" : bp->left,
147 "close", bp->right == NULL ? "" : bp->right);
149 post = print_otag(p, TAG_MROW, "");
151 print_otag(p, TAG_MROW, "");
154 eqn_box(p, bp->first);
157 if (NULL != bp->bottom) {
158 t = print_otag(p, TAG_MO, "");
159 print_text(p, bp->bottom);
162 if (NULL != bp->top) {
163 t = print_otag(p, TAG_MO, "");
164 print_text(p, bp->top);
171 eqn_box(p, bp->next);
175 print_eqn(struct html *p, const struct eqn *ep)
179 t = print_otag(p, TAG_MATH, "c", "eqn");
181 p->flags |= HTML_NONOSPACE;
182 eqn_box(p, ep->root);
183 p->flags &= ~HTML_NONOSPACE;