Class: MuPDF::Page

Inherits:
Object
  • Object
show all
Defined in:
lib/mupdf/page.rb

Overview

A wrapper for a PDF page within a PDF document.

Constant Summary collapse

REGEX =
%r{<page pagenum="(?<pagenum>\d+)">(?<content>.*?)</page>}m
MEDIA_BOX_REGEX =
%r{<MediaBox (?<content>.*?) />}
CROP_BOX_REGEX =
%r{<CropBox (?<content>.*?) />}
ART_BOX_REGEX =
%r{<ArtBox (?<content>.*?) />}
BLEED_BOX_REGEX =
%r{<BleedBox (?<content>.*?) />}
TRIM_BOX_REGEX =
%r{<TrimBox (?<content>.*?) />}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(media_box:, crop_box:, art_box:, bleed_box:, trim_box:, number:) ⇒ Page

Returns a new instance of Page.

Parameters:



109
110
111
112
113
114
115
116
# File 'lib/mupdf/page.rb', line 109

def initialize(media_box:, crop_box:, art_box:, bleed_box:, trim_box:, number:)
  @media_box = media_box
  @crop_box = crop_box
  @art_box = art_box
  @bleed_box = bleed_box
  @trim_box = trim_box
  @number = number
end

Instance Attribute Details

#art_boxMuPDFBox?

Returns:

  • (MuPDFBox, nil)


89
90
91
# File 'lib/mupdf/page.rb', line 89

def art_box
  @art_box
end

#bleed_boxMuPDFBox?

Returns:

  • (MuPDFBox, nil)


93
94
95
# File 'lib/mupdf/page.rb', line 93

def bleed_box
  @bleed_box
end

#crop_boxMuPDFBox?

Returns:

  • (MuPDFBox, nil)


85
86
87
# File 'lib/mupdf/page.rb', line 85

def crop_box
  @crop_box
end

#media_boxMuPDFBox?

Returns:

  • (MuPDFBox, nil)


81
82
83
# File 'lib/mupdf/page.rb', line 81

def media_box
  @media_box
end

#numberInteger

Returns:

  • (Integer)


101
102
103
# File 'lib/mupdf/page.rb', line 101

def number
  @number
end

#trim_boxMuPDFBox?

Returns:

  • (MuPDFBox, nil)


97
98
99
# File 'lib/mupdf/page.rb', line 97

def trim_box
  @trim_box
end

Class Method Details

.parse(text) ⇒ Array<MuPDF::Page>

Parameters:

  • text (String)

Returns:



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/mupdf/page.rb', line 16

def self.parse(text)
  text.scan(REGEX).map do |number, content|
    new(
      media_box: parse_media_box(content),
      crop_box: parse_crop_box(content),
      art_box: parse_art_box(content),
      bleed_box: parse_bleed_box(content),
      trim_box: parse_trim_box(content),
      number: Integer(number)
    )
  end
end

.parse_art_box(text) ⇒ MuPDF::Box?

Parameters:

  • text (String)

Returns:



52
53
54
55
56
57
# File 'lib/mupdf/page.rb', line 52

def self.parse_art_box(text)
  match = ART_BOX_REGEX.match(text)
  return unless match

  MuPDF::Box.parse(match[:content], kind: :art)
end

.parse_bleed_box(text) ⇒ MuPDF::Box?

Parameters:

  • text (String)

Returns:



62
63
64
65
66
67
# File 'lib/mupdf/page.rb', line 62

def self.parse_bleed_box(text)
  match = BLEED_BOX_REGEX.match(text)
  return unless match

  MuPDF::Box.parse(match[:content], kind: :bleed)
end

.parse_crop_box(text) ⇒ MuPDF::Box?

Parameters:

  • text (String)

Returns:



42
43
44
45
46
47
# File 'lib/mupdf/page.rb', line 42

def self.parse_crop_box(text)
  match = CROP_BOX_REGEX.match(text)
  return unless match

  MuPDF::Box.parse(match[:content], kind: :crop)
end

.parse_media_box(text) ⇒ MuPDF::Box?

Parameters:

  • text (String)

Returns:



32
33
34
35
36
37
# File 'lib/mupdf/page.rb', line 32

def self.parse_media_box(text)
  match = MEDIA_BOX_REGEX.match(text)
  return unless match

  MuPDF::Box.parse(match[:content], kind: :media)
end

.parse_trim_box(text) ⇒ MuPDF::Box?

Parameters:

  • text (String)

Returns:



72
73
74
75
76
77
# File 'lib/mupdf/page.rb', line 72

def self.parse_trim_box(text)
  match = TRIM_BOX_REGEX.match(text)
  return unless match

  MuPDF::Box.parse(match[:content], kind: :trim)
end

Instance Method Details

#heightBigDecimal?

Returns:

  • (BigDecimal, nil)


129
130
131
# File 'lib/mupdf/page.rb', line 129

def height
  @media_box&.height
end

#inspectString

Returns:

  • (String)


119
120
121
# File 'lib/mupdf/page.rb', line 119

def inspect
  "#<#{self.class.name} number=#{@number} width=#{width} height=#{height}>"
end

#widthBigDecimal?

Returns:

  • (BigDecimal, nil)


124
125
126
# File 'lib/mupdf/page.rb', line 124

def width
  @media_box&.width
end