aboutsummaryrefslogtreecommitdiff
path: root/contrib/groff/src/preproc/pic/troff.cc
blob: 62fe540ae3a634bfa144a889dcc70b10b02ae5cb (plain) (blame)
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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
// -*- C++ -*-
/* Copyright (C) 1989, 1990, 1991, 1992, 2000, 2001
   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 2, 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 groff; see the file COPYING.  If not, write to the Free Software
Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */

#include "pic.h"
#include "common.h"
#include "htmlindicate.h"


const double RELATIVE_THICKNESS = -1.0;
const double BAD_THICKNESS = -2.0;

class simple_output : public common_output {
  virtual void simple_line(const position &, const position &) = 0;
  virtual void simple_spline(const position &, const position *, int n) = 0;
  virtual void simple_arc(const position &, const position &,
			  const position &) = 0;
  virtual void simple_circle(int, const position &, double rad) = 0;
  virtual void simple_ellipse(int, const position &, const distance &) = 0;
  virtual void simple_polygon(int, const position *, int) = 0;
  virtual void line_thickness(double) = 0;
  virtual void set_fill(double) = 0;
  void dot(const position &, const line_type &) = 0;
public:
  void start_picture(double sc, const position &ll, const position &ur) = 0;
  void finish_picture() = 0;
  void text(const position &, text_piece *, int, double) = 0;
  void line(const position &, const position *, int n,
	    const line_type &);
  void polygon(const position *, int n,
	       const line_type &, double);
  void spline(const position &, const position *, int n,
	      const line_type &);
  void arc(const position &, const position &, const position &,
	   const line_type &);
  void circle(const position &, double rad, const line_type &, double);
  void ellipse(const position &, const distance &, const line_type &, double);
  int supports_filled_polygons();
};

int simple_output::supports_filled_polygons()
{
  return driver_extension_flag != 0;
}

void simple_output::arc(const position &start, const position &cent,
			const position &end, const line_type &lt)
{
  switch (lt.type) {
  case line_type::solid:
    line_thickness(lt.thickness);
    simple_arc(start, cent, end);
    break;
  case line_type::invisible:
    break;
  case line_type::dashed:
    dashed_arc(start, cent, end, lt);
    break;
  case line_type::dotted:
    dotted_arc(start, cent, end, lt);
    break;
  }
}

void simple_output::line(const position &start, const position *v, int n,
			 const line_type &lt)
{
  position pos = start;
  line_thickness(lt.thickness);
  for (int i = 0; i < n; i++) {
    switch (lt.type) {
    case line_type::solid:
      simple_line(pos, v[i]);
      break;
    case line_type::dotted:
      {
	distance vec(v[i] - pos);
	double dist = hypot(vec);
	int ndots = int(dist/lt.dash_width + .5);
	if (ndots == 0)
	  dot(pos, lt);
	else {
	  vec /= double(ndots);
	  for (int j = 0; j <= ndots; j++)
	    dot(pos + vec*j, lt);
	}
      }
      break;
    case line_type::dashed:
      {
	distance vec(v[i] - pos);
	double dist = hypot(vec);
	if (dist <= lt.dash_width*2.0)
	  simple_line(pos, v[i]);
	else {
	  int ndashes = int((dist - lt.dash_width)/(lt.dash_width*2.0) + .5);
	  distance dash_vec = vec*(lt.dash_width/dist);
	  double dash_gap = (dist - lt.dash_width)/ndashes;
	  distance dash_gap_vec = vec*(dash_gap/dist);
	  for (int j = 0; j <= ndashes; j++) {
	    position s(pos + dash_gap_vec*j);
	    simple_line(s, s + dash_vec);
	  }
	}
      }
      break;
    case line_type::invisible:
      break;
    default:
      assert(0);
    }
    pos = v[i];
  }
}

void simple_output::spline(const position &start, const position *v, int n,
			   const line_type &lt)
{
  line_thickness(lt.thickness);
  simple_spline(start, v, n);
}

void simple_output::polygon(const position *v, int n,
			    const line_type &lt, double fill)
{
  if (driver_extension_flag) {
    if (fill >= 0.0) {
      set_fill(fill);
      simple_polygon(1, v, n);
    }
  }
  if (lt.type == line_type::solid && driver_extension_flag) {
    line_thickness(lt.thickness);
    simple_polygon(0, v, n);
  }
  else if (lt.type != line_type::invisible) {
    line_thickness(lt.thickness);
    line(v[n - 1], v, n, lt);
  }
}

void simple_output::circle(const position &cent, double rad,
			   const line_type &lt, double fill)
{
  if (driver_extension_flag && fill >= 0.0) {
    set_fill(fill);
    simple_circle(1, cent, rad);
  }
  line_thickness(lt.thickness);
  switch (lt.type) {
  case line_type::invisible:
    break;
  case line_type::dashed:
    dashed_circle(cent, rad, lt);
    break;
  case line_type::dotted:
    dotted_circle(cent, rad, lt);
    break;
  case line_type::solid:
    simple_circle(0, cent, rad);
    break;
  default:
    assert(0);
  }
}

