prawn-extras 0.1.0 → 0.1.1

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
  SHA1:
3
- metadata.gz: 0674038f691b219cab7a8e3974ff74d97bba0c95
4
- data.tar.gz: 996793d3a1e293886c9553b5cdbb6205ada35039
3
+ metadata.gz: d8ee555f97717cd38b126129b42cf05ca6303bc1
4
+ data.tar.gz: 372404b0fedfa539b618002bef860e9f8a596feb
5
5
  SHA512:
6
- metadata.gz: 5f4dc02abfc5e4163615539cda0f3423b3f1dfb2ac14dfd9449e0a3ac5f24697cb2c78a91ec7e4b6bd8eeec1e4801a95d12bfeae7c9ed0e053ba13611b506749
7
- data.tar.gz: 977b5273cd914f7bdaa72a502afeaf5e09e1e466879d7f4cabe946fc7d6c2d78affea2046de91d74d762d75b9230f04fdce905bef1a7983dc923e54214216f04
6
+ metadata.gz: fa74c11b88d7359b56aa8e1d11c7aa1046c9d43fff88a3b09e553b9ac63aa95e9329e46e9decdad3df60c4de190a3f311660966dfab32b76f81a938c644e085a
7
+ data.tar.gz: c9997bc25526083a1e41c9edda945c374ce8a43bf8b30be9f97fde1cf33adbac46d427fc8fb0262b46ca9cc81324e8fe172ca9db6bc2366da9bc117adecc3240
@@ -3,42 +3,70 @@ module Prawn
3
3
  module Box
4
4
  delegate :top_left, :width, :height, to: :bounds
5
5
 
6
+ # Returns the most recent created box which was created using the "box"
7
+ # method. It will be nil if no boxes were created or if they were all
8
+ # created with the :dont_track option set to true.
6
9
  def last_created_box
7
10
  @last_created_box ||= nil
8
11
  end
9
12
 
10
- # Retorna um valor absoluto de width correspondente a porcentagem indicada,
11
- # nos limites do relatório, que é entre '0' e 'bounds.width'.
12
- # O parâmetro value deve ser um valor entre 0 e 100.
13
- # Ex: chamando percent_w(50) fora de um bounding_box: 50% da width da página
14
- # Ex: chamando percent_w(9) em um bounding_box: 9% da width do bounding_box
13
+ # Translates a percentage value to an absolute width value, within the
14
+ # limits of the current bounds (between 0 and "bounds.width").
15
+ #
16
+ # The value parameter must be an integer between 0 and 100. If a value
17
+ # outside these bounds is provided, it will be clamped.
18
+ #
15
19
  def percent_w(value)
16
- # Faz o clamp no valor para garantir que ele fique entre 0% e 100%
17
- clamped_value = [0, value, 100].sort[1]
18
- bounds.width * (clamped_value / 100.0)
20
+ bounds.width * (clamp_percentage(value) / 100.0)
19
21
  end
20
22
 
21
- # Retorna um valor absoluto de height correspondente a porcentagem indicada,
22
- # nos limites do relatório, que é entre 'bounds.height' e '0'.
23
- # O parâmetro value deve ser um valor entre 0 e 100.
24
- # Ex: chamando percent_h(50) fora de um bounding_box: 50% da height da página
25
- # Ex: chamando percent_h(9) em um bounding_box: 9% da height do bounding_box
23
+ # Translates a percentage value to an absolute width value, within the
24
+ # limits of the current bounds (between "bounds.height" and 0).
25
+ #
26
+ # The value parameter must be an integer between 0 and 100. If a value
27
+ # outside these bounds is provided, it will be clamped.
28
+ #
26
29
  def percent_h(value)
27
- # Faz o clamp no valor para garantir que ele fique entre 0% e 100%
28
- clamped_value = [0, value, 100].sort[1]
29
- bounds.height * (clamped_value / 100.0)
30
+ bounds.height * (clamp_percentage(value) / 100.0)
30
31
  end
31
32
 
32
- # Retorna um valor absoluto de height correspondente à height restante abaixo
33
- # de base_height. base_height deve ser um objeto do tipo Bounds
33
+ # Returns the absolute height value remaining to the bottom of the
34
+ # current bounds, relative to the provided base_height.
35
+ #
36
+ # The base_height parameter must be a Prawn::Document::BoundingBox object,
37
+ # and the remaining height is calculated from the bottom side of this
38
+ # object.
39
+ #
34
40
  def remaining_height(base_height)
35
41
  base_height.anchor[1] - bounds.anchor[1]
36
42
  end
37
43
 
