-
Notifications
You must be signed in to change notification settings - Fork 0
/
xvps.c
1738 lines (1334 loc) · 46.7 KB
/
xvps.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
/*
* xvps.c - Postscript dialog box, file output functions
*
* callable functions:
*
* CreatePSD(geom) - creates the psW window. Doesn't map it.
* PSDialog() - maps psW
* PSCheckEvent(event) - called by event handler
* PSSaveParams(str,int) - tells PSDialog what to do when 'Ok' clicked
* PSResize() - called whenever ePic changes size
* LoadPS() - attempts to load PS files using Ghostscript
*/
#include "copyright.h"
#define NEEDSDIR
#include "xv.h"
#define PSWIDE 431
#define PSHIGH 350
#define PMAX 200 /* size of square that a 'page' has to fit into */
#define PS_BOK 0
#define PS_BCANC 1
#define PS_BCENT 2
#define PS_BORG 3
#define PS_BMAX 4
#define PS_BPOSX 5
#define PS_BPOSY 6
#define PS_NBUTTS 7
/* paperRB indicies */
#define PSZ_NORM 0
#define PSZ_A4 1
#define PSZ_B5 2
#define PSZ_A3 3
#define PSZ_LEGAL 4
#define PSZ_BSIZE 5
#define PSZ_4BY5 6
#define PSZ_35MM 7
/* orientRB indicies */
#define ORNT_PORT 0
#define ORNT_LAND 1
#define BUTTH 24
#define IN2CM 2.54
#define PIX2INCH 72.0 /* # of pixels per inch, at 100% scaling */
static void drawPSD PARM((int, int, int, int));
static void drawPosStr PARM((void));
static void drawSizeStr PARM((void));
static void drawResStr PARM((void));
static void drawPage PARM((void));
static void clickPSD PARM((int, int));
static void clickPage PARM((int, int));
static void doCmd PARM((int));
static void changedScale PARM((void));
static void setScale PARM((void));
static void changedPaper PARM((void));
static void setPaper PARM((void));
static void drawIRect PARM((int));
static void centerImage PARM((void));
static void maxImage PARM((void));
static void moveImage PARM((double, double));
static void writePS PARM((void));
static int rle_encode PARM((byte *, byte *, int));
static void psColorImage PARM((FILE *));
static void psColorMap PARM((FILE *fp, int, int, byte *, byte *, byte *));
static void psRleCmapImage PARM((FILE *, int));
static void epsPreview PARM((FILE *, byte *, int, int, int, int,
byte *, byte *, byte *, int));
static int writeBWStip PARM((FILE *, byte *, char *, int, int, int));
static void buildCmdStr PARM((char *, char *, char *, int, int));
/* local variables */
static Window pageF;
static DIAL xsDial, ysDial;
static RBUTT *orientRB, *paperRB;
static CBUTT lockCB;
static BUTT psbut[PS_NBUTTS];
static double sz_inx, sz_iny; /* image size, in inches */
static double pos_inx, pos_iny; /* top-left offset of image, in inches */
static int dpix, dpiy; /* # of image pixels per inch */
static int tracking=0; /* used in changedScale */
static int posxType, posyType;
/* sizes of pages in inches */
static double paperSize[8][2] = { { 8.500, 11.000}, /* US NORMAL */
{ 8.268, 11.693}, /* A4 210mm x 297mm */
{ 7.205, 10.118}, /* B5 183mm x 257mm */
{11.693, 16.535}, /* A3 297mm x 420mm */
{ 8.500, 14.000}, /* US LEGAL */
{11.000, 17.000}, /* B-size */
{ 3.875, 4.875}, /* 4 by 5 */
{ 0.945, 1.417}}; /* 35mm (24x36) */
/* size of l+r margin and t+b margin. image is centered */
static double margins[8][2] = { { 1.000, 1.000}, /* US NORMAL */
{ 1.000, 1.000}, /* A4 */
{ 1.000, 1.000}, /* B5 */
{ 1.000, 1.000}, /* A3 */
{ 1.000, 1.000}, /* US LEGAL */
{ 1.000, 1.000}, /* B-size */
{ 0.275, 0.275}, /* 4 by 5 */
{ 0.078, 0.078}}; /* 35mm (24x36) */
static double psizex, psizey; /* current paper size, in inches */
static double in2pix; /* inch to pixels in 'pageF' */
static XRectangle pageRect; /* bounding rect of page, in screen coords */
static char *filename; /* filename to save to */
static int colorType; /* value of 'Colors' rbutt in dir box */
static int firsttime=1; /* first time PSDialog being opened ? */
/***************************************************/
void CreatePSD(geom)
char *geom;
{
psW = CreateWindow("xv postscript", "XVps", geom,
PSWIDE, PSHIGH, infofg, infobg, 0);
if (!psW) FatalError("can't create postscript window!");
pageF = XCreateSimpleWindow(theDisp, psW, 20,30, PMAX+1,PMAX+1,
1,infofg,infobg);
if (!pageF) FatalError("couldn't create frame windows");
XSetWindowBackgroundPixmap(theDisp, pageF, grayTile);
XSelectInput(theDisp, pageF, ExposureMask | ButtonPressMask);
XSelectInput(theDisp, psW, ExposureMask | ButtonPressMask | KeyPressMask);
CBCreate(&encapsCB, psW, 240, 7, "preview", infofg, infobg, hicol, locol);
CBCreate(&pscompCB, psW, 331, 7, "compress", infofg, infobg, hicol, locol);
DCreate(&xsDial, psW, 240, 30, 80, 100, 10.0, 800.0, 100.0, 0.5, 5.0,
infofg, infobg, hicol, locol, "Width", "%");
DCreate(&ysDial, psW, 331, 30, 80, 100, 10.0, 800.0, 100.0, 0.5, 5.0,
infofg, infobg, hicol, locol, "Height", "%");
xsDial.drawobj = changedScale;
ysDial.drawobj = changedScale;
CBCreate(&lockCB, psW, 318, 134, "", infofg, infobg, hicol, locol);
lockCB.val = 1;
orientRB = RBCreate(NULL, psW, 36, 240+18, "Portrait", infofg, infobg,
hicol, locol);
RBCreate(orientRB, psW, 36+80, 240+18, "Landscape", infofg, infobg,
hicol, locol);
paperRB = RBCreate(NULL, psW,36, 240+18+36, "8.5\"x11\"",
infofg, infobg, hicol, locol);
RBCreate(paperRB, psW, 36+80, 240+18+36, "A4",
infofg, infobg, hicol, locol);
RBCreate(paperRB, psW, 36+122, 240+18+36, "B5",
infofg, infobg, hicol, locol);
RBCreate(paperRB, psW, 36+164, 240+18+36, "A3",
infofg, infobg, hicol, locol);
RBCreate(paperRB, psW, 36, 240+36+36, "8.5\"x14\"",
infofg, infobg, hicol, locol);
RBCreate(paperRB, psW, 36+80, 240+36+36, "11\"x17\"",
infofg, infobg, hicol, locol);
RBCreate(paperRB, psW, 36, 240+54+36, "4\"x5\"",
infofg, infobg, hicol, locol);
RBCreate(paperRB, psW, 36+80, 240+54+36, "35mm slide",
infofg, infobg, hicol, locol);
BTCreate(&psbut[PS_BOK], psW, PSWIDE-180, PSHIGH-10-BUTTH, 80, BUTTH,
"Ok", infofg, infobg, hicol, locol);
BTCreate(&psbut[PS_BCANC], psW, PSWIDE-90, PSHIGH-10-BUTTH, 80, BUTTH,
"Cancel", infofg, infobg, hicol, locol);
BTCreate(&psbut[PS_BCENT], psW, 240, 154, 55, BUTTH-2,
"Center", infofg, infobg, hicol, locol);
BTCreate(&psbut[PS_BORG], psW, 298, 154, 55, BUTTH-2,
"Origin", infofg, infobg, hicol, locol);
BTCreate(&psbut[PS_BMAX], psW, 356, 154, 55, BUTTH-2,
"Max", infofg, infobg, hicol, locol);
BTCreate(&psbut[PS_BPOSX], psW, 256-14, 190+13-8, 8,8, "",
infofg, infobg, hicol, locol);
BTCreate(&psbut[PS_BPOSY], psW, 256-14, 190+26-8, 8,8, "",
infofg, infobg, hicol, locol);
posxType = posyType = 0;
pos_inx = 1.0; pos_iny = 1.0; /* temporary bootstrapping... */
setPaper();
setScale();
/*** handle PS dialog resources ***/
if (rd_str("pspaper")) { /* xv.pspaper: default paper size */
char buf[256];
strcpy(buf, def_str);
lower_str(buf);
if (!strcmp(buf, "8.5x11")) RBSelect(paperRB, PSZ_NORM);
else if (!strcmp(buf, "a4")) RBSelect(paperRB, PSZ_A4);
else if (!strcmp(buf, "b5")) RBSelect(paperRB, PSZ_B5);
else if (!strcmp(buf, "a3")) RBSelect(paperRB, PSZ_A3);
else if (!strcmp(buf, "8.5x14")) RBSelect(paperRB, PSZ_LEGAL);
else if (!strcmp(buf, "11x17")) RBSelect(paperRB, PSZ_BSIZE);
else if (!strcmp(buf, "4x5")) RBSelect(paperRB, PSZ_4BY5);
else if (!strcmp(buf, "35mm")) RBSelect(paperRB, PSZ_35MM);
else {
fprintf(stderr,"%s: unknown 'pspaper' value: %s\n",cmd,def_str);
fprintf(stderr,
"\tValid choices: 8.5x11 A4 B5 A3 8.5x14 11x17 4x5 35mm\n");
}
setPaper();
}
if (rd_str("psorient")) { /* xv.psorient: default paper ornt. */
char buf[256];
strcpy(buf, def_str);
lower_str(buf);
if (!strcmp(buf, "portrait")) RBSelect(orientRB, ORNT_PORT);
else if (!strcmp(buf, "landscape")) RBSelect(orientRB, ORNT_LAND);
else {
fprintf(stderr,"%s: unknown 'psorient' value: %s\n",cmd,def_str);
fprintf(stderr,
"\tValid choices: portrait landscape\n");
}
setPaper();
}
if (rd_int("psres")) { /* xv.psres: default paper resolution */
if (def_int >= 10 && def_int <= 720) {
double v = (PIX2INCH * 100) / def_int;
DSetVal(&xsDial, v);
DSetVal(&ysDial, v);
}
}
XMapSubwindows(theDisp, psW);
}
/***************************************************/
void PSDialog(vis)
int vis;
{
if (vis) {
if (picType == PIC24) { /* no comp in 24-bit mode */
pscompCB.val = 0;
CBSetActive(&pscompCB, 0);
}
else CBSetActive(&pscompCB, 1);
setScale();
if (firsttime) centerImage();
firsttime = 0;
CenterMapWindow(psW, psbut[PS_BOK].x + (int) psbut[PS_BOK].w/2,
psbut[PS_BOK].y + (int) psbut[PS_BOK].h/2,
PSWIDE, PSHIGH);
}
else XUnmapWindow(theDisp, psW);
psUp = vis;
}
/***************************************************/
int PSCheckEvent(xev)
XEvent *xev;
{
/* check event to see if it's for one of our subwindows. If it is,
deal accordingly, and return '1'. Otherwise, return '0' */
int rv;
rv = 1;
if (!psUp) return 0;
if (xev->type == Expose) {
int x,y,w,h;
XExposeEvent *e = (XExposeEvent *) xev;
x = e->x; y = e->y; w = e->width; h = e->height;
/* throw away excess expose events for 'dumb' windows */
if (e->count > 0 &&
(e->window == xsDial.win || e->window == ysDial.win ||
e->window == pageF)) {}
else if (e->window == psW) drawPSD(x, y, w, h);
else if (e->window == xsDial.win) DRedraw(&xsDial);
else if (e->window == ysDial.win) DRedraw(&ysDial);
else if (e->window == pageF) drawPage();
else rv = 0;
}
else if (xev->type == ButtonPress) {
XButtonEvent *e = (XButtonEvent *) xev;
int x,y;
x = e->x; y = e->y;
if (e->button == Button1) {
if (e->window == psW) clickPSD(x,y);
else if (e->window == pageF) clickPage(x,y);
else if (e->window == xsDial.win || e->window == ysDial.win) {
if (e->window == xsDial.win) {
tracking = 1;
DTrack(&xsDial, x,y);
tracking = 0;
}
else if (e->window == ysDial.win) {
tracking = 2;
DTrack(&ysDial, x,y);
tracking = 0;
}
}
else rv = 0;
} /* button1 */
else rv = 0;
} /* button press */
else if (xev->type == KeyPress) {
XKeyEvent *e = (XKeyEvent *) xev;
char buf[128]; KeySym ks;
int stlen, shift, ck;
stlen = XLookupString(e,buf,128,&ks,(XComposeStatus *) NULL);
shift = e->state & ShiftMask;
ck = CursorKey(ks, shift, 0);
buf[stlen] = '\0';
RemapKeyCheck(ks, buf, &stlen);
if (e->window == psW) {
double dx, dy;
int movekey;
movekey = 0; dx = dy = 0.0;
if (ck==CK_LEFT) { dx = -0.001; movekey = 1; }
else if (ck==CK_RIGHT) { dx = 0.001; movekey = 1; }
else if (ck==CK_UP) { dy = -0.001; movekey = 1; }
else if (ck==CK_DOWN) { dy = 0.001; movekey = 1; }
else if (stlen) {
if (buf[0] == '\r' || buf[0] == '\n') { /* enter */
FakeButtonPress(&psbut[PS_BOK]);
}
else if (buf[0] == '\033') { /* ESC */
FakeButtonPress(&psbut[PS_BCANC]);
}
}
if (movekey) {
if (e->state & ShiftMask) { dx *= 10.0; dy *= 10.0; }
moveImage(pos_inx+dx, pos_iny+dy);
}
}
else rv = 0;
}
else rv = 0;
if (rv==0 && (xev->type == ButtonPress || xev->type == KeyPress)) {
XBell(theDisp, 50);
rv = 1; /* eat it */
}
return rv;
}
/***************************************************/
void PSSaveParams(fname, col)
char *fname;
int col;
{
filename = fname;
colorType = col;
}
/***************************************************/
void PSResize()
{
changedScale();
}
/***************************************************/
static void drawPSD(x,y,w,h)
int x,y,w,h;
{
char *title = "Save PostScript File...";
int i,cx;
XRectangle xr;
xr.x = x; xr.y = y; xr.width = w; xr.height = h;
XSetClipRectangles(theDisp, theGC, 0,0, &xr, 1, Unsorted);
XSetForeground(theDisp, theGC, infofg);
XSetBackground(theDisp, theGC, infobg);
RBRedraw(orientRB,-1);
RBRedraw(paperRB,-1);
for (i=0; i<PS_NBUTTS; i++) BTRedraw(&psbut[i]);
CBRedraw(&encapsCB);
if (colorType != F_BWDITHER && picType!=PIC24) CBRedraw(&pscompCB);
CBRedraw(&lockCB);
ULineString(psW, orientRB->x-16, orientRB->y-3-DESCENT, "Orientation:");
ULineString(psW, paperRB->x-16, paperRB->y-3-DESCENT, "Paper Size:");
/* draw 'lock' arrows */
cx = 240 + 40; /* center of xsDial */
XDrawLine(theDisp, psW, theGC, lockCB.x, lockCB.y+6, cx+2, lockCB.y+6);
XDrawLine(theDisp, psW, theGC, cx+2, lockCB.y+6, cx+2, lockCB.y-2);
XDrawLine(theDisp, psW, theGC, lockCB.x, lockCB.y+10, cx-2, lockCB.y+10);
XDrawLine(theDisp, psW, theGC, cx-2, lockCB.y+10, cx-2, lockCB.y-2);
XDrawLine(theDisp, psW, theGC, cx-2-3, lockCB.y-2+3, cx, lockCB.y-2-2);
XDrawLine(theDisp, psW, theGC, cx+2+3, lockCB.y-2+3, cx, lockCB.y-2-2);
cx = 330 + 40; /* center of ysDial */
XDrawLine(theDisp, psW, theGC, lockCB.x+17, lockCB.y+6, cx-2, lockCB.y+6);
XDrawLine(theDisp, psW, theGC, cx-2, lockCB.y+6, cx-2, lockCB.y-2);
XDrawLine(theDisp, psW, theGC, lockCB.x+17, lockCB.y+10, cx+2, lockCB.y+10);
XDrawLine(theDisp, psW, theGC, cx+2, lockCB.y+10, cx+2, lockCB.y-2);
XDrawLine(theDisp, psW, theGC, cx-2-3, lockCB.y-2+3, cx, lockCB.y-2-2);
XDrawLine(theDisp, psW, theGC, cx+2+3, lockCB.y-2+3, cx, lockCB.y-2-2);
DrawString(psW, 10, 19, title);
ULineString(psW, 240, 190, "Position:");
drawPosStr();
ULineString(psW, 240, 190+45, "Size:");
drawSizeStr();
ULineString(psW, 240, 190+90, "Resolution:");
drawResStr();
XSetClipMask(theDisp, theGC, None);
}
/***************************************************/
static void drawPosStr()
{
int x,y;
double cmx, cmy, inx, iny;
char str[64], str1[64], *xst, *yst;
x = 256; y = 190 + 13;
inx = iny = 0; xst = yst = (char *) NULL;
switch (posxType) {
case 0: xst = "Left: "; inx = pos_inx; break;
case 1: xst = "Right:"; inx = psizex - (pos_inx + sz_inx); break;
case 2: xst = "X Mid:"; inx = pos_inx + sz_inx/2; break;
}
switch (posyType) {
case 0: yst = "Top: "; iny = pos_iny; break;
case 1: yst = "Bot: "; iny = psizey - (pos_iny + sz_iny); break;
case 2: yst = "Y Mid:"; iny = pos_iny + sz_iny/2; break;
}
cmx = inx * IN2CM;
cmy = iny * IN2CM;
sprintf(str, "%s %.3f\" (%.2fcm) ", xst, inx, cmx);
sprintf(str1, "%s %.3f\" (%.2fcm) ", yst, iny, cmy);
XSetForeground(theDisp, theGC, infofg);
XSetBackground(theDisp, theGC, infobg);
XSetFont(theDisp, theGC, monofont);
XDrawImageString(theDisp, psW, theGC, x, y, str, (int) strlen(str));
XDrawImageString(theDisp, psW, theGC, x, y+13, str1, (int) strlen(str1));
XSetFont(theDisp, theGC, mfont);
}
/***************************************************/
static void drawSizeStr()
{
int x,y;
double cmx, cmy;
char str[64], str1[64];
x = 256; y = 190+13+45;
cmx = sz_inx * IN2CM;
cmy = sz_iny * IN2CM;
sprintf(str, "%.3f\" x %.3f\" ", sz_inx, sz_iny);
sprintf(str1, "%.2fcm x %.2fcm ", cmx, cmy);
XSetForeground(theDisp, theGC, infofg);
XSetBackground(theDisp, theGC, infobg);
XSetFont(theDisp, theGC, monofont);
XDrawImageString(theDisp, psW, theGC, x, y, str, (int) strlen(str));
XDrawImageString(theDisp, psW, theGC, x, y+13, str1, (int) strlen(str1));
XSetFont(theDisp, theGC, mfont);
}
/***************************************************/
static void drawResStr()
{
int x,y;
char str[64];
x = 256; y = 190 + 13 + 90;
sprintf(str, "%ddpi x %ddpi ", dpix, dpiy);
XSetForeground(theDisp, theGC, infofg);
XSetBackground(theDisp, theGC, infobg);
XSetFont(theDisp, theGC, monofont);
XDrawImageString(theDisp, psW, theGC, x, y, str, (int) strlen(str));
XSetFont(theDisp, theGC, mfont);
}
/***************************************************/
static void drawPage()
{
/* draw page */
XSetForeground(theDisp, theGC, infobg);
XFillRectangle(theDisp, pageF, theGC, pageRect.x+1, pageRect.y+1,
(u_int) pageRect.width-1, (u_int) pageRect.height-1);
XSetForeground(theDisp, theGC, infofg);
XDrawRectangle(theDisp, pageF, theGC, pageRect.x, pageRect.y,
(u_int) pageRect.width, (u_int) pageRect.height);
drawIRect(1);
}
/***************************************************/
static void clickPSD(x,y)
int x,y;
{
int i;
BUTT *bp;
/* check BUTTs */
for (i=0; i<PS_NBUTTS; i++) {
bp = &psbut[i];
if (PTINRECT(x, y, bp->x, bp->y, bp->w, bp->h)) break;
}
if (i<PS_NBUTTS) { /* found one */
if (BTTrack(bp)) doCmd(i);
}
/* check RBUTTs */
else if ((i=RBClick(orientRB,x,y)) >= 0) {
if (RBTrack(orientRB, i)) changedPaper();
}
else if ((i=RBClick(paperRB,x,y)) >= 0) {
if (RBTrack(paperRB, i)) changedPaper();
}
/* check CBUTTs */
else if (CBClick(&lockCB,x,y)) {
if (CBTrack(&lockCB) && lockCB.val) { /* turned on lock */
DSetVal(&ysDial, xsDial.val); /* copy xsDial.val to ysDial */
changedScale();
}
}
else if (CBClick(&encapsCB,x,y)) CBTrack(&encapsCB);
else if (CBClick(&pscompCB,x,y)) CBTrack(&pscompCB);
}
/***************************************************/
static void clickPage(mx,my)
int mx,my;
{
Window rW,cW;
int rx,ry,x,y;
unsigned int mask;
double offx, offy, newx, newy;
/* compute offset (in inches) between 'drag point' and
the top-left corner of the image */
offx = ((mx - pageRect.x) / in2pix) - pos_inx;
offy = ((my - pageRect.y) / in2pix) - pos_iny;
/* if clicked outside of image rectangle, ignore */
if (offx<0.0 || offy < 0.0 || offx>=sz_inx || offy >= sz_iny) return;
while (1) {
if (XQueryPointer(theDisp,pageF,&rW,&cW,&rx,&ry,&x,&y,&mask)) {
if (!(mask & Button1Mask)) break; /* button released */
/* compute new pos_inx, pos_iny based on x,y coords */
newx = ((x-pageRect.x) / in2pix) - offx;
newy = ((y-pageRect.y) / in2pix) - offy;
moveImage(newx, newy);
}
}
}
/***************************************************/
static void doCmd(cmd)
int cmd;
{
char *fullname;
switch (cmd) {
case PS_BOK: writePS();
PSDialog(0);
fullname = GetDirFullName();
if (!ISPIPE(fullname[0])) {
XVCreatedFile(fullname);
StickInCtrlList(0);
}
break;
case PS_BCANC: PSDialog(0); break;
case PS_BCENT: drawIRect(0);
centerImage();
drawIRect(1);
drawPosStr();
break;
case PS_BORG: drawIRect(0);
pos_inx = 0.0;
pos_iny = (psizey - sz_iny);
drawIRect(1);
drawPosStr();
break;
case PS_BMAX: drawIRect(0);
maxImage();
drawIRect(1);
drawPosStr();
drawSizeStr();
drawResStr();
break;
case PS_BPOSX: posxType = (posxType + 1) % 3;
drawPosStr();
break;
case PS_BPOSY: posyType = (posyType + 1) % 3;
drawPosStr();
break;
default: break;
}
}
/***************************************************/
static void changedScale()
{
double oldx,oldy;
drawIRect(0);
if (lockCB.val) {
if (tracking == 1) DSetVal(&ysDial, xsDial.val);
else if (tracking == 2) DSetVal(&xsDial, ysDial.val);
}
oldx = pos_inx; oldy = pos_iny;
setScale();
drawIRect(1);
if (pos_inx != oldx || pos_iny != oldy ||
posxType != 0 || posyType != 0) drawPosStr();
drawSizeStr();
drawResStr();
XFlush(theDisp);
}
/***************************************************/
static void setScale()
{
double hsx, hsy;
int w,h;
GetSaveSize(&w, &h);
sz_inx = (double) w / PIX2INCH * (xsDial.val / 100.0);
sz_iny = (double) h / PIX2INCH * (ysDial.val / 100.0);
/* round to integer .001ths of an inch */
sz_inx = floor(sz_inx * 1000.0 + 0.5) / 1000.0;
sz_iny = floor(sz_iny * 1000.0 + 0.5) / 1000.0;
dpix = (int) (PIX2INCH / (xsDial.val / 100.0));
dpiy = (int) (PIX2INCH / (ysDial.val / 100.0));
/* make sure 'center' of image is still on page */
hsx = sz_inx/2; hsy = sz_iny/2;
RANGE(pos_inx, -hsx, psizex-hsx);
RANGE(pos_iny, -hsy, psizey-hsy);
/* round to integer .001ths of an inch */
pos_inx = floor(pos_inx * 1000.0 + 0.5) / 1000.0;
pos_iny = floor(pos_iny * 1000.0 + 0.5) / 1000.0;
}
/***************************************************/
static void changedPaper()
{
setPaper();
XClearWindow(theDisp, pageF);
centerImage();
drawPosStr();
drawPage();
}
/***************************************************/
static void setPaper()
{
int i;
double tmp;
i = RBWhich(paperRB);
psizex = paperSize[i][0];
psizey = paperSize[i][1];
in2pix = (double) PMAX / psizey;
if (RBWhich(orientRB)==ORNT_LAND) {
tmp = psizex; psizex = psizey; psizey = tmp;
}
pageRect.x = (int) ((PMAX/2) - ((psizex/2.0) * in2pix));
pageRect.y = (int) ((PMAX/2) - ((psizey/2.0) * in2pix));
pageRect.width = (int) (psizex * in2pix);
pageRect.height = (int) (psizey * in2pix);
}
/***************************************************/
static void drawIRect(draw)
int draw;
{
int x,y,w,h;
XRectangle xr;
x = pageRect.x + (int) (pos_inx * in2pix);
y = pageRect.y + (int) (pos_iny * in2pix);
w = sz_inx * in2pix;
h = sz_iny * in2pix;
xr.x = pageRect.x + 1;
xr.y = pageRect.y + 1;
xr.width = pageRect.width - 1;
xr.height = pageRect.height - 1;
if (draw) XSetForeground(theDisp, theGC, infofg);
else XSetForeground(theDisp, theGC, infobg);
XSetClipRectangles(theDisp, theGC, 0,0, &xr, 1, Unsorted);
XDrawRectangle(theDisp, pageF, theGC, x, y, (u_int) w, (u_int) h);
XDrawLine(theDisp, pageF, theGC, x, y, x+w, y+h);
XDrawLine(theDisp, pageF, theGC, x, y+h, x+w, y);
XSetClipMask(theDisp, theGC, None);
}
/***************************************************/
static void centerImage()
{
pos_inx = psizex/2 - sz_inx/2;
pos_iny = psizey/2 - sz_iny/2;
/* round to integer .001ths of an inch */
pos_inx = floor(pos_inx * 1000.0 + 0.5) / 1000.0;
pos_iny = floor(pos_iny * 1000.0 + 0.5) / 1000.0;
}
/***************************************************/
static void maxImage()
{
double scx, scy;
int w,h;
GetSaveSize(&w, &h);
sz_inx = psizex - margins[RBWhich(paperRB)][0];
sz_iny = psizey - margins[RBWhich(paperRB)][1];
/* choose the smaller scaling factor */
scx = sz_inx / w;
scy = sz_iny / h;
if (scx < scy) { sz_iny = h * scx; }
else { sz_inx = w * scy; }
DSetVal(&xsDial, 100 * (sz_inx * PIX2INCH) / w);
DSetVal(&ysDial, xsDial.val);
sz_inx = (double) w / PIX2INCH * (xsDial.val / 100.0);
sz_iny = (double) h / PIX2INCH * (ysDial.val / 100.0);
/* round to integer .001ths of an inch */
sz_inx = floor(sz_inx * 1000.0 + 0.5) / 1000.0;
sz_iny = floor(sz_iny * 1000.0 + 0.5) / 1000.0;
dpix = (int) (PIX2INCH / (xsDial.val / 100.0));
dpiy = (int) (PIX2INCH / (ysDial.val / 100.0));
pos_inx = psizex/2 - sz_inx/2;
pos_iny = psizey/2 - sz_iny/2;
/* round to integer .001ths of an inch */
pos_inx = floor(pos_inx * 1000.0 + 0.5) / 1000.0;
pos_iny = floor(pos_iny * 1000.0 + 0.5) / 1000.0;
}
/***************************************************/
static void moveImage(newx,newy)
double newx, newy;
{
double hsx, hsy;
hsx = sz_inx/2; hsy = sz_iny/2;
/* round to integer .001ths of an inch */
newx = floor(newx * 1000.0 + 0.5) / 1000.0;
newy = floor(newy * 1000.0 + 0.5) / 1000.0;
/* keep center of image within page limits */
RANGE(newx, -hsx, psizex-hsx);
RANGE(newy, -hsy, psizey-hsy);
if (newx != pos_inx || newy != pos_iny) { /* moved */
drawIRect(0);
pos_inx = newx;
pos_iny = newy;
drawIRect(1);
drawPosStr();
}
}
/***************************************************/
static void writePS()
{
FILE *fp;
int i, j, err, rpix, gpix, bpix, nc, ptype;
int iw, ih, ox, oy, slen, lwidth, bits, colorps, w, h, pfree;
double iwf, ihf;
byte *inpix, *rmap, *gmap, *bmap;
slen = bits = colorps = 0;
fp = OpenOutFile(filename);
if (!fp) return;
WaitCursor();
inpix = GenSavePic(&ptype, &w, &h, &pfree, &nc, &rmap, &gmap, &bmap);
/* printed image will have size iw,ih (in picas) */
iw = (int) (sz_inx * 72.0 + 0.5);
ih = (int) (sz_iny * 72.0 + 0.5);
iwf = sz_inx * 72.0;
ihf = sz_iny * 72.0;
/* compute offset to bottom-left of image (in picas) */
ox = (int) (pos_inx * 72.0 + 0.5);
oy = (int) ((psizey - (pos_iny + sz_iny)) * 72.0 + 0.5);
/*** write PostScript header ***/
fprintf(fp,"%%!PS-Adobe-2.0 EPSF-2.0\n");
fprintf(fp,"%%%%Title: %s\n",filename);
fprintf(fp,"%%%%Creator: XV %s - by John Bradley\n",REVDATE);
if (RBWhich(orientRB)==ORNT_LAND) /* Landscape mode */
fprintf(fp,"%%%%BoundingBox: %d %d %d %d\n",
(int) (pos_iny * 72.0 + 0.5),
(int) (pos_inx * 72.0 + 0.5),
(int) (pos_iny * 72.0 + 0.5) + ih,
(int) (pos_inx * 72.0 + 0.5) + iw);
else
fprintf(fp,"%%%%BoundingBox: %d %d %d %d\n", ox, oy, ox+iw, oy+ih);
fprintf(fp,"%%%%Pages: 1\n");
fprintf(fp,"%%%%DocumentFonts:\n");
fprintf(fp,"%%%%EndComments\n");
switch (colorType) {
case F_FULLCOLOR:
case F_REDUCED: slen = w*3; bits = 8; colorps = 1; break;
case F_GREYSCALE: slen = w; bits = 8; colorps = 0; break;
case F_BWDITHER: slen = (w+7)/8; bits = 1; colorps = 0; break;
default: FatalError("unknown colorType in writePS()"); break;
}
if (encapsCB.val) epsPreview(fp, inpix, ptype, colorType, w, h,
rmap,gmap,bmap,
(RBWhich(orientRB)==ORNT_LAND) );
fprintf(fp,"%%%%EndProlog\n\n");
fprintf(fp,"%%%%Page: 1 1\n\n");
fprintf(fp,"%% remember original state\n");
fprintf(fp,"/origstate save def\n\n");
fprintf(fp,"%% build a temporary dictionary\n");
fprintf(fp,"20 dict begin\n\n");
if (colorType == F_BWDITHER || ptype==PIC24 || !pscompCB.val) {
fprintf(fp,"%% define string to hold a scanline's worth of data\n");
fprintf(fp,"/pix %d string def\n\n", slen);
}
/*
* Add loop invariant strings that should not be defined
* over and over again inside a loop
* K. Schnepper DLR FF-DR Oberpfaffenhofen, Mai 12. 1993
*/
fprintf(fp,"%% define space for color conversions\n");
fprintf(fp,"/grays %d string def %% space for gray scale line\n", w);
fprintf(fp,"/npixls 0 def\n");
fprintf(fp,"/rgbindx 0 def\n\n");
if (RBWhich(orientRB)==ORNT_LAND) { /* Landscape mode */
fprintf(fp,"%% print in landscape mode\n");
fprintf(fp,"90 rotate 0 %d translate\n\n",(int) (-psizey*72.0));
}
if (RBWhich(paperRB) == PSZ_4BY5 ||
RBWhich(paperRB) == PSZ_35MM) {
fprintf(fp,"%% we're going to a 4x5 or a 35mm film recorder.\n");
fprintf(fp,"%% clear page to black to avoid registration problems\n");
fprintf(fp,"newpath\n");
fprintf(fp," 0 0 moveto\n");
fprintf(fp," 0 %d rlineto\n", (int) (psizey * 72.0));
fprintf(fp," %d 0 rlineto\n", (int) (psizex * 72.0));
fprintf(fp," 0 %d rlineto\n", (int) (-psizey * 72.0));
fprintf(fp," closepath\n");
fprintf(fp," 0 setgray\n");
fprintf(fp," fill\n\n");
}
fprintf(fp,"%% lower left corner\n");
fprintf(fp,"%d %d translate\n\n",ox,oy);