proforma-prawn-renderer 1.0.2 → 1.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d353eb183e887bb3f96ea8d3c79cfc113fef27bf2057042b717e881dc445252b
4
- data.tar.gz: f71dc344909b7b68c44702b6cd2e4124a7849cfe2477e96e802a314734baa954
3
+ metadata.gz: 023bb2b06bdf92d4065a6c52bf6e6e522e21bf20ffe1c058797a68c8c83455bc
4
+ data.tar.gz: 9553acd72e862cff0b2d1b186bffd646187f51b17a9cf543f2ae3add94a3d3d6
5
5
  SHA512:
6
- metadata.gz: 41b223807262ed034c8a2ff0cf81c19bd1390993e202655d17ec306ed5306ffa108bf346d2c8845866d5625b5e8159d414dd440e57fae9d0addc52c3f8a9d1e8
7
- data.tar.gz: fda9c314d671fb2671ce73164a5081d35c996c8c689f141ef55c7049c1255e66bea2017c5ebcb12ed88f109bd5b5d8d89c932c7ffe5af404dd455a63aa81071d
6
+ metadata.gz: 204cabc54005e2f46ea269a8de1a4353ceffb952bb75af3de0051239bb78ff20f2d37bbcbdc3687b2a134f455dcb2c99fb3067ca296feda338d630eb7ad0c18a
7
+ data.tar.gz: e789d0114fa922808f7b373e0c2cae1044a53fb102a1a838a5e179d440fb2e7f123aa4016b536c509ad2d88e8d3529d4fb3920779aa10dde9b754d167d8f29b8
data/CHANGELOG.md CHANGED
@@ -1,3 +1,7 @@
1
+ # 1.0.3 (July 7th, 2019)
2
+
3
+ * Added better pane width balancing algorithm that fixes some width assignment bugs.
4
+
1
5
  # 1.0.2 (June 14th, 2019)
2
6
 
3
7
  * Added overflow and min_font_size attributes to cell_style for tables.
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- proforma-prawn-renderer (1.0.2)
4
+ proforma-prawn-renderer (1.0.3)
5
5
  prawn (~> 2)
6
6
  prawn-table (~> 0)
7
7
  proforma (~> 1)
@@ -0,0 +1,51 @@
1
+ # frozen_string_literal: true
2
+
3
+ #
4
+ # Copyright (c) 2019-present, Blue Marble Payroll, LLC
5
+ #
6
+ # This source code is licensed under the MIT license found in the
7
+ # LICENSE file in the root directory of this source tree.
8
+ #
9
+
10
+ module Proforma
11
+ class PrawnRenderer
12
+ # This class can balance out an un-even set of values.
13
+ class BalancedSet
14
+ class << self
15
+ def calculate(values, sum, round: 2)
16
+ free_spots = values.count(&:nil?)
17
+
18
+ if free_spots.positive?
19
+ available_value = sum - values.map(&:to_f).reduce(0, :+)
20
+
21
+ new_values = divide(free_spots, available_value, round)
22
+
23
+ values = values.map { |v| v || new_values.shift }
24
+ end
25
+
26
+ balance(values, sum, round)
27
+ end
28
+
29
+ private
30
+
31
+ def balance(values, sum, round)
32
+ difference = sum - values.map(&:to_f).reduce(0, :+)
33
+
34
+ return values if difference.zero?
35
+
36
+ diff_spots = divide(values.length, difference, round)
37
+
38
+ values.map.with_index { |value, index| value + diff_spots[index] }
39
+ end
40
+
41
+ def divide(count, sum, round)
42
+ spot_value = (sum / count.to_f).round(round)
43
+
44
+ remainder = sum - (spot_value * count)
45
+
46
+ Array.new(count) { |i| i.zero? ? spot_value + remainder : spot_value }
47
+ end
48
+ end
49
+ end
50
+ end
51
+ end
@@ -7,6 +7,7 @@
7
7
  # LICENSE file in the root directory of this source tree.
8
8
  #
9
9
 
10
+ require_relative 'balanced_set'
10
11
  require_relative 'renderer'
11
12
 
12
13
  module Proforma
@@ -78,18 +79,12 @@ module Proforma
78
79
  end
79
80
 
80
81
  def make_column_widths(columns)
81
- {}.tap do |column_widths|
82
- columns.each_with_index do |column, index|
83
- label_width = column.label_width
84
- value_width = column.value_width
82
+ widths = columns.map { |col| [col.label_width, col.value_width] }.flatten
85
83
 
86
- label_index = index * 2
87
- value_index = label_index + 1
88
-
89
- column_widths[label_index] = calculate_width(label_width) if label_width
90
- column_widths[value_index] = calculate_width(value_width) if value_width
91
- end
92
- end
84
+ BalancedSet.calculate(widths, 100)
85
+ .map
86
+ .with_index { |v, i| [i, calculate_width(v)] }
87
+ .to_h
93
88
  end
94
89
 
95
90
  def base_value_cell_style
@@ -9,6 +9,6 @@
9
9
 
10
10
  module Proforma
11
11
  class PrawnRenderer
12
- VERSION = '1.0.2'
12
+ VERSION = '1.0.3'
13
13
  end
14
14
  end
@@ -17,7 +17,7 @@ endobj
17
17
  >>
18
18
  endobj
19
19
  4 0 obj
20
- << /Length 2011
20
+ << /Length 2009
21
21
  >>
22
22
  stream
