3
3
//
4
4
5
5
#include " gnuplot.h"
6
+ #ifdef CXX_FILESYSTEM_IS_EXPERIMENTAL
7
+ #include < experimental/filesystem>
8
+ #else
6
9
#include < filesystem>
10
+ #endif
7
11
#include < iostream>
8
12
#include < matplot/util/common.h>
9
13
#include < matplot/util/popen.h>
10
14
#include < regex>
11
15
#include < thread>
16
+ #include < cstdlib>
12
17
13
18
#ifdef MATPLOT_HAS_FBUFSIZE
14
19
@@ -31,14 +36,32 @@ namespace matplot::backend {
31
36
bool gnuplot::consumes_gnuplot_commands () { return true ; }
32
37
33
38
gnuplot::gnuplot () {
34
- // List terminal types
35
- terminal_ = default_terminal_type ();
39
+ // 1st option: terminal in GNUTERM environment variable
40
+ const char *environment_terminal = std::getenv (" GNUTERM" );
41
+ if (environment_terminal) {
42
+ if (terminal_is_available (environment_terminal)) {
43
+ terminal_ = environment_terminal;
44
+ }
45
+ #if defined(_WIN32) || defined(_WIN64) || defined(__MINGW32__) || defined(__CYGWIN__)
46
+ } else if (terminal_is_available (" wxt" )) {
47
+ // 2nd option: wxt on windows, even if not default
48
+ terminal_ = " wxt" ;
49
+ #endif
50
+ } else if (terminal_is_available (" qt" )) {
51
+ // 3rd option: qt
52
+ terminal_ = " qt" ;
53
+ } else {
54
+ // 4rd option: default terminal type
55
+ terminal_ = default_terminal_type ();
56
+ }
57
+
36
58
// Open the gnuplot pipe_
37
59
if constexpr (windows_should_persist_by_default) {
38
60
pipe_ = POPEN (" gnuplot --persist" , " w" );
39
61
} else {
40
62
pipe_ = POPEN (" gnuplot" , " w" );
41
63
}
64
+
42
65
// Check if everything is OK
43
66
if (!pipe_) {
44
67
std::cerr << " Opening the gnuplot pipe_ failed!" << std::endl;
@@ -71,6 +94,12 @@ namespace matplot::backend {
71
94
72
95
const std::string &gnuplot::output_format () { return terminal_; }
73
96
97
+ #ifdef STRING_VIEW_CONSTEXPR_BUG
98
+ #define SV_CONSTEXPR
99
+ #else
100
+ #define SV_CONSTEXPR constexpr
101
+ #endif
102
+
74
103
bool gnuplot::output (const std::string &filename) {
75
104
if (filename.empty ()) {
76
105
output_ = filename;
@@ -79,12 +108,17 @@ namespace matplot::backend {
79
108
}
80
109
81
110
// look at the extension
111
+ #ifdef CXX_FILESYSTEM_IS_EXPERIMENTAL
112
+ namespace fs = std::experimental::filesystem;
113
+ #else
82
114
namespace fs = std::filesystem;
115
+ #endif
116
+
83
117
fs::path p{filename};
84
118
std::string ext = p.extension ().string ();
85
119
86
120
// check terminal for that extension
87
- constexpr auto exts = extension_terminal ();
121
+ SV_CONSTEXPR auto exts = extension_terminal ();
88
122
auto it = std::find_if (exts.begin (), exts.end (),
89
123
[&](const auto &e) { return e.first == ext; });
90
124
@@ -115,7 +149,7 @@ namespace matplot::backend {
115
149
}
116
150
117
151
// Check if file format is valid
118
- constexpr auto exts = extension_terminal ();
152
+ SV_CONSTEXPR auto exts = extension_terminal ();
119
153
auto it = std::find_if (exts.begin (), exts.end (), [&](const auto &e) {
120
154
return e.second == format;
121
155
});
@@ -127,7 +161,11 @@ namespace matplot::backend {
127
161
}
128
162
129
163
// Create file if it does not exist
164
+ #ifdef CXX_FILESYSTEM_IS_EXPERIMENTAL
165
+ namespace fs = std::experimental::filesystem;
166
+ #else
130
167
namespace fs = std::filesystem;
168
+ #endif
131
169
fs::path p{filename};
132
170
if (!p.parent_path ().empty () && !fs::exists (p.parent_path ())) {
133
171
fs::create_directory (p.parent_path ());
@@ -271,6 +309,11 @@ namespace matplot::backend {
271
309
return terminal_type;
272
310
}
273
311
312
+ bool gnuplot::terminal_is_available (std::string_view term) {
313
+ std::string msg = run_and_get_output (" gnuplot -e \" set terminal " + std::string (term.data ()) + " \" 2>&1" );
314
+ return msg.empty ();
315
+ }
316
+
274
317
std::tuple<int , int , int > gnuplot::gnuplot_version () {
275
318
static std::tuple<int , int , int > version{0 , 0 , 0 };
276
319
const bool dont_know_gnuplot_version_yet =
@@ -316,7 +359,7 @@ namespace matplot::backend {
316
359
}
317
360
318
361
bool gnuplot::terminal_has_title_option (const std::string &t) {
319
- constexpr std::string_view whitelist[] = {
362
+ SV_CONSTEXPR std::string_view whitelist[] = {
320
363
" qt" , " aqua" , " caca" , " canvas" , " windows" , " wxt" , " x11" };
321
364
return std::find (std::begin (whitelist), std::end (whitelist), t) !=
322
365
std::end (whitelist);
@@ -326,7 +369,7 @@ namespace matplot::backend {
326
369
// Terminals that have the size option *in the way we expect it to work*
327
370
// This includes only the size option with {width, height} and not
328
371
// the size option for cropping or scaling
329
- constexpr std::string_view whitelist[] = {
372
+ SV_CONSTEXPR std::string_view whitelist[] = {
330
373
" qt" , " aqua" , " caca" , " canvas" , " eepic" ,
331
374
" emf" , " gif" , " jpeg" , " pbm" , " png" ,
332
375
" sixelgd" , " tkcanvas" , " windows" , " wxt" , " svg" };
@@ -335,13 +378,13 @@ namespace matplot::backend {
335
378
}
336
379
337
380
bool gnuplot::terminal_has_position_option (const std::string &t) {
338
- constexpr std::string_view whitelist[] = {" qt" , " windows" , " wxt" };
381
+ SV_CONSTEXPR std::string_view whitelist[] = {" qt" , " windows" , " wxt" };
339
382
return std::find (std::begin (whitelist), std::end (whitelist), t) !=
340
383
std::end (whitelist);
341
384
}
342
385
343
386
bool gnuplot::terminal_has_enhanced_option (const std::string &t) {
344
- constexpr std::string_view whitelist[] = {
387
+ SV_CONSTEXPR std::string_view whitelist[] = {
345
388
" canvas" , " postscript" , " qt" , " aqua" , " caca" ,
346
389
" canvas" , " dumb" , " emf" , " enhanced" , " jpeg" ,
347
390
" pdf" , " pdfcairo" , " pm" , " png" , " pngcairo" ,
@@ -352,7 +395,7 @@ namespace matplot::backend {
352
395
}
353
396
354
397
bool gnuplot::terminal_has_color_option (const std::string &t) {
355
- constexpr std::string_view whitelist[] = {
398
+ SV_CONSTEXPR std::string_view whitelist[] = {
356
399
" postscript" , " aifm" , " caca" , " cairolatex" , " context" ,
357
400
" corel" , " eepic" , " emf" , " epscairo" , " epslatex" ,
358
401
" fig" , " lua tikz" , " mif" , " mp" , " pbm" ,
@@ -367,7 +410,7 @@ namespace matplot::backend {
367
410
// and terminals for which we want to use only the default fonts
368
411
// We prefer a blacklist because it's better to get a warning
369
412
// in a false positive than remove the fonts in a false negative.
370
- constexpr std::string_view blacklist[] = {
413
+ SV_CONSTEXPR std::string_view blacklist[] = {
371
414
" dxf" , " eepic" , " emtex" , " hpgl" , " latex" ,
372
415
" mf" , " pcl5" , " pslatex" , " pstex" , " pstricks" ,
373
416
" qms" , " tek40xx" , " tek410x" , " texdraw" , " tkcanvas" ,
0 commit comments