Handle strings with spaces
parent
e53151f2c1
commit
355813462f
|
@ -5,14 +5,22 @@ require "pdf-reader"
|
|||
require_relative "find_text/version"
|
||||
|
||||
module PDF::Reader::FindText
|
||||
def find_text(text)
|
||||
text = text.tr(' ', '')
|
||||
def find_text(value)
|
||||
runs(merge: false).each_cons(value.tr(' ', '').size).map do |chars|
|
||||
string = merge_runs_with_max_length(chars, value.size)
|
||||
string if string.text[0, value.size] == value
|
||||
end.compact
|
||||
end
|
||||
|
||||
runs(merge: false).each_cons(text.size).select do |r|
|
||||
r.map(&:text).join == text
|
||||
end.map do |r|
|
||||
PDF::Reader::TextRun.new r.first.x, r.first.y,
|
||||
r.sum(&:width), r.map(&:font_size).max, r.map(&:text).join
|
||||
private
|
||||
|
||||
def merge_runs_with_max_length(chars, length)
|
||||
chars.inject do |string, char|
|
||||
if string.mergable?(char) && string.text.size < length
|
||||
string + char
|
||||
else
|
||||
string
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue