/
griddle.c
1609 lines (1358 loc) · 46.4 KB
/
griddle.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
/**
* \mainpage
*
* This project attempts to recreate the semantics of the grid graphics package
* for R as a C library using cairo.
*/
#include "griddle.h"
#include <math.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
/**
* NOTE: this function assumes device coordinates are pixels.
*/
static double
unit_to_npc_helper(double dev_per_npc, double dev_per_line, double dev_per_em,
double o_ntv, double size_ntv, const unit_t *u)
{
if (strcmp(u->type, "+") == 0) {
double x = unit_to_npc_helper(dev_per_npc, dev_per_line, dev_per_em,
o_ntv, size_ntv, u->arg1);
double y = unit_to_npc_helper(dev_per_npc, dev_per_line, dev_per_em,
o_ntv, size_ntv, u->arg2);
return x + y;
} else if (strcmp(u->type, "-") == 0) {
double x = unit_to_npc_helper(dev_per_npc, dev_per_line, dev_per_em,
o_ntv, size_ntv, u->arg1);
double y = unit_to_npc_helper(dev_per_npc, dev_per_line, dev_per_em,
o_ntv, size_ntv, u->arg2);
return x - y;
} else if (strcmp(u->type, "*") == 0) {
double x = unit_to_npc_helper(dev_per_npc, dev_per_line, dev_per_em,
o_ntv, size_ntv, u->arg1);
return x * u->value;
} else if (strcmp(u->type, "/") == 0) {
double x = unit_to_npc_helper(dev_per_npc, dev_per_line, dev_per_em,
o_ntv, size_ntv, u->arg1);
return x / u->value;
} else if (strcmp(u->type, "npc") == 0) {
return u->value;
} else if (strcmp(u->type, "px") == 0) {
return u->value / dev_per_npc;
} else if (strncmp(u->type, "line", 4) == 0) {
return u->value * dev_per_line / dev_per_npc;
} else if (strcmp(u->type, "em") == 0) {
return u->value * dev_per_em / dev_per_npc;
} else if (strcmp(u->type, "native") == 0) {
return (u->value - o_ntv) / size_ntv;
} else {
fprintf(stderr, "Warning: can't convert unit '%s' to npc\n", u->type);
return 0.0;
}
}
/**
* Convert a unit to a single NPC value.
*/
static double
unit_to_npc(grid_context_t *gr, char dim, const unit_t *u) {
grid_viewport_node_t *node = gr->current_node;
double dev_x_per_npc, dev_y_per_npc;
dev_x_per_npc = dev_y_per_npc = 1.0;
cairo_matrix_transform_distance(node->npc_to_dev, &dev_x_per_npc,
&dev_y_per_npc);
double x_ntv, y_ntv, w_ntv, h_ntv;
x_ntv = y_ntv = 0.0;
w_ntv = h_ntv = 1.0;
cairo_matrix_transform_point(node->npc_to_ntv, &x_ntv, &y_ntv);
cairo_matrix_transform_distance(node->npc_to_ntv, &w_ntv, &h_ntv);
cairo_font_extents_t font_extents;
cairo_font_extents(gr->cr, &font_extents);
cairo_text_extents_t em_extents;
cairo_text_extents(gr->cr, "m", &em_extents);
double dev_per_npc, o_ntv, size_ntv;
dev_per_npc = o_ntv = size_ntv = 0.0;
if (dim == 'x') {
dev_per_npc = dev_x_per_npc;
o_ntv = x_ntv;
size_ntv = w_ntv;
} else if (dim == 'y') {
dev_per_npc = dev_y_per_npc;
o_ntv = y_ntv;
size_ntv = h_ntv;
} else {
fprintf(stderr, "Warning: unknown dimension '%c'\n", dim);
}
double result = unit_to_npc_helper(dev_per_npc, font_extents.height,
em_extents.width, o_ntv, size_ntv, u);
return result;
}
/**
* NOTE: this function assumes device coordinates are pixels.
*/
static void
unit_array_to_npc_helper(double *result, double dev_per_npc,
double dev_per_line, double dev_per_em,
double o_ntv, double size_ntv,
int size, const unit_array_t *u)
{
int i;
double *xs, *ys;
if (strcmp(u->type, "+") == 0) {
xs = malloc(size * sizeof(double));
ys = malloc(size * sizeof(double));
unit_array_to_npc_helper(xs, dev_per_npc, dev_per_line, dev_per_em,
o_ntv, size_ntv, size, u->arg1);
unit_array_to_npc_helper(ys, dev_per_npc, dev_per_line, dev_per_em,
o_ntv, size_ntv, size, u->arg1);
for (i = 0; i < size; i++)
result[i] = xs[i] + ys[i];
free(xs);
free(ys);
} else if (strcmp(u->type, "-") == 0) {
xs = malloc(size * sizeof(double));
ys = malloc(size * sizeof(double));
unit_array_to_npc_helper(xs, dev_per_npc, dev_per_line, dev_per_em,
o_ntv, size_ntv, size, u->arg1);
unit_array_to_npc_helper(ys, dev_per_npc, dev_per_line, dev_per_em,
o_ntv, size_ntv, size, u->arg1);
for (i = 0; i < size; i++)
result[i] = xs[i] - ys[i];
free(xs);
free(ys);
} else if (strcmp(u->type, "*") == 0) {
xs = malloc(size * sizeof(double));
unit_array_to_npc_helper(xs, dev_per_npc, dev_per_line, dev_per_em,
o_ntv, size_ntv, size, u->arg1);
for (i = 0; i < size; i++)
result[i] = xs[i] * u->values[0];
free(xs);
} else if (strcmp(u->type, "/") == 0) {
xs = malloc(size * sizeof(double));
unit_array_to_npc_helper(xs, dev_per_npc, dev_per_line, dev_per_em,
o_ntv, size_ntv, size, u->arg1);
for (i = 0; i < size; i++)
result[i] = xs[i] * u->values[0];
free(xs);
} else if (strcmp(u->type, "npc") == 0) {
for (i = 0; i < size; i++)
result[i] = u->values[i];
} else if (strcmp(u->type, "px") == 0) {
for (i = 0; i < size; i++)
result[i] = u->values[i] / dev_per_npc;
} else if (strncmp(u->type, "line", 4) == 0) {
for (i = 0; i < size; i++)
result[i] = u->values[i] * dev_per_line / dev_per_npc;
} else if (strcmp(u->type, "em") == 0) {
for (i = 0; i < size; i++)
result[i] = u->values[i] * dev_per_em / dev_per_npc;
} else if (strcmp(u->type, "native") == 0) {
for (i = 0; i < size; i++)
result[i] = (u->values[i] - o_ntv) / size_ntv;
} else {
fprintf(stderr, "Warning: can't convert unit '%s' to npc\n", u->type);
for (i = 0; i < size; i++)
result[i] = 0.0;
}
}
/**
* Convert a unit array to a C array of doubles representing NPC values.
*/
static void
unit_array_to_npc(double *result, grid_context_t *gr, char dim,
const unit_array_t *u)
{
grid_viewport_node_t *node = gr->current_node;
double dev_x_per_npc, dev_y_per_npc;
dev_x_per_npc = dev_y_per_npc = 1.0;
cairo_matrix_transform_distance(node->npc_to_dev, &dev_x_per_npc,
&dev_y_per_npc);
double x_ntv, y_ntv, w_ntv, h_ntv;
x_ntv = y_ntv = 0.0;
w_ntv = h_ntv = 1.0;
cairo_matrix_transform_point(node->npc_to_ntv, &x_ntv, &y_ntv);
cairo_matrix_transform_distance(node->npc_to_ntv, &w_ntv, &h_ntv);
cairo_font_extents_t font_extents;
cairo_font_extents(gr->cr, &font_extents);
cairo_text_extents_t em_extents;
cairo_text_extents(gr->cr, "m", &em_extents);
double dev_per_npc, o_ntv, size_ntv;
dev_per_npc = o_ntv = size_ntv = 0.0;
if (dim == 'x') {
dev_per_npc = dev_x_per_npc;
o_ntv = x_ntv;
size_ntv = w_ntv;
} else if (dim == 'y') {
dev_per_npc = dev_y_per_npc;
o_ntv = y_ntv;
size_ntv = h_ntv;
} else {
fprintf(stderr, "Warning: unknown dimension '%c'\n", dim);
}
unit_array_to_npc_helper(result, dev_per_npc, font_extents.height,
em_extents.width, o_ntv, size_ntv,
unit_array_size(u), u);
}
//
// graphics parameters
//
/**
* Allocate an \ref rgba_t with the given red, green, and blue parameters.
* `alpha` is set to 1 (opacity).
*/
rgba_t*
rgb(double red, double green, double blue) {
rgba_t *color = malloc(sizeof(rgba_t));
color->red = red;
color->green = green;
color->blue = blue;
color->alpha = 1;
return color;
}
/**
* Allocate an \ref rgba_t with the given red, green, blue, and alpha parameters.
*/
rgba_t*
rgba(double red, double green, double blue, double alpha) {
rgba_t *color = malloc(sizeof(rgba_t));
color->red = red;
color->green = green;
color->blue = blue;
color->alpha = alpha;
return color;
}
/**
* Allocate a new parameter struct with parameters set to default values.
*/
grid_par_t*
new_grid_default_par(void) {
grid_par_t *par = malloc(sizeof(grid_par_t));
par->color = rgba(0, 0, 0, 1);
par->fill = NULL;
char *line_type = "solid";
par->line_type = malloc(strlen(line_type) + 1);
strcpy(par->line_type, line_type);
char *point_type = "round";
par->point_type = malloc(strlen(point_type) + 1);
strcpy(par->point_type, point_type);
char *just = "center";
par->just = malloc(strlen(just) + 1);
strcpy(par->just, just);
char *vjust = "top";
par->vjust = malloc(strlen(vjust) + 1);
strcpy(par->vjust, vjust);
par->line_width = unit(2, "px");
par->point_size = unit(4, "px");
par->font_size = unit(20, "px");
return par;
}
/**
* Deallocate a parameter struct.
*/
void
free_grid_par(grid_par_t *par) {
if (par->color)
free(par->color);
if (par->line_type)
free(par->line_type);
if (par->line_width)
free(par->line_width);
free(par);
}
//
// viewports
//
/**
* Allocate a new \ref grid_viewport_t. By default, the native scale is NPC.
*
* \param x The x coordinate of the center of the new viewport.
* \param y The y coordinate of the center of the new viewport.
* \param width The width of the new viewport.
* \param height The height of the new viewport.
* \return A pointer to the newly allocated \ref grid_viewport_t.
*/
grid_viewport_t*
new_grid_viewport(unit_t *x, unit_t *y, unit_t *width, unit_t *height) {
grid_viewport_t *vp = malloc(sizeof(grid_viewport_t));
vp->x = x;
vp->y = y;
vp->w = width;
vp->h = height;
vp->has_ntv = false;
return vp;
}
/**
* Allocate a new \ref grid_viewport_t with default values. `x` and `y` are set
* to 0 and `width` and `height` are set to 1. All units are npc.
*
* \return A pointer to the newly allocated \ref grid_viewport_t.
*/
grid_viewport_t*
new_grid_default_viewport() {
return new_grid_viewport(unit(0, "npc"), unit(0, "npc"),
unit(1, "npc"), unit(1, "npc"));
}
/**
* Allocate a new \ref grid_viewport_t whose native coordinate system is
* calculated from the given data.
*/
grid_viewport_t*
new_grid_data_viewport(int data_size, const double *xs, const double *ys) {
grid_viewport_t *vp = new_grid_default_viewport();
vp->has_ntv = true;
if (data_size > 0) {
double min, max, pad;
min = max = xs[0];
int i;
for (i = 1; i < data_size; i++) {
if (xs[i] > max)
max = xs[i];
else if (xs[i] < min)
min = xs[i];
}
pad = 0.1 * (max - min) / 1.8;
vp->x_ntv = min - pad;
vp->w_ntv = max - min + 2 * pad;
min = max = ys[0];
for (i = 1; i < data_size; i++) {
if (ys[i] > max)
max = ys[i];
else if (ys[i] < min)
min = ys[i];
}
pad = 0.1 * (max - min) / 1.8;
vp->y_ntv = min - pad;
vp->h_ntv = max - min + 2 * pad;
}
return vp;
}
/**
* Allocate a \ref grid_viewport_t with margins given by the arguments. Argument
* values are interpreted as number of lines in the current font.
*/
grid_viewport_t*
new_grid_plot_viewport(grid_context_t *gr,
double top, double right, double bottom, double left)
{
unit_t *x = unit(left, "lines");
unit_t *y = unit(bottom, "lines");
unit_t *width = unit_sub(unit(1, "npc"),
unit_add(unit(left, "lines"), unit(right, "lines")));
unit_t *height = unit_sub(unit(1, "npc"),
unit_add(unit(top, "lines"), unit(bottom, "lines")));
return new_grid_viewport(x, y, width, height);
}
/**
* Deallocate a \ref grid_viewport_t.
*
* \param vp A viewport.
*/
void
free_grid_viewport(grid_viewport_t *vp) {
if (vp->x)
free_unit(vp->x);
if (vp->y)
free_unit(vp->y);
if (vp->w)
free_unit(vp->w);
if (vp->h)
free_unit(vp->h);
free(vp);
}
/**
* Allocate a new \ref grid_viewport_node_t.
*/
static grid_viewport_node_t*
new_grid_viewport_node(void) {
grid_viewport_node_t *node = malloc(sizeof(grid_viewport_node_t));
node->parent = node->gege = node->didi = node->child = NULL;
node->name = NULL;
node->npc_to_dev = malloc(sizeof(cairo_matrix_t));
node->npc_to_ntv = malloc(sizeof(cairo_matrix_t));
node->par = NULL;
cairo_matrix_init_identity(node->npc_to_ntv);
cairo_matrix_init_identity(node->npc_to_dev);
return node;
}
/**
* Deallocate a \ref grid_viewport_node_t.
*/
static void
free_grid_viewport_node(grid_viewport_node_t *node) {
if (node->npc_to_ntv)
free(node->npc_to_ntv);
if (node->npc_to_dev)
free(node->npc_to_dev);
if (node->name)
free(node->name);
if (node->par)
free(node->par);
free(node);
}
void
grid_push_named_viewport(grid_context_t *gr,
const char *name, const grid_viewport_t *vp)
{
double x = unit_to_npc(gr, 'x', vp->x);
double y = unit_to_npc(gr, 'y', vp->y);
double w = unit_to_npc(gr, 'x', vp->w);
double h = unit_to_npc(gr, 'y', vp->h);
cairo_matrix_t vp_mtx, temp_mtx;
cairo_matrix_init(&vp_mtx, w, 0, 0, h, x, y);
cairo_matrix_init(&temp_mtx, w, 0, 0, h, x, y);
cairo_status_t status = cairo_matrix_invert(&temp_mtx);
if (status == CAIRO_STATUS_SUCCESS) {
grid_viewport_node_t *node = new_grid_viewport_node();
cairo_matrix_multiply(node->npc_to_dev, &vp_mtx, gr->current_node->npc_to_dev);
if (vp->has_ntv) {
cairo_matrix_init(node->npc_to_ntv, vp->w_ntv, 0, 0, vp->h_ntv,
vp->x_ntv, vp->y_ntv);
} else {
// inherit native coordinates from parent
cairo_matrix_multiply(node->npc_to_ntv,
&vp_mtx, gr->current_node->npc_to_ntv);
}
if (name) {
node->name = malloc(strlen(name) + 1);
strcpy(node->name, name);
}
if (gr->current_node->child) {
gr->current_node->child->didi = node;
node->gege = gr->current_node->child;
}
gr->current_node->child = node;
node->parent = gr->current_node;
gr->current_node = node;
} else {
fprintf(stderr, "Warning: can't create singular viewport\n");
}
}
/**
* Push a viewport onto the tree. The viewport becomes a leaf of the current
* viewport and becomes the new current viewport. This function acts
* destructively on `vp->x`, `vp->y`, `vp->w`, and `vp->h`.
*/
void
grid_push_viewport(grid_context_t *gr, const grid_viewport_t *vp) {
grid_push_named_viewport(gr, NULL, vp);
}
/**
* Pop and deallocate the current viewport node from the tree; its parent
* becomes the new current viewport.
*
* \return True if successful, false otherwise.
*/
bool
grid_pop_viewport_1(grid_context_t *gr) {
if (gr->current_node == gr->root_node) {
fprintf(stderr, "Warning: attempted to pop root viewport from the stack.\n");
return false;
} else {
grid_viewport_node_t *node = gr->current_node;
gr->current_node = node->parent;
if (node->gege) {
if (node->didi) {
node->didi->gege = node->gege;
node->gege->didi = node->didi;
node->didi = node->gege = NULL;
} else {
node->parent->child = node->gege;
node->gege->didi = NULL;
node->gege = NULL;
}
}
free_grid_viewport_node(node);
return true;
}
}
/**
* Pop `n` viewports from the tree. The new current viewport is the parent of
* the last popped viewport. If there are fewer than `n + 1` levels in the
* tree, then an error message is printed and the current viepwort is set to the
* root viewport. Popped viewports and the nodes that contain them are not
* deallocated.
*
* \return The number of viewport successfully popped.
*/
int
grid_pop_viewport(grid_context_t *gr, int n) {
int i;
for (i = 0; i < n; i++) {
if (!grid_pop_viewport_1(gr))
// grid_up_viewport_1 will print an error message
break;
}
return i;
}
/**
* Move up one viewport in the viewport tree. Prints a warning and does nothing
* if the current viewport is the root viewport.
*
* \return `true` if successful, `false` if the current viewport is the root
* viewport.
*/
bool
grid_up_viewport_1(grid_context_t *gr) {
if (gr->current_node == gr->root_node) {
fprintf(stderr, "Warning: attempted to move up from root viewport\n");
return false;
}
gr->current_node = gr->current_node->parent;
return true;
}
/**
* Move up `n` viewports in the viewport tree. Prints a warning if you try to
* move beyond the root viewport.
*
* \return The number of viewports traversed.
*/
int
grid_up_viewport(grid_context_t *gr, int n) {
int i;
for (i = 0; i < n; i++) {
if (!grid_up_viewport_1(gr))
// grid_up_viewport_1 will print an error message
break;
}
return i;
}
/**
* Perform a depth first search for a viewport with the given name. `*plevel`
* is incremented each time the algorithm recurses on a child viewport and is
* set to -1 if no matching viewport is found. `path` is a list of nodes
* traversed to find the target. If the return value is NULL, then the value of
* `*path` is undefined and `*plevel` is set to -1.
*/
static grid_viewport_node_t*
grid_viewport_dfs(grid_viewport_node_t *this, const char *name, int *plevel)
{
if (this->name && strcmp(this->name, name) == 0) {
return this;
}
grid_viewport_node_t *node;
grid_viewport_node_t *this_child = this->child;
int temp_level;
while (this_child != NULL) {
temp_level = *plevel + 1;
node = grid_viewport_dfs(this_child, name, &temp_level);
if (node) {
*plevel = temp_level;
return node;
} else {
this_child = this_child->gege;
}
}
*plevel = -1;
return NULL;
}
/**
* Move down the tree to the viewport with the given name. Prints a warning if
* no viewport is found. If the name matches the name of the current node, then
* the current viewport doesn't change.
*
* \return The number of levels traversed, -1 if no viewport found.
*/
int
grid_down_viewport(grid_context_t *gr, const char *name) {
int n = 0;
grid_viewport_node_t *node = grid_viewport_dfs(gr->current_node, name, &n);
if (node)
gr->current_node = node;
else
fprintf(stderr, "Warning: didn't find viewport with name '%s'\n", name);
return n;
}
/**
* Perform a depth-first search for a viewport named `name` in the viewport
* tree, starting from the root. Prints a warning if no viewport is found.
*
* \return The new level in the viewport tree, -1 if no viewport found.
*/
int
grid_seek_viewport(grid_context_t *gr, const char *name) {
int level = 0;
grid_viewport_node_t *node = grid_viewport_dfs(gr->root_node, name, &level);
if (node)
gr->current_node = node;
else
fprintf(stderr, "Warning: didn't find viewport with name '%s'\n", name);
return level;
}
//
// draw functions
//
static void
grid_apply_color(grid_context_t *gr, const rgba_t *color) {
cairo_set_source_rgba(gr->cr, color->red, color->green, color->blue, color->alpha);
}
/**
* Set the global (foreground) color.
*
* \return The old color.
*/
rgba_t*
grid_set_color(grid_context_t *gr, rgba_t *color) {
rgba_t *old = gr->par->color;
gr->par->color = color;
grid_apply_color(gr, color);
return old;
}
/**
* Set the global fill color.
*
* \return The old color.
*/
rgba_t*
grid_set_fill(grid_context_t *gr, rgba_t *fill) {
rgba_t *old = gr->par->color;
gr->par->fill = fill;
return old;
}
static double *grid_dash_pattern1_px = (double[]){10, 5};
static int grid_dash_pattern1_len = 2;
static double *grid_dash_pattern2_px = (double[]){3, 4};
static int grid_dash_pattern2_len = 2;
static double *grid_dash_pattern3_px = (double[]){3, 5, 10, 5};
static int grid_dash_pattern3_len = 4;
static void
grid_apply_line_type(grid_context_t *gr, const char *line_type) {
double *dash_pattern_px, *dash_pattern_dev;
dash_pattern_px = dash_pattern_dev = NULL;
int dash_pattern_len = 0;
if (strcmp(line_type, "solid") == 0) {
// do nothing (and don't print a warning)
} else if (strcmp(line_type, "dotdash") == 0) {
dash_pattern_px = grid_dash_pattern3_px;
dash_pattern_len = grid_dash_pattern3_len;
} else if (strncmp(line_type, "dash", 4) == 0) {
dash_pattern_px = grid_dash_pattern1_px;
dash_pattern_len = grid_dash_pattern1_len;
} else if (strncmp(line_type, "dot", 3) == 0) {
dash_pattern_px = grid_dash_pattern2_px;
dash_pattern_len = grid_dash_pattern2_len;
} else {
fprintf(stderr, "Unknown line type: '%s'\n", line_type);
}
if (dash_pattern_px) {
dash_pattern_dev = malloc(dash_pattern_len * sizeof(double));
int i;
double temp;
unit_t this_unit;
for (i = 0; i < dash_pattern_len; i++) {
this_unit = Unit(dash_pattern_px[i], "px");
dash_pattern_dev[i] = unit_to_npc(gr, 'x', &this_unit);
temp = 0;
cairo_matrix_transform_distance(gr->current_node->npc_to_dev,
dash_pattern_dev + i, &temp);
}
}
cairo_set_dash(gr->cr, dash_pattern_dev, dash_pattern_len, 0);
if (dash_pattern_dev)
free(dash_pattern_dev);
}
/**
* Set the global line type.
*/
char*
grid_set_line_type(grid_context_t *gr, char *line_type) {
char *old = gr->par->line_type;
gr->par->line_type = line_type;
grid_apply_line_type(gr, line_type);
return old;
}
/**
* Set the global point type.
*/
char*
grid_set_point_type(grid_context_t *gr, char *pty) {
char *old = gr->par->point_type;
gr->par->point_type = pty;
return old;
}
/**
* Set the global horizontal justification.
*/
char*
grid_set_just(grid_context_t *gr, char *just) {
char *old = gr->par->just;
gr->par->just = just;
return old;
}
/**
* Set the global vertical justification.
*/
char*
grid_set_vjust(grid_context_t *gr, char *vjust) {
char *old = gr->par->vjust;
gr->par->vjust = vjust;
return old;
}
static void
grid_apply_line_width(grid_context_t *gr, const unit_t *lwd) {
double lwd_npc = unit_to_npc(gr, 'x', lwd);
double temp = 0;
cairo_matrix_transform_distance(gr->current_node->npc_to_dev, &lwd_npc, &temp);
cairo_set_line_width(gr->cr, lwd_npc);
}
/**
* Set the global line width to the given value.
*
* \return The old line width.
*/
unit_t*
grid_set_line_width(grid_context_t *gr, unit_t *lwd) {
unit_t *old = gr->par->line_width;
gr->par->line_width = lwd;
grid_apply_line_width(gr, lwd);
return old;
}
/**
* Set the global point size to the given value.
*
* \return The old font size.
*/
unit_t*
grid_set_point_size(grid_context_t *gr, unit_t *point_size) {
unit_t *old = gr->par->point_size;
gr->par->point_size = point_size;
return old;
}
static void
grid_apply_font_size(grid_context_t *gr, const unit_t *font_size) {
double x_npc = unit_to_npc(gr, 'x', font_size);
double temp = 0;
cairo_matrix_transform_distance(gr->current_node->npc_to_dev, &x_npc, &temp);
cairo_set_font_size(gr->cr, x_npc);
}
/**
* Set the global font size to the given value.
*
* \return The old font size.
*/
unit_t*
grid_set_font_size(grid_context_t *gr, unit_t *font_size) {
unit_t *old = gr->par->font_size;
gr->par->font_size = font_size;
grid_apply_font_size(gr, font_size);
return old;
}
#define Parameter(F,A,B,C) (A && A->F ? A->F : B && B->F ? B->F : C->F)
/**
* Attempt to set parameters first from the passed \ref grid_par_t, then from
* the current node parameters, and finally from the global parameters. Sets the
* cairo source color to the foreground color.
*/
static void
grid_apply_parameters(grid_context_t *gr, const grid_par_t *par) {
rgba_t *col;
char *s;
unit_t *u;
grid_par_t *cur = gr->current_node->par;
grid_par_t *def = gr->par;
col = Parameter(color, par, cur, def);
grid_apply_color(gr, col);
s = Parameter(line_type, par, cur, def);
grid_apply_line_type(gr, s);
u = Parameter(line_width, par, cur, def);
grid_apply_line_width(gr, u);
u = Parameter(font_size, par, cur, def);
grid_apply_font_size(gr, u);
}
/**
* For any non-null fields in `par`, set the corresponding value to the current
* node parameters where applicable, and the global defaults otherwise.
*/
static void
grid_restore_parameters(grid_context_t *gr, const grid_par_t *par) {
if (par) {
rgba_t *col;
char *s;
unit_t *u;
grid_par_t *nil = NULL;
grid_par_t *cur = gr->current_node->par;
grid_par_t *def = gr->par;
if (par->color) {
col = Parameter(color, nil, cur, def);
grid_apply_color(gr, col);
}
if (par->line_type) {
s = Parameter(line_type, nil, cur, def);
grid_apply_line_type(gr, s);
}
if (par->line_width) {
u = Parameter(line_width, nil, cur, def);
grid_apply_line_width(gr, u);
}
if (par->font_size) {
u = Parameter(font_size, nil, cur, def);
grid_apply_font_size(gr, u);
}
}
}
/**
* Allocate a new grid context. The grid context contains a reference to a cairo
* image surface with the given width and height that it can draw to.
*
* \param width_px The width of the underlying image in pixels.
* \param height_px The height of the underlying image in pixels.
* \return A pointer to the newly allocated \ref grid_context_t.
*/
grid_context_t*
new_grid_context(int width_px, int height_px) {
grid_context_t *gr = malloc(sizeof(grid_context_t));
gr->surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, width_px, height_px);
gr->cr = cairo_create(gr->surface);
// put the origin at the lower left instead of the upper left
cairo_matrix_t m = { .xx = 1, .yy = -1, .y0 = height_px };
cairo_set_matrix(gr->cr, &m);
grid_viewport_node_t *root = new_grid_viewport_node();
root->name = malloc(sizeof(char) * 5);
strcpy(root->name, "root");
cairo_matrix_scale(root->npc_to_ntv, width_px, height_px);
cairo_matrix_scale(root->npc_to_dev, width_px, height_px);
gr->current_node = gr->root_node = root;
grid_par_t *par = new_grid_default_par();
gr->par = par;
grid_apply_parameters(gr, NULL);
return gr;
}
/**
* Recursively deallocate a viewport tree and referenced viewports. The
* implementation assumes the top-level root node does not have any siblings.
*
* \todo Current implementation is incomplete, it should free parameters.
*/
void
free_grid_viewport_tree(grid_viewport_node_t *root) {
if (root->gege)
free_grid_viewport_tree(root->gege);
if (root->child)
free_grid_viewport_tree(root->child);
free_grid_viewport_node(root);
}
/**
* Deallocate a \ref grid_context_t.
*/
void
free_grid_context(grid_context_t *gr) {
free_grid_viewport_tree(gr->root_node);
cairo_destroy(gr->cr);
cairo_surface_destroy(gr->surface);
free(gr);
}
/**
* Draw a line connecting two points.
*/
void
grid_line(grid_context_t *gr, const unit_t *x1, const unit_t *y1,
const unit_t *x2, const unit_t *y2, const grid_par_t *par)
{
grid_apply_parameters(gr, par);
cairo_t *cr = gr->cr;
double x1_npc = unit_to_npc(gr, 'x', x1);
double y1_npc = unit_to_npc(gr, 'y', y1);