prawn_hebrew 0.1.2 → 0.1.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.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/lib/prawn_hebrew.rb +97 -11
  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: 2ca8bc44877a68e2516ecd5cfc591e40efa870723c454557afc79f5ebc15ba73
4
- data.tar.gz: 00bb555cbfb15f29ef9f97be0acb18aabb3a4c6462b91fc52d73fa81205e7b46
3
+ metadata.gz: c85ea02d489bf50f5f0c8facaab7928485a9042f324a54bfb06cd974708e5324
4
+ data.tar.gz: 21a63093c22ffe1baf7c9b646a000138ef05694448315f077642a0e7f72acf43
5
5
  SHA512:
6
- metadata.gz: 2f6d7b8727ded96d4ccd4d837e4d8eae48aa04b96b3078368eb2ae0a5c358375eb258e7114d7b7d658d0276b8e5cad7a85d6f31c704807c8020f4633b17b8167
7
- data.tar.gz: b0b4ac686442a1ae89c57691b9e8f064c1951763b1f4d6c4ae9183b11c3e61ec4bb1244a048b65b41081d27052d4637f49b189c6fe507425122bff59207712f1
6
+ metadata.gz: c4ab1e0ce09de9756667400c9b77b43d2f859734056adce3252ffd327af19c1fc97d79ec8c4832c794ef1020a971980e32b9dbd4adf3d4b1960772e06e2edcc7
7
+ data.tar.gz: dc6e7cd6daae94a23eb250652dd9d1ec754439e921cd3d0707d2b3060cf6515d1e8859e5e32502e46ba0fdbfc8a55fdf62665a0b9089e4c92871a542ea75c66c
data/lib/prawn_hebrew.rb CHANGED
@@ -81,6 +81,8 @@ module PrawnHebrew
81
81
  rotation = box_opts.delete(:rotate) || 0
82
82
  char_spacing = box_opts.delete(:character_spacing) || 0
83
83
  leading = box_opts.delete(:leading) || 0
84
+ min_font_size = box_opts.delete(:min_font_size)
85
+ overflow = box_opts[:overflow]
84
86
 
85
87
  # Check if text contains Hebrew characters
86
88
  contains_hebrew = text.to_s =~ /\p{Hebrew}/
@@ -90,21 +92,38 @@ module PrawnHebrew
90
92
  direction = contains_hebrew ? :rtl : :ltr
91
93
  end
92
94
 
93
- # For completely English text (no Hebrew characters and LTR direction),
94
- # use standard Prawn text_box for better performance and compatibility
95
- if !contains_hebrew && direction == :ltr
96
- render_english_only_text(text, final_size, style, final_english_font,
97
- rotation, char_spacing, leading, box_opts)
95
+ # Handle shrink_to_fit behavior
96
+ if overflow == :shrink_to_fit
97
+ box_opts.delete(:overflow)
98
+
99
+ if !contains_hebrew && direction == :ltr
100
+ # English-only: use Prawn's built-in shrink_to_fit
101
+ box_opts[:overflow] = :shrink_to_fit
102
+ box_opts[:min_font_size] = min_font_size if min_font_size
103
+ render_english_only_text(text, final_size, style, final_english_font,
104
+ rotation, char_spacing, leading, box_opts)
105
+ else
106
+ # Hebrew/mixed: implement shrinking manually
107
+ shrink_hebrew_text_to_fit(text, final_size, style, final_hebrew_font,
108
+ final_english_font, char_spacing, leading,
109
+ min_font_size, rotation, box_opts)
110
+ end
98
111
  else
