summaryrefslogtreecommitdiff
path: root/filter/hpgl-vector.c
blob: dba76c9a35e27e5ec36240f5b9542b5609693c2b (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
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
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
/*
 * "$Id: hpgl-vector.c 6649 2007-07-11 21:46:42Z mike $"
 *
 *   HP-GL/2 vector routines for the Common UNIX Printing System (CUPS).
 *
 *   Copyright 2007 by Apple Inc.
 *   Copyright 1993-2007 by Easy Software Products.
 *
 *   These coded instructions, statements, and computer programs are the
 *   property of Apple Inc. and are protected by Federal copyright
 *   law.  Distribution and use rights are outlined in the file "LICENSE.txt"
 *   which should have been included with this file.  If this file is
 *   file is missing or damaged, see the license at "http://www.cups.org/".
 *
 *   This file is subject to the Apple OS-Developed Software exception.
 *
 * Contents:
 *
 *   AA_arc_absolute()    - Draw an arc.
 *   AR_arc_relative()    - Draw an arc relative to the current pen
 *   AT_arc_absolute3()   - Draw an arc using 3 points.
 *   CI_circle()          - Draw a circle.
 *   PA_plot_absolute()   - Plot a line using absolute coordinates.
 *   PD_pen_down()        - Start drawing.
 *   PE_polygon_encoded() - Draw an encoded polyline.
 *   PR_plot_relative()   - Plot a line using relative coordinates.
 *   PU_pen_up()          - Stop drawing.
 *   RT_arc_relative3()   - Draw an arc through 3 points relative to the
 *   decode_number()      - Decode an encoded number.
 *   plot_points()        - Plot the specified points.
 */

/*
 * Include necessary headers...
 */

#include "hpgltops.h"


/*
 * Local functions...
 */

static double	decode_number(unsigned char **, int, double);
static void	plot_points(int, param_t *);


/*
 * 'AA_arc_absolute()' - Draw an arc.
 */

void
AA_arc_absolute(int     num_params,	/* I - Number of parameters */
                param_t *params)	/* I - Parameters */
{
  float x, y,				/* Transformed coordinates */
	dx, dy;				/* Distance from current pen */
  float start, end,			/* Start and end angles */
	theta,				/* Current angle */
	dt,				/* Step between points */
	radius;				/* Radius of arc */


  if (num_params < 3)
    return;

  x = Transform[0][0] * params[0].value.number +
      Transform[0][1] * params[1].value.number +
      Transform[0][2];
  y = Transform[1][0] * params[0].value.number +
      Transform[1][1] * params[1].value.number +
      Transform[1][2];

  dx = PenPosition[0] - x;
  dy = PenPosition[1] - y;

  start = (float)(180.0 * atan2(dy, dx) / M_PI);
  if (start < 0.0)
    start += 360.0f;

  end    = start + params[2].value.number;
  radius = (float)hypot(dx, dy);

  if (PenDown)
  {
    if (num_params > 3 && params[3].value.number > 0.0)
      dt = (float)fabs(params[3].value.number);
    else
      dt = 5.0;

    if (!PolygonMode)
      Outputf("MP\n");

    PenValid = 1;

    Outputf("%.3f %.3f MO\n", PenPosition[0], PenPosition[1]);

    if (start < end)
      for (theta = start + dt; theta < end; theta += dt)
      {
	PenPosition[0] = (float)(x + radius * cos(M_PI * theta / 180.0));
	PenPosition[1] = (float)(y + radius * sin(M_PI * theta / 180.0));

	Outputf("%.3f %.3f LI\n", PenPosition[0], PenPosition[1]);
      }
    else
      for (theta = start - dt; theta > end; theta -= dt)
      {
	PenPosition[0] = (float)(x + radius * cos(M_PI * theta / 180.0));
	PenPosition[1] = (float)(y + radius * sin(M_PI * theta / 180.0));

	Outputf("%.3f %.3f LI\n", PenPosition[0], PenPosition[1]);
      }
  }

  PenPosition[0] = (float)(x + radius * cos(M_PI * end / 180.0));
  PenPosition[1] = (float)(y + radius * sin(M_PI * end / 180.0));

  if (PenDown)
  {
    Outputf("%.3f %.3f LI\n", PenPosition[0], PenPosition[1]);

    if (!PolygonMode)
      Outputf("ST\n");
  }
}


/*
 * 'AR_arc_relative()' - Draw an arc relative to the current pen
 *                       position.
 */

void
AR_arc_relative(int     num_params,	/* I - Number of parameters */
                param_t *params)	/* I - Parameters */
{
  float x, y,				/* Transformed coordinates */
	dx, dy;				/* Distance from current pen */
  float start, end,			/* Start and end angles */
	theta,				/* Current angle */
	dt,				/* Step between points */
	radius;				/* Radius of arc */


  if (num_params < 3)
    return;

  x = Transform[0][0] * params[0].value.number +
      Transform[0][1] * params[1].value.number +
      PenPosition[0];
  y = Transform[1][0] * params[0].value.number +
      Transform[1][1] * params[1].value.number +
      PenPosition[1];

  dx = PenPosition[0] - x;
  dy = PenPosition[1] - y;

  start = (float)(180.0 * atan2(dy, dx) / M_PI);
  if (start < 0.0)
    start += 360.0f;

  end    = start + params[2].value.number;
  radius = (float)hypot(dx, dy);

  if (PenDown)
  {
    if (num_params > 3 && params[3].value.number > 0.0)
      dt = (float)fabs(params[3].value.number);
    else
      dt = 5.0;

    if (!PolygonMode)
      Outputf("MP\n");

    PenValid = 1;

    Outputf("%.3f %.3f MO\n", PenPosition[0], PenPosition[1]);

    if (start < end)
      for (theta = start + dt; theta < end; theta += dt)
      {
	PenPosition[0] = (float)(x + radius * cos(M_PI * theta / 180.0));
	PenPosition[1] = (float)(y + radius * sin(M_PI * theta / 180.0));

	Outputf("%.3f %.3f LI\n", PenPosition[0], PenPosition[1]);
      }
    else
      for (theta = start - dt; theta > end; theta -= dt)
      {
	PenPosition[0] = (float)(x + radius * cos(M_PI * theta / 180.0));
	PenPosition[1] = (float)(y + radius * sin(M_PI * theta / 180.0));

	Outputf("%.3f %.3f LI\n", PenPosition[0], PenPosition[1]);
      }
  }

  PenPosition[0] = (float)(x + radius * cos(M_PI * end / 180.0));
  PenPosition[1] = (float)(y + radius * sin(M_PI * end / 180.0));

  if (PenDown)
  {
    Outputf("%.3f %.3f LI\n", PenPosition[0], PenPosition[1]);

    if (!PolygonMode)
      Outputf("ST\n");
  }
}


/*
 * 'AT_arc_absolute3()' - Draw an arc using 3 points.
 *
 * Note:
 *
 *   Currently this only draws two line segments through the
 *   specified points.
 */

void
AT_arc_absolute3(int     num_params,	/* I - Number of parameters */
                 param_t *params)	/* I - Parameters */
{
  if (num_params < 4)
    return;

  if (PenDown)
  {
    if (!PolygonMode)
      Outputf("MP\n");

    PenValid = 1;

    Outputf("%.3f %.3f MO\n", PenPosition[0], PenPosition[1]);

    PenPosition[0] = Transform[0][0] * params[0].value.number +
                     Transform[0][1] * params[1].value.number +
                     Transform[0][2];
    PenPosition[1] = Transform[1][0] * params[0].value.number +
                     Transform[1][1] * params[1].value.number +
                     Transform[1][2];

    Outputf("%.3f %.3f LI\n", PenPosition[0], PenPosition[1]);
  }

  PenPosition[0] = Transform[0][0] * params[2].value.number +
                   Transform[0][1] * params[3].value.number +
                   Transform[0][2];
  PenPosition[1] = Transform[1][0] * params[2].value.number +
                   Transform[1][1] * params[3].value.number +
                   Transform[1][2];

  if (PenDown)
  {
    Outputf("%.3f %.3f LI\n", PenPosition[0], PenPosition[1]);

    if (!PolygonMode)
      Outputf("ST\n");
  }
}


/*
 * 'CI_circle()' - Draw a circle.
 */

void
CI_circle(int     num_params,	/* I - Number of parameters */
          param_t *params)	/* I - Parameters */
{
  float x, y;			/* Transformed coordinates */
  float theta,			/* Current angle */
	dt,			/* Step between points */
	radius;			/* Radius of circle */


  if (num_params < 1)
    return;

  if (!PenDown)
    return;

  radius = params[0].value.number;

  if (num_params > 1)
    dt = (float)fabs(params[1].value.number);
  else
    dt = 5.0;

  if (!PolygonMode)
    Outputf("MP\n");

  PenValid = 1;

  for (theta = 0.0; theta < 360.0; theta += dt)
  {
    x = (float)(PenPosition[0] +
                radius * cos(M_PI * theta / 180.0) * Transform[0][0] +
                radius * sin(M_PI * theta / 180.0) * Transform[0][1]);
    y = (float)(PenPosition[1] +
                radius * cos(M_PI * theta / 180.0) * Transform[1][0] +
                radius * sin(M_PI * theta / 180.0) * Transform[1][1]);

    Outputf("%.3f %.3f %s\n", x, y, theta == 0.0 ? "MO" : "LI");
  }

  Outputf("CP\n");
  if (!PolygonMode)
    Outputf("ST\n");
}


/*
 * 'PA_plot_absolute()' - Plot a line using absolute coordinates.
 */

void
PA_plot_absolute(int     num_params,	/* I - Number of parameters */
                 param_t *params)	/* I - Parameters */
{
  PenMotion = 0;

  if (num_params > 1)
    plot_points(num_params, params);
}


/*
 * 'PD_pen_down()' - Start drawing.
 */

void
PD_pen_down(int     num_params,		/* I - Number of parameters */
            param_t *params)		/* I - Parameters */
{
  PenDown = 1;

  if (num_params > 1)
    plot_points(num_params, params);
}


/*
 * 'PE_polygon_encoded()' - Draw an encoded polyline.
 */

void
PE_polyline_encoded(int     num_params,	/* I - Number of parameters */
                    param_t *params)	/* I - Parameters */
{
  unsigned char	*s;			/* Pointer into string */
  int		temp,			/* Temporary value */
		base_bits,		/* Data bits per byte */
		draw,			/* Draw or move */
		abscoords;		/* Use absolute coordinates */
  double	tx, ty,			/* Transformed coordinates */
		x, y,			/* Raw coordinates */
		frac_bits;		/* Multiplier for encoded number */


  base_bits = 6;
  frac_bits = 1.0;
  draw      = PenDown;
  abscoords = 0;

  if (num_params == 0)
    return;

  if (!PolygonMode)
  {
    Outputf("MP\n");
    PenValid = 0;
  }

  if (!PenValid)
  {
    Outputf("%.3f %.3f MO\n", PenPosition[0], PenPosition[1]);
    PenValid = 1;
  }

  for (s = (unsigned char *)params[0].value.string; *s != '\0';)
    switch (*s)
    {
      case '7' :
          s ++;
          base_bits = 5;

#ifdef DEBUG
          fputs("DEBUG:     7-bit\n", stderr);
#endif /* DEBUG */

          Outputf("%% PE: 7-bit\n");
          break;
      case ':' :	/* Select pen */
          s ++;
          PenNumber = (int)decode_number(&s, base_bits, 1.0);

#ifdef DEBUG
          fprintf(stderr, "DEBUG:     set pen #%d\n", PenNumber);
#endif /* DEBUG */

          Outputf("%% PE: set pen #%d\n", PenNumber);

	  if (PageDirty)
	    printf("%.3f %.3f %.3f %.2f SP\n", Pens[PenNumber].rgb[0],
		   Pens[PenNumber].rgb[1], Pens[PenNumber].rgb[2],
		   Pens[PenNumber].width * PenScaling);
          break;
      case '<' :	/* Next coords are a move-to */
          draw = 0;
          s ++;

#ifdef DEBUG
          fputs("DEBUG:     moveto\n", stderr);
#endif /* DEBUG */

	  Outputf("%% PE: moveto\n");
          break;
      case '>' :	/* Set fractional bits */
          s ++;
          temp      = (int)decode_number(&s, base_bits, 1.0);
          frac_bits = 1.0 / (1 << temp);

#ifdef DEBUG
          fprintf(stderr, "DEBUG:     set fractional bits %d\n", temp);
#endif /* DEBUG */

          Outputf("%% PE: set fractional bits %d\n", temp);
          break;
      case '=' :	/* Next coords are absolute */
          s ++;
          abscoords = 1;

#ifdef DEBUG
          fputs("DEBUG:     absolute\n", stderr);
#endif /* DEBUG */

          Outputf("%% PE: absolute\n");
          break;
      default :
          if (*s >= 63)
          {
           /*
            * Coordinate...
            */

            x = decode_number(&s, base_bits, frac_bits);
            y = decode_number(&s, base_bits, frac_bits);

#ifdef DEBUG
            fprintf(stderr, "DEBUG:     coords %.3f %.3f\n", x, y);
#endif /* DEBUG */

            Outputf("%% PE: coords %.3f %.3f\n", x, y);

            if (abscoords)
            {
	      tx = Transform[0][0] * x + Transform[0][1] * y +
        	   Transform[0][2];
	      ty = Transform[1][0] * x + Transform[1][1] * y +
        	   Transform[1][2];
	    }
	    else if (x == 0.0 && y == 0.0)
	    {
	      draw = 1;
	      continue;
	    }
	    else
	    {
	      tx = Transform[0][0] * x + Transform[0][1] * y +
        	   PenPosition[0];
	      ty = Transform[1][0] * x + Transform[1][1] * y +
        	   PenPosition[1];
	    }

            if (draw)
	    {
	      if (fabs(PenPosition[0] - tx) > 0.001 ||
        	  fabs(PenPosition[1] - ty) > 0.001)
        	Outputf("%.3f %.3f LI\n", tx, ty);
            }
	    else
              Outputf("%.3f %.3f MO\n", tx, ty);

	    PenPosition[0] = (float)tx;
	    PenPosition[1] = (float)ty;

	    draw           = 1;
	    abscoords      = 0;
          }
          else
          {
           /*
            * Junk - ignore...
            */

            if (*s != '\n' && *s != '\r')
              fprintf(stderr, "WARNING: ignoring illegal PE char \'%c\'...\n", *s);
            s ++;
          }
          break;
    }

  if (!PolygonMode)
    Outputf("ST\n");
}


/*
 * 'PR_plot_relative()' - Plot a line using relative coordinates.
 */

void
PR_plot_relative(int     num_params,	/* I - Number of parameters */
                 param_t *params)	/* I - Parameters */
{
  PenMotion = 1;

  if (num_params > 1)
    plot_points(num_params, params);
}


/*
 * 'PU_pen_up()' - Stop drawing.
 */

void
PU_pen_up(int     num_params,	/* I - Number of parameters */
          param_t *params)	/* I - Parameters */
{
  PenDown = 0;

  if (num_params > 1)
    plot_points(num_params, params);
}


/*
 * 'RT_arc_relative3()' - Draw an arc through 3 points relative to the
 *                        current pen position.
 *
 * Note:
 *
 *   This currently only draws two line segments through the specified
 *   points.
 */

void
RT_arc_relative3(int     num_params,	/* I - Number of parameters */
                 param_t *params)	/* I - Parameters */
{
  if (num_params < 4)
    return;

  if (PenDown)
  {
    if (!PolygonMode)
      Outputf("MP\n");

    PenValid = 1;

    Outputf("%.3f %.3f MO\n", PenPosition[0], PenPosition[1]);

    PenPosition[0] = Transform[0][0] * params[0].value.number +
                     Transform[0][1] * params[1].value.number +
                     PenPosition[0];
    PenPosition[1] = Transform[1][0] * params[0].value.number +
                     Transform[1][1] * params[1].value.number +
                     PenPosition[1];

    Outputf("%.3f %.3f LI\n", PenPosition[0], PenPosition[1]);
  }

  PenPosition[0] = Transform[0][0] * params[2].value.number +
                   Transform[0][1] * params[3].value.number +
                   PenPosition[0];
  PenPosition[1] = Transform[1][0] * params[2].value.number +
                   Transform[1][1] * params[3].value.number +
                   PenPosition[1];

  if (PenDown)
  {
    Outputf("%.3f %.3f LI\n", PenPosition[0], PenPosition[1]);

    if (!PolygonMode)
      Outputf("ST\n");
  }
}


/*
 * 'decode_number()' - Decode an encoded number.
 */

static double				/* O - Value */
decode_number(unsigned char **s,	/* IO - String to decode */
              int           base_bits,	/* I - Number of data bits per byte */
	      double        frac_bits)	/* I - Multiplier for fractional data */
{
  double	temp,		/* Current value */
		shift;		/* Multiplier */
  int		sign;		/* Sign of result */


  sign = 0;

  if (base_bits == 5)
  {
    for (temp = 0.0, shift = frac_bits * 0.5; **s != '\0'; (*s) ++)
      if (**s >= 95 && **s < 127)
      {
        if (sign == 0)
        {
          if ((**s - 95) & 1)
            sign = -1;
          else
            sign = 1;

          temp += ((**s - 95) & ~1) * shift;
        }
        else
          temp += (**s - 95) * shift;
        break;
      }
      else if (**s < 63)
      {
        if (**s != '\r' && **s != '\n')
          fprintf(stderr, "DEBUG: Bad PE character 0x%02X!\n", **s);

        continue;
      }
      else
      {
        if (sign == 0)
        {
          if ((**s - 63) & 1)
            sign = -1;
          else
            sign = 1;

          temp += ((**s - 63) & ~1) * shift;
        }
        else
          temp += (**s - 63) * shift;

	shift *= 32.0;
      }
  }
  else
  {
    for (temp = 0.0, shift = frac_bits * 0.5; **s != '\0'; (*s) ++)
      if (**s >= 191 && **s < 255)
      {
        if (sign == 0)
        {
          if ((**s - 191) & 1)
            sign = -1;
          else
            sign = 1;

          temp += ((**s - 191) & ~1) * shift;
        }
        else
          temp += (**s - 191) * shift;
        break;
      }
      else if (**s < 63)
      {
        if (**s != '\r' && **s != '\n')
          fprintf(stderr, "DEBUG: Bad PE character 0x%02X!\n", **s);

        continue;
      }
      else
      {
        if (sign == 0)
        {
          if ((**s - 63) & 1)
            sign = -1;
          else
            sign = 1;

          temp += ((**s - 63) & ~1) * shift;
        }
        else
          temp += (**s - 63) * shift;

        shift *= 64.0;
      }
  }

  (*s) ++;

  return (temp * sign);
}


/*
 * 'plot_points()' - Plot the specified points.
 */

static void
plot_points(int     num_params,	/* I - Number of parameters */
            param_t *params)	/* I - Parameters */
{
  int	i;			/* Looping var */
  float	x, y;			/* Transformed coordinates */


  if (PenDown)
  {
    if (!PolygonMode)
    {
      Outputf("MP\n");
      Outputf("%.3f %.3f MO\n", PenPosition[0], PenPosition[1]);

      PenValid = 1;
    }
  }

  for (i = 0; i < num_params; i += 2)
  {
    if (PenMotion == 0)
    {
      x = Transform[0][0] * params[i + 0].value.number +
          Transform[0][1] * params[i + 1].value.number +
          Transform[0][2];
      y = Transform[1][0] * params[i + 0].value.number +
          Transform[1][1] * params[i + 1].value.number +
          Transform[1][2];
    }
    else
    {
      x = Transform[0][0] * params[i + 0].value.number +
          Transform[0][1] * params[i + 1].value.number +
          PenPosition[0];
      y = Transform[1][0] * params[i + 0].value.number +
          Transform[1][1] * params[i + 1].value.number +
          PenPosition[1];
    }

    if (PenDown)
    {
      if (PolygonMode && i == 0)
        Outputf("%.3f %.3f MO\n", x, y);
      else if (fabs(PenPosition[0] - x) > 0.001 ||
               fabs(PenPosition[1] - y) > 0.001)
        Outputf("%.3f %.3f LI\n", x, y);
    }

    PenPosition[0] = x;
    PenPosition[1] = y;
  }

  if (PenDown)
  {
    if (!PolygonMode)
      Outputf("ST\n");
  }
}


/*
 * End of "$Id: hpgl-vector.c 6649 2007-07-11 21:46:42Z mike $".
 */