/
engine.c
3898 lines (3601 loc) · 117 KB
/
engine.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
/*
* R : A Computer Language for Statistical Data Analysis
* Copyright (C) 2001-2021 The R Core Team.
*
* This program 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 of the License, or
* (at your option) any later version.
*
* This program 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, a copy is available at
* https://www.R-project.org/Licenses/
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <Defn.h>
#include <Internal.h>
#include <float.h> /* for DBL_MAX */
#include <R_ext/GraphicsEngine.h>
#include <R_ext/Applic.h> /* R_pretty() */
#include <Rmath.h>
# include <rlocale.h>
int R_GE_getVersion(void)
{
return R_GE_version;
}
void R_GE_checkVersionOrDie(int version)
{
if (version != R_GE_version)
error(_("Graphics API version mismatch"));
}
/* A note on memory management ...
* Here (with GEDevDesc's) I have continued the deplorable tradition of
* malloc'ing device structures and maintaining global variables to
* record the device structures. I believe that what I should
* be doing is recording the device structures in R-level objects
* (i.e., SEXP's) using Luke's reference pointers to make sure that
* nasty things like duplicate copies of device structures do not
* occur. The thing stopping me doing "the right thing" right now
* is time. Hopefully, I will get time later to come back and do
* it properly -- in the meantime I'll just have to burn in hell.
* Paul.
*/
static int numGraphicsSystems = 0;
static GESystemDesc* registeredSystems[MAX_GRAPHICS_SYSTEMS];
/****************************************************************
* GEdestroyDevDesc
****************************************************************
*/
static void unregisterOne(pGEDevDesc dd, int systemNumber) {
if (dd->gesd[systemNumber] != NULL) {
(dd->gesd[systemNumber]->callback)(GE_FinaliseState, dd, R_NilValue);
free(dd->gesd[systemNumber]);
dd->gesd[systemNumber] = NULL;
}
}
/* NOTE that dd->dev has been shut down by a call
* to dev->close within devices.c
*/
void GEdestroyDevDesc(pGEDevDesc dd)
{
int i;
if (dd != NULL) {
for (i = 0; i < MAX_GRAPHICS_SYSTEMS; i++) unregisterOne(dd, i);
free(dd->dev);
dd->dev = NULL;
free(dd);
}
}
/****************************************************************
* GEsystemState
****************************************************************
Currently unused, but future systems might need it.
*/
void* GEsystemState(pGEDevDesc dd, int index)
{
return dd->gesd[index]->systemSpecific;
}
/****************************************************************
* GEregisterWithDevice
****************************************************************
*/
/* The guts of adding information about a specific graphics
* system to a specific device.
*/
static void registerOne(pGEDevDesc dd, int systemNumber, GEcallback cb) {
SEXP result;
dd->gesd[systemNumber] =
(GESystemDesc*) calloc(1, sizeof(GESystemDesc));
if (dd->gesd[systemNumber] == NULL)
error(_("unable to allocate memory (in GEregister)"));
result = cb(GE_InitState, dd, R_NilValue);
if (isNull(result)) {
/* tidy up */
free(dd->gesd[systemNumber]);
error(_("unable to allocate memory (in GEregister)"));
} else {
dd->gesd[systemNumber]->callback = cb;
}
}
/* Store the graphics system state and callback information
* for a specified device.
* This is called when a new device is created.
*/
void GEregisterWithDevice(pGEDevDesc dd) {
int i;
for (i = 0; i < MAX_GRAPHICS_SYSTEMS; i++)
/* If a graphics system has unregistered, there might be
* "holes" in the array of registeredSystems.
*/
if (registeredSystems[i] != NULL)
registerOne(dd, i, registeredSystems[i]->callback);
}
/****************************************************************
* GEregisterSystem
****************************************************************
*/
/* Record the state and callback information for a new graphics
* system.
* This is called when a graphics system is loaded.
* Return the index of the system's information in the graphic
* engine's register.
*/
void GEregisterSystem(GEcallback cb, int *systemRegisterIndex) {
int i, devNum;
pGEDevDesc gdd;
if (numGraphicsSystems + 1 == MAX_GRAPHICS_SYSTEMS)
error(_("too many graphics systems registered"));
/* Set the system register index so that, if there are existing
* devices, it will know where to put the system-specific
* information in those devices
* If a graphics system has been unregistered, there might
* be "holes" in the list of graphics systems, so start
* from zero and look for the first NULL
*/
*systemRegisterIndex = 0;
while (registeredSystems[*systemRegisterIndex] != NULL) {
(*systemRegisterIndex)++;
}
/* Run through the existing devices and add the new information
* to any GEDevDesc's
*/
i = 1;
if (!NoDevices()) {
devNum = curDevice();
while (i++ < NumDevices()) {
gdd = GEgetDevice(devNum);
registerOne(gdd, *systemRegisterIndex, cb);
devNum = nextDevice(devNum);
}
}
/* Store the information for adding to any new devices
*/
registeredSystems[*systemRegisterIndex] =
(GESystemDesc*) calloc(1, sizeof(GESystemDesc));
if (registeredSystems[*systemRegisterIndex] == NULL)
error(_("unable to allocate memory (in GEregister)"));
registeredSystems[*systemRegisterIndex]->callback = cb;
numGraphicsSystems += 1;
}
/****************************************************************
* GEunregisterSystem
****************************************************************
*/
void GEunregisterSystem(int registerIndex)
{
int i, devNum;
pGEDevDesc gdd;
/* safety check if called before Ginit() */
if(registerIndex < 0) return;
if (numGraphicsSystems == 0) {
/* This gets called from KillAllDevices, which is called
during shutdown. Prior to 2.14.0 it gave an error, which
would inhibit shutdown. This should not happen, but
apparently it did after a segfault:
https://stat.ethz.ch/pipermail/r-devel/2011-June/061153.html
*/
warning(_("no graphics system to unregister"));
return;
}
/* Run through the existing devices and remove the information
* from any GEDevDesc's
*/
i = 1;
if (!NoDevices()) {
devNum = curDevice();
while (i++ < NumDevices()) {
gdd = GEgetDevice(devNum);
unregisterOne(gdd, registerIndex);
devNum = nextDevice(devNum);
}
}
/* Remove the information from the global record
* NOTE that there is no systemSpecific information stored
* in the global record -- just the system callback pointer.
*/
if (registeredSystems[registerIndex] != NULL) {
free(registeredSystems[registerIndex]);
registeredSystems[registerIndex] = NULL;
}
numGraphicsSystems -= 1;
}
/****************************************************************
* GEhandleEvent
****************************************************************
*/
/* This guy can be called by device drivers.
* It calls back to registered graphics systems and passes on the event
* so that the graphics systems can respond however they want to.
*
* Currently only used for GE_ScalePS in devWindows.c
*/
SEXP GEhandleEvent(GEevent event, pDevDesc dev, SEXP data)
{
int i;
pGEDevDesc gdd = desc2GEDesc(dev);
for (i = 0; i < MAX_GRAPHICS_SYSTEMS; i++)
if (registeredSystems[i] != NULL)
(registeredSystems[i]->callback)(event, gdd, data);
return R_NilValue;
}
/****************************************************************
* Some graphics engine transformations
****************************************************************
*/
double fromDeviceX(double value, GEUnit to, pGEDevDesc dd)
{
double result = value;
switch (to) {
case GE_DEVICE:
break;
case GE_NDC:
result = (result - dd->dev->left) / (dd->dev->right - dd->dev->left);
break;
case GE_INCHES:
result = (result - dd->dev->left) / (dd->dev->right - dd->dev->left) *
fabs(dd->dev->right - dd->dev->left) * dd->dev->ipr[0];
break;
case GE_CM:
result = (result - dd->dev->left) / (dd->dev->right - dd->dev->left) *
fabs(dd->dev->right - dd->dev->left) * dd->dev->ipr[0] * 2.54;
}
return result;
}
double toDeviceX(double value, GEUnit from, pGEDevDesc dd)
{
double result = value;
switch (from) {
case GE_CM:
/* Convert GE_CM to GE_INCHES */
result = result / 2.54;
case GE_INCHES:
/* Convert GE_INCHES to GE_NDC */
result = (result / dd->dev->ipr[0]) / fabs(dd->dev->right - dd->dev->left);
case GE_NDC:
/* Convert GE_NDC to Dev */
result = dd->dev->left + result*(dd->dev->right - dd->dev->left);
case GE_DEVICE:
/* Do nothing */
break;
}
return result;
}
double fromDeviceY(double value, GEUnit to, pGEDevDesc dd)
{
double result = value;
switch (to) {
case GE_DEVICE:
break;
case GE_NDC:
result = (result - dd->dev->bottom) / (dd->dev->top - dd->dev->bottom);
break;
case GE_INCHES:
result = (result - dd->dev->bottom) / (dd->dev->top - dd->dev->bottom) *
fabs(dd->dev->top - dd->dev->bottom) * dd->dev->ipr[1];
break;
case GE_CM:
result = (result - dd->dev->bottom) / (dd->dev->top - dd->dev->bottom) *
fabs(dd->dev->top - dd->dev->bottom) * dd->dev->ipr[1] * 2.54;
}
return result;
}
double toDeviceY(double value, GEUnit from, pGEDevDesc dd)
{
double result = value;
switch (from) {
case GE_CM:
/* Convert GE_CM to GE_INCHES */
result = result / 2.54;
case GE_INCHES:
/* Convert GE_INCHES to GE_NDC */
result = (result / dd->dev->ipr[1]) / fabs(dd->dev->top - dd->dev->bottom);
case GE_NDC:
/* Convert GE_NDC to Dev */
result = dd->dev->bottom + result*(dd->dev->top - dd->dev->bottom);
case GE_DEVICE:
/* Do nothing */
break;
}
return result;
}
double fromDeviceWidth(double value, GEUnit to, pGEDevDesc dd)
{
double result = value;
switch (to) {
case GE_DEVICE:
break;
case GE_NDC:
result = result / (dd->dev->right - dd->dev->left);
break;
case GE_INCHES:
result = result * dd->dev->ipr[0];
break;
case GE_CM:
result = result * dd->dev->ipr[0] * 2.54;
}
return result;
}
double toDeviceWidth(double value, GEUnit from, pGEDevDesc dd)
{
double result = value;
switch (from) {
case GE_CM:
/* Convert GE_CM to GE_INCHES */
result = result / 2.54;
case GE_INCHES:
/* Convert GE_INCHES to GE_NDC */
result = (result / dd->dev->ipr[0]) / fabs(dd->dev->right - dd->dev->left);
case GE_NDC:
/* Convert GE_NDC to Dev */
result = result*(dd->dev->right - dd->dev->left);
case GE_DEVICE:
/* Do nothing */
break;
}
return result;
}
double fromDeviceHeight(double value, GEUnit to, pGEDevDesc dd)
{
double result = value;
switch (to) {
case GE_DEVICE:
break;
case GE_NDC:
result = result / (dd->dev->top - dd->dev->bottom);
break;
case GE_INCHES:
result = result * dd->dev->ipr[1];
break;
case GE_CM:
result = result * dd->dev->ipr[1] * 2.54;
}
return result;
}
double toDeviceHeight(double value, GEUnit from, pGEDevDesc dd)
{
double result = value;
switch (from) {
case GE_CM:
/* Convert GE_CM to GE_INCHES */
result = result / 2.54;
case GE_INCHES:
/* Convert GE_INCHES to GE_NDC */
result = (result / dd->dev->ipr[1]) / fabs(dd->dev->top - dd->dev->bottom);
case GE_NDC:
/* Convert GE_NDC to Dev */
result = result*(dd->dev->top - dd->dev->bottom);
case GE_DEVICE:
/* Do nothing */
break;
}
return result;
}
/****************************************************************
* Code for converting line ends and joins from SEXP to internal
* representation
****************************************************************
*/
typedef struct {
char *name;
R_GE_lineend end;
} LineEND;
static LineEND lineend[] = {
{ "round", GE_ROUND_CAP },
{ "butt", GE_BUTT_CAP },
{ "square", GE_SQUARE_CAP },
{ NULL, 0 }
};
static int nlineend = (sizeof(lineend)/sizeof(LineEND)-2);
R_GE_lineend GE_LENDpar(SEXP value, int ind)
{
int i, code;
double rcode;
if(isString(value)) {
for(i = 0; lineend[i].name; i++) { /* is it the i-th name ? */
if(!strcmp(CHAR(STRING_ELT(value, ind)), lineend[i].name)) /*ASCII */
return lineend[i].end;
}
error(_("invalid line end")); /*NOTREACHED, for -Wall : */ return 0;
}
else if(isInteger(value)) {
code = INTEGER(value)[ind];
if(code == NA_INTEGER || code < 0)
error(_("invalid line end"));
if (code > 0)
code = (code-1) % nlineend + 1;
return lineend[code].end;
}
else if(isReal(value)) {
rcode = REAL(value)[ind];
if(!R_FINITE(rcode) || rcode < 0)
error(_("invalid line end"));
code = (int) rcode;
if (code > 0)
code = (code-1) % nlineend + 1;
return lineend[code].end;
}
else {
error(_("invalid line end")); /*NOTREACHED, for -Wall : */ return 0;
}
}
SEXP GE_LENDget(R_GE_lineend lend)
{
SEXP ans = R_NilValue;
int i;
for (i = 0; lineend[i].name; i++) {
if(lineend[i].end == lend)
return mkString(lineend[i].name);
}
error(_("invalid line end"));
/*
* Should never get here
*/
return ans;
}
typedef struct {
char *name;
R_GE_linejoin join;
} LineJOIN;
static LineJOIN linejoin[] = {
{ "round", GE_ROUND_JOIN },
{ "mitre", GE_MITRE_JOIN },
{ "bevel", GE_BEVEL_JOIN},
{ NULL, 0 }
};
static int nlinejoin = (sizeof(linejoin)/sizeof(LineJOIN)-2);
R_GE_linejoin GE_LJOINpar(SEXP value, int ind)
{
int i, code;
double rcode;
if(isString(value)) {
for(i = 0; linejoin[i].name; i++) { /* is it the i-th name ? */
if(!strcmp(CHAR(STRING_ELT(value, ind)), linejoin[i].name)) /* ASCII */
return linejoin[i].join;
}
error(_("invalid line join")); /*NOTREACHED, for -Wall : */ return 0;
}
else if(isInteger(value)) {
code = INTEGER(value)[ind];
if(code == NA_INTEGER || code < 0)
error(_("invalid line join"));
if (code > 0)
code = (code-1) % nlinejoin + 1;
return linejoin[code].join;
}
else if(isReal(value)) {
rcode = REAL(value)[ind];
if(!R_FINITE(rcode) || rcode < 0)
error(_("invalid line join"));
code = (int) rcode;
if (code > 0)
code = (code-1) % nlinejoin + 1;
return linejoin[code].join;
}
else {
error(_("invalid line join")); /*NOTREACHED, for -Wall : */ return 0;
}
}
SEXP GE_LJOINget(R_GE_linejoin ljoin)
{
SEXP ans = R_NilValue;
int i;
for (i = 0; linejoin[i].name; i++) {
if(linejoin[i].join == ljoin)
return mkString(linejoin[i].name);
}
error(_("invalid line join"));
/*
* Should never get here
*/
return ans;
}
/****************************************************************
* Code to retrieve current clipping rect from device
****************************************************************
*/
static void getClipRect(double *x1, double *y1, double *x2, double *y2,
pGEDevDesc dd)
{
/* Since these are only set by GESetClip they should be in order */
if (dd->dev->clipLeft < dd->dev->clipRight) {
*x1 = dd->dev->clipLeft;
*x2 = dd->dev->clipRight;
} else {
*x2 = dd->dev->clipLeft;
*x1 = dd->dev->clipRight;
}
if (dd->dev->clipBottom < dd->dev->clipTop) {
*y1 = dd->dev->clipBottom;
*y2 = dd->dev->clipTop;
} else {
*y2 = dd->dev->clipBottom;
*y1 = dd->dev->clipTop;
}
}
static void getClipRectToDevice(double *x1, double *y1, double *x2, double *y2,
pGEDevDesc dd)
{
double factor = 4;
double dx, dy, d;
/* Devices can have flipped coord systems */
if (dd->dev->left < dd->dev->right) {
*x1 = dd->dev->left;
*x2 = dd->dev->right;
} else {
*x2 = dd->dev->left;
*x1 = dd->dev->right;
}
if (dd->dev->bottom < dd->dev->top) {
*y1 = dd->dev->bottom;
*y2 = dd->dev->top;
} else {
*y2 = dd->dev->bottom;
*y1 = dd->dev->top;
}
/*
* Do NOT clip to the actual device edge (that produces artifacts).
* Instead, clip to a much larger region.
*/
dx = factor*(*x2 - *x1);
dy = factor*(*y2 - *y1);
d = (dx > dy)? dx : dy;
*x1 = *x1 - d;
*x2 = *x2 + d;
*y1 = *y1 - d;
*y2 = *y2 + d;
}
/****************************************************************
* GESetClip
****************************************************************
*/
void GESetClip(double x1, double y1, double x2, double y2, pGEDevDesc dd)
{
pDevDesc d = dd->dev;
double dx1 = d->left, dx2 = d->right, dy1 = d->bottom, dy2 = d->top;
/* clip to device region */
if (dx1 <= dx2) {
x1 = fmax2(x1, dx1);
x2 = fmin2(x2, dx2);
} else {
x1 = fmin2(x1, dx1);
x2 = fmax2(x2, dx2);
}
if (dy1 <= dy2) {
y1 = fmax2(y1, dy1);
y2 = fmin2(y2, dy2);
} else {
y1 = fmin2(y1, dy1);
y2 = fmax2(y2, dy2);
}
d->clip(x1, x2, y1, y2, dd->dev);
/*
* Record the current clip rect settings so that calls to
* getClipRect get the up-to-date values.
*/
d->clipLeft = fmin2(x1, x2);
d->clipRight = fmax2(x1, x2);
d->clipTop = fmax2(y1, y2);
d->clipBottom = fmin2(y1, y2);
}
/****************************************************************
* R code for clipping lines
****************************************************************
*/
/* Draw Line Segments, Clipping to the Viewport */
/* Cohen-Sutherland Algorithm */
/* Unneeded if the device can do the clipping */
#define CS_BOTTOM 001
#define CS_LEFT 002
#define CS_TOP 004
#define CS_RIGHT 010
typedef struct {
double xl;
double xr;
double yb;
double yt;
} cliprect;
static int clipcode(double x, double y, cliprect *cr)
{
int c = 0;
if(x < cr->xl)
c |= CS_LEFT;
else if(x > cr->xr)
c |= CS_RIGHT;
if(y < cr->yb)
c |= CS_BOTTOM;
else if(y > cr->yt)
c |= CS_TOP;
return c;
}
static Rboolean
CSclipline(double *x1, double *y1, double *x2, double *y2,
cliprect *cr, int *clipped1, int *clipped2,
pGEDevDesc dd)
{
int c, c1, c2;
double x, y, xl, xr, yb, yt;
*clipped1 = 0;
*clipped2 = 0;
c1 = clipcode(*x1, *y1, cr);
c2 = clipcode(*x2, *y2, cr);
if ( !c1 && !c2 )
return TRUE;
xl = cr->xl;
xr = cr->xr;
yb = cr->yb;
yt = cr->yt;
/* Paul took out the code for (dd->dev->gp.xlog || dd->dev->gp.ylog)
* (i) because device holds no state on whether scales are logged
* (ii) it appears to be identical to the code for non-log scales !?
*/
x = xl; /* keep -Wall happy */
y = yb; /* keep -Wall happy */
while( c1 || c2 ) {
if(c1 & c2)
return FALSE;
if( c1 )
c = c1;
else
c = c2;
if( c & CS_LEFT ) {
y = *y1 + (*y2 - *y1) * (xl - *x1) / (*x2 - *x1);
x = xl;
}
else if( c & CS_RIGHT ) {
y = *y1 + (*y2 - *y1) * (xr - *x1) / (*x2 - *x1);
x = xr;
}
else if( c & CS_BOTTOM ) {
x = *x1 + (*x2 - *x1) * (yb - *y1) / (*y2 - *y1);
y = yb;
}
else if( c & CS_TOP ) {
x = *x1 + (*x2 - *x1) * (yt - *y1)/(*y2 - *y1);
y = yt;
}
if( c==c1 ) {
*x1 = x;
*y1 = y;
*clipped1 = 1;
c1 = clipcode(x, y, cr);
}
else {
*x2 = x;
*y2 = y;
*clipped2 = 1;
c2 = clipcode(x, y, cr);
}
}
return TRUE;
}
/* Clip the line
If toDevice = 1, clip to the device extent (i.e., temporarily ignore
dd->dev->gp.xpd) */
static Rboolean
clipLine(double *x1, double *y1, double *x2, double *y2,
int toDevice, pGEDevDesc dd)
{
int dummy1, dummy2;
cliprect cr;
if (toDevice)
getClipRectToDevice(&cr.xl, &cr.yb, &cr.xr, &cr.yt, dd);
else
getClipRect(&cr.xl, &cr.yb, &cr.xr, &cr.yt, dd);
return CSclipline(x1, y1, x2, y2, &cr, &dummy1, &dummy2, dd);
}
/****************************************************************
* GELine
****************************************************************
*/
/* If the device canClip, R clips line to device extent and
device does all other clipping. */
void GELine(double x1, double y1, double x2, double y2,
const pGEcontext gc, pGEDevDesc dd)
{
Rboolean clip_ok;
if (gc->lwd == R_PosInf || gc->lwd < 0.0)
error(_("'lwd' must be non-negative and finite"));
if (ISNAN(gc->lwd) || gc->lty == LTY_BLANK) return;
if (dd->dev->deviceVersion >= R_GE_deviceClip &&
dd->dev->deviceClip) {
dd->dev->line(x1, y1, x2, y2, gc, dd->dev);
} else {
if (dd->dev->canClip) {
clip_ok = clipLine(&x1, &y1, &x2, &y2, 1, dd);
}
else {
clip_ok = clipLine(&x1, &y1, &x2, &y2, 0, dd);
}
if (clip_ok)
dd->dev->line(x1, y1, x2, y2, gc, dd->dev);
}
}
/****************************************************************
* R code for clipping polylines
****************************************************************
*/
static void CScliplines(int n, double *x, double *y,
const pGEcontext gc, int toDevice, pGEDevDesc dd)
{
int ind1, ind2;
/*int firstPoint = 1;*/
int count = 0;
int i = 0;
double *xx, *yy;
double x1, y1, x2, y2;
cliprect cr;
const void *vmax = vmaxget();
if (toDevice)
getClipRectToDevice(&cr.xl, &cr.yb, &cr.xr, &cr.yt, dd);
else
getClipRect(&cr.xl, &cr.yb, &cr.xr, &cr.yt, dd);
xx = (double *) R_alloc(n, sizeof(double));
yy = (double *) R_alloc(n, sizeof(double));
if (xx == NULL || yy == NULL)
error(_("out of memory while clipping polyline"));
xx[0] = x1 = x[0];
yy[0] = y1 = y[0];
count = 1;
for (i = 1; i < n; i++) {
x2 = x[i];
y2 = y[i];
if (CSclipline(&x1, &y1, &x2, &y2, &cr, &ind1, &ind2, dd)) {
if (ind1 && ind2) {
xx[0] = x1;
yy[0] = y1;
xx[1] = x2;
yy[1] = y2;
dd->dev->polyline(2, xx, yy, gc, dd->dev);
}
else if (ind1) {
xx[0] = x1;
yy[0] = y1;
xx[1] = x2;
yy[1] = y2;
count = 2;
if (i == n - 1)
dd->dev->polyline(count, xx, yy, gc, dd->dev);
}
else if (ind2) {
xx[count] = x2;
yy[count] = y2;
count++;
if (count > 1)
dd->dev->polyline(count, xx, yy, gc, dd->dev);
}
else {
xx[count] = x2;
yy[count] = y2;
count++;
if (i == n - 1 && count > 1)
dd->dev->polyline(count, xx, yy, gc, dd->dev);
}
}
x1 = x[i];
y1 = y[i];
}
vmaxset(vmax);
}
/****************************************************************
* GEPolyline
****************************************************************
*/
/* Clip and draw the polyline.
If clipToDevice = 0, clip according to dd->dev->gp.xpd
If clipToDevice = 1, clip to the device extent */
static void clipPolyline(int n, double *x, double *y,
const pGEcontext gc, int clipToDevice, pGEDevDesc dd)
{
CScliplines(n, x, y, gc, clipToDevice, dd);
}
/* Draw a series of line segments. */
/* If the device canClip, R clips to the device extent and the device
does all other clipping */
void GEPolyline(int n, double *x, double *y, const pGEcontext gc, pGEDevDesc dd)
{
if (gc->lwd == R_PosInf || gc->lwd < 0.0)
error(_("'lwd' must be non-negative and finite"));
if (ISNAN(gc->lwd) || gc->lty == LTY_BLANK) return;
if (dd->dev->deviceVersion >= R_GE_deviceClip &&
dd->dev->deviceClip) {
dd->dev->polyline(n, x, y, gc, dd->dev);
} else if (dd->dev->canClip) {
clipPolyline(n, x, y, gc, 1, dd); /* clips to device extent
then draws */
}
else
clipPolyline(n, x, y, gc, 0, dd);
}
/****************************************************************
* R code for clipping polygons
****************************************************************
*/
typedef enum {
Left = 0,
Right = 1,
Bottom = 2,
Top = 3
} Edge;
/* Clipper State Variables */
typedef struct {
int first; /* true if we have seen the first point */
double fx; /* x coord of the first point */
double fy; /* y coord of the first point */
double sx; /* x coord of the most recent point */
double sy; /* y coord of the most recent point */
}
GClipState;
/* The Clipping Rectangle */
typedef struct {
double xmin;
double xmax;
double ymin;
double ymax;
}
GClipRect;
static
int inside (Edge b, double px, double py, GClipRect *clip)
{
switch (b) {
case Left: if (px < clip->xmin) return 0; break;
case Right: if (px > clip->xmax) return 0; break;
case Bottom: if (py < clip->ymin) return 0; break;
case Top: if (py > clip->ymax) return 0; break;
}
return 1;
}
static
int cross (Edge b, double x1, double y1, double x2, double y2,
GClipRect *clip)
{
if (inside (b, x1, y1, clip) == inside (b, x2, y2, clip))
return 0;
else return 1;
}
static
void intersect (Edge b, double x1, double y1, double x2, double y2,
double *ix, double *iy, GClipRect *clip)
{
double m = 0;
if (x1 != x2) m = (y1 - y2) / (x1 - x2);
switch (b) {
case Left:
*ix = clip->xmin;
*iy = y2 + (clip->xmin - x2) * m;
break;
case Right:
*ix = clip->xmax;
*iy = y2 + (clip->xmax - x2) * m;
break;
case Bottom:
*iy = clip->ymin;
if (x1 != x2) *ix = x2 + (clip->ymin - y2) / m;
else *ix = x2;
break;
case Top:
*iy = clip->ymax;
if (x1 != x2) *ix = x2 + (clip->ymax - y2) / m;
else *ix = x2;
break;
}
}
static
void clipPoint (Edge b, double x, double y,
double *xout, double *yout, int *cnt, int store,
GClipRect *clip, GClipState *cs)
{
double ix = 0.0, iy = 0.0 /* -Wall */;
if (!cs[b].first) {
/* No previous point exists for this edge. */
/* Save this point. */
cs[b].first = 1;
cs[b].fx = x;
cs[b].fy = y;
}
else
/* A previous point exists. */
/* If 'p' and previous point cross edge, find intersection. */
/* Clip against next boundary, if any. */
/* If no more edges, add intersection to output list. */
if (cross (b, x, y, cs[b].sx, cs[b].sy, clip)) {
intersect (b, x, y, cs[b].sx, cs[b].sy, &ix, &iy, clip);
if (b < Top)
clipPoint (b + 1, ix, iy, xout, yout, cnt, store,