/
devX11.c
2075 lines (1827 loc) · 59.5 KB
/
devX11.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) 1995, 1996 Robert Gentleman and Ross Ihaka
* Copyright (C) 1997--2000 Robert Gentleman, Ross Ihaka and the
* R Development 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, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
/*
#define OLD
*/
#ifdef HAVE_RINT
#define R_rint(x) rint(x)
#else
#define R_rint(x) ((int) x + 0.5)
#endif
#include <stdio.h>
#include <X11/X.h>
#include <X11/Xlib.h>
#include <X11/Xatom.h>
#include <X11/Xutil.h>
#include <X11/cursorfont.h>
#include <X11/Intrinsic.h>/*-> Xlib.h Xutil.h Xresource.h .. */
#include "Defn.h"
#include "Graphics.h"
#include "Fileio.h" /* R_fopen */
#include "rotated.h"/* 'Public' routines from here */
/* For the input handlers of the event loop mechanism: */
#include "R_ext/eventloop.h"
#include "R_ext/Memory.h" /* vmaxget */
#include "Devices.h"
#include "devX11.h"
/********************************************************/
/* This device driver has been documented so that it be */
/* used as a template for new drivers */
/********************************************************/
#define CURSOR XC_crosshair /* Default cursor */
#define MM_PER_INCH 25.4 /* mm -> inch conversion */
#define IS_100DPI ((int) (1./pixelHeight() + 0.5) == 100)
/********************************************************/
/* Each driver can have its own device-specic graphical */
/* parameters and resources. these should be wrapped */
/* in a structure (like the x11Desc structure below) */
/* and attached to the overall device description via */
/* the dd->deviceSpecific pointer */
/* NOTE that there are generic graphical parameters */
/* which must be set by the device driver, but are */
/* common to all device types (see Graphics.h) */
/* so go in the GPar structure rather than this device- */
/* specific structure */
/********************************************************/
typedef struct {
/* R Graphics Parameters */
/* Local device copy so that we can detect */
/* when parameter changes. */
double cex; /* Character expansion */
double srt; /* String rotation */
int lty; /* Line type */
double lwd;
int col; /* Color */
int fg; /* Foreground */
int bg; /* Background */
int fontface; /* Typeface */
int fontsize; /* Size in points */
int basefontface; /* Typeface */
int basefontsize; /* Size in points */
/* X11 Driver Specific */
/* Parameters with copy per X11 device. */
int windowWidth; /* Window width (pixels) */
int windowHeight; /* Window height (pixels) */
int resize; /* Window resized */
Window window; /* Graphics Window */
GC wgc; /* GC for window */
Cursor gcursor; /* Graphics Cursor */
XSetWindowAttributes attributes; /* Window attributes */
#if 0
XColor fgcolor; /* Foreground color */
XColor bgcolor; /* Background color */
#endif
XRectangle clip; /* The clipping rectangle */
int usefixed;
XFontStruct *fixedfont;
XFontStruct *font;
X_GTYPE type; /* Window or pixmap? */
int npages; /* counter for a pixmap */
FILE *fp; /* file for a bitmap device */
int quality; /* JPEG quality */
} x11Desc;
/********************************************************/
/* If there are resources that are shared by all devices*/
/* of this type, you may wish to make them globals */
/* rather than including them in the device-specific */
/* parameters structure (especially if they are large !)*/
/********************************************************/
/* X11 Driver Specific parameters
* with only one copy for all x11 devices */
static Display *display; /* Display */
static int screen; /* Screen */
static Window rootwin; /* Root Window */
static Visual *visual; /* Visual */
static int depth; /* Pixmap depth */
static int Vclass; /* Visual class */
static X_COLORTYPE model; /* User color model */
static int maxcubesize; /* Max colorcube size */
static XSetWindowAttributes attributes; /* Window attributes */
static Colormap colormap; /* Default color map */
static int blackpixel; /* Black */
static int whitepixel; /* White */
static XContext devPtrContext;
static Atom _XA_WM_PROTOCOLS, protocol;
static Rboolean displayOpen = FALSE;
static Rboolean inclose = FALSE;
static int numX11Devices = 0;
/********************************************************/
/* There must be an entry point for the device driver */
/* which will create device-specific resources, */
/* initialise the device-specific parameters structure */
/* and return whether the setup succeeded */
/* This is called by the graphics engine when the user */
/* creates a new device of this type */
/********************************************************/
/********************************************************/
/* There are a number of actions that every device */
/* driver is expected to perform (even if, in some */
/* cases it does nothing - just so long as it doesn't */
/* crash !). this is how the graphics engine interacts */
/* with each device. ecah action will be documented */
/* individually. */
/* hooks for these actions must be set up when the */
/* device is first created */
/********************************************************/
/* Device Driver Actions */
static void X11_Activate(DevDesc *);
static void X11_Circle(double, double, int, double, int, int, DevDesc*);
static void X11_Clip(double, double, double, double, DevDesc*);
static void X11_Close(DevDesc*);
static void X11_Deactivate(DevDesc *);
static void X11_Hold(DevDesc*);
static void X11_Line(double, double, double, double, int, DevDesc*);
static Rboolean X11_Locator(double*, double*, DevDesc*);
static void X11_Mode(int, DevDesc*);
static void X11_NewPage(DevDesc*);
static Rboolean
X11_Open(DevDesc*, x11Desc*, char*, double, double, double, X_COLORTYPE, int);
static void X11_Polygon(int, double*, double*, int, int, int, DevDesc*);
static void X11_Polyline(int, double*, double*, int, DevDesc*);
static void X11_Rect(double, double, double, double, int, int, int, DevDesc*);
static void X11_Resize(DevDesc*);
static double X11_StrWidth(char*, DevDesc*);
static void X11_Text(double, double, int, char*, double, double, DevDesc*);
static void X11_MetricInfo(int, double*, double*, double*, DevDesc*);
/*************************************************/
/* End of list of required device driver actions */
/*************************************************/
/* Support Routines */
static XFontStruct *RLoadFont(int, int);
static double pixelHeight(void);
static double pixelWidth(void);
static int SetBaseFont(x11Desc*);
static void SetColor(int, DevDesc*);
static void SetFont(int, int, DevDesc*);
static void SetLinetype(int, double, DevDesc*);
/************************/
/* X11 Color Management */
/************************/
static double RedGamma = 0.6;
static double GreenGamma = 0.6;
static double BlueGamma = 0.6;
/* Variables Used To Store Colormap Information */
static struct { int red; int green; int blue; } RPalette[512];
static XColor XPalette[512];
static int PaletteSize;
static int RedLevels, GreenLevels, BlueLevels;
/* Monochome Displays : Compute pixel values by converting */
/* RGB values to luminance and then thresholding. */
/* See: Foley & van Damm. */
static void SetupMonochrome()
{
depth = 1;
}
static unsigned GetMonochromePixel(int r, int g, int b)
{
if ((int)(0.299 * r + 0.587 * g + 0.114 * b) > 127)
return WhitePixel(display, screen);
else
return BlackPixel(display, screen);
}
/* Grayscale Displays : Compute pixel values by converting */
/* RGB values to luminance. See: Foley & van Damm. */
static unsigned GetGrayScalePixel(int r, int g, int b)
{
unsigned int d, dmin = 0xFFFFFFFF;
unsigned int dr;
int i, imin;
unsigned int pixel = 0; /* -Wall */
int gray = (0.299 * r + 0.587 * g + 0.114 * b) + 0.0001;
for (i = 0; i < PaletteSize; i++) {
dr = (RPalette[i].red - gray);
d = dr * dr;
if (d < dmin) {
pixel = XPalette[i].pixel;
dmin = d;
imin = i;
}
}
return pixel;
}
static Rboolean GetGrayPalette(Display *displ, Colormap cmap, int n)
{
int status, i, m;
m = 0;
i = 0;
for (i = 0; i < n; i++) {
RPalette[i].red = (i * 0xff) / (n - 1);
RPalette[i].green = RPalette[i].red;
RPalette[i].blue = RPalette[i].red;
/* Gamma correct here */
XPalette[i].red = (i * 0xffff) / (n - 1);
XPalette[i].green = XPalette[i].red;
XPalette[i].blue = XPalette[i].red;
status = XAllocColor(displ, cmap, &XPalette[i]);
if (status == 0) {
XPalette[i].flags = 0;
m++;
}
else
XPalette[i].flags = DoRed|DoGreen|DoBlue;
}
PaletteSize = n;
if (m > 0) {
for (i = 0; i < PaletteSize; i++) {
if (XPalette[i].flags != 0)
XFreeColors(displ, cmap, &(XPalette[i].pixel), 1, 0);
}
PaletteSize = 0;
return FALSE;
}
else return TRUE;
}
static void SetupGrayScale()
{
int res = 0, d;
PaletteSize = 0;
/* try for 128 grays on an 8-bit display */
if (depth > 8) d = depth = 8; else d = depth - 1;
/* try (256), 128, 64, 32, 16 grays */
while (d >= 4 && !(res = GetGrayPalette(display, colormap, 1 << d)))
d--;
if (!res) {
/* Can't find a sensible grayscale, so revert to monochrome */
warning("can't set grayscale: reverting to monochrome");
model = MONOCHROME;
SetupMonochrome();
}
}
/* PseudoColor Displays : There are two strategies here. */
/* 1) allocate a standard color cube and match colors */
/* within that based on (weighted) distances in RGB space. */
/* 2) allocate colors exactly as they are requested until */
/* all color cells are used. Fail with an error message */
/* when this happens. */
static int RGBlevels[][3] = { /* PseudoColor Palettes */
{ 8, 8, 4 },
{ 6, 7, 6 },
{ 6, 6, 6 },
{ 6, 6, 5 },
{ 6, 6, 4 },
{ 5, 5, 5 },
{ 5, 5, 4 },
{ 4, 4, 4 },
{ 4, 4, 3 },
{ 3, 3, 3 },
{ 2, 2, 2 }
};
static int NRGBlevels = sizeof(RGBlevels) / (3 * sizeof(int));
static int GetColorPalette(Display *dpy, Colormap cmap, int nr, int ng, int nb)
{
int status, i, m, r, g, b;
m = 0;
i = 0;
for (r = 0; r < nr; r++) {
for (g = 0; g < ng; g++) {
for (b = 0; b < nb; b++) {
RPalette[i].red = (r * 0xff) / (nr - 1);
RPalette[i].green = (g * 0xff) / (ng - 1);
RPalette[i].blue = (b * 0xff) / (nb - 1);
/* Perform Gamma Correction Here */
#ifdef OLD
XPalette[i].red = (r * 0xffff) / (nr - 1);
XPalette[i].green = (g * 0xffff) / (ng - 1);
XPalette[i].blue = (b * 0xffff) / (nb - 1);
#else
XPalette[i].red = pow(r / (nr - 1.0), RedGamma) * 0xffff;
XPalette[i].green = pow(g / (ng - 1.0), GreenGamma) * 0xffff;
XPalette[i].blue = pow(b / (nb - 1.0), BlueGamma) * 0xffff;
#endif
/* End Gamma Correction */
status = XAllocColor(dpy, cmap, &XPalette[i]);
if (status == 0) {
XPalette[i].flags = 0;
m++;
}
else
XPalette[i].flags = DoRed|DoGreen|DoBlue;
i++;
}
}
}
PaletteSize = nr * ng * nb;
if (m > 0) {
for (i = 0; i < PaletteSize; i++) {
if (XPalette[i].flags != 0)
XFreeColors(dpy, cmap, &(XPalette[i].pixel), 1, 0);
}
PaletteSize = 0;
return 0;
}
else {
RedLevels = nr;
GreenLevels = ng;
BlueLevels = nb;
return 1;
}
}
static void SetupPseudoColor()
{
int i, size;
PaletteSize = 0;
if (model == PSEUDOCOLOR1) {
for (i = 0; i < NRGBlevels; i++) {
size = RGBlevels[i][0] * RGBlevels[i][1] * RGBlevels[i][2];
if (size < maxcubesize && GetColorPalette(display, colormap,
RGBlevels[i][0],
RGBlevels[i][1],
RGBlevels[i][2]))
break;
}
if (PaletteSize == 0) {
warning("X11 driver unable to obtain color cube\n reverting to monochrome");
model = MONOCHROME;
SetupMonochrome();
}
}
else {
PaletteSize = 0;
}
}
static unsigned int GetPseudoColor1Pixel(int r, int g, int b)
{
unsigned int d, dmin = 0xFFFFFFFF;
unsigned int dr, dg, db;
unsigned int pixel;
int i, imin;
pixel = 0; /* -Wall */
for (i = 0; i < PaletteSize; i++) {
dr = (RPalette[i].red - r);
dg = (RPalette[i].green - g);
db = (RPalette[i].blue - b);
d = dr * dr + dg * dg + db * db;
if (d < dmin) {
pixel = XPalette[i].pixel;
dmin = d;
imin = i;
}
}
return pixel;
}
static unsigned int GetPseudoColor2Pixel(int r, int g, int b)
{
int i;
/* Search for previously allocated color */
for (i = 0; i < PaletteSize ; i++) {
if (r == RPalette[i].red &&
g == RPalette[i].green &&
b == RPalette[i].blue) return XPalette[i].pixel;
}
/* Attempt to allocate a new color */
XPalette[PaletteSize].red = pow(r / 255.0, RedGamma) * 0xffff;
XPalette[PaletteSize].green = pow(g / 255.0, GreenGamma) * 0xffff;
XPalette[PaletteSize].blue = pow(b / 255.0, BlueGamma) * 0xffff;
if (PaletteSize == 256 ||
XAllocColor(display, colormap, &XPalette[PaletteSize]) == 0) {
error("Error: X11 cannot allocate additional graphics colors.\n"
"Consider using X11 with colortype=\"pseudo.cube\" or \"gray\".");
}
RPalette[PaletteSize].red = r;
RPalette[PaletteSize].green = g;
RPalette[PaletteSize].blue = b;
PaletteSize++;
return XPalette[PaletteSize - 1].pixel;
}
static unsigned int GetPseudoColorPixel(int r, int g, int b)
{
if (model == PSEUDOCOLOR1)
return GetPseudoColor1Pixel(r, g, b);
else
return GetPseudoColor2Pixel(r, g, b);
}
/* Truecolor Displays : Allocate the colors as they are requested */
static unsigned int RMask, RShift;
static unsigned int GMask, GShift;
static unsigned int BMask, BShift;
static void SetupTrueColor()
{
RMask = visual->red_mask;
GMask = visual->green_mask;
BMask = visual->blue_mask;
RShift = 0; while ((RMask & 1) == 0) { RShift++; RMask >>= 1; }
GShift = 0; while ((GMask & 1) == 0) { GShift++; GMask >>= 1; }
BShift = 0; while ((BMask & 1) == 0) { BShift++; BMask >>= 1; }
}
static unsigned GetTrueColorPixel(int r, int g, int b)
{
r = pow((r / 255.0), RedGamma) * 255;
g = pow((g / 255.0), GreenGamma) * 255;
b = pow((b / 255.0), BlueGamma) * 255;
return
(((r * RMask) / 255) << RShift) |
(((g * GMask) / 255) << GShift) |
(((b * BMask) / 255) << BShift);
}
/* Interface for General Visual */
static unsigned int GetX11Pixel(int r, int g, int b)
{
switch(model) {
case MONOCHROME:
return GetMonochromePixel(r, g, b);
case GRAYSCALE:
return GetGrayScalePixel(r, g, b);
case PSEUDOCOLOR1:
case PSEUDOCOLOR2:
return GetPseudoColorPixel(r, g, b);
case TRUECOLOR:
return GetTrueColorPixel(r, g, b);
default:
printf("Unknown Visual\n");
}
return 0;
}
static void FreeX11Colors()
{
int i;
if (model == PSEUDOCOLOR2) {
for (i = 0; i < PaletteSize; i++)
XFreeColors(display, colormap, &(XPalette[i].pixel), 1, 0);
PaletteSize = 0;
}
}
static Rboolean SetupX11Color()
{
if (depth <= 1) {
/* On monchome displays we must use black/white */
model = MONOCHROME;
SetupMonochrome();
}
else if (Vclass == StaticGray || Vclass == GrayScale) {
if (model == MONOCHROME)
SetupMonochrome();
else {
model = GRAYSCALE;
SetupGrayScale();
}
}
else if (Vclass == StaticColor) {
/* FIXME : Currently revert to mono. */
/* Should do the real thing. */
model = MONOCHROME;
SetupMonochrome();
}
else if (Vclass == PseudoColor) {
if (model == MONOCHROME)
SetupMonochrome();
else if (model == GRAYSCALE)
SetupGrayScale();
else {
if (model == TRUECOLOR)
model = PSEUDOCOLOR2;
SetupPseudoColor(model);
}
}
else if (Vclass == TrueColor) {
if (model == MONOCHROME)
SetupMonochrome();
else if (model == GRAYSCALE)
SetupGrayScale();
else if (model == PSEUDOCOLOR1 || model == PSEUDOCOLOR2)
SetupPseudoColor(model);
else
SetupTrueColor();
}
else if (Vclass == DirectColor) {
/* FIXME : Currently revert to mono. */
/* Should do the real thing. */
model = MONOCHROME;
SetupMonochrome();
}
else {
printf("Unknown Visual\n");
return FALSE;
}
return TRUE;
}
/* Pixel Dimensions (Inches) */
static double pixelWidth(void)
{
double width, widthMM;
width = DisplayWidth(display, screen);
widthMM = DisplayWidthMM(display, screen);
return ((double)widthMM / (double)width) / MM_PER_INCH;
}
static double pixelHeight(void)
{
double height, heightMM;
height = DisplayHeight(display, screen);
heightMM = DisplayHeightMM(display, screen);
return ((double)heightMM / (double)height) / MM_PER_INCH;
}
static void handleEvent(XEvent event)
{
caddr_t temp;
DevDesc *dd;
x11Desc *xd;
if (event.xany.type == Expose) {
while(XCheckTypedEvent(display, Expose, &event))
;
XFindContext(display, event.xexpose.window,
devPtrContext, &temp);
dd = (DevDesc *) temp;
xd = (x11Desc *) dd->deviceSpecific;
if (xd->resize)
dd->dp.resize(dd);
playDisplayList(dd);
}
else if (event.type == ConfigureNotify) {
XFindContext(display, event.xconfigure.window,
devPtrContext, &temp);
dd = (DevDesc *) temp;
xd = (x11Desc *) dd->deviceSpecific;
xd->windowWidth = event.xconfigure.width;
xd->windowHeight = event.xconfigure.height;
xd->resize = 1;
}
else if ((event.type == ClientMessage) &&
(event.xclient.message_type == _XA_WM_PROTOCOLS))
if (!inclose && event.xclient.data.l[0] == protocol) {
XFindContext(display, event.xclient.window,
devPtrContext, &temp);
dd = (DevDesc *) temp;
KillDevice(dd);
}
}
static void R_ProcessEvents(void *data)
{
XEvent event;
while (displayOpen && XPending(display)) {
XNextEvent(display, &event);
/* printf("%i\n",event.type); */
handleEvent(event);
}
}
#ifdef OLD
/* Font information array. */
/* Point sizes: 6-24 */
/* Faces: plain, bold, oblique, bold-oblique */
/* Symbol may be added later */
#define NFONT 19
static XFontStruct *fontarray[NFONT][5] = {
{NULL, NULL, NULL, NULL, NULL}, /* 6 */
{NULL, NULL, NULL, NULL, NULL}, /* 7 */
{NULL, NULL, NULL, NULL, NULL}, /* 8 */
{NULL, NULL, NULL, NULL, NULL}, /* 9 */
{NULL, NULL, NULL, NULL, NULL}, /* 10 */
{NULL, NULL, NULL, NULL, NULL}, /* 11 */
{NULL, NULL, NULL, NULL, NULL}, /* 12 */
{NULL, NULL, NULL, NULL, NULL}, /* 13 */
{NULL, NULL, NULL, NULL, NULL}, /* 14 */
{NULL, NULL, NULL, NULL, NULL}, /* 15 */
{NULL, NULL, NULL, NULL, NULL}, /* 16 */
{NULL, NULL, NULL, NULL, NULL}, /* 17 */
{NULL, NULL, NULL, NULL, NULL}, /* 18 */
{NULL, NULL, NULL, NULL, NULL}, /* 19 */
{NULL, NULL, NULL, NULL, NULL}, /* 20 */
{NULL, NULL, NULL, NULL, NULL}, /* 21 */
{NULL, NULL, NULL, NULL, NULL}, /* 22 */
{NULL, NULL, NULL, NULL, NULL}, /* 23 */
{NULL, NULL, NULL, NULL, NULL}, /* 24 */
};
static int missingfont[NFONT][5] = {
{0, 0, 0, 0, 0}, /* 6 */
{0, 0, 0, 0, 0}, /* 7 */
{0, 0, 0, 0, 0}, /* 8 */
{0, 0, 0, 0, 0}, /* 9 */
{0, 0, 0, 0, 0}, /* 10 */
{0, 0, 0, 0, 0}, /* 11 */
{0, 0, 0, 0, 0}, /* 12 */
{0, 0, 0, 0, 0}, /* 13 */
{0, 0, 0, 0, 0}, /* 14 */
{0, 0, 0, 0, 0}, /* 15 */
{0, 0, 0, 0, 0}, /* 16 */
{0, 0, 0, 0, 0}, /* 17 */
{0, 0, 0, 0, 0}, /* 18 */
{0, 0, 0, 0, 0}, /* 19 */
{0, 0, 0, 0, 0}, /* 20 */
{0, 0, 0, 0, 0}, /* 21 */
{0, 0, 0, 0, 0}, /* 22 */
{0, 0, 0, 0, 0}, /* 23 */
{0, 0, 0, 0, 0}, /* 24 */
};
static char *fontname_R6 = "-adobe-helvetica-%s-%s-*-*-*-%d-*-*-*-*-*-*";
static char *fontname_R5 = "-adobe-helvetica-%s-%s-*-*-*-%d-*-*-*-*-*-*";
static char *symbolname = "-adobe-symbol-*-*-*-*-*-%d-*-*-*-*-*-*";
static char *fontname;
static char *slant[] = {"r", "o"};
static char *weight[] = {"medium", "bold"};
/* attempt to load a font into the fontarray and return the font */
/* if the font is already there don't load it again */
/* if can't load the font, return NULL */
static XFontStruct *RLoadFont(int face, int size)
{
if (fontarray[size-6][face-1])
return fontarray[size-6][face-1];
else {
char buf[128];
XFontStruct *tmp;
if (face == 5)
sprintf(buf, symbolname, 10 * size);
else
sprintf(buf, fontname,
weight[(face-1)%2],
slant[((face-1)/2)%2], 10 * size);
#ifdef DEBUG_X11
Rprintf("loading:\n%s\n",buf);
#endif
tmp = XLoadQueryFont(display, buf);
#ifdef DEBUG_X11
if (tmp) Rprintf("success\n"); else Rprintf("failure\n");
#endif
if (tmp)
fontarray[size-6][face-1] = tmp;
return tmp;
}
}
/* Quiz the server about fonts. */
/* 1) Try for 100dpi (X11R6) font */
/* 2) Try for *dpi (X11R6) font */
/* 3) Try "fixed" and if that fails, bail out */
static int SetBaseFont(x11Desc *xd)
{
xd->fontface = 1;
xd->fontsize = 12;
xd->usefixed = 0;
fontname = fontname_R6;
xd->font = RLoadFont(xd->fontface, xd->fontsize);
if (!xd->font) {
fontname = fontname_R5;
xd->font = RLoadFont(xd->fontface, xd->fontsize);
}
if (!xd->font) {
xd->usefixed = 1;
xd->font = xd->fixedfont = XLoadQueryFont(display, "fixed");
if (!xd->fixedfont)
return 0;
}
return 1;
}
/* Set the font size and face */
/* If the font of this size and at that the specified */
/* rotation is not present it is loaded. */
/* 0 = plain text, 1 = bold */
/* 2 = oblique, 3 = bold-oblique */
#define SMALLEST 8
#define LARGEST 24
static void SetFont(int face, int size, DevDesc *dd)
{
x11Desc *xd = (x11Desc *) dd->deviceSpecific;
if (face < 1 || face > 5) face = 1;
size = 2 * size / 2;
if (size < SMALLEST) size = SMALLEST;
if (size > LARGEST) size = LARGEST;
if (!xd->usefixed && (size != xd->fontsize || face != xd->fontface)) {
while(size < dd->gp.ps) {
if (fontarray[size-6][face-1]) goto found;
if (!missingfont[size-6][face-1]) {
fontarray[size-6][face-1] = RLoadFont(face, size);
if (fontarray[size-6][face-1]) goto found;
missingfont[size-6][face-1] = 1;
}
size += 2;
}
while(size >= dd->gp.ps) {
if (fontarray[size-6][face-1]) goto found;
if (!missingfont[size-6][face-1]) {
fontarray[size-6][face-1] = RLoadFont(face, size);
if (fontarray[size-6][face-1]) goto found;
missingfont[size-6][face-1] = 1;
}
size -= 2;
}
size = dd->gp.ps;
face = 1;
found:
xd->font = fontarray[size-6][face-1];
xd->fontface = face;
xd->fontsize = size;
XSetFont(display, xd->wgc, xd->font->fid);
}
}
#else /* not OLD */
static char *fontname = "-*-helvetica-%s-%s-*-*-%d-*-*-*-*-*-*-*";
static char *symbolname = "-*-symbol-*-*-*-*-%d-*-*-*-*-*-*-*";
static char *slant[] = {"r", "o"};
static char *weight[] = {"medium", "bold"};
/* Bitmap of the Adobe design sizes */
static unsigned int adobe_sizes = 0x0403165D;
#define MAXFONTS 64
#define CLRFONTS 16 /* Number to free when cache runs full */
typedef struct {int face, size; XFontStruct *font;} cacheentry;
static cacheentry fontcache[MAXFONTS];
static int nfonts = 0;
static int force_nonscalable = 0; /* for testing */
#define ADOBE_SIZE(I) ((I) > 7 && (I) < 35 && (adobe_sizes & (1<<((I)-8))))
#define SMALLEST 2
static XFontStruct *RLoadFont(int face, int size)
{
int pixelsize, i;
cacheentry *f;
char buf[128];
XFontStruct *tmp;
if (size < SMALLEST) size = SMALLEST;
face--;
/* search fontcache */
for ( i = nfonts ; i-- ; ) {
f = &fontcache[i];
if ( f->face == face && f->size == size ) return f->font;
}
/* Here's a 1st class fudge: make sure that the Adobe design sizes
8, 10, 11, 12, 14, 17, 18, 20, 24, 25, 34 can be obtained via
an integer "size" at 100 dpi, namely 6, 7, 8, 9, 10, 12, 13,
14, 17, 18, 24 points. It's almost y = x * 100/72, but not
quite. The constants were found using lm(). --pd */
pixelsize = IS_100DPI ? R_rint(size * 1.43 - 0.4) : size;
if (face == 4)
sprintf(buf, symbolname, pixelsize);
else
sprintf(buf, fontname,
weight[face & 1],
slant[(face & 2)>>1 ], pixelsize);
#ifdef DEBUG_X11
Rprintf("loading:\n%s\n",buf);
#endif
tmp = XLoadQueryFont(display, buf);
#ifdef DEBUG_X11
if (tmp) Rprintf("success\n"); else Rprintf("failure\n");
#endif
if (!tmp || (force_nonscalable && !ADOBE_SIZE(size)) ){
static int near[]=
{14,14,14,17,17,18,20,20,20,20,24,24,24,25,25,25,25};
/* 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 */
/* If ADOBE_SIZE(pixelsize) is true at this point then
the user's system does not have the standard ADOBE font set
so we just have to use a "fixed" font.
If we can't find a "fixed" font then something is seriously
wrong */
if ( ADOBE_SIZE(pixelsize) ) {
tmp = XLoadQueryFont(display, "fixed");
if (tmp)
return tmp;
else
error("Could not find any X11 fonts\nCheck that the Font Path is correct.");
}
if ( pixelsize < 8 )
pixelsize = 8;
else if (pixelsize == 9)
pixelsize = 8;
else if (pixelsize < 30) /* must be at least 13 */
pixelsize = near[size-13];
else
pixelsize = 34;
if (face == 4)
sprintf(buf, symbolname, pixelsize);
else
sprintf(buf, fontname,
weight[face & 1],
slant[(face & 2)>>1 ], pixelsize);
#ifdef DEBUG_X11
Rprintf("loading:\n%s\n",buf);
#endif
tmp = XLoadQueryFont(display, buf);
#ifdef DEBUG_X11
if (tmp) Rprintf("success\n"); else Rprintf("failure\n");
#endif
}
if(!tmp && size > 24) {
/* final try, size 24 */
pixelsize = 24;
if (face == 4)
sprintf(buf, symbolname, 24);
else
sprintf(buf, fontname,
weight[face & 1],
slant[(face & 2)>>1 ], 24);
#ifdef DEBUG_X11
Rprintf("loading:\n%s\n",buf);
#endif
tmp = XLoadQueryFont(display, buf);
#ifdef DEBUG_X11
if (tmp) Rprintf("success\n"); else Rprintf("failure\n");
#endif
}
if (tmp){
f = &fontcache[nfonts++];
f->face = face;
f->size = size;
f->font = tmp;
if (fabs( (pixelsize - size)/(double)size ) > 0.1)
warning("X11 used font size %d when %d was requested",
pixelsize, size);
}
if (nfonts == MAXFONTS) /* make room in the font cache */
{
for (i = 0 ; i < CLRFONTS ; i++)
XFreeFont(display, fontcache[i].font);
for (i = CLRFONTS ; i < MAXFONTS ; i++)
fontcache[i - CLRFONTS] = fontcache[i];
nfonts -= CLRFONTS;
}
return tmp;
}
static int SetBaseFont(x11Desc *xd)
{
xd->fontface = xd->basefontface;
xd->fontsize = xd->basefontsize;
xd->usefixed = 0;
xd->font = RLoadFont(xd->fontface, xd->fontsize);
if (!xd->font) {
xd->usefixed = 1;
xd->font = xd->fixedfont = XLoadQueryFont(display, "fixed");
if (!xd->fixedfont)
return 0;
}
return 1;
}
static void SetFont(int face, int size, DevDesc *dd)
{
x11Desc *xd = (x11Desc *) dd->deviceSpecific;
XFontStruct *tmp;
if (face < 1 || face > 5) face = 1;
if (!xd->usefixed && (size != xd->fontsize || face != xd->fontface)) {
tmp = RLoadFont(face, size);
if(tmp) {
xd->font = tmp;
xd->fontface = face;
xd->fontsize = size;
XSetFont(display, xd->wgc, xd->font->fid);
} else
error("X11 font at size %d could not be loaded", size);
}
}
#endif
static void SetColor(int color, DevDesc *dd)
{
x11Desc *xd = (x11Desc *) dd->deviceSpecific;
if (color != xd->col) {
blackpixel = GetX11Pixel(R_RED(color), R_GREEN(color), R_BLUE(color));
xd->col = color;
XSetState(display, xd->wgc, blackpixel, whitepixel, GXcopy, AllPlanes);
}
}
/* --> See "Notes on Line Textures" in ../include/Graphics.h
*
* 27/5/98 Paul - change to allow lty and lwd to interact:
* the line texture is now scaled by the line width so that,
* for example, a wide (lwd=2) dotted line (lty=2) has bigger
* dots which are more widely spaced. Previously, such a line
* would have "dots" which were wide, but not long, nor widely
* spaced.