Class: PDF::Reader::PageLayout

Inherits:
Object
  • Object
show all
Defined in:
lib/pdf/reader/page_layout.rb

Overview

Takes a collection of TextRun objects and renders them into a single string that best approximates the way they’d appear on a render PDF page.

media box should be a 4 number array that describes the dimensions of the page to be rendered as described by the page’s MediaBox attribute

Instance Method Summary collapse

Constructor Details

#initialize(runs, mediabox) ⇒ PageLayout

Returns a new instance of PageLayout.



11
12
13
14
15
16
17
18
19
20
# File 'lib/pdf/reader/page_layout.rb', line 11

def initialize(runs, mediabox)
  @runs    = merge_runs(runs)
  @mean_font_size   = mean(@runs.map(&:font_size)) || 0
  @mean_glyph_width = mean(@runs.map(&:mean_character_width)) || 0
  @page_width  = mediabox[2] - mediabox[0]
  @page_height = mediabox[3] - mediabox[1]
  @x_offset = @runs.map(&:x).sort.first
  @current_platform_is_rbx_19 = RUBY_DESCRIPTION =~ /\Arubinius 2.0.0/ &&
                                  RUBY_VERSION >= "1.9.0"
end

Instance Method Details

#to_sObject



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/pdf/reader/page_layout.rb', line 22

def to_s
  return "" if @runs.empty?

  page = row_count.times.map { |i| " " * col_count }
  @runs.each do |run|
    x_pos = ((run.x - @x_offset) / col_multiplier).round
    y_pos = row_count - (run.y / row_multiplier).round
    if y_pos < row_count && y_pos >= 0 && x_pos < col_count && x_pos >= 0
      local_string_insert(page[y_pos], run.text, x_pos)
    end
  end
  interesting_rows(page).map(&:rstrip).join("\n")
end