Sync-to-go: src/pre-eqn..
[s-roff.git] / src / pre-eqn / pile.cpp
blob2f81ea85c6c96cf91a964744daf2d9b0c0382b7d
1 /*@ piles and matrices.
3 * Copyright (c) 2014 - 2015 Steffen (Daode) Nurpmeso <sdaoden@users.sf.net>.
5 * Copyright (C) 1989 - 1992, 2004, 2007
6 * Free Software Foundation, Inc.
7 * Written by James Clark (jjc@jclark.com)
9 * This is free software; you can redistribute it and/or modify it under
10 * the terms of the GNU General Public License as published by the Free
11 * Software Foundation; either version 2, or (at your option) any later
12 * version.
14 * This is distributed in the hope that it will be useful, but WITHOUT ANY
15 * WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 * for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with groff; see the file COPYING. If not, write to the Free Software
21 * Foundation, 51 Franklin St - Fifth Floor, Boston, MA 02110-1301, USA.
24 #include "config.h"
25 #include "eqn-config.h"
27 #include "eqn.h"
28 #include "pbox.h"
30 // SUP_RAISE_FORMAT gives the first baseline
31 // BASELINE_SEP_FORMAT gives the separation between baselines
33 int pile_box::compute_metrics(int style)
35 int i;
36 for (i = 0; i < col.len; i++)
37 col.p[i]->compute_metrics(style);
38 printf(".nr " WIDTH_FORMAT " 0", uid);
39 for (i = 0; i < col.len; i++)
40 printf(">?\\n[" WIDTH_FORMAT "]", col.p[i]->uid);
41 printf("\n");
42 printf(".nr " BASELINE_SEP_FORMAT " %dM",
43 uid, baseline_sep+col.space);
44 for (i = 1; i < col.len; i++)
45 printf(">?(\\n[" DEPTH_FORMAT "]+\\n[" HEIGHT_FORMAT "]+%dM)",
46 col.p[i-1]->uid, col.p[i]->uid, default_rule_thickness*5);
47 // round it so that it's a multiple of the vertical resolution
48 printf("+(\\n(.V/2)/\\n(.V*\\n(.V\n");
50 printf(".nr " SUP_RAISE_FORMAT " \\n[" BASELINE_SEP_FORMAT "]*%d/2"
51 "+%dM\n",
52 uid, uid, col.len-1, axis_height - shift_down);
53 printf(".nr " HEIGHT_FORMAT " \\n[" SUP_RAISE_FORMAT "]+\\n["
54 HEIGHT_FORMAT "]\n",
55 uid, uid, col.p[0]->uid);
56 printf(".nr " DEPTH_FORMAT " \\n[" BASELINE_SEP_FORMAT "]*%d+\\n["
57 DEPTH_FORMAT "]-\\n[" SUP_RAISE_FORMAT "]\n",
58 uid, uid, col.len-1, col.p[col.len-1]->uid, uid);
59 return FOUND_NOTHING;
62 void pile_box::output()
64 if (output_format == troff) {
65 int i;
66 printf("\\v'-\\n[" SUP_RAISE_FORMAT "]u'", uid);
67 for (i = 0; i < col.len; i++) {
68 switch (col.align) {
69 case LEFT_ALIGN:
70 break;
71 case CENTER_ALIGN:
72 printf("\\h'\\n[" WIDTH_FORMAT "]u-\\n[" WIDTH_FORMAT "]u/2u'",
73 uid, col.p[i]->uid);
74 break;
75 case RIGHT_ALIGN:
76 printf("\\h'\\n[" WIDTH_FORMAT "]u-\\n[" WIDTH_FORMAT "]u'",
77 uid, col.p[i]->uid);
78 break;
79 default:
80 assert(0);
82 col.p[i]->output();
83 printf("\\h'-\\n[" WIDTH_FORMAT "]u'", col.p[i]->uid);
84 switch (col.align) {
85 case LEFT_ALIGN:
86 break;
87 case CENTER_ALIGN:
88 printf("\\h'\\n[" WIDTH_FORMAT "]u-\\n[" WIDTH_FORMAT "]u/2u'",
89 col.p[i]->uid, uid);
90 break;
91 case RIGHT_ALIGN:
92 printf("\\h'\\n[" WIDTH_FORMAT "]u-\\n[" WIDTH_FORMAT "]u'",
93 col.p[i]->uid, uid);
94 break;
95 default:
96 assert(0);
98 if (i != col.len - 1)
99 printf("\\v'\\n[" BASELINE_SEP_FORMAT "]u'", uid);
101 printf("\\v'\\n[" SUP_RAISE_FORMAT "]u'", uid);
102 printf("\\v'-(%du*\\n[" BASELINE_SEP_FORMAT "]u)'", col.len - 1, uid);
103 printf("\\h'\\n[" WIDTH_FORMAT "]u'", uid);
105 else if (output_format == mathml) {
106 const char *av;
107 switch (col.align) {
108 case LEFT_ALIGN:
109 av = "left";
110 break;
111 case RIGHT_ALIGN:
112 av = "right";
113 break;
114 case CENTER_ALIGN:
115 av = "center";
116 break;
117 default:
118 assert(0);
120 printf("<mtable columnalign='%s'>", av);
121 for (int i = 0; i < col.len; i++) {
122 printf("<mtr><mtd>");
123 col.p[i]->output();
124 printf("</mtd></mtr>");
126 printf("</mtable>");
130 pile_box::pile_box(box *pp) : col(pp)
134 void pile_box::check_tabs(int level)
136 col.list_check_tabs(level);
139 void pile_box::debug_print()
141 col.debug_print("pile");
144 int matrix_box::compute_metrics(int style)
146 int i, j;
147 int max_len = 0;
148 int space = 0;
149 for (i = 0; i < len; i++) {
150 for (j = 0; j < p[i]->len; j++)
151 p[i]->p[j]->compute_metrics(style);
152 if (p[i]->len > max_len)
153 max_len = p[i]->len;
154 if (p[i]->space > space)
155 space = p[i]->space;
157 for (i = 0; i < len; i++) {
158 printf(".nr " COLUMN_WIDTH_FORMAT " 0", uid, i);
159 for (j = 0; j < p[i]->len; j++)
160 printf(">?\\n[" WIDTH_FORMAT "]", p[i]->p[j]->uid);
161 printf("\n");
163 printf(".nr " WIDTH_FORMAT " %dM",
164 uid, column_sep*(len-1)+2*matrix_side_sep);
165 for (i = 0; i < len; i++)
166 printf("+\\n[" COLUMN_WIDTH_FORMAT "]", uid, i);
167 printf("\n");
168 printf(".nr " BASELINE_SEP_FORMAT " %dM",
169 uid, baseline_sep+space);
170 for (i = 0; i < len; i++)
171 for (j = 1; j < p[i]->len; j++)
172 printf(">?(\\n[" DEPTH_FORMAT "]+\\n[" HEIGHT_FORMAT "]+%dM)",
173 p[i]->p[j-1]->uid, p[i]->p[j]->uid, default_rule_thickness*5);
174 // round it so that it's a multiple of the vertical resolution
175 printf("+(\\n(.V/2)/\\n(.V*\\n(.V\n");
176 printf(".nr " SUP_RAISE_FORMAT " \\n[" BASELINE_SEP_FORMAT "]*%d/2"
177 "+%dM\n",
178 uid, uid, max_len-1, axis_height - shift_down);
179 printf(".nr " HEIGHT_FORMAT " 0\\n[" SUP_RAISE_FORMAT "]+(0",
180 uid, uid);
181 for (i = 0; i < len; i++)
182 printf(">?\\n[" HEIGHT_FORMAT "]", p[i]->p[0]->uid);
183 printf(")>?0\n");
184 printf(".nr " DEPTH_FORMAT " \\n[" BASELINE_SEP_FORMAT "]*%d-\\n["
185 SUP_RAISE_FORMAT "]+(0",
186 uid, uid, max_len-1, uid);
187 for (i = 0; i < len; i++)
188 if (p[i]->len == max_len)
189 printf(">?\\n[" DEPTH_FORMAT "]", p[i]->p[max_len-1]->uid);
190 printf(")>?0\n");
191 return FOUND_NOTHING;
194 void matrix_box::output()
196 if (output_format == troff) {
197 printf("\\h'%dM'", matrix_side_sep);
198 for (int i = 0; i < len; i++) {
199 int j;
200 printf("\\v'-\\n[" SUP_RAISE_FORMAT "]u'", uid);
201 for (j = 0; j < p[i]->len; j++) {
202 switch (p[i]->align) {
203 case LEFT_ALIGN:
204 break;
205 case CENTER_ALIGN:
206 printf("\\h'\\n[" COLUMN_WIDTH_FORMAT "]u-\\n[" WIDTH_FORMAT "]u/2u'",
207 uid, i, p[i]->p[j]->uid);
208 break;
209 case RIGHT_ALIGN:
210 printf("\\h'\\n[" COLUMN_WIDTH_FORMAT "]u-\\n[" WIDTH_FORMAT "]u'",
211 uid, i, p[i]->p[j]->uid);
212 break;
213 default:
214 assert(0);
216 p[i]->p[j]->output();
217 printf("\\h'-\\n[" WIDTH_FORMAT "]u'", p[i]->p[j]->uid);
218 switch (p[i]->align) {
219 case LEFT_ALIGN:
220 break;
221 case CENTER_ALIGN:
222 printf("\\h'\\n[" WIDTH_FORMAT "]u-\\n[" COLUMN_WIDTH_FORMAT "]u/2u'",
223 p[i]->p[j]->uid, uid, i);
224 break;
225 case RIGHT_ALIGN:
226 printf("\\h'\\n[" WIDTH_FORMAT "]u-\\n[" COLUMN_WIDTH_FORMAT "]u'",
227 p[i]->p[j]->uid, uid, i);
228 break;
229 default:
230 assert(0);
232 if (j != p[i]->len - 1)
233 printf("\\v'\\n[" BASELINE_SEP_FORMAT "]u'", uid);
235 printf("\\v'\\n[" SUP_RAISE_FORMAT "]u'", uid);
236 printf("\\v'-(%du*\\n[" BASELINE_SEP_FORMAT "]u)'", p[i]->len - 1, uid);
237 printf("\\h'\\n[" COLUMN_WIDTH_FORMAT "]u'", uid, i);
238 if (i != len - 1)
239 printf("\\h'%dM'", column_sep);
241 printf("\\h'%dM'", matrix_side_sep);
243 else if (output_format == mathml) {
244 int n = p[0]->len; // Each column must have the same number of rows in it
245 printf("<mtable>");
246 for (int i = 0; i < n; i++) {
247 printf("<mtr>");
248 for (int j = 0; j < len; j++) {
249 const char *av;
250 switch (p[j]->align) {
251 case LEFT_ALIGN:
252 av = "left";
253 break;
254 case RIGHT_ALIGN:
255 av = "right";
256 break;
257 case CENTER_ALIGN:
258 av = "center";
259 break;
260 default:
261 assert(0);
263 printf("<mtd columnalign='%s'>", av);
264 p[j]->p[i]->output();
265 printf("</mtd>");
267 printf("</mtr>");
269 printf("</mtable>");
273 matrix_box::matrix_box(column *pp)
275 p = new column*[10];
276 for (int i = 0; i < 10; i++)
277 p[i] = 0;
278 maxlen = 10;
279 len = 1;
280 p[0] = pp;
283 matrix_box::~matrix_box()
285 for (int i = 0; i < len; i++)
286 delete p[i];
287 a_delete p;
290 void matrix_box::append(column *pp)
292 if (len + 1 > maxlen) {
293 column **oldp = p;
294 maxlen *= 2;
295 p = new column*[maxlen];
296 memcpy(p, oldp, sizeof(column*)*len);
297 a_delete oldp;
299 p[len++] = pp;
302 void matrix_box::check_tabs(int level)
304 for (int i = 0; i < len; i++)
305 p[i]->list_check_tabs(level);
308 void matrix_box::debug_print()
310 fprintf(stderr, "matrix { ");
311 p[0]->debug_print("col");
312 for (int i = 1; i < len; i++) {
313 fprintf(stderr, " ");
314 p[i]->debug_print("col");
316 fprintf(stderr, " }");
319 column::column(box *pp) : box_list(pp), align(CENTER_ALIGN), space(0)
323 void column::set_alignment(alignment a)
325 align = a;
328 void column::set_space(int n)
330 space = n;
333 void column::debug_print(const char *s)
335 char c = '\0'; // shut up -Wall
336 switch (align) {
337 case LEFT_ALIGN:
338 c = 'l';
339 break;
340 case RIGHT_ALIGN:
341 c = 'r';
342 break;
343 case CENTER_ALIGN:
344 c = 'c';
345 break;
346 default:
347 assert(0);
349 fprintf(stderr, "%c%s %d { ", c, s, space);
350 list_debug_print(" above ");
351 fprintf(stderr, " }");
354 // s-it2-mode