class PDF::Reader::PageState

Constants

DEFAULT_GRAPHICS_STATE

Public Class Methods

new(page) click to toggle source

starting a new page

# File lib/pdf/reader/page_state.rb, line 22
def initialize(page)
  @page          = page
  @objects       = page.objects
  @font_stack    = [build_fonts(page.fonts)]
  @xobject_stack = [page.xobjects]
  @cs_stack      = [page.color_spaces]
  @stack         = [DEFAULT_GRAPHICS_STATE.dup]
end

Public Instance Methods

begin_text_object() click to toggle source

Text Object Operators

# File lib/pdf/reader/page_state.rb, line 71
def begin_text_object
  @text_matrix      = Matrix.identity(3)
  @text_line_matrix = Matrix.identity(3)
end
concatenate_matrix(a, b, c, d, e, f) click to toggle source

update the current transformation matrix.

If the CTM is currently undefined, just store the new values.

If there's an existing CTM, then multiply the existing matrix with the new matrix to form the updated matrix.

# File lib/pdf/reader/page_state.rb, line 54
def concatenate_matrix(a, b, c, d, e, f)
  transform = Matrix[
    [a, b, 0],
    [c, d, 0],
    [e, f, 1]
  ]
  if state[:ctm]
    state[:ctm] = transform * state[:ctm]
  else
    state[:ctm] = transform
  end
end
ctm_transform(x, y, z = 1) click to toggle source

transform x and y co-ordinates from the current user space to the underlying device space.

# File lib/pdf/reader/page_state.rb, line 198
def ctm_transform(x, y, z = 1)
  [
    (ctm[0,0] * x) + (ctm[1,0] * y) + (ctm[2,0] * z),
    (ctm[0,1] * x) + (ctm[1,1] * y) + (ctm[2,1] * z)
  ]
end
current_font() click to toggle source
# File lib/pdf/reader/page_state.rb, line 216
def current_font
  find_font(state[:text_font])
end
end_text_object() click to toggle source
# File lib/pdf/reader/page_state.rb, line 76
def end_text_object
  @text_matrix      = Matrix.identity(3)
  @text_line_matrix = Matrix.identity(3)
end
find_color_space(label) click to toggle source
# File lib/pdf/reader/page_state.rb, line 227
def find_color_space(label)
  dict = @cs_stack.detect { |colorspaces|
    colorspaces.has_key?(label)
  }
  dict ? dict[label] : nil
end
find_font(label) click to toggle source
# File lib/pdf/reader/page_state.rb, line 220
def find_font(label)
  dict = @font_stack.detect { |fonts|
    fonts.has_key?(label)
  }
  dict ? dict[label] : nil
end
find_xobject(label) click to toggle source
# File lib/pdf/reader/page_state.rb, line 234
def find_xobject(label)
  dict = @xobject_stack.detect { |xobjects|
    xobjects.has_key?(label)
  }
  dict ? dict[label] : nil
end
font_size() click to toggle source
# File lib/pdf/reader/page_state.rb, line 98
def font_size
  state[:text_font_size] * @text_matrix[0,0]
end
invoke_xobject(label) { |form| ... } click to toggle source

XObjects

# File lib/pdf/reader/page_state.rb, line 169
def invoke_xobject(label)
  save_graphics_state
  xobject = find_xobject(label)

  raise MalformedPDFError, "XObject #{label} not found" if xobject.nil?
  matrix = xobject.hash[:Matrix]
  concatenate_matrix(*matrix) if matrix

  if xobject.hash[:Subtype] == :Form
    form = PDF::Reader::FormXObject.new(@page, xobject)
    @font_stack.unshift(form.font_objects)
    @xobject_stack.unshift(form.xobjects)
    yield form if block_given?
    @font_stack.shift
    @xobject_stack.shift
  else
    yield xobject if block_given?
  end

  restore_graphics_state
end
move_text_position(x, y) click to toggle source

Text Positioning Operators

# File lib/pdf/reader/page_state.rb, line 122
def move_text_position(x, y) # Td
  temp_matrix = Matrix[
    [1, 0, 0],
    [0, 1, 0],
    [x, y, 1]
  ]
  @text_matrix = @text_line_matrix = temp_matrix * @text_line_matrix
end
move_text_position_and_set_leading(x, y) click to toggle source
# File lib/pdf/reader/page_state.rb, line 131
def move_text_position_and_set_leading(x, y) # TD
  set_text_leading(-1 * y)
  move_text_position(x, y)
end
move_to_next_line_and_show_text(str) click to toggle source
# File lib/pdf/reader/page_state.rb, line 156
def move_to_next_line_and_show_text(str) # '
  move_to_start_of_next_line
