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, debug, error, fatal, #format_exception, #identify, info, init_logger, log_to, logger, set_level, #spawn, warn

Constructor Details

#initializePostProcess

Settings up default postprocessing



64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/ctioga2/postprocess.rb', line 64

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

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

  @processed_files = []
end

Instance Attribute Details

#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).



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

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)



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

def png_pdftoppm
  @png_pdftoppm
end

#png_resObject

PNG resolution



54
55
56
# File 'lib/ctioga2/postprocess.rb', line 54

def png_res
  @png_res
end

#png_scaleObject

PNG scale: how many pixels for one postscript point ?



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

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.



107
108
109
110
111
112
113
114
115
116
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
# File 'lib/ctioga2/postprocess.rb', line 107

def process_file(file, last = false)
  @processed_files << file
  # 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



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/ctioga2/postprocess.rb', line 79

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