~ubuntu-branches/ubuntu/oneiric/ctioga2/oneiric

« back to all changes in this revision

Viewing changes to lib/ctioga2/postprocess.rb

  • Committer: Bazaar Package Importer
  • Author(s): Vincent Fourmond
  • Date: 2011-01-24 21:36:06 UTC
  • Revision ID: james.westby@ubuntu.com-20110124213606-9ettx0ugl83z0bzp
Tags: upstream-0.1
ImportĀ upstreamĀ versionĀ 0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# postprocess.rb: what happens to generated PDF files ?
 
2
# copyright (c) 2009 by Vincent Fourmond
 
3
  
 
4
# This program is free software; you can redistribute it and/or modify
 
5
# it under the terms of the GNU General Public License as published by
 
6
# the Free Software Foundation; either version 2 of the License, or
 
7
# (at your option) any later version.
 
8
  
 
9
# This program is distributed in the hope that it will be useful, but
 
10
# WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
12
# General Public License for more details (in the COPYING file).
 
13
 
 
14
 
 
15
require 'ctioga2/utils'
 
16
require 'ctioga2/log'
 
17
 
 
18
module CTioga2
 
19
 
 
20
  Version::register_svn_info('$Revision: 155 $', '$Date: 2010-06-21 21:41:32 +0200 (Mon, 21 Jun 2010) $')
 
21
 
 
22
  # What happens to generated PDF files ?
 
23
  #
 
24
  # \todo
 
25
  # * handle movie generation ? That would be fun !
 
26
  class PostProcess
 
27
 
 
28
    # Include logging facilities for ctioga2
 
29
    include CTioga2::Log
 
30
 
 
31
    # View all produced files -- or only the last one ?
 
32
    attr_accessor :view_all
 
33
    
 
34
    # The viewer command. If not _nil_, automatically spawn a viewer
 
35
    # after the final figure, or for each produced file if view_all is
 
36
    # on.
 
37
    attr_accessor :viewer
 
38
 
 
39
    # All files processed so far..
 
40
    attr_accessor :processed_files
 
41
 
 
42
    # Are we converting to SVG using pdf2svg ? 
 
43
    attr_accessor :svg
 
44
 
 
45
    # Are we converting to EPS using pdftops ? 
 
46
    attr_accessor :eps
 
47
 
 
48
    # PNG resolution
 
49
    attr_accessor :png_res
 
50
 
 
51
    # PNG oversampling: how many pixels are rendered for one target
 
52
    # linear pixel (take that squared for the real number).
 
53
    attr_accessor :png_oversampling
 
54
 
 
55
    # PNG scale: how many pixels for one postscript point ?
 
56
    attr_accessor :png_scale
 
57
 
 
58
    # Settings up default postprocessing
 
59
    def initialize
 
60
      @view_all = false
 
61
      @viewer = false
 
62
      @svg = false
 
63
 
 
64
      @png_res = nil 
 
65
      @png_oversampling = 2
 
66
      @png_scale = 1
 
67
 
 
68
      @processed_files = []
 
69
    end
 
70
 
 
71
 
 
72
    # Process the given _file_. If _last_ is true, things that should
 
73
    # only happen last happen.
 
74
    def process_file(file, last = false)
 
75
      @processed_files << file
 
76
      # Converts to SVG if applicable
 
77
      if @svg
 
78
        target = file.sub(/(\.pdf)?$/,'.svg')
 
79
        info { "Converting #{file} to SVG" }
 
80
        spawn("pdf2svg #{file} #{target}")
 
81
      end
 
82
 
 
83
      if @eps
 
84
        target = file.sub(/(\.pdf)?$/,'.eps')
 
85
        info { "Converting #{file} to EPS" }
 
86
        ## \todo provide some facility to pass options to pdftops ?
 
87
        spawn("pdftops -eps -level2 -paper match #{file} #{target}")
 
88
      end
 
89
 
 
90
      # Converts to PNG if applicable
 
91
      if @png_res
 
92
        target = file.sub(/(\.pdf)?$/,'.png')
 
93
        info { "Converting #{file} to PNG" }
 
94
        spawn "convert -density #{(@png_oversampling * @png_scale * 72).to_i} #{file} -resize #{@png_res.join('x')} #{target}"
 
95
      end
 
96
 
 
97
      # View produced PDF or PNG files...
 
98
      if (last || @view_all) && @viewer
 
99
        if @png_res
 
100
          cmd = "display #{target}"
 
101
        elsif @viewer =~ /%s/
 
102
          cmd = @viewer % file
 
103
        else
 
104
          cmd = "#{@viewer} #{file}"
 
105
        end
 
106
        info { "Spawning the viewer as requested for #{file}" }
 
107
        spawn(cmd)
 
108
      end
 
109
    end
 
110
 
 
111
 
 
112
  end
 
113
 
 
114
end
 
115