Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to transpose text object coordinates. #7

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/pdf-reader-turtletext.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require 'pdf-reader'
require 'pdf/reader/patch/object_hash'
require 'pdf/reader/positional_text_receiver'
require 'pdf/reader/transposed_positional_text_receiver'

require 'pdf/reader/turtletext'
require 'pdf/reader/turtletext/version'
Expand Down
31 changes: 31 additions & 0 deletions lib/pdf/reader/transposed_positional_text_receiver.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Receiver to access positional (x,y) text content from a PDF
#
# Typical usage:
#
# reader = PDF::Reader.new(filename)
# receiver = PDF::Reader::TransposedPositionalTextReceiver.new
# reader.page(page).walk(receiver)
# receiver.content
#
class PDF::Reader::TransposedPositionalTextReceiver < PDF::Reader::PositionalTextReceiver

# record text that is drawn on the page
def show_text(string) # Tj
raise PDF::Reader::MalformedPDFError, "current font is invalid" if @state.current_font.nil?
newx, newy = @state.trm_transform(0,0)
@content[newx] ||= {}
@content[newx][newy] ||= ''
@content[newx][newy] << @state.current_font.to_utf8(string)
end

# override PageTextReceiver content accessor .
# Returns a hash of positional text:
# {
# x_coord=>{y_coord=>text, y_coord=>text },
# x_coord=>{y_coord=>text, y_coord=>text }
# }
def content
super
end

end
13 changes: 12 additions & 1 deletion lib/pdf/reader/turtletext.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class PDF::Reader::Turtletext
# +source+ is a file name or stream-like object
# Supported +options+ include:
# * :y_precision
# * :transpose_coordinates
def initialize(source, options={})
@options = options
@reader = PDF::Reader.new(source)
Expand All @@ -32,6 +33,14 @@ def y_precision
options[:y_precision] ||= 3
end

# Returns whether or not the coordinates of the text objects are transposed.
# This is an option specified at object creation time that will essentially swap
# X and Y coordinates of all the text objects. This can improve the structured content
# if the PDF document is oriented in landscape.
def coordinates_transposed?
options[:transpose_coordinates] ||= false
end

# Returns positional (with fuzzed y positioning) text content collection as a hash:
# [ fuzzed_y_position, [[x_position,content]] ]
def content(page=1)
Expand Down Expand Up @@ -153,7 +162,9 @@ def bounding_box(&block)
private

def load_content(page)
receiver = PDF::Reader::PositionalTextReceiver.new
receiver = (coordinates_transposed? ?
PDF::Reader::TransposedPositionalTextReceiver.new :
PDF::Reader::PositionalTextReceiver.new)
reader.page(page).walk(receiver)
receiver.content
end
Expand Down