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.
@@ -0,0 +1,189 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Gamba
4
+ module Layout
5
+ # A table of named columns as a box: as wide as its box, broken across pages with its header when a stack
6
+ # flows it. Prawn's table lays it out and draws it.
7
+ class Table < Node
8
+ OPTIONS = (Node::OPTIONS + %i[columns header stripes cell]).freeze
9
+ COLUMN_OPTIONS = %i[title width align weight italic size color].freeze
10
+ HEADER_OPTIONS = %i[align weight italic size color background].freeze
11
+ CELL_OPTIONS = %i[padding size font color background borders border_width border_color].freeze
12
+ DEFAULT_HEADER = {weight: :bold}.freeze
13
+ ROLES = %i[table artifact].freeze
14
+ DEFAULT_ROLE = :table
15
+ KIND = "a table"
16
+ # A native name on the left, the name of Prawn's cell on the right.
17
+ PRAWN_CELL_NAMES = {color: :text_color, background: :background_color}.freeze
18
+ # Prawn starts a new page unless a row is strictly shorter than what is left of the page: a table that
19
+ # fills the page to the point would be cut. Measured a hair higher, it fits with room to spare or moves.
20
+ PAGE_BREAK_SLACK_POINTS = 0.001
21
+
22
+ def initialize(rows, styling, styles, **options)
23
+ Styles.check_options(options, OPTIONS)
24
+ super(**options.slice(*Node::OPTIONS))
25
+ @rows = rows
26
+ @styling = styling
27
+ @styles = styles
28
+ @columns = checked_columns(options[:columns] || inferred_columns)
29
+ @header = checked_header(options.fetch(:header, true))
30
+ @stripes = options[:stripes]
31
+ @cell = options.fetch(:cell, {})
32
+ Styles.check_options(@cell, CELL_OPTIONS)
33
+ end
34
+
35
+ def paint(prawn, left, top, width)
36
+ return if data.empty?
37
+
38
+ table = table(prawn, width)
39
+ prawn.y = top
40
+ within(prawn, left, width) { table.draw }
41
+ end
42
+
43
+ # Prawn draws a row higher than a page past its bottom: here it raises, as any box that cannot be split does.
44
+ def flow(prawn, cursor, left, width)
45
+ check_rows_fit(prawn, cursor, width) unless data.empty?
46
+ paint(prawn, left, cursor.y, width)
47
+ cursor.y = prawn.y unless data.empty?
48
+ end
49
+
50
+ # A page can start with the header and the first row.
51
+ def first_height(prawn, width)
52
+ return 0 if data.empty?
53
+
54
+ table(prawn, width).rows(0..[@header ? 1 : 0, data.size - 1].min).height + PAGE_BREAK_SLACK_POINTS
55
+ end
56
+
57
+ private
58
+
59
+ # The table element holds the elements its cells make as they are drawn (Prawn::Table::Cell#tag_content).
60
+ def tag_body(_prawn)
61
+ yield
62
+ end
63
+
64
+ def measure(prawn, width)
65
+ data.empty? ? 0 : table(prawn, width).height + PAGE_BREAK_SLACK_POINTS
66
+ end
67
+
68
+ def description
69
+ "a table"
70
+ end
71
+
72
+ def check_rows_fit(prawn, cursor, width)
73
+ heights = table(prawn, width).row_heights
74
+ header = @header ? heights.first : 0
75
+ tallest = (@header ? heights.drop(1) : heights).max || 0
76
+ return if header + tallest <= cursor.page_height + FIT_TOLERANCE_POINTS
77
+
78
+ raise Prawn::Errors::CannotFit, "#{description} has a row #{tallest.round(1)} pt high#{" under a #{header.round(1)} pt header" if @header}, " \
79
+ "the page has #{cursor.page_height.to_f.round(1)} pt"
80
+ end
81
+
82
+ # Without columns, a table of hashes has those of its first row.
83
+ def inferred_columns
84
+ first = @rows.first
85
+ unless first.is_a?(Hash)
86
+ raise ArgumentError, 'columns are missing: name them, columns: {total: {title: "Total", align: :right}}, or give rows as hashes'
87
+ end
88
+
89
+ first.keys.to_h { |name| [name, {}] }
90
+ end
91
+
92
+ def checked_columns(columns)
93
+ columns.each_value { |options| Styles.check_options(options, COLUMN_OPTIONS) }
94
+ raise ArgumentError, "a table needs at least one column" if columns.empty?
95
+
96
+ columns
97
+ end
98
+
99
+ def checked_header(header)
100
+ return header ? DEFAULT_HEADER : nil unless header.is_a?(Hash)
101
+
102
+ Styles.check_options(header, HEADER_OPTIONS)
103
+ {**DEFAULT_HEADER, **header}
104
+ end
105
+
106
+ def data
107
+ @data ||= begin
108
+ body = @rows.map { |row| @columns.each_key.map { |name| value(row, name) } }
109
+ @header ? [@columns.map { |name, options| options.fetch(:title) { name.to_s.tr("_", " ").capitalize } }, *body] : body
110
+ end
111
+ end
112
+
113
+ def value(row, name)
114
+ case row
115
+ when Hash then row[name]
116
+ when Array then raise ArgumentError, "a row is an array: rows are hashes, or objects answering the columns' names (Prawn's table of arrays is document.prawn.table)"
117
+ else row.public_send(name)
118
+ end
119
+ end
120
+
121
+ # Columns given all their widths keep them; otherwise the table takes the width of its box.
122
+ def table(prawn, width)
123
+ return @table if @table_width == width
124
+
125
+ options = prawn_options(width)
126
+ @table = within(prawn, prawn.bounds.absolute_left, width) do
127
+ prawn.make_table(data, options) do |table|
128
+ style_columns(table)
129
+ style(table.row(0), @header) if @header
130
+ @styling&.call(table)
131
+ check_faces(table)
132
+ end
133
+ end
134
+ @table_width = width
135
+ @table
136
+ end
137
+
138
+ # Once the styling block has had its say: a face a cell asks for and its font lacks says so.
139
+ def check_faces(table)
140
+ hint = header_hint
141
+ table.cells.each do |cell|
142
+ next unless cell.is_a?(Prawn::Table::Cell::Text) && (font = cell.font_name) && (font_style = cell.font_style)
143
+
144
+ @styles.check_face(font, font_style, (hint.last if hint && cell.row.zero? && font_style == hint.first), in_table: true)
145
+ end
146
+ end
147
+
148
+ # The header's own face, and how to drop it: a cell of the first row in that face may take a regular header.
149
+ def header_hint
150
+ return unless @header && (style = Styles.font_style(@header[:weight], @header[:italic])) != :normal
151
+
152
+ drops = [("weight: :normal" if @header[:weight] == :bold), ("italic: false" if @header[:italic])].compact
153
+ [style, "; or header: {#{drops.join(", ")}}"]
154
+ end
155
+
156
+ # Prawn's table measures a cell's text in the document's font and draws it in the cell's: each native cell is
157
+ # measured in its own (Prawn::Table::Cell#sized_in_its_font=), so it fits what it draws.
158
+ def prawn_options(width)
159
+ widths = @columns.each_value.map { |options| options[:width] }
160
+ cell_style = {sized_in_its_font: true, **prawn_cell_style({**@styles.cell_defaults, **@cell})}
161
+ options = {header: !@header.nil?, cell_style:}
162
+ options[:row_colors] = @stripes if @stripes
163
+ options[:column_widths] = widths.each_with_index.to_h { |column_width, index| [index, column_width] }.compact
164
+ options[:width] = width unless widths.all?
165
+ options
166
+ end
167
+
168
+ def style_columns(table)
169
+ @columns.each_value.with_index { |options, index| style(table.column(index), options.except(:title, :width)) }
170
+ end
171
+
172
+ def style(cells, options)
173
+ style = prawn_cell_style(options.except(:weight, :italic))
174
+ style[:font_style] = Styles.font_style(options[:weight], options[:italic]) if options[:weight] || options[:italic]
175
+ cells.style(style)
176
+ end
177
+
178
+ def prawn_cell_style(options)
179
+ options.transform_keys { |name| PRAWN_CELL_NAMES.fetch(name, name) }
180
+ end
181
+
182
+ # Prawn's table reads its left edge and the width it may take from the bounds.
183
+ def within(prawn, left, width, &)
184
+ bounds = prawn.bounds
185
+ prawn.indent(left - bounds.absolute_left, bounds.absolute_right - left - width, &)
186
+ end
187
+ end
188
+ end
189
+ end
@@ -0,0 +1,68 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Gamba
4
+ # The boxes and the turn of a page read from a PDF written elsewhere, through @reader, whatever the file holds:
5
+ # a value that is not a usable box or number reads as missing.
6
+ module PageBoxes
7
+ DEFAULT_MEDIA_BOX = [0.0, 0.0, 612.0, 792.0].freeze
8
+ # What draws a page's content turned as a viewer shows it under /Rotate, in a box from the origin.
9
+ ROTATIONS = {
10
+ 90 => ->(width, height) { [0.0, -1.0, 1.0, 0.0, 0.0, width] },
11
+ 180 => ->(width, height) { [-1.0, 0.0, 0.0, -1.0, width, height] },
12
+ 270 => ->(width, height) { [0.0, 1.0, -1.0, 0.0, height, 0.0] }
13
+ }.freeze
14
+
15
+ private
16
+
17
+ def media_box(page)
18
+ box(page[:MediaBox]) || DEFAULT_MEDIA_BOX
19
+ end
20
+
21
+ # What a viewer shows: the crop box within the media box.
22
+ def visible_box(page)
23
+ media = media_box(page)
24
+ crop = box(page[:CropBox])
25
+ crop ? intersection(crop, media) : media
26
+ end
27
+
28
+ # 0, 90, 180 or 270: a turn that is not a quarter one reads as none.
29
+ def rotation(page)
30
+ rotation = number(page[:Rotate]).to_i % 360
31
+ (rotation % 90).zero? ? rotation : 0
32
+ end
33
+
34
+ def box(value)
35
+ values = @reader.resolve(value)
36
+ return nil unless values.is_a?(Array) && values.size == 4
37
+
38
+ numbers = values.map { number(_1) }
39
+ return nil if numbers.any?(&:nil?) || numbers[0] == numbers[2] || numbers[1] == numbers[3]
40
+
41
+ left, right = [numbers[0], numbers[2]].minmax
42
+ bottom, top = [numbers[1], numbers[3]].minmax
43
+ [left, bottom, right, top]
44
+ end
45
+
46
+ def number(value)
47
+ numeric(@reader.resolve(value))
48
+ end
49
+
50
+ # A real too large for a Float reads as infinite, and would write NaN into the page's matrix: no number then.
51
+ def numeric(value)
52
+ number = (value.is_a?(PDF::Core::Real) || value.is_a?(Numeric)) ? value.to_f : nil
53
+ number if number&.finite?
54
+ end
55
+
56
+ def intersection(crop, media)
57
+ left, bottom = [crop[0], media[0]].max, [crop[1], media[1]].max
58
+ right, top = [crop[2], media[2]].min, [crop[3], media[3]].min
59
+ (right > left && top > bottom) ? [left, bottom, right, top] : media
60
+ end
61
+
62
+ def multiply(first, second)
63
+ a, b, c, d, e, f = first
64
+ a2, b2, c2, d2, e2, f2 = second
65
+ [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]
66
+ end
67
+ end
68
+ end
@@ -129,6 +129,12 @@ module PDF
129
129
  pages
