Class: PDF::TrimDetector

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/pdf/trim_detector.rb

Overview

attempts to detect the trim marks on a PDF page and returns a rectangle describing the box they indicate. Useful for when you have a PDF with trim marks drawn on the page but no matching page box (TrimBox, ArtBox, etc)

See the project README for a usage overview.

Defined Under Namespace

Classes: Path, Point

Instance Method Summary collapse

Instance Method Details

#append_line(x, y) ⇒ Object

l



51
52
53
54
55
56
# File 'lib/pdf/trim_detector.rb', line 51

def append_line(x, y) # l
  if @current_paths.last
    x, y = @state.ctm_transform(x, y)
    @current_paths.last.add_point(x, y)
  end
end

#begin_new_subpath(x, y) ⇒ Object

m



45
46
47
48
49
# File 'lib/pdf/trim_detector.rb', line 45

def begin_new_subpath(x, y) # m
  @current_paths << Path.new
  x, y = @state.ctm_transform(x, y)
  @current_paths.last.add_point(x, y)
end

#fill_strokeObject

B



58
59
60
61
62
63
# File 'lib/pdf/trim_detector.rb', line 58

def fill_stroke # B
  # TODO set the path colour
  while path = @current_paths.shift
    @paths << path
  end
end

#page=(page) ⇒ Object



24
25
26
27
28
29
30
# File 'lib/pdf/trim_detector.rb', line 24

def page=(page)
  @page  = page
  @state = PDF::Reader::PageState.new(page)
  @paths = []
  @trim  = nil
  @current_paths = []
end

#stroke_pathObject

S



65
66
67
68
69
70
# File 'lib/pdf/trim_detector.rb', line 65

def stroke_path # S
  # TODO set the path colour
  while path = @current_paths.shift
    @paths << path
  end
end

#trimObject



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/pdf/trim_detector.rb', line 32

def trim
  xes = outer_x_candidates
  yes = outer_y_candidates
  if xes.size == 2 && yes.size == 2
    [
      xes.sort.first,
      yes.sort.first,
      xes.sort.last,
      yes.sort.last,
    ]
  end
end