void simple_output::ellipse(const position &cent, const distance &dim,
			    const line_type &lt, double fill)
{
  if (driver_extension_flag && fill >= 0.0) {
    set_fill(fill);
    simple_ellipse(1, cent, dim);
  }
  if (lt.type != line_type::invisible)
    line_thickness(lt.thickness);
  switch (lt.type) {
  case line_type::invisible:
    break;
  case line_type::dotted:
  case line_type::dashed:
  case line_type::solid:
    simple_ellipse(0, cent, dim);
    break;
  default:
    assert(0);
  }
}

#define FILL_MAX 1000

class troff_output : public simple_output {
  const char *last_filename;
  position upper_left;
  double height;
  double scale;
  double last_line_thickness;
  double last_fill;
public:
  troff_output();
  ~troff_output();
  void start_picture(double, const position &ll, const position &ur);
  void finish_picture();
  void text(const position &, text_piece *, int, double);
  void dot(const position &, const line_type &);
  void command(const char *, const char *, int);
  void set_location(const char *, int);
  void simple_line(const position &, const position &);
  void simple_spline(const position &, const position *, int n);
  void simple_arc(const position &, const position &, const position &);
  void simple_circle(int, const position &, double rad);
  void simple_ellipse(int, const position &, const distance &);
  void simple_polygon(int, const position *, int);
  void line_thickness(double p);
  void set_fill(double);
  position transform(const position &);
};

output *make_troff_output()
{
  return new troff_output;
}

troff_output::troff_output()
: last_filename(0), last_line_thickness(BAD_THICKNESS), last_fill(-1.0)
{
}

troff_output::~troff_output()
{
}

inline position troff_output::transform(const position &pos)
{
  return position((pos.x - upper_left.x)/scale,
		  (upper_left.y - pos.y)/scale);
}

#define FILL_REG "00"

// If this register > 0, then pic will generate \X'ps: ...' commands
// if the aligned attribute is used.
#define GROPS_REG "0p"

// If this register is defined, geqn won't produce `\x's.
#define EQN_NO_EXTRA_SPACE_REG "0x"

void troff_output::start_picture(double sc,
				 const position &ll, const position &ur)
{
  upper_left.x = ll.x;
  upper_left.y = ur.y;
  scale = compute_scale(sc, ll, ur);
  height = (ur.y - ll.y)/scale;
  double width = (ur.x - ll.x)/scale;
  graphic_start(0);
  printf(".PS %.3fi %.3fi", height, width);
  if (args)
    printf(" %s\n", args);
  else
    putchar('\n');
  printf(".\\\" %g %g %g %g\n", ll.x, ll.y, ur.x, ur.y);
  printf(".\\\" %.3fi %.3fi %.3fi %.3fi\n", 0.0, height, width, 0.0);
  printf(".nr " FILL_REG " \\n(.u\n.nf\n");
  printf(".nr " EQN_NO_EXTRA_SPACE_REG " 1\n");
  // This guarantees that if the picture is used in a diversion it will
  // have the right width.
  printf("\\h'%.3fi'\n.sp -1\n", width);
}

void troff_output::finish_picture()
{
  line_thickness(BAD_THICKNESS);
  last_fill = -1.0;		// force it to be reset for each picture
  if (!flyback_flag)
    printf(".sp %.3fi+1\n", height);
  printf(".if \\n(" FILL_REG " .fi\n");
  printf(".br\n");
  printf(".nr " EQN_NO_EXTRA_SPACE_REG " 0\n");
  // this is a little gross
  set_location(current_filename, current_lineno);
  fputs(flyback_flag ? ".PF\n" : ".PE\n", stdout);
  graphic_end();
}

void troff_output::command(const char *s,
			   const char *filename, int lineno)
{
  if (filename != 0)
    set_location(filename, lineno);
  fputs(s, stdout);
  putchar('\n');
}

void troff_output::simple_circle(int filled, const position &cent, double rad)
{
  position c = transform(cent);
  printf("\\h'%.3fi'"
	 "\\v'%.3fi'"
	 "\\D'%c%.3fi'"
	 "\n.sp -1\n",
	 c.x - rad/scale,
	 c.y,
	 (filled ? 'C' : 'c'),
	 rad*2.0/scale);
}

void troff_output::simple_ellipse(int filled, const position &cent,
				  const distance &dim)
{
  position c = transform(cent);
  printf("\\h'%.3fi'"
	 "\\v'%.3fi'"
	 "\\D'%c%.3fi %.3fi'"
	 "\n.sp -1\n",
	 c.x - dim.x/(2.0*scale),
	 c.y,
	 (filled ? 'E' : 'e'),
	 dim.x/scale, dim.y/scale);
}

void troff_output::simple_arc(const position &start, const distance &cent,
			      const distance &end)
{
  position s = transform(start);
  position c = transform(cent);
  distance cv = c - s;
  distance ev = transform(end) - c;
  printf("\\h'%.3fi'"
	 "\\v'%.3fi'"
	 "\\D'a%.3fi %.3fi %.3fi %.3fi'"
	 "\n.sp -1\n",
	 s.x, s.y, cv.x, cv.y, ev.x, ev.y);
}

