thumb_gen 0.2.1 → 0.2.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c3afc45cc8b694142b79f2cb82e3c03ec4e871894c218bede07a9c28f53fba77
4
- data.tar.gz: 405767838d151c46427641ff2a5fa4264fa21ceec2cdf48327c2290fe9f7e24c
3
+ metadata.gz: 96583f8cea87b49a125c5f5d6499b9b9c1f17b4cf0876e1e69c83f4d3d842f6b
4
+ data.tar.gz: 19f73d44d5d410f5cf63c742962e9bc66231e82b56df70115bae652526c87c79
5
5
  SHA512:
6
- metadata.gz: b72ee8d31e58373006a83097ae077a59af16bf5bd50740fefa353791768f1beef4f927b1ba2fe0ace9e85daa6f0e6b71c687e117c8c872a612cd7292665f0dac
7
- data.tar.gz: '0814c2366fdba305f9bb14c66019baeca5e40bd7950990526525be48b0b744887c17645eddd19e612aeddb0d2ffc6485b5560394e13d8214ba3e08a4db21a358'
6
+ metadata.gz: b694d60b9ebb8746f68f1e5a3604027a7b6748d7b5e758268b978f844eadfecd4b355a6625cf46c617bb1d1b4efbe325a8a18f9fef6061d700d7a9655ccd8461
7
+ data.tar.gz: 85f0e68efa2e949980550f5fa47b4605bc1a4070e06925cb680d11580d2b20e69fb68326b68a21c25414578f30ed577450847c619c58f63cfffaea576f0c686a
@@ -22,12 +22,10 @@ module ThumbGen
22
22
 
23
23
  private
24
24
 
25
- # Retrieves and caches the background image.
26
25
  def background
27
26
  @background ||= Magick::Image.read(background_url).first
28
27
  end
29
28
 
30
- # Handles the resizing, formatting, and text addition for the image.
31
29
  def generate_image
32
30
  background.resize_to_fill!(options[:width], options[:height])
33
31
  background.format = options[:format]
@@ -35,15 +33,103 @@ module ThumbGen
35
33
  background.write(output_path)
36
34
  end
37
35
 
38
- # Adds text overlays to the image based on provided text configurations.
39
36
  def add_texts
40
- texts.each do |text|
37
+ auto_texts, normal_texts = texts.partition { |t| t[:gravity].to_s == 'auto' }
38
+
39
+ draw_auto_centered_texts(auto_texts) unless auto_texts.empty?
40
+
41
+ normal_texts.each do |text|
41
42
  draw_text(background, text[:text], **text_options(text))
42
43
  end
43
44
  end
44
45
 