end
move_to_start_of_next_line() click to toggle source
# File lib/pdf/reader/page_state.rb, line 144
def move_to_start_of_next_line # T*
  move_text_position(0, -state[:text_leading])
end
restore_graphics_state() click to toggle source
# File lib/pdf/reader/page_state.rb, line 39
def restore_graphics_state
  @stack.pop
end
save_graphics_state() click to toggle source

Graphics State Operators

# File lib/pdf/reader/page_state.rb, line 35
def save_graphics_state
  @stack.push clone_state
end
set_character_spacing(char_spacing) click to toggle source

Text State Operators

# File lib/pdf/reader/page_state.rb, line 85
def set_character_spacing(char_spacing)
  state[:char_spacing] = char_spacing
end
set_horizontal_text_scaling(h_scaling) click to toggle source
# File lib/pdf/reader/page_state.rb, line 89
def set_horizontal_text_scaling(h_scaling)
  state[:h_scaling] = h_scaling
end
set_spacing_next_line_show_text(aw, ac, string) click to toggle source
# File lib/pdf/reader/page_state.rb, line 160
def set_spacing_next_line_show_text(aw, ac, string) # "
  set_word_spacing(aw)
  set_character_spacing(ac)
  move_to_next_line_and_show_text(string)
end
set_text_font_and_size(label, size) click to toggle source
# File lib/pdf/reader/page_state.rb, line 93
def set_text_font_and_size(label, size)
  state[:text_font]      = label
  state[:text_font_size] = size
end
set_text_leading(leading) click to toggle source
# File lib/pdf/reader/page_state.rb, line 102
def set_text_leading(leading)
  state[:text_leading] = leading
end
set_text_matrix_and_text_line_matrix(a, b, c, d, e, f) click to toggle source
# File lib/pdf/reader/page_state.rb, line 136
def set_text_matrix_and_text_line_matrix(a, b, c, d, e, f) # Tm
  @text_matrix = @text_line_matrix = Matrix[
    [a, b, 0],
    [c, d, 0],
    [e, f, 1]
  ]
end
set_text_rendering_mode(mode) click to toggle source
# File lib/pdf/reader/page_state.rb, line 106
def set_text_rendering_mode(mode)
  state[:text_mode] = mode
end
set_text_rise(rise) click to toggle source
# File lib/pdf/reader/page_state.rb, line 110
def set_text_rise(rise)
  state[:text_rise] = rise
end
set_word_spacing(word_spacing) click to toggle source
# File lib/pdf/reader/page_state.rb, line 114
def set_word_spacing(word_spacing)
  state[:word_spacing] = word_spacing
end
show_text_with_positioning(params) click to toggle source

Text Showing Operators

# File lib/pdf/reader/page_state.rb, line 152
def show_text_with_positioning(params) # TJ
  # TODO record position changes in state here
end
trm_transform(x, y, z = 1) click to toggle source

transform x and y co-ordinates from the current text space to the underlying device space.

# File lib/pdf/reader/page_state.rb, line 208
def trm_transform(x, y, z = 1)
  trm = text_rendering_matrix
  [
    (trm[0,0] * x) + (trm[1,0] * y) + (trm[2,0] * z),
    (trm[0,1] * x) + (trm[1,1] * y) + (trm[2,1] * z)
  ]
end

Private Instance Methods

build_fonts(raw_fonts) click to toggle source

wrap the raw PDF Font objects in handy ruby Font objects.

# File lib/pdf/reader/page_state.rb, line 265
def build_fonts(raw_fonts)
  wrapped_fonts = raw_fonts.map { |label, font|
    [label, PDF::Reader::Font.new(@objects, @objects.deref(font))]
  }

  ::Hash[wrapped_fonts]
end
clone_state() click to toggle source

when #save_graphics_state is called, we need to push a new copy of the current state onto the stack. That way any modifications to the state will be undone once #restore_graphics_state is called.

This returns a deep clone of the current state, ensuring changes are keep separate from earlier states.

Marshal is used to round-trip the state through a string to easily perform the deep clone. Kinda hacky, but effective.

# File lib/pdf/reader/page_state.rb, line 283
def clone_state
  if @stack.empty?
    {}
  else
    Marshal.load Marshal.dump(@stack.last)
  end
end
ctm() click to toggle source

return the current transformation matrix

# File lib/pdf/reader/page_state.rb, line 255
def ctm
  state[:ctm]
end
state() click to toggle source
# File lib/pdf/reader/page_state.rb, line 259
def state
  @stack.last
end
text_rendering_matrix() click to toggle source
# File lib/pdf/reader/page_state.rb, line 243
def text_rendering_matrix
  state_matrix = Matrix[
    [font_size * state[:h_scaling], 0, 0],
    [0, font_size, 0],
    [0, state[:text_rise], 1]
  ]

  state_matrix * @text_matrix * ctm
end