23
23
  q
@@ -173,7 +173,7 @@ ET
173
173
  0.0 0.0 0.0 SCN
174
174
 
175
175
  BT
176
- 224.0954 567.858 Td
176
+ 216.036 567.858 Td
177
177
  /F2.0 10 Tf
178
178
  [<4944204e756d626572>] TJ
179
179
  ET
@@ -182,7 +182,7 @@ ET
182
182
  0.0 0.0 0.0 SCN
183
183
 
184
184
  BT
185
- 361.0954 568.062 Td
185
+ 353.036 568.062 Td
186
186
  /F1.0 10 Tf
187
187
  [<31>] TJ
188
188
  ET
@@ -258,14 +258,14 @@ xref
258
258
  0000000109 00000 n
259
259
  0000000158 00000 n
260
260
  0000000215 00000 n
261
- 0000002278 00000 n
262
- 0000002576 00000 n
263
- 0000002678 00000 n
261
+ 0000002276 00000 n
262
+ 0000002574 00000 n
263
+ 0000002676 00000 n
264
264
  trailer
265
265
  << /Size 8
266
266
  /Root 2 0 R
267
267
  /Info 1 0 R
268
268
  >>
269
269
  startxref
270
- 2775
270
+ 2773
271
271
  %%EOF
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ #
4
+ # Copyright (c) 2019-present, Blue Marble Payroll, LLC
5
+ #
6
+ # This source code is licensed under the MIT license found in the
7
+ # LICENSE file in the root directory of this source tree.
8
+ #
9
+
10
+ require './spec/spec_helper'
11
+
12
+ describe ::Proforma::PrawnRenderer::BalancedSet do
13
+ describe '#calculate' do
14
+ specify 'with no explicit values' do
15
+ expect(described_class.calculate([nil], 100)).to eq([100])
16
+ expect(described_class.calculate([nil, nil], 100)).to eq([50, 50])
17
+ expect(described_class.calculate([nil, nil, nil], 100)).to eq([33.34, 33.33, 33.33])
18
+ end
19
+
20
+ specify 'with some explicit values' do
21
+ expect(described_class.calculate([50, nil], 100)).to eq([50, 50])
22
+ expect(described_class.calculate([46.53, nil, nil], 100)).to eq([46.53, 26.73, 26.74])
23
+ end
24
+
25
+ specify 'with all explicit values' do
26
+ expect(described_class.calculate([50, 50], 100)).to eq([50, 50])
27
+ expect(described_class.calculate([50, 41, 30], 100)).to eq([43, 34, 23])
28
+ expect(described_class.calculate([2, 40, 37], 100)).to eq([9, 47, 44])
29
+ end
30
+ end
31
+ end
@@ -80,4 +80,38 @@ describe ::Proforma::PrawnRenderer do
80
80
 
81
81
  expect(documents.first.contents).to eq(pdf)
82
82
  end
83
+
84
+ specify 'panes with un-even column widths equalize' do
85
+ data = [
86
+ { id: 1, name: 'Chicago Bulls' },
87
+ { id: 2, name: 'Indiana Pacers' },
88
+ { id: 3, name: 'Boston Celtics' }
89
+ ]
90
+
91
+ template = {
92
+ children: [
93
+ {
94
+ type: 'Pane',
95
+ columns: [
96
+ {
97
+ label_width: '0.5',
98
+ value_width: '',
99
+ lines: [
100
+ { label: 'Label', value: 'Value' }
101
+ ]
102
+ },
103
+ {
104
+ label_width: '10',
105
+ value_width: '',
106
+ lines: [
107
+ { label: 'Label', value: 'Value' }
108
+ ]
109
+ }
110
+ ]
111
+ }
112
+ ]
113
+ }
114
+
115
+ Proforma.render(data, template, renderer: Proforma::PrawnRenderer.new)
116
+ end
83
117
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: proforma-prawn-renderer
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.2
4
+ version: 1.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matthew Ruggio
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-06-14 00:00:00.000000000 Z
11
+ date: 2019-07-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: prawn
@@ -203,6 +203,7 @@ files:
203
203
  - bin/console
204
204
  - bin/render
205
205
  - lib/proforma/prawn_renderer.rb
206
+ - lib/proforma/prawn_renderer/balanced_set.rb
206
207
  - lib/proforma/prawn_renderer/banner_renderer.rb
207
208
  - lib/proforma/prawn_renderer/header_renderer.rb
208
209
  - lib/proforma/prawn_renderer/pane_renderer.rb
@@ -217,6 +218,7 @@ files:
217
218
  - spec/fixtures/component_test.yml
218
219
  - spec/fixtures/snapshots/user_details.yml
219
220
  - spec/fixtures/snapshots/user_list.yml
221
+ - spec/proforma/prawn_renderer/balanced_set_spec.rb
220
222
  - spec/proforma/prawn_renderer_spec.rb
221
223
  - spec/spec_helper.rb
222
224
  homepage: https://github.com/bluemarblepayroll/proforma-prawn-renderer
@@ -247,5 +249,6 @@ test_files:
247
249
  - spec/fixtures/component_test.yml
248
250
  - spec/fixtures/snapshots/user_details.yml
249
251
  - spec/fixtures/snapshots/user_list.yml
252
+ - spec/proforma/prawn_renderer/balanced_set_spec.rb
250
253
  - spec/proforma/prawn_renderer_spec.rb
251
254
  - spec/spec_helper.rb