From: Kouhei S. <nu...@co...> - 2017-07-23 06:05:30
|
Kouhei Sutou 2017-07-23 15:04:44 +0900 (Sun, 23 Jul 2017) New Revision: 9d768bd517387107fd303d74d12b4ebc52ba89b1 https://github.com/ruby-gnome2/ruby-gnome2/commit/9d768bd517387107fd303d74d12b4ebc52ba89b1 Message: poppler: support Poppler::Document.new(path) Modified files: poppler/lib/poppler/document.rb Modified: poppler/lib/poppler/document.rb (+62 -5) =================================================================== --- poppler/lib/poppler/document.rb 2017-07-23 15:04:22 +0900 (ebcad8f) +++ poppler/lib/poppler/document.rb 2017-07-23 15:04:44 +0900 (0b635b2) @@ -16,12 +16,69 @@ module Poppler class Document - # TODO : - # new_from_file - # new_from_data - # new_from_gstream - # new_from_gfile (should we ?) + alias_method :initialize_raw, :initialize + def initialize(*args) + if args.size == 1 and args[0].is_a?(Hash) + options = args[0] + data = options[:data] + uri = options[:uri] + path = options[:path] + stream = options[:stream] + length = options[:length] + file = options[:file] + password = options[:password] + + if data + initialize_new_from_data(data, password) + elsif uri + initialize_new_from_file(uri, password) + elsif path + uri = ensure_uri(path) + initialize_new_from_file(uri, password) + elsif stream + if length.nil? + raise(ArgumentError, + "must specify :length for :stream: #{options.inspect}") + end + initialize_new_from_stream(stream, length, password) + elsif file + if file.is_a?(String) + initialize(path: file, password: password) + else + initialize_new_from_gfile(file, password) + end + else + message = + "must specify one of :data, :uri, :path, :stream or :file: " + + options.inspect + raise(ArgumentError, message) + end + else + uri_or_data, password = args + if pdf_data?(uri_or_data) + initialize_new_from_data(uri_or_data, password) + else + uri = ensure_uri(uri_or_data) + initialize_new_from_file(uri, password) + end + end + end alias_method :[], :get_page + + private + def pdf_data?(data) + data.start_with?("%PDF-1.") + end + + def ensure_uri(uri) + if GLib.path_is_absolute?(uri) + GLib.filename_to_uri(uri) + elsif /\A[a-zA-Z][a-zA-Z\d\-+.]*:/.match(uri) + uri + else + GLib.filename_to_uri(File.expand_path(uri)) + end + end end end |