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(api_r_plot, []). Daniel@0: Daniel@0: :- use_module(library(http/http_dispatch)). Daniel@0: :- use_module(library(http/http_parameters)). Daniel@0: :- use_module(library(sandbox)). Daniel@0: :- use_module(library(insist)). Daniel@0: :- use_module(library(fileutils)). Daniel@0: :- use_module(library(swipe)). Daniel@0: :- use_module(library(httpfiles)). Daniel@0: :- use_module(library(real)). Daniel@0: Daniel@0: :- set_prolog_flag(double_quotes,string). Daniel@0: :- set_prolog_flag(back_quotes,symbol_char). Daniel@0: Daniel@0: :- http_handler(api(r/render), r_figure_render, []). Daniel@0: Daniel@0: :- setting(r_plot:pixels_per_inch,number,150,"Pixels per inch for in browser figures"). Daniel@0: :- setting(r_plot:default_figure_format,oneof([png,svg]),svg,"Default R figure rendering method"). Daniel@0: Daniel@0: %% figure_render(+Request) is det. Daniel@0: % Daniel@0: % HTTP handler for rendering R figures. Daniel@0: r_figure_render(Request) :- Daniel@0: setting(r_plot:pixels_per_inch,DefPPI), Daniel@0: http_parameters(Request, Daniel@0: [ code(CodeA,[ atom, optional(false), description("Prolog rendering goal")]) Daniel@0: , format(F, [ oneof([eps,svg,pdf,png]), optional(false), description("Output format") ]) Daniel@0: , width(W, [ number, optional(true), default(10), description("Width in cm")]) Daniel@0: , height(H, [ number, optional(true), default(6), description("Height in cm")]) Daniel@0: , font_name(FN, [ atom, optional(true), default(helvetica) ]) Daniel@0: , font_size(FS, [ number, optional(true), default(10) ]) Daniel@0: , ppi(PPI, [ number, optional(true), default(DefPPI), description("PNG resolution")]) Daniel@0: ]), Daniel@0: debug(r_plot,"Attempting to parse \"~s\".",[CodeA]), Daniel@0: read_term_from_atom(CodeA,Code,[]), Daniel@0: ( user_db:logged_on(A) Daniel@0: -> debug(r_plot,"Logged on as ~w, no checking",[A]) Daniel@0: ; debug(r_plot,"Checking ~q for safety...",[Code]), Daniel@0: sandbox:safe_goal(Code), Daniel@0: debug(r_plot,"Goal is safe.",[]) Daniel@0: ), Daniel@0: insist(with_temp_dir(Dir, ( Daniel@0: render(F,Code,Dir,File, [size(W,H), ppi(PPI), font_name(FN), font_size(FS) ]), Daniel@0: debug(r_plot,'Replying with file ~s',[File]), Daniel@0: reply_file(File,F) Daniel@0: ))). Daniel@0: Daniel@0: Daniel@0: render(png,Code,D,PNGPath,Opts) :- !, Daniel@0: file_name_extension(tmp,png,PNGFile), Daniel@0: directory_file_path(D,PNGFile,PNGPath), Daniel@0: render(pdf,Code,D,EPSFile,Opts), Daniel@0: option(ppi(PPI),Opts,300), Daniel@0: run(sh(0>>0, "gs -dBATCH -dNumRenderingThreads=2 -dEPSCrop -dNOPAUSE -sDEVICE=pngalpha -sOutputFile=~s -r~d -q ~s", Daniel@0: [PNGPath+write,\PPI,EPSFile+read])). Daniel@0: Daniel@0: % render(png,Code,D,Opts) :- !, option(ppi(PPI),Opts,300), render(png(PPI),Code,D,Opts). Daniel@0: render(Fmt,Code,Dir,Path,Opts) :- Daniel@0: file_name_extension(tmp,Fmt,File), Daniel@0: directory_file_path(Dir,File,Path), Daniel@0: debug(r_plot,'Running ~q',[print_fig(Fmt,Code,Path,Opts)]), Daniel@0: with_mutex(r_plot,api_r_plot:print_fig(Fmt,Code,Path,Opts)). Daniel@0: Daniel@0: print_fig(Fmt,Code,Path,Opts) :- Daniel@0: debug(r_plot,'In print_fig...',[]), Daniel@0: option(size(Width,Height),Opts), Daniel@0: maplist(cm_inch,[Width,Height],[WidthInches,HeightInches]), Daniel@0: debug(r_plot,'Getting device ~w: ~s',[Fmt,Path]), Daniel@0: dev(Fmt,Path,WidthInches,HeightInches,Dev), Daniel@0: debug(r_plot,'Got device ~q',[Dev]), Daniel@0: setup_call_cleanup( Daniel@0: r(Dev), Daniel@0: call_cleanup( Daniel@0: with_output_to(string(_),Code), Daniel@0: exception(Ex), Daniel@0: debug(r_plot,'Exception running R code: ~q',[Ex])), Daniel@0: r('dev.off()')). Daniel@0: Daniel@0: dev(pdf,Name,W,H,pdf(+Name,width=W,height=H)). Daniel@0: dev(eps,Name,W,H,cairo_ps(+Name,width=W,height=H)). Daniel@0: dev(svg,Name,W,H,svg(+Name,width=W,height=H)). Daniel@0: dev(png(PPI),Name,W,H,png(+Name,width=WP,height=HP)) :- WP is PPI*W, HP is PPI*H. Daniel@0: Daniel@0: cm_inch(CM,INCH) :- INCH is CM/2.54. Daniel@0: