## Copyright (C) 2016 Pantxo Diribarne
## 
## This program is free software; you can redistribute it and/or modify it
## under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 3 of the License, or
## (at your option) any later version.
## 
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
## 
## You should have received a copy of the GNU General Public License
## along with this program.  If not, see <http://www.gnu.org/licenses/>.

## -*- texinfo -*- 
## @deftypefn {Function File} {} svg2other (@var{infile})
## @deftypefnx {Function File} {} svg2other (@var{infile}, @var{outfile})
## @deftypefnx {Function File} {} svg2other (@var{infile}, @var{outfile}, @var{fmt})
## Convert an svg image into another format.
##
## The input file @var{infile} must be a valid svg file. If the name of
## the output file @var{outfile} is not provided or is empty, the output
## file will have the same name as the input file with an extension that
## depends on the output format.
##
## The output format @var{fmt} may be one of "pdf", "eps", "png" or
## "jpg". The default is "pdf".
## @seealso{}
## @end deftypefn

## Author: Pantxo Diribarne <pantxo@gre046552>
## Created: 2016-09-27

function [retval] = svg2other (fnamein = "", fnameout = "", fmt = "pdf")
  persistent libsloaded = false;

  if (! libsloaded)
    javaaddpath ("/usr/share/java/batik-transcoder.jar");
    javaaddpath ("/usr/share/java/batik-rasterizer.jar");
    javaaddpath ("/usr/share/java/fop.jar");
    libsloaded = true;
  endif

  formats = {"pdf", "png", "eps", "jpg"};
  if (isempty (fnamein) || ! ischar (fnamein) || ! ischar (fnameout))
    print_usage ();
  elseif (! exist (fnamein, "file"))
    error ("svg2other: unable to find input file %s", fnamein)
  elseif (! any (strcmpi (fmt, formats)))
    error ("svg2other: allowed output formats are %s.", ...
           strjoin (formats, ", "));
  endif
  
  if (isempty (fnameout))
    [d, name, ext] = fileparts (fnamein);
    fnameout = fullfile (d, [name "." fmt]);
  endif
  
  if (exist (fnameout, "file"))
    tmp = "x";
    tmp = input (sprintf ("%s already exists, do you want to overwrite it? (type \"n\" to abort):", fnameout), "s");
    if (strcmpi (tmp, "n"))
      return
    endif
  endif

  unwind_protect
    jfilei = javaObject ("java.io.File", fnamein);
    jinstream = javaObject ("java.io.FileInputStream", jfilei);
    jfileo = javaObject ("java.io.File", fnameout);
    joutstream = javaObject ("java.io.FileOutputStream", jfileo);
    jtransin = javaObject ("org.apache.batik.transcoder.TranscoderInput", ...
                           jinstream);
    jtransout = javaObject ("org.apache.batik.transcoder.TranscoderOutput", ...
                            joutstream);
    if (strcmpi (fmt, "pdf"))
      jtranscoder = javaObject ("org.apache.fop.svg.PDFTranscoder");
    elseif (strcmpi (fmt, "png"))
      jtranscoder = ...
          javaObject ("org.apache.batik.transcoder.image.PNGTranscoder");
    elseif (strcmpi (fmt, "jpg"))
      ## FIXME: see http://batik.2283329.n4.nabble.com/org-apache-batik-transcoder-TranscoderException-null-td2977034.html
      jtranscoder = ...
          javaObject ("org.apache.batik.transcoder.image.JPEGTranscoder");
      jquality = javaObject ("java.lang.Float", 0.8);
      jtranscoder.addTranscodingHint(jtranscoder.KEY_QUALITY, jquality);
    else
      jtranscoder = javaObject ("org.apache.fop.render.ps.EPSTranscoder");
    endif
    
    jtranscoder.transcode (jtransin, jtransout);
    
  unwind_protect_cleanup
    joutstream.close ();
    jinstream.close ();
  end_unwind_protect
endfunction
