summaryrefslogtreecommitdiff
path: root/src/preproc/eqn/pile.cpp
blob: 239e81b33fee30a9aceadb925aee46878bdf8c10 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
// -*- C++ -*-
/* Copyright (C) 1989, 1990, 1991, 1992, 2004, 2007, 2009
   Free Software Foundation, Inc.
     Written by James Clark (jjc@jclark.com)

This file is part of groff.

groff is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free
Software Foundation, either version 3 of the License, or
(at your option) any later version.

groff is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
for more details.

You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
// piles and matrices

#include "eqn.h"
#include "pbox.h"

// SUP_RAISE_FORMAT gives the first baseline
// BASELINE_SEP_FORMAT gives the separation between baselines

int pile_box::compute_metrics(int style)
{
  int i;
  for (i = 0; i < col.len; i++)
    col.p[i]->compute_metrics(style);
  printf(".nr " WIDTH_FORMAT " 0", uid);
  for (i = 0; i < col.len; i++)
    printf(">?\\n[" WIDTH_FORMAT "]", col.p[i]->uid);
  printf("\n");
  printf(".nr " BASELINE_SEP_FORMAT " %dM",
	 uid, baseline_sep+col.space);
  for (i = 1; i < col.len; i++)
    printf(">?(\\n[" DEPTH_FORMAT "]+\\n[" HEIGHT_FORMAT "]+%dM)",
	   col.p[i-1]->uid, col.p[i]->uid, default_rule_thickness*5);
  // round it so that it's a multiple of the vertical resolution
  printf("/\\n(.V+(\\n(.V/2)*\\n(.V\n");

  printf(".nr " SUP_RAISE_FORMAT " \\n[" BASELINE_SEP_FORMAT "]*%d/2"
	 "+%dM\n",
	 uid, uid, col.len-1, axis_height - shift_down);
  printf(".nr " HEIGHT_FORMAT " \\n[" SUP_RAISE_FORMAT "]+\\n["
	 HEIGHT_FORMAT "]\n",
	 uid, uid, col.p[0]->uid);
  printf(".nr " DEPTH_FORMAT " \\n[" BASELINE_SEP_FORMAT "]*%d+\\n["
	 DEPTH_FORMAT "]-\\n[" SUP_RAISE_FORMAT "]\n",
	 uid, uid, col.len-1, col.p[col.len-1]->uid, uid);
  return FOUND_NOTHING;
}

void pile_box::output()
{
  if (output_format == troff) {
    int i;
    printf("\\v'-\\n[" SUP_RAISE_FORMAT "]u'", uid);
    for (i = 0; i < col.len; i++) {
      switch (col.align) {
      case LEFT_ALIGN:
	break;
      case CENTER_ALIGN:
	printf("\\h'\\n[" WIDTH_FORMAT "]u-\\n[" WIDTH_FORMAT "]u/2u'",
	       uid, col.p[i]->uid);
	break;
      case RIGHT_ALIGN:
	printf("\\h'\\n[" WIDTH_FORMAT "]u-\\n[" WIDTH_FORMAT "]u'",
	       uid, col.p[i]->uid);
	break;
      default:
	assert(0);
      }
      col.p[i]->output();
      printf("\\h'-\\n[" WIDTH_FORMAT "]u'", col.p[i]->uid);
      switch (col.align) {
      case LEFT_ALIGN:
	break;
      case CENTER_ALIGN:
	printf("\\h'\\n[" WIDTH_FORMAT "]u-\\n[" WIDTH_FORMAT "]u/2u'",
	       col.p[i]->uid, uid);
	break;
      case RIGHT_ALIGN:
	printf("\\h'\\n[" WIDTH_FORMAT "]u-\\n[" WIDTH_FORMAT "]u'",
	       col.p[i]->uid, uid);
	break;
      default:
	assert(0);
      }
      if (i != col.len - 1)
	printf("\\v'\\n[" BASELINE_SEP_FORMAT "]u'", uid);
    }
    printf("\\v'\\n[" SUP_RAISE_FORMAT "]u'", uid);
    printf("\\v'-(%du*\\n[" BASELINE_SEP_FORMAT "]u)'", col.len - 1, uid);
    printf("\\h'\\n[" WIDTH_FORMAT "]u'", uid);
  }
  else if (output_format == mathml) {
    const char *av;
    switch (col.align) {
    case LEFT_ALIGN:
      av = "left";
      break;
    case RIGHT_ALIGN:
      av = "right";
      break;
    case CENTER_ALIGN:
      av = "center";
      break;
    default:
      assert(0);
    }
    printf("<mtable columnalign='%s'>", av);
    for (int i = 0; i < col.len; i++) {
      printf("<mtr><mtd>");
      col.p[i]->output();
      printf("</mtd></mtr>");
    }
    printf("</mtable>");
  }
}

pile_box::pile_box(box *pp) : col(pp)
{
}

void pile_box::check_tabs(int level)
{
  col.list_check_tabs(level);
}

void pile_box::debug_print()
{
  col.debug_print("pile");
}

int matrix_box::compute_metrics(int style)
{
  int i, j;
  int max_len = 0;
  int space = 0;
  for (i = 0; i < len; i++) {
    for (j = 0; j < p[i]->len; j++)
      p[i]->p[j]->compute_metrics(style);
    if (p[i]->len > max_len)
      max_len = p[i]->len;
    if (p[i]->space > space)
      space = p[i]->space;
  }
  for (i = 0; i < len; i++) {
    printf(".nr " COLUMN_WIDTH_FORMAT " 0", uid, i);
    for (j = 0; j < p[i]->len; j++)
      printf(">?\\n[" WIDTH_FORMAT "]", p[i]->p[j]->uid);
    printf("\n");
  }
  printf(".nr " WIDTH_FORMAT " %dM",
	 uid, column_sep*(len-1)+2*matrix_side_sep);
  for (i = 0; i < len; i++)
    printf("+\\n[" COLUMN_WIDTH_FORMAT "]", uid, i);
  printf("\n");
  printf(".nr " BASELINE_SEP_FORMAT " %dM",
	 uid, baseline_sep+space);
  for (i = 0; i < len; i++)
    for (j = 1; j < p[i]->len; j++)
      printf(">?(\\n[" DEPTH_FORMAT "]+\\n[" HEIGHT_FORMAT "]+%dM)",
	   p[i]->p[j-1]->uid, p[i]->p[j]->uid, default_rule_thickness*5);
  // round it so that it's a multiple of the vertical resolution
  printf("/\\n(.V+(\\n(.V/2)*\\n(.V\n");
  printf(".nr " SUP_RAISE_FORMAT " \\n[" BASELINE_SEP_FORMAT "]*%d/2"
	 "+%dM\n",
	 uid, uid, max_len-1, axis_height - shift_down);
  printf(".nr " HEIGHT_FORMAT " 0\\n[" SUP_RAISE_FORMAT "]+(0",
	 uid, uid);
  for (i = 0; i < len; i++)
    printf(">?\\n[" HEIGHT_FORMAT "]", p[i]->p[0]->uid);
  printf(")>?0\n");
  printf(".nr " DEPTH_FORMAT " \\n[" BASELINE_SEP_FORMAT "]*%d-\\n["
	 SUP_RAISE_FORMAT "]+(0",
	 uid, uid, max_len-1, uid);
  for (i = 0; i < len; i++)
    if (p[i]->len == max_len)
      printf(">?\\n[" DEPTH_FORMAT "]", p[i]->p[max_len-1]->uid);
  printf(")>?0\n");
  return FOUND_NOTHING;
}

void matrix_box::output()
{
  if (output_format == troff) {
    printf("\\h'%dM'", matrix_side_sep);
    for (int i = 0; i < len; i++) {
      int j;
      printf("\\v'-\\n[" SUP_RAISE_FORMAT "]u'", uid);
      for (j = 0; j < p[i]->len; j++) {
	switch (p[i]->align) {
	case LEFT_ALIGN:
	  break;
	case CENTER_ALIGN:
	  printf("\\h'\\n[" COLUMN_WIDTH_FORMAT "]u-\\n[" WIDTH_FORMAT "]u/2u'",
		 uid, i, p[i]->p[j]->uid);
	  break;
	case RIGHT_ALIGN:
	  printf("\\h'\\n[" COLUMN_WIDTH_FORMAT "]u-\\n[" WIDTH_FORMAT "]u'",
		 uid, i, p[i]->p[j]->uid);
	  break;
	default:
	  assert(0);
	}
	p[i]->p[j]->output();
	printf("\\h'-\\n[" WIDTH_FORMAT "]u'", p[i]->p[j]->uid);
	switch (p[i]->align) {
	case LEFT_ALIGN:
	  break;
	case CENTER_ALIGN:
	  printf("\\h'\\n[" WIDTH_FORMAT "]u-\\n[" COLUMN_WIDTH_FORMAT "]u/2u'",
		 p[i]->p[j]->uid, uid, i);
	  break;
	case RIGHT_ALIGN:
	  printf("\\h'\\n[" WIDTH_FORMAT "]u-\\n[" COLUMN_WIDTH_FORMAT "]u'",
		 p[i]->p[j]->uid, uid, i);
	  break;
	default:
	  assert(0);
	}
	if (j != p[i]->len - 1)
	  printf("\\v'\\n[" BASELINE_SEP_FORMAT "]u'", uid);
      }
      printf("\\v'\\n[" SUP_RAISE_FORMAT "]u'", uid);
      printf("\\v'-(%du*\\n[" BASELINE_SEP_FORMAT "]u)'", p[i]->len - 1, uid);
      printf("\\h'\\n[" COLUMN_WIDTH_FORMAT "]u'", uid, i);
      if (i != len - 1)
	printf("\\h'%dM'", column_sep);
    }
    printf("\\h'%dM'", matrix_side_sep);
  }
  else if (output_format == mathml) {
    int n = p[0]->len;	// Each column must have the same number of rows in it
    printf("<mtable>");
    for (int i = 0; i < n; i++) {
      printf("<mtr>");
      for (int j = 0; j < len; j++) {
	const char *av;
	switch (p[j]->align) {
	case LEFT_ALIGN:
	  av = "left";
	  break;
	case RIGHT_ALIGN:
	  av = "right";
	  break;
	case CENTER_ALIGN:
	  av = "center";
	  break;
	default:
	  assert(0);
	}
	printf("<mtd columnalign='%s'>", av);
	p[j]->p[i]->output();
	printf("</mtd>");
      }
      printf("</mtr>");
    }
    printf("</mtable>");
  }
}

matrix_box::matrix_box(column *pp)
{
  p = new column*[10];
  for (int i = 0; i < 10; i++)
    p[i] = 0;
  maxlen = 10;
  len = 1;
  p[0] = pp;
}

matrix_box::~matrix_box()
{
  for (int i = 0; i < len; i++)
    delete p[i];
  a_delete p;
}

void matrix_box::append(column *pp)
{
  if (len + 1 > maxlen) {
    column **oldp = p;
    maxlen *= 2;
    p = new column*[maxlen];
    memcpy(p, oldp, sizeof(column*)*len);
    a_delete oldp;
  }
  p[len++] = pp;
}

void matrix_box::check_tabs(int level)
{
  for (int i = 0; i < len; i++)
    p[i]->list_check_tabs(level);
}

void matrix_box::debug_print()
{
  fprintf(stderr, "matrix { ");
  p[0]->debug_print("col");
  for (int i = 1; i < len; i++) {
    fprintf(stderr, " ");
    p[i]->debug_print("col");
  }
  fprintf(stderr, " }");
}

column::column(box *pp) : box_list(pp), align(CENTER_ALIGN), space(0)
{
}

void column::set_alignment(alignment a)
{
  align = a;
}

void column::set_space(int n)
{
  space = n;
}

void column::debug_print(const char *s)
{
  char c = '\0';		// shut up -Wall
  switch (align) {
  case LEFT_ALIGN:
    c = 'l';
    break;
  case RIGHT_ALIGN:
    c = 'r';
    break;
  case CENTER_ALIGN:
    c = 'c';
    break;
  default:
    assert(0);
  }
  fprintf(stderr, "%c%s %d { ", c, s, space);
  list_debug_print(" above ");
  fprintf(stderr, " }");
}