-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspec_yaml.cpp
More file actions
363 lines (334 loc) · 13 KB
/
spec_yaml.cpp
File metadata and controls
363 lines (334 loc) · 13 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
#include "gnuplotpp/spec_yaml.hpp"
#include <algorithm>
#include <cctype>
#include <string>
#include "gnuplotpp/presets.hpp"
#include <yaml-cpp/yaml.h>
namespace gnuplotpp {
namespace {
std::string trim(std::string s) {
s.erase(s.begin(),
std::find_if(s.begin(), s.end(), [](unsigned char c) { return !std::isspace(c); }));
s.erase(std::find_if(s.rbegin(), s.rend(), [](unsigned char c) { return !std::isspace(c); }).base(),
s.end());
return s;
}
std::string lower(std::string s) {
std::transform(s.begin(), s.end(), s.begin(), [](unsigned char c) {
return static_cast<char>(std::tolower(c));
});
return s;
}
std::string normalize_token(std::string s) {
s = lower(trim(std::move(s)));
for (char& c : s) {
if (c == '-' || c == ' ') {
c = '_';
}
}
return s;
}
bool parse_bool(const std::string& v) {
const auto l = lower(trim(v));
return l == "true" || l == "1" || l == "yes" || l == "on";
}
OutputFormat parse_format(const std::string& s) {
const auto l = lower(trim(s));
if (l == "pdf") return OutputFormat::Pdf;
if (l == "svg") return OutputFormat::Svg;
if (l == "eps") return OutputFormat::Eps;
if (l == "png") return OutputFormat::Png;
throw std::invalid_argument("unsupported output format: " + s);
}
Preset parse_preset(const std::string& s) {
const auto l = lower(trim(s));
if (l == "ieee_singlecolumn") return Preset::IEEE_SingleColumn;
if (l == "ieee_doublecolumn") return Preset::IEEE_DoubleColumn;
if (l == "ieee_tran") return Preset::IEEE_Tran;
if (l == "nature_1col") return Preset::Nature_1Col;
if (l == "elsevier_1col") return Preset::Elsevier_1Col;
if (l == "aiaa_column") return Preset::AIAA_Column;
if (l == "aiaa_page") return Preset::AIAA_Page;
if (l == "custom") return Preset::Custom;
throw std::invalid_argument("unsupported preset: " + s);
}
ColorPalette parse_palette(const std::string& s) {
const auto l = lower(trim(s));
if (l == "default") return ColorPalette::Default;
if (l == "tab10") return ColorPalette::Tab10;
if (l == "viridis") return ColorPalette::Viridis;
if (l == "grayscale") return ColorPalette::Grayscale;
throw std::invalid_argument("unsupported palette: " + s);
}
LegendPosition parse_legend_position(const std::string& s) {
const auto l = lower(trim(s));
if (l == "topright" || l == "top_right") return LegendPosition::TopRight;
if (l == "topleft" || l == "top_left") return LegendPosition::TopLeft;
if (l == "bottomright" || l == "bottom_right") return LegendPosition::BottomRight;
if (l == "bottomleft" || l == "bottom_left") return LegendPosition::BottomLeft;
if (l == "outsideright" || l == "outside_right") return LegendPosition::OutsideRight;
if (l == "outsidebottom" || l == "outside_bottom") return LegendPosition::OutsideBottom;
throw std::invalid_argument("unsupported legend position: " + s);
}
ColorMap parse_colormap(const std::string& s) {
const auto l = lower(trim(s));
if (l == "viridis") return ColorMap::Viridis;
if (l == "cividis") return ColorMap::Cividis;
if (l == "turbo") return ColorMap::Turbo;
if (l == "magma") return ColorMap::Magma;
if (l == "coolwarm" || l == "cool_warm") return ColorMap::CoolWarm;
if (l == "gray" || l == "grayscale") return ColorMap::Gray;
throw std::invalid_argument("unsupported color map: " + s);
}
ColorNorm parse_color_norm(const std::string& s) {
const auto l = lower(trim(s));
if (l == "linear") return ColorNorm::Linear;
if (l == "log" || l == "logarithmic") return ColorNorm::Log;
throw std::invalid_argument("unsupported color norm: " + s);
}
StyleProfile parse_style_profile(const std::string& s) {
const auto l = normalize_token(s);
if (l == "science") return StyleProfile::Science;
if (l == "ieee_strict") return StyleProfile::IEEE_Strict;
if (l == "aiaa_strict") return StyleProfile::AIAA_Strict;
if (l == "presentation") return StyleProfile::Presentation;
if (l == "darkprintsafe" || l == "dark_print_safe") return StyleProfile::DarkPrintSafe;
if (l == "tufte_minimal") return StyleProfile::Tufte_Minimal;
throw std::invalid_argument("unsupported style profile: " + s);
}
bool parse_figure_key(FigureSpec& figure, const std::string& key, const std::string& value) {
if (key == "title") {
figure.title = value;
} else if (key == "caption") {
figure.caption = value;
} else if (key == "preset") {
figure.preset = parse_preset(value);
} else if (key == "palette") {
figure.palette = parse_palette(value);
} else if (key == "rows") {
figure.rows = std::max(1, std::stoi(value));
} else if (key == "cols") {
figure.cols = std::max(1, std::stoi(value));
} else if (key == "panel_labels") {
figure.panel_labels = parse_bool(value);
} else if (key == "auto_layout") {
figure.auto_layout = parse_bool(value);
} else if (key == "interactive_preview") {
figure.interactive_preview = parse_bool(value);
} else if (key == "font") {
figure.style.font = value;
} else if (key == "font_pt") {
figure.style.font_pt = std::stod(value);
} else if (key == "profile") {
apply_style_profile(figure, parse_style_profile(value));
} else {
return false;
}
return true;
}
bool parse_axis_key(AxesSpec& axis, const std::string& axis_key, const std::string& value) {
if (axis_key == "title") axis.title = value;
else if (axis_key == "xlabel") axis.xlabel = value;
else if (axis_key == "ylabel") axis.ylabel = value;
else if (axis_key == "y2label") axis.y2label = value;
else if (axis_key == "grid") axis.grid = parse_bool(value);
else if (axis_key == "legend") axis.legend = parse_bool(value);
else if (axis_key == "enable_crosshair") axis.enable_crosshair = parse_bool(value);
else if (axis_key == "xlog") axis.xlog = parse_bool(value);
else if (axis_key == "ylog") axis.ylog = parse_bool(value);
else if (axis_key == "y2log") axis.y2log = parse_bool(value);
else if (axis_key == "xmin") {
axis.has_xlim = true;
axis.xmin = std::stod(value);
} else if (axis_key == "xmax") {
axis.has_xlim = true;
axis.xmax = std::stod(value);
} else if (axis_key == "ymin") {
axis.has_ylim = true;
axis.ymin = std::stod(value);
} else if (axis_key == "ymax") {
axis.has_ylim = true;
axis.ymax = std::stod(value);
} else if (axis_key == "y2min") {
axis.has_y2lim = true;
axis.y2min = std::stod(value);
} else if (axis_key == "y2max") {
axis.has_y2lim = true;
axis.y2max = std::stod(value);
} else if (axis_key == "has_xtick_step") {
axis.has_xtick_step = parse_bool(value);
} else if (axis_key == "xtick_step") {
axis.has_xtick_step = true;
axis.xtick_step = std::stod(value);
} else if (axis_key == "has_ytick_step") {
axis.has_ytick_step = parse_bool(value);
} else if (axis_key == "ytick_step") {
axis.has_ytick_step = true;
axis.ytick_step = std::stod(value);
} else if (axis_key == "has_xminor_count") {
axis.has_xminor_count = parse_bool(value);
} else if (axis_key == "xminor_count") {
axis.has_xminor_count = true;
axis.xminor_count = std::stoi(value);
} else if (axis_key == "has_yminor_count") {
axis.has_yminor_count = parse_bool(value);
} else if (axis_key == "yminor_count") {
axis.has_yminor_count = true;
axis.yminor_count = std::stoi(value);
} else if (axis_key == "xformat") {
axis.xformat = value;
} else if (axis_key == "yformat") {
axis.yformat = value;
} else if (axis_key == "color_map") {
axis.color_map = parse_colormap(value);
} else if (axis_key == "color_norm") {
axis.color_norm = parse_color_norm(value);
} else if (axis_key == "colorbar_label") {
axis.colorbar_label = value;
} else if (axis_key == "cbmin") {
axis.has_cbrange = true;
axis.cbmin = std::stod(value);
} else if (axis_key == "cbmax") {
axis.has_cbrange = true;
axis.cbmax = std::stod(value);
} else if (axis_key == "cbtick_step") {
axis.has_cbtick_step = true;
axis.cbtick_step = std::stod(value);
} else if (axis_key == "legend_spec.position") {
axis.legend_spec.position = parse_legend_position(value);
} else if (axis_key == "legend_spec.columns") {
axis.legend_spec.columns = std::max(1, std::stoi(value));
} else if (axis_key == "legend_spec.boxed") {
axis.legend_spec.boxed = parse_bool(value);
} else if (axis_key == "legend_spec.opaque") {
axis.legend_spec.opaque = parse_bool(value);
} else if (axis_key == "legend_spec.font_pt") {
axis.legend_spec.has_font_pt = true;
axis.legend_spec.font_pt = std::stod(value);
} else if (axis_key == "typography.tick_font_pt") {
axis.typography.has_tick_font_pt = true;
axis.typography.tick_font_pt = std::stod(value);
} else if (axis_key == "typography.label_font_pt") {
axis.typography.has_label_font_pt = true;
axis.typography.label_font_pt = std::stod(value);
} else if (axis_key == "typography.title_font_pt") {
axis.typography.has_title_font_pt = true;
axis.typography.title_font_pt = std::stod(value);
} else if (axis_key == "typography.title_bold") {
axis.typography.has_title_bold = true;
axis.typography.title_bold = parse_bool(value);
} else if (axis_key == "frame.border_mask") {
axis.frame.has_border_mask = true;
axis.frame.border_mask = std::stoi(value);
} else if (axis_key == "frame.border_line_width_pt") {
axis.frame.has_border_line_width_pt = true;
axis.frame.border_line_width_pt = std::stod(value);
} else if (axis_key == "frame.border_color") {
axis.frame.has_border_color = true;
axis.frame.border_color = value;
} else if (axis_key == "frame.ticks_out") {
axis.frame.has_ticks_out = true;
axis.frame.ticks_out = parse_bool(value);
} else if (axis_key == "frame.ticks_mirror") {
axis.frame.has_ticks_mirror = true;
axis.frame.ticks_mirror = parse_bool(value);
} else {
return false;
}
return true;
}
std::string scalar_as_string(const YAML::Node& n, const std::string& key) {
if (!n.IsScalar()) {
throw std::runtime_error("expected scalar for key '" + key + "'");
}
return n.as<std::string>();
}
void parse_formats(FigureSpec& figure, const YAML::Node& formats) {
if (!formats.IsSequence()) {
throw std::runtime_error("figure.formats must be a sequence");
}
figure.formats.clear();
for (const auto& item : formats) {
figure.formats.push_back(parse_format(item.as<std::string>()));
}
}
void parse_figure(FigureSpec& figure, const YAML::Node& node, const YamlLoadOptions& options) {
if (!node || !node.IsMap()) {
throw std::runtime_error("'figure' must be a YAML map");
}
for (const auto& kv : node) {
const auto key = kv.first.as<std::string>();
if (key == "formats") {
parse_formats(figure, kv.second);
continue;
}
if (!kv.second.IsScalar()) {
if (options.strict_unknown_keys) {
throw std::runtime_error("expected scalar for key 'figure." + key + "'");
}
continue;
}
const auto value = scalar_as_string(kv.second, "figure." + key);
const bool known = parse_figure_key(figure, key, value);
if (!known && options.strict_unknown_keys) {
throw std::runtime_error("unknown figure key: '" + key + "'");
}
}
}
void parse_axis_map(AxesSpec& axis, const YAML::Node& axis_node, const YamlLoadOptions& options) {
if (!axis_node.IsMap()) {
throw std::runtime_error("each axes entry must be a YAML map");
}
for (const auto& kv : axis_node) {
const auto key = kv.first.as<std::string>();
const auto& value_node = kv.second;
if (key == "typography" || key == "frame" || key == "legend_spec") {
if (!value_node.IsMap()) {
throw std::runtime_error("axes." + key + " must be a YAML map");
}
for (const auto& skv : value_node) {
const auto sub = skv.first.as<std::string>();
const auto full = key + "." + sub;
const bool known = parse_axis_key(axis, full, scalar_as_string(skv.second, "axes." + full));
if (!known && options.strict_unknown_keys) {
throw std::runtime_error("unknown axis key: '" + full + "'");
}
}
continue;
}
if (!value_node.IsScalar()) {
if (options.strict_unknown_keys) {
throw std::runtime_error("expected scalar for key 'axes." + key + "'");
}
continue;
}
const bool known = parse_axis_key(axis, key, scalar_as_string(value_node, "axes." + key));
if (!known && options.strict_unknown_keys) {
throw std::runtime_error("unknown axis key: '" + key + "'");
}
}
}
} // namespace
YamlFigureSpec load_yaml_figure_spec(const std::filesystem::path& path,
const YamlLoadOptions& options) {
YamlFigureSpec out;
out.figure = FigureSpec{};
const auto root = YAML::LoadFile(path.string());
parse_figure(out.figure, root["figure"], options);
const auto axes_node = root["axes"];
if (axes_node) {
if (!axes_node.IsSequence()) {
throw std::runtime_error("'axes' must be a YAML sequence");
}
for (const auto& axis_node : axes_node) {
AxesSpec axis;
parse_axis_map(axis, axis_node, options);
out.axes.push_back(axis);
}
}
if (out.figure.formats.empty()) {
out.figure.formats = {OutputFormat::Pdf};
}
return out;
}
} // namespace gnuplotpp