130
130
  end
131
131
 
132
+ # A stream's bytes decoded, Flate being the only filter read, as for object streams: a page's content in
133
+ # several streams is joined this way.
134
+ def decoded(stream)
135
+ decode(stream)
136
+ end
137
+
132
138
  private
133
139
 
134
140
  # Objects read so far were read as stored: forget them, so they are read again and decrypted.
@@ -393,14 +399,14 @@ module PDF
393
399
  end
394
400
  end
395
401
 
396
- # Object and cross-reference streams only: Flate, optionally with a PNG predictor (§7.4.4.4). Every filter's
402
+ # Flate, optionally with a PNG predictor (§7.4.4.4). Every filter's
397
403
  # output counts against what the file may decode in all.
398
404
  def decode(stream)
399
405
  filters = [resolve(stream.dictionary[:Filter])].flatten
400
406
  filters.compact!
401
407
  parameters = [resolve(stream.dictionary[:DecodeParms])].flatten
402
408
  filters.each_with_index.reduce(stream.data) do |data, (filter, position)|
403
- raise Prawn::Errors::MalformedPDF, "Unsupported filter #{filter.inspect} on an object stream" unless filter == FLATE
409
+ raise Prawn::Errors::MalformedPDF, "Unsupported filter #{filter.inspect}: only Flate is read" unless filter == FLATE
404
410
 
