gubbins-shell 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (49) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +19 -0
  3. data/LICENSE +289 -0
  4. data/README.md +186 -0
  5. data/lib/gubbins/shell.rb +4 -0
  6. data/lib/shell/all.rb +34 -0
  7. data/lib/shell/base_64.rb +40 -0
  8. data/lib/shell/clam_av.rb +68 -0
  9. data/lib/shell/command.rb +1400 -0
  10. data/lib/shell/convert.rb +312 -0
  11. data/lib/shell/errors.rb +89 -0
  12. data/lib/shell/exiftool.rb +15 -0
  13. data/lib/shell/file.rb +84 -0
  14. data/lib/shell/ghostscript.rb +239 -0
  15. data/lib/shell/identify.rb +111 -0
  16. data/lib/shell/img2_pdf.rb +47 -0
  17. data/lib/shell/magick.rb +111 -0
  18. data/lib/shell/open_ssl.rb +26 -0
  19. data/lib/shell/optional_dependencies.rb +36 -0
  20. data/lib/shell/path.rb +40 -0
  21. data/lib/shell/pdf_detach.rb +86 -0
  22. data/lib/shell/pdffonts.rb +73 -0
  23. data/lib/shell/pdfimages.rb +48 -0
  24. data/lib/shell/pdfinfo.rb +79 -0
  25. data/lib/shell/pdftocairo.rb +24 -0
  26. data/lib/shell/pdftotext.rb +32 -0
  27. data/lib/shell/ping.rb +50 -0
  28. data/lib/shell/power_array.rb +42 -0
  29. data/lib/shell/seven_zip.rb +142 -0
  30. data/lib/shell/tesseract.rb +120 -0
  31. data/lib/shell/testing/mock/shell/clam_av.rb +12 -0
  32. data/lib/shell/testing/mock/shell/command.rb +241 -0
  33. data/lib/shell/testing/mock/shell/configurator.rb +223 -0
  34. data/lib/shell/testing/mock/shell/convert.rb +12 -0
  35. data/lib/shell/testing/mock/shell/file.rb +12 -0
  36. data/lib/shell/testing/mock/shell/ghostscript.rb +13 -0
  37. data/lib/shell/testing/mock/shell/identify.rb +11 -0
  38. data/lib/shell/testing/mock/shell/open_ssl.rb +11 -0
  39. data/lib/shell/testing/mock/shell/tesseract.rb +12 -0
  40. data/lib/shell/testing/mock/shell/unzip.rb +20 -0
  41. data/lib/shell/testing/mock/shell/zip.rb +11 -0
  42. data/lib/shell/testing.rb +15 -0
  43. data/lib/shell/tiff_2_pdf.rb +19 -0
  44. data/lib/shell/unzip.rb +137 -0
  45. data/lib/shell/version.rb +5 -0
  46. data/lib/shell/zbar.rb +123 -0
  47. data/lib/shell/zip.rb +52 -0
  48. data/lib/shell.rb +16 -0
  49. metadata +116 -0
