Handle strings with spaces

main
Guillaume Dott 2022-02-24 10:00:10 +01:00
parent e53151f2c1
commit 355813462f
1 changed files with 15 additions and 7 deletions

View File

@ -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