Skip to content

Commit

Permalink
Merge pull request #496 from yob/page-text-skip-empty-runs
Browse files Browse the repository at this point in the history
Skip text runs with no text when rendering a page
  • Loading branch information
yob authored Jul 2, 2022
2 parents a8372c9 + a5a3c99 commit 01f2739
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/pdf/reader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,7 @@ def root
require 'pdf/reader/rectangle'
require 'pdf/reader/reference'
require 'pdf/reader/register_receiver'
require 'pdf/reader/no_text_filter'
require 'pdf/reader/null_security_handler'
require 'pdf/reader/security_handler_factory'
require 'pdf/reader/standard_key_builder'
Expand Down
14 changes: 14 additions & 0 deletions lib/pdf/reader/no_text_filter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# coding: utf-8
# typed: strict
# frozen_string_literal: true

class PDF::Reader
# There's no point rendering zero-width characters
class NoTextFilter

def self.exclude_empty_strings(runs)
runs.reject { |run| run.text.to_s.size == 0 }
end
end
end

2 changes: 2 additions & 0 deletions lib/pdf/reader/page_text_receiver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ def runs(opts = {})
runs = OverlappingRunsFilter.exclude_redundant_runs(runs)
end

runs = NoTextFilter.exclude_empty_strings(runs)

if opts.fetch(:merge, true)
runs = merge_runs(runs)
end
Expand Down
5 changes: 5 additions & 0 deletions rbi/pdf-reader.rbi
Original file line number Diff line number Diff line change
Expand Up @@ -846,6 +846,11 @@ module PDF
def self.detect_intersection(sweep_line_status, event_point); end
end

class NoTextFilter
sig { params(runs: T::Array[PDF::Reader::TextRun]).returns(T::Array[PDF::Reader::TextRun]) }
def self.exclude_empty_strings(runs); end
end

class EventPoint
sig { returns(Numeric) }
attr_reader :x
Expand Down

0 comments on commit 01f2739

Please sign in to comment.