gamba 2.0.2 → 2.1.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/CHANGELOG.md +29 -1
- data/README.md +148 -27
- data/lib/gamba/annotations.rb +5 -0
- data/lib/gamba/barcodes/data_matrix.rb +204 -0
- data/lib/gamba/barcodes/linear.rb +182 -0
- data/lib/gamba/barcodes/qr_code.rb +339 -0
- data/lib/gamba/barcodes.rb +136 -0
- data/lib/gamba/combine.rb +13 -135
- data/lib/gamba/copier.rb +107 -0
- data/lib/gamba/document/internals.rb +5 -1
- data/lib/gamba/document.rb +33 -4
- data/lib/gamba/font.rb +7 -3
- data/lib/gamba/fonts/afm.rb +3 -2
- data/lib/gamba/fonts/ttf.rb +26 -20
- data/lib/gamba/images.rb +23 -1
- data/lib/gamba/imported_page.rb +137 -0
- data/lib/gamba/minitest.rb +35 -0
- data/lib/gamba/native/band.rb +56 -0
- data/lib/gamba/native/document.rb +122 -0
- data/lib/gamba/native/layout.rb +1063 -0
- data/lib/gamba/native/table.rb +189 -0
- data/lib/gamba/page_boxes.rb +68 -0
- data/lib/gamba/pdf/reader.rb +8 -2
- data/lib/gamba/pdf_a.rb +61 -20
- data/lib/gamba/pdf_check.rb +220 -0
- data/lib/gamba/railtie.rb +52 -0
- data/lib/gamba/rspec.rb +58 -0
- data/lib/gamba/table/cell/kinds.rb +45 -5
- data/lib/gamba/table/cell.rb +37 -0
- data/lib/gamba/table.rb +7 -0
- data/lib/gamba/tagging.rb +302 -0
- data/lib/gamba/testing.rb +79 -0
- data/lib/gamba/text/formatted/box.rb +5 -0
- data/lib/gamba/text/formatted/wrap.rb +1 -1
- data/lib/gamba/true_type/open_type_layout.rb +44 -17
- data/lib/gamba/true_type/simple_subsets.rb +11 -7
- data/lib/gamba/version.rb +1 -1
- data/lib/gamba.rb +18 -1
- metadata +24 -6
data/lib/gamba/combine.rb
CHANGED
|
@@ -4,18 +4,11 @@ module Gamba
|
|
|
4
4
|
# Each page keeps its resources and content streams as they were encoded; only the page dictionaries and the
|
|
5
5
|
# document structure are written anew. Outlines, forms and tagged structure of the sources are not carried over.
|
|
6
6
|
class Combine
|
|
7
|
+
include PageBoxes
|
|
8
|
+
|
|
7
9
|
BOXES = %i[MediaBox CropBox BleedBox TrimBox ArtBox].freeze
|
|
8
|
-
DEFAULT_MEDIA_BOX = [0.0, 0.0, 612.0, 792.0].freeze
|
|
9
10
|
# A quarter turn counterclockwise, the way combine_pdf turns a page to the requested orientation.
|
|
10
|
-
QUARTER_TURN =
|
|
11
|
-
ROTATIONS = {
|
|
12
|
-
90 => ->(width, height) { [0.0, -1.0, 1.0, 0.0, 0.0, width] },
|
|
13
|
-
180 => ->(width, height) { [-1.0, 0.0, 0.0, -1.0, width, height] },
|
|
14
|
-
270 => QUARTER_TURN
|
|
15
|
-
}.freeze
|
|
16
|
-
# References followed to tell whether two objects are the same: a chain deeper than this is copied unshared,
|
|
17
|
-
# instead of overflowing the stack on a file that links its objects into a list as long as it likes.
|
|
18
|
-
MAX_DESCRIBED_DEPTH = 32
|
|
11
|
+
QUARTER_TURN = ROTATIONS.fetch(270)
|
|
19
12
|
|
|
20
13
|
# page_size: a name from PDF::Core::PageGeometry::SIZES or [width, height] in points; each page is scaled to fit
|
|
21
14
|
# it, keeping its proportions, from the lower left corner. orientation: :portrait or :landscape; a page the other
|
|
@@ -64,122 +57,41 @@ module Gamba
|
|
|
64
57
|
# Page references first, so a link from one page to another lands on the copy instead of dragging the old tree.
|
|
65
58
|
def copy_pages(reader)
|
|
66
59
|
@reader = reader
|
|
67
|
-
@
|
|
68
|
-
@pending = []
|
|
69
|
-
@content_identifiers = {}
|
|
70
|
-
@description_depth = 0
|
|
60
|
+
@copier = Copier.new(reader, shared: @shared, identifiers: @identifiers) { reference(_1) }
|
|
71
61
|
pages = reader.pages.map do |source_reference, page|
|
|
72
62
|
copy = reference(nil)
|
|
73
|
-
if source_reference.is_a?(PDF::Core::Reader::Ref)
|
|
74
|
-
@copies[source_reference.number] = copy
|
|
75
|
-
@content_identifiers[source_reference.number] = nil
|
|
76
|
-
end
|
|
63
|
+
@copier.place(source_reference.number, copy) if source_reference.is_a?(PDF::Core::Reader::Ref)
|
|
77
64
|
[copy, page]
|
|
78
65
|
end
|
|
79
66
|
annotations_to_move = []
|
|
80
67
|
pages.each do |copy, page|
|
|
81
68
|
matrix, *sizes = transformation(page)
|
|
82
|
-
copy.data =
|
|
69
|
+
copy.data = @copier.copy(matrix ? page.except(:Parent, :B, *BOXES, :Rotate, :Contents) : page.except(:Parent, :B))
|
|
83
70
|
copy.data[:Parent] = @pages_reference
|
|
71
|
+
# A page names its resources even when it has none (ISO 32000-1 §7.7.3.3): qpdf 12.4 repairs a page without.
|
|
72
|
+
copy.data[:Resources] ||= {}
|
|
84
73
|
if matrix
|
|
85
74
|
reshape(copy.data, page, matrix, *sizes)
|
|
86
75
|
annotations_to_move << [copy.data[:Annots], matrix] if copy.data[:Annots]
|
|
87
76
|
end
|
|
88
77
|
@kids << copy
|
|
89
78
|
end
|
|
90
|
-
|
|
79
|
+
@copier.finish
|
|
91
80
|
moved = {}.compare_by_identity
|
|
92
81
|
annotations_to_move.each { |annotations, matrix| move_annotations(annotations, matrix, moved) }
|
|
93
82
|
reader.version
|
|
94
83
|
end
|
|
95
84
|
|
|
96
|
-
def copy_value(value)
|
|
97
|
-
case value
|
|
98
|
-
when PDF::Core::Reader::Ref
|
|
99
|
-
@copies[value.number] ||= begin
|
|
100
|
-
identifier = content_identifier(value.number)
|
|
101
|
-
identifier ? (@shared[identifier] ||= pending_copy(value.number)) : pending_copy(value.number)
|
|
102
|
-
end
|
|
103
|
-
when Hash then value.transform_values { copy_value(_1) }
|
|
104
|
-
when Array then value.map { copy_value(_1) }
|
|
105
|
-
else value
|
|
106
|
-
end
|
|
107
|
-
end
|
|
108
|
-
|
|
109
|
-
def pending_copy(number)
|
|
110
|
-
reference(nil).tap { @pending << [number, _1] }
|
|
111
|
-
end
|
|
112
|
-
|
|
113
|
-
# Objects with the same content, references included, are written once: the fonts, images and glyph procedures a
|
|
114
|
-
# carrier repeats on every label. Pages, and annotations (whose place is their own), are never shared.
|
|
115
|
-
def content_identifier(number)
|
|
116
|
-
return @content_identifiers[number] if @content_identifiers.key?(number)
|
|
117
|
-
return nil if @description_depth >= MAX_DESCRIBED_DEPTH
|
|
118
|
-
|
|
119
|
-
@content_identifiers[number] = nil # an object reached again while describing it stays unshared
|
|
120
|
-
@description_depth += 1
|
|
121
|
-
begin
|
|
122
|
-
value = @reader.object(number)
|
|
123
|
-
key = if value.is_a?(PDF::Core::Reader::Stream)
|
|
124
|
-
description = describe(value.dictionary.except(:Length))
|
|
125
|
-
[description, value.data] if description
|
|
126
|
-
elsif !(value.is_a?(Hash) && value.key?(:Rect))
|
|
127
|
-
describe(value)
|
|
128
|
-
end
|
|
129
|
-
ensure
|
|
130
|
-
@description_depth -= 1
|
|
131
|
-
end
|
|
132
|
-
@content_identifiers[number] = key && (@identifiers[key] ||= @identifiers.size)
|
|
133
|
-
end
|
|
134
|
-
|
|
135
|
-
def describe(value, description = +"")
|
|
136
|
-
case value
|
|
137
|
-
when PDF::Core::Reader::Ref
|
|
138
|
-
identifier = content_identifier(value.number)
|
|
139
|
-
identifier && description << "R" << identifier.to_s << " "
|
|
140
|
-
when Hash
|
|
141
|
-
description << "<<"
|
|
142
|
-
value.each { |key, entry| describe(entry, description << PDF::Core.escape_name(key.name) << " ") || (return nil) }
|
|
143
|
-
description << ">>"
|
|
144
|
-
when Array
|
|
145
|
-
description << "["
|
|
146
|
-
value.each { describe(_1, description) || (return nil) }
|
|
147
|
-
description << "]"
|
|
148
|
-
when PDF::Core::ByteString then description << "(" << value.bytesize.to_s << ":" << value << ")"
|
|
149
|
-
when PDF::Core::Name then description << PDF::Core.escape_name(value.value) << " "
|
|
150
|
-
when PDF::Core::Real then description << value.source << " "
|
|
151
|
-
else description << value.inspect << " "
|
|
152
|
-
end
|
|
153
|
-
end
|
|
154
|
-
|
|
155
|
-
def copy_pending
|
|
156
|
-
until @pending.empty?
|
|
157
|
-
number, copy = @pending.pop
|
|
158
|
-
value = @reader.object(number)
|
|
159
|
-
if value.is_a?(PDF::Core::Reader::Stream)
|
|
160
|
-
dictionary = value.dictionary.except(:Length)
|
|
161
|
-
# The writer packs streams without bytes as plain objects: keep one byte of data instead.
|
|
162
|
-
dictionary = dictionary.except(:Filter, :DecodeParms) if value.data.empty?
|
|
163
|
-
copy.data = copy_value(dictionary)
|
|
164
|
-
copy.stream = value.data.empty? ? +" " : +value.data
|
|
165
|
-
else
|
|
166
|
-
copy.data = copy_value(value)
|
|
167
|
-
end
|
|
168
|
-
end
|
|
169
|
-
end
|
|
170
|
-
|
|
171
85
|
# The matrix that draws the page's visible box at its new place, and the new page's size; nil when it stays put.
|
|
172
86
|
def transformation(page)
|
|
173
87
|
return nil unless @page_size || @orientation
|
|
174
88
|
|
|
175
|
-
media =
|
|
176
|
-
|
|
177
|
-
visible = crop ? intersection(crop, media) : media
|
|
89
|
+
media = media_box(page)
|
|
90
|
+
visible = visible_box(page)
|
|
178
91
|
left, bottom, right, top = visible
|
|
179
92
|
width = right - left
|
|
180
93
|
height = top - bottom
|
|
181
|
-
rotation =
|
|
182
|
-
rotation = 0 unless (rotation % 90).zero?
|
|
94
|
+
rotation = rotation(page)
|
|
183
95
|
shown_width, shown_height = (rotation % 180).zero? ? [width, height] : [height, width]
|
|
184
96
|
turn = @orientation && shown_width != shown_height && (shown_width > shown_height) != (@orientation == :landscape)
|
|
185
97
|
final_width, final_height = turn ? [shown_height, shown_width] : [shown_width, shown_height]
|
|
@@ -208,7 +120,7 @@ module Gamba
|
|
|
208
120
|
def contents(value)
|
|
209
121
|
resolved = @reader.resolve(value)
|
|
210
122
|
value = resolved if resolved.is_a?(Array)
|
|
211
|
-
Array(value).filter_map { |content|
|
|
123
|
+
Array(value).filter_map { |content| @copier.copy(content) if content.is_a?(PDF::Core::Reader::Ref) }
|
|
212
124
|
end
|
|
213
125
|
|
|
214
126
|
# Annotation rectangles follow the content they sit on.
|
|
@@ -260,18 +172,6 @@ module Gamba
|
|
|
260
172
|
numbers.all? ? numbers : nil
|
|
261
173
|
end
|
|
262
174
|
|
|
263
|
-
def box(value)
|
|
264
|
-
values = @reader.resolve(value)
|
|
265
|
-
return nil unless values.is_a?(Array) && values.size == 4
|
|
266
|
-
|
|
267
|
-
numbers = values.map { number(_1) }
|
|
268
|
-
return nil if numbers.any?(&:nil?) || numbers[0] == numbers[2] || numbers[1] == numbers[3]
|
|
269
|
-
|
|
270
|
-
left, right = [numbers[0], numbers[2]].minmax
|
|
271
|
-
bottom, top = [numbers[1], numbers[3]].minmax
|
|
272
|
-
[left, bottom, right, top]
|
|
273
|
-
end
|
|
274
|
-
|
|
275
175
|
def plain_box(values)
|
|
276
176
|
return nil unless values.is_a?(Array) && values.size == 4
|
|
277
177
|
|
|
@@ -279,28 +179,6 @@ module Gamba
|
|
|
279
179
|
numbers.any?(&:nil?) ? nil : numbers
|
|
280
180
|
end
|
|
281
181
|
|
|
282
|
-
def number(value)
|
|
283
|
-
numeric(@reader.resolve(value))
|
|
284
|
-
end
|
|
285
|
-
|
|
286
|
-
# A real too large for a Float reads as infinite, and would write NaN into the page's matrix: no number then.
|
|
287
|
-
def numeric(value)
|
|
288
|
-
number = (value.is_a?(PDF::Core::Real) || value.is_a?(Numeric)) ? value.to_f : nil
|
|
289
|
-
number if number&.finite?
|
|
290
|
-
end
|
|
291
|
-
|
|
292
|
-
def intersection(crop, media)
|
|
293
|
-
left, bottom = [crop[0], media[0]].max, [crop[1], media[1]].max
|
|
294
|
-
right, top = [crop[2], media[2]].min, [crop[3], media[3]].min
|
|
295
|
-
(right > left && top > bottom) ? [left, bottom, right, top] : media
|
|
296
|
-
end
|
|
297
|
-
|
|
298
|
-
def multiply(first, second)
|
|
299
|
-
a, b, c, d, e, f = first
|
|
300
|
-
a2, b2, c2, d2, e2, f2 = second
|
|
301
|
-
[a * a2 + b * c2, a * b2 + b * d2, c * a2 + d * c2, c * b2 + d * d2, e * a2 + f * c2 + e2, e * b2 + f * d2 + f2]
|
|
302
|
-
end
|
|
303
|
-
|
|
304
182
|
def apply(matrix, x, y)
|
|
305
183
|
a, b, c, d, e, f = matrix
|
|
306
184
|
[a * x + c * y + e, b * x + d * y + f]
|
data/lib/gamba/copier.rb
ADDED
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Gamba
|
|
4
|
+
# Copies objects of a PDF written elsewhere into one being written, through the references a block makes: each
|
|
5
|
+
# object once, those with the same content (references included) once in all, the rest when #finish is called.
|
|
6
|
+
class Copier
|
|
7
|
+
# References followed to tell whether two objects are the same: a chain deeper than this is copied unshared,
|
|
8
|
+
# instead of overflowing the stack on a file that links its objects into a list as long as it likes.
|
|
9
|
+
MAX_DESCRIBED_DEPTH = 32
|
|
10
|
+
|
|
11
|
+
# shared and identifiers carry what was written from earlier files, so that a font they all use is written once.
|
|
12
|
+
def initialize(reader, shared: {}, identifiers: {}, &reference)
|
|
13
|
+
@reader = reader
|
|
14
|
+
@shared = shared
|
|
15
|
+
@identifiers = identifiers
|
|
16
|
+
@reference = reference
|
|
17
|
+
@copies = {}
|
|
18
|
+
@pending = []
|
|
19
|
+
@content_identifiers = {}
|
|
20
|
+
@description_depth = 0
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
# An object of the source written by the caller as `copy`, so that a link to it lands on that copy; never shared.
|
|
24
|
+
def place(number, copy)
|
|
25
|
+
@copies[number] = copy
|
|
26
|
+
@content_identifiers[number] = nil
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
# The value with each reference to the source replaced by one to its copy, written by #finish.
|
|
30
|
+
def copy(value)
|
|
31
|
+
case value
|
|
32
|
+
when PDF::Core::Reader::Ref
|
|
33
|
+
@copies[value.number] ||= begin
|
|
34
|
+
identifier = content_identifier(value.number)
|
|
35
|
+
identifier ? (@shared[identifier] ||= pending_copy(value.number)) : pending_copy(value.number)
|
|
36
|
+
end
|
|
37
|
+
when Hash then value.transform_values { copy(_1) }
|
|
38
|
+
when Array then value.map { copy(_1) }
|
|
39
|
+
else value
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def finish
|
|
44
|
+
until @pending.empty?
|
|
45
|
+
number, copy = @pending.pop
|
|
46
|
+
value = @reader.object(number)
|
|
47
|
+
if value.is_a?(PDF::Core::Reader::Stream)
|
|
48
|
+
dictionary = value.dictionary.except(:Length)
|
|
49
|
+
# The writer packs streams without bytes as plain objects: keep one byte of data instead.
|
|
50
|
+
dictionary = dictionary.except(:Filter, :DecodeParms) if value.data.empty?
|
|
51
|
+
copy.data = copy(dictionary)
|
|
52
|
+
copy.stream = value.data.empty? ? +" " : +value.data
|
|
53
|
+
else
|
|
54
|
+
copy.data = copy(value)
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
private
|
|
60
|
+
|
|
61
|
+
def pending_copy(number)
|
|
62
|
+
@reference.call(nil).tap { @pending << [number, _1] }
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
# Objects with the same content, references included, are written once: the fonts, images and glyph procedures a
|
|
66
|
+
# carrier repeats on every label. Pages, and annotations (whose place is their own), are never shared.
|
|
67
|
+
def content_identifier(number)
|
|
68
|
+
return @content_identifiers[number] if @content_identifiers.key?(number)
|
|
69
|
+
return nil if @description_depth >= MAX_DESCRIBED_DEPTH
|
|
70
|
+
|
|
71
|
+
@content_identifiers[number] = nil # an object reached again while describing it stays unshared
|
|
72
|
+
@description_depth += 1
|
|
73
|
+
begin
|
|
74
|
+
value = @reader.object(number)
|
|
75
|
+
key = if value.is_a?(PDF::Core::Reader::Stream)
|
|
76
|
+
description = describe(value.dictionary.except(:Length))
|
|
77
|
+
[description, value.data] if description
|
|
78
|
+
elsif !(value.is_a?(Hash) && value.key?(:Rect))
|
|
79
|
+
describe(value)
|
|
80
|
+
end
|
|
81
|
+
ensure
|
|
82
|
+
@description_depth -= 1
|
|
83
|
+
end
|
|
84
|
+
@content_identifiers[number] = key && (@identifiers[key] ||= @identifiers.size)
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
def describe(value, description = +"")
|
|
88
|
+
case value
|
|
89
|
+
when PDF::Core::Reader::Ref
|
|
90
|
+
identifier = content_identifier(value.number)
|
|
91
|
+
identifier && description << "R" << identifier.to_s << " "
|
|
92
|
+
when Hash
|
|
93
|
+
description << "<<"
|
|
94
|
+
value.each { |key, entry| describe(entry, description << PDF::Core.escape_name(key.name) << " ") || (return nil) }
|
|
95
|
+
description << ">>"
|
|
96
|
+
when Array
|
|
97
|
+
description << "["
|
|
98
|
+
value.each { describe(_1, description) || (return nil) }
|
|
99
|
+
description << "]"
|
|
100
|
+
when PDF::Core::ByteString then description << "(" << value.bytesize.to_s << ":" << value << ")"
|
|
101
|
+
when PDF::Core::Name then description << PDF::Core.escape_name(value.value) << " "
|
|
102
|
+
when PDF::Core::Real then description << value.source << " "
|
|
103
|
+
else description << value.inspect << " "
|
|
104
|
+
end
|
|
105
|
+
end
|
|
106
|
+
end
|
|
107
|
+
end
|
|
@@ -34,6 +34,7 @@ module Prawn
|
|
|
34
34
|
end
|
|
35
35
|
|
|
36
36
|
def add_content(string)
|
|
37
|
+
check_tagged_content(string) if @tagging
|
|
37
38
|
open_graphics_state if page.stack.empty?
|
|
38
39
|
page.content_stream << string << "\n"
|
|
39
40
|
end
|
|
@@ -227,6 +228,7 @@ module Prawn
|
|
|
227
228
|
@pages_reference = ref!(Type: :Pages, Count: 0, Kids: [])
|
|
228
229
|
@root.data[:Pages] = @pages_reference
|
|
229
230
|
@root.data[:ViewerPreferences] = {PrintScaling: :None} if options[:print_scaling] == :none
|
|
231
|
+
@root.data[:Lang] = PDF::Core::LiteralString.new(options[:lang]) if options[:lang]
|
|
230
232
|
@version = 1.3
|
|
231
233
|
# One entry per save not yet restored, document-wide.
|
|
232
234
|
@unrestored_graphics_states = []
|
|
@@ -255,9 +257,11 @@ module Prawn
|
|
|
255
257
|
page.finalize
|
|
256
258
|
end
|
|
257
259
|
@before_render_callbacks.each { _1.call(self) }
|
|
260
|
+
finalize_tagging if tagged?
|
|
258
261
|
finalize_pdf_a if pdf_a?
|
|
262
|
+
finalize_metadata if pdf_a? || pdf_ua?
|
|
259
263
|
PDF::Core::Writer.call(objects: @objects, root: @root, info: @info, version: @version, trailer: @trailer, encryption: @encryption,
|
|
260
|
-
identified: pdf_a?)
|
|
264
|
+
identified: pdf_a? || pdf_ua?)
|
|
261
265
|
end
|
|
262
266
|
end
|
|
263
267
|
end
|
data/lib/gamba/document.rb
CHANGED
|
@@ -10,17 +10,22 @@ module Prawn
|
|
|
10
10
|
include PDF::Core::Destinations
|
|
11
11
|
include Security
|
|
12
12
|
include Images
|
|
13
|
+
include Barcodes
|
|
13
14
|
include Gamba::PdfA
|
|
15
|
+
include Gamba::Tagging
|
|
14
16
|
include Table::Interface
|
|
15
17
|
include Stamp
|
|
16
18
|
include SoftMask
|
|
17
19
|
|
|
18
20
|
VALID_OPTIONS = %i[
|
|
19
21
|
page_size page_layout margin left_margin right_margin top_margin bottom_margin skip_page_creation compress
|
|
20
|
-
background info text_formatter print_scaling pdf_a
|
|
22
|
+
background info text_formatter print_scaling pdf_a pdf lang background_pdf background_pdf_page
|
|
21
23
|
].freeze
|
|
22
24
|
|
|
23
25
|
attr_accessor :margin_box, :page_number, :text_formatter
|
|
26
|
+
# The font used until one is set, Helvetica unless told: PDF/A and PDF/UA documents of Gamba's own API take
|
|
27
|
+
# their default style's.
|
|
28
|
+
attr_writer :default_font_family
|
|
24
29
|
attr_reader :margins, :y
|
|
25
30
|
|
|
26
31
|
# Modules every new document extends itself with: a subclass starts with its parent's.
|
|
@@ -43,9 +48,12 @@ module Prawn
|
|
|
43
48
|
self.class.extensions.reverse_each { extend(_1) }
|
|
44
49
|
|
|
45
50
|
initialize_internals(options)
|
|
46
|
-
@
|
|
51
|
+
@pdf_a_conformance = Gamba::PdfA.conformance(options.delete(:pdf_a))
|
|
52
|
+
initialize_tagging(options)
|
|
47
53
|
@background = options[:background]
|
|
48
54
|
@background_scale = options[:background_scale] || 1
|
|
55
|
+
@background_pdf = options[:background_pdf]
|
|
56
|
+
@background_pdf_page = options[:background_pdf_page] || 1
|
|
49
57
|
@font_size = 12
|
|
50
58
|
@bounding_box = nil
|
|
51
59
|
@margin_box = nil
|
|
@@ -63,6 +71,7 @@ module Prawn
|
|
|
63
71
|
end
|
|
64
72
|
|
|
65
73
|
def start_new_page(options = {})
|
|
74
|
+
close_marks_on_page
|
|
66
75
|
last_page = page
|
|
67
76
|
page_options = {size: options[:size] || last_page&.size, layout: options[:layout] || last_page&.layout, margins: last_page&.margins&.dup}
|
|
68
77
|
if last_page
|
|
@@ -84,9 +93,10 @@ module Prawn
|
|
|
84
93
|
|
|
85
94
|
insert_page(page, @page_number)
|
|
86
95
|
@page_number += 1
|
|
87
|
-
canvas { image(@background, scale: @background_scale, at: bounds.top_left) } if @background
|
|
96
|
+
tag_artifact { canvas { image(@background, scale: @background_scale, at: bounds.top_left) } } if @background
|
|
97
|
+
draw_background_pdf(options)
|
|
88
98
|
@y = @bounding_box.absolute_top
|
|
89
|
-
float { @on_page_create&.call(self) }
|
|
99
|
+
float { tag_artifact { @on_page_create&.call(self) } }
|
|
90
100
|
header_blend_mode = page.stack.last&.blend_mode
|
|
91
101
|
reopen_ext_gstate_blocks
|
|
92
102
|
# The blend mode in effect on the last page, which the new one resets to Normal (ISO 32000-1 §8.4.1), once the
|
|
@@ -111,6 +121,7 @@ module Prawn
|
|
|
111
121
|
end
|
|
112
122
|
|
|
113
123
|
def go_to_page(page_number)
|
|
124
|
+
close_marks_on_page
|
|
114
125
|
@page_number = page_number
|
|
115
126
|
@page = pages[page_number - 1]
|
|
116
127
|
generate_margin_box
|
|
@@ -161,6 +172,17 @@ module Prawn
|
|
|
161
172
|
File.binwrite(filename, render)
|
|
162
173
|
end
|
|
163
174
|
|
|
175
|
+
# Gamba's own API on this document, from the cursor where it stands: gamba { |document| document.text "…" }.
|
|
176
|
+
def gamba
|
|
177
|
+
@gamba ||= Gamba::Document.new(self)
|
|
178
|
+
if block_given?
|
|
179
|
+
yield @gamba
|
|
180
|
+
# Boxes kept with a next one the block never added are placed before Prawn's code goes on.
|
|
181
|
+
@gamba.prawn
|
|
182
|
+
end
|
|
183
|
+
@gamba
|
|
184
|
+
end
|
|
185
|
+
|
|
164
186
|
def bounds
|
|
165
187
|
@bounding_box
|
|
166
188
|
end
|
|
@@ -229,6 +251,13 @@ module Prawn
|
|
|
229
251
|
|
|
230
252
|
private
|
|
231
253
|
|
|
254
|
+
# A page of another PDF under each page, at its own size from the top left corner: the document's, unless
|
|
255
|
+
# start_new_page names another (background_pdf:, background_pdf_page:) or none (background_pdf: false).
|
|
256
|
+
def draw_background_pdf(options)
|
|
257
|
+
source, number = options.key?(:background_pdf) ? [options[:background_pdf], options[:background_pdf_page] || 1] : [@background_pdf, @background_pdf_page]
|
|
258
|
+
tag_artifact { canvas { pdf_page(source, page: number, at: bounds.top_left) } } if source
|
|
259
|
+
end
|
|
260
|
+
|
|
232
261
|
# A gradient set as the fill or stroke stays it on the new page, in place of the colour Prawn writes.
|
|
233
262
|
def use_graphic_settings(override_settings = false)
|
|
234
263
|
write_fill_paint if graphic_state.fill_pattern || current_fill_color != "000000" || override_settings
|
data/lib/gamba/font.rb
CHANGED
|
@@ -10,7 +10,7 @@ module Prawn
|
|
|
10
10
|
}.freeze
|
|
11
11
|
|
|
12
12
|
def font(name = nil, options = DEFAULT_FONT_OPTIONS)
|
|
13
|
-
return @font || font("Helvetica") if name.nil?
|
|
13
|
+
return @font || font(@default_font_family || "Helvetica") if name.nil?
|
|
14
14
|
raise Errors::NotOnPage if pages.empty? && !page.in_stamp_stream?
|
|
15
15
|
|
|
16
16
|
new_font = find_font(name.to_s, options)
|
|
@@ -60,7 +60,7 @@ module Prawn
|
|
|
60
60
|
end
|
|
61
61
|
|
|
62
62
|
def save_font
|
|
63
|
-
@font ||= find_font("Helvetica")
|
|
63
|
+
@font ||= find_font(@default_font_family || "Helvetica")
|
|
64
64
|
original_font = @font
|
|
65
65
|
original_size = @font_size
|
|
66
66
|
yield
|
|
@@ -219,8 +219,12 @@ module Prawn
|
|
|
219
219
|
encoded_string = font.measured_string(string)
|
|
220
220
|
width = width_by_string(font, @document.font_size, options[:size], options[:kerning])[encoded_string] ||=
|
|
221
221
|
font.compute_width_of(encoded_string, options)
|
|
222
|
+
character_spacing = @document.character_spacing
|
|
223
|
+
# Without spacing the glyphs need no counting, which ligatures make a shaping pass.
|
|
224
|
+
return width if character_spacing.zero?
|
|
225
|
+
|
|
222
226
|
character_count = @document.font.character_count(encoded_string)
|
|
223
|
-
character_count.positive? ? width + (
|
|
227
|
+
character_count.positive? ? width + (character_spacing * (character_count - 1)) : width
|
|
224
228
|
end
|
|
225
229
|
|
|
226
230
|
private
|
data/lib/gamba/fonts/afm.rb
CHANGED
|
@@ -28,8 +28,9 @@ module Prawn
|
|
|
28
28
|
def initialize(document, name, options = {})
|
|
29
29
|
name ||= options[:family]
|
|
30
30
|
raise Errors::UnknownFont, "#{name} (#{options[:style] || "normal"}) is not a known font." unless BUILT_INS.include?(name)
|
|
31
|
-
if document.pdf_a?
|
|
32
|
-
|
|
31
|
+
if document.pdf_a? || document.pdf_ua?
|
|
32
|
+
error, standard = document.pdf_ua? ? [Errors::PdfUaViolation, "PDF/UA"] : [Errors::PdfAViolation, "PDF/A"]
|
|
33
|
+
raise error, "#{standard} embeds every font, which #{name}, a standard PDF font, cannot be: use a TrueType or OpenType font"
|
|
33
34
|
end
|
|
34
35
|
|
|
35
36
|
super
|
data/lib/gamba/fonts/ttf.rb
CHANGED
|
@@ -95,20 +95,7 @@ module Prawn
|
|
|
95
95
|
return options[:kerning] ? encode_kerned_codepoints(text.codepoints) : @simple_subsets.encode_codepoints(text.codepoints, cmap)
|
|
96
96
|
end
|
|
97
97
|
|
|
98
|
-
|
|
99
|
-
return @simple_subsets.encode(clusters) unless options[:kerning]
|
|
100
|
-
|
|
101
|
-
encoded = []
|
|
102
|
-
run = []
|
|
103
|
-
clusters.each do |cluster|
|
|
104
|
-
if run.any? && (offset = kerning_between(run.last[0], cluster[0]))
|
|
105
|
-
append_encoded_run(encoded, run)
|
|
106
|
-
encoded.last[1] = Array(encoded.last[1]) << -(offset * scale_factor)
|
|
107
|
-
run = []
|
|
108
|
-
end
|
|
109
|
-
run << cluster
|
|
110
|
-
end
|
|
111
|
-
append_encoded_run(encoded, run)
|
|
98
|
+
encode_shaped(text.codepoints, options[:kerning])
|
|
112
99
|
end
|
|
113
100
|
|
|
114
101
|
def basename
|
|
@@ -185,7 +172,11 @@ module Prawn
|
|
|
185
172
|
|
|
186
173
|
# Glyphs, which character spacing applies to: fewer than characters once ligatures join some.
|
|
187
174
|
def character_count(string)
|
|
188
|
-
|
|
175
|
+
return string.length unless @layout
|
|
176
|
+
|
|
177
|
+
count = 0
|
|
178
|
+
@layout.each_glyph(string.codepoints) { count += 1 }
|
|
179
|
+
count
|
|
189
180
|
end
|
|
190
181
|
|
|
191
182
|
private
|
|
@@ -241,7 +232,7 @@ module Prawn
|
|
|
241
232
|
# PDF word spacing (Tw) only reaches byte 32 of single-byte encodings (ISO 32000-1 §9.3.3): with two-byte
|
|
242
233
|
# codes, each space gets the same shift as a TJ offset instead.
|
|
243
234
|
def space_out_words(runs, word_spacing, font_size)
|
|
244
|
-
space =
|
|
235
|
+
space = glyph_string([32])
|
|
245
236
|
# From the spacing as Tw would write it, rounded to five decimals, so spaces land where Prawn's do.
|
|
246
237
|
offset = -(Float(PDF::Core.real(word_spacing)) * 1000.0 / font_size)
|
|
247
238
|
runs.flat_map do |run|
|
|
@@ -254,11 +245,12 @@ module Prawn
|
|
|
254
245
|
def shaped_width_of(string, options)
|
|
255
246
|
width = 0
|
|
256
247
|
previous_glyph = nil
|
|
257
|
-
|
|
248
|
+
codes = string.codepoints
|
|
249
|
+
@layout.each_glyph(codes) do |glyph, index, length|
|
|
258
250
|
if options[:kerning] && previous_glyph && (offset = kerning_between(previous_glyph, glyph))
|
|
259
251
|
width += offset * scale_factor
|
|
260
252
|
end
|
|
261
|
-
width += (codes ==
|
|
253
|
+
width += (length == 1 && codes[index] == LINE_FEED) ? 0.0 : Integer(@ttf.advance_widths[glyph] * scale_factor)
|
|
262
254
|
previous_glyph = glyph
|
|
263
255
|
end
|
|
264
256
|
width * ((options[:size] || size) / 1000.0)
|
|
@@ -303,8 +295,22 @@ module Prawn
|
|
|
303
295
|
end
|
|
304
296
|
|
|
305
297
|
# Runs of one subset merge into the previous part when it is of the same subset, as in Prawn's TJ arrays.
|
|
306
|
-
|
|
307
|
-
|
|
298
|
+
# Each glyph's code straight into the parts of its run, with no cluster built for it: text is drawn glyph by
|
|
299
|
+
# glyph, and most glyphs stand for one character.
|
|
300
|
+
def encode_shaped(codes, kerning)
|
|
301
|
+
encoded = []
|
|
302
|
+
run = []
|
|
303
|
+
previous_glyph = nil
|
|
304
|
+
@layout.each_glyph(codes) do |glyph, index, length|
|
|
305
|
+
if kerning && previous_glyph && (offset = kerning_between(previous_glyph, glyph))
|
|
306
|
+
merge_encoded_parts(encoded, run)
|
|
307
|
+
encoded.last[1] = Array(encoded.last[1]) << -(offset * scale_factor)
|
|
308
|
+
run = []
|
|
309
|
+
end
|
|
310
|
+
@simple_subsets.append_cluster(run, glyph, Gamba::TrueType::OpenTypeLayout.cluster_codes(codes, index, length))
|
|
311
|
+
previous_glyph = glyph
|
|
312
|
+
end
|
|
313
|
+
merge_encoded_parts(encoded, run)
|
|
308
314
|
end
|
|
309
315
|
|
|
310
316
|
def merge_encoded_parts(encoded, parts)
|
data/lib/gamba/images.rb
CHANGED
|
@@ -52,6 +52,24 @@ module Prawn
|
|
|
52
52
|
[entry[:obj], entry[:info]]
|
|
53
53
|
end
|
|
54
54
|
|
|
55
|
+
# A page of another PDF, placed and sized as an image is, written once however many times it is drawn: a
|
|
56
|
+
# letterhead, a form to fill in. source: a path, an IO, or the bytes of a PDF; page: from 1.
|
|
57
|
+
def pdf_page(source, options = {})
|
|
58
|
+
options = options.dup
|
|
59
|
+
number = options.delete(:page) || 1
|
|
60
|
+
Prawn.verify_options(%i[at position vposition height width scale fit], options)
|
|
61
|
+
pdf_object, info = build_pdf_page_object(source, number)
|
|
62
|
+
embed_image(pdf_object, info, options)
|
|
63
|
+
info
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
def build_pdf_page_object(source, number = 1)
|
|
67
|
+
bytes = Gamba::ImportedPage.bytes(source)
|
|
68
|
+
imported = (@imported_pdfs ||= {})[Digest::SHA1.hexdigest(bytes)] ||= {source: Gamba::ImportedPage.source(self, bytes), pages: {}}
|
|
69
|
+
page = imported[:pages][number] ||= Gamba::ImportedPage.new(self, imported[:source], number)
|
|
70
|
+
[page.form, page]
|
|
71
|
+
end
|
|
72
|
+
|
|
55
73
|
def embed_image(pdf_object, info, options)
|
|
56
74
|
width, height = info.calc_image_dimensions(options)
|
|
57
75
|
if options[:at]
|
|
@@ -62,12 +80,16 @@ module Prawn
|
|
|
62
80
|
end
|
|
63
81
|
label = "I#{@image_counter = (@image_counter || 0) + 1}"
|
|
64
82
|
page.xobjects[label] = pdf_object
|
|
65
|
-
|
|
83
|
+
matrix = info.respond_to?(:placement) ? info.placement(width, height, x, y - height) : [width, 0, 0, height, x, y - height]
|
|
84
|
+
add_content("\nq\n#{PDF::Core.real_params(matrix)} cm\n/#{label} Do\nQ")
|
|
66
85
|
end
|
|
67
86
|
|
|
68
87
|
private
|
|
69
88
|
|
|
89
|
+
# An IO, a path, or anything answering download, as Active Storage attachments and blobs do.
|
|
70
90
|
def verify_and_read_image(io_or_path)
|
|
91
|
+
return io_or_path.download if io_or_path.respond_to?(:download) && !io_or_path.respond_to?(:rewind)
|
|
92
|
+
|
|
71
93
|
if io_or_path.respond_to?(:rewind)
|
|
72
94
|
io_or_path.rewind
|
|
73
95
|
io_or_path.binmode if io_or_path.respond_to?(:binmode)
|