prawn_hebrew 0.0.6 → 0.0.8

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 +82 -12
  3. data/lib/version.rb +2 -2
  4. metadata +3 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d5df90e0dbfa49359bec17f0242bd74575961defdbcd08fa0a9e349e16b741cb
4
- data.tar.gz: 0d6d7e0e2894a4aed5174ca6a16fe7d2e53f44546974feb741d417f5c46c67a4
3
+ metadata.gz: 5bce759324c9ddd6c50281d64a986b18df0390951268ec3b1d73070ab49f9484
4
+ data.tar.gz: 1213589f341946c555682929bd737ffe8d715ee9400f174e01377751c9c7c64c
5
5
  SHA512:
6
- metadata.gz: ddaaa14bc478082942318462c87f774ad2ed195bd1fe8d3595179a7b61ec3c0d295fb97829f0a88ce575fd496b5c0bffb49d2e67a1a09316e3ebbc0228d0bc5d
7
- data.tar.gz: 524de0466b4737077454a0e42148c3ec96ab8e4d6e8883d32440f7bd0cfe9edcb9f147b8a9ad837795d98fdc3f6ec75d24720ca19c4a529fa7467e00955e33a9
6
+ metadata.gz: e9b463d274d655efcade8b1c3acab361e9f4776c9fce6c6aedc09dee0f6c8b9d7c3cd6cd2c8ae2a2f5f7e6888155477d550708e654b41561dddb765a113d4c01
7
+ data.tar.gz: 787b80276ff7af3e9da4c3067192562a4b8ba0f5af9cd0cd52e75e7ffd5431e32cbfba215d671728407c5dcdd8cbd052bc19baaae65f61312db8c503faab4146
data/lib/prawn_hebrew.rb CHANGED
@@ -4,16 +4,29 @@ require_relative 'version'
4
4
  module PrawnHebrew
5
5
  module Text
6
6
  DEFAULT_HEBREW_FONT = 'GveretLevinHebrew'.freeze
7
- DEFAULT_ENGLISH_FONT = 'Arial'.freeze
7
+ DEFAULT_ENGLISH_FONT = 'Helvetica'.freeze
8
+
9
+ # Set to true for debugging which text rendering path is used
10
+ DEBUG_MODE = false
11
+ INVISIBLE_CHARS = /[\u200B\u200C\u200D\u200E\u200F\uFEFF\u00AD\u202A\u202B\u202C\u202D\u202E]/.freeze
12
+ NBSP_CHARS = /[\u00A0\u202F]/.freeze
13
+
14
+ def sanitize_text(text)
15
+ return text if text.nil?
16
+ text.to_s.gsub(INVISIBLE_CHARS, '').gsub(NBSP_CHARS, ' ')
17
+ end
8
18
 
9
19
  def hebrew_formatted_text(text, size: 12, style: :normal, hebrew_font: DEFAULT_HEBREW_FONT, english_font: DEFAULT_ENGLISH_FONT)
20
+ text = sanitize_text(text)
10
21
  words = text.to_s.split(/(\s+|\n)/)
11
22
  hebrew_run = []
12
23
  fragments = []
24
+
25
+ styles = style.is_a?(Array) ? style : [style].compact
13
26
 
14
27
  words.each do |word|
15
28
  if word.strip.empty?
16
- fragments << { text: word, font: english_font, size: size, styles: [style] } if word != ' '
29
+ fragments << { text: word, font: english_font, size: size, styles: styles } if word != ' '
17
30
  next
18
31
  end
19
32
 
@@ -22,20 +35,20 @@ module PrawnHebrew
22
35
  else
23
36
  unless hebrew_run.empty?
24
37
  hebrew_run.reverse.each_with_index do |hw, idx|
25
- fragments << { text: hw, font: hebrew_font, size: size, direction: :rtl, styles: [style] }
26
- fragments << { text: ' ', font: hebrew_font, size: size, direction: :rtl, styles: [style] } if idx < hebrew_run.length - 1
38
+ fragments << { text: hw, font: hebrew_font, size: size, direction: :rtl, styles: styles }
39
+ fragments << { text: ' ', font: hebrew_font, size: size, direction: :rtl, styles: styles } if idx < hebrew_run.length - 1
27
40
  end
28
41
  fragments << { text: ' ' }
29
42
  hebrew_run.clear
30
43
  end
31
- fragments << { text: "#{word} ", font: english_font, size: size, styles: [style] }
44
+ fragments << { text: "#{word} ", font: english_font, size: size, styles: styles }
32
45
  end
33
46
  end
34
47
 
35
48
  unless hebrew_run.empty?
36
49
  hebrew_run.reverse.each_with_index do |hw, idx|
