mupdf 0.3.0 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 025aace876d1fc5f6ba89c7e94fb7a618b645c5011d1eb7d3923252ca1643693
4
- data.tar.gz: 9cc27d161818550e2b3290933652be4c1b8474f550f7c6f71a59eda5af4f02cc
3
+ metadata.gz: b7668e9689741bc823e304c97df7c135f9ed31453fedbed2d5f06e19a8be3f43
4
+ data.tar.gz: ee5b8c0c417dee4b2e80a063b06fb2699753e1857e9c6c11e0d50d7448815e81
5
5
  SHA512:
6
- metadata.gz: baf0ad502de63e12d938481d8944a96091a74f85e5d42189bcac9b7b0afb5b262f133e70eae434ac3f129349105262ececeff6e3feadbc037f4be14fd7127502
7
- data.tar.gz: 90a9bedd5984280f820b76bbbcab596bea290bdb79c6e699cf029898569cc006e23e7c26ea1877a95d8406a025e2dd70f2faec675f8aab0af2b143d9fa76352f
6
+ metadata.gz: 2b85d9364a453fabcce166ef780a6340a885d88022765488de11a09c080ff6e4770d9c92609b1a531bea062b6ceb08d1ea46035a4d8db119065d6302f49b5326
7
+ data.tar.gz: a315ed8e2f213ed39a5cfe45cd742d80d73d65c8fa37bdbf3e16d96a433a34a9490ac3ec454e2efcf9e86a6dfac0abc30c9dae2406f9a18e82fa8e92cbfc94df
data/README.md CHANGED
@@ -16,7 +16,7 @@ gem install mupdf
16
16
 
17
17
  ### Document
18
18
 
19
- A `MuPDF::Document` wraps for a file:
19
+ A `MuPDF::Document` wraps a PDF file:
20
20
 
21
21
  ```ruby
22
22
  document = MuPDF::Document.new('./file.pdf')
@@ -24,9 +24,23 @@ document = MuPDF::Document.new('./file.pdf')
24
24
 
25
25
  #### Info
26
26
 
27
- The `info` command displays information about a document such as the number of pages:
27
+ The `info` command displays information about the document such as the number of pages:
28
28
 
29
29
  ```ruby
30
30
  info = document.info
31
31
  info.pages # e.g. 2
