Class: FN::PDF::Writer

Inherits:
Object
  • Object
show all
Includes:
Node
Defined in:
lib/fn/pdf/writer.rb

Constant Summary collapse

SPACE =
/[\s_]+/

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Node

#BeginDocument, #BeginPageExt, #CreateTextflow, #EndPageExt, #FitImage, #FitPdiPage, #FitTextflow, #Invert, #LoadImage, #OpenPdi, #OpenPdiPage, #ResumePage, #SetParameter, #Watermark

Class Method Details

.encodingObject



21
22
23
# File 'lib/fn/pdf/writer.rb', line 21

def self.encoding
  @encoding
end

.encoding=(e) ⇒ Object



17
18
19
# File 'lib/fn/pdf/writer.rb', line 17

def self.encoding=(e)
  @encoding = e
end

Instance Method Details

#calculate(block, dims) ⇒ Object



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
# File 'lib/fn/pdf/writer.rb', line 142

def calculate(block, dims)
  x = bx = block["boxX"].to_i
  y = by = block["boxY"].to_i
  bw = block["boxWidth"].to_i
  bh = block["boxHeight"].to_i
  aw = block["width"].to_i
  iw, ih = dims
  ah =  aw * ih / iw
  case block["align"]
  when /right/
    x = bx + bw - aw
  when /center/
    x = bx + (bw - aw) / 2
  else
    x = bx
  end
  
  case block["align"]
  when /bottom/
    y = by + bh 
  when /middle/
    y = by + bh / 2
  else
    y = by
  end
  
  return [x, y, aw, ah]
end

#titlecase(str) ⇒ Object



36
37
38
# File 'lib/fn/pdf/writer.rb', line 36

def titlecase(str)
  face = str.split(SPACE).map{|s| s.capitalize }.join("")
end

#translate(doc, options = {}) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
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
# File 'lib/fn/pdf/writer.rb', line 40

def translate(doc, options = {})
  raise "Not an FN Document" unless doc.is_a?(FN::Document)
  
  file                    = options[:save_as]
  bkg                     = options[:background]
  pdf_version             = options[:pdf_version] || 1.5
  license                 = options[:license]
  resource_file           = options[:font_list] || File.join(File.dirname(__FILE__), "pdflib.upr")
  self.class.encoding     = options[:encoding] || "unicode"
  textformat              = options[:textformat] || "utf8"
  root                    = options[:resource_root] || ""
  watermark               = options[:watermark]
  
  context = FN::Node::Context.new
  
  context.add SetParameter("license", license)
  context.add SetParameter("resourcefile", resource_file)
  context.add SetParameter("hypertextencoding", self.class.encoding)
  context.add SetParameter("textformat", textformat)
  
  context << OpenPdi(bkg, assigns = "pdi")      if bkg
  context << BeginDocument(file, pdf_version)
  context.add SetParameter("topdown", true)
  
  doc.fonts do |name|
    LoadFont.add_all_variants_to context, name
  end
  
  flows_by_name = {}
  doc.textflows.each do |flow|
    ctf = CreateTextflow(flow)
    flows_by_name[ctf.flow_name] = ctf
    context.add ctf
  end
  
  doc.pages.each do |page|
    context.retain_after do
      context << BeginPageExt(page["width"], page["height"], page["number"])
      if bkg
        context <<  OpenPdiPage("{pdi}", page["number"], assigns = "page" )
        context.add FitPdiPage("{page}")
      elsif page["background"]
        bkg_image = doc.resource(page["background"]).path_from(root)
        context.add LoadImage(bkg_image, "tmp")
        context.add FitImage("{tmp}", 0, page["height"], 
                                 :fitmethod => :meet, 
                                 :boxsize => [page["width"], page["height"]])
      end
      context.add(Watermark(watermark)) if watermark
    end
  end
  
  doc.text_blocks.each do |block|
    flow = CreateTextflow(block)
    
    context.retain_after do
      key = "flow-#{block.text}"
      if flow.empty?
        flow = flows_by_name[key]
      else
        flows_by_name.delete(key)
        context.add(flow)
      end
      
      if flow
        context << ResumePage(block.page_number)
        context << Invert(block) if block["flip"] == "yes"
        context.add FitTextflow(flow, block)
      end
    end
  end
  
  doc.photo_blocks.each do |block|
    image = nil
    begin
      context.inject_at_page(block.page_number) do 
        # image = doc.resource(block.src).path_from(root)
        # tmp = Magick::Image::read(image).first
        # http://stackoverflow.com/questions/7264895/rmagick-can-not-read-remote-image
        image = open(doc.resource(block.src).path_from(root))
        tmp = Magick::Image::from_blob(image.read).first
        dims = [tmp.columns.to_f, tmp.rows.to_f]
        x, y, width, height = calculate(block, dims)
        context.add LoadImage(image, "tmp")
        context.add FitImage("{tmp}", x, y, :fitmethod => "meet",
                                 :boxsize => [width, height])
      end
    rescue Magick::ImageMagickError => e
      $stderr.puts e.message
      $stderr.puts e.backtrace.join("\n")
      raise WriterError.new("Couldn't load remote photo '#{block.src}', given by #{doc.resource(block.src).node}")
    end
  end
  
  doc.pages.each do |page|
    context.add EndPageExt(page["number"])
  end
  
  return context.doc
end

#write(doc, options = {}, debug = false) ⇒ Object



25
26
27
28
# File 'lib/fn/pdf/writer.rb', line 25

def write(doc, options = {}, debug = false)
  options[:save_as] ||= (tmp = Tempfile.new("pdf"); tmp.close; tmp.path)
  write_xml translate(doc, options), options[:save_as], debug
end

#write_xml(xml, file, debug = false) ⇒ Object



30
31
32
33
34
# File 'lib/fn/pdf/writer.rb', line 30

def write_xml(xml, file, debug = false)
  xml.root.extend(FN::Node::Root)
  xml.root.visit(FN::PDF::Struct.new(debug),debug)
  return file
end