405
411
  inflated = inflate(data)
406
412
  @decoded_bytes_left -= inflated.bytesize
data/lib/gamba/pdf_a.rb CHANGED
@@ -31,8 +31,18 @@ module Gamba
31
31
  # XML 1.0 has no way to write these, not even as references: the XMP would not parse.
32
32
  XML_FORBIDDEN_CHARACTERS = /[\u0000-\u0008\u000B\u000C\u000E-\u001F\uFFFE\uFFFF]/
33
33
 
34
+ PDFUA_NAMESPACE = "http://www.aiim.org/pdfua/ns/id/"
35
+ # pdf_a: true is PDF/A-3b, the level that keeps the look; :a adds the structure that PDF/A-3a asks for.
36
+ CONFORMANCE_BY_OPTION = {nil => nil, false => nil, true => "B", :b => "B", :a => "A"}.freeze
37
+
38
+ def self.conformance(option)
39
+ CONFORMANCE_BY_OPTION.fetch(option) do
40
+ raise ArgumentError, "pdf_a is #{option.inspect}: true or :b for PDF/A-3b, :a for PDF/A-3a"
41
+ end
42
+ end
43
+
34
44
  def pdf_a?
35
- @pdf_a == true
45
+ !@pdf_a_conformance.nil?
36
46
  end
