Daniel@0
|
1 /* Part of DML (Digital Music Laboratory)
|
Daniel@0
|
2 Copyright 2014-2015 Samer Abdallah, University of London
|
Daniel@0
|
3
|
Daniel@0
|
4 This program is free software; you can redistribute it and/or
|
Daniel@0
|
5 modify it under the terms of the GNU General Public License
|
Daniel@0
|
6 as published by the Free Software Foundation; either version 2
|
Daniel@0
|
7 of the License, or (at your option) any later version.
|
Daniel@0
|
8
|
Daniel@0
|
9 This program is distributed in the hope that it will be useful,
|
Daniel@0
|
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
|
Daniel@0
|
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
Daniel@0
|
12 GNU General Public License for more details.
|
Daniel@0
|
13
|
Daniel@0
|
14 You should have received a copy of the GNU General Public
|
Daniel@0
|
15 License along with this library; if not, write to the Free Software
|
Daniel@0
|
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
Daniel@0
|
17 */
|
Daniel@0
|
18
|
Daniel@0
|
19 :- module(api_r_plot, []).
|
Daniel@0
|
20
|
Daniel@0
|
21 :- use_module(library(http/http_dispatch)).
|
Daniel@0
|
22 :- use_module(library(http/http_parameters)).
|
Daniel@0
|
23 :- use_module(library(sandbox)).
|
Daniel@0
|
24 :- use_module(library(insist)).
|
Daniel@0
|
25 :- use_module(library(fileutils)).
|
Daniel@0
|
26 :- use_module(library(swipe)).
|
Daniel@0
|
27 :- use_module(library(httpfiles)).
|
Daniel@0
|
28 :- use_module(library(real)).
|
Daniel@0
|
29
|
Daniel@0
|
30 :- set_prolog_flag(double_quotes,string).
|
Daniel@0
|
31 :- set_prolog_flag(back_quotes,symbol_char).
|
Daniel@0
|
32
|
Daniel@0
|
33 :- http_handler(api(r/render), r_figure_render, []).
|
Daniel@0
|
34
|
Daniel@0
|
35 :- setting(r_plot:pixels_per_inch,number,150,"Pixels per inch for in browser figures").
|
Daniel@0
|
36 :- setting(r_plot:default_figure_format,oneof([png,svg]),svg,"Default R figure rendering method").
|
Daniel@0
|
37
|
Daniel@0
|
38 %% figure_render(+Request) is det.
|
Daniel@0
|
39 %
|
Daniel@0
|
40 % HTTP handler for rendering R figures.
|
Daniel@0
|
41 r_figure_render(Request) :-
|
Daniel@0
|
42 setting(r_plot:pixels_per_inch,DefPPI),
|
Daniel@0
|
43 http_parameters(Request,
|
Daniel@0
|
44 [ code(CodeA,[ atom, optional(false), description("Prolog rendering goal")])
|
Daniel@0
|
45 , format(F, [ oneof([eps,svg,pdf,png]), optional(false), description("Output format") ])
|
Daniel@0
|
46 , width(W, [ number, optional(true), default(10), description("Width in cm")])
|
Daniel@0
|
47 , height(H, [ number, optional(true), default(6), description("Height in cm")])
|
Daniel@0
|
48 , font_name(FN, [ atom, optional(true), default(helvetica) ])
|
Daniel@0
|
49 , font_size(FS, [ number, optional(true), default(10) ])
|
Daniel@0
|
50 , ppi(PPI, [ number, optional(true), default(DefPPI), description("PNG resolution")])
|
Daniel@0
|
51 ]),
|
Daniel@0
|
52 debug(r_plot,"Attempting to parse \"~s\".",[CodeA]),
|
Daniel@0
|
53 read_term_from_atom(CodeA,Code,[]),
|
Daniel@0
|
54 ( user_db:logged_on(A)
|
Daniel@0
|
55 -> debug(r_plot,"Logged on as ~w, no checking",[A])
|
Daniel@0
|
56 ; debug(r_plot,"Checking ~q for safety...",[Code]),
|
Daniel@0
|
57 sandbox:safe_goal(Code),
|
Daniel@0
|
58 debug(r_plot,"Goal is safe.",[])
|
Daniel@0
|
59 ),
|
Daniel@0
|
60 insist(with_temp_dir(Dir, (
|
Daniel@0
|
61 render(F,Code,Dir,File, [size(W,H), ppi(PPI), font_name(FN), font_size(FS) ]),
|
Daniel@0
|
62 debug(r_plot,'Replying with file ~s',[File]),
|
Daniel@0
|
63 reply_file(File,F)
|
Daniel@0
|
64 ))).
|
Daniel@0
|
65
|
Daniel@0
|
66
|
Daniel@0
|
67 render(png,Code,D,PNGPath,Opts) :- !,
|
Daniel@0
|
68 file_name_extension(tmp,png,PNGFile),
|
Daniel@0
|
69 directory_file_path(D,PNGFile,PNGPath),
|
Daniel@0
|
70 render(pdf,Code,D,EPSFile,Opts),
|
Daniel@0
|
71 option(ppi(PPI),Opts,300),
|
Daniel@0
|
72 run(sh(0>>0, "gs -dBATCH -dNumRenderingThreads=2 -dEPSCrop -dNOPAUSE -sDEVICE=pngalpha -sOutputFile=~s -r~d -q ~s",
|
Daniel@0
|
73 [PNGPath+write,\PPI,EPSFile+read])).
|
Daniel@0
|
74
|
Daniel@0
|
75 % render(png,Code,D,Opts) :- !, option(ppi(PPI),Opts,300), render(png(PPI),Code,D,Opts).
|
Daniel@0
|
76 render(Fmt,Code,Dir,Path,Opts) :-
|
Daniel@0
|
77 file_name_extension(tmp,Fmt,File),
|
Daniel@0
|
78 directory_file_path(Dir,File,Path),
|
Daniel@0
|
79 debug(r_plot,'Running ~q',[print_fig(Fmt,Code,Path,Opts)]),
|
Daniel@0
|
80 with_mutex(r_plot,api_r_plot:print_fig(Fmt,Code,Path,Opts)).
|
Daniel@0
|
81
|
Daniel@0
|
82 print_fig(Fmt,Code,Path,Opts) :-
|
Daniel@0
|
83 debug(r_plot,'In print_fig...',[]),
|
Daniel@0
|
84 option(size(Width,Height),Opts),
|
Daniel@0
|
85 maplist(cm_inch,[Width,Height],[WidthInches,HeightInches]),
|
Daniel@0
|
86 debug(r_plot,'Getting device ~w: ~s',[Fmt,Path]),
|
Daniel@0
|
87 dev(Fmt,Path,WidthInches,HeightInches,Dev),
|
Daniel@0
|
88 debug(r_plot,'Got device ~q',[Dev]),
|
Daniel@0
|
89 setup_call_cleanup(
|
Daniel@0
|
90 r(Dev),
|
Daniel@0
|
91 call_cleanup(
|
Daniel@0
|
92 with_output_to(string(_),Code),
|
Daniel@0
|
93 exception(Ex),
|
Daniel@0
|
94 debug(r_plot,'Exception running R code: ~q',[Ex])),
|
Daniel@0
|
95 r('dev.off()')).
|
Daniel@0
|
96
|
Daniel@0
|
97 dev(pdf,Name,W,H,pdf(+Name,width=W,height=H)).
|
Daniel@0
|
98 dev(eps,Name,W,H,cairo_ps(+Name,width=W,height=H)).
|
Daniel@0
|
99 dev(svg,Name,W,H,svg(+Name,width=W,height=H)).
|
Daniel@0
|
100 dev(png(PPI),Name,W,H,png(+Name,width=WP,height=HP)) :- WP is PPI*W, HP is PPI*H.
|
Daniel@0
|
101
|
Daniel@0
|
102 cm_inch(CM,INCH) :- INCH is CM/2.54.
|
Daniel@0
|
103
|