38
- # Cria um bounding_box. Esse método é basicamente um alias de bounding_box,
39
- # exceto que oferece mais flexibilidade e facilidade na entrada dos
40
- # parâmetros, permitindo expressar width e height como porcentagens, ex:
41
- # box(top_left, '25%', '100%') {}
44
+ # This method is mostly an alias to Prawn's default bounding_box method.
45
+ # It passes parameters in a differnt way, but also includes the option to
46
+ # define a padding.
47
+ #
48
+ # The padding may be a single integer value applied to all sides, or four
49
+ # separate values passed as an array, applied clockwise starting from top.
50
+ #
51
+ # It also tracks by default which was the last box created (if using this
52
+ # method), so that the next box may be positioned relative to the previous
53
+ # one without the need to assign variables This can be disabled by
54
+ # setting the option :dont_track to any truthy value.
55
+ #
56
+ # The size parameters can be expressed either as points, which is the
57
+ # default, but also in percentages, both "global" and "local". To use
58
+ # percentages, the value must be passed as a String, with the % character
59
+ # at the end. The box will be created using "xx%" of the total available
60
+ # space of the current bounds.
61
+ #
62
+ # There's also the option to specify the width and height in percentages
63
+ # relative to the remaining space left. For example, if a box is created
64
+ # with its left side at 300, and the page is 600 wide, there's only 50% of
65
+ # space left, so width may only be 50% or lower. However, if width is
66
+ # passed as "100%l", with an l at the end, it will occupy "100%" of the
67
+ # remaining available space. This is useful for when a box must fill the
68
+ # remaining of the page but you don't know exactly where is the cursor.
69
+ #
42
70
  def box(position, width, height, options = {})
43
71
  size = build_size_options(position, width, height)
44
72
  box = bounding_box(position, size) do
@@ -48,46 +76,71 @@ module Prawn
48
76
  box
49
77
  end
50
78
 
51
- # Cria um novo bounding_box posicionado à direita do bounding_box pai.
52
- # O comportamento é o mesmo de chamar 'bounding_box', e aceita um bloco.
53
- # Este método retorna o bounding_box criado, para ser referenciado depois.
54
- # O parâmetro espaçamento é opcional e indica o tamanho do espaço entre a
55
- # direita do pai e a esquerda do novo bounding_box.
79
+ # Sets a padding inside the current bounds. It essentially creates a new
80
+ # bounding_box centered on the current one, smaller in size, to simulate
81
+ # padding.
82
+ #
83
+ # "values" may be a single integer value applied to all sides, or four
84
+ # separate values passed as an array, applied clockwise starting from top.
85
+ #
86
+ def padding(values)
87
+ values = build_padding_values(values)
88
+ position = padding_position(values)
89
+ width, height = padding_size(values)
90
+ bounding_box(position, width: width, height: height) { yield }
91
+ end
92
+
93
+ # Defines a box directly to the right of "origin_box". The position is
94
+ # calculated relative to "origin_box", all other parameters are the same
95
+ # as in the "box" method above.
96
+ #
97
+ # An additional :gutter option may be set. This will add the specified
98
+ # value as space between "origin_box" and the new box.
99
+ #
56
100
  def box_beside(origin_box, width, height, options = {}, &block)
57
- posicao = position_beside(origin_box, options[:gutter] || 0)
58
- box(posicao, width, height, options, &block)
101
+ position = position_beside(origin_box, options[:gutter] || 0)
102
+ box(position, width, height, options, &block)
59
103
  end
60
104
 
61
- # Cria um novo bounding_box posicionado à direita do último box criado.
62
- # Funciona como o método acima box_a_direita_de, exceto que ele pega o box
63
- # automaticamente. Caso não exista um último box criado (esse foi o primeiro),
64
- # ele irá posicionar o box no top_left.
105
+ # Defines a box directly to the right of the most recent previously
106
+ # created box. The position is calculated relative to it, all other
107
+ # parameters are the same as in the "box" method above.
108
+ #
109
+ # An additional :gutter option may be set. This will add the specified
110
+ # value as space between the previous and the new box.
111
+ #
65
112
  def box_beside_previous(width, height, options = {}, &block)
66
113
  box_beside(last_created_box, width, height, options, &block)
67
114
  end
68
115
 
69
- # Cria um novo bounding_box posicionado abaixo do bounding_box pai.
70
- # O comportamento é o mesmo de chamar 'bounding_box', e aceita um bloco.
71
- # Este método retorna o bounding_box criado, para ser referenciado depois.
72
- # O parâmetro espaçamento é opcional e indica o tamanho do espaço entre o
73
- # limite inferior do pai e o topo do novo bounding_box.
116
+ # Defines a box directly below "origin_box". The position is calculated
117
+ # relative to "origin_box", all other parameters are the same as in the
118
+ # "box" method above.
119
+ #
120
+ # An additional :gutter option may be set. This will add the specified
121
+ # value as space between "origin_box" and the new box.
122
+ #
74
123
  def box_below(origin_box, width, height, options = {}, &block)
