-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththeme.cpp
More file actions
114 lines (102 loc) · 3.33 KB
/
theme.cpp
File metadata and controls
114 lines (102 loc) · 3.33 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
#include "gnuplotpp/theme.hpp"
#include <fstream>
#include <sstream>
#include <string>
#include "gnuplotpp/presets.hpp"
namespace gnuplotpp {
namespace {
template <typename T>
bool parse_scalar(const std::string& content, const std::string& key, T& out) {
const auto k = "\"" + key + "\"";
const auto p = content.find(k);
if (p == std::string::npos) {
return false;
}
const auto c = content.find(':', p + k.size());
if (c == std::string::npos) {
return false;
}
std::istringstream iss(content.substr(c + 1));
iss >> out;
return !iss.fail();
}
bool parse_string(const std::string& content, const std::string& key, std::string& out) {
const auto k = "\"" + key + "\"";
const auto p = content.find(k);
if (p == std::string::npos) {
return false;
}
const auto q1 = content.find('"', p + k.size());
if (q1 == std::string::npos) {
return false;
}
const auto q2 = content.find('"', q1 + 1);
if (q2 == std::string::npos) {
return false;
}
out = content.substr(q1 + 1, q2 - q1 - 1);
return true;
}
} // namespace
bool save_theme_json(const std::filesystem::path& path, const FigureSpec& spec) {
std::ofstream os(path);
if (!os.is_open()) {
return false;
}
os << "{\n";
os << " \"palette\": " << static_cast<int>(spec.palette) << ",\n";
os << " \"text_mode\": " << static_cast<int>(spec.text_mode) << ",\n";
os << " \"font\": \"" << spec.style.font << "\",\n";
os << " \"font_pt\": " << spec.style.font_pt << ",\n";
os << " \"line_width_pt\": " << spec.style.line_width_pt << ",\n";
os << " \"point_size\": " << spec.style.point_size << ",\n";
os << " \"grid\": " << (spec.style.grid ? 1 : 0) << "\n";
os << "}\n";
return os.good();
}
bool load_theme_json(const std::filesystem::path& path, FigureSpec& spec) {
std::ifstream in(path);
if (!in.is_open()) {
return false;
}
const std::string content((std::istreambuf_iterator<char>(in)), std::istreambuf_iterator<char>());
int palette = static_cast<int>(spec.palette);
int text_mode = static_cast<int>(spec.text_mode);
int grid = spec.style.grid ? 1 : 0;
parse_scalar(content, "palette", palette);
parse_scalar(content, "text_mode", text_mode);
parse_string(content, "font", spec.style.font);
parse_scalar(content, "font_pt", spec.style.font_pt);
parse_scalar(content, "line_width_pt", spec.style.line_width_pt);
parse_scalar(content, "point_size", spec.style.point_size);
parse_scalar(content, "grid", grid);
spec.palette = static_cast<ColorPalette>(palette);
spec.text_mode = static_cast<TextMode>(text_mode);
spec.style.grid = grid != 0;
return true;
}
void apply_theme_preset(FigureSpec& spec, const ThemePreset preset) {
switch (preset) {
case ThemePreset::IEEE_Strict_v1:
apply_style_profile(spec, StyleProfile::IEEE_Strict);
break;
case ThemePreset::Science_v1:
apply_style_profile(spec, StyleProfile::Science);
break;
case ThemePreset::Tufte_Minimal_v1:
apply_style_profile(spec, StyleProfile::Tufte_Minimal);
break;
}
}
const char* theme_preset_id(const ThemePreset preset) noexcept {
switch (preset) {
case ThemePreset::IEEE_Strict_v1:
return "ieee_strict_v1";
case ThemePreset::Science_v1:
return "science_v1";
case ThemePreset::Tufte_Minimal_v1:
return "tufte_minimal_v1";
}
return "unknown";
}
} // namespace gnuplotpp