mupdf 0.2.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 +4 -4
- data/Gemfile +1 -0
- data/README.md +28 -1
- data/lib/mupdf/box.rb +72 -0
- data/lib/mupdf/document.rb +36 -0
- data/lib/mupdf/info.rb +25 -0
- data/lib/mupdf/page.rb +113 -0
- data/lib/mupdf/version.rb +1 -1
- metadata +8 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b7668e9689741bc823e304c97df7c135f9ed31453fedbed2d5f06e19a8be3f43
|
4
|
+
data.tar.gz: ee5b8c0c417dee4b2e80a063b06fb2699753e1857e9c6c11e0d50d7448815e81
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2b85d9364a453fabcce166ef780a6340a885d88022765488de11a09c080ff6e4770d9c92609b1a531bea062b6ceb08d1ea46035a4d8db119065d6302f49b5326
|
7
|
+
data.tar.gz: a315ed8e2f213ed39a5cfe45cd742d80d73d65c8fa37bdbf3e16d96a433a34a9490ac3ec454e2efcf9e86a6dfac0abc30c9dae2406f9a18e82fa8e92cbfc94df
|
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -14,6 +14,33 @@ gem install mupdf
|
|
14
14
|
|
15
15
|
## Usage
|
16
16
|
|
17
|
+
### Document
|
18
|
+
|
19
|
+
A `MuPDF::Document` wraps a PDF file:
|
20
|
+
|
21
|
+
```ruby
|
22
|
+
document = MuPDF::Document.new('./file.pdf')
|
23
|
+
```
|
24
|
+
|
25
|
+
#### Info
|
26
|
+
|
27
|
+
The `info` command displays information about the document such as the number of pages:
|
28
|
+
|
29
|
+
```ruby
|
30
|
+
info = document.info
|
31
|
+
info.pages # e.g. 2
|
32
|
+
```
|
33
|
+
|
34
|
+
#### Pages
|
35
|
+
|
36
|
+
The `pages` command finds sizing information about the pages within a document:
|
37
|
+
|
17
38
|
```ruby
|
18
|
-
|
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
|
19
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
|
@@ -0,0 +1,36 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module MuPDF
|
4
|
+
# A wrapper for a PDF document allowing for MuPDF APIs.
|
5
|
+
class Document
|
6
|
+
# @param pathname [Pathname]
|
7
|
+
def initialize(pathname)
|
8
|
+
@pathname = pathname
|
9
|
+
end
|
10
|
+
|
11
|
+
# @return [String]
|
12
|
+
def inspect
|
13
|
+
"#<#{self.class.name} pathname=#{@pathname}>"
|
14
|
+
end
|
15
|
+
|
16
|
+
# @raise [MuPDF::CommandError]
|
17
|
+
#
|
18
|
+
# @return [MuPDF::Info]
|
19
|
+
def info
|
20
|
+
@info ||= begin
|
21
|
+
result = MuPDF.mutool('info', String(@pathname))
|
22
|
+
MuPDF::Info.parse(result)
|
23
|
+
end
|
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
|
35
|
+
end
|
36
|
+
end
|
data/lib/mupdf/info.rb
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module MuPDF
|
4
|
+
# A wrapper for the result of a `mutool info ...` command.
|
5
|
+
class Info
|
6
|
+
PARSE_REGEX = /^Pages: (?<pages>\d+)$/
|
7
|
+
|
8
|
+
# @!attribute description
|
9
|
+
# @return [String] The description of the task.
|
10
|
+
attr_reader :pages
|
11
|
+
|
12
|
+
# @param text [String]
|
13
|
+
#
|
14
|
+
# @return [MuPDF::Info]
|
15
|
+
def self.parse(text)
|
16
|
+
match = text.match(PARSE_REGEX)
|
17
|
+
new(pages: Integer(match[:pages]))
|
18
|
+
end
|
19
|
+
|
20
|
+
# @param pages [Integer]
|
21
|
+
def initialize(pages:)
|
22
|
+
@pages = pages
|
23
|
+
end
|
24
|
+
end
|
25
|
+
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
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.
|
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-
|
11
|
+
date: 2025-01-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: open3
|
@@ -50,7 +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
|
55
|
+
- lib/mupdf/document.rb
|
56
|
+
- lib/mupdf/info.rb
|
57
|
+
- lib/mupdf/page.rb
|
54
58
|
- lib/mupdf/version.rb
|
55
59
|
homepage: https://github.com/ksylvest/mupdf
|
56
60
|
licenses:
|
@@ -58,8 +62,8 @@ licenses:
|
|
58
62
|
metadata:
|
59
63
|
rubygems_mfa_required: 'true'
|
60
64
|
homepage_uri: https://github.com/ksylvest/mupdf
|
61
|
-
source_code_uri: https://github.com/ksylvest/mupdf/tree/v0.
|
62
|
-
changelog_uri: https://github.com/ksylvest/mupdf/releases/tag/v0.
|
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
|
63
67
|
documentation_uri: https://mupdf.ksylvest.com/
|
64
68
|
post_install_message:
|
65
69
|
rdoc_options: []
|