37
47
 
38
48
  def factur_x(xml, profile: :en16931, filename: FACTUR_X_FILENAME, description: "Factur-X invoice")
@@ -52,6 +62,10 @@ module Gamba
52
62
  Info: PDF::Core::LiteralString.new(SrgbProfile::DESCRIPTION), DestOutputProfile: icc_profile
53
63
  }]
54
64
  embed_factur_x if @factur_x
65
+ end
66
+
67
+ # The XMP metadata PDF/A and PDF/UA both ask for, written last.
68
+ def finalize_metadata
55
69
  metadata = ref!({Type: :Metadata, Subtype: :XML}, xmp_packet)
56
70
  metadata.uncompressed = true
57
71
  @root.data[:Metadata] = metadata
@@ -87,28 +101,29 @@ module Gamba
87
101
  <?xpacket begin="\u{FEFF}" id="W5M0MpCehiHzreSzNTczkc9d"?>
88
102
  <x:xmpmeta xmlns:x="adobe:ns:meta/">
89
103
  <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
90
- <rdf:Description rdf:about="" xmlns:pdfaid="http://www.aiim.org/pdfa/ns/id/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:pdf="http://ns.adobe.com/pdf/1.3/" xmlns:xmp="http://ns.adobe.com/xap/1.0/">
91
- <pdfaid:part>3</pdfaid:part>
92
- <pdfaid:conformance>B</pdfaid:conformance>
104
+ <rdf:Description rdf:about=""#{' xmlns:pdfaid="http://www.aiim.org/pdfa/ns/id/"' if pdf_a?}#{" xmlns:pdfuaid=\"#{PDFUA_NAMESPACE}\"" if pdf_ua?} xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:pdf="http://ns.adobe.com/pdf/1.3/" xmlns:xmp="http://ns.adobe.com/xap/1.0/">
105
+ #{identification_xmp}
93
106
  #{properties.join("\n")}
94
107
  </rdf:Description>
95
- #{factur_x_xmp}
108
+ #{factur_x_xmp}#{extension_schemas_xmp}
96
109
  </rdf:RDF>
97
110
  </x:xmpmeta>
98
111
  <?xpacket end="w"?>
99
112
  XMP
100
113
  end
101
114
 
102
- # The Factur-X properties, and the PDF/A extension schema declaring them (Factur-X specification § 6.2.1).
115
+ # The PDF/A part and level, and the PDF/UA part.
116
+ def identification_xmp
117
+ lines = []
118
+ lines << "<pdfaid:part>3</pdfaid:part>" << "<pdfaid:conformance>#{@pdf_a_conformance}</pdfaid:conformance>" if pdf_a?
119
+ lines << "<pdfuaid:part>1</pdfuaid:part>" if pdf_ua?
120
+ lines.join("\n")
121
+ end
122
+
123
+ # The Factur-X properties (Factur-X specification § 6.2.1).
103
124
  def factur_x_xmp
104
125
  return "" unless @factur_x
105
126
 
106
- fields = {DocumentFileName: "name of the embedded XML invoice file", DocumentType: "INVOICE",
107
- Version: "The actual version of the Factur-X XML schema", ConformanceLevel: "The conformance level of the embedded Factur-X data"}
108
- described = fields.map do |name, description|
109
- "<rdf:li rdf:parseType=\"Resource\"><pdfaProperty:name>#{name}</pdfaProperty:name><pdfaProperty:valueType>Text</pdfaProperty:valueType>" \
110
- "<pdfaProperty:category>external</pdfaProperty:category><pdfaProperty:description>#{description}</pdfaProperty:description></rdf:li>"
111
- end
112
127
  <<~XMP
