Skip to content

Use built-in 'matlab.net.base64decode' (introduced in R2016b) instead… #505

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Use built-in 'matlab.net.base64decode' (introduced in R2016b) instead…
… of vendoring 'base64decode'
  • Loading branch information
robertoffmoura committed Aug 4, 2024
commit db50b97c9ef69331c4a3cabc7be3552d28cb1ac0
198 changes: 102 additions & 96 deletions plotly/export_fig2/write_image.m
Original file line number Diff line number Diff line change
@@ -1,111 +1,117 @@
function output = write_image(pfObj, imageFormat, filename, height, width, scale)
% Function to write plotly figures to a supported image format, which
% are the following: "png", "jpg", "jpeg", "webp", "svg", "pdf", "eps",
% "json".

debug=0;
if nargin < 2
imageFormat = "png";
filename = "figure.png";
height = pfObj.layout.height;
width = pfObj.layout.width;
scale = 1;
elseif nargin < 3
filename = "figure." + imageFormat;
height = pfObj.layout.height;
width = pfObj.layout.width;
scale = 1;
elseif nargin < 4
height = pfObj.layout.height;
width = pfObj.layout.width;
scale = 1;
elseif nargin < 5
width = pfObj.layout.width;
scale = 1;
elseif nargin < 6
scale = 1;
end

% Function to write plotly figures to a supported image format, which are the following: "png", "jpg", "jpeg", "webp", "svg", "pdf", "eps", "json"

debug=0;
if nargin < 2
imageFormat="png";
filename='figure.png';
height=pfObj.layout.height;
width=pfObj.layout.width;
scale=1;
elseif nargin < 3
filename=['figure.',char(imageFormat)];
height=pfObj.layout.height;
width=pfObj.layout.width;
scale=1;
elseif nargin < 4
height=pfObj.layout.height;
width=pfObj.layout.width;
scale=1;
elseif nargin < 5
width=pfObj.layout.width;
scale=1;
elseif nargin < 6
scale=1;
end

if strcmpi(imageFormat,'jpg')
imageFormat = "jpeg";
end

wd=fileparts(fileparts(mfilename('fullpath')));
output=[];

if ~isa(pfObj,'plotlyfig')
fprintf('\nError: Input is not a plotlyfig object.\n\n');
return
end

if isunix()
kExec = string(fullfile(wd,'kaleido','kaleido'));
cc="cat";
else
kExec = string(fullfile(wd,'kaleido','kaleido.cmd'));
cc="type";
end
plyJsLoc = string(fullfile(wd,'kaleido','plotly-latest.min.js'));
if strcmpi(imageFormat, "jpg")
imageFormat = "jpeg";
end

if ~isfile(kExec) || ~isfile(plyJsLoc)
status=getKaleido();
else
status=1;
end
wd = fileparts(fileparts(mfilename("fullpath")));
output = [];

if status == 0
return
end
if ~isa(pfObj, "plotlyfig")
fprintf("\nError: Input is not a plotlyfig object.\n\n");
return
end

mjLoc = replace(string(fullfile(wd,'kaleido','etc','mathjax','MathJax.js')),'\','/');
scope="plotly";
if isunix()
kExec = string(fullfile(wd,"kaleido", "kaleido"));
cc = "cat";
else
kExec = string(fullfile(wd,"kaleido", "kaleido.cmd"));
cc = "type";
end
plyJsLoc = string(fullfile(wd,"kaleido", "plotly-latest.min.js"));

if ~isfile(kExec) || ~isfile(plyJsLoc)
status = getKaleido();
else
status = 1;
end

% Prepare input plotly object for Kaleido
q=struct();
q.data.data = pfObj.data;
q.data.layout = pfObj.layout;
q.data.layout = rmfield(q.data.layout,'height');
q.data.layout = rmfield(q.data.layout,'width');
q.format = string(imageFormat);
q.height = height;
q.scale = scale;
q.width = width;
if status == 0
return
end

pfJson = native2unicode(jsonencode(q),'UTF-8');
tFile = string(fullfile(wd,'kaleido','temp.txt'));
f=fopen(tFile,'w');
fprintf(f,"%s",pfJson);
fclose(f);
mjLoc = replace(string(fullfile( ...
wd, "kaleido", "etc", "mathjax", "MathJax.js")), "\", "/");
scope="plotly";

% Prepare input plotly object for Kaleido
q = struct();
q.data.data = pfObj.data;
q.data.layout = pfObj.layout;
q.data.layout = rmfield(q.data.layout, "height");
q.data.layout = rmfield(q.data.layout, "width");
q.format = string(imageFormat);
q.height = height;
q.scale = scale;
q.width = width;

pfJson = native2unicode(jsonencode(q), "UTF-8");
tFile = string(fullfile(wd, "kaleido", "temp.txt"));
f = fopen(tFile, "w");
fprintf(f, "%s", pfJson);
fclose(f);

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"];
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"];

if debug
inputCmd=char(join(cmd,''));
fprintf('\nDebug info:\n%s\n\n',inputCmd);
end
if debug
inputCmd = char(join(cmd, ""));
fprintf("\nDebug info:\n%s\n\n", inputCmd);
end

[code,out]=system(char(join(cmd,'')));
if debug
disp(out);
end
[code,out] = system(char(join(cmd, "")));
if debug
disp(out);
end

if code ~= 0
fprintf('\nFatal: Failed to run Kaleido.\n\n');
return;
else
a=string(split(out,newline));
if a(end)==""
a(end)=[];
if code ~= 0
fprintf("\nFatal: Failed to run Kaleido.\n\n");
return;
else
a = string(split(out,newline));
if a(end) == ""
a(end) = [];
end
output = jsondecode(a(end));
end
output = jsondecode(a(end));
end

if output.code ~= 0
fprintf('\nError: %s\n',output.message);
else
out=unicode2native(output.result,'UTF-8');
out=base64decode(out);
f=fopen(char(filename),'wb');
fwrite(f,out);
fclose(f);
if output.code ~= 0
fprintf("\nError: %s\n", output.message);
else
out = unicode2native(output.result, "UTF-8");
out = matlab.net.base64decode(out);
f = fopen(char(filename), "wb");
fwrite(f, out);
fclose(f);
end
end
117 changes: 0 additions & 117 deletions plotly/plotly_aux/base64decode.m

This file was deleted.