Skip to content

Commit db50b97

Browse files
Use built-in 'matlab.net.base64decode' (introduced in R2016b) instead of vendoring 'base64decode'
1 parent a51d7e8 commit db50b97

File tree

2 files changed

+102
-213
lines changed

2 files changed

+102
-213
lines changed

plotly/export_fig2/write_image.m

+102-96
Original file line numberDiff line numberDiff line change
@@ -1,111 +1,117 @@
11
function output = write_image(pfObj, imageFormat, filename, height, width, scale)
2+
% Function to write plotly figures to a supported image format, which
3+
% are the following: "png", "jpg", "jpeg", "webp", "svg", "pdf", "eps",
4+
% "json".
5+
6+
debug=0;
7+
if nargin < 2
8+
imageFormat = "png";
9+
filename = "figure.png";
10+
height = pfObj.layout.height;
11+
width = pfObj.layout.width;
12+
scale = 1;
13+
elseif nargin < 3
14+
filename = "figure." + imageFormat;
15+
height = pfObj.layout.height;
16+
width = pfObj.layout.width;
17+
scale = 1;
18+
elseif nargin < 4
19+
height = pfObj.layout.height;
20+
width = pfObj.layout.width;
21+
scale = 1;
22+
elseif nargin < 5
23+
width = pfObj.layout.width;
24+
scale = 1;
25+
elseif nargin < 6
26+
scale = 1;
27+
end
228

3-
% Function to write plotly figures to a supported image format, which are the following: "png", "jpg", "jpeg", "webp", "svg", "pdf", "eps", "json"
4-
5-
debug=0;
6-
if nargin < 2
7-
imageFormat="png";
8-
filename='figure.png';
9-
height=pfObj.layout.height;
10-
width=pfObj.layout.width;
11-
scale=1;
12-
elseif nargin < 3
13-
filename=['figure.',char(imageFormat)];
14-
height=pfObj.layout.height;
15-
width=pfObj.layout.width;
16-
scale=1;
17-
elseif nargin < 4
18-
height=pfObj.layout.height;
19-
width=pfObj.layout.width;
20-
scale=1;
21-
elseif nargin < 5
22-
width=pfObj.layout.width;
23-
scale=1;
24-
elseif nargin < 6
25-
scale=1;
26-
end
27-
28-
if strcmpi(imageFormat,'jpg')
29-
imageFormat = "jpeg";
30-
end
31-
32-
wd=fileparts(fileparts(mfilename('fullpath')));
33-
output=[];
34-
35-
if ~isa(pfObj,'plotlyfig')
36-
fprintf('\nError: Input is not a plotlyfig object.\n\n');
37-
return
38-
end
39-
40-
if isunix()
41-
kExec = string(fullfile(wd,'kaleido','kaleido'));
42-
cc="cat";
43-
else
44-
kExec = string(fullfile(wd,'kaleido','kaleido.cmd'));
45-
cc="type";
46-
end
47-
plyJsLoc = string(fullfile(wd,'kaleido','plotly-latest.min.js'));
29+
if strcmpi(imageFormat, "jpg")
30+
imageFormat = "jpeg";
31+
end
4832

49-
if ~isfile(kExec) || ~isfile(plyJsLoc)
50-
status=getKaleido();
51-
else
52-
status=1;
53-
end
33+
wd = fileparts(fileparts(mfilename("fullpath")));
34+
output = [];
5435

55-
if status == 0
56-
return
57-
end
36+
if ~isa(pfObj, "plotlyfig")
37+
fprintf("\nError: Input is not a plotlyfig object.\n\n");
38+
return
39+
end
5840