void troff_output::simple_line(const position &start, const position &end)
{
  position s = transform(start);
  distance ev = transform(end) - s;
  printf("\\h'%.3fi'"
	 "\\v'%.3fi'"
	 "\\D'l%.3fi %.3fi'"
	 "\n.sp -1\n",
	 s.x, s.y, ev.x, ev.y);
}

void troff_output::simple_spline(const position &start,
				 const position *v, int n)
{
  position pos = transform(start);
  printf("\\h'%.3fi'"
	 "\\v'%.3fi'",
	 pos.x, pos.y);
  fputs("\\D'~", stdout);
  for (int i = 0; i < n; i++) {
    position temp = transform(v[i]);
    distance d = temp - pos;
    pos = temp;
    if (i != 0)
      putchar(' ');
    printf("%.3fi %.3fi", d.x, d.y);
  }
  printf("'\n.sp -1\n");
}

// a solid polygon

void troff_output::simple_polygon(int filled, const position *v, int n)
{
  position pos = transform(v[0]);
  printf("\\h'%.3fi'"
	 "\\v'%.3fi'",
	 pos.x, pos.y);
  printf("\\D'%c", (filled ? 'P' : 'p'));
  for (int i = 1; i < n; i++) {
    position temp = transform(v[i]);
    distance d = temp - pos;
    pos = temp;
    if (i != 1)
      putchar(' ');
    printf("%.3fi %.3fi", d.x, d.y);
  }
  printf("'\n.sp -1\n");
}

const double TEXT_AXIS = 0.22;	// in ems

static const char *choose_delimiter(const char *text)
{
  if (strchr(text, '\'') == 0)
    return "'";
  else
    return "\\(ts";
}

void troff_output::text(const position &center, text_piece *v, int n,
			double ang)
{
  line_thickness(BAD_THICKNESS); // the text might use lines (eg in equations)
  int rotate_flag = 0;
  if (driver_extension_flag && ang != 0.0) {
    rotate_flag = 1;
    position c = transform(center);
    printf(".if \\n(" GROPS_REG " \\{\\\n"
	   "\\h'%.3fi'"
	   "\\v'%.3fi'"
	   "\\X'ps: exec gsave currentpoint 2 copy translate %.4f rotate neg exch neg exch translate'"
	   "\n.sp -1\n"
	   ".\\}\n",
	   c.x, c.y, -ang*180.0/M_PI);
  }
  for (int i = 0; i < n; i++)
    if (v[i].text != 0 && *v[i].text != '\0') {
      position c = transform(center);
      if (v[i].filename != 0)
	set_location(v[i].filename, v[i].lineno);
      printf("\\h'%.3fi", c.x);
      const char *delim = choose_delimiter(v[i].text);
      if (v[i].adj.h == RIGHT_ADJUST)
	printf("-\\w%s%s%su", delim, v[i].text, delim);
      else if (v[i].adj.h != LEFT_ADJUST)
	printf("-(\\w%s%s%su/2u)", delim, v[i].text, delim);
      putchar('\'');
      printf("\\v'%.3fi-(%dv/2u)+%dv+%.2fm",
	     c.y,
	     n - 1,
	     i,
	     TEXT_AXIS);
      if (v[i].adj.v == ABOVE_ADJUST)
	printf("-.5v");
      else if (v[i].adj.v == BELOW_ADJUST)
	printf("+.5v");
      putchar('\'');
      fputs(v[i].text, stdout);
      fputs("\n.sp -1\n", stdout);
    }
  if (rotate_flag)
    printf(".if '\\*(.T'ps' \\{\\\n"
	   "\\X'ps: exec grestore'\n.sp -1\n"
	   ".\\}\n");
}

void troff_output::line_thickness(double p)
{
  if (p < 0.0)
    p = RELATIVE_THICKNESS;
  if (driver_extension_flag && p != last_line_thickness) {
    printf("\\D't %.3fp'\\h'%.3fp'\n.sp -1\n", p, -p);
    last_line_thickness = p;
  }
}

void troff_output::set_fill(double f)
{
  if (driver_extension_flag && f != last_fill) {
    printf("\\D'f %du'\\h'%du'\n.sp -1\n", int(f*FILL_MAX), -int(f*FILL_MAX));
    last_fill = f;
  }
}

const double DOT_AXIS = .044;

void troff_output::dot(const position &cent, const line_type &lt)
{
  if (driver_extension_flag) {
    line_thickness(lt.thickness);
    simple_line(cent, cent);
  }
  else {
    position c = transform(cent);
    printf("\\h'%.3fi-(\\w'.'u/2u)'"
	   "\\v'%.3fi+%.2fm'"
	   ".\n.sp -1\n",
	   c.x,
	   c.y, 
	   DOT_AXIS);
  }
}

void troff_output::set_location(const char *s, int n)
{
  if (last_filename != 0 && strcmp(s, last_filename) == 0)
    printf(".lf %d\n", n);
  else {
    printf(".lf %d %s\n", n, s);
    last_filename = s;
  }
}