comparison cpack/dml/api/r_plot.pl @ 0:718306e29690 tip

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