@@ -0,0 +1,86 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../shell"
4
+
5
+ module Shell
6
+ # Interface for `pdfdetach`
7
+ class PDFDetach < Command
8
+ command_path `which pdfdetach`
9
+ option_prefix "-"
10
+ positional_parameters :last
11
+
12
+ # Extracts embedded files from `input` to a temporary directory
13
+ #
14
+ # When called with a block, the temporary directory is automatically
15
+ # removed after the block returns; your block receives a {Fs::Directory}
16
+ # to the directory.
17
+ #
18
+ # @example
19
+ # ```ruby
20
+ # Shell::PDFDetach.to_temp("/some/collection.pdf") do |path|
21
+ # puts path.children
22
+ # end
23
+ # ```
24
+ #
25
+ # When called without a block, the {Pathname} is returned in
26
+ # {Result#value}.
27
+ #
28
+ # ```ruby
29
+ # result = Shell::PDFDetach.to_temp("/some/collection.pdf")
30
+ # puts result.value.children
31
+ # ```
32
+ #
33
+ # @param input [Pathname, Fs::File, String]
34
+ # @yieldparam temp_dir [Fs::Directory] an `Fs::Directory` instance for the temp dir
35
+ # @raise [Shell::Errors::DependencyMissing] if the gubbins-fs gem is not installed
36
+ # @return [Shell::Command::Result]
37
+ # `Result#value` [Fs::Directory] an `Fs::Directory` instance for the temp dir; only
38
+ # when called without a block
39
+ def self.to_temp(input, &block)
40
+ require_optional "gubbins/fs", gem_name: "gubbins-fs"
41
+
42
+ result = nil
43
+
44
+ if block_given?
45
+ Fs::Directory.temp_dir_for do |temp_dir|
46
+ result =
47
+ exec(input, :saveall, o: temp_dir).on_success do
48
+ yield temp_dir
49
+ end
50
+ end
51
+
52
+ else
53
+ temp_dir = Fs::Directory.temp_dir_for
54
+ result = exec(input, :saveall, o: temp_dir)
55
+ result.value = temp_dir
56
+ end
57
+
58
+ result
59
+ end
60
+
61
+ # Extracts embedded files from `input` to `output` directory
62
+ #
63
+ # @param input [Pathname, Fs::File, String]
64
+ # @param output [Pathname, Fs::File, String]
65
+ # @return [Shell::Command::Result]
66
+ def self.to(input:, output:)
67
+ exec(input, :saveall, o: output)
68
+ end
69
+
70
+ # Returns the filenames embedded in the PDF collection as {Pathname} objects
71
+ #
72
+ # `pdfdetach -list` output format: "1: filename.pdf\n2: other.pdf\n"
73
+ #
74
+ # @param input [Pathname, Fs::File, String]
75
+ # @return [Shell::Command::Result]
76
+ # `Result#value` [Array<Pathname>]
77
+ def self.list(input)
78
+ exec(input, :list).on_success do |result|
79
+ result.value =
80
+ result.stdout
81
+ .lines
82
+ .map { |line| Pathname.new(line.split(": ", 2).last.strip) }
83
+ end
84
+ end
85
+ end
86
+ end
@@ -0,0 +1,73 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../shell"
4
+
5
+ module Shell
6
+ # Interface for `pdffonts`
7
+ class Pdffonts < Command
8
+ command_path `which pdffonts`
9
+
10
+ # The columns of a font listing line which we are interested in: everything from "encoding" up to
11
+ # the object ID. "name" and "type" may contain spaces, so we anchor on the end of the line instead.
12
+ FONT_LINE = /\s(?<encoding>\S+)\s+(?<embedded>yes|no)\s+(yes|no)\s+(?<unicode>yes|no)\s+\d+\s+\d+\s*$/
13
+
14
+ # A font used by a PDF
15
+ #
16
+ # @!attribute [r] encoding
17
+ # @return [String] eg. "WinAnsi", or "Identity-H"
18
+ # @!attribute [r] embedded
19
+ # @return [Boolean] does the PDF carry the font's glyphs?
20
+ # @!attribute [r] unicode_mapped
21
+ # @return [Boolean] does the PDF map the font's character codes to Unicode (`ToUnicode`)?
22
+ Font = Data.define(:encoding, :embedded, :unicode_mapped) do
23
+ alias_method :embedded?, :embedded
24
+ alias_method :unicode_mapped?, :unicode_mapped
25
+
26
+ # Are the font's character codes indices into the glyph table of the original font file?
27
+ #
28
+ # @return [Boolean]
29
+ def glyph_indexed?
30
+ encoding.start_with?("Identity-")
31
+ end
32
+
33
+ # Is this a glyph-indexed font whose glyphs the PDF does not carry?
34
+ #
35
+ # A renderer which has to substitute another font cannot know which glyphs those indices
36
+ # mean, and draws whatever has those indices in the substitute (Ghostscript falls back to a
37
+ # CJK font and draws Hangul). Only {#unicode_mapped?} fonts can be told apart from that.
38
+ #
39
+ # @return [Boolean]
40
+ def glyphs_missing?
41
+ glyph_indexed? && !embedded?
42
+ end
43
+ end
44
+
45
+ # The fonts the PDF at `input` uses
46
+ #
47
+ # @param input [Pathname, String, Fs::File]
48
+ # @return [Shell::Command::Result]
49
+ # `Result#value` [Array<Font>]
50
+ def self.list(input)
51
+ exec(input).on_success do |result|
52
+ result.value =
53
+ result.stdout
54
+ .scrub
55
+ .lines
56
+ .filter_map { |line| font_from(line) }
57
+ end
58
+ end
59
+
60
+ # @param line [String] a line of the font listing
61
+ # @return [Font, nil] `nil` for the header, and anything else which is not a font
62
+ def self.font_from(line)
63
+ match = line.match(FONT_LINE)
64
+
65
+ return unless match
66
+
67
+ Font.new(encoding: match[:encoding],
68
+ embedded: match[:embedded] == "yes",
69
+ unicode_mapped: match[:unicode] == "yes")
70
+ end
71
+ private_class_method :font_from
72
+ end
73
+ end
@@ -0,0 +1,48 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../shell"
4
+ require_relative "power_array"
5
+
6
+ module Shell
7
+ class Pdfimages < Command
8
+ using Shell::PowerArray
9
+
10
+ command_path `which pdfimages`
11
+ option_prefix "-"
12
+
13
+ # Get the average and median pixels per inch over all
14
+ # pages of a PDF
15
+ #
16
+ # @example
17
+ # Shell::Pdfimages.resolution("/some/pdf.pdf")
18
+ # .value
19
+ # # => #<OpenStruct x_average=157, y_average=183, x_median=155, y_median=175>
20
+ #
21
+ # @param input [Pathname, String, Fs::File]
22
+ # @return [Shell::Command::Result]
23
+ # `Result#value` [OpenStruct] a struct with `#x_average`, `#y_average`, `#x_median` and `#y_median`
24
+ def self.resolution(input)
25
+ exec(:list, input).on_success do |result|
26
+ columns =
27
+ result.stdout
28
+ .lines[2..]
29
+ .map(&:split)
30
+
31
+ next unless columns.any?
32
+
33
+ x_ppis =
34
+ columns.map { |column| column[12].to_i }
35
+ y_ppis =
36
+ columns.map { |column| column[13].to_i }
37
+
38
+ result.value =
39
+ OpenStruct.new(
40
+ x_average: x_ppis.average.round,
41
+ y_average: y_ppis.average.round,
42
+ x_median: x_ppis.median.round,
43
+ y_median: y_ppis.median.round
44
+ )
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,79 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../shell"
4
+
5
+ module Shell
6
+ class Pdfinfo < Command
7
+ command_path `which pdfinfo`
8
+
9
+ transform_options first_page: :f,
10
+ last_page: :l
11
+
12
+ # Is the PDF at `input` encrypted?
13
+ #
14
+ # @param input [Pathname, String, Fs::File]
15
+ # @return [Shell::Command::Result]
16
+ # `Result#value` [Boolean]
17
+ def self.encrypted?(input)
18
+ exec(input).on_success do |result|
19
+ result.value =
20
+ !!report(result).match(/Encrypted:\s*yes/)
21
+ end
22
+ end
23
+
24
+ # Do the PDF restrictions for `input` prevent us from reading the file contents?
25
+ #
26
+ # @param input [Pathname, String, Fs::File]
27
+ # @return [Shell::Command::Result]
28
+ # `Result#value` [Boolean]
29
+ def self.pdf_reading_prohibited?(input)
30
+ exec(input).on_success do |result|
31
+ text = report(result)
32
+
33
+ result.value =
34
+ !!text.match(/Encrypted:\s*yes/) && !!text.match(/print:no/)
35
+ end
36
+ end
37
+
38
+ # The number of pages contained in this file
39
+ #
40
+ # @param input [Pathname, String, Fs::File]
41
+ # @return [Shell::Command::Result]
42
+ # `Result#value` [Integer]
43
+ def self.page_count(input)
44
+ exec(input).on_success do |result|
45
+ result.value =
46
+ report(result).match(/Pages:\s*(\d+)/)[1].to_i
47
+ end
48
+ end
49
+
50
+ # The size of the pages in the PDF, in points (1/72 inch)
51
+ #
52
+ # @param input [Pathname, String, Fs::File]
53
+ # @param page [Integer, nil] only report this page (1-based); all pages if nil
54
+ # @return [Shell::Command::Result]
55
+ # `Result#value` [Array<Array(Float, Float)>] width and height per page, in page order
56
+ def self.page_sizes(input, page: nil)
57
+ first_page = page || 1
58
+ last_page = page || 1_000_000
59
+
60
+ exec(input, first_page: first_page, last_page: last_page).on_success do |result|
61
+ result.value =
62
+ report(result).scan(/^Page\s+\d+\s+size:\s+([\d.]+) x ([\d.]+) pts/)
63
+ .map { |width, height| [width.to_f, height.to_f] }
64
+ end
65
+ end
66
+
67
+ # pdfinfo's report includes the document's metadata (Title, Author, Keywords...), which
68
+ # is written by whatever produced the PDF and is not guaranteed to be valid UTF-8. We only
69
+ # look for ASCII fields, so invalid bytes are replaced instead of letting them make the
70
+ # regex matches raise.
71
+ #
72
+ # @param result [Shell::Command::Result]
73
+ # @return [String]
74
+ def self.report(result)
75
+ result.stdout.scrub
76
+ end
77
+ private_class_method :report
78
+ end
79
+ end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../shell"
4
+
5
+ module Shell
6
+ # Interface for `pdftocairo`
7
+ class Pdftocairo < Command
8
+ command_path `which pdftocairo`
9
+ option_prefix "-"
10
+
11
+ # Writes the PDF at `input` anew, with all fonts embedded
12
+ #
13
+ # Poppler maps the glyphs of fonts which the PDF does not embed through the PDF's
14
+ # `ToUnicode` table onto an installed font, and embeds the result. That makes the
15
+ # output independent of which fonts the system rendering it happens to have.
16
+ #
17
+ # @param input [Pathname, String, Fs::File]
18
+ # @param output [Pathname, String, Fs::File]
19
+ # @return [Shell::Command::Result]
20
+ def self.embed_fonts(input:, output:)
21
+ exec(input, output, :pdf)
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../shell"
4
+
5
+ module Shell
6
+ class Pdftotext < Command
7
+ command_path `which pdftotext`
8
+
9
+ # Is the PDF at `input` encrypted?
10
+ #
11
+ # @param input [Pathname, String, Fs::File]
12
+ # @return [Shell::Command::Result]
13
+ # `Result#value` [String]
14
+ def self.text(input)
15
+ exec(input, "-").on_success do |result|
16
+ result.value = result.stdout
17
+ end
18
+ end
19
+
20
+ # Is the PDF at `input` encrypted?
21
+ #
22
+ # @param input [Pathname, String, Fs::File]
23
+ # @param page [Integer] read only page `page`
24
+ # @return [Shell::Command::Result]
25
+ # `Result#value` [String]
26
+ def self.page(input, page: nil)
27
+ exec(input, "-", f: page, l: page).on_success do |result|
28
+ result.value = result.stdout
29
+ end
30
+ end
31
+ end
32
+ end
data/lib/shell/ping.rb ADDED
@@ -0,0 +1,50 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../shell"
4
+
5
+ module Shell
6
+ class Ping < Command
7
+ command_path `which ping`
8
+
9
+ transform_options attempts: "-c",
10
+ interval: "-i",
11
+ ping_timeout: "-w"
12
+
13
+ detect_version do
14
+ "mac" if RUBY_PLATFORM.include?("darwin")
15
+ end
16
+
17
+ # How long beyond ping's own deadline we wait before killing it
18
+ #
19
+ # Ping ends by itself when its deadline expires; the kill is only there for the cases
20
+ # it cannot cover, such as a name lookup which hangs.
21
+ KILL_GRACE_PERIOD = 1
22
+
23
+ # Is `host` reachable by ICMP packets?
24
+ #
25
+ # The `:timeout` is handed to ping as its own deadline (`-w` / `-t`), not used as
26
+ # {Shell::Command#exec}'s `:timeout:`, so an unreachable host ends in an unsuccessful
27
+ # result instead of a `Shell::Errors::CommandTimeout`.
28
+ #
29
+ # @param host [String]
30
+ # @param attempts [Integer] how many packets to send
31
+ # @param interval [Float, Integer] how long to wait between packets
32
+ # @param timeout [Float, Integer] how long to wait overall before ping aborts,
33
+ # regardless of responses received
34
+ # @return [Shell::Command::Result]
35
+ def self.host_reachable?(host, attempts: 3, interval: 0.5, timeout: 2)
36
+ exec(host,
37
+ attempts: attempts,
38
+ interval: interval,
39
+ ping_timeout: timeout,
40
+ timeout: timeout + KILL_GRACE_PERIOD)
41
+ end
42
+
43
+ version("mac") do
44
+ # BSD `ping` and GNU `ping` have different arguments
45
+ transform_options attempts: "-c",
46
+ interval: "-i",
47
+ ping_timeout: "-t"
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,42 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Shell
4
+ # An extended `Array` subclass containing useful facilities
5
+ # commonly employed around the application
6
+ module PowerArray
7
+ refine Array do
8
+ # Compares self with `other_array` while ignoring
9
+ # sort order
10
+ #
11
+ # @param other_array [Array]
12
+ # @raise [ArgumentError]
13
+ # @return [Boolean]
14
+ def eql_contents?(other_array)
15
+ raise ArgumentError, "array expected" unless other_array.is_a?(Array)
16
+
17
+ length == other_array.length &&
18
+ self & other_array == self
19
+ end
20
+
21
+ # The average of an Array of {Numeric} values
22
+ #
23
+ # @return [Float]
24
+ def average
25
+ sum(0.0) / size
26
+ end
27
+
28
+ # The median of an Array of {Numeric} values
29
+ #
30
+ # @return [Float, Integer]
31
+ def median
32
+ midpoint = size / 2
33
+
34
+ if size.even?
35
+ sort[midpoint-1, 2].sum / 2.0
36
+ else
37
+ sort[midpoint]
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,142 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "active_support"
4
+ require "active_support/core_ext/array/access"
5
+ require "active_support/core_ext/numeric/bytes"
6
+ require_relative "../shell"
7
+
8
+ module Shell
9
+ # Interface for `7z`
10
+ class SevenZip < Command
11
+ command_path `which 7z`
12
+ option_prefix "-"
13
+ positional_parameters :last
14
+ transform_options t: "t",
15
+ l: "l",
16
+ x: "x",
17
+ o: "-o#VALUE#"
18
+
19
+ # Extracts archive at `input` to a temporary directory
20
+ #
21
+ # When called with a block, the temporary directory is
22
+ # automatically removed after the block returns; your block
23
+ # receives a {Fs::Directory} to the directory.
24
+ #
25
+ # @example
26
+ # ```ruby
27
+ # Shell::SevenZip.to_temp("/some/file.zip") do |path|
28
+ # puts path.children
29
+ # end
30
+ # ```
31
+ #
32
+ # When called without a block, the {Pathname} is returned in
33
+ # {Result#value}.
34
+ #
35
+ # ```ruby
36
+ # result = Shell::SevenZip.to_temp("/some/file.zip")
37
+ # puts result.value.children
38
+ # ```
39
+ #
40
+ # @param input [Pathname, Fs::File, String]
41
+ # @yieldparam pathname [Fs::Directory] an `Fs::Directory` instance for the temp dir
42
+ # @raise [Shell::Errors::DependencyMissing] if the gubbins-fs gem is not installed
43
+ # @return [Shell::Command::Result]
44
+ # `Result#value` [Fs::Directory] an `Fs::Directory` instance for the temp dir; only
45
+ # when called without a block
46
+ def self.to_temp(input, &block)
47
+ require_optional "gubbins/fs", gem_name: "gubbins-fs"
48
+
49
+ result = nil
50
+
51
+ if block_given?
52
+ Fs::Directory.temp_dir_for do |temp_dir|
53
+ result =
54
+ exec(input, :x, o: temp_dir).on_success do
55
+ yield temp_dir
56
+ end
57
+ end
58
+
59
+ else
60
+ temp_dir = Fs::Directory.temp_dir_for
61
+ result = exec(input, :x, o: temp_dir)
62
+ result.value = temp_dir
63
+ end
64
+
65
+ result
66
+ end
67
+
68
+ # Extracts archive at `input` to `output` directory
69
+ #
70
+ # @param input [Pathname, Fs::File, String]
71
+ # @param output [Pathname, Fs::File, String]
72
+ # @return [Shell::Command::Result]
73
+ def self.to(input:, output:)
74
+ exec(input, :x, o: output)
75
+ end
76
+
77
+ # Returns the files contained in the archive as {Pathname} objects
78
+ #
79
+ # @param input [Pathname, Fs::File, String]
80
+ # @return [Shell::Command::Result]
81
+ # `Result#value` [Array<Pathname>]
82
+ def self.list(input)
83
+ exec(input, :l, :ba).on_success do |result|
84
+ result.value =
85
+ result.stdout
86
+ .lines
87
+ .map { |line| Pathname.new(line.split.last) }
88
+ end
89
+ end
90
+
91
+ # Is the uncompressed size of the archive below `megabytes`?
92
+ #
93
+ # @param input [Pathname, Fs::File, String]
94
+ # @param megabytes [Integer, Float]
95
+ # @return [Shell::Command::Result]
96
+ # `Result#value` [Boolean]
97
+ def self.uncompressed_size_below?(input, megabytes:)
98
+ exec(input, :l).on_success do |result|
99
+ uncompressed_size =
100
+ result.stdout
101
+ .lines
102
+ .last
103
+ .split
104
+ .third
105
+ .to_i
106
+
107
+ result.value =
108
+ megabytes.megabytes > uncompressed_size
109
+ end
110
+ end
111
+
112
+ # Validate `input`
113
+ #
114
+ # @param input [Pathname, Fs::File, String]
115
+ # @return [Shell::Command::Result]
116
+ # `Result#value` [Boolean]
117
+ def self.validate(input)
118
+ exec(input, :t, :bd, :bb0).tap do |result|
119
+ result.value = result.success?
120
+ end
121
+ end
122
+
123
+ # Does this archive test error-free?
124
+ #
125
+ # @param input [Pathname, Fs::File, String]
126
+ # @return [Boolean]
127
+ def self.valid?(input)
128
+ validate(input).raise_failures
129
+ .success?
130
+ end
131
+
132
+ # Is `input` a password-protected archive?
133
+ #
134
+ # @param input [Pathname, Fs::File, String]
135
+ # @return [Boolean]
136
+ def self.encrypted?(input)
137
+ exec(input, :l, :slt).raise_failures
138
+ .stdout
139
+ .include?("Encrypted = +")
140
+ end
141
+ end
142
+ end
@@ -0,0 +1,120 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "active_support"
4
+ require "active_support/core_ext/object/try"
5
+ require_relative "../shell"
6
+
7
+ module Shell
8
+ class Tesseract < Command
9
+ command_path `which tesseract`
10
+ option_prefix "-"
11
+ positional_parameters :first
12
+ transform_options language: :l,
13
+ page_segmentation_mode: :psm,
14
+ pdf: "pdf",
15
+ tsv: "tsv",
16
+ quiet: "quiet",
17
+ engine: :oem
18
+
19
+ detect_version do
20
+ # note that a regular `.exec` would lead to infinite recursion,
21
+ # because `.exec` calls `detect_version` itself
22
+ exec_base("--version").on_success do |result|
23
+ result.value =
24
+ result.stdout
25
+ .match(/tesseract (\d)/)
26
+ .try(:[], 1)
27
+ end
28
+ end
29
+
30
+ # Returns the files rotation in degrees
31
+ #
32
+ # Where there is little text to go on, Tesseract guesses, and says how sure it is. Pass
33
+ # `min_confidence` to treat an answer it is less sure of as no answer.
34
+ #
35
+ # @param input [Pathname, String, Fs::File]
36
+ # @param language [Symbol]
37
+ # @param min_confidence [Numeric, nil] the lowest "Orientation confidence" to accept; any
38
+ # answer is accepted if `nil`
39
+ # @return [Shell::Command::Result]
40
+ # `Result#value` [Integer, nil] `nil` if there is no answer, or none we trust
41
+ def self.detect_rotation(input:, language: :osd, min_confidence: nil)
42
+ exec(input, "stdout", language: language, page_segmentation_mode: 0).on_success do |result|
43
+ rotation = result.stdout.match(/^Rotate: (\d+)/)
44
+ confidence = result.stdout.match(/^Orientation confidence: ([\d.]+)/)
45
+
46
+ if rotation && (min_confidence.nil? || (confidence && confidence[1].to_f >= min_confidence))
47
+ result.value = rotation[1].to_i
48
+ end
49
+ end
50
+ end
51
+
52
+ # Performs OCR on the given :input
53
+ #
54
+ # OCR result is given on STDOUT by default; pass `:output` to write a file instead.
55
+ # Output format is selected depending on file extension; `.txt` and `.pdf` are supported.
56
+ #
57
+ # @param input [Pathname, String, Fs::File]
58
+ # @param output [Symbol, Pathname, String, Fs::File] set :stdout to output
59
+ # the OCR result to STDOUT instead of writing into a file
60
+ # @param format [Symbol] :plain, :pdf or :tsv
61
+ # @param page_segmentation_mode [Integer] see `tesseract --help-extra` for available modes
62
+ # @param language [String] the training data to use; Tesseract's own default
63
+ # @param engine [Integer] 0 - legacy, 1 - neural net, 2 - both, 3 - default
64
+ # Please note that the chosen language/.traineddata file must support the selected engine. If
65
+ # you see initialization errors, remove the :quiet argument to see the error message.
66
+ # @return [Shell::Command::Result]
67
+ # `Result#value` [String, nil]
68
+ def self.ocr(input:, output: :stdout, format: :plain, page_segmentation_mode: 3, language: "eng", engine: 3)
69
+ input = Path.convert(input)
70
+ args = [input]
71
+
72
+ if output == :stdout
73
+ args << "stdout"
74
+ else
75
+ args << Path.replace_extname(output, with: nil)
76
+ end
77
+
78
+ case format
79
+ when :pdf
80
+ args << :pdf
81
+ when :tsv
82
+ args << :tsv
83
+ end
84
+
85
+ exec(:quiet, *args, language: language, engine: engine, page_segmentation_mode: page_segmentation_mode).on_success do |result|
86
+ result.value = result.stdout
87
+ end
88
+ end
89
+
90
+ version("4") do
91
+ option_prefix :auto
92
+ end
93
+
94
+ version("5") do
95
+ option_prefix :auto
96
+
97
+ private
98
+
99
+ # Tesseract uses a more peculiar argument format in the form of
100
+ # `tesseract input output <options> config_flags`, so we overload the
101
+ # command builder to automatically append the config_flags at the end.
102
+ #
103
+ # TODO enable a more elegant approach to complex custom argument
104
+ # ordering somehow.
105
+ def build_command_arguments(*args, **kargs)
106
+ quiet = args.delete(:quiet)
107
+ pdf = args.delete(:pdf)
108
+ tsv = args.delete(:tsv)
109
+
110
+ super(*args, **kargs).then do |options_array|
111
+ options_array << "quiet" if quiet
112
+ options_array << "pdf" if pdf
113
+ options_array << "tsv" if tsv
114
+
115
+ options_array
116
+ end
117
+ end
118
+ end
119
+ end
120
+ end