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