46
+ def draw_auto_centered_texts(text_items)
47
+ metrics_list = []
48
+
49
+ text_items.each do |text|
50
+ opts = text_options(text)
51
+ draw = Magick::Draw.new
52
+ draw.font = opts[:font]
53
+ draw.pointsize = opts[:font_size]
54
+ draw.font_weight = opts[:font_weight]
55
+ draw.font_style = opts[:font_style]
56
+
57
+ wrapped_text = wrap_text(text[:text], opts[:wrapped_width] || background.columns, opts)
58
+ lines = wrapped_text.split("\n")
59
+ line_metrics = lines.map { |line| draw.get_type_metrics(background, line) }
60
+ metrics_list << [lines, opts, line_metrics]
61
+ end
62
+
63
+ total_height = metrics_list.sum { |_, _, ms| ms.sum(&:height) }
64
+ y_start = (background.rows - total_height) / 2.0
65
+
66
+ y = y_start
67
+ metrics_list.each do |lines, opts, metrics|
68
+ lines.zip(metrics).each do |line, metric|
69
+ draw = Magick::Draw.new
70
+ draw.font = opts[:font]
71
+ draw.pointsize = opts[:font_size]
72
+ draw.fill = opts[:color]
73
+ draw.font_weight = opts[:font_weight]
74
+ draw.font_style = opts[:font_style]
75
+
76
+ x = (background.columns - metric.width) / 2.0
77
+ draw.annotate(background, 0, 0, x, y + metric.ascent, line)
78
+ y += metric.height
79
+ end
80
+ end
81
+ end
82
+
83
+ def wrap_text(text, max_width, opts)
84
+ draw = Magick::Draw.new
85
+ draw.font = opts[:font]
86
+ draw.pointsize = opts[:font_size]
87
+ draw.font_weight = opts[:font_weight]
88
+ draw.font_style = opts[:font_style]
89
+
90
+ if text.include?(' ')
91
+ # 📝 For languages with spaces (e.g. English) → wrap by words
92
+ words = text.split(/\s+/)
93
+ lines = []
94
+ line = ''
95
+
96
+ words.each do |word|
97
+ test_line = line.empty? ? word : "#{line} #{word}"
98
+ width = draw.get_type_metrics(background, test_line).width
99
+ if width <= max_width
100
+ line = test_line
101
+ else
102
+ lines << line unless line.empty?
103
+ line = word
104
+ end
105
+ end
106
+
107
+ lines << line unless line.empty?
108
+ lines.join("\n")
109
+ else
110
+ # 🇯🇵 For languages without spaces (e.g. Japanese, Chinese) → wrap by character
111
+ chars = text.scan(/.{1}/m)
112
+ lines = []
113
+ line = ''
114
+
115
+ chars.each do |char|
116
+ test_line = line + char
117
+ width = draw.get_type_metrics(background, test_line).width
118
+ if width <= max_width
119
+ line = test_line
120
+ else
121
+ lines << line unless line.empty?
122
+ line = char
123
+ end
124
+ end
125
+
126
+ lines << line unless line.empty?
127
+ lines.join("\n")
128
+ end
129
+ end
130
+
45
131
  def text_options(text)
46
- font_family = text[:font] || 'PUblisSans-Regular'
132
+ font_family = text[:font] || 'PublicSans-Regular'
47
133
  {
48
134
  wrapped_width: wrapped_width(text[:wrapped_width]),
49
135
  font: font(font_family),
@@ -59,38 +145,27 @@ module ThumbGen
59
145
  }
60
146
  end
61
147
 
62
- # Determines the width within which text should be wrapped.
63
148
  def wrapped_width(width)
64
149
  width || background.columns
65
150
  end
66
151
 
67
- # Determines the font based on the style.
68
152
  def font(font_family)
69
153
  base = File.expand_path('../../fonts', __dir__)
70
154
  File.join(base, "#{font_family}.ttf")
71
155
  end
72
156
 
73
- # Determines the font weight based on the name.
74
157
  def font_weight(font_family)
75
- if font_family.downcase.include?('bold')
76
- Magick::BolderWeight
77
- else
78
- Magick::NormalWeight
79
- end
158
+ font_family.downcase.include?('bold') ? Magick::BolderWeight : Magick::NormalWeight
80
159
  end
81
160
 
82
- # Determines the font style based on the name.
83
161
  def font_style(font_family)
84
- if font_family.downcase.include?('italic')
85
- Magick::ItalicStyle
86
- else
87
- Magick::NormalStyle
88
- end
162
+ font_family.downcase.include?('italic') ? Magick::ItalicStyle : Magick::NormalStyle
89
163
  end
90
164
 
91
- # Converts a string description to a Magick gravity constant using a hash map.
92
165
  def gravity(str)
93
- gravity_map = {
166
+ return nil if str.to_s == 'auto'
167
+
168
+ {
94
169
  'northwest' => Magick::NorthWestGravity,
95
170
  'north' => Magick::NorthGravity,
96
171
  'northeast' => Magick::NorthEastGravity,
@@ -100,10 +175,7 @@ module ThumbGen
100
175
  'south' => Magick::SouthGravity,
101
176
  'southeast' => Magick::SouthEastGravity,
102
177
  'center' => Magick::CenterGravity
103
- }
104
-
105
- # Return the corresponding gravity value or default to CenterGravity if not found
106
- gravity_map[str] || Magick::CenterGravity
178
+ }[str] || Magick::CenterGravity
107
179
  end
108
180
  end
109
181
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ThumbGen
4
- VERSION = '0.2.1'
4
+ VERSION = '0.2.3'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: thumb_gen
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.2.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - YutoYasunaga