37
- fragments << { text: hw, font: hebrew_font, size: size, direction: :rtl, styles: [style] }
38
- fragments << { text: ' ', font: hebrew_font, size: size, direction: :rtl, styles: [style] } if idx < hebrew_run.length - 1
50
+ fragments << { text: hw, font: hebrew_font, size: size, direction: :rtl, styles: styles }
51
+ fragments << { text: ' ', font: hebrew_font, size: size, direction: :rtl, styles: styles } if idx < hebrew_run.length - 1
39
52
  end
40
53
  end
41
54
  fragments
@@ -43,11 +56,68 @@ module PrawnHebrew
43
56
 
44
57
  def hebrew_text_box(text, size: 12, style: :normal,
45
58
  hebrew_font: DEFAULT_HEBREW_FONT,
46
- english_font: DEFAULT_ENGLISH_FONT, **box_opts)
47
- fragments = hebrew_formatted_text(text, size: size, style: style,
48
- hebrew_font: hebrew_font,
49
- english_font: english_font)
50
- formatted_text_box(fragments, box_opts)
59
+ english_font: DEFAULT_ENGLISH_FONT,
60
+ direction: :auto, **box_opts)
61
+
62
+ # Handle font specification in box_opts or use defaults
63
+ final_hebrew_font = box_opts.delete(:hebrew_font) || hebrew_font
64
+ final_english_font = box_opts.delete(:english_font) || english_font
65
+ final_size = box_opts.delete(:size) || size
66
+ rotation = box_opts.delete(:rotate) || 0
67
+ char_spacing = box_opts.delete(:character_spacing) || 0
68
+ leading = box_opts.delete(:leading) || 0
69
+
70
+ # Check if text contains Hebrew characters
71
+ contains_hebrew = text.to_s =~ /\p{Hebrew}/
72
+
73
+ # If direction is auto, determine based on content
74
+ if direction == :auto
75
+ direction = contains_hebrew ? :rtl : :ltr
76
+ end
77
+
78
+ # For completely English text (no Hebrew characters and LTR direction),
79
+ # use standard Prawn text_box for better performance and compatibility
80
+ if !contains_hebrew && direction == :ltr
81
+ render_english_only_text(text, final_size, style, final_english_font,
82
+ rotation, char_spacing, leading, box_opts)
83
+ else
84
+ # For Hebrew text or RTL direction, use formatted text approach
85
+ if rotation != 0
86
+ rotate(rotation, origin: box_opts[:at] || [0, 0]) do
87
+ render_hebrew_text_content(text, contains_hebrew, direction, final_size, style,
88
+ final_hebrew_font, final_english_font, char_spacing, leading, box_opts)
89
+ end
90
+ else
91
+ render_hebrew_text_content(text, contains_hebrew, direction, final_size, style,
92
+ final_hebrew_font, final_english_font, char_spacing, leading, box_opts)
93
+ end
94
+ end
95
+ end
96
+
97
+ private
98
+
99
+ # Render pure English text using standard Prawn text_box for optimal performance
100
+ def render_english_only_text(text, final_size, style, final_english_font,
101
+ rotation, char_spacing, leading, box_opts)
102
+ font(final_english_font) do
103
+ text_box(text.to_s, { size: final_size, style: style }.merge(box_opts))
104
+ end
105
+ end
106
+
107
+ # Render Hebrew text or mixed text using formatted text approach
108
+ def render_hebrew_text_content(text, contains_hebrew, direction, final_size, style,
109
+ final_hebrew_font, final_english_font, char_spacing, leading, box_opts)
110
+ # Apply character spacing and leading if specified
111
+ character_spacing(char_spacing) do
112
+ fragments = hebrew_formatted_text(text, size: final_size, style: style,
113
+ hebrew_font: final_hebrew_font,
114
+ english_font: final_english_font)
115
+
116
+ # Add leading to box_opts if specified
117
+ box_opts[:leading] = leading if leading > 0
118
+
119
+ formatted_text_box(fragments, box_opts)
120
+ end
51
121
  end
52
122
  end
53
123
  end
data/lib/version.rb CHANGED
@@ -1,3 +1,3 @@
1
- module PrawnHebrew
2
- VERSION = "0.0.1"
1
+ module PrawnHebrew
2
+ VERSION = "0.0.8"
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.0.6
4
+ version: 0.0.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ben Lite
@@ -10,7 +10,8 @@ cert_chain: []
10
10
  date: 1980-01-02 00:00:00.000000000 Z
11
11
  dependencies: []
12
12
  description: Working with hebrew words in prawn
13
- email: benlite96@gmail.com
13
+ email:
14
+ - benlite96@gmail.com
14
15
  executables: []
15
16
  extensions: []
16
17
  extra_rdoc_files: []