59-
mjLoc = replace(string(fullfile(wd,'kaleido','etc','mathjax','MathJax.js')),'\','/');
60-
scope="plotly";
41+
if isunix()
42+
kExec = string(fullfile(wd,"kaleido", "kaleido"));
43+
cc = "cat";
44+
else
45+
kExec = string(fullfile(wd,"kaleido", "kaleido.cmd"));
46+
cc = "type";
47+
end
48+
plyJsLoc = string(fullfile(wd,"kaleido", "plotly-latest.min.js"));
6149

50+
if ~isfile(kExec) || ~isfile(plyJsLoc)
51+
status = getKaleido();
52+
else
53+
status = 1;
54+
end
6255

63-
% Prepare input plotly object for Kaleido
64-
q=struct();
65-
q.data.data = pfObj.data;
66-
q.data.layout = pfObj.layout;
67-
q.data.layout = rmfield(q.data.layout,'height');
68-
q.data.layout = rmfield(q.data.layout,'width');
69-
q.format = string(imageFormat);
70-
q.height = height;
71-
q.scale = scale;
72-
q.width = width;
56+
if status == 0
57+
return
58+
end
7359

74-
pfJson = native2unicode(jsonencode(q),'UTF-8');
75-
tFile = string(fullfile(wd,'kaleido','temp.txt'));
76-
f=fopen(tFile,'w');
77-
fprintf(f,"%s",pfJson);
78-
fclose(f);
60+
mjLoc = replace(string(fullfile( ...
61+
wd, "kaleido", "etc", "mathjax", "MathJax.js")), "\", "/");
62+
scope="plotly";
63+
64+
% Prepare input plotly object for Kaleido
65+
q = struct();
66+
q.data.data = pfObj.data;
67+
q.data.layout = pfObj.layout;
68+
q.data.layout = rmfield(q.data.layout, "height");
69+
q.data.layout = rmfield(q.data.layout, "width");
70+
q.format = string(imageFormat);
71+
q.height = height;
72+
q.scale = scale;
73+
q.width = width;
74+
75+
pfJson = native2unicode(jsonencode(q), "UTF-8");
76+
tFile = string(fullfile(wd, "kaleido", "temp.txt"));
77+
f = fopen(tFile, "w");
78+
fprintf(f, "%s", pfJson);
79+
fclose(f);
7980

80-
cmd=[cc," ",tFile," | ",kExec," ",scope," --plotlyjs='",plyJsLoc,"' ","--mathjax='file:///",mjLoc,"' --no-sandbox --disable-gpu --allow-file-access-from-files --disable-breakpad --disable-dev-shm-usage"];
81+
cmd = [cc, " ", tFile, " | ", kExec, " ", scope, " --plotlyjs='", ...
82+
plyJsLoc, "' ", "--mathjax='file:///",mjLoc,"' " ...
83+
+ "--no-sandbox --disable-gpu " ...
84+
+ "--allow-file-access-from-files --disable-breakpad " ...
85+
+ "--disable-dev-shm-usage"];
8186

82-
if debug
83-
inputCmd=char(join(cmd,''));
84-
fprintf('\nDebug info:\n%s\n\n',inputCmd);
85-
end
87+
if debug
88+
inputCmd = char(join(cmd, ""));
89+
fprintf("\nDebug info:\n%s\n\n", inputCmd);
90+
end
8691

87-
[code,out]=system(char(join(cmd,'')));
88-
if debug
89-
disp(out);
90-
end
92+
[code,out] = system(char(join(cmd, "")));
93+
if debug
94+
disp(out);
95+
end
9196

92-
if code ~= 0
93-
fprintf('\nFatal: Failed to run Kaleido.\n\n');
94-
return;
95-
else
96-
a=string(split(out,newline));
97-
if a(end)==""
98-
a(end)=[];
97+
if code ~= 0
98+
fprintf("\nFatal: Failed to run Kaleido.\n\n");
99+
return;
100+
else
101+
a = string(split(out,newline));
102+
if a(end) == ""
103+
a(end) = [];
104+
end
105+
output = jsondecode(a(end));
99106
end
100-
output = jsondecode(a(end));
101-
end
102107

103-
if output.code ~= 0
104-
fprintf('\nError: %s\n',output.message);
105-
else
106-
out=unicode2native(output.result,'UTF-8');
107-
out=base64decode(out);
108-
f=fopen(char(filename),'wb');
109-
fwrite(f,out);
110-
fclose(f);
108+
if output.code ~= 0
109+
fprintf("\nError: %s\n", output.message);
110+
else
111+
out = unicode2native(output.result, "UTF-8");
112+
out = matlab.net.base64decode(out);
113+
f = fopen(char(filename), "wb");
114+
fwrite(f, out);
115+
fclose(f);
116+
end
111117
end

plotly/plotly_aux/base64decode.m

-117
This file was deleted.

0 commit comments

Comments
 (0)