Kouhei Sutou
kous****@users*****
Fri May 19 18:07:49 JST 2006
Index: kazehakase/data/ext/ruby/kz/actions/Makefile.am diff -u kazehakase/data/ext/ruby/kz/actions/Makefile.am:1.3 kazehakase/data/ext/ruby/kz/actions/Makefile.am:1.4 --- kazehakase/data/ext/ruby/kz/actions/Makefile.am:1.3 Sat Apr 29 12:14:12 2006 +++ kazehakase/data/ext/ruby/kz/actions/Makefile.am Fri May 19 18:07:48 2006 @@ -7,6 +7,7 @@ reload-ruby.rb \ reload-without-fragment.rb \ ruby-dialog.rb \ + view-pdf-as-image.rb \ text-link.rb EXTRA_DIST = \ Index: kazehakase/data/ext/ruby/kz/actions/view-pdf-as-image.rb diff -u /dev/null kazehakase/data/ext/ruby/kz/actions/view-pdf-as-image.rb:1.1 --- /dev/null Fri May 19 18:07:49 2006 +++ kazehakase/data/ext/ruby/kz/actions/view-pdf-as-image.rb Fri May 19 18:07:48 2006 @@ -0,0 +1,123 @@ +require 'erb' +require 'fileutils' + +begin + require "poppler" +rescue LoadError +end + +def h(str) + ERB::Util.h(str) +end + +def image_type + "png" +end + +def image_suffix + "png" +end + +def image_name(i) + "#{i}.#{image_suffix}" +end + +def thumbnail_name(i) + "#{i}-thumb.#{image_suffix}" +end + +def ruby_poppler_is_unavailable(action, kz) + statusbar_timeout = 3 * 1000 + statusbar_id = kz.statusbar.get_context_id(action.name) + kz.statusbar.push(statusbar_id, _("need to install Ruby/Poppler")) + Kz.pop_statusbar(kz, statusbar_id, statusbar_timeout) +end + +def start_downloader(kz, uri) + tmp_dir = "/tmp/kazehakase" + FileUtils.mkdir_p(tmp_dir) + Kz.add_exit_proc {FileUtils.rm_rf(tmp_dir)} + filename = File.join(tmp_dir, File.basename(uri)) + dl = Kz::Downloader.new(uri, filename) + dl.signal_connect("completed") do + Kz.barrier do + completed(kz, tmp_dir, filename) + end + end + Kz::DownloaderGroup.add(dl) + dl.to_file +end + +def make_images(kz, pages, tmp_dir, &block) + scale = 0.1 + Gtk.idle_add do + Kz.barrier do + page, i = pages.shift + width, height = page.size + pixbuf_width, pixbuf_height = page.size.collect {|x| x * scale} + + pixbuf = Gdk::Pixbuf.new(Gdk::Pixbuf::COLORSPACE_RGB, true, 8, + width, height) + page.render_to_pixbuf(0, 0, width, height, 1, 0, pixbuf) + output = File.join(tmp_dir, image_name(i)) + pixbuf.save(output, image_type) + + pixbuf = Gdk::Pixbuf.new(Gdk::Pixbuf::COLORSPACE_RGB, true, 8, + pixbuf_width, pixbuf_height) + page.render_to_pixbuf(0, 0, width, height, scale, 0, pixbuf) + thumbnail = File.join(tmp_dir, thumbnail_name(i)) + pixbuf.save(thumbnail, image_type) + + pixbuf = nil + GC.start + + if pages.empty? + tab = block.call + tab.reload(Kz::EmbedReloadFlag::BYPASS_CACHE) + tab = nil + GC.start + false + else + true + end + end + end +end + +def completed(kz, tmp_dir, filename) + doc = Poppler::Document.new("file://#{filename}") + tab = nil + scale = 0.1 + pages = [] + doc.each_with_index do |page, i| + pages.push([page, i]) + end + + make_images(kz, pages, tmp_dir) {tab} + + html_file = File.join(tmp_dir, "index.html") + File.open(html_file, "w") do |f| + f << "<html><head><title>#{h doc.title}</title></head><body>\n" + doc.n_pages.times do |i| + f << "<a href=\"#{h image_name(i)}\">\n" + f << "<img src=\"#{h thumbnail_name(i)}\">\n" + f << "</a>\n" + end + f << "\n</body></html>" + end + tab = kz.open_new_tab("file://#{html_file}") +end + +def act_view_pdf_as_image(action, group, kz) + unless ::Object.const_defined?(:Poppler) + ruby_poppler_is_unavailable(action, kz) + return + end + return unless kz.current_page + + uri = nil + uri = kz.mouse_event_info.link if kz.mouse_event_info + return if uri.nil? + + start_downloader(kz, uri) +end