Class: FN::SWF::Writer

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

Instance Method Summary collapse

Methods included from Node

#Break, #Flash, #Font, #Frame, #HotSpot, #Image, #Page, #PhotoBlock, #Text

Constructor Details

#initializeWriter

Returns a new instance of Writer.



10
11
# File 'lib/fn/swf/writer.rb', line 10

def initialize
end

Instance Method Details

#image_dimensions(image) ⇒ Object



13
14
15
# File 'lib/fn/swf/writer.rb', line 13

def image_dimensions(image)
  img = `identify "#{image}"`.scan(/ JPEG (\d+)x(\d+)/).flatten.map{|a|a.to_i}
end

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



28
29
30
31
32
33
34
35
36
37
38
39
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
# File 'lib/fn/swf/writer.rb', line 28

def translate(doc, options = {})
  raise "Not an FN Document" unless doc.is_a?(FN::Document)
  
  file          = options[:save_as] or raise "must provide :save_as"
  root          = options[:resource_root]
  
  context = FN::Node::Context.new       
  $depth = 500
  
  page = doc.pages.first
  context << Flash(page[:width], page[:height])
  
  # sorta no-op-ish
  # doc.fonts.each do |font|
  #   context.add Font.load_all_variants(font.key, font.path_from(root))
  # end
  
  global_texts = doc.textflows.inject({}) do |memo, node|
    memo[node["id"]] = node.children.to_s
    memo
  end
  
  context.add Break()
  
  image_dims = {}
  
  doc.images.each do |img|
    if img.complete?
      path = img.path_from(root)
      context.add Image(img.key, path)
      image_dims[img.key] = image_dimensions(path)
    end
  end
  
  context.add Break()
  
  doc.pages.each do |page|
    context.retain_after do
      context.pre HotSpot(page)
      context << Page(page[:number], page[:background])
      
      doc.text_blocks_by(page["number"]).each do |block|
        context.add Text(block, global_texts[block["text"]])
      end
      
      context.add Break()
      
      i = 0
      doc.photo_blocks_by(page["number"]).each do |block|
        i += 1
        context.add PhotoBlock(block, image_dims)
      end
      
      context.add Break()
    end
  end
  
  return context.doc
end

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



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

def write(doc, options = {})
  write_xml translate(doc, options), options[:save_as]
end

#write_xml(xml, file) ⇒ Object



21
22
23
24
25
26
# File 'lib/fn/swf/writer.rb', line 21

def write_xml(xml, file)
  xml.root.extend(FN::Node::Root)
  File.open("#{file}.txt", "w") {|f| f.puts xml.root.visit(FN::SWF::Struct.new).buffer }        
  cmd = %[swfc -o #{file} #{file}.txt]
  system(cmd) ? file : raise("couldn't write swf")
end