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 = 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
|