substrate 0.4.0 → 0.4.1
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:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: b2f33f7baead2c71d256553e5b8fbcadd463a14c4b6a2b4890c732afc3ef40f8
|
|
4
|
+
data.tar.gz: 91655afe2c2fd644fdd6077b1c2baeeddf256f55ab34729f3ad2345b40c32268
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 8c9af828c81fae8e010ef5a488e9f804d2c7f865f4483d1e8f96eda852589b0b2e9ebd9139e831087934b07ef78d92b901db92d9d8f239b430efa77bbf98b01c
|
|
7
|
+
data.tar.gz: 16025303d9a1cf09192aff457898573fa17cfad03743ae2ad4d6c9e5dece98afb712020d7aa02db8f0dd47afc7c6b03817e3c2ce901c36be4a4f6d15d491eae4
|
|
@@ -36,6 +36,7 @@ module Substrate
|
|
|
36
36
|
def extract_document
|
|
37
37
|
StringIO.open(@bytes) do |io|
|
|
38
38
|
document = HexaPDF::Document.new(io: io, config: {"document.auto_decrypt" => false, "filter.map" => {BrotliDecode: nil}})
|
|
39
|
+
configure_unicode_mapping(document)
|
|
39
40
|
raise Mediums::Pdf::EncryptedPdf, "Encrypted PDFs are not supported" if document.encrypted?
|
|
40
41
|
objects = page_objects(document)
|
|
41
42
|
pages = extract_pages(objects)
|
|
@@ -47,6 +48,22 @@ module Substrate
|
|
|
47
48
|
raise Mediums::Pdf::InvalidPdf, "PDF parsing or page extraction failed", cause: error
|
|
48
49
|
end
|
|
49
50
|
|
|
51
|
+
def configure_unicode_mapping(document)
|
|
52
|
+
fallback = document.config["font.on_missing_unicode_mapping"]
|
|
53
|
+
document.config["font.on_missing_unicode_mapping"] = lambda do |code, font|
|
|
54
|
+
name = font.encoding.name(code) if font.is_a?(HexaPDF::Type::FontSimple)
|
|
55
|
+
base, suffix = name.to_s.split(".", 2)
|
|
56
|
+
mapped = glyph_unicode(base) if suffix && !base.empty?
|
|
57
|
+
mapped || fallback.call(code, font)
|
|
58
|
+
end
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
def glyph_unicode(name)
|
|
62
|
+
HexaPDF::Font::Encoding::GlyphList.name_to_unicode(name.to_sym)
|
|
63
|
+
rescue RangeError
|
|
64
|
+
nil
|
|
65
|
+
end
|
|
66
|
+
|
|
50
67
|
def page_objects(document)
|
|
51
68
|
root = document.catalog[:Pages]
|
|
52
69
|
raise Mediums::Pdf::InvalidPdf, "PDF page tree is missing" unless root.is_a?(HexaPDF::Dictionary)
|
|
@@ -3,7 +3,15 @@
|
|
|
3
3
|
module Substrate
|
|
4
4
|
module Mediums
|
|
5
5
|
module Pdf
|
|
6
|
-
OutlineSpan = Data.define(:title, :range, :parent_index)
|
|
6
|
+
OutlineSpan = Data.define(:title, :range, :parent_index, :front_matter) do
|
|
7
|
+
def initialize(title:, range:, parent_index:, front_matter: false)
|
|
8
|
+
super
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def front_matter?
|
|
12
|
+
front_matter
|
|
13
|
+
end
|
|
14
|
+
end
|
|
7
15
|
end
|
|
8
16
|
end
|
|
9
17
|
end
|
|
@@ -23,8 +23,13 @@ module Substrate
|
|
|
23
23
|
|
|
24
24
|
def resolve_spans
|
|
25
25
|
@page_separator = @page_separator.dup.freeze unless @page_separator.frozen?
|
|
26
|
-
|
|
27
|
-
|
|
26
|
+
entries = root_entries
|
|
27
|
+
roots = entries.map { |entry| root_anchor(entry, false) }
|
|
28
|
+
return success(payload: outline_spans(roots)) if reliable_roots?(roots, false)
|
|
29
|
+
|
|
30
|
+
corroborated = corroborated_roots?(entries)
|
|
31
|
+
roots = entries.map { |entry| root_anchor(entry, corroborated) }
|
|
32
|
+
success(payload: reliable_roots?(roots, corroborated) ? outline_spans(roots) : nil)
|
|
28
33
|
end
|
|
29
34
|
|
|
30
35
|
def root_entries
|
|
@@ -32,15 +37,19 @@ module Substrate
|
|
|
32
37
|
entries_by_parent.fetch(nil, [])
|
|
33
38
|
end
|
|
34
39
|
|
|
35
|
-
def reliable_roots?(roots)
|
|
40
|
+
def reliable_roots?(roots, corroborated)
|
|
36
41
|
return false if roots.empty? || roots.any?(&:nil?)
|
|
37
|
-
roots.first.start == [1, 0] && ordered?(roots.map(&:start))
|
|
42
|
+
(roots.first.start == [1, 0] || corroborated) && ordered?(roots.map(&:start))
|
|
38
43
|
end
|
|
39
44
|
|
|
40
45
|
def outline_spans(roots)
|
|
41
46
|
@remaining_subsection_bytes = limits.text_bytes
|
|
42
47
|
pending = sibling_work(roots, [content.pages.length + 1, 0], nil, 0)
|
|
43
48
|
[].tap do |spans|
|
|
49
|
+
if roots.first.start != [1, 0]
|
|
50
|
+
range = TextRange.new(start: [1, 0], finish: roots.first.start, page_separator: page_separator)
|
|
51
|
+
spans << OutlineSpan.new(title: "Front matter", range: range, parent_index: nil, front_matter: true)
|
|
52
|
+
end
|
|
44
53
|
while (work = pending.pop)
|
|
45
54
|
anchor, finish, parent_index, depth = work
|
|
46
55
|
span_index = spans.length
|
|
@@ -120,11 +129,52 @@ module Substrate
|
|
|
120
129
|
content.outline.entries.select { |entry| entry.parent_id && subsection_destination?(entry) }
|
|
121
130
|
end
|
|
122
131
|
|
|
123
|
-
def root_anchor(entry)
|
|
124
|
-
return unless page_boundary.match?(entry)
|
|
132
|
+
def root_anchor(entry, corroborated)
|
|
133
|
+
return unless page_boundary.match?(entry) || corroborated
|
|
125
134
|
Anchor.new(entry: entry, start: [entry.destination.page_number, 0], heading_end: nil)
|
|
126
135
|
end
|
|
127
136
|
|
|
137
|
+
def corroborated_roots?(entries)
|
|
138
|
+
return false if entries.empty?
|
|
139
|
+
pages = entries.map { |entry| entry.destination.page_number }
|
|
140
|
+
return false unless pages.all? { |page| page.is_a?(Integer) && page.positive? } && ordered?(pages)
|
|
141
|
+
entries.each_with_index.all? do |entry, index|
|
|
142
|
+
corroborated_root?(entry, index + 1)
|
|
143
|
+
end
|
|
144
|
+
end
|
|
145
|
+
|
|
146
|
+
def corroborated_root?(entry, ordinal)
|
|
147
|
+
destination = entry.destination
|
|
148
|
+
return false if entry.title.bytesize > limits.title_bytes || entry.title.strip.empty?
|
|
149
|
+
return false if destination.kind != :local || destination.fit != :XYZ
|
|
150
|
+
return false unless destination.parameters.length == 3
|
|
151
|
+
page = content.pages[destination.page_number.to_i - 1] if destination.page_number.to_i.positive?
|
|
152
|
+
return false unless page&.crop_box
|
|
153
|
+
left, top, = destination.parameters
|
|
154
|
+
box = page.crop_box
|
|
155
|
+
return false unless left.is_a?(Numeric) && top.is_a?(Numeric)
|
|
156
|
+
return false unless left.between?(box.left, box.right) && top.between?(box.bottom, box.top)
|
|
157
|
+
|
|
158
|
+
heading = normalize_heading("#{ordinal} #{entry.title}")
|
|
159
|
+
found = false
|
|
160
|
+
nonblank = 0
|
|
161
|
+
page.text.each_line("\n") do |line|
|
|
162
|
+
normalized = normalize_heading(line)
|
|
163
|
+
next if normalized.empty?
|
|
164
|
+
nonblank += 1
|
|
165
|
+
if normalized == heading
|
|
166
|
+
return false if found || nonblank > 3
|
|
167
|
+
found = true
|
|
168
|
+
end
|
|
169
|
+
return false if nonblank >= 3 && !found
|
|
170
|
+
end
|
|
171
|
+
found
|
|
172
|
+
end
|
|
173
|
+
|
|
174
|
+
def normalize_heading(text)
|
|
175
|
+
text.unicode_normalize(:nfc).gsub(/[[:space:]]+/, " ").strip
|
|
176
|
+
end
|
|
177
|
+
|
|
128
178
|
def child_anchor(entry)
|
|
129
179
|
return unless subsection_destination?(entry)
|
|
130
180
|
page = entry.destination.page_number
|
data/lib/substrate/version.rb
CHANGED