Class: CTioga2::PostProcess

Inherits:
Object
  • Object
show all
Includes:
Log
Defined in:
lib/ctioga2/postprocess.rb

Overview

What happens to generated PDF files ?

todo

  • handle movie generation ? That would be fun !

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Log

context, counts, debug, error, fatal, #format_exception, #identify, info, init_logger, log_to, logger, set_level, #spawn, warn

Constructor Details

#initializePostProcess

Settings up default postprocessing



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/ctioga2/postprocess.rb', line 71

def initialize
  @view_all = false
  @viewer = false
  @svg = false

  @png_res = nil 
  @png_oversampling = 2
  @png_scale = 1
  @png_pdftoppm = false

  @processed_files = []

  gs = Utils::which('gs')
  @cleanup_pdf = (gs ? true : false)
end

Instance Attribute Details

#cleanup_pdfObject

TODO:

Path to gs…

Are we cleaning up the PDF produced using gs (in particular, to include missing markers and such, that are known to cause problems from time to time).



51
52
53
# File 'lib/ctioga2/postprocess.rb', line 51

def cleanup_pdf
  @cleanup_pdf
end

#epsObject

Are we converting to EPS using pdftops ?



44
45
46
# File 'lib/ctioga2/postprocess.rb', line 44

def eps
  @eps
end

#png_oversamplingObject

PNG oversampling: how many pixels are rendered for one target linear pixel (take that squared for the real number).



65
66
67
# File 'lib/ctioga2/postprocess.rb', line 65

def png_oversampling
  @png_oversampling
end

#png_pdftoppmObject

If on, we use pdftoppm rather than imagemagick (gs, used by pdftoppm is much slower than pdftoppm)



58
59
60
# File 'lib/ctioga2/postprocess.rb', line 58

def png_pdftoppm
  @png_pdftoppm
end

#png_resObject

PNG resolution



61
62
63
# File 'lib/ctioga2/postprocess.rb', line 61

def png_res
  @png_res
end

#png_scaleObject

PNG scale: how many pixels for one postscript point ?



68
69
70
# File 'lib/ctioga2/postprocess.rb', line 68

def png_scale
  @png_scale
end

#processed_filesObject

All files processed so far..



38
39
40
# File 'lib/ctioga2/postprocess.rb', line 38

def processed_files
  @processed_files
end

#svgObject

Are we converting to SVG using pdf2svg ?



41
42
43
# File 'lib/ctioga2/postprocess.rb', line 41

def svg
  @svg
end

#view_allObject

View all produced files – or only the last one ?



30
31
32
# File 'lib/ctioga2/postprocess.rb', line 30

def view_all
  @view_all
end

#viewerObject

The viewer command. If not nil, automatically spawn a viewer after the final figure, or for each produced file if view_all is on.



35
36
37
# File 'lib/ctioga2/postprocess.rb', line 35

def viewer
  @viewer
end

Instance Method Details

#process_file(file, last = false) ⇒ Object

Process the given file. If last is true, things that should only happen last happen.



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/ctioga2/postprocess.rb', line 117

def process_file(file, last = false)
  @processed_files << file


  if @cleanup_pdf
    nw_src = file.sub(/(\.pdf)?$/,'.raw.pdf')
    begin
      File::rename(file, nw_src)
      info { "Running gs to clean up the target PDF file: '#{file}'" }
      if ! system('gs', "-sOutputFile=#{file}", "-q", "-sDEVICE=pdfwrite",
                  "-dCompatibilityLevel=1.4", "-dNOPAUSE", "-dAutoRotatePages=/None", "-dBATCH", nw_src)
        error { "Failed to run gs to cleanup '#{nw_src}', you can disable that using --no-cleanup-pdf" }
      else
        File::unlink(nw_src)
      end
    rescue SystemCallError => e
      error { "Could not rename '#{file}' to '#{nw_src}': #{e.message}, try using --no-cleanup-pdf, or resolve the problem otherwise" }
    end
  end
  
  # Converts to SVG if applicable
  if @svg
    target = file.sub(/(\.pdf)?$/,'.svg')
    info { "Converting #{file} to SVG" }
    spawn("pdf2svg #{file} #{target}")
  end

  if @eps
    target = file.sub(/(\.pdf)?$/,'.eps')
    info { "Converting #{file} to EPS" }
    ## \todo provide some facility to pass options to pdftops ?
    spawn("pdftops -eps -level2 -paper match #{file} #{target}")
  end

  # Converts to PNG if applicable
  if @png_res
    tbase = file.sub(/(\.pdf)?$/,'')
    info { "Converting #{file} to PNG" }
    
    if @png_pdftoppm
      spawn "pdftoppm -singlefile -png -r #{(@png_scale * 72).to_i} #{file} #{tbase}"
    else
      spawn "convert -density #{(@png_oversampling * @png_scale * 72).to_i} #{file} -resize #{@png_res.join('x')} #{tbase}.png"
    end
  end

  # View produced PDF or PNG files...
  if (last || @view_all) && @viewer
    if @viewer == :auto
      view_pdf(file)
    else
      if @png_res
        cmd = "display #{target}"
      elsif @viewer =~ /%s/
        cmd = @viewer % file
      else
        cmd = "#{@viewer} #{file}"
      end
      info { "Spawning the viewer as requested for #{file}" }
      spawn(cmd)
    end
  end
end

#view_pdf(pdf) ⇒ Object

Try to open the file with xpdf, or fallback to system defaults



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/ctioga2/postprocess.rb', line 89

def view_pdf(pdf)
  if Utils.which("xpdf")
    spawn(["xpdf", "-z", "page", pdf])
  else
    case Utils.os
    when :windows
      # Use start
      spawn(["start", "/B", pdf])
    when :macosx
      spawn(["open", pdf])
    else
      for w in %w{evince gv mimeopen}
        if Utils.which(w)
          if w == "mimeopen"
            spawn(["mimeopen", "-n", pdf])
          else
            spawn([w, pdf])
          end
          break
        end
      end
    end
  end
end