113
128
  <rdf:Description rdf:about="" xmlns:fx="#{FACTUR_X_NAMESPACE}">
114
129
  <fx:DocumentType>INVOICE</fx:DocumentType>
@@ -116,24 +131,50 @@ module Gamba
116
131
  <fx:Version>1.0</fx:Version>
117
132
  <fx:ConformanceLevel>#{@factur_x[:level]}</fx:ConformanceLevel>
118
133
  </rdf:Description>
134
+ XMP
135
+ end
136
+
137
+ # PDF/A declares every schema it does not define, in one bag: Factur-X's, and PDF/UA's identification.
138
+ def extension_schemas_xmp
139
+ schemas = []
140
+ if @factur_x
141
+ fields = {DocumentFileName: "name of the embedded XML invoice file", DocumentType: "INVOICE",
142
+ Version: "The actual version of the Factur-X XML schema", ConformanceLevel: "The conformance level of the embedded Factur-X data"}
143
+ schemas << extension_schema("Factur-X PDFA Extension Schema", FACTUR_X_NAMESPACE, "fx", fields.map { |name, description| [name, "Text", "external", description] })
144
+ end
145
+ if pdf_a? && pdf_ua?
146
+ schemas << extension_schema("PDF/UA Universal Accessibility Schema", PDFUA_NAMESPACE, "pdfuaid",
147
+ [["part", "Integer", "internal", "Indicates, which part of ISO 14289 standard is followed"]])
148
+ end
149
+ return "" if schemas.empty?
150
+
151
+ <<~XMP
119
152
  <rdf:Description rdf:about="" xmlns:pdfaExtension="http://www.aiim.org/pdfa/ns/extension/" xmlns:pdfaSchema="http://www.aiim.org/pdfa/ns/schema#" xmlns:pdfaProperty="http://www.aiim.org/pdfa/ns/property#">
120
- <pdfaExtension:schemas><rdf:Bag><rdf:li rdf:parseType="Resource">
121
- <pdfaSchema:schema>Factur-X PDFA Extension Schema</pdfaSchema:schema>
122
- <pdfaSchema:namespaceURI>#{FACTUR_X_NAMESPACE}</pdfaSchema:namespaceURI>
123
- <pdfaSchema:prefix>fx</pdfaSchema:prefix>
124
- <pdfaSchema:property><rdf:Seq>#{described.join}</rdf:Seq></pdfaSchema:property>
125
- </rdf:li></rdf:Bag></pdfaExtension:schemas>
153
+ <pdfaExtension:schemas><rdf:Bag>#{schemas.join}</rdf:Bag></pdfaExtension:schemas>
126
154
  </rdf:Description>
127
155
  XMP
128
156
  end
129
157
 
158
+ def extension_schema(name, namespace, prefix, properties)
159
+ described = properties.map do |property, type, category, description|
160
+ "<rdf:li rdf:parseType=\"Resource\"><pdfaProperty:name>#{property}</pdfaProperty:name><pdfaProperty:valueType>#{type}</pdfaProperty:valueType>" \
161
+ "<pdfaProperty:category>#{category}</pdfaProperty:category><pdfaProperty:description>#{description}</pdfaProperty:description></rdf:li>"
162
+ end
163
+ "<rdf:li rdf:parseType=\"Resource\">\n<pdfaSchema:schema>#{name}</pdfaSchema:schema>\n<pdfaSchema:namespaceURI>#{namespace}</pdfaSchema:namespaceURI>\n" \
164
+ "<pdfaSchema:prefix>#{prefix}</pdfaSchema:prefix>\n<pdfaSchema:property><rdf:Seq>#{described.join}</rdf:Seq></pdfaSchema:property>\n</rdf:li>"
165
+ end
166
+
130
167
  def escape_xml(text)
131
168
  text = text.encode(::Encoding::UTF_8)
132
- raise Prawn::Errors::PdfAViolation, "PDF/A metadata cannot hold control characters: #{text.inspect}" if text.match?(XML_FORBIDDEN_CHARACTERS)
169
+ raise metadata_violation, "#{pdf_ua? ? "PDF/UA" : "PDF/A"} metadata cannot hold control characters: #{text.inspect}" if text.match?(XML_FORBIDDEN_CHARACTERS)
133
170
 
