prawn_hebrew 0.1.2 → 0.1.4

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 +89 -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: 62db7c69cd7c2e1252b867a77a7149cb529c804a58e1487bf70baf2d4b069ec6
4
+ data.tar.gz: b330b9b04cbec401d31fda50971e26b5eda97c213fd62db46a6f47b3fd4027cf
5
5
  SHA512:
6
- metadata.gz: 2f6d7b8727ded96d4ccd4d837e4d8eae48aa04b96b3078368eb2ae0a5c358375eb258e7114d7b7d658d0276b8e5cad7a85d6f31c704807c8020f4633b17b8167
7
- data.tar.gz: b0b4ac686442a1ae89c57691b9e8f064c1951763b1f4d6c4ae9183b11c3e61ec4bb1244a048b65b41081d27052d4637f49b189c6fe507425122bff59207712f1
6
+ metadata.gz: a7e3d327abda1b5a64e81d95cbbded4c5592ac9401a66630073dbc1a2dd2f2e940a868f2924f609ddbf43157fb55f5cc2c3c3f52d48d640b65ef4fcfa1a527b2
7
+ data.tar.gz: 1734fa4415c8b42148ea2a939b3066a8c0f3662fd07bed346da3ae2df147151282fc3a9b7ff673dc7fdb61d259f7b02b467ae22951092c9429478d21e17e1d3e
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,65 @@ 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
+ fitting_size = nil
164
+
165
+ # Find the largest font size that fits by testing with dry_run
166
+ 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)
170
+
171
+ test_opts = box_opts.dup
172
+ test_opts[:leading] = leading if leading > 0
173
+
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))
182
+ end
183
+ end
184
+
185
+ # If overflow_text is empty or nil, the text fits
186
+ if overflow_text.nil? || overflow_text.empty? || overflow_text == ""
187
+ fitting_size = current_size
188
+ break
189
+ end
190
+
191
+ # Reduce font size and try again
192
+ current_size -= 0.5
193
+ end
194
+
195
+ # Use the fitting size, or min_size if nothing fit
196
+ final_size = fitting_size || min_size
197
+
198
+ # Now render once with the final size
199
+ fragments = hebrew_formatted_text(text, size: final_size, style: style,
200
+ hebrew_font: hebrew_font,
201
+ english_font: english_font)
202
+
203
+ render_opts = box_opts.dup
204
+ render_opts[:leading] = leading if leading > 0
205
+
206
+ character_spacing(char_spacing) do
207
+ if rotation != 0
208
+ rotate(rotation, origin: render_opts[:at] || [0, 0]) do
209
+ formatted_text_box(fragments, render_opts)
210
+ end
211
+ else
212
+ formatted_text_box(fragments, render_opts)
213
+ end
214
+ end
215
+ end
138
216
  end
139
217
  end
140
218
 
data/lib/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module PrawnHebrew
2
- VERSION = "0.1.2"
2
+ VERSION = "0.1.4"
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.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ben Lite