32
32
  ```
33
+
34
+ #### Pages
35
+
36
+ The `pages` command finds sizing information about the pages within a document:
37
+
38
+ ```ruby
39
+ pages = document.pages
40
+ pages.count # e.g. 2
41
+ page = pages[0]
42
+ page.pagenum # 1
43
+ box = page.media_box # page.crop_box / page.bleed_box / page.trim_box / page.art_box
44
+ box.width # 612
45
+ box.height # 792
46
+ ```
data/lib/mupdf/box.rb ADDED
@@ -0,0 +1,72 @@
1
+ # frozen_string_literal: true
2
+
3
+ module MuPDF
4
+ # A bounding box for a PDF (e.g. media / crop / bleed / trim).
5
+ class Box
6
+ REGEX = /l="(?<l>\d+)" b="(?<b>\d+)" r="(?<r>\d+)" t="(?<t>\d+)"/
7
+
8
+ # @!attribute l
9
+ # @return [String] The left coordinate.
10
+ attr_reader :l
11
+
12
+ # @!attribute b
13
+ # @return [String] The bottom coordinate.
14
+ attr_reader :b
15
+
16
+ # @!attribute r
17
+ # @return [String] The right coordinate.
18
+ attr_reader :r
19
+
20
+ # @!attribute t
21
+ # @return [String] The top coordinate.
22
+ attr_reader :t
23
+
24
+ # @!attribute kind
25
+ # @return [Symbol] The kind of box.
26
+ attr_reader :kind
27
+
28
+ # @param text [String]
29
+ # @param kind [Symbol]
30
+ #
31
+ # @return [MuPDF::Box]
32
+ def self.parse(text, kind:)
33
+ match = text.match(REGEX)
34
+
35
+ new(
36
+ l: Integer(match[:l]),
37
+ b: Integer(match[:b]),
38
+ r: Integer(match[:r]),
39
+ t: Integer(match[:t]),
40
+ kind:
41
+ )
42
+ end
43
+
44
+ # @param l [Integer]
45
+ # @param b [Integer]
46
+ # @param r [Integer]
47
+ # @param t [Integer]
48
+ # @param kind [Symbol] optional
49
+ def initialize(l:, b:, r:, t:, kind: nil)
50
+ @l = l
51
+ @b = b
52
+ @r = r
53
+ @t = t
54
+ @kind = kind
55
+ end
56
+
57
+ # @return [String]
58
+ def inspect
59
+ "#<#{self.class.name} l=#{l} b=#{b} r=#{r} t=#{t} kind=#{kind}>"
60
+ end
61
+
62
+ # @return [Integer]
63
+ def width
64
+ @r - @l
65
+ end
66
+
67
+ # @return [Integer]
68
+ def height
69
+ @t - @b
70
+ end
71
+ end
72
+ end
@@ -8,6 +8,11 @@ module MuPDF
8
8
  @pathname = pathname
9
9
  end
10
10
 
11
+ # @return [String]
12
+ def inspect
13
+ "#<#{self.class.name} pathname=#{@pathname}>"
14
+ end
15
+
11
16
  # @raise [MuPDF::CommandError]
12
17
  #
13
18
  # @return [MuPDF::Info]
@@ -17,5 +22,15 @@ module MuPDF
17
22
  MuPDF::Info.parse(result)
18
23
  end
19
24
  end
25
+
26
+ # @raise [MuPDF::CommandError]
27
+ #
28
+ # @return [Array<MuPDF::Page>]
29
+ def pages
30
+ @pages ||= begin
31
+ result = MuPDF.mutool('pages', String(@pathname))
32
+ MuPDF::Page.parse(result)
33
+ end
34
+ end
20
35
  end
21
36
  end
data/lib/mupdf/page.rb ADDED
@@ -0,0 +1,113 @@
1
+ # frozen_string_literal: true
2
+
3
+ module MuPDF
4
+ # A wrapper for a PDF page within a PDF document.
5
+ class Page
6
+ REGEX = %r{<page pagenum="(?<pagenum>\d+)">(?<content>.*?)</page>}m
7
+ MEDIA_BOX_REGEX = %r{<MediaBox (?<content>.*?) />}
8
+ CROP_BOX_REGEX = %r{<CropBox (?<content>.*?) />}
9
+ ART_BOX_REGEX = %r{<ArtBox (?<content>.*?) />}
10
+ BLEED_BOX_REGEX = %r{<BleedBox (?<content>.*?) />}
11
+ TRIM_BOX_REGEX = %r{<TrimBox (?<content>.*?) />}
12
+
13
+ # @param text [String]
14
+ #
15
+ # @return [Array<MuPDF::Page>]
16
+ def self.parse(text)
17
+ text.scan(REGEX).map do |pagenum, content|
18
+ new(
19
+ media_box: parse_media_box(content),
20
+ crop_box: parse_crop_box(content),
21
+ art_box: parse_art_box(content),
22
+ bleed_box: parse_bleed_box(content),
23
+ trim_box: parse_trim_box(content),
24
+ pagenum: Integer(pagenum)
25
+ )
26
+ end
27
+ end
28
+
29
+ # @param text [String]
30
+ #
31
+ # @return [MuPDF::Box]
32
+ def self.parse_media_box(text)
33
+ match = MEDIA_BOX_REGEX.match(text)
34
+ MuPDF::Box.parse(match[:content], kind: :media)
35
+ end
36
+
37
+ # @param text [String]
38
+ #
39
+ # @return [MuPDF::Box]
40
+ def self.parse_crop_box(text)
41
+ match = CROP_BOX_REGEX.match(text)
42
+ MuPDF::Box.parse(match[:content], kind: :crop)
43
+ end
44
+
45
+ # @param text [String]
46
+ #
47
+ # @return [MuPDF::Box]
48
+ def self.parse_art_box(text)
49
+ match = ART_BOX_REGEX.match(text)
50
+ MuPDF::Box.parse(match[:content], kind: :art)
51
+ end
52
+
53
+ # @param text [String]
54
+ #
55
+ # @return [MuPDF::Box]
56
+ def self.parse_bleed_box(text)
57
+ match = BLEED_BOX_REGEX.match(text)
58
+ MuPDF::Box.parse(match[:content], kind: :bleed)
59
+ end
60
+
61
+ # @param text [String]
62
+ #
63
+ # @return [MuPDF::Box]
64
+ def self.parse_trim_box(text)
65
+ match = TRIM_BOX_REGEX.match(text)
66
+ MuPDF::Box.parse(match[:content], kind: :trim)
67
+ end
68
+
69
+ # @!attribute media_box
70
+ # @return [MuPDFBox]
71
+ attr_accessor :media_box
72
+
73
+ # @!attribute crop_box
74
+ # @return [MuPDFBox]
75
+ attr_accessor :crop_box
76
+
77
+ # @!attribute art_box
78
+ # @return [MuPDFBox]
79
+ attr_accessor :art_box
80
+
81
+ # @!attribute bleed_box
82
+ # @return [MuPDFBox]
83
+ attr_accessor :bleed_box
84
+
85
+ # @!attribute trim_box
86
+ # @return [MuPDFBox]
87
+ attr_accessor :trim_box
88
+
89
+ # @!attribute pagenum
90
+ # @return [Integer]
91
+ attr_accessor :pagenum
92
+
93
+ # @param media_box [MuPDF::Box]
94
+ # @param crop_box [MuPDF::Box]
95
+ # @param art_box [MuPDF::Box]
96
+ # @param bleed_box [MuPDF::Box]
97
+ # @param trim_box [MuPDF::Box]
98
+ # @param pagenum [Integer]
99
+ def initialize(media_box:, crop_box:, art_box:, bleed_box:, trim_box:, pagenum:)
100
+ @media_box = media_box
101
+ @crop_box = crop_box
102
+ @art_box = art_box
103
+ @bleed_box = bleed_box
104
+ @trim_box = trim_box
105
+ @pagenum = pagenum
106
+ end
107
+
108
+ # @return [String]
109
+ def inspect
110
+ "#<#{self.class.name} pagenum=#{pagenum}>"
111
+ end
112
+ end
113
+ end
data/lib/mupdf/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module MuPDF
4
- VERSION = '0.3.0'
4
+ VERSION = '0.4.0'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mupdf
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kevin Sylvestre
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2025-01-02 00:00:00.000000000 Z
11
+ date: 2025-01-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: open3
@@ -50,9 +50,11 @@ files:
50
50
  - bin/console
51
51
  - bin/setup
52
52
  - lib/mupdf.rb
53
+ - lib/mupdf/box.rb
53
54
  - lib/mupdf/command_error.rb
54
55
  - lib/mupdf/document.rb
55
56
  - lib/mupdf/info.rb
57
+ - lib/mupdf/page.rb
56
58
  - lib/mupdf/version.rb
57
59
  homepage: https://github.com/ksylvest/mupdf
58
60
  licenses:
@@ -60,8 +62,8 @@ licenses:
60
62
  metadata:
61
63
  rubygems_mfa_required: 'true'
62
64
  homepage_uri: https://github.com/ksylvest/mupdf
63
- source_code_uri: https://github.com/ksylvest/mupdf/tree/v0.3.0
64
- changelog_uri: https://github.com/ksylvest/mupdf/releases/tag/v0.3.0
65
+ source_code_uri: https://github.com/ksylvest/mupdf/tree/v0.4.0
66
+ changelog_uri: https://github.com/ksylvest/mupdf/releases/tag/v0.4.0
65
67
  documentation_uri: https://mupdf.ksylvest.com/
66
68
  post_install_message:
67
69
  rdoc_options: []