added spec for pdf2swf url conversion
authorMatthias Kramm <kramm@quiss.org>
Thu, 17 Sep 2009 11:36:10 +0000 (13:36 +0200)
committerMatthias Kramm <kramm@quiss.org>
Thu, 17 Sep 2009 11:36:27 +0000 (13:36 +0200)
spec/links.pdf [new file with mode: 0644]
spec/links.py [new file with mode: 0644]
spec/links.spec.rb [new file with mode: 0644]
spec/spec_helper.rb

diff --git a/spec/links.pdf b/spec/links.pdf
new file mode 100644 (file)
index 0000000..230970a
Binary files /dev/null and b/spec/links.pdf differ
diff --git a/spec/links.py b/spec/links.py
new file mode 100644 (file)
index 0000000..39e44cd
--- /dev/null
@@ -0,0 +1,26 @@
+from sys import * 
+from pdflib_py import * 
+p = PDF_new() 
+PDF_open_file(p, "links.pdf")
+
+PDF_set_parameter(p, "usercoordinates", "true")
+PDF_set_info(p, "Creator", "links.py")
+
+width=200
+height=200
+
+PDF_begin_page(p, width, height)
+def draw_link(x1,y1,x2,y2,url):
+    action = PDF_create_action(p, "URI", "url="+url);
+    optlist = "action={activate "+str(action)+"} linewidth=5"
+    PDF_create_annotation(p, x1,y1,x2,y2, "Link", optlist);
+draw_link(0, 0, 100, 100, "http://www.swftools.org")
+draw_link(0, 100, 100, 200, "http://www.quiss.org")
+PDF_end_page(p)
+
+PDF_begin_page(p, width, height)
+draw_link(0, 0, 100, 100, "http://www.pdf2swf.org")
+PDF_end_page(p)
+
+PDF_close(p)
+PDF_delete(p);
diff --git a/spec/links.spec.rb b/spec/links.spec.rb
new file mode 100644 (file)
index 0000000..25e754d
--- /dev/null
@@ -0,0 +1,9 @@
+require File.dirname(__FILE__) + '/spec_helper'
+
+describe "pdf conversion" do
+    convert_file "links.pdf" do
+        area_at(0,0,200,200).should_contain_link("http://www.swftools.org")
+        area_at(0,0,200,200).should_contain_link("http://www.pdf2swf.org")
+        area_at(0,0,200,200).should_contain_link("http://www.quiss.org")
+    end
+end
index 1727b0f..876f1d4 100644 (file)
@@ -56,6 +56,10 @@ class Area
        text2 = @file.get_text(@x1,@y1,@x2,@y2) 
        text2 == text or raise AreaError.new(self, "doesn't contain text \"#{text}\" (found: \"#{text2}\")")
     end
+    def should_contain_link(url)
+       links = @file.get_links(@x1,@y1,@x2,@y2) 
+       (links & [url]) or raise AreaError.new(self, "doesn't contain url \"#{url}\")
+    end
     def to_s
        "(#{@x1},#{@y1},#{@x2},#{@y2})"
     end
@@ -134,6 +138,10 @@ class DocFile
        #puts `swfstrings -x #{x1} -y #{y1} -W #{x2-x1} -H #{y2-y1} #{@swfname}`
        `swfstrings -x #{x1} -y #{y1} -W #{x2-x1} -H #{y2-y1} #{@swfname}`.chomp
     end
+    def get_links(x1,y1,x2,y2)
+       self.convert()
+       `swfdump -a #{@swfname}`.scan(/GetUrl2? URL:"([^"]*)"/).inject([]) do |a,u| a + u end
+    end
     def get_area(x1,y1,x2,y2)
        self.render()
        data = @img.export_pixels(x1, y1, x2-x1, y2-y1, "RGB")