Daniel@0: /* Part of DML (Digital Music Laboratory) Daniel@0: Copyright 2014-2015 Samer Abdallah, University of London Daniel@0: Daniel@0: This program is free software; you can redistribute it and/or Daniel@0: modify it under the terms of the GNU General Public License Daniel@0: as published by the Free Software Foundation; either version 2 Daniel@0: of the License, or (at your option) any later version. Daniel@0: Daniel@0: This program is distributed in the hope that it will be useful, Daniel@0: but WITHOUT ANY WARRANTY; without even the implied warranty of Daniel@0: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Daniel@0: GNU General Public License for more details. Daniel@0: Daniel@0: You should have received a copy of the GNU General Public Daniel@0: License along with this library; if not, write to the Free Software Daniel@0: Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA Daniel@0: */ Daniel@0: Daniel@0: :- module(cp_matlab, Daniel@0: [ figure//4 Daniel@0: , figure//3 Daniel@0: , term_rendering//3 Daniel@0: ]). Daniel@0: Daniel@0: :- meta_predicate figure(0,+,+,+,?,?). Daniel@0: :- meta_predicate figure(0,+,+,?,?). Daniel@0: :- meta_predicate figure(0,+,?,?). Daniel@0: Daniel@0: :- use_module(library(http/http_dispatch),[http_link_to_id/3]). Daniel@0: :- use_module(library(http/html_write)). Daniel@0: :- use_module(library(http/html_head)). Daniel@0: :- use_module(library(dcg_core)). Daniel@0: :- use_module(library(listutils)). Daniel@0: :- use_module(library(optutils)). Daniel@0: :- use_module(library(pengines_io)). Daniel@0: :- use_module(library(swish/render)). Daniel@0: :- use_module(library(mlserver)). Daniel@0: :- use_module(library(code_cache)). Daniel@0: :- use_module(library(insist)). Daniel@0: :- use_module(components(smartimg)). Daniel@0: Daniel@0: :- register_renderer(matlab,"Render Matlab figures"). Daniel@0: Daniel@0: :- html_resource(js('smartimg.js'), []). % no requirements Daniel@0: Daniel@0: %% term_rendering(FigSpec,Something,Opts)// is det. Daniel@0: % Daniel@0: % Matlab term rendering. Daniel@0: % Output from Matlab code is send to current_output of Matlab server thread (usually user_output). Daniel@0: % Options are: Daniel@0: % * timeout(T:float) Daniel@0: % * format(F:oneof([svg,png])) Daniel@0: % And any accepted by api_matlab:figure_render/1.r Daniel@0: term_rendering(fig(Code),_,Opts) --> Daniel@0: {rendering_options(T,Opts,Opts1)}, Daniel@0: figure(mlserver:ml_async(exec(Code),T), Opts1). Daniel@0: term_rendering(fig(Code,W,H),_,Opts) --> term_rendering(fig(Code),_,[width(W),height(H)|Opts]). Daniel@0: term_rendering(fig(Code,FOpts),_,Opts) --> Daniel@0: {merge_options(FOpts,Opts,Opts1)}, Daniel@0: term_rendering(fig(Code),_,Opts1). Daniel@0: Daniel@0: rendering_options(T) --> Daniel@0: seqmap(option_default_select,[module(_),numbervars(_),quoted(_)],_), Daniel@0: option_default_select(timeout(T),60). Daniel@0: Daniel@0: user:portray(ws(X)) :- ml_ws_name(X,Var,Eng), format('ws(|~w:~w|)',[Eng,Var]). Daniel@0: pengines_io:blob_rendering(ws,_Blob,_) --> Daniel@0: html(span(class='pl-blob','ws:_')). Daniel@0: Daniel@0: %% figure(+Code:callable, +Width:natural, +Height:natural, +Params:list)// is det. Daniel@0: %% figure(+Code:callable, +Width:natural, +Height:natural)// is det. Daniel@0: % Daniel@0: % Component to render a Matlab figure into a HTML code. Code must be a Prolog Daniel@0: % goal that renders the desired figure into Matlab's current figure. Daniel@0: % The default figure format (eg SVG or PNG) is taken from the default_figure_format Daniel@0: % setting. Options are Daniel@0: % * cache(boolean) Daniel@0: % If true (default: false), put the code in a cache and use its unique id to call Daniel@0: % it. Useful for code that is to long to encode in a URL. Daniel@0: % * downloadable(boolean) Daniel@0: % If true (default: false), add download links (for PDF and EPS formats) below the image. Daniel@0: % * smart(boolean) Daniel@0: % If true (default: false), then use Javascript image loading code instead of a standard HTML Daniel@0: % tag. If the server returns an error HTML page, this is displayed Daniel@0: % instead of the image. Daniel@0: % See api_matlab:figure_render/1 for other valid options. Daniel@0: figure(Code,W,H) --> figure(Code,[width(W),height(H)]). Daniel@0: figure(Code,W,H,Params) --> figure(Code,[width(W),height(H)|Params]). Daniel@0: figure(Code,Params) --> Daniel@0: { debug(cp_matlab,"figure(~q,~q)",[Code,Params]), Daniel@0: process_options(Download,Smart,Code,Params,Params1), Daniel@0: insist(option(width(W),Params1), missing_parameter(width)), Daniel@0: insist(option(height(H),Params1), missing_parameter(height)), Daniel@0: http_link_to_id(figure_render, Params1,URL) Daniel@0: }, Daniel@0: ( {Download=true} Daniel@0: -> { option_default_select(format(_),_,Params1,Params2)}, Daniel@0: html(div([\image(Smart,URL,W,H),br([]), 'Download as:', \download(pdf,Params2), \download(eps,Params2)])) Daniel@0: ; image(Smart,URL,W,H) Daniel@0: ). Daniel@0: Daniel@0: download(F,Params) --> Daniel@0: {http_link_to_id(figure_render,[format(F)|Params],URL)}, Daniel@0: html([' ',a([href(URL),download],F)]). Daniel@0: Daniel@0: process_options(Download,Smart,Code) --> Daniel@0: {setting(matlab:default_figure_format,Fmt0)}, Daniel@0: seqmap(option_default_select, [format(Fmt), cache(Cache), downloadable(Download), smart(Smart)], Daniel@0: [Fmt0,false,false,false]), Daniel@0: (select_option(color_map(CM)) -> {term_to_atom(CM,CMA)}, cons(color_map(CMA)); []), Daniel@0: (select_option(size(W,H)) -> cons(width(W)), cons(height(H)); []), Daniel@0: {Cache=true -> cache_code(Download,Code,Code1); Code=Code1}, Daniel@0: {term_to_atom(Code1,CodeAtom)}, Daniel@0: cons(format(Fmt)), Daniel@0: cons(code(CodeAtom)).