thumb_gen 0.2.1 → 0.2.2

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: 171cd85f8638b80c939cf1f4623ee4bfa578e2cd9adcd32828b6257f01bf0e11
4
+ data.tar.gz: 489dd00512f4a53dfe9d1b1050a51ecde9609ed438ad7e0addceab33105b5894
5
5
  SHA512:
6
- metadata.gz: b72ee8d31e58373006a83097ae077a59af16bf5bd50740fefa353791768f1beef4f927b1ba2fe0ace9e85daa6f0e6b71c687e117c8c872a612cd7292665f0dac
7
- data.tar.gz: '0814c2366fdba305f9bb14c66019baeca5e40bd7950990526525be48b0b744887c17645eddd19e612aeddb0d2ffc6485b5560394e13d8214ba3e08a4db21a358'
6
+ metadata.gz: 83a20a5809ed73e8a9d26465a3ec959012ab99fe28f08585b1c24bf3b714a65516e43635104463555f6dcc4478b3f66aedd6c9faa319902345cfc2143cc1c22a
7
+ data.tar.gz: 62d8fe868ba9373bd7dbe9ff17c044400d0a009ad0e08fa9871fb3c93850f7bb52fc5ec6cd7718fbbe6bbe04b9dc16c1595dc044b5c2a4e86f69cd91271d8bda
@@ -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,80 @@ 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
+ words = text.split(/\s+/)
85
+ lines = []
86
+ line = ''
87
+ draw = Magick::Draw.new
88
+ draw.font = opts[:font]
89
+ draw.pointsize = opts[:font_size]
90
+ draw.font_weight = opts[:font_weight]
91
+ draw.font_style = opts[:font_style]
92
+
93
+ words.each do |word|
94
+ test_line = line.empty? ? word : "#{line} #{word}"
95
+ width = draw.get_type_metrics(background, test_line).width
96
+ if width <= max_width
97
+ line = test_line
98
+ else
99
+ lines << line unless line.empty?
100
+ line = word
101
+ end
102
+ end
103
+
104
+ lines << line unless line.empty?
105
+ lines.join("\n")
106
+ end
107
+
45
108
  def text_options(text)
46
- font_family = text[:font] || 'PUblisSans-Regular'
109
+ font_family = text[:font] || 'PublicSans-Regular'
47
110
  {
48
111
  wrapped_width: wrapped_width(text[:wrapped_width]),
49
112
  font: font(font_family),
@@ -59,38 +122,27 @@ module ThumbGen
59
122
  }
60
123
  end
61
124
 
62
- # Determines the width within which text should be wrapped.
63
125
  def wrapped_width(width)
64
126
  width || background.columns
65
127
  end
66
128
 
67
- # Determines the font based on the style.
68
129
  def font(font_family)
69
130
  base = File.expand_path('../../fonts', __dir__)
70
131
  File.join(base, "#{font_family}.ttf")
71
132
  end
72
133
 
73
- # Determines the font weight based on the name.
74
134
  def font_weight(font_family)
75
- if font_family.downcase.include?('bold')
76
- Magick::BolderWeight
77
- else
78
- Magick::NormalWeight
79
- end
135
+ font_family.downcase.include?('bold') ? Magick::BolderWeight : Magick::NormalWeight
80
136
  end
81
137
 
82
- # Determines the font style based on the name.
83
138
  def font_style(font_family)
84
- if font_family.downcase.include?('italic')
85
- Magick::ItalicStyle
86
- else
87
- Magick::NormalStyle
88
- end
139
+ font_family.downcase.include?('italic') ? Magick::ItalicStyle : Magick::NormalStyle
89
140
  end
90
141
 
91
- # Converts a string description to a Magick gravity constant using a hash map.
92
142
  def gravity(str)
93
- gravity_map = {
143
+ return nil if str.to_s == 'auto'
144
+
145
+ {
94
146
  'northwest' => Magick::NorthWestGravity,
95
147
  'north' => Magick::NorthGravity,
96
148
  'northeast' => Magick::NorthEastGravity,
@@ -100,10 +152,7 @@ module ThumbGen
100
152
  'south' => Magick::SouthGravity,
101
153
  'southeast' => Magick::SouthEastGravity,
102
154
  'center' => Magick::CenterGravity
103
- }
104
-
105
- # Return the corresponding gravity value or default to CenterGravity if not found
106
- gravity_map[str] || Magick::CenterGravity
155
+ }[str] || Magick::CenterGravity
107
156
  end
108
157
  end
109
158
  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.2'
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.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - YutoYasunaga