99
- # For Hebrew text or RTL direction, use formatted text approach
100
- if rotation != 0
101
- rotate(rotation, origin: box_opts[:at] || [0, 0]) do
112
+ # Normal rendering without shrinking
113
+ if !contains_hebrew && direction == :ltr
114
+ render_english_only_text(text, final_size, style, final_english_font,
115
+ rotation, char_spacing, leading, box_opts)
116
+ else
117
+ # For Hebrew text or RTL direction, use formatted text approach
118
+ if rotation != 0
119
+ rotate(rotation, origin: box_opts[:at] || [0, 0]) do
120
+ render_hebrew_text_content(text, contains_hebrew, direction, final_size, style,
121
+ final_hebrew_font, final_english_font, char_spacing, leading, box_opts)
122
+ end
123
+ else
102
124
  render_hebrew_text_content(text, contains_hebrew, direction, final_size, style,
103
125
  final_hebrew_font, final_english_font, char_spacing, leading, box_opts)
104
126
  end
105
- else
106
- render_hebrew_text_content(text, contains_hebrew, direction, final_size, style,
107
- final_hebrew_font, final_english_font, char_spacing, leading, box_opts)
108
127
  end
109
128
  end
110
129
  end
@@ -135,6 +154,73 @@ module PrawnHebrew
135
154
  formatted_text_box(fragments, box_opts)
136
155
  end
137
156
  end
157
+
158
+ # Shrink Hebrew text to fit in the box by reducing font size
159
+ def shrink_hebrew_text_to_fit(text, initial_size, style, hebrew_font, english_font,
160
+ char_spacing, leading, min_font_size, rotation, box_opts)
161
+ min_size = min_font_size || 5
162
+ current_size = initial_size
163
+
164
+ # Try rendering with progressively smaller font sizes until it fits
165
+ while current_size >= min_size
166
+ # Create a copy of box_opts to test rendering
167
+ test_opts = box_opts.dup
168
+
169
+ # Try rendering at current size
170
+ fragments = hebrew_formatted_text(text, size: current_size, style: style,
171
+ hebrew_font: hebrew_font,
172
+ english_font: english_font)
173
+
174
+ test_opts[:leading] = leading if leading > 0
175
+
176
+ # Test if it fits by attempting to render
177
+ success = character_spacing(char_spacing) do
178
+ if rotation != 0
179
+ rotate(rotation, origin: test_opts[:at] || [0, 0]) do
180
+ result = formatted_text_box(fragments, test_opts.merge(dry_run: true))
181
+ result.empty? || result == ""
182
+ end
183
+ else
184
+ result = formatted_text_box(fragments, test_opts.merge(dry_run: true))
185
+ result.empty? || result == ""
186
+ end
187
+ end
188
+
189
+ # If it fits, render it for real and return
190
+ if success
191
+ character_spacing(char_spacing) do
192
+ if rotation != 0
193
+ rotate(rotation, origin: box_opts[:at] || [0, 0]) do
194
+ formatted_text_box(fragments, box_opts.merge(leading: leading > 0 ? leading : nil).compact)
195
+ end
196
+ else
197
+ formatted_text_box(fragments, box_opts.merge(leading: leading > 0 ? leading : nil).compact)
198
+ end
199
+ end
200
+ return
201
+ end
202
+
203
+ # Reduce font size and try again
204
+ current_size -= 0.5
205
+ end
206
+
207
+ # If we've reached min size, render at min size anyway
208
+ fragments = hebrew_formatted_text(text, size: min_size, style: style,
209
+ hebrew_font: hebrew_font,
210
+ english_font: english_font)
211
+
212
+ box_opts[:leading] = leading if leading > 0
213
+
214
+ character_spacing(char_spacing) do
215
+ if rotation != 0
216
+ rotate(rotation, origin: box_opts[:at] || [0, 0]) do
217
+ formatted_text_box(fragments, box_opts)
218
+ end
219
+ else
220
+ formatted_text_box(fragments, box_opts)
221
+ end
222
+ end
223
+ end
138
224
  end
139
225
  end
140
226
 
data/lib/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module PrawnHebrew
2
- VERSION = "0.1.2"
2
+ VERSION = "0.1.3"
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.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ben Lite