prawn_hebrew 0.1.4 → 0.1.6

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.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/lib/prawn_hebrew.rb +28 -18
  3. data/lib/version.rb +1 -1
  4. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 62db7c69cd7c2e1252b867a77a7149cb529c804a58e1487bf70baf2d4b069ec6
4
- data.tar.gz: b330b9b04cbec401d31fda50971e26b5eda97c213fd62db46a6f47b3fd4027cf
3
+ metadata.gz: 1c923e10c8587bf590c02c70f3ef62dd6d2e95c6fb5c338c453e41db11fce73a
4
+ data.tar.gz: 15fee6431648b088fd6b6e824db4a00f43880eb7a30cc208a0ecbc0b59ffeaf6
5
5
  SHA512:
6
- metadata.gz: a7e3d327abda1b5a64e81d95cbbded4c5592ac9401a66630073dbc1a2dd2f2e940a868f2924f609ddbf43157fb55f5cc2c3c3f52d48d640b65ef4fcfa1a527b2
7
- data.tar.gz: 1734fa4415c8b42148ea2a939b3066a8c0f3662fd07bed346da3ae2df147151282fc3a9b7ff673dc7fdb61d259f7b02b467ae22951092c9429478d21e17e1d3e
6
+ metadata.gz: b6ed446d3c436f65630bfb528b79735e6c2d9cc4120b68e70c7c173047868081068a3eb22689d79ac472d2db7306cf3373feb8998b520b613f9fa466f69a27f3
7
+ data.tar.gz: a4ae2aaa3b7b7e8990df02a953345010e66aaa41b167ba3dea0e44011d1719081397fcc2fea5a2c5e12e2ae0c04eb2ca94e84d464e4b23fcc4045e87e7673370
data/lib/prawn_hebrew.rb CHANGED
@@ -159,43 +159,53 @@ module PrawnHebrew
159
159
  def shrink_hebrew_text_to_fit(text, initial_size, style, hebrew_font, english_font,
160
160
  char_spacing, leading, min_font_size, rotation, box_opts)
161
161
  min_size = min_font_size || 5
162
+
163
+ # Get box dimensions
164
+ box_width = box_opts[:width]
165
+ box_height = box_opts[:height]
166
+
167
+ return render_hebrew_text_content(text, true, :rtl, initial_size, style,
168
+ hebrew_font, english_font, char_spacing, leading, box_opts) unless box_width && box_height
169
+
162
170
  current_size = initial_size
163
171
  fitting_size = nil
164
172
 
165
- # Find the largest font size that fits by testing with dry_run
173
+ # Try progressively smaller sizes
166
174
  while current_size >= min_size
167
- fragments = hebrew_formatted_text(text, size: current_size, style: style,
168
- hebrew_font: hebrew_font,
169
- english_font: english_font)
175
+ # Calculate approximate text dimensions at this size
176
+ test_fragments = hebrew_formatted_text(text, size: current_size, style: style,
177
+ hebrew_font: hebrew_font,
178
+ english_font: english_font)
170
179
 
171
- test_opts = box_opts.dup
172
- test_opts[:leading] = leading if leading > 0
180
+ # Calculate total text width by measuring each fragment
181
+ total_width = 0
182
+ total_height = current_size * 1.2 # Approximate line height
173
183
 
174
- # Test if it fits without actually rendering
175
- overflow_text = character_spacing(char_spacing) do
176
- if rotation != 0
177
- rotate(rotation, origin: test_opts[:at] || [0, 0]) do
178
- formatted_text_box(fragments, test_opts.merge(dry_run: true))
179
- end
180
- else
181
- formatted_text_box(fragments, test_opts.merge(dry_run: true))
184
+ test_fragments.each do |fragment|
185
+ next if fragment[:text] == "\n"
186
+ fragment_font = fragment[:font] || hebrew_font
187
+ font(fragment_font) do
188
+ total_width += width_of(fragment[:text], size: current_size)
182
189
  end
183
190
  end
184
191
 
185
- # If overflow_text is empty or nil, the text fits
186
- if overflow_text.nil? || overflow_text.empty? || overflow_text == ""
192
+ # Count newlines to estimate height
193
+ line_count = text.count("\n") + 1
194
+ total_height = (current_size * 1.2 * line_count) + (leading * (line_count - 1))
195
+
196
+ # Check if it would fit (with some padding)
197
+ if total_width <= (box_width * 0.95) && total_height <= (box_height * 0.95)
187
198
  fitting_size = current_size
188
199
  break
189
200
  end
190
201
 
191
- # Reduce font size and try again
192
202
  current_size -= 0.5
193
203
  end
194
204
 
195
205
  # Use the fitting size, or min_size if nothing fit
196
206
  final_size = fitting_size || min_size
197
207
 
198
- # Now render once with the final size
208
+ # Render with the final size
199
209
  fragments = hebrew_formatted_text(text, size: final_size, style: style,
200
210
  hebrew_font: hebrew_font,
201
211
  english_font: english_font)
data/lib/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module PrawnHebrew
2
- VERSION = "0.1.4"
2
+ VERSION = "0.1.6"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: prawn_hebrew
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ben Lite