134
171
  text.encode(xml: :text)
135
172
  rescue EncodingError => error
136
- raise Prawn::Errors::PdfAViolation, "PDF/A metadata must be text: #{error.message}"
173
+ raise metadata_violation, "#{pdf_ua? ? "PDF/UA" : "PDF/A"} metadata must be text: #{error.message}"
174
+ end
175
+
176
+ def metadata_violation
177
+ pdf_ua? ? Prawn::Errors::PdfUaViolation : Prawn::Errors::PdfAViolation
137
178
  end
138
179
  end
139
180
  end
@@ -0,0 +1,220 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "open3"
4
+ require "pathname"
5
+ require "tempfile"
6
+ require "tmpdir"
7
+
8
+ module Gamba
9
+ # What tells two PDFs apart, or a PDF from a valid one, through tools that share no code with Gamba: MuPDF for the
10
+ # text and its positions, MuPDF and Poppler for pixels, qpdf and Poppler for validity, veraPDF for PDF/A. Gamba's
11
+ # own oracle compares it with Prawn this way; Gamba::Minitest and Gamba::RSpec put it in an application's tests.
12
+ # A PDF is given as its bytes or as the path of a file.
13
+ module PdfCheck
14
+ # Each glyph: [character, font without its subset tag, size, x, y], positions in points from the top left.
15
+ Page = Data.define(:size, :glyphs)
16
+
17
+ RESOLUTION_DPI = 150
18
+ POSITION_PRECISION_DIGITS = 2
19
+ # Same character, font and size, at most this far apart: positions round to 0.01 on either side of a boundary.
20
+ POSITION_TOLERANCE = 0.0101
21
+ RENDERERS = {"MuPDF" => :rasterize, "Poppler" => :rasterize_with_poppler}.freeze
22
+ QPDF_WARNINGS_EXIT_STATUS = 3
23
+ TOOLS = {
24
+ "mutool" => "brew install mupdf-tools, or apt-get install mupdf-tools",
25
+ "pdftoppm" => "brew install poppler, or apt-get install poppler-utils",
26
+ "pdftotext" => "brew install poppler, or apt-get install poppler-utils",
27
+ "qpdf" => "brew install qpdf, or apt-get install qpdf",
28
+ "verapdf" => "brew install verapdf, or https://verapdf.org/software/"
29
+ }.freeze
30
+
31
+ # Raised when a check needs a tool that is not installed: the message names the install command.
32
+ class ToolMissing < StandardError; end
33
+
34
+ class << self
35
+ # A password encrypted PDFs open with: the tools are given it when they cannot open one without.
36
+ attr_accessor :password
37
+
38
+ # The pages of the PDF, their text read by MuPDF.
39
+ def text_layout(pdf)
40
+ with_path(pdf) { |path| parse_text(mutool("draw", "-F", "stext", "-o", "-", path)) }
41
+ end
42
+
43
+ # The text of each page by line, top to bottom: what a reader sees, words joined.
44
+ def text_lines(pdf)
45
+ text_layout(pdf).map { |page| page.glyphs.group_by(&:last).sort.map! { |_y, glyphs| glyphs.map(&:first).join } }
46
+ end
47
+
48
+ # What makes qpdf or Poppler find the PDF broken, nil when nothing does. qpdf exits 3 for warnings: a PDF readers
49
+ # have to repair is not a valid one. Poppler checks the operands of content stream operators, which qpdf does not.
50
+ def invalidity(pdf)
51
+ with_path(pdf) do |path|
52
+ output, status = run("qpdf", "--check", path)
53
+ output, status = run("qpdf", "--check", "--password=#{password}", path) if password && output.include?("invalid password")
54
+ next output.lines.grep(/WARNING|ERROR|error/).first(3).join.strip.then { |problem| problem.empty? ? "qpdf exit #{status.exitstatus}" : problem } unless status.success?
55
+
56
+ errors, _status = run("pdftotext", *(password ? ["-upw", password] : []), path, File::NULL)
57
+ errors.lines.grep(/Syntax Error/).first(3).join.strip.then { |problem| problem unless problem.empty? }
58
+ end
59
+ end
60
+
61
+ # What veraPDF finds against the PDF/A flavour, nil when it passes.
62
+ def pdf_a_failure(pdf, flavour: "3b")
63
+ with_path(pdf) do |path|
64
+ report, _status = run("verapdf", "--flavour", flavour, "--format", "text", path)
65
+ report.start_with?("PASS") ? nil : report.strip
66
+ end
67
+ end
68
+
69
+ # Every difference between the pages of two PDFs: their number and sizes, each glyph with its font and position,
70
+ # then pixel rows in MuPDF and in Poppler. Empty when a reader could not tell them apart.
71
+ def page_differences(expected, actual)
72
+ with_path(expected) do |expected_path|
73
+ with_path(actual) do |actual_path|
74
+ text_differences(expected_path, actual_path) + pixel_differences(expected_path, actual_path)
75
+ end
76
+ end
77
+ end
78
+
79
+ def text_differences(expected, actual)
80
+ expected_pages = text_layout(expected)
81
+ actual_pages = text_layout(actual)
82
+ return ["#{actual_pages.size} pages instead of #{expected_pages.size}"] if expected_pages.size != actual_pages.size
83
+
84
+ expected_pages.zip(actual_pages).each_with_index.filter_map do |(expected_page, actual_page), index|
85
+ next if expected_page.size == actual_page.size && expected_page.glyphs.size == actual_page.glyphs.size &&
86
+ expected_page.glyphs.zip(actual_page.glyphs).all? { |expected_glyph, actual_glyph| same_glyph?(expected_glyph, actual_glyph) }
87
+ next "page #{index + 1}: size #{actual_page.size} instead of #{expected_page.size}" if expected_page.size != actual_page.size
88
+
89
+ position = expected_page.glyphs.zip(actual_page.glyphs).index { |expected_glyph, actual_glyph| !same_glyph?(expected_glyph, actual_glyph) }
90
+ next "page #{index + 1}: #{actual_page.glyphs.size} glyphs instead of #{expected_page.glyphs.size}" unless position
91
+
92
+ "page #{index + 1}: glyph #{position} is #{actual_page.glyphs[position].inspect} instead of #{expected_page.glyphs[position].inspect}"
93
+ end
94
+ end
95
+
96
+ def same_glyph?(expected_glyph, actual_glyph)
97
+ return false if actual_glyph.nil? || expected_glyph.first(3) != actual_glyph.first(3)
98
+
99
+ expected_glyph.last(2).zip(actual_glyph.last(2)).all? { |expected, actual| (expected - actual).abs <= POSITION_TOLERANCE }
100
+ end
101
+
102
+ # Two renderers that share no code, MuPDF and Poppler, must each draw both documents identically. Subset tags
103
+ # are made the same first: MuPDF hints glyphs differently whether a tag is valid or not.
104
+ def pixel_differences(expected, actual)
105
+ with_path(expected) do |expected_path|
106
+ with_path(actual) do |actual_path|
107
+ Dir.mktmpdir do |directory|
108
+ expected_path = with_neutral_subset_tags(expected_path, File.join(directory, "expected.pdf"))
109
+ actual_path = with_neutral_subset_tags(actual_path, File.join(directory, "actual.pdf"))
110
+ rendered_pixel_differences(expected_path, actual_path)
111
+ end
112
+ end
113
+ end
114
+ end
115
+
116
+ def rendered_pixel_differences(expected, actual)
117
+ RENDERERS.flat_map do |renderer, rasterizer|
118
+ Dir.mktmpdir do |directory|
119
+ expected_images = send(rasterizer, expected, File.join(directory, "expected"))
120
+ actual_images = send(rasterizer, actual, File.join(directory, "actual"))
121
+ expected_images.zip(actual_images).each_with_index.filter_map do |(expected_image, actual_image), index|
122
+ next if actual_image.nil? || File.binread(expected_image) == File.binread(actual_image)
123
+
124
+ "page #{index + 1}: #{differing_rows(expected_image, actual_image)} pixel rows differ at #{RESOLUTION_DPI} dpi#{" in #{renderer}" unless renderer == "MuPDF"}"
125
+ end
126
+ end
127
+ end
128
+ end
129
+
130
+ # Each page as an image file, by MuPDF.
131
+ def rasterize(pdf, prefix)
132
+ mutool("draw", "-r", RESOLUTION_DPI.to_s, "-o", "#{prefix}-%04d.pnm", pdf)
133
+ Dir["#{prefix}-*.pnm"].sort
134
+ end
135
+
136
+ def rasterize_with_poppler(pdf, prefix)
137
+ _output, status = run("pdftoppm", "-r", RESOLUTION_DPI.to_s, pdf, prefix)
138
+ run("pdftoppm", "-r", RESOLUTION_DPI.to_s, "-upw", password, pdf, prefix) if password && !status.success?
139
+ Dir["#{prefix}-*.ppm"].sort
140
+ end
141
+
142
+ def mutool(*arguments, path)
143
+ output, errors, status = Open3.capture3(tool("mutool"), arguments.first, "-q", *password_arguments("-p"), *arguments.drop(1), path, binmode: true)
144
+ raise "mutool #{arguments.first} failed on #{path}: #{errors}" unless status.success?
145
+
146
+ output
147
+ end
148
+
149
+ # The bytes of a PDF written to a file for the tools, or the path given (a String or a Pathname).
150
+ def with_path(pdf)
151
+ return yield pdf.to_s if pdf.is_a?(Pathname) || (!pdf.b.start_with?("%PDF") && !pdf.include?("\0") && File.file?(pdf))
152
+
153
+ Tempfile.create(["gamba-check", ".pdf"]) do |file|
154
+ file.binmode
155
+ file.write(pdf)
156
+ file.flush
157
+ yield file.path
158
+ end
159
+ end
160
+
161
+ # The PDF written to path with every subset tag the same. Only names after /BaseFont or /FontName change: compressed stream data can hold a tag's bytes by chance.
162
+ def with_neutral_subset_tags(pdf, path)
163
+ _output, status = run("qpdf", "--qdf", "--object-streams=disable", "--stream-data=preserve", pdf, path)
164
+ return pdf unless status.success? || status.exitstatus == QPDF_WARNINGS_EXIT_STATUS
165
+
166
+ File.binwrite(path, File.binread(path).gsub(%r{(/(?:BaseFont|FontName)\s*)/[0-9A-Za-z]{6}\+}n, '\1/AAAAAA+'))
167
+ path
168
+ end
169
+
170
+ private
171
+
172
+ def parse_text(xml)
173
+ xml.split("<page ").drop(1).map! do |page|
174
+ font = nil
175
+ glyphs = []
176
+ page.scan(/<(font|char) ([^>]*)>/) do |element, attributes|
177
+ value_by_name = attributes.scan(/(\w+)="([^"]*)"/).to_h
178
+ if element == "font"
179
+ font = [value_by_name.fetch("name").sub(/\A[A-Z]{6}\+/, ""), value_by_name.fetch("size").to_f.round(POSITION_PRECISION_DIGITS)]
180
+ else
181
+ position = value_by_name.fetch_values("x", "y").map { _1.to_f.round(POSITION_PRECISION_DIGITS) }
182
+ glyphs << [value_by_name.fetch("c"), *font, *position]
183
+ end
184
+ end
185
+ Page.new(size: page[/width="[^"]*" height="[^"]*"/], glyphs:)
186
+ end
187
+ end
188
+
189
+ def differing_rows(expected_image, actual_image)
190
+ expected_rows = pnm_rows(expected_image)
191
+ actual_rows = pnm_rows(actual_image)
192
+ return "all" if expected_rows.size != actual_rows.size
193
+
194
+ expected_rows.zip(actual_rows).count { |expected_row, actual_row| expected_row != actual_row }
195
+ end
196
+
197
+ def pnm_rows(image)
198
+ content = File.binread(image)
199
+ header = content[/\AP\d\s+\d+\s+\d+\s+\d+\s/]
200
+ _format, width, height = header.split.map { Integer(_1[/\d+/]) }
201
+ row_bytes = width * (content.start_with?("P5") ? 1 : 3)
202
+ Array.new(height) { |row| content.byteslice(header.bytesize + (row * row_bytes), row_bytes) }
203
+ end
204
+
205
+ def password_arguments(flag)
206
+ password ? [flag, password] : []
207
+ end
208
+
209
+ def run(name, *arguments)
210
+ Open3.capture2e(tool(name), *arguments)
211
+ end
212
+
213
+ def tool(name)
214
+ @found ||= {}
215
+ @found[name] ||= ENV.fetch("PATH", "").split(File::PATH_SEPARATOR).map { File.join(_1, name) }.find { File.executable?(_1) } ||
216
+ raise(ToolMissing, "#{name} is not installed: #{TOOLS.fetch(name)}")
217
+ end
218
+ end
219
+ end
220
+ end