Kouhei Sutou
null+****@clear*****
Thu Jul 6 15:26:07 JST 2017
Kouhei Sutou 2017-07-06 15:26:07 +0900 (Thu, 06 Jul 2017) New Revision: 6109e6501f9d765c4204080aa68d7233e88d311e https://github.com/ranguba/chupa-text-decomposer-pdf/commit/6109e6501f9d765c4204080aa68d7233e88d311e Message: Support screenshot Added files: test/fixture/screenshot.odt test/fixture/screenshot.pdf test/fixture/screenshot.png Modified files: chupa-text-decomposer-pdf.gemspec lib/chupa-text/decomposers/pdf.rb test/test-pdf.rb Modified: chupa-text-decomposer-pdf.gemspec (+3 -2) =================================================================== --- chupa-text-decomposer-pdf.gemspec 2017-07-06 15:15:47 +0900 (2ecb9e8) +++ chupa-text-decomposer-pdf.gemspec 2017-07-06 15:26:07 +0900 (6499cb2) @@ -39,12 +39,13 @@ Gem::Specification.new do |spec| spec.files += Dir.glob("doc/text/*") spec.files += Dir.glob("test/**/*") - spec.add_runtime_dependency("chupa-text") + spec.add_runtime_dependency("chupa-text", ">= 1.0.7") spec.add_runtime_dependency("poppler") spec.add_development_dependency("bundler") spec.add_development_dependency("rake") spec.add_development_dependency("test-unit") spec.add_development_dependency("packnga") - spec.add_development_dependency("redcarpet") + spec.add_development_dependency("kramdown") + spec.add_development_dependency("gdk_pixbuf2") end Modified: lib/chupa-text/decomposers/pdf.rb (+34 -1) =================================================================== --- lib/chupa-text/decomposers/pdf.rb 2017-07-06 15:15:47 +0900 (e2e6403) +++ lib/chupa-text/decomposers/pdf.rb 2017-07-06 15:26:07 +0900 (1d374ab) @@ -1,4 +1,4 @@ -# Copyright (C) 2013-2014 Kouhei Sutou <kou �� clear-code.com> +# Copyright (C) 2013-2017 Kouhei Sutou <kou �� clear-code.com> # # This library is free software; you can redistribute it and/or # modify it under the terms of the GNU Lesser General Public @@ -53,6 +53,9 @@ module ChupaText add_attribute(text_data, document, :creator) add_attribute(text_data, document, :producer) add_attribute(text_data, document, :creation_date, :created_time) + if data.need_screenshot? + text_data.screenshot = create_screenshot(data, document) + end yield(text_data) end @@ -99,6 +102,36 @@ module ChupaText data_attribute_name ||= pdf_attribute_name.to_s.gsub(/_/, "-") text_data[data_attribute_name] = value end + + def create_screenshot(data, document) + screenshot_width, screenshot_height = data.expected_screenshot_size + + page = document[0] + page_width, page_height = page.size + + surface = Cairo::ImageSurface.new(:argb32, + screenshot_width, + screenshot_height) + context = Cairo::Context.new(surface) + context.set_source_color(:white) + context.paint + if page_width > page_height + ratio = screenshot_width / page_width + context.translate(0, + ((screenshot_height - page_height * ratio) / 2)) + context.scale(ratio, ratio) + else + ratio = screenshot_height / page_height + context.translate(((screenshot_width - page_width * ratio) / 2), + 0) + context.scale(ratio, ratio) + end + context.render_poppler_page(page) + png = StringIO.new + surface.write_to_png(png) + + Screenshot.new("image/png", [png.string].pack("m*"), "base64") + end end end end Added: test/fixture/screenshot.odt (+0 -0) 100644 =================================================================== (Binary files differ) Added: test/fixture/screenshot.pdf (+0 -0) 100644 =================================================================== (Binary files differ) Added: test/fixture/screenshot.png (+0 -0) 100644 =================================================================== (Binary files differ) Modified: test/test-pdf.rb (+39 -0) =================================================================== --- test/test-pdf.rb 2017-07-06 15:15:47 +0900 (99b3ba1) +++ test/test-pdf.rb 2017-07-06 15:26:07 +0900 (7283e4e) @@ -15,6 +15,7 @@ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA require "pathname" +require "gdk_pixbuf2" class TestPDF < Test::Unit::TestCase def setup @@ -168,5 +169,43 @@ class TestPDF < Test::Unit::TestCase super(fixture_path("encrypted.pdf")) end end + + sub_test_case("screenshot") do + def test_with_password + assert_equal([ + { + "mime-type" => "image/png", + "pixels" => load_image_fixture("screenshot.png"), + "encoding" => "base64", + }, + ], + decompose("screenshot.pdf")) + end + + private + def decompose(fixture_name) + super(fixture_path(fixture_name)).collect do |decompose| + screenshot = decompose.screenshot + { + "mime-type" => screenshot.mime_type, + "pixels" => load_image_data(screenshot.decoded_data), + "encoding" => screenshot.encoding, + } + end + end + + def load_image_data(data) + loader = GdkPixbuf::PixbufLoader.new + loader.write(data) + loader.close + loader.pixbuf.pixels + end + + def load_image_fixture(fixture_name) + File.open(fixture_path(fixture_name), "rb") do |file| + load_image_data(file.read) + end + end + end end end -------------- next part -------------- HTML����������������������������... Télécharger