forked from dgtlrift/hp2xx
/
hpgl.c
5792 lines (5368 loc) · 141 KB
/
hpgl.c
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
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Copyright (c) 1991 - 1994 Heinz W. Werntges. All rights reserved.
Parts Copyright (c) 1999, 2001, 2002, 2003, 2004
Martin Kroeker All rights reserved.
Distributed by Free Software Foundation, Inc.
This file is part of HP2xx.
HP2xx is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY. No author or distributor accepts responsibility
to anyone for the consequences of using it or for whether it serves any
particular purpose or works at all, unless he says so in writing. Refer
to the GNU General Public License, Version 2 or later, for full details.
Everyone is granted permission to copy, modify and redistribute
HP2xx, but only under the conditions described in the GNU General Public
License. A copy of this license is supposed to have been
given to you along with HP2xx so you can know your rights and
responsibilities. It should be in a file named COPYING. Among other
things, the copyright notice and this notice must be preserved on all
copies.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
*/
/** HPGL.c: HPGL parser & i/o part of HP2xx (based on D. Donath's "HPtoGF.c")
**
** 91/01/13 V 1.00 HWW Originating
** 91/01/19 V 1.01 HWW reorganized
** 91/01/24 V 1.02 HWW ESC.-Sequences acknowledged (preliminary!!)
** 91/01/29 V 1.03 HWW Incl. SUN portation
** 91/01/31 V 1.04 HWW Parser: ESC sequences should be skipped now
** 91/02/10 V 1.05 HWW Parser renewed
** 91/02/15 V 1.06 HWW stdlib.h supported
** 91/02/19 V 1.07a HWW parser refined, bugs fixed
** 91/06/09 V 1.08 HWW New options added; some restructuring
** 91/06/16 V 1.09 HWW VGA mode added; some renaming; silent_mode!
** 91/06/20 V 1.10 HWW Rotation added
** 91/10/15 V 1.11 HWW ANSI_C; header files reorganized
** 91/10/20 V 1.11a HWW VAX_C support
** 91/10/25 V 1.11b HWW Support of LT; and LT0; (line type, partial)
** 91/11/20 V 1.12 HWW SPn; support: many changes!
** 91/11/21 V 1.12b HWW First comma in "PA,xxxx,yyyy..." accepted
** 91/12/22 V 1.13 HWW Multiple MOVE compression; "plot_rel", "old_pen"
** 92/01/13 V 1.13c HWW VAX problem with ungetc()/fscanf() fixed; bug fixed
** 92/01/15 V 1.13d HWW "vga" --> "pre"
** 92/01/30 V 1.14c HWW Parser: no need of ';', better portable
** 92/02/06 V 1.15a HWW Parser: AR, AA, CI, read_float() added;
** toupper() removed (MACH problems)
** 92/02/19 V 1.16c HWW LB etc. supported
** 92/02/23 V 1.17b HWW LB etc. improved, PG supported
** 92/02/25 V 1.17c HWW Parser improved: SP, LT, multi-mv suppression
** 92/03/01 V 1.17d HWW Char sizes: debugged
** 92/03/03 V 1.17e HWW LB_Mode introduced
** 92/04/15 V 1.17f HWW Width x Height limit assumed
** 92/05/21 V 1.18a HWW Multiple-file usage
** 92/05/28 V 1.19a HWW XT, YT, TL, SM added
** 92/10/20 V 1.20c HWW More line types added (debugged)
** 92/11/08 V 1.20d HWW Interval of active pages
** 92/12/13 V 1.20e HWW truesize option added
** 93/02/10 V 1.21a HWW Arcs & circles now properly closed;
** Bug fixed: SC does not interfere with last move
** 93/03/10 V 1.21b HWW Bug fixed in LT scanner part
** 93/03/22, V 1.21c HWW HYPOT() workaround for a weird BCC behavior;
** 93/04/02 Line_Generator(): Case *pb==*pa caught
** 93/04/13 V 1.22a HWW UC supported (code by Alois Treindl)
** 93/04/25 V 1.22b HWW LB/PR bug fix by E. Norum included
** 93/05/20 V 1.22c HWW LT1 pattern adjustment (report by E. Frambach)
** 93/09/02 V 1.22d HWW EA (rect) added (by Th. Hiller)
** 94/01/01 V 1.22e HWW Misc. additions suggested by L. Lowe:
** 1) PlotCmd_from_tmpfile(): int --> PlotCmd
** 2) ES: 2nd parameter now optional
** 3) evaluate_HPGL(): center_mode introduced
** 94/02/14 V 1.30a HWW Re-organized; many changes; min/max bug fixed
** 99/02/28 MK IW,CA,CS,SA,SS commands added
** 99/04/24 MK PC,PW commands added
** 99/05/10 MK RO command added (code by rs@daveg.com)
** 99/05/18 MK partial PE support (by Eugene Doudine)
** 99/06/05 MK PC improvements and fixes
** 99/11/30 MK support for fractional PE; PS/RO fixes
** 00/02/06 MK allow commandline overrides for PC/PW
** 00/02/13 MK DV support (backport from delayed 3.4.prealpha)
** 00/02/26 MK ER,EP,FP,FT,PM,PT,RA,RR,WG commands added
** 00/02/27 MK WU command added
** 00/03/02 MK SC types 1 and 2, more robust handling of PE,
** removed PE_line(), split lines() into file reader
** and common linedrawing backend for PD/PA and PE
** added PJL parser and RTL escape sequences
** (all these patches provided by Eugene Doudine)
** 00/03/03 MK convert IW parameters if scaling is in effect
** 00/03/05 MK AT/RT (arc through three points) added
** 01/01/01 MK UL added and PW rewritten (Andrew J.Bird)
** empty PUPD sequence now draws a small dot
** linedrawing fixed (added two moves when IW was
** in effect, ever since IW support was added)
** 01/04/01 MK BR/BZ added
** 01/04/22 MK reset PW and RO flags/values on reinitialization
** (Yuri Strelenko)
** 01/12/04 MK moved reinitialization of n_unknown and n_unexpected
** from reset_HPGL to init_HPGL so that they are not overwritten
** when a single drawing contains several IN statements
** 02/06/02 AJB Moved HYPOT macro to hpgl.h - so we can use it in murphy.c
**/
#define DEBUG_ESC 1
#define DEBUG_ESC_2
/*
#undef DEBUG_ESC
#undef DEBUG_ESC_2
*/
#include <stdio.h>
#include <stdlib.h>
#ifndef _NO_VCL
#include <unistd.h>
#endif
#include <string.h>
#include <ctype.h>
#include <math.h>
#include "bresnham.h"
#include "hp2xx.h"
#include "chardraw.h"
#include "clip.h"
#include "pendef.h"
#include "lindef.h"
#include "hpgl.h"
#define ETX '\003'
/*
#define P1X_default 603.0
#define P1Y_default 521.0
#define P2X_default 10603.0
#define P2Y_default 7721.0
*/
#define P1X_default 0.0 /* drop margins */
#define P1Y_default 0.0
/*
#define P2X_default 11880.
#define P2Y_default 16800.
*/
#define P2X_default 33600. /* A0 media */
#define P2Y_default 47520.
#ifdef NORINT
#define rint(a) (long)(a+0.5)
#endif
/**
** Globals needed in other source files:
**/
LineType CurrentLineType = LT_solid;
short scale_flag = FALSE;
short record_off = FALSE;
long vec_cntr_w = 0L;
short silent_mode = FALSE;
FILE *td;
HPGL_Pt HP_pos = { 0, 0 }; /* Actual plotter pen position */
HPGL_Pt P1 = { P1X_default, P1Y_default }; /* Scaling points */
HPGL_Pt P2 = { P2X_default, P2Y_default };
int iwflag = 0; /*MK */
int mode_vert = 0;
HPGL_Pt C1 = { P1X_default, P1Y_default }; /* Clipping points */
HPGL_Pt C2 = { P2X_default, P2Y_default };
HPGL_Pt S1 = { P1X_default, P1Y_default }; /* Scaled */
HPGL_Pt S2 = { P2X_default, P2Y_default }; /* points */
HPGL_Pt Q = { 1., 1. }; /* Delta-P/Delta-S: Initialized with first SC */
HPGL_Pt M; /* maximum coordinates set by PS instruction */
/**
** Global from chardraw.c:
**/
extern TextPar tp;
/**
** "Local" globals (I know this is messy...) :
**/
static float lxmin, lxmax, lymin, lymax, neg_ticklen, pos_ticklen;
static double Diag_P1_P2, pat_pos;
static HPGL_Pt p_last = { M_PI, M_PI }; /* Init. to "impossible" values */
static HPGL_Pt polygons[MAXPOLY];
static short polygon_penstate[MAXPOLY];
static int vertices = -1;
static short polygon_mode = FALSE;
static int filltype = 1;
static float hatchspace = 0.;
static short hatchscale = FALSE;
static float hatchangle = 0.;
static float saved_hatchspace[4] = { 0., 0., 0. , 0.};
static int saved_hatchscale[4] = { FALSE , FALSE, FALSE, FALSE };
static float saved_hatchangle[4] = { 0., 0. ,0., 0.};
static float thickness = 0.;
static short polygon_penup = FALSE;
static HPGL_Pt anchor = { 100000.0, 100000.0 };
static HPGL_Pt polystart = { 0.0, 0.0 };
static float rot_cos, rot_sin;
static short rotate_flag = FALSE; /* Flags tec external to HP-GL */
static short ps_flag = FALSE;
static short ac_flag = FALSE;
static short pm1_flag = FALSE;
static double rot_ang = 0.;
static double rot_tmp = 0.; /* saved RO value for resetting after drawing */
static short mv_flag = FALSE;
static short pg_flag = FALSE;
static short ct_dist = FALSE;
static int mmsupress= TRUE;
static short fixedcolor = FALSE;
static short fixedwidth = FALSE;
static int first_page = 0;
static int last_page = 0;
static int n_unexpected = 0;
static int n_unknown = 0;
static int page_number = 1;
static long vec_cntr_r = 0L;
static short pen = -1;
static short pens_in_use[NUMPENS];
static short pen_down = FALSE; /* Internal HP-GL book-keeping: */
static short in_place=FALSE;
static short plot_rel = FALSE;
static short saved_penstate = FALSE; /* to track penstate over polygon mode */
static short wu_relative = FALSE;
static int again = FALSE;
static char StrTerm = ETX; /* String terminator char */
static short StrTermSilent = 1; /* only terminates, or prints too */
static char *strbuf = NULL;
static unsigned int strbufsize = MAX_LB_LEN + 1;
static char symbol_char = '\0'; /* Char in Symbol Mode (0=off) */
static unsigned char r_base = 0;
static unsigned char g_base = 0;
static unsigned char b_base = 0;
static unsigned char r_max = 255;
static unsigned char g_max = 255;
static unsigned char b_max = 255;
static int lasterror = 0;
static int initialized = 0;
static int initp1p2 = 0;
static unsigned char **otmp;
/* Known HPGL commands, ASCII-coded as High-byte/low-byte int's */
#define AA 0x4141
#define AC 0x4143
#define AD 0x4144
#define AF 0x4146
#define AH 0x4148
#define AR 0x4152
#define AT 0x4154
#define BL 0x424C
#define BP 0x4250
#define BR 0x4252
#define BZ 0x425A
#define CA 0x4341 /*MK */
#define CI 0x4349
#define CO 0x434F /*AJB*/
#define CP 0x4350
#define CR 0x4352
#define CS 0x4353 /*MK */
#define CT 0x4354
#define DF 0x4446
#define DI 0x4449
#define DR 0x4452
#define DT 0x4454
#define DV 0x4456
#define EA 0x4541
#define EC 0x4543 /*AJB*/
#define EP 0x4550
#define ER 0x4552
#define ES 0x4553
#define EW 0x4557 /*MK */
#define FP 0x4650
#define FT 0x4654
#define IN 0x494E
#define IP 0x4950
#define IR 0x4952
#define IW 0x4957 /*MK */
#define LA 0x4C41 /*AJB*/
#define LB 0x4C42
#define LO 0x4C4F
#define LT 0x4C54
#define MG 0x4D47
#define NP 0x4E50
#define NR 0x4E52
#define OE 0x4F45
#define OI 0x4F49
#define OP 0x4F50
#define OS 0x4F53
#define OW 0x4F57
#define PA 0x5041
#define PB 0x5042
#define PC 0x5043 /*MK */
#define PD 0x5044
#define PE 0x5045
#define PG 0x5047
#define PM 0x504D
#define PR 0x5052
#define PS 0x5053
#define PT 0x5054
#define PU 0x5055
#define PW 0x5057 /*MK */
#define RA 0x5241
#define RF 0x5246
#define RO 0x524F /*RS */
#define RR 0x5252
#define RT 0x5254
#define SA 0x5341 /*MK */
#define SC 0x5343
#define SD 0x5344
#define SI 0x5349
#define SL 0x534C
#define SM 0x534D
#define SP 0x5350
#define SR 0x5352
#define SS 0x5353 /*MK */
#define TL 0x544C
#define UC 0x5543
#define UL 0x554C /*AJB*/
#define VS 0x5653
#define WD 0x5744
#define WG 0x5747
#define WU 0x5755
#define XT 0x5854
#define YT 0x5954
static void par_err_exit(int code, int cmd, FILE * hd)
{
const char *msg;
char tmpstr[21];
long position;
int i;
switch (code) {
case 0:
msg = "Illegal parameters";
lasterror = 3;
break;
case 1:
msg = "Error in first parameter";
lasterror = 3;
break;
case 2:
msg = "No second parameter";
lasterror = 2;
break;
case 3:
msg = "No third parameter";
lasterror = 2;
break;
case 4:
msg = "No fourth parameter";
lasterror = 2;
break;
case 98:
msg = "sscanf error: corrupted file?";
lasterror = 7;
break;
case 99:
default:
msg = "Internal error";
lasterror = 7;
break;
}
if (!silent_mode) {
Eprintf("\nError in command %c%c: %s\n", cmd >> 8, cmd & 0xFF,
msg);
Eprintf(" @ Cmd %ld\n", vec_cntr_w);
position=ftell(hd);
fseek(hd, -10L, SEEK_CUR);
for (i=0;i<21;i++) tmpstr[i]=fgetc(hd);
tmpstr[20] = '\0';
fseek(hd, position, SEEK_SET);
Eprintf(" lately read: %s\n", tmpstr);
}
return;
/* exit(ERROR);*/
}
static void reset_HPGL(void)
{
int i;
pat=0;
pw[0]=pw[1]=pw[2]=ph[0]=ph[1]=ph[2]=256;
pw[3]=pw[4]=pw[5]=ph[3]=ph[4]=ph[5]=256;
pw[6]=pw[7]=ph[6]=ph[7]=256;
memset(pattern[0],1,256*256);
memset(pattern[1],1,256*256);
memset(pattern[2],1,256*256);
memset(pattern[3],1,256*256);
memset(pattern[4],1,256*256);
memset(pattern[5],1,256*256);
memset(pattern[6],1,256*256);
memset(pattern[7],1,256*256);
p_last.x = p_last.y = M_PI;
pen_down = FALSE;
plot_rel = FALSE;
in_place = FALSE;
pen = -1;
/* n_unexpected = 0;
n_unknown = 0;*/
mv_flag = FALSE;
wu_relative = FALSE;
pg_flag = FALSE;
iwflag = FALSE;
ps_flag = FALSE;
ac_flag = FALSE;
filltype = 1;
saved_hatchangle[0] = saved_hatchangle[1] = 0.;
saved_hatchangle[2] = saved_hatchangle[3] = 0.;
saved_hatchspace[0] = saved_hatchspace[1] = 0.;
saved_hatchspace[2] = saved_hatchspace[3] = 0.;
saved_hatchscale[0] = saved_hatchscale[1] = FALSE;
saved_hatchscale[2] = saved_hatchscale[3] = FALSE;
ct_dist = FALSE;
CurrentLineType = LT_solid;
set_line_style_defaults();
/* set_line_attr_defaults();*/
CurrentLineAttr.Join = LAJ_plain_miter;
CurrentLineAttr.End = LAE_butt;
CurrentLineAttr.Limit = 5;
tp->sstrokewidth = tp->astrokewidth = tp->strokewidth = 0.11;
StrTerm = ETX;
StrTermSilent = 1;
if (strbuf == NULL) {
strbuf = malloc(strbufsize);
if (strbuf == NULL) {
fprintf(stderr, "\nNo memory !\n");
exit(ERROR);
}
}
strbuf[0] = '\0';
P1.x = P1X_default;
P1.y = P1Y_default;
Diag_P1_P2 = /*@-unrecog@ */ HYPOT(P2.x - P1.x, P2.y - P1.y);
CurrentLinePatLen = 0.04 * Diag_P1_P2;
pat_pos = 0.0;
scale_flag = FALSE;
S1 = P1;
S2 = P2;
Q.x = Q.y = 1.0;
HP_pos.x = HP_pos.y = 0.0;
neg_ticklen = 0.005; /* 0.5 % */
pos_ticklen = 0.005;
symbol_char = '\0';
rot_ang -= rot_tmp;
rot_tmp = 0.;
if (rot_ang == 0.)
rotate_flag = FALSE;
if (rotate_flag) {
rot_cos = cos(M_PI * rot_ang / 180.0);
rot_sin = sin(M_PI * rot_ang / 180.0);
}
init_text_par();
if (fixedcolor == FALSE) {
set_color_rgb(xxBackground, 255, 255, 255);
set_color_rgb(xxForeground, 0, 0, 0);
set_color_rgb(xxRed, 255, 0, 0);
set_color_rgb(xxGreen, 0, 255, 0);
set_color_rgb(xxBlue, 0, 0, 255);
set_color_rgb(xxCyan, 0, 255, 255);
set_color_rgb(xxMagenta, 255, 0, 255);
set_color_rgb(xxYellow, 255, 255, 0);
pt.color[0] = xxBackground;
pt.color[1] = xxForeground;
pt.color[2] = xxRed;
pt.color[3] = xxGreen;
pt.color[4] = xxBlue;
pt.color[5] = xxCyan;
pt.color[6] = xxMagenta;
pt.color[7] = xxYellow;
}
if (fixedwidth == FALSE)
for (i = 0; i < 8; i++)
pt.width[i] = 0.1;
record_off = (first_page > page_number)
|| ((last_page < page_number) && (last_page > 0));
lasterror = 0;
initialized = 1;
initp1p2 = 1;
}
static void init_HPGL(GEN_PAR * pg, const IN_PAR * pi)
{
/**
** Re-init. global var's for multiple-file applications
**/
td = pg->td;
silent_mode = (short) pg->quiet;
lxmin = pi->x0;
lymin = pi->y0;
lxmax = pi->x1;
lymax = pi->y1;
fixedcolor = (short) pi->hwcolor;
fixedwidth = (short) pi->hwsize;
r_base = g_base = b_base = 0;
r_max = g_max = b_max = 255;
tp->truetype= pg->truetype;
/* pens_in_use = 0; */
pg->maxpens = 8;
pg->maxcolor = 1;
memset(pens_in_use, 0, NUMPENS * sizeof(short));
/**
** Record ON if no page selected (pg->page == 0)!
**/
first_page = pi->first_page; /* May be 0 */
last_page = pi->last_page; /* May be 0 */
record_off = (first_page > page_number)
|| ((last_page < page_number) && (last_page > 0));
rot_ang = pi->rotation;
rotate_flag = (rot_ang != 0.0) ? TRUE : FALSE;
if (rotate_flag) {
rot_cos = cos(M_PI * rot_ang / 180.0);
rot_sin = sin(M_PI * rot_ang / 180.0);
}
vec_cntr_r = 0L;
vec_cntr_w = 0L;
n_unexpected = 0;
pg->n_commands = 0;
n_unknown = 0;
if (pi->hwlimit.x > 0.)
P2.x = S2.x = pi->hwlimit.x;
if (pi->hwlimit.y > 0.)
P2.y = S2.y = pi->hwlimit.y;
if (HAS_POLY(pg->xx_mode))
mmsupress = FALSE;
else
mmsupress = TRUE;
reset_HPGL();
}
void User_to_Plotter_coord(const HPGL_Pt * p_usr, HPGL_Pt * p_plot)
/**
** Utility: Transformation from (scaled) user coordinates
** to plotter coordinates
**/
{
p_plot->x = P1.x + (p_usr->x - S1.x) * Q.x;
p_plot->y = P1.y + (p_usr->y - S1.y) * Q.y;
}
static void Plotter_to_User_coord(const HPGL_Pt * p_plot, HPGL_Pt * p_usr)
/**
** Utility: Transformation from plotter coordinates
** to (scaled) user coordinates
**/
{
p_usr->x = S1.x + (p_plot->x - P1.x) / Q.x;
p_usr->y = S1.y + (p_plot->y - P1.y) / Q.y;
}
void PlotCmd_to_tmpfile(PlotCmd cmd)
{
if (record_off) /* Wrong page! */
return;
if (!silent_mode)
switch (vec_cntr_w++) {
case 0:
Eprintf("Writing Cmd: ");
break;
case 1:
Eprintf("1 ");
break;
case 2:
Eprintf("2 ");
break;
case 5:
Eprintf("5 ");
break;
case 10:
Eprintf("10 ");
break;
case 20:
Eprintf("20 ");
break;
case 50:
Eprintf("50 ");
break;
case 100:
Eprintf("100 ");
break;
case 200:
Eprintf("200 ");
break;
case 500:
Eprintf("500 ");
break;
case 1000:
Eprintf("1k ");
break;
case 2000:
Eprintf("2k ");
break;
case 5000:
Eprintf("5k ");
break;
case 10000:
Eprintf("10k ");
break;
case 20000:
Eprintf("20k ");
break;
case 50000L:
Eprintf("50k ");
break;
case 100000L:
Eprintf("100k ");
break;
case 200000L:
Eprintf("200k ");
break;
case 500000L:
Eprintf("500k ");
break;
case 1000000L:
Eprintf("1000k ");
break;
case 2000000L:
Eprintf("2000k ");
break;
case 3000000L:
Eprintf("3000k ");
break;
case 4000000L:
Eprintf("4000k ");
break;
case 5000000L:
Eprintf("5000k... ");
break;
case 10000000L:
Eprintf("10000k ");
break;
}
if (fputc((int) cmd, td) == EOF) {
PError("PlotCmd_to_tmpfile");
Eprintf("Error @ Cmd %ld\n", vec_cntr_w);
exit(ERROR);
}
}
void HPGL_Pt_to_tmpfile(const HPGL_Pt * pf)
{
if (record_off) /* Wrong page! */
return;
if (fwrite((VOID *) pf, sizeof(*pf), 1, td) != 1) {
PError("HPGL_Pt_to_tmpfile");
Eprintf("Error @ Cmd %ld\n", vec_cntr_w);
exit(ERROR);
}
lxmin = MIN(pf->x, lxmin);
lymin = MIN(pf->y, lymin);
lxmax = MAX(pf->x, lxmax);
lymax = MAX(pf->y, lymax);
}
long position_from_tmpfile(void)
{
return ftell(td);
}
void reposition_tmpfile(long position)
{
fseek(td,position,SEEK_SET);
}
void HPGL_Pt_to_polygon(HPGL_Pt pf)
{
if (record_off) /* Wrong page! */
return;
polygons[++vertices] = pf;
polygon_penstate[vertices]=polygon_penup;
if (scale_flag) User_to_Plotter_coord(&pf,&pf);
if (rotate_flag) {
double tmp = rot_cos * pf.x - rot_sin * pf.y;
pf.y = rot_sin * pf.x + rot_cos * pf.y;
pf.x = tmp;
}
lxmin = MIN(pf.x, lxmin);
lymin = MIN(pf.y, lymin);
lxmax = MAX(pf.x, lxmax);
lymax = MAX(pf.y, lymax);
}
/**
** Low-level vector generation & file I/O
**/
static void
LPattern_Generator(HPGL_Pt * pa,
double dx, double dy,
double start_of_pat, double end_of_pat)
/**
** Generator of Line type patterns:
**
** pa: Start point (ptr) of current line segment
** dx, dy: Components of c * (*pb - *pa), c holding
** dx^2 + dy^2 = pattern_length^2
** start_of_pat: Fraction of start point within pattern
** end_of_pat: Fraction of end point within pattern
** Valid: 0 <= start_of_pat <= end_of_pat <= 1
**
** A pattern consists of alternating "line"/"point" and "gap" elements,
** always starting with a line/point. A point is a line of zero length.
** The table below contains the relative lengths of the elements
** of all line types except LT0; and LT; (7), which are treated separately.
** These lengths always add up to 1. A negative value terminates a pattern.
**/
{
double length_of_ele, start_of_action, end_of_action;
static double *p_cur_pat;
p_cur_pat = lt[(LT_MIN * -1) + (int) CurrentLinePattern]; /* was CurrentLineType */
if (CurrentLineType == LT_adaptive)
for (;;) {
length_of_ele = (double) *p_cur_pat++ / 100; /* Line or point */
if (length_of_ele < 0.)
return;
if (length_of_ele < 1.e-5)
PlotCmd_to_tmpfile(PLOT_AT);
else
PlotCmd_to_tmpfile(DRAW_TO);
pa->x += dx * length_of_ele;
pa->y += dy * length_of_ele;
HPGL_Pt_to_tmpfile(pa);
length_of_ele = (double) *p_cur_pat++ / 100; /* Gap */
if (length_of_ele < 0.)
return;
pa->x += dx * length_of_ele;
pa->y += dy * length_of_ele;
PlotCmd_to_tmpfile(MOVE_TO);
HPGL_Pt_to_tmpfile(pa);
} else /* LT_fixed */
for (end_of_action = 0.0;;) {
/**
** Line or point:
**/
start_of_action = end_of_action;
length_of_ele = (double) *p_cur_pat++ / 100;
if (length_of_ele < 0.)
return;
if (length_of_ele < 1.e-5) { /* Dot Only */
PlotCmd_to_tmpfile(PLOT_AT);
HPGL_Pt_to_tmpfile(pa);
} else { /* Line Segment */
end_of_action += length_of_ele;
if (end_of_action > start_of_pat) { /* If anything to do: */
if (start_of_pat <= start_of_action) { /* If start is valid */
if (end_of_action <= end_of_pat) { /* Draw full element */
pa->x +=
dx *
length_of_ele;
pa->y +=
dy *
length_of_ele;
PlotCmd_to_tmpfile
(DRAW_TO);
HPGL_Pt_to_tmpfile
(pa);
} else
/* End_of_action beyond End_of_pattern: */
{ /* --> Draw only first part of element: */
pa->x +=
dx *
(end_of_pat -
start_of_action);
pa->y +=
dy *
(end_of_pat -
start_of_action);
PlotCmd_to_tmpfile
(DRAW_TO);
HPGL_Pt_to_tmpfile
(pa);
return;
}
} else
/* Start_of_action before Start_of_pattern: */
{
if (end_of_action <= end_of_pat) { /* Draw remainder of element */
pa->x +=
dx *
(end_of_action
-
start_of_pat);
pa->y +=
dy *
(end_of_action
-
start_of_pat);
PlotCmd_to_tmpfile
(DRAW_TO);
HPGL_Pt_to_tmpfile
(pa);
} else
/* End_of_action beyond End_of_pattern: */
/* Draw central part of element & leave */
{
if (end_of_pat ==
start_of_pat)
PlotCmd_to_tmpfile
(PLOT_AT);
else
PlotCmd_to_tmpfile
(DRAW_TO);
pa->x +=
dx *
(end_of_pat -
start_of_pat);
pa->y +=
dy *
(end_of_pat -
start_of_pat);
HPGL_Pt_to_tmpfile
(pa);
return;
}
}
}
}
/**
** Gap (analogous to line/point):
**/
start_of_action = end_of_action;
length_of_ele = (double) *p_cur_pat++ / 100;
if (length_of_ele < 0)
return;
end_of_action += length_of_ele;
if (end_of_action > start_of_pat) { /* If anything to do: */
if (start_of_pat <= start_of_action) { /* If start is valid */
if (end_of_action <= end_of_pat) { /* Full gap */
pa->x +=
dx * length_of_ele;
pa->y +=
dy * length_of_ele;
PlotCmd_to_tmpfile
(MOVE_TO);
HPGL_Pt_to_tmpfile(pa);
} else
/* End_of_action beyond End_of_pattern: */
{ /* --> Apply only first part of gap: */
pa->x +=
dx * (end_of_pat -
start_of_action);
pa->y +=
dy * (end_of_pat -
start_of_action);
PlotCmd_to_tmpfile
(MOVE_TO);
HPGL_Pt_to_tmpfile(pa);
return;
}
} else
/* Start_of_action before Start_of_pattern: */
{
if (end_of_action <= end_of_pat) { /* Apply remainder of gap */
pa->x +=
dx * (end_of_action -
start_of_pat);
pa->y +=
dy * (end_of_action -
start_of_pat);
PlotCmd_to_tmpfile
(MOVE_TO);
HPGL_Pt_to_tmpfile(pa);
} else
/* End_of_action beyond End_of_pattern: */
/* Apply central part of gap & leave */
{
if (end_of_pat ==
start_of_pat)
return; /* A null move */
pa->x +=
dx * (end_of_pat -
start_of_pat);
pa->y +=
dy * (end_of_pat -
start_of_pat);
PlotCmd_to_tmpfile
(MOVE_TO);
HPGL_Pt_to_tmpfile(pa);
return;
}
}
}
}
}
static void fillpoly(int fillalg,int filltype,
float xscale,float hatchspace,float hatchscale,float hatchangle,
int rotate_flag,float rot_ang)
{
float ftmp;
PlotCmd_to_tmpfile(fillalg);
if ((EOF==fputc(filltype,td))) {
PError("Writing to temporary file:");
Eprintf ("Error @ Cmd %ld\n", vec_cntr_w);
exit (ERROR);
}
if (3==filltype || 4==filltype) {
if (hatchscale) {
ftmp=hatchspace*xscale;
} else {
ftmp=hatchspace;
}
if (fwrite ((VOID *) &ftmp, sizeof(ftmp), 1, td) != 1) {
PError("Writing to temporary file:");
Eprintf ("Error @ Cmd %ld\n", vec_cntr_w);
exit (ERROR);
}
ftmp=hatchangle;
if (rotate_flag) {
ftmp+=rot_ang;
}
if (fwrite ((VOID *) &ftmp, sizeof(ftmp), 1, td) != 1) {
PError("Writing to temporary file:");
Eprintf ("Error @ Cmd %ld\n", vec_cntr_w);
exit (ERROR);
}
}
}
/**
** Rectangles -- by Th. Hiller (hiller@tu-harburg.d400.de)
**/
static void rect(const GEN_PAR * pg, int relative, int filled, float cur_pensize, HPGL_Pt p)
{
HPGL_Pt p1;
if (relative) { /* Process coordinates */
p.x += p_last.x;
p.y += p_last.y;
}
if (!filled || HAS_POLY(pg->xx_mode)) {
if (HAS_POLY(pg->xx_mode)) {
PlotCmd_to_tmpfile(OP_PBUF);
}
p1.x = p_last.x;
p1.y = p.y;
Pen_action_to_tmpfile(DRAW_TO, &p1, scale_flag);
p1.x = p.x;
p1.y = p.y;
Pen_action_to_tmpfile(DRAW_TO, &p1, scale_flag);
p1.x = p.x;
p1.y = p_last.y;
Pen_action_to_tmpfile(DRAW_TO, &p1, scale_flag);
p1.x = p_last.x;
p1.y = p_last.y;
Pen_action_to_tmpfile(DRAW_TO, &p1, scale_flag);
if (HAS_POLY(pg->xx_mode)) {
PlotCmd_to_tmpfile(CL_PBUF);
if (filled) {
fillpoly(NZFILL_POLY,filltype,Q.x,
hatchspace,hatchscale,hatchangle,rotate_flag,rot_ang);
} else {
PlotCmd_to_tmpfile(EDGE_POLY);
}
}
} else {
vertices = -1;
HPGL_Pt_to_polygon(p_last);
p1.x = p_last.x;
p1.y = p.y;
HPGL_Pt_to_polygon(p1);