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.
@@ -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 < clusters.size
30
- ligature = (@ligatures_by_first_glyph[clusters[index][0]] || []).find do |components, _|
31
- components.each_with_index.all? { |component, offset| clusters[index + 1 + offset]&.first == component }
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
- components, glyph = ligature
35
- shaped << [glyph, clusters[index, components.size + 1].flat_map(&:last)]
36
- index += components.size + 1
44
+ length = ligature[0].size + 1
45
+ yield ligature[1], index, length
37
46
  else
38
- shaped << clusters[index]
39
- index += 1
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 do |glyph, codes|
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
@@ -6,5 +6,5 @@ module Prawn
6
6
  end
7
7
 
8
8
  module Gamba
9
- VERSION = "2.0.2"
9
+ VERSION = "2.1.0"
10
10
  end
data/lib/gamba.rb CHANGED
@@ -55,6 +55,7 @@ require_relative "gamba/view"
55
55
  require_relative "gamba/images"
56
56
  require_relative "gamba/images/png_decoder"
57
57
  require_relative "gamba/images/image"
58
+ require_relative "gamba/barcodes"
58
59
  require_relative "gamba/repeater"
59
60
  require_relative "gamba/grid"
60
61
  require_relative "gamba/outline"
@@ -64,6 +65,7 @@ require_relative "gamba/table/cell"
64
65
  require_relative "gamba/table/cell/kinds"
65
66
  require_relative "gamba/security"
66
67
  require_relative "gamba/pdf_a"
68
+ require_relative "gamba/tagging"
67
69
  require_relative "gamba/pdf_a/srgb_profile"
68
70
  require_relative "gamba/document/internals"
69
71
  require_relative "gamba/document/bounding_box"
@@ -98,6 +100,19 @@ end
98
100
 
99
101
  module Gamba
100
102
  autoload :Combine, ::File.expand_path("gamba/combine", __dir__)
103
+ autoload :Copier, ::File.expand_path("gamba/copier", __dir__)
104
+ autoload :ImportedPage, ::File.expand_path("gamba/imported_page", __dir__)
105
+ autoload :PageBoxes, ::File.expand_path("gamba/page_boxes", __dir__)
106
+ autoload :Document, ::File.expand_path("gamba/native/document", __dir__)
107
+ autoload :Layout, ::File.expand_path("gamba/native/layout", __dir__)
108
+
109
+ # A document written with Gamba's own API: Gamba.document(page_size: "A4") { |document| document.text "Hello" }.
110
+ # See Gamba::Document::OPTIONS for the options; Prawn's API stays reachable through Gamba::Document#prawn.
111
+ def self.document(**options)
112
+ document = Document.new(**options)
113
+ yield document if block_given?
114
+ document
115
+ end
101
116
 
102
117
  # The pages of other PDF files, one after the other in a new one:
103
118
  # Gamba.combine([label, label], page_size: [288, 432]). See Gamba::Combine#initialize for the options.
@@ -108,7 +123,7 @@ module Gamba
108
123
  # Loads what Gamba otherwise loads on first use, SVG support included: call it before a server forks its
109
124
  # workers, so they share that memory instead of each loading its own copy.
110
125
  def self.eager_load!
111
- [TrueType::File, TrueType::Subset, Prawn::Fonts::TTF, Combine, PDF::Core::Reader]
126
+ [TrueType::File, TrueType::Subset, Prawn::Fonts::TTF, Combine, ImportedPage, Barcodes::QRCode, PDF::Core::Reader, Document]
112
127
  require "prawn-svg"
113
128
  end
114
129
  end
@@ -122,3 +137,5 @@ end
122
137
 
123
138
  Prawn.image_handler.register(Prawn::Images::JPG)
124
139
  Prawn.image_handler.register(Prawn::Images::PNG)
