X-Git-Url: https://git.creatis.insa-lyon.fr/pubgit/?p=CreaPhase.git;a=blobdiff_plain;f=octave_packages%2Fm%2Fplot%2Fprivate%2F__tight_eps_bbox__.m;fp=octave_packages%2Fm%2Fplot%2Fprivate%2F__tight_eps_bbox__.m;h=e93f21ae6ad1c1686da340ad7b2fb39df5497e63;hp=0000000000000000000000000000000000000000;hb=1c0469ada9531828709108a4882a751d2816994a;hpb=63de9f36673d49121015e3695f2c336ea92bc278 diff --git a/octave_packages/m/plot/private/__tight_eps_bbox__.m b/octave_packages/m/plot/private/__tight_eps_bbox__.m new file mode 100644 index 0000000..e93f21a --- /dev/null +++ b/octave_packages/m/plot/private/__tight_eps_bbox__.m @@ -0,0 +1,124 @@ +## Copyright (C) 2010-2012 Ben Abbott +## +## This file is part of Octave. +## +## Octave 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. +## +## Octave 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 Octave; see the file COPYING. If not, see +## . + +## -*- texinfo -*- +## @deftypefn {Function File} {@var{bbox} =} __tight_eps_bbox__ (@var{@dots{}}) +## Undocumented internal function. +## @end deftypefn + +## Author: Ben Abbott +## Created: 2010-07-26 + +function bb = __tight_eps_bbox__ (opts, eps_file_name) + + box_string = "%%BoundingBox:"; + + cmd = sprintf ("\"%s\" \"%s\" 2>&1", "head", eps_file_name); + [status, output] = system (cmd); + + if (status == 0) + orig_bbox_line = get_bbox (output); + else + error ("print:noboundingbox", + "print.m: no bounding box found in '%s'", eps_file_name); + endif + + ghostscript_options = "-q -dBATCH -dSAFER -dNOPAUSE -dTextAlphaBits=4 -sDEVICE=bbox"; + cmd = sprintf ("\"%s\" %s \"%s\" 2>&1", opts.ghostscript.binary, + ghostscript_options, eps_file_name); + [status, output] = system (cmd); + + if (status == 0) + tight_bbox_line = get_bbox (output); + else + warning ("print:nogsboundingbox", + "print.m: ghostscript failed to determine the bounding for '%s'", + eps_file_name); + endif + + ## Attempt to fix the bbox in place. + fid = fopen (eps_file_name, "r+"); + unwind_protect + bbox_replaced = false; + looking_for_bbox = true; + while (looking_for_bbox) + current_line = fgetl (fid); + if (strncmpi (current_line, box_string, numel(box_string))) + line_length = numel (current_line); + num_spaces = line_length - numel (tight_bbox_line); + if (numel (current_line) >= numel (tight_bbox_line)) + new_line = tight_bbox_line; + new_line(end+1:numel(current_line)) = " "; + bbox_replaced = true; + ## Back up to the beginning of the line (include EOL characters). + if (ispc ()) + fseek (fid, -line_length-2, "cof"); + else + fseek (fid, -line_length-1, "cof"); + endif + count = fprintf (fid, "%s", new_line); + endif + looking_for_bbox = false; + elseif (! ischar (current_line)) + looking_for_bbox = false; + endif + endwhile + unwind_protect_cleanup + fclose (fid); + end_unwind_protect + + ## If necessary load the eps-file and replace the bbox (can be slow). + if (! bbox_replaced) + fid = fopen (eps_file_name, "r"); + unwind_protect + data = char (fread (fid, Inf)).'; + unwind_protect_cleanup + fclose (fid); + end_unwind_protect + n = strfind (data, box_string); + if (numel (n) > 1) + ## Only replace one instance. + n = n(1); + elseif (isempty (n)) + error ("print:noboundingbox", ... + "print.m: no bounding box found in '%s'.", eps_file_name); + endif + m = numel (orig_bbox_line); + data = horzcat (data(1:(n-1)), tight_bbox_line, data((n+m):end)); + fid = fopen (eps_file_name, "w"); + unwind_protect + fprintf (fid, "%s", data); + unwind_protect_cleanup + fclose (fid); + end_unwind_protect + endif + +endfunction + +function bbox_line = get_bbox (lines) + box_string = "%%BoundingBox:"; + pattern = strcat (box_string, "[^%]*"); + pattern = pattern(1:find(double(pattern)>32, 1, "last")); + bbox_line = regexp (lines, pattern, "match"); + if (iscell (bbox_line)) + bbox_line = bbox_line{1}; + endif + ## Remove the EOL characters. + bbox_line(double(bbox_line)<32) = ""; +endfunction +