gamba 2.0.2 → 2.2.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 +47 -1
- data/README.md +174 -31
- 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 +36 -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/invoice/cross_industry_invoice.rb +255 -0
- data/lib/gamba/invoice/rules.rb +183 -0
- data/lib/gamba/invoice.rb +272 -0
- data/lib/gamba/minitest.rb +35 -0
- data/lib/gamba/native/band.rb +56 -0
- data/lib/gamba/native/document.rb +128 -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/decryption.rb +1 -20
- data/lib/gamba/pdf/reader.rb +8 -2
- data/lib/gamba/pdf/writer.rb +34 -5
- data/lib/gamba/pdf.rb +15 -1
- data/lib/gamba/pdf_a.rb +65 -21
- 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/security.rb +133 -5
- 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/watermark.rb +108 -0
- data/lib/gamba.rb +22 -1
- metadata +294 -260
|
@@ -0,0 +1,302 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Prawn
|
|
4
|
+
module Errors
|
|
5
|
+
PdfUaViolation = Class.new(StandardError)
|
|
6
|
+
end
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
module Gamba
|
|
10
|
+
# Tagged PDF (ISO 32000-1 §14.7 and §14.8), switched on with pdf: :ua (PDF/UA-1, ISO 14289-1) or pdf_a: :a
|
|
11
|
+
# (PDF/A-3a): a structure tree that says what the content is and in which order it reads, each piece of content
|
|
12
|
+
# drawn in a marked-content sequence naming its element, and what only decorates the page marked as an artifact.
|
|
13
|
+
# Content drawn outside both raises: a reader would never see it.
|
|
14
|
+
module Tagging
|
|
15
|
+
# A node of the structure tree: a standard structure type (P, H1, Table…), its attributes (Alt, Lang, A), and
|
|
16
|
+
# its kids, other elements or the marked-content sequences that hold its content, as [page, MCID].
|
|
17
|
+
class Element
|
|
18
|
+
attr_reader :type, :attributes, :kids
|
|
19
|
+
|
|
20
|
+
def initialize(type, parent, attributes = {})
|
|
21
|
+
@type = type
|
|
22
|
+
@attributes = attributes
|
|
23
|
+
@kids = []
|
|
24
|
+
parent&.kids&.push(self)
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
# A figure is read as one: its alternative text, not what it is drawn with.
|
|
28
|
+
def whole?
|
|
29
|
+
@type == :Figure
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
# The table row of this table element at a row index of Prawn's table, made when its first cell is drawn.
|
|
33
|
+
def row(index)
|
|
34
|
+
(@rows ||= {})[index] ||= Element.new(:TR, self)
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
# A marked-content sequence: around an element's content, or an artifact when element is nil. page is the page
|
|
39
|
+
# it is open on, nil once a page break closed it there: it opens again on the next page, when something is drawn.
|
|
40
|
+
Mark = Struct.new(:element, :properties, :page)
|
|
41
|
+
|
|
42
|
+
# Operators starting what paints the page: a text object, a path, an image, a shading (ISO 32000-1 §8.2). A
|
|
43
|
+
# marked-content sequence goes around them, never inside a text or a path object.
|
|
44
|
+
BEGINS_GRAPHICS_OBJECT = /(?:\A|\s)(?:BT|BI|Do|m|re|sh)$/
|
|
45
|
+
DOCUMENT_TYPE = :Document
|
|
46
|
+
# A BCP 47 language tag, as /Lang takes it: fr, en-GB, zh-Hant-TW.
|
|
47
|
+
LANGUAGE_TAG = /\A[A-Za-z]{1,8}(?:-[A-Za-z0-9]{1,8})*\z/
|
|
48
|
+
HEADING_LEVELS = {H1: 1, H2: 2, H3: 3, H4: 4, H5: 5, H6: 6}.freeze
|
|
49
|
+
# Something besides spaces and invisible format characters (zero-width space, byte order mark…).
|
|
50
|
+
VISIBLE = /[^[:space:]\p{Cf}]/
|
|
51
|
+
|
|
52
|
+
# Text a reader sees or hears: a title, an alt. A string in another encoding counts if it converts to UTF-8.
|
|
53
|
+
def self.visible_text?(value)
|
|
54
|
+
value.is_a?(String) && value.valid_encoding? && value.encode(Encoding::UTF_8).match?(VISIBLE)
|
|
55
|
+
rescue EncodingError
|
|
56
|
+
false
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
# Binary bytes that are UTF-8, as File.binread returns them, read as the text they hold; anything else as given.
|
|
60
|
+
def self.text(value)
|
|
61
|
+
return value unless value.is_a?(String) && value.encoding == Encoding::BINARY
|
|
62
|
+
|
|
63
|
+
utf8 = value.dup.force_encoding(Encoding::UTF_8)
|
|
64
|
+
utf8.valid_encoding? ? utf8 : value
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
def tagged?
|
|
68
|
+
!@tagging.nil?
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
def pdf_ua?
|
|
72
|
+
@pdf_ua == true
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
# An element of the structure tree under the current one, which the block's elements and content go into.
|
|
76
|
+
# Nothing is made untagged, or within an artifact.
|
|
77
|
+
def tag_structure(type, **attributes, &)
|
|
78
|
+
return yield unless tagging_structure? && !@tagging.last.whole?
|
|
79
|
+
|
|
80
|
+
attributes.compact!
|
|
81
|
+
check_text_attributes(type, attributes)
|
|
82
|
+
check_list_nesting(type)
|
|
83
|
+
check_heading_level(type) if pdf_ua?
|
|
84
|
+
|
|
85
|
+
within_element(Tagging::Element.new(type, @tagging.last, attributes), &)
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
# A cell of the table element being drawn, in the row element of its row index: a header cell for a column,
|
|
89
|
+
# or a data cell. In a figure, the cell is part of it; outside a table element, nothing is made.
|
|
90
|
+
def tag_table_cell(row, header:, colspan:, rowspan:, &)
|
|
91
|
+
return yield unless tagging_structure?
|
|
92
|
+
return tag_content(&) if @tagging.last.whole?
|
|
93
|
+
return yield unless (table = @tagging.last).type == :Table
|
|
94
|
+
|
|
95
|
+
attributes = {}
|
|
96
|
+
attributes[:Scope] = :Column if header
|
|
97
|
+
attributes[:ColSpan] = colspan if colspan > 1
|
|
98
|
+
attributes[:RowSpan] = rowspan if rowspan > 1
|
|
99
|
+
cell = Tagging::Element.new(header ? :TH : :TD, table.row(row), attributes.empty? ? {} : {A: {O: :Table, **attributes}})
|
|
100
|
+
within_element(cell) { within_mark(Tagging::Mark.new(cell), &) }
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
# What the block draws is the content of the current element.
|
|
104
|
+
def tag_content(&)
|
|
105
|
+
return yield unless tagging_structure?
|
|
106
|
+
|
|
107
|
+
check_list_nesting(nil)
|
|
108
|
+
within_mark(Tagging::Mark.new(@tagging.last), &)
|
|
109
|
+
end
|
|
110
|
+
|
|
111
|
+
# What the block draws only decorates the page, as a header or a footer of type :Pagination and subtype
|
|
112
|
+
# :Header or :Footer: readers skip it. Elements and content within it are neither made nor marked.
|
|
113
|
+
def tag_artifact(type = nil, subtype = nil, &)
|
|
114
|
+
return yield unless tagged?
|
|
115
|
+
|
|
116
|
+
properties = type && "<</Type /#{type}#{" /Subtype /#{subtype}" if subtype}>>"
|
|
117
|
+
within_mark(Tagging::Mark.new(nil, properties), &)
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
# What only decorates a part of the content, a table cell's background or border: an artifact, unless it is
|
|
121
|
+
# drawn within tagged content, a table nested in a cell, where an artifact cannot be (ISO 14289-1 §7.1).
|
|
122
|
+
def tag_decoration(&)
|
|
123
|
+
return yield if tagged? && @marks.any?(&:element)
|
|
124
|
+
|
|
125
|
+
tag_artifact(&)
|
|
126
|
+
end
|
|
127
|
+
|
|
128
|
+
private
|
|
129
|
+
|
|
130
|
+
def initialize_tagging(options)
|
|
131
|
+
pdf = options.delete(:pdf)
|
|
132
|
+
raise ArgumentError, "pdf is #{pdf.inspect}: :ua is the only conformance level, PDF/UA-1" unless [nil, false, :ua].include?(pdf)
|
|
133
|
+
|
|
134
|
+
@pdf_ua = pdf == :ua
|
|
135
|
+
return unless @pdf_ua || @pdf_a_conformance == "A"
|
|
136
|
+
|
|
137
|
+
check_tagging_options(options)
|
|
138
|
+
# The open elements, the document's own first, then the marks open on the page, the innermost last.
|
|
139
|
+
@tagging = [Tagging::Element.new(DOCUMENT_TYPE, nil)]
|
|
140
|
+
@marks = []
|
|
141
|
+
# By page, the element of each marked-content sequence, at its MCID.
|
|
142
|
+
@elements_by_page = {}.compare_by_identity
|
|
143
|
+
end
|
|
144
|
+
|
|
145
|
+
def check_tagging_options(options)
|
|
146
|
+
lang = options[:lang]
|
|
147
|
+
if lang && !(lang.is_a?(String) && lang.match?(Tagging::LANGUAGE_TAG))
|
|
148
|
+
raise ArgumentError, "lang is #{lang.inspect}: give a language tag, \"fr\" or \"en-GB\""
|
|
149
|
+
end
|
|
150
|
+
return unless @pdf_ua
|
|
151
|
+
|
|
152
|
+
unless lang
|
|
153
|
+
raise Prawn::Errors::PdfUaViolation, 'PDF/UA documents say their language, for screen readers to speak it: lang: "en"'
|
|
154
|
+
end
|
|
155
|
+
title = options.dig(:info, :Title)
|
|
156
|
+
return if Tagging.visible_text?(title)
|
|
157
|
+
|
|
158
|
+
raise Prawn::Errors::PdfUaViolation, "PDF/UA documents have a title, shown in the window bar: info: {Title: \"…\"}, not #{title.inspect}"
|
|
159
|
+
end
|
|
160
|
+
|
|
161
|
+
def tagging_structure?
|
|
162
|
+
tagged? && @marks.none? { _1.element.nil? }
|
|
163
|
+
end
|
|
164
|
+
|
|
165
|
+
# Boxes check their alt and lang when made (Gamba::Layout::Node): here a figure must have an alt.
|
|
166
|
+
def check_text_attributes(type, attributes)
|
|
167
|
+
alt = attributes[:Alt]
|
|
168
|
+
if type == :Figure && !Tagging.visible_text?(alt)
|
|
169
|
+
raise tagging_violation, "#{tagging_standard} gives every figure an alternative text: alt: \"…\", not #{alt.inspect}"
|
|
170
|
+
end
|
|
171
|
+
end
|
|
172
|
+
|
|
173
|
+
# A list holds list items only, and a list item sits in a list. type is nil for content marked in the current
|
|
174
|
+
# element.
|
|
175
|
+
def check_list_nesting(type)
|
|
176
|
+
parent = @tagging.last.type
|
|
177
|
+
return unless (type == :LI) ? parent != :L : (parent == :L && type != :LI)
|
|
178
|
+
|
|
179
|
+
raise tagging_violation, (type == :LI) ? "#{tagging_standard} puts a list item in a list: role: :list on its container" :
|
|
180
|
+
"#{tagging_standard} puts only list items in a list: role: :list_item on each of its boxes"
|
|
181
|
+
end
|
|
182
|
+
|
|
183
|
+
# PDF/UA reads headings as an outline: the first is an h1, and each goes at most one level below the last.
|
|
184
|
+
def check_heading_level(type)
|
|
185
|
+
level = HEADING_LEVELS[type]
|
|
186
|
+
return unless level
|
|
187
|
+
|
|
188
|
+
last = @heading_level || 0
|
|
189
|
+
if level > last + 1
|
|
190
|
+
raise Prawn::Errors::PdfUaViolation, "PDF/UA does not skip heading levels: an h#{level} " \
|
|
191
|
+
"#{last.zero? ? "comes first, before any h1" : "follows an h#{last}"}"
|
|
192
|
+
end
|
|
193
|
+
@heading_level = level
|
|
194
|
+
end
|
|
195
|
+
|
|
196
|
+
def tagging_standard
|
|
197
|
+
pdf_ua? ? "PDF/UA" : "PDF/A-3a"
|
|
198
|
+
end
|
|
199
|
+
|
|
200
|
+
def tagging_violation
|
|
201
|
+
pdf_ua? ? Prawn::Errors::PdfUaViolation : Prawn::Errors::PdfAViolation
|
|
202
|
+
end
|
|
203
|
+
|
|
204
|
+
def within_element(element)
|
|
205
|
+
@tagging << element
|
|
206
|
+
yield
|
|
207
|
+
ensure
|
|
208
|
+
@tagging.pop
|
|
209
|
+
end
|
|
210
|
+
|
|
211
|
+
# The sequence is written when something is drawn in it: a box that moves to the next page before drawing
|
|
212
|
+
# leaves no empty sequence behind, and its decoration sees nothing drawn on the page it leaves.
|
|
213
|
+
def within_mark(mark)
|
|
214
|
+
@marks << mark
|
|
215
|
+
begin
|
|
216
|
+
yield
|
|
217
|
+
ensure
|
|
218
|
+
@marks.pop
|
|
219
|
+
add_content("EMC") if mark.page
|
|
220
|
+
end
|
|
221
|
+
end
|
|
222
|
+
|
|
223
|
+
def write_mark(mark)
|
|
224
|
+
mark.page = page
|
|
225
|
+
if mark.element
|
|
226
|
+
elements = (@elements_by_page[page] ||= [])
|
|
227
|
+
mark.element.kids << [page, elements.size]
|
|
228
|
+
add_content("/#{mark.element.type} <</MCID #{elements.size}>> BDC")
|
|
229
|
+
elements << mark.element
|
|
230
|
+
else
|
|
231
|
+
add_content(mark.properties ? "/Artifact #{mark.properties} BDC" : "/Artifact BMC")
|
|
232
|
+
end
|
|
233
|
+
end
|
|
234
|
+
|
|
235
|
+
# Called before the content goes to the page: what paints opens the sequences not written on this page yet, or
|
|
236
|
+
# raises when it is in none. Within an artifact, only the outermost one is: what it holds is part of it, and
|
|
237
|
+
# the elements around it wait for content of their own.
|
|
238
|
+
def check_tagged_content(string)
|
|
239
|
+
innermost = @marks.last
|
|
240
|
+
return if innermost&.page || !string.match?(Tagging::BEGINS_GRAPHICS_OBJECT)
|
|
241
|
+
|
|
242
|
+
unless innermost
|
|
243
|
+
raise tagging_violation, "#{tagging_standard} tags all content, and this is drawn outside any tagged box or artifact: " \
|
|
244
|
+
"draw it with Gamba's boxes, or leave it out (#{string.strip.lines.last.strip.inspect})"
|
|
245
|
+
end
|
|
246
|
+
|
|
247
|
+
artifact = @marks.index { _1.element.nil? }
|
|
248
|
+
if artifact
|
|
249
|
+
# Artifacts within one, a decoration moved beneath what it holds, are written too: they leave it.
|
|
250
|
+
@marks[artifact..].each { |mark| write_mark(mark) unless mark.element || mark.page }
|
|
251
|
+
else
|
|
252
|
+
@marks.each { |mark| write_mark(mark) unless mark.page }
|
|
253
|
+
end
|
|
254
|
+
end
|
|
255
|
+
|
|
256
|
+
# Before leaving a page, the sequences open on it are closed there.
|
|
257
|
+
def close_marks_on_page
|
|
258
|
+
@marks&.reverse_each do |mark|
|
|
259
|
+
next unless mark.page
|
|
260
|
+
|
|
261
|
+
add_content("EMC")
|
|
262
|
+
mark.page = nil
|
|
263
|
+
end
|
|
264
|
+
end
|
|
265
|
+
|
|
266
|
+
# The structure tree, the parent tree that maps each page's marked content back to its elements, and the
|
|
267
|
+
# catalog entries a tagged PDF and PDF/UA ask for.
|
|
268
|
+
def finalize_tagging
|
|
269
|
+
raise ArgumentError, "a marked-content sequence is still open" unless @marks.empty?
|
|
270
|
+
|
|
271
|
+
min_version(1.7)
|
|
272
|
+
references = {}.compare_by_identity
|
|
273
|
+
tree = ref!(Type: :StructTreeRoot)
|
|
274
|
+
tree.data[:K] = [structure_reference(@tagging.first, tree, references)]
|
|
275
|
+
nums = []
|
|
276
|
+
pages.each do |page|
|
|
277
|
+
elements = @elements_by_page[page]
|
|
278
|
+
next unless elements
|
|
279
|
+
|
|
280
|
+
page.dictionary.data[:StructParents] = nums.size / 2
|
|
281
|
+
nums << (nums.size / 2) << elements.map { references.fetch(_1) }
|
|
282
|
+
end
|
|
283
|
+
tree.data[:ParentTree] = ref!(Nums: nums)
|
|
284
|
+
tree.data[:ParentTreeNextKey] = nums.size / 2
|
|
285
|
+
@root.data[:StructTreeRoot] = tree
|
|
286
|
+
@root.data[:MarkInfo] = {Marked: true}
|
|
287
|
+
@root.data[:ViewerPreferences] = {**(@root.data[:ViewerPreferences] || {}), DisplayDocTitle: true}
|
|
288
|
+
end
|
|
289
|
+
|
|
290
|
+
def structure_reference(element, parent, references)
|
|
291
|
+
reference = ref!(Type: :StructElem, S: element.type, P: parent, **element.attributes)
|
|
292
|
+
references[element] = reference
|
|
293
|
+
reference.data[:K] = element.kids.map do |kid|
|
|
294
|
+
next structure_reference(kid, reference, references) if kid.is_a?(Tagging::Element)
|
|
295
|
+
|
|
296
|
+
page, mcid = kid
|
|
297
|
+
{Type: :MCR, Pg: page.dictionary, MCID: mcid}
|
|
298
|
+
end
|
|
299
|
+
reference
|
|
300
|
+
end
|
|
301
|
+
end
|
|
302
|
+
end
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "pdf_check"
|
|
4
|
+
|
|
5
|
+
module Gamba
|
|
6
|
+
# What an application's tests ask of its PDFs, in words of the test frameworks: Gamba::Minitest and Gamba::RSpec
|
|
7
|
+
# put it in their own forms. A check whose tool is missing fails with the install command, or is skipped when
|
|
8
|
+
# Gamba::Testing.missing_tools is :skip; it never passes unchecked.
|
|
9
|
+
module Testing
|
|
10
|
+
MISSING_TOOLS = %i[fail skip].freeze
|
|
11
|
+
# Text found this many points from where it was expected is where it was expected.
|
|
12
|
+
POSITION_TOLERANCE = 1
|
|
13
|
+
|
|
14
|
+
class << self
|
|
15
|
+
attr_reader :missing_tools
|
|
16
|
+
|
|
17
|
+
def missing_tools=(choice)
|
|
18
|
+
raise ArgumentError, "missing_tools is #{choice.inspect}: give :fail or :skip" unless MISSING_TOOLS.include?(choice)
|
|
19
|
+
|
|
20
|
+
@missing_tools = choice
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
# nil when the text is on the page (any page unless told), else what was there instead. text: a String, whose
|
|
24
|
+
# spaces match any run of them, or a Regexp; at: [x, y] of its first character's baseline, in points from the page's
|
|
25
|
+
# top left, as MuPDF reads it.
|
|
26
|
+
def text_failure(pdf, text, page: nil, at: nil)
|
|
27
|
+
pages = PdfCheck.text_layout(pdf)
|
|
28
|
+
candidates = page ? [pages[page - 1]].compact : pages
|
|
29
|
+
return "the PDF has #{pages.size} pages, not #{page}" if candidates.empty?
|
|
30
|
+
|
|
31
|
+
pattern = text.is_a?(Regexp) ? text : Regexp.new(text.split.map { Regexp.escape(_1) }.join("\\s+"))
|
|
32
|
+
found = candidates.any? { |candidate| lines(candidate).any? { |glyphs| occurs?(glyphs, pattern, at) } }
|
|
33
|
+
return nil if found
|
|
34
|
+
|
|
35
|
+
place = page ? "page #{page}" : "the PDF"
|
|
36
|
+
"#{text.inspect} is not on #{place}#{" at #{at.inspect}" if at}: it reads\n#{candidates.map { |candidate| lines(candidate).map { _1.map(&:first).join }.join("\n") }.join("\n---\n")}"
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def pages_failure(expected, actual)
|
|
40
|
+
differences = PdfCheck.page_differences(expected, actual)
|
|
41
|
+
differences.empty? ? nil : "the pages differ:\n#{differences.join("\n")}"
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def validity_failure(pdf)
|
|
45
|
+
problem = PdfCheck.invalidity(pdf)
|
|
46
|
+
problem && "the PDF is not valid: #{problem}"
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def pdf_a_failure(pdf, flavour: "3b")
|
|
50
|
+
report = PdfCheck.pdf_a_failure(pdf, flavour:)
|
|
51
|
+
report && "the PDF is not PDF/A-#{flavour}:\n#{report}"
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
private
|
|
55
|
+
|
|
56
|
+
def lines(page)
|
|
57
|
+
page.glyphs.group_by(&:last).sort.map!(&:last)
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
# Whether the pattern matches the line, from a glyph near `at` when told.
|
|
61
|
+
def occurs?(glyphs, pattern, at)
|
|
62
|
+
line = glyphs.map(&:first).join
|
|
63
|
+
position = 0
|
|
64
|
+
while (match = pattern.match(line, position))
|
|
65
|
+
return true if at.nil? || near?(glyphs[match.begin(0)], at)
|
|
66
|
+
|
|
67
|
+
position = match.begin(0) + 1
|
|
68
|
+
end
|
|
69
|
+
false
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
def near?(glyph, (x, y))
|
|
73
|
+
glyph && (glyph[3] - x).abs <= POSITION_TOLERANCE && (glyph[4] - y).abs <= POSITION_TOLERANCE
|
|
74
|
+
end
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
self.missing_tools = :fail
|
|
78
|
+
end
|
|
79
|
+
end
|
|
@@ -93,6 +93,11 @@ module Prawn
|
|
|
93
93
|
@overflow != :shrink_to_fit && self.class.extensions.empty? && @original_array.none? { _1[:color] }
|
|
94
94
|
end
|
|
95
95
|
|
|
96
|
+
# How far below the box's top each line of the last kept layout ends, its descender included.
|
|
97
|
+
def line_bottoms
|
|
98
|
+
@laid_out_lines.map { |baseline_y, _drawn_fragments, descender| descender - baseline_y }
|
|
99
|
+
end
|
|
100
|
+
|
|
96
101
|
# Draws, with its top left corner at `at`, the lines the last kept layout wrapped, without wrapping them again.
|
|
97
102
|
def render_laid_out(at)
|
|
98
103
|
@at = at.dup
|
|
@@ -55,7 +55,7 @@ module Prawn
|
|
|
55
55
|
drawn_fragments << drawn_fragment if @laid_out_lines
|
|
56
56
|
accumulated_width += fragment.width
|
|
57
57
|
end
|
|
58
|
-
@laid_out_lines << [@baseline_y, drawn_fragments] if @laid_out_lines
|
|
58
|
+
@laid_out_lines << [@baseline_y, drawn_fragments, @descender] if @laid_out_lines
|
|
59
59
|
@printed_lines << printed_fragments.map { _1.dup.force_encoding(::Encoding::UTF_8) }.join
|
|
60
60
|
end
|
|
61
61
|
|
|
@@ -17,33 +17,62 @@ module Gamba
|
|
|
17
17
|
@features = features.map(&:to_s)
|
|
18
18
|
@ligatures_by_first_glyph = read_ligatures
|
|
19
19
|
@pair_lookups = read_pair_lookups
|
|
20
|
+
@kerning_by_pair = {}
|
|
20
21
|
end
|
|
21
22
|
|
|
22
|
-
# Glyphs and the characters each stands for, ligatures applied: [[glyph, [code, …]], …]
|
|
23
|
+
# Glyphs and the characters each stands for, ligatures applied: [[glyph, code or [code, …]], …], a glyph
|
|
24
|
+
# standing for one character carrying its code alone, as unshaped text does.
|
|
23
25
|
def shape(codes)
|
|
24
|
-
clusters = codes.map { |code| [@file.glyph_by_code[code], [code]] }
|
|
25
|
-
return clusters if @ligatures_by_first_glyph.empty?
|
|
26
|
-
|
|
27
26
|
shaped = []
|
|
27
|
+
each_glyph(codes) { |glyph, index, length| shaped << [glyph, self.class.cluster_codes(codes, index, length)] }
|
|
28
|
+
shaped
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def self.cluster_codes(codes, index, length)
|
|
32
|
+
(length == 1) ? codes[index] : codes[index, length]
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
# Each glyph, ligatures applied, with the index of its first character and how many it stands for. Text is
|
|
36
|
+
# measured word by word: this builds nothing for the glyphs of a word.
|
|
37
|
+
def each_glyph(codes)
|
|
38
|
+
glyph_by_code = @file.glyph_by_code
|
|
28
39
|
index = 0
|
|
29
|
-
while index <
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
end
|
|
40
|
+
while index < codes.size
|
|
41
|
+
glyph = glyph_by_code[codes[index]]
|
|
42
|
+
ligature = ligature_at(codes, index, glyph, glyph_by_code)
|
|
33
43
|
if ligature
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
index += components.size + 1
|
|
44
|
+
length = ligature[0].size + 1
|
|
45
|
+
yield ligature[1], index, length
|
|
37
46
|
else
|
|
38
|
-
|
|
39
|
-
index
|
|
47
|
+
length = 1
|
|
48
|
+
yield glyph, index, length
|
|
40
49
|
end
|
|
50
|
+
index += length
|
|
41
51
|
end
|
|
42
|
-
shaped
|
|
43
52
|
end
|
|
44
53
|
|
|
45
|
-
# The horizontal advance adjustment between two glyphs, in font units, or nil.
|
|
54
|
+
# The horizontal advance adjustment between two glyphs, in font units, or nil. Each pair is read from the
|
|
55
|
+
# font once: a document kerns the same pairs over and over.
|
|
46
56
|
def kerning(left, right)
|
|
57
|
+
pair = (left << 16) | right
|
|
58
|
+
adjustment = @kerning_by_pair.fetch(pair) { @kerning_by_pair[pair] = pair_adjustment(left, right) || false }
|
|
59
|
+
adjustment || nil
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
private
|
|
63
|
+
|
|
64
|
+
# The longest ligature starting with this glyph whose other components follow it: [components, glyph].
|
|
65
|
+
def ligature_at(codes, index, glyph, glyph_by_code)
|
|
66
|
+
@ligatures_by_first_glyph[glyph]&.find do |components, _ligature_glyph|
|
|
67
|
+
next false if index + components.size >= codes.size
|
|
68
|
+
|
|
69
|
+
matched = 0
|
|
70
|
+
matched += 1 while matched < components.size && glyph_by_code[codes[index + 1 + matched]] == components[matched]
|
|
71
|
+
matched == components.size
|
|
72
|
+
end
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
def pair_adjustment(left, right)
|
|
47
76
|
@pair_lookups.each do |lookup|
|
|
48
77
|
adjustment = lookup.call(left, right)
|
|
49
78
|
return adjustment if adjustment
|
|
@@ -51,8 +80,6 @@ module Gamba
|
|
|
51
80
|
nil
|
|
52
81
|
end
|
|
53
82
|
|
|
54
|
-
private
|
|
55
|
-
|
|
56
83
|
def read_ligatures
|
|
57
84
|
table = @file.table("GSUB")
|
|
58
85
|
return {} unless table && @features.include?("liga")
|
|
@@ -14,6 +14,7 @@ module Gamba
|
|
|
14
14
|
end
|
|
15
15
|
|
|
16
16
|
SPACE = 32
|
|
17
|
+
SPACE_CLUSTER = [SPACE].freeze
|
|
17
18
|
FIRST_FREE_CODE = 33
|
|
18
19
|
|
|
19
20
|
def initialize(glyph_for_cluster)
|
|
@@ -33,16 +34,19 @@ module Gamba
|
|
|
33
34
|
# [[subset index, bytes], …] for clusters of characters, each cluster [glyph, codes].
|
|
34
35
|
def encode(clusters)
|
|
35
36
|
parts = []
|
|
36
|
-
clusters.each
|
|
37
|
-
if !parts.empty? && (codes == [SPACE] || codes == SPACE) && glyph == @subsets[0].glyph_by_code[SPACE]
|
|
38
|
-
parts.last[1] << SPACE
|
|
39
|
-
else
|
|
40
|
-
append(parts, *(@location_by_cluster[codes] ||= code_for([glyph, codes])))
|
|
41
|
-
end
|
|
42
|
-
end
|
|
37
|
+
clusters.each { |glyph, codes| append_cluster(parts, glyph, codes) }
|
|
43
38
|
parts
|
|
44
39
|
end
|
|
45
40
|
|
|
41
|
+
# Adds one cluster's code to parts: [[subset index, bytes], …].
|
|
42
|
+
def append_cluster(parts, glyph, codes)
|
|
43
|
+
if !parts.empty? && (codes == SPACE || codes == SPACE_CLUSTER) && glyph == @subsets[0].glyph_by_code[SPACE]
|
|
44
|
+
parts.last[1] << SPACE
|
|
45
|
+
else
|
|
46
|
+
append(parts, *(@location_by_cluster[codes] ||= code_for([glyph, codes])))
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
|
|
46
50
|
# The same for single characters, by code point: the common case, where a character already given its code
|
|
47
51
|
# costs a single hash access, and its glyph is only looked up the first time.
|
|
48
52
|
def encode_codepoints(codepoints, glyph_by_code)
|
data/lib/gamba/version.rb
CHANGED
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Gamba
|
|
4
|
+
# A text across every page, beneath its content: watermark: "Draft", or a hash with the text and any of font, style,
|
|
5
|
+
# size, angle (degrees), color and opacity. Each page calls its size's stamp as it opens, so the watermark lies
|
|
6
|
+
# above a background and beneath what the page draws next; the stamp is drawn when the document renders, once its
|
|
7
|
+
# fonts are registered.
|
|
8
|
+
class Watermark
|
|
9
|
+
OPTIONS = %i[text font style size angle color opacity].freeze
|
|
10
|
+
STYLES = %i[normal bold italic bold_italic].freeze
|
|
11
|
+
# Without a size, the rotated text spans at most this share of the page's width and height.
|
|
12
|
+
FILL_RATIO = 0.8
|
|
13
|
+
ARTIFACT = %i[Pagination Watermark].freeze
|
|
14
|
+
# A page can open inside a block that changed the text state, which a form XObject inherits.
|
|
15
|
+
TEXT_STATE_RESET = "0 Tr 0 Tc 0 Tw 100 Tz 0 Ts"
|
|
16
|
+
|
|
17
|
+
def self.from_option(option)
|
|
18
|
+
return unless option
|
|
19
|
+
raise ArgumentError, "watermark is #{option.inspect}: give its text, or a hash with the text and its options" unless option.is_a?(String) || option.is_a?(Hash)
|
|
20
|
+
|
|
21
|
+
option = {text: option} if option.is_a?(String)
|
|
22
|
+
unknown = option.keys - OPTIONS
|
|
23
|
+
raise ArgumentError, "unknown watermark option#{"s" if unknown.size > 1} #{unknown.map(&:inspect).join(", ")}: accepted options are #{OPTIONS.map(&:inspect).join(", ")}" if unknown.any?
|
|
24
|
+
|
|
25
|
+
new(**option.compact)
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def initialize(text:, font: nil, style: nil, size: nil, angle: nil, color: "808080", opacity: 0.2)
|
|
29
|
+
@text = text.to_s
|
|
30
|
+
raise ArgumentError, "the watermark text #{text.inspect} shows nothing" unless Tagging.visible_text?(@text)
|
|
31
|
+
raise ArgumentError, "the watermark text #{text.inspect} holds a line break or a control character: it is one line" if @text.match?(/[[:cntrl:]
]/)
|
|
32
|
+
raise ArgumentError, "the watermark style is #{style.inspect}: give one of #{STYLES.map(&:inspect).join(", ")}" unless style.nil? || STYLES.include?(style)
|
|
33
|
+
raise ArgumentError, "the watermark size is #{size.inspect}: give a positive number of points" if size && !(size.is_a?(Numeric) && size.finite? && size.positive?)
|
|
34
|
+
raise ArgumentError, "the watermark angle is #{angle.inspect}: give a number of degrees" if angle && !(angle.is_a?(Numeric) && angle.finite?)
|
|
35
|
+
raise ArgumentError, "the watermark opacity is #{opacity.inspect}: give a number from 0 to 1" unless opacity.is_a?(Numeric) && opacity.between?(0, 1)
|
|
36
|
+
raise ArgumentError, "the watermark color is #{color.inspect}: give RGB as \"808080\" or CMYK as [0, 0, 0, 50]" unless color?(color)
|
|
37
|
+
|
|
38
|
+
@font = font
|
|
39
|
+
@style = style
|
|
40
|
+
@size = size
|
|
41
|
+
@angle = angle
|
|
42
|
+
@color = color
|
|
43
|
+
@opacity = opacity
|
|
44
|
+
@unfilled_stamps = []
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def open_page(document)
|
|
48
|
+
_left, _bottom, width, height = document.page.dimensions
|
|
49
|
+
name = "Gamba watermark #{width}x#{height}"
|
|
50
|
+
@unfilled_stamps << [document.__send__(:create_stamp_dictionary, name), width, height] unless document.__send__(:stamp_dictionary_registry).key?(name)
|
|
51
|
+
document.tag_artifact(*ARTIFACT) { document.stamp(name) }
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
def fill_stamps(document)
|
|
55
|
+
@unfilled_stamps.each do |dictionary, width, height|
|
|
56
|
+
document.page.stamp_stream(dictionary) { document.tag_artifact(*ARTIFACT) { draw(document, width, height) } }
|
|
57
|
+
end
|
|
58
|
+
@unfilled_stamps.clear
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
private
|
|
62
|
+
|
|
63
|
+
def standard_font(document)
|
|
64
|
+
message = "the watermark needs font: a registered TrueType or OpenType family, as PDF/A and PDF/UA embed every font"
|
|
65
|
+
raise Prawn::Errors::PdfAViolation, message if document.pdf_a?
|
|
66
|
+
raise Prawn::Errors::PdfUaViolation, message if document.pdf_ua?
|
|
67
|
+
|
|
68
|
+
"Helvetica"
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
def color?(color)
|
|
72
|
+
(color.is_a?(String) && color.match?(/\A\h{6}\z/)) || (color.is_a?(Array) && color.size == 4 && color.all?(Numeric))
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
# By default it runs along the page's diagonal, from the bottom left corner to the top right one.
|
|
76
|
+
def draw(document, width, height)
|
|
77
|
+
family = @font || document.default_font_family || standard_font(document)
|
|
78
|
+
style = @style || (document.font_families.fetch(family, {}).key?(:bold) ? :bold : :normal)
|
|
79
|
+
angle = @angle || (Math.atan2(height, width) * 180 / Math::PI)
|
|
80
|
+
center_x = width / 2.0
|
|
81
|
+
center_y = height / 2.0
|
|
82
|
+
document.add_content(TEXT_STATE_RESET)
|
|
83
|
+
document.canvas do
|
|
84
|
+
document.font(family, style:) do
|
|
85
|
+
document.font_size(@size || fitted_size(document, angle, width, height)) do
|
|
86
|
+
kerning = document.default_kerning?
|
|
87
|
+
font = document.font
|
|
88
|
+
baseline = [center_x - (document.width_of(@text, kerning:) / 2), center_y - ((font.ascender - font.descender) / 2)]
|
|
89
|
+
document.fill_color(@color)
|
|
90
|
+
document.transparent(@opacity) { document.rotate(angle, origin: [center_x, center_y]) { document.draw_text(@text, at: baseline, kerning:) } }
|
|
91
|
+
end
|
|
92
|
+
end
|
|
93
|
+
end
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
# The rotated box of the text, its width by the font's height, fits the share of the page FILL_RATIO leaves.
|
|
97
|
+
def fitted_size(document, angle, width, height)
|
|
98
|
+
radians = angle * Math::PI / 180
|
|
99
|
+
cosine = Math.cos(radians).abs
|
|
100
|
+
sine = Math.sin(radians).abs
|
|
101
|
+
text_width = document.width_of(@text, size: 1, kerning: document.default_kerning?)
|
|
102
|
+
raise ArgumentError, "the watermark text #{@text.inspect} has no width in #{document.font.name}" unless text_width.positive?
|
|
103
|
+
|
|
104
|
+
text_height = document.font.height_at(1)
|
|
105
|
+
[FILL_RATIO * width / ((text_width * cosine) + (text_height * sine)), FILL_RATIO * height / ((text_width * sine) + (text_height * cosine))].min
|
|
106
|
+
end
|
|
107
|
+
end
|
|
108
|
+
end
|