140
+
141
+ require_relative "gamba/railtie" if defined?(Rails::Railtie)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gamba
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.2
4
+ version: 2.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Level UP Solutions
@@ -83,9 +83,10 @@ dependencies:
83
83
  - - "<"
84
84
  - !ruby/object:Gem::Version
85
85
  version: '4'
86
- description: 'Gamba is a Ruby library that writes PDF files. It is a faster rewrite
87
- of Prawn 2.5, prawn-table and prawn-svg: code written for them runs unchanged and
88
- draws the same pages, in lighter files.'
86
+ description: Gamba is a Ruby library that writes PDF files from stacks, rows, tables,
87
+ headers and footers it measures and places, tagged for PDF/UA. It also runs code
88
+ written for Prawn 2.5, prawn-table and prawn-svg unchanged, drawing the same pages
89
+ faster and in lighter files.
89
90
  executables: []
90
91
  extensions: []
91
92
  extra_rdoc_files: []
@@ -114,7 +115,12 @@ files:
114
115
  - data/fonts/ZapfDingbats.afm
115
116
  - lib/gamba.rb
116
117
  - lib/gamba/annotations.rb
118
+ - lib/gamba/barcodes.rb
119
+ - lib/gamba/barcodes/data_matrix.rb
120
+ - lib/gamba/barcodes/linear.rb
121
+ - lib/gamba/barcodes/qr_code.rb
117
122
  - lib/gamba/combine.rb
123
+ - lib/gamba/copier.rb
118
124
  - lib/gamba/document.rb
119
125
  - lib/gamba/document/bounding_box.rb
120
126
  - lib/gamba/document/column_box.rb
@@ -135,10 +141,17 @@ files:
135
141
  - lib/gamba/images.rb
136
142
  - lib/gamba/images/image.rb
137
143
  - lib/gamba/images/png_decoder.rb
144
+ - lib/gamba/imported_page.rb
138
145
  - lib/gamba/measurement_extensions.rb
139
146
  - lib/gamba/measurements.rb
147
+ - lib/gamba/minitest.rb
148
+ - lib/gamba/native/band.rb
149
+ - lib/gamba/native/document.rb
150
+ - lib/gamba/native/layout.rb
151
+ - lib/gamba/native/table.rb
140
152
  - lib/gamba/outline.rb
141
153
  - lib/gamba/page.rb
154
+ - lib/gamba/page_boxes.rb
142
155
  - lib/gamba/page_geometry.rb
143
156
  - lib/gamba/pdf.rb
144
157
  - lib/gamba/pdf/decryption.rb
@@ -148,13 +161,18 @@ files:
148
161
  - lib/gamba/pdf/writer.rb
149
162
  - lib/gamba/pdf_a.rb
150
163
  - lib/gamba/pdf_a/srgb_profile.rb
164
+ - lib/gamba/pdf_check.rb
165
+ - lib/gamba/railtie.rb
151
166
  - lib/gamba/repeater.rb
167
+ - lib/gamba/rspec.rb
152
168
  - lib/gamba/security.rb
153
169
  - lib/gamba/stamp.rb
154
170
  - lib/gamba/table.rb
155
171
  - lib/gamba/table/cell.rb
156
172
  - lib/gamba/table/cell/kinds.rb
157
173
  - lib/gamba/table/cells.rb
174
+ - lib/gamba/tagging.rb
175
+ - lib/gamba/testing.rb
158
176
  - lib/gamba/text.rb
159
177
  - lib/gamba/text/box.rb
160
178
  - lib/gamba/text/formatted/arranger.rb
@@ -279,6 +297,6 @@ required_rubygems_version: !ruby/object:Gem::Requirement
279
297
  requirements: []
280
298
  rubygems_version: 4.0.20
281
299
  specification_version: 4
282
- summary: A faster rewrite of Prawn, prawn-table and prawn-svg that draws the same
283
- pages
300
+ summary: PDFs from layout you describe, tagged for screen readers; runs Prawn code
301
+ unchanged, faster
284
302
  test_files: []