123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197(* initialisations système *)(* initialise aussi sdl pour charger la fonte... à faire après ? *)exceptionShell_errorof(int*string)letconcat=Filename.concat(* installation *)letoplot_dir=trySys.getenv"OPLOTDIR"with|Not_found->letexe=(* Whereami.exe ()*)Sys.executable_nameinbeginletbasename,dirname=Filename.basename,Filename.dirnameinDebug.print"Executable: %s"(basenameexe);Debug.print"Directory: %s"(basename(dirnameexe));match(basenameexe),(basename(dirnameexe))with|"goplot","bin"(* = cas où la librairie est utilisée par goplot *)->Filename.concat(dirname(dirnameexe))"share/goplot"|"goplot.exe","gui"(* = lancement par dune exec gui/goplot.exe *)|"utop.exe",".utop"(* = lancement par dune utop *)->Filename.concat(dirname(dirnameexe))"share"|_->letsystem=Unix.open_process_in"opam config var prefix"inletres=input_linesysteminmatchUnix.close_process_insystemwith|Unix.WEXITED0->Filename.concatres"share/oplot"|_->failwith"Please tell me where the oplot directory is, by setting the \
environment variable OPLOTDIR."end|e->raisee(* let oplot_dir = ref oplot_dir
*
* let set_oplot_dir s =
* oplot_dir := s;
* Debug.print "Using oplotdir=%s " !oplot_dir
*
* let () = set_oplot_dir !oplot_dir *)letfirst_time=reftrue(* répertoire perso. Inutilisé pour le moment *)lethome_dir=lethome=trySys.getenv"HOME"withNot_found->"."inlethome_dir_name=concathome".oplot"inifSys.file_existshome_dir_namethenif(Unix.stathome_dir_name).Unix.st_kind=Unix.S_DIRthen(first_time:=false;home_dir_name)else"./"(* then we use the current dir *)else(print_endline("Creating personal oplot directory in "^home_dir_name);Unix.mkdirhome_dir_name0o755;home_dir_name)(* répertoire temporaire; hack around temp_file *)letinit_tmp_dirvar=lettmp=Filename.temp_file"oplot"""inSys.removetmp;Unix.mkdirtmp0o755;print_endline("Creating temp dir in "^tmp);var:=tmplettmp_dir=ref""let()=init_tmp_dirtmp_dirletget_tmp_dir()=!tmp_dir(* font path for font *)letinit_font_path?(fontname="FreeSans.ttf")var=letsearchlist=[fontname;(concatoplot_dirfontname);(concat"/usr/share/fonts/truetype/freefont/"fontname);(concat"/usr/share/fonts/TTF/"fontname);(concat"/usr/share/vlc/skins2/fonts/"fontname)]inletrecloopl=matchlwith|[]->print_endline("Fatal error: font "^fontname^" not found");raiseNot_found|s::ll->ifSys.file_existssthenvar:=selseloopllinloopsearchlistletfont_path=ref""let()=init_font_path~fontname:"DejaVuSans.ttf"font_path(* FreeSans.ttf est beaucoup plus proche du Helvetica de xfig, et donc de la
sortie postscript. Mais le "hinting" (rendu à l'écran en petite taille) est
nettement meilleur avec DejaVuSans... *)letcurrent_font_size=ref12let()=Sdlttf.init()letcurrent_font=ref(Sdlttf.open_font!font_path!current_font_size)(* device ghostscript: verifier qu'on a bien pngalpha avec *)(* gs -h. Sinon mettre autre chose ? png??? *)letxfig_output_file=concat!tmp_dir"oplot.fig"(* any other way ??: *)letxfig_main_channel=ref(open_out(concat!tmp_dir".dummy.main"))(* let (xfig_main_file , xfig_main_channel) = Filename.open_temp_file
"oplot_fig" ".main";; *)let()=close_out!xfig_main_channelletxfig_head_channel=ref(open_out(concat!tmp_dir".dummy.head"))let()=close_out!xfig_head_channelletfig_color_counter=ref32letlatex_header=(concatoplot_dir"header.tex")leteps_output_file=concat!tmp_dir"oplot.eps"letpdf_output_file=concat!tmp_dir"oplot.pdf"(* doit être le même que xfig_output_file, avec extension eps (pour
fig2eps). L'implémenter directement ainsi ? *)letlatex_tmp="oplot-tmp.tex"letbmp_output="oplot.bmp"(* deux précautions valent mieux qu'une pour éviter de détruire d'importe
quoi... *)letremove_tmp_dir()=letfilelist=["oplot.bmp";"oplot.eps";"oplot.pdf";"oplot-tmp.aux";"oplot-tmp.dvi";"oplot-tmp.log";"oplot-tmp.png";"oplot-tmp.ps";"oplot-tmp.tex";".dummy.head";".dummy.main";"oplot.fig"]inList.iter(funs->letfile=concat!tmp_dirsinifSys.file_existsfilethenSys.removefile)filelist;tryUnix.rmdir!tmp_dirwith|Unix.Unix_error(err,_,_)whenerr=Unix.ENOTEMPTY->print_endline"Warning: temporary directory was not empty. We are not \
deleting."|e->raiseeletshellcommand=letexecs=ifDebug.debugthenprint_endlines;match(Sys.commands)with|0->()|a->raise(Shell_error(a,s))inPrintf.kprintfexeccommand(* vérifie si gs est compilé avec le device "pngalpha" *)letpngalpha()=Sys.command"gs --help | grep pngalpha > /dev/null"=0(* vérifie la présence d'un exécutable *)lethas_exename=Sys.command(Printf.sprintf"which %s"name)=0lethas_latex=has_exe"latex"lethas_gs=has_exe"gs"lethas_xfig=has_exe"xfig"lethas_inkscape=has_exe"inkscape"lethas_fig2dev=has_exe"fig2dev"letfig2ps=ifhas_exe"fig2ps"then"fig2ps"elseletlocal=concatoplot_dir"fig2eps"inif(Unix.statlocal).st_perm<>0o755thenUnix.chmodlocal0o755;localletfig2eps=fig2ps^" --eps --noforcespecial --nogv"letfig2pdf=fig2ps^" --pdf --noforcespecial --nogv"(* vérifie la présence d'au moins un exécutable dans une liste. Renvoie le
premier trouvé (option)*)letexe_from_listlist=letrecloopl=matchlwith|[]->None|v::ll->ifhas_exevthen(Somev)elseloopllinlooplist(* cherche un programme pour voir des images. None si rien trouvé *)letviewer=exe_from_list["kuickshow";"gwenview";"eog";"display";"gimp";"Gimp"](* cherche un programme pour voir du postscript. None si rien trouvé *)letpsviewer=exe_from_list["okular";"gv";"kghostview"]letfirst_time()=!first_time