-
Notifications
You must be signed in to change notification settings - Fork 19
/
grid.cpp
189 lines (151 loc) · 4.88 KB
/
grid.cpp
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
#include "grid.h"
NumericVector unit_pt(NumericVector x) {
// create unit vector by calling back to R
Environment env = Environment::namespace_env("grid");
Function unit = env["unit"];
return unit(x, "pt");
}
NumericVector unit_pt(Length x) {
NumericVector out(1, x);
return unit_pt(out);
}
List gpar_empty() {
List out;
out.attr("class") = "gpar";
return out;
}
List text_grob(CharacterVector label, NumericVector x_pt, NumericVector y_pt, RObject gp, RObject name) {
if (label.size() != 1 || x_pt.size() != 1 || y_pt.size() != 1) {
stop("Function text_grob() is not vectorized.\n");
}
if (gp.isNULL()) {
gp = gpar_empty();
}
// need to produce a unique name for each grob, otherwise grid gets grumpy
static int tg_count = 0;
if (name.isNULL()) {
tg_count += 1;
string s("gridtext.text.");
s = s + to_string(tg_count);
CharacterVector vs;
vs.push_back(s);
name = vs;
}
List out = List::create(
_["label"] = label, _["x"] = unit_pt(x_pt), _["y"] = unit_pt(y_pt), _["just"] = "centre",
_["hjust"] = 0., _["vjust"] = 0., _["rot"] = 0.,
_["check.overlap"] = false, _["name"] = name, _["gp"] = gp, _["vp"] = R_NilValue
);
Rcpp::StringVector cl(3);
cl(0) = "text";
cl(1) = "grob";
cl(2) = "gDesc";
out.attr("class") = cl;
return out;
}
List raster_grob(RObject image, NumericVector x_pt, NumericVector y_pt, NumericVector width_pt, NumericVector height_pt,
LogicalVector interpolate, RObject gp, RObject name) {
if (x_pt.size() != 1 || y_pt.size() != 1 || width_pt.size() != 1 || height_pt.size() != 1) {
stop("Function raster_grob() is not vectorized.\n");
}
// need to produce a unique name for each grob, otherwise grid gets grumpy
static int tg_count = 0;
if (name.isNULL()) {
tg_count += 1;
string s("gridtext.raster.");
s = s + to_string(tg_count);
CharacterVector vs;
vs.push_back(s);
name = vs;
}
RObject raster = image;
if (!raster.inherits("nativeRaster")) {
// convert to raster by calling grDevices::as.raster()
Environment env = Environment::namespace_env("grDevices");
Function as_raster = env["as.raster"];
raster = as_raster(image);
}
List out = List::create(
_["raster"] = raster,
_["x"] = unit_pt(x_pt), _["y"] = unit_pt(y_pt),
_["width"] = unit_pt(width_pt), _["height"] = unit_pt(height_pt),
_["just"] = "centre", _["hjust"] = 0., _["vjust"] = 0.,
_["interpolate"] = interpolate,
_["name"] = name, _["gp"] = gp, _["vp"] = R_NilValue
);
Rcpp::StringVector cl(3);
cl(0) = "rastergrob";
cl(1) = "grob";
cl(2) = "gDesc";
out.attr("class") = cl;
return out;
}
List rect_grob(NumericVector x_pt, NumericVector y_pt, NumericVector width_pt, NumericVector height_pt,
RObject gp, RObject name) {
if (x_pt.size() != 1 || y_pt.size() != 1 || width_pt.size() != 1 || height_pt.size() != 1) {
stop("Function rect_grob() is not vectorized.\n");
}
if (gp.isNULL()) {
gp = gpar_empty();
}
// need to produce a unique name for each grob, otherwise grid gets grumpy
static int tg_count = 0;
if (name.isNULL()) {
tg_count += 1;
string s("gridtext.rect.");
s = s + to_string(tg_count);
CharacterVector vs;
vs.push_back(s);
name = vs;
}
List out = List::create(
_["x"] = unit_pt(x_pt), _["y"] = unit_pt(y_pt),
_["width"] = unit_pt(width_pt), _["height"] = unit_pt(height_pt),
_["just"] = "centre", _["hjust"] = 0., _["vjust"] = 0.,
_["name"] = name, _["gp"] = gp, _["vp"] = R_NilValue
);
Rcpp::StringVector cl(3);
cl(0) = "rect";
cl(1) = "grob";
cl(2) = "gDesc";
out.attr("class") = cl;
return out;
}
List roundrect_grob(NumericVector x_pt, NumericVector y_pt, NumericVector width_pt, NumericVector height_pt,
NumericVector r_pt, RObject gp, RObject name) {
if (x_pt.size() != 1 || y_pt.size() != 1 || width_pt.size() != 1 || height_pt.size() != 1 || r_pt.size() != 1) {
stop("Function roundrect_grob() is not vectorized.\n");
}
if (gp.isNULL()) {
gp = gpar_empty();
}
// need to produce a unique name for each grob, otherwise grid gets grumpy
static int tg_count = 0;
if (name.isNULL()) {
tg_count += 1;
string s("gridtext.roundrect.");
s = s + to_string(tg_count);
CharacterVector vs;
vs.push_back(s);
name = vs;
}
NumericVector justv(2); // c(0, 0)
List out = List::create(
_["x"] = unit_pt(x_pt), _["y"] = unit_pt(y_pt),
_["width"] = unit_pt(width_pt), _["height"] = unit_pt(height_pt),
_["r"] = unit_pt(r_pt),
_["just"] = justv,
_["name"] = name, _["gp"] = gp, _["vp"] = R_NilValue
);
Rcpp::StringVector cl(3);
cl(0) = "roundrect";
cl(1) = "grob";
cl(2) = "gDesc";
out.attr("class") = cl;
return out;
}
RObject set_grob_coords(RObject grob, NumericVector x, NumericVector y) {
as<List>(grob)["x"] = x;
as<List>(grob)["y"] = y;
return grob;
}