75
- posicao = position_below(origin_box, options[:gutter] || 0)
76
- box(posicao, width, height, options, &block)
124
+ position = position_below(origin_box, options[:gutter] || 0)
125
+ box(position, width, height, options, &block)
77
126
  end
78
127
 
79
- # Cria um novo bounding_box posicionado abaixo do último box criado.
80
- # Funciona como o método acima box_abaixo_de, exceto que ele pega o box
81
- # automaticamente. Caso não exista um último box criado (esse foi o primeiro),
82
- # ele irá posicionar o box no top_left.
128
+ # Defines a box directly below the most recent previously created box.
129
+ # The position is calculated relative to it, all other parameters are the
130
+ # same as in the "box" method above.
131
+ #
132
+ # An additional :gutter option may be set. This will add the specified
133
+ # value as space between the previous and the new box.
134
+ #
83
135
  def box_below_previous(width, height, opcoes = {}, &block)
84
136
  box_below(last_created_box, width, height, opcoes, &block)
85
137
  end
86
138
 
87
- # Retorna uma posição (para um novo bounding_box), imediatamente à direita
88
- # de um bounding_box pai, alinhado verticalmente ao topo do pai.
89
- # O parâmetro espaçamento é opcional e indica o tamanho do espaço entre a
90
- # direita do pai e a esquerda dessa nova posição.
139
+ # Returns a two element array defining a position that is directly to the
140
+ # right of "origin_box". An optional gutter may be passed, and it will
141
+ # be converted to horizontal space between the "origin_box" and this new
142
+ # position.
143
+ #
91
144
  def position_beside(origin_box, gutter = 0)
92
145
  correct_origin = Array(origin_box).first
93
146
  return top_left if origin_box.nil?
@@ -95,10 +148,11 @@ module Prawn
95
148
  sum_dimensions(correct_origin.absolute_top_right, diff)
96
149
  end
97
150
 
98
- # Retorna uma posição (para um novo bounding_box), imediatamente abaixo de um
99
- # bounding_box pai, alinhado horizontalmente com a esquerda do pai.
100
- # O parâmetro espaçamento é opcional e indica o tamanho do espaço entre o
101
- # limite inferior do pai e o topo dessa nova posição.
151
+ # Returns a two element array defining a position that is directly below
152
+ # "origin_box". An optional gutter may be passed, and it will be
153
+ # converted to vertical space between the "origin_box" and this new
154
+ # position.
155
+ #
102
156
  def position_below(origin_box, gutter = 0)
103
157
  correct_origin = Array(origin_box).first
104
158
  return top_left if correct_origin.nil?
@@ -106,45 +160,20 @@ module Prawn
106
160
  sum_dimensions(correct_origin.absolute_bottom_left, diff)
107
161
  end
108
162
 
109
- # Cria um bounding box para agir como se fosse um padding (igual ao do CSS).
110
- # Tamanho pode ser um número, que será aplicado aos quatro lados, ou pode ser
111
- # um array com 4 valores, para o padding de cima, direita, baixo e esquerda,
112
- # nessa ordem.
113
- def padding(values)
114
- values = build_padding_values(values)
115
- posicao = padding_position(values)
116
- width, height = padding_size(values)
117
- bounding_box(posicao, width: width, height: height) { yield }
118
- end
119
-
120
163
  protected
121
164
 
122
- # Checa se o valor da width é uma string e contém o caractere %. Se tiver,
123
- # fazer o cálculo correto da width, senão, retornar a mesma width.
124
- # O cálculo da width é relativo ao espaço horizontal global. Para fazer ele
125
- # ser relativo ao espaço restante, basta adicionar um 'l' depois de '%'.
126
- # Espaço restante é o espaço que sobrou após a posição passada no parâmetro,
127
- # ou seja, se o pai ocupar metade da width da página, o valor em porcentagem
128
- # será relativo aos 50% livres, e não à width total da página.
129
165
  def t_width(position, width)
130
166
  return width unless width.to_s.include? '%'
