123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228Debug.print"* Loading sysinit";;(* System inits *)(* This will also initialize SDL for loading the font *)moduleSdlttf=Tsdl_ttf.TtfexceptionShell_errorof(int*string)letgo=Debug.golet(//)=Filename.concat(* installation *)letshare_dir()=tryletsystem=Unix.open_process_in"opam var share"inletres=input_linesysteminmatchUnix.close_process_insystemwith|Unix.WEXITED0->res//"oplot"|_->failwith"Cannot find share dir. Please set the environment variable OPLOTDIR."with_->Debug.print"No oplot installation found. Using current dir.";Filename.current_dir_nameletshare_dir=share_dir()letoplot_dir=trySys.getenv"OPLOTDIR"with|Not_found->(letexe=(* Whereami.exe ()*)Sys.executable_nameinletbasename,dirname=(Filename.basename,Filename.dirname)inDebug.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 *)->dirname(dirnameexe)//"share/goplot"|"goplot.exe","gui"(* = lancement par dune exec gui/goplot.exe *)(* | "utop.exe", ".utop" (\* = lancement par dune utop *\)
* -> Filename.concat (dirname (dirname exe)) "share" *)|_->share_dir)|e->raiseeletfirst_time=reftrue(* répertoire perso. Inutilisé pour le moment *)lethome_dir=lethome=trySys.getenv"HOME"withNot_found->"."inlethome_dir_name=home//".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(Debug.print"Creating personal oplot directory in %s"home_dir_name;tryUnix.mkdirhome_dir_name0o755;home_dir_namewith_->print_endline("Error creating "^home_dir_name);".")(* répertoire temporaire *)letinit_tmp_dirvar=lettmp=Filename.temp_file"oplot"""inSys.removetmp;Unix.mkdirtmp0o755;Debug.print"Creating temp dir in %s"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;".."//"share"//fontname;(* for dune runtest *)oplot_dir//fontname;share_dir//fontname;"/usr/share/fonts/truetype/freefont/"//fontname;"/usr/share/fonts/truetype/dejavu/"//fontname;"/usr/share/fonts/TTF/"//fontname;"/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()|>goletcurrent_font=ref(Sdlttf.open_font!font_path!current_font_size|>go)(* device ghostscript: verifier qu'on a bien pngalpha avec *)(* gs -h. Sinon mettre autre chose ? png??? *)letxfig_output_tmp=!tmp_dir//"oplot.fig"(* any other way ??: *)letxfig_main_channel=ref(open_out(!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(!tmp_dir//".dummy.head"))let()=close_out!xfig_head_channelletfig_color_counter=ref32letlatex_header=oplot_dir//"header.tex"leteps_output_tmp=!tmp_dir//"oplot.eps"letpdf_output_tmp=!tmp_dir//"oplot.pdf"(* doit être le même que xfig_output_tmp, avec extension eps (pour
fig2eps). L'implémenter directement ainsi ? *)letlatex_tmp="oplot-tmp.tex"letpng_output="oplot.png"letpdf_output="oplot.pdf"leteps_output="oplot.eps"letfig_output="oplot.fig"(* deux précautions valent mieux qu'une pour éviter de détruire d'importe
quoi... *)letremove_tmp_dir()=Debug.print"Cleaning tmp directory: %s"!tmp_dir;letfilelist=["oplot.png";"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=!tmp_dir//sinifSys.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;matchSys.commandswith0->()|a->raise(Shell_error(a,s))inPrintf.ksprintfexeccommand(* 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%s"name(ifDebug.debugthen""else" > /dev/null"))=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=oplot_dir//"fig2eps"inifSys.file_existslocalthenlet()=if(Unix.statlocal).st_perm<>0o755thentryUnix.chmodlocal0o755with_->Debug.print"Cannot make %s executable"localinlocalelsebeginprint_endline"WARNING: fig2ps not found. You will not be able to export to ps or \
pdf.";"fig2ps"endletfig2eps=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_exevthenSomevelseloopllinlooplist(* 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";"evince";"gv";"kghostview"]letfirst_time()=!first_time