-
Notifications
You must be signed in to change notification settings - Fork 99
Expand file tree
/
Copy pathboundary.c
More file actions
1628 lines (1425 loc) · 57.7 KB
/
boundary.c
File metadata and controls
1628 lines (1425 loc) · 57.7 KB
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
/* GNUPLOT - boundary.c */
/*[
* Copyright 1986 - 1993, 1998, 2004 Thomas Williams, Colin Kelley
*
* Permission to use, copy, and distribute this software and its
* documentation for any purpose with or without fee is hereby granted,
* provided that the above copyright notice appear in all copies and
* that both that copyright notice and this permission notice appear
* in supporting documentation.
*
* Permission to modify the software is granted, but not the right to
* distribute the complete modified source code. Modifications are to
* be distributed as patches to the released version. Permission to
* distribute binaries produced by compiling modified sources is granted,
* provided you
* 1. distribute the corresponding source modifications from the
* released version in the form of a patch file along with the binaries,
* 2. add special version identification to distinguish your version
* in addition to the base release version number,
* 3. provide your name and address as the primary contact for the
* support of your modified version, and
* 4. retain our contact information in regard to use of the base
* software.
* Permission to distribute the released version of the source code along
* with corresponding source modifications in the form of a patch file is
* granted with same provisions 2 through 4 for binary distributions.
*
* This software is provided "as is" without express or implied warranty
* to the extent permitted by applicable law.
]*/
#include "graphics.h"
#include "boundary.h"
#include "alloc.h"
#include "axis.h"
#include "misc.h"
#include "plot2d.h"
#include "pm3d.h" /* for is_plot_with_palette */
#define ERRORBARTIC GPMAX((t->h_tic/2),1)
/*{{{ local variables */
static int xlablin, x2lablin, ylablin, y2lablin, titlelin, xticlin, x2ticlin;
/*{{{ local and global variables */
static int key_sample_width; /* width of line sample */
static int key_sample_height; /* sample itself; does not scale with "set key spacing" */
static int key_sample_left; /* offset from x for left of line sample */
static int key_sample_right; /* offset from x for right of line sample */
static int key_text_left; /* offset from x for left-justified text */
static int key_text_right; /* offset from x for right-justified text */
static int key_size_left; /* size of left bit of key (text or sample, depends on key->reverse) */
static int key_size_right; /* size of right part of key (including padding) */
static int key_xleft; /* Amount of space on the left required by the key */
static int max_ptitl_len = 0; /* max length of plot-titles (keys) */
static int ptitl_cnt; /* count keys with len > 0 */
static int key_width; /* calculate once, then everyone uses it */
static int key_height; /* ditto */
static int key_title_height; /* nominal number of lines * character height */
static int key_title_extra; /* allow room for subscript/superscript */
static int key_title_ypos; /* offset from key->bounds.ytop */
static int time_y, time_x;
int title_x, title_y; /* Used by boundary and by 2D graphics */
/*
* These quantities are needed in do_plot() e.g. for histogtram title layout
*/
int key_entry_height; /* bigger of t->v_char, t->v_tic */
int key_point_offset; /* offset from x for point sample */
int ylabel_x, y2label_x, xlabel_y, x2label_y;
int x2label_yoffset;
int ylabel_y, y2label_y, xtic_y, x2tic_y, ytic_x, y2tic_x;
int key_rows;
int key_cols;
int key_count;
static int key_col_wth, yl_ref;
static int xl, yl;
/*{{{ boundary() */
/* borders of plotting area
* computed once on every call to do_plot
*
* The order in which things are done has become critical:
* plot_bounds.ytop depends on title, x2label
* plot_bounds.ybot depends on key, if "under"
* once we have these, we can setup the y1 and y2 tics and the
* only then can we calculate plot_bounds.xleft and plot_bounds.xright
* plot_bounds.xright depends also on key RIGHT
* then we can do x and x2 tics
*
* For set size ratio ..., everything depends on everything else...
* not really a lot we can do about that, so we lose if the plot has to
* be reduced vertically. But the chances are the
* change will not be very big, so the number of tics will not
* change dramatically.
*
* Margin computation redone by Dick Crawford ([email protected]) 4/98
*/
void
boundary(struct curve_points *plots, int count)
{
int yticlin = 0, y2ticlin = 0;
legend_key *key = &keyT;
struct termentry *t = term;
TBOOLEAN can_rotate = (*t->text_angle) (TEXT_VERTICAL);
int xtic_textheight=0; /* height of xtic labels */
int x2tic_textheight=0; /* height of x2tic labels */
int title_textheight=0; /* height of title */
int xlabel_textheight=0; /* height of xlabel */
int x2label_textheight=0; /* height of x2label */
int ylabel_textwidth=0; /* width of (rotated) ylabel */
int y2label_textwidth=0; /* width of (rotated) y2label */
int timelabel_textwidth=0; /* width of timestamp */
int timelabel_textheight=0; /* height of timestamp */
int ytic_textwidth=0; /* width of ytic labels */
int y2tic_textwidth=0; /* width of y2tic labels */
int x2tic_height=0; /* 0 for tic_in or no x2tics, ticscale*v_tic otherwise */
int xtic_textwidth=0; /* amount by which the xtic label protrude to the right */
int xtic_height=0;
int ytic_width=0;
int y2tic_width=0;
int ttic_textheight=0; /* vertical clearance for ttics */
/* figure out which rotatable items are to be rotated
* (ylabel and y2label are rotated if possible) */
int vertical_timelabel = can_rotate ? timelabel.rotate : 0;
int vertical_xtics = can_rotate ? axis_array[FIRST_X_AXIS].tic_rotate : 0;
int vertical_x2tics = can_rotate ? axis_array[SECOND_X_AXIS].tic_rotate : 0;
int vertical_ytics = can_rotate ? axis_array[FIRST_Y_AXIS].tic_rotate : 0;
int vertical_y2tics = can_rotate ? axis_array[SECOND_Y_AXIS].tic_rotate : 0;
TBOOLEAN shift_labels_to_border = FALSE;
xticlin = ylablin = y2lablin = xlablin = x2lablin = titlelin = 0;
/*{{{ count lines in labels and tics */
if (title.text)
label_width(title.text, &titlelin);
if (axis_array[FIRST_X_AXIS].label.text)
label_width(axis_array[FIRST_X_AXIS].label.text, &xlablin);
/* This should go *inside* label_width(), but it messes up the key title */
/* Imperfect check for subscripts or superscripts */
if ((term->flags & TERM_ENHANCED_TEXT) && axis_array[FIRST_X_AXIS].label.text
&& strpbrk(axis_array[FIRST_X_AXIS].label.text, "_^"))
xlablin++;
if (axis_array[SECOND_X_AXIS].label.text)
label_width(axis_array[SECOND_X_AXIS].label.text, &x2lablin);
if (axis_array[FIRST_Y_AXIS].label.text)
label_width(axis_array[FIRST_Y_AXIS].label.text, &ylablin);
if (axis_array[SECOND_Y_AXIS].label.text)
label_width(axis_array[SECOND_Y_AXIS].label.text, &y2lablin);
if (axis_array[FIRST_X_AXIS].ticmode) {
label_width(axis_array[FIRST_X_AXIS].formatstring, &xticlin);
/* Reserve room for user tic labels even if format of autoticks is "" */
if (xticlin == 0 && axis_array[FIRST_X_AXIS].ticdef.def.user)
xticlin = 1;
}
if (axis_array[SECOND_X_AXIS].ticmode)
label_width(axis_array[SECOND_X_AXIS].formatstring, &x2ticlin);
if (axis_array[FIRST_Y_AXIS].ticmode)
label_width(axis_array[FIRST_Y_AXIS].formatstring, &yticlin);
if (axis_array[SECOND_Y_AXIS].ticmode)
label_width(axis_array[SECOND_Y_AXIS].formatstring, &y2ticlin);
/*}}} */
/*{{{ preliminary plot_bounds.ytop calculation */
/* first compute heights of things to be written in the margin */
/* Title placement has been reworked for 5.4
* NOTE: title_textheight is _not_ the height of the title!
* It is the amount of space reserved for the title above the plot.
* A negative offset greater than the number of title lines means
* that the title will appear inside the boundary and no extra space
* needs to be reserved for it above the plot.
*/
title_textheight = 0;
if (titlelin) {
title_textheight = t->v_char; /* Gap of one normal line height */
if (title.font)
t->set_font(title.font);
title_y = titlelin * t->v_char;
if ((titlelin + title.offset.y) > 0)
title_textheight += titlelin * t->v_char;
if (title.font)
t->set_font("");
title_y += 0.5 * t->v_char; /* Approximate same placement as version 5.2 */
}
/* Extra space at the top for spiderplot axis label */
if (spiderplot)
title_textheight += 1.5 * t->v_char;
/* x2label */
if (x2lablin) {
double tmpx, tmpy;
map_position_r(&(axis_array[SECOND_X_AXIS].label.offset),
&tmpx, &tmpy, "x2label");
if (axis_array[SECOND_X_AXIS].label.font)
t->set_font(axis_array[SECOND_X_AXIS].label.font);
x2label_textheight = (int) (x2lablin * t->v_char);
x2label_yoffset = tmpy;
if (axis_array[SECOND_X_AXIS].label.font)
t->set_font("");
} else
x2label_textheight = 0;
/* tic labels */
if (axis_array[SECOND_X_AXIS].ticmode & TICS_ON_BORDER) {
/* ought to consider tics on axes if axis near border */
x2tic_textheight = (int) (x2ticlin * t->v_char);
} else
x2tic_textheight = 0;
/* tics */
if (axis_array[SECOND_X_AXIS].ticmode & TICS_ON_BORDER) {
x2tic_height = t->v_tic * axis_array[SECOND_X_AXIS].ticscale;
if (axis_array[SECOND_X_AXIS].tic_in)
x2tic_height = -x2tic_height;
} else
x2tic_height = 0;
/* Polar (theta) tic labels need space at top and bottom of plot */
if (THETA_AXIS.ticmode) {
/* Really wants to be 5% of polar grid radius, but we don't know that yet */
ttic_textheight = 2. * t->v_char;
}
/* timestamp */
if (timelabel.text) {
int timelin;
timelabel_textwidth = label_width(timelabel.text, &timelin);
if (vertical_timelabel) {
timelabel_textheight = timelabel_textwidth * t->v_char;
timelabel_textwidth = (timelin + 1.5) * t->h_char;
timelabel.place.y = 0;
} else {
timelabel_textheight = timelin * t->v_char;
timelabel_textwidth = timelabel_textwidth * t->h_char;
/* save textheight for use in do_key_bounds() */
timelabel.place.y = timelabel_textheight;
}
}
/* ylabel placement */
if (axis_array[FIRST_Y_AXIS].label.text) {
if (can_rotate && axis_array[FIRST_Y_AXIS].label.rotate != 0) {
ylabel_textwidth = ylablin * t->v_char;
} else {
/* Trying to estimate this length caused more problems than it solved.
* For one thing it comes out wrong for text passed to TeX terminals.
* Assume the common case is roughly 3 character widths and let the
* user adjust lmargin and offset for longer non-rotated ylabels.
*/
ylabel_textwidth = 3 * t->h_char;
}
}
/* y2label placement */
if (axis_array[SECOND_Y_AXIS].label.text) {
if (can_rotate && axis_array[SECOND_Y_AXIS].label.rotate != 0) {
y2label_textwidth = y2lablin * t->v_char;
if (!axis_array[SECOND_Y_AXIS].ticmode)
y2label_textwidth += 0.5 * t->v_char;
} else {
/* See above. Estimating true text length causes problems */
y2label_textwidth = 3 * t->h_char;
}
}
/* compute plot_bounds.ytop from the various components
* unless tmargin is explicitly specified
*/
plot_bounds.ytop = (int) (0.5 + (ysize + yoffset) * (t->ymax-1));
/* Sanity check top and bottom margins, in case the user got confused */
if (bmargin.scalex == screen && tmargin.scalex == screen)
if (bmargin.x > tmargin.x) {
double tmp = bmargin.x;
bmargin.x = tmargin.x;
tmargin.x = tmp;
}
if (tmargin.scalex == screen) {
/* Specified as absolute position on the canvas */
plot_bounds.ytop = (tmargin.x) * (float)(t->ymax-1);
} else if (tmargin.x >=0) {
/* Specified in terms of character height */
plot_bounds.ytop -= (int)(tmargin.x * (float)t->v_char + 0.5);
} else {
/* Auto-calculation of space required */
int top_margin = title_textheight;
if (x2label_textheight + x2label_yoffset > 0)
top_margin += x2label_textheight;
if (timelabel_textheight > top_margin && !timelabel_bottom && !vertical_timelabel)
top_margin = timelabel_textheight;
top_margin += x2tic_textheight;
top_margin += t->v_char;
if (x2tic_height > 0)
top_margin += x2tic_height;
top_margin += ttic_textheight;
plot_bounds.ytop -= top_margin;
if (plot_bounds.ytop == (int)(0.5 + (ysize + yoffset) * (t->ymax-1))) {
/* make room for the end of rotated ytics or y2tics */
plot_bounds.ytop -= (int) (t->h_char * 2);
}
}
/* end of preliminary plot_bounds.ytop calculation }}} */
/*{{{ preliminary plot_bounds.xleft, needed for "under" */
if (lmargin.scalex == screen)
plot_bounds.xleft = lmargin.x * (float)t->xmax;
else
plot_bounds.xleft = xoffset * t->xmax
+ t->h_char * (lmargin.x >= 0 ? lmargin.x : 1);
/*}}} */
/*{{{ tentative plot_bounds.xright, needed for "under" */
if (rmargin.scalex == screen)
plot_bounds.xright = rmargin.x * (float)(t->xmax - 1);
else
plot_bounds.xright = (xsize + xoffset) * (t->xmax - 1)
- t->h_char * (rmargin.x >= 0 ? rmargin.x : 2);
/*}}} */
/*{{{ preliminary plot_bounds.ybot calculation
* first compute heights of labels and tics */
/* tic labels */
shift_labels_to_border = FALSE;
if (axis_array[FIRST_X_AXIS].ticmode & TICS_ON_AXIS) {
/* FIXME: This test for how close the axis is to the border does not match */
/* the tests in axis_output_tics(), and assumes FIRST_Y_AXIS. */
if (!inrange(0.0, axis_array[FIRST_Y_AXIS].min, axis_array[FIRST_Y_AXIS].max))
shift_labels_to_border = TRUE;
if (0.05 > fabs( axis_array[FIRST_Y_AXIS].min
/ (axis_array[FIRST_Y_AXIS].max - axis_array[FIRST_Y_AXIS].min)))
shift_labels_to_border = TRUE;
}
if ((axis_array[FIRST_X_AXIS].ticmode & TICS_ON_BORDER)
|| shift_labels_to_border) {
xtic_textheight = (int) (t->v_char * (xticlin + 1));
} else
xtic_textheight = 0;
/* tics */
if (!axis_array[FIRST_X_AXIS].tic_in
&& ((axis_array[FIRST_X_AXIS].ticmode & TICS_ON_BORDER)
|| ((axis_array[SECOND_X_AXIS].ticmode & TICS_MIRROR)
&& (axis_array[SECOND_X_AXIS].ticmode & TICS_ON_BORDER))))
xtic_height = (int) (t->v_tic * axis_array[FIRST_X_AXIS].ticscale);
else
xtic_height = 0;
/* xlabel */
if (xlablin) {
double tmpx, tmpy;
map_position_r(&(axis_array[FIRST_X_AXIS].label.offset),
&tmpx, &tmpy, "boundary");
/* offset is subtracted because if > 0, the margin is smaller */
/* textheight is inflated by 0.2 to allow descenders to clear bottom of canvas */
xlabel_textheight = (((float)xlablin + 0.2) * t->v_char - tmpy);
if (!axis_array[FIRST_X_AXIS].ticmode)
xlabel_textheight += 0.5 * t->v_char;
} else
xlabel_textheight = 0;
/* compute plot_bounds.ybot from the various components
* unless bmargin is explicitly specified */
plot_bounds.ybot = yoffset * (float)t->ymax;
if (bmargin.scalex == screen) {
/* Absolute position for bottom of plot */
plot_bounds.ybot = bmargin.x * (float)t->ymax;
} else if (bmargin.x >= 0) {
/* Position based on specified character height */
plot_bounds.ybot += bmargin.x * (float)t->v_char + 0.5;
} else {
plot_bounds.ybot += xtic_height + xtic_textheight;
if (xlabel_textheight > 0)
plot_bounds.ybot += xlabel_textheight;
if (!vertical_timelabel && timelabel_bottom && timelabel_textheight > 0)
plot_bounds.ybot += timelabel_textheight;
if (plot_bounds.ybot == (int) (t->ymax * yoffset)) {
/* make room for the end of rotated ytics or y2tics */
plot_bounds.ybot += (int) (t->h_char * 2);
}
if (spiderplot) /* Extra space at the bottom for spiderplot axis label */
plot_bounds.ybot += 2 * t->h_char;
/* Last chance for better estimate of space required for ttic labels */
/* It is too late to go back and adjust positions relative to ytop */
if (ttic_textheight > 0) {
ttic_textheight = 0.05 * (plot_bounds.ytop - plot_bounds.ybot);
plot_bounds.ybot += ttic_textheight;
}
}
/* end of preliminary plot_bounds.ybot calculation }}} */
/* Determine the size and position of the key box */
if (key->visible) {
/* Count max_len key and number keys with len > 0 */
max_ptitl_len = find_maxl_keys(plots, count, &ptitl_cnt);
do_key_layout(key);
}
/* Adjust range of dependent axes y and y2 */
if (nonlinear(&axis_array[FIRST_Y_AXIS]))
extend_primary_ticrange(&axis_array[FIRST_Y_AXIS]);
if (nonlinear(&axis_array[SECOND_Y_AXIS]))
extend_primary_ticrange(&axis_array[SECOND_Y_AXIS]);
setup_tics(&axis_array[FIRST_Y_AXIS], 20);
setup_tics(&axis_array[SECOND_Y_AXIS], 20);
/* Adjust color axis limits if necessary. */
if (is_plot_with_palette()) {
axis_checked_extend_empty_range(COLOR_AXIS, "All points of color axis undefined.");
if (color_box.where != SMCOLOR_BOX_NO)
setup_tics(&axis_array[COLOR_AXIS], 20);
}
/*{{{ recompute plot_bounds.xleft based on widths of ytics, ylabel etc
unless it has been explicitly set by lmargin */
/* tic labels */
shift_labels_to_border = FALSE;
if (axis_array[FIRST_Y_AXIS].ticmode & TICS_ON_AXIS) {
/* FIXME: This test for how close the axis is to the border does not match */
/* the tests in axis_output_tics(), and assumes FIRST_X_AXIS. */
if (!inrange(0.0, axis_array[FIRST_X_AXIS].min, axis_array[FIRST_X_AXIS].max))
shift_labels_to_border = TRUE;
if (0.1 > fabs( axis_array[FIRST_X_AXIS].min
/ (axis_array[FIRST_X_AXIS].max - axis_array[FIRST_X_AXIS].min)))
shift_labels_to_border = TRUE;
}
if ((axis_array[FIRST_Y_AXIS].ticmode & TICS_ON_BORDER)
|| shift_labels_to_border) {
if (vertical_ytics)
/* HBB: we will later add some white space as part of this, so
* reserve two more rows (one above, one below the text ...).
* Same will be done to similar calc.'s elsewhere */
ytic_textwidth = (int) (t->v_char * (yticlin + 2));
else {
widest_tic_strlen = 0; /* reset the global variable ... */
/* get gen_tics to call widest_tic_callback with all labels
* the latter sets widest_tic_strlen to the length of the widest
* one ought to consider tics on axis if axis near border...
*/
gen_tics(&axis_array[FIRST_Y_AXIS], widest_tic_callback);
ytic_textwidth = (int) (t->h_char * (widest_tic_strlen + 2));
}
} else if (axis_array[FIRST_Y_AXIS].label.text) {
/* substitutes for extra space added to left of ytix labels */
ytic_textwidth = 2 * (t->h_char);
} else {
ytic_textwidth = 0;
}
/* tics */
if (!axis_array[FIRST_Y_AXIS].tic_in
&& ((axis_array[FIRST_Y_AXIS].ticmode & TICS_ON_BORDER)
|| ((axis_array[SECOND_Y_AXIS].ticmode & TICS_MIRROR)
&& (axis_array[SECOND_Y_AXIS].ticmode & TICS_ON_BORDER))))
ytic_width = (int) (t->h_tic * axis_array[FIRST_Y_AXIS].ticscale);
else
ytic_width = 0;
if (lmargin.x < 0) {
/* Auto-calculation */
int space_to_left = key_xleft;
if (space_to_left < timelabel_textwidth && vertical_timelabel)
space_to_left = timelabel_textwidth;
if (space_to_left < ylabel_textwidth)
space_to_left = ylabel_textwidth;
plot_bounds.xleft = xoffset * t->xmax;
plot_bounds.xleft += space_to_left;
plot_bounds.xleft += ytic_width + ytic_textwidth;
if (plot_bounds.xleft - ytic_width - ytic_textwidth < 0)
plot_bounds.xleft = ytic_width + ytic_textwidth;
if (plot_bounds.xleft < t->xmax * xoffset + t->h_char * 2)
plot_bounds.xleft = t->xmax * xoffset + t->h_char * 2;
/* DBT 12-3-98 extra margin just in case */
plot_bounds.xleft += 0.5 * t->h_char;
}
/* Note: we took care of explicit 'set lmargin foo' at line 492 */
/* end of plot_bounds.xleft calculation }}} */
/*{{{ recompute plot_bounds.xright based on widest y2tic. y2labels, key "outside"
unless it has been explicitly set by rmargin */
/* tic labels */
if (axis_array[SECOND_Y_AXIS].ticmode & TICS_ON_BORDER) {
if (vertical_y2tics)
y2tic_textwidth = (int) (t->v_char * (y2ticlin + 2));
else {
widest_tic_strlen = 0; /* reset the global variable ... */
/* get gen_tics to call widest_tic_callback with all labels
* the latter sets widest_tic_strlen to the length of the widest
* one ought to consider tics on axis if axis near border...
*/
gen_tics(&axis_array[SECOND_Y_AXIS], widest_tic_callback);
y2tic_textwidth = (int) (t->h_char * (widest_tic_strlen + 2));
}
} else {
y2tic_textwidth = 0;
}
/* EAM May 2009
* Check to see if any xtic labels are so long that they extend beyond
* the right boundary of the plot. If so, allow extra room in the margin.
* If the labels are too long to fit even with a big margin, too bad.
*/
if (axis_array[FIRST_X_AXIS].ticdef.def.user) {
struct ticmark *tic = axis_array[FIRST_X_AXIS].ticdef.def.user;
int maxrightlabel = plot_bounds.xright;
/* We don't really know the plot layout yet, but try for an estimate */
axis_set_scale_and_range(&axis_array[FIRST_X_AXIS], plot_bounds.xleft, plot_bounds.xright);
while (tic) {
if (tic->label) {
double xx;
int length = estimate_strlen(tic->label, NULL)
* cos(DEG2RAD * (double)(axis_array[FIRST_X_AXIS].tic_rotate))
* term->h_char;
if (inrange(tic->position,
axis_array[FIRST_X_AXIS].set_min,
axis_array[FIRST_X_AXIS].set_max)) {
xx = axis_log_value_checked(FIRST_X_AXIS, tic->position, "xtic");
xx = map_x(xx);
xx += (axis_array[FIRST_X_AXIS].tic_rotate) ? length : length /2;
if (maxrightlabel < xx)
maxrightlabel = xx;
}
}
tic = tic->next;
}
xtic_textwidth = maxrightlabel - plot_bounds.xright;
if (xtic_textwidth > term->xmax/4) {
xtic_textwidth = term->xmax/4;
int_warn(NO_CARET, "difficulty making room for xtic labels");
}
}
/* tics */
if (!axis_array[SECOND_Y_AXIS].tic_in
&& ((axis_array[SECOND_Y_AXIS].ticmode & TICS_ON_BORDER)
|| ((axis_array[FIRST_Y_AXIS].ticmode & TICS_MIRROR)
&& (axis_array[FIRST_Y_AXIS].ticmode & TICS_ON_BORDER))))
y2tic_width = (int) (t->h_tic * axis_array[SECOND_Y_AXIS].ticscale);
else
y2tic_width = 0;
/* Make room for the color box if needed. */
if (rmargin.scalex != screen) {
if (is_plot_with_colorbox()) {
#define COLORBOX_SCALE 0.100
#define WIDEST_COLORBOX_TICTEXT 3
if ((color_box.where != SMCOLOR_BOX_NO) && (color_box.where != SMCOLOR_BOX_USER)) {
plot_bounds.xright -= (int) (plot_bounds.xright-plot_bounds.xleft)*COLORBOX_SCALE;
plot_bounds.xright -= (int) ((t->h_char) * WIDEST_COLORBOX_TICTEXT);
}
color_box.xoffset = 0;
}
if (rmargin.x < 0) {
color_box.xoffset = plot_bounds.xright;
plot_bounds.xright -= y2tic_width + y2tic_textwidth;
if (y2label_textwidth > 0)
plot_bounds.xright -= y2label_textwidth;
if (plot_bounds.xright > (xsize+xoffset)*(t->xmax-1) - (t->h_char * 2))
plot_bounds.xright = (xsize+xoffset)*(t->xmax-1) - (t->h_char * 2);
color_box.xoffset -= plot_bounds.xright;
/* EAM 2009 - protruding xtic labels */
if (term->xmax - plot_bounds.xright < xtic_textwidth)
plot_bounds.xright = term->xmax - xtic_textwidth;
/* DBT 12-3-98 extra margin just in case */
plot_bounds.xright -= 1.0 * t->h_char;
}
/* Note: we took care of explicit 'set rmargin foo' at line 502 */
}
/* end of plot_bounds.xright calculation }}} */
/* Set up x and x2 tics */
/* we should base the guide on the width of the xtics, but we cannot
* use widest_tics until tics are set up. Bit of a downer - let us
* assume tics are 5 characters wide
*/
setup_tics(&axis_array[FIRST_X_AXIS], 20);
setup_tics(&axis_array[SECOND_X_AXIS], 20);
/* Make sure that if polar grid is shown on a cartesian axis plot */
/* the rtics match up with the primary x tics. */
if (R_AXIS.ticmode && (polar || raxis)) {
if (bad_axis_range(&R_AXIS) || (!polar && R_AXIS.min != 0)) {
set_explicit_range(&R_AXIS, 0.0, X_AXIS.max);
R_AXIS.min = 0;
R_AXIS.max = axis_array[FIRST_X_AXIS].max;
int_warn(NO_CARET, "resetting rrange");
}
setup_tics(&axis_array[POLAR_AXIS], 10);
/* If setup_tics extends rrange to next tic then the previous
* x/y range limits from polar_range_fiddling are out of date.
*/
polar_range_fiddling(&axis_array[FIRST_X_AXIS], &axis_array[FIRST_Y_AXIS]);
}
/* Modify the bounding box to fit the aspect ratio, if any was given */
if (aspect_ratio != 0.0) {
double current_aspect_ratio;
double current, required;
if (aspect_ratio < 0 && (X_AXIS.max - X_AXIS.min) != 0.0) {
current_aspect_ratio = -aspect_ratio
* fabs((Y_AXIS.max - Y_AXIS.min) / (X_AXIS.max - X_AXIS.min));
} else
current_aspect_ratio = aspect_ratio;
if (current_aspect_ratio < 0.005 || current_aspect_ratio > 2000.0)
int_warn(NO_CARET, "extreme aspect ratio");
current = ((double) (plot_bounds.ytop - plot_bounds.ybot))
/ ((double) (plot_bounds.xright - plot_bounds.xleft));
required = (current_aspect_ratio * t->v_tic) / t->h_tic;
/* Fixed borders take precedence over centering */
if (current > required) {
/* too tall */
int old_height = plot_bounds.ytop - plot_bounds.ybot;
int new_height = required * (plot_bounds.xright - plot_bounds.xleft);
if (bmargin.scalex == screen)
plot_bounds.ytop = plot_bounds.ybot + new_height;
else if (tmargin.scalex == screen)
plot_bounds.ybot = plot_bounds.ytop - new_height;
else {
plot_bounds.ybot += (old_height - new_height) / 2;
plot_bounds.ytop -= (old_height - new_height) / 2;
}
} else {
/* too wide */
int old_width = plot_bounds.xright - plot_bounds.xleft;
int new_width = (plot_bounds.ytop - plot_bounds.ybot) / required;
if (lmargin.scalex == screen)
plot_bounds.xright = plot_bounds.xleft + new_width;
else if (rmargin.scalex == screen)
plot_bounds.xleft = plot_bounds.xright - new_width;
else {
plot_bounds.xleft += (old_width - new_width) / 2;
plot_bounds.xright -= (old_width - new_width) / 2;
}
}
}
/* Calculate space needed for tic label rotation.
* If [tb]margin is auto, move the plot boundary.
* Otherwise use textheight to adjust placement of various titles.
*/
if (axis_array[SECOND_X_AXIS].ticmode & TICS_ON_BORDER && vertical_x2tics) {
/* Assuming left justified tic labels. Correction below if they aren't */
double projection = sin((double)axis_array[SECOND_X_AXIS].tic_rotate*DEG2RAD);
if (axis_array[SECOND_X_AXIS].tic_pos == RIGHT)
projection *= -1;
else if (axis_array[SECOND_X_AXIS].tic_pos == CENTRE)
projection = 0.5*fabs(projection);
widest_tic_strlen = 0; /* reset the global variable ... */
gen_tics(&axis_array[SECOND_X_AXIS], widest_tic_callback);
if (tmargin.x < 0) /* Undo original estimate */
plot_bounds.ytop += x2tic_textheight;
/* Adjust spacing for rotation */
if (projection > 0.0)
x2tic_textheight += (int) (t->h_char * (widest_tic_strlen)) * projection;
if (tmargin.x < 0)
plot_bounds.ytop -= x2tic_textheight;
}
if (axis_array[FIRST_X_AXIS].ticmode & TICS_ON_BORDER && vertical_xtics) {
double projection;
/* This adjustment will happen again in axis_output_tics but we need it now */
if (axis_array[FIRST_X_AXIS].tic_rotate == TEXT_VERTICAL
&& !axis_array[FIRST_X_AXIS].manual_justify)
axis_array[FIRST_X_AXIS].tic_pos = RIGHT;
if (axis_array[FIRST_X_AXIS].tic_rotate == 90)
projection = -1.0;
else if (axis_array[FIRST_X_AXIS].tic_rotate == TEXT_VERTICAL)
projection = -1.0;
else
projection = -sin((double)axis_array[FIRST_X_AXIS].tic_rotate*DEG2RAD);
if (axis_array[FIRST_X_AXIS].tic_pos == RIGHT)
projection *= -1;
widest_tic_strlen = 0; /* reset the global variable ... */
gen_tics(&axis_array[FIRST_X_AXIS], widest_tic_callback);
if (bmargin.x < 0)
plot_bounds.ybot -= xtic_textheight;
if (projection > 0.0)
xtic_textheight = (int) (t->h_char * widest_tic_strlen) * projection
+ t->v_char;
if (bmargin.x < 0)
plot_bounds.ybot += xtic_textheight;
}
/*
* Notwithstanding all these fancy calculations,
* plot_bounds.ytop must always be above plot_bounds.ybot
*/
if (plot_bounds.ytop < plot_bounds.ybot) {
int i = plot_bounds.ytop;
plot_bounds.ytop = plot_bounds.ybot;
plot_bounds.ybot = i;
FPRINTF((stderr,"boundary: Big problems! plot_bounds.ybot > plot_bounds.ytop\n"));
}
/* compute coordinates for axis labels, title etc */
x2label_y = plot_bounds.ytop + x2label_textheight;
x2label_y += 0.5 * t->v_char;
if (x2label_textheight + x2label_yoffset >= 0) {
x2label_y += 1.5 * x2tic_textheight;
/* Adjust for the tics themselves */
if (x2tic_height > 0)
x2label_y += x2tic_height;
}
title_x = (plot_bounds.xleft + plot_bounds.xright) / 2;
/* title_y was previously set to the actual title height.
* Further corrections to this placement only if it is above the plot
*/
title_y += plot_bounds.ytop;
if (titlelin + title.offset.y > 0) {
title_y += x2tic_textheight;
title_y += ttic_textheight;
if (x2label_y + x2label_yoffset > plot_bounds.ytop)
title_y += x2label_textheight;
if (x2tic_height > 0)
title_y += x2tic_height;
}
/* Shift upward by 0.2 line to allow for descenders in xlabel text */
xlabel_y = plot_bounds.ybot - xtic_height - xtic_textheight - xlabel_textheight
+ ((float)xlablin+0.2) * t->v_char;
xlabel_y -= ttic_textheight;
ylabel_x = plot_bounds.xleft - ytic_width - ytic_textwidth;
ylabel_x -= ylabel_textwidth/2;
y2label_x = plot_bounds.xright + y2tic_width + y2tic_textwidth;
y2label_x += y2label_textwidth/2;
/* Nov 2016 - simplify placement of timestamp
* Stamp the same place on the page regardless of plot margins
*/
if (vertical_timelabel) {
time_x = 1.5 * term->h_char;
if (timelabel_bottom)
time_y = term->v_char;
else
time_y = term->ymax - term->v_char;
} else {
time_x = 1.0 * term->h_char;
if (timelabel_bottom)
time_y = timelabel_textheight - 0.5 * term->v_char;
else
time_y = term->ymax;
}
xtic_y = plot_bounds.ybot - xtic_height
- (int) (vertical_xtics ? t->h_char : t->v_char);
x2tic_y = plot_bounds.ytop + (x2tic_height > 0 ? x2tic_height : 0)
+ (vertical_x2tics ? (int) t->h_char : t->v_char);
ytic_x = plot_bounds.xleft - ytic_width
- (vertical_ytics
? (ytic_textwidth - (int) t->v_char)
: (int) t->h_char);
y2tic_x = plot_bounds.xright + y2tic_width
+ (int) (vertical_y2tics ? t->v_char : t->h_char);
/* restore text to horizontal [we tested rotation above] */
(void) (*t->text_angle) (0);
/* needed for map_position() below */
axis_set_scale_and_range(&axis_array[FIRST_X_AXIS], plot_bounds.xleft, plot_bounds.xright);
axis_set_scale_and_range(&axis_array[SECOND_X_AXIS], plot_bounds.xleft, plot_bounds.xright);
axis_set_scale_and_range(&axis_array[FIRST_Y_AXIS], plot_bounds.ybot, plot_bounds.ytop);
axis_set_scale_and_range(&axis_array[SECOND_Y_AXIS], plot_bounds.ybot, plot_bounds.ytop);
/* Calculate limiting bounds of the key */
do_key_bounds(key);
/* Set default clipping to the plot boundary */
clip_area = &plot_bounds;
/* Sanity checks */
if (plot_bounds.xright < plot_bounds.xleft
|| plot_bounds.ytop < plot_bounds.ybot)
int_warn(NO_CARET, "Terminal canvas area too small to hold plot."
"\n\t Check plot boundary and font sizes.");
}
/*}}} */
void
do_key_bounds(legend_key *key)
{
struct termentry *t = term;
double dx, dy;
key_height = key_title_height + key_title_extra
+ key_rows * key_entry_height + key->height_fix * key_entry_height;
if (key->user_width.x == 0)
key_width = key_col_wth * key_cols;
if (key->user_width.x > 0) {
if (key->user_width.scalex == screen)
key_width = key->user_width.x * (double)(t->xmax-1);
else if (key->user_width.scalex == graph)
key_width = key->user_width.x * (plot_bounds.xright - plot_bounds.xleft);
}
/* Key inside plot boundaries */
if (key->region == GPKEY_AUTO_INTERIOR_LRTBC
|| (key->region == GPKEY_AUTO_EXTERIOR_LRTBC && key->vpos == JUST_CENTRE && key->hpos == CENTRE)) {
if (key->vpos == JUST_TOP) {
key->bounds.ytop = plot_bounds.ytop - t->v_tic;
key->bounds.ybot = key->bounds.ytop - key_height;
} else if (key->vpos == JUST_BOT) {
key->bounds.ybot = plot_bounds.ybot + t->v_tic;
key->bounds.ytop = key->bounds.ybot + key_height;
} else /* (key->vpos == JUST_CENTRE) */ {
key->bounds.ybot = ((plot_bounds.ybot + plot_bounds.ytop) - key_height) / 2;
key->bounds.ytop = ((plot_bounds.ybot + plot_bounds.ytop) + key_height) / 2;
}
if (key->hpos == LEFT) {
key->bounds.xleft = plot_bounds.xleft + t->h_char;
key->bounds.xright = key->bounds.xleft + key_width;
} else if (key->hpos == RIGHT) {
key->bounds.xright = plot_bounds.xright - t->h_char;
key->bounds.xleft = key->bounds.xright - key_width;
} else /* (key->hpos == CENTER) */ {
key->bounds.xleft = ((plot_bounds.xright + plot_bounds.xleft) - key_width) / 2;
key->bounds.xright = ((plot_bounds.xright + plot_bounds.xleft) + key_width) / 2;
}
/* Key outside plot boundaries */
} else if (key->region == GPKEY_AUTO_EXTERIOR_LRTBC || key->region == GPKEY_AUTO_EXTERIOR_MARGIN) {
/* Vertical alignment */
if (key->margin == GPKEY_TMARGIN) {
/* align top first since tmargin may be manual */
key->bounds.ytop = (ysize + yoffset) * t->ymax - t->v_tic;
key->bounds.ybot = key->bounds.ytop - key_height;
} else if (key->margin == GPKEY_BMARGIN) {
/* align bottom first since bmargin may be manual */
key->bounds.ybot = yoffset * t->ymax + t->v_tic;
if (timelabel.rotate == 0 && timelabel_bottom && timelabel.place.y > 0)
key->bounds.ybot += (int)(timelabel.place.y);
key->bounds.ytop = key->bounds.ybot + key_height;
} else {
if (key->vpos == JUST_TOP) {
/* align top first since tmargin may be manual */
key->bounds.ytop = plot_bounds.ytop;
key->bounds.ybot = key->bounds.ytop - key_height;
} else if (key->vpos == JUST_CENTRE) {
key->bounds.ybot = ((plot_bounds.ybot + plot_bounds.ytop) - key_height) / 2;
key->bounds.ytop = ((plot_bounds.ybot + plot_bounds.ytop) + key_height) / 2;
} else {
/* align bottom first since bmargin may be manual */
key->bounds.ybot = plot_bounds.ybot;
key->bounds.ytop = key->bounds.ybot + key_height;
}
}
/* Horizontal alignment */
if (key->margin == GPKEY_LMARGIN) {
/* align left first since lmargin may be manual */
key->bounds.xleft = xoffset * t->xmax + t->h_char;
key->bounds.xright = key->bounds.xleft + key_width;
} else if (key->margin == GPKEY_RMARGIN) {
/* align right first since rmargin may be manual */
key->bounds.xright = (xsize + xoffset) * (t->xmax-1) - t->h_char;
key->bounds.xleft = key->bounds.xright - key_width;
} else {
if (key->hpos == LEFT) {
/* align left first since lmargin may be manual */
key->bounds.xleft = plot_bounds.xleft;
key->bounds.xright = key->bounds.xleft + key_width;
} else if (key->hpos == CENTRE) {
key->bounds.xleft = ((plot_bounds.xright + plot_bounds.xleft) - key_width) / 2;
key->bounds.xright = ((plot_bounds.xright + plot_bounds.xleft) + key_width) / 2;
} else {
/* align right first since rmargin may be manual */
key->bounds.xright = plot_bounds.xright;
key->bounds.xleft = key->bounds.xright - key_width;
}
}
/* Key at explicit position specified by user */
} else {
int x, y;
map_position(&key->user_pos, &x, &y, "key");
/* Here top, bottom, left, right refer to the alignment with respect to point. */
key->bounds.xleft = x;
if (key->hpos == CENTRE)
key->bounds.xleft -= key_width/2;
else if (key->hpos == RIGHT)
key->bounds.xleft -= key_width;
key->bounds.xright = key->bounds.xleft + key_width;
key->bounds.ytop = y;
if (key->vpos == JUST_CENTRE)
key->bounds.ytop += key_height/2;
else if (key->vpos == JUST_BOT)
key->bounds.ytop += key_height;
key->bounds.ybot = key->bounds.ytop - key_height;
}
/* Regardless of how the key was nominally positioned,
* the result can be manually tweaked by "set key offset dx, dy"
*/
map_position_r(&key->offset, &dx, &dy, "key");
key->bounds.ytop += dy;
key->bounds.ybot += dy;
key->bounds.xleft += dx;
key->bounds.xright += dx;
}
/* Calculate positioning of components that make up the key box */
void
do_key_layout(legend_key *key)
{
struct termentry *t = term;
TBOOLEAN key_panic = FALSE;
/* If there is a separate font for the key, use it for space calculations. */
if (key->font)
t->set_font(key->font);
/* Is it OK to initialize these here rather than in do_plot? */
key_count = 0;
key_xleft = 0;
xl = yl = 0;
if (key->swidth >= 0) {
key_sample_width = key->swidth * t->h_char + t->h_tic;
} else {
key_sample_width = 0;
}
key_sample_height = GPMAX( 1.25 * t->v_tic, t->v_char );
key_entry_height = key_sample_height * key->vert_factor;
/* HBB 20020122: safeguard to prevent division by zero later */
if (key_entry_height == 0)
key_entry_height = 1;
/* Key title length and height, adjusted for font size and markup */
key_title_height = 0;
key_title_extra = 0;