131
- valor = percent_w(width.to_f) # Valor percentual global
132
- return valor unless width.to_s.include? 'l' # 'l' de 'local'
133
- valor * (1.0 - (position.first / bounds.width)) # Valor percentual relativo
134
- end
135
-
136
- # Checa se o valor da height é uma string e contém o caractere %. Se tiver,
137
- # fazer o cálculo correto da width, senão, retornar a mesma height.
138
- # O cálculo da height é relativo ao espaço vertical global. Para fazer ele
139
- # ser relativo ao espaço restante, basta adicionar um 'l' depois de '%'.
140
- # Espaço restante é o espaço que sobrou após a posição passada no parâmetro,
141
- # ou seja, se o pai ocupar metade da height da página, o valor em porcentagem
142
- # será relativo aos 50% livres, e não à height total da página.
167
+ valor = percent_w(width.to_f) # Global percentage
168
+ return valor unless width.to_s.include? 'l'
169
+ valor * (1.0 - (position.first / bounds.width)) # Local percentage
170
+ end
171
+
143
172
  def t_height(position, height)
144
173
  return height unless height.to_s.include? '%'
145
- valor = percent_h(height.to_f) # Valor percentual global
146
- return valor unless height.to_s.include? 'l' # 'l' de 'local'
147
- valor * (position.last / bounds.height) # Valor percentual relativo
174
+ valor = percent_h(height.to_f) # Global percentage
175
+ return valor unless height.to_s.include? 'l'
176
+ valor * (position.last / bounds.height) # Local percentage
148
177
  end
149
178
 
150
179
  def build_padding_values(values)
@@ -169,6 +198,10 @@ module Prawn
169
198
  def build_size_options(position, width, height)
170
199
  { width: t_width(position, width), height: t_height(position, height) }
171
200
  end
201
+
202
+ def clamp_percentage(value)
203
+ [0, value.to_i, 100].sort[1]
204
+ end
172
205
  end
173
206
  end
174
207
  end
@@ -30,9 +30,8 @@ module Prawn
30
30
  # ==========================================================================
31
31
  module DynamicRepeater
32
32
  # Saves a named value for a specific page of the generated PDF. This will
33
- # save the value for the specified page down to the first page of the
34
- # document (page 1), or until there's already another value saved for a
35
- # previous page.
33
+ # save the value for the specified page and also fill any page gaps with
34
+ # the latest previous value submitted.
36
35
  #
37
36
  # Example:
38
37
  #
@@ -43,10 +42,11 @@ module Prawn
43
42
  # override these values.
44
43
  #
45
44
  def store_value_in_page(key, value, page = page_number)
46
- page.downto(1).each do |page_index|
47
- next if repeater_values(key).keys.include?[page_index]
48
- repeater_values(key)[page_index] = value
45
+ latest_value = value_in_page(key, page) # Gets the last value submitted
46
+ (page - 1).downto(max_index(page)).each do |page_index|
47
+ repeater_values(key)[page_index] = latest_value
49
48
  end
49
+ repeater_values(key)[page] = value
50
50
  end
51
51
 
52
52
  # Returns the value for a key at a specific page. If the page is greater
@@ -66,13 +66,13 @@ module Prawn
66
66
  # value_in_page(:name, -1) => ""
67
67
  #
68
68
  def value_in_page(key, page, default_value = '')
69
- repeater_values(key)[[page, max_index(key).min]] || default_value
69
+ repeater_values(key)[[page, max_index(key)].min] || default_value
70
70
  end
71
71
 
72
72
  private
73
73
 
74
74
  def max_index(key)
75
- repeater_values(key).keys.max
75
+ repeater_values(key).keys.max.to_i
76
76
  end
77
77
 
78
78
  def repeater_values(key)
@@ -1,5 +1,5 @@
1
1
  module Prawn
2
2
  module Extras
3
- VERSION = '0.1.0'.freeze
3
+ VERSION = '0.1.1'.freeze
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,29 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: prawn-extras
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rodrigo Castro
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-12-09 00:00:00.000000000 Z
11
+ date: 2017-02-06 00:00:00.000000000 Z
12
12
  dependencies:
13
- - !ruby/object:Gem::Dependency
14
- name: rails
15
- requirement: !ruby/object:Gem::Requirement
16
- requirements:
17
- - - ">="
18
- - !ruby/object:Gem::Version
19
- version: 4.2.0
20
- type: :runtime
21
- prerelease: false
22
- version_requirements: !ruby/object:Gem::Requirement
23
- requirements:
24
- - - ">="
25
- - !ruby/object:Gem::Version
26
- version: 4.2.0
27
13
  - !ruby/object:Gem::Dependency
28
14
  name: prawn
29
15
  requirement: !ruby/object:Gem::Requirement
@@ -89,7 +75,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
89
75
  version: '0'
90
76
  requirements: []
91
77
  rubyforge_project:
92
- rubygems_version: 2.5.1
78
+ rubygems_version: 2.4.6
93
79
  signing_key:
94
80
  specification_version: 4
95
81
  summary: Extra functions for the great Prawn gem