thumb_gen 0.2.0 → 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: c0664e5568eaee147154ba653f077ce91355e19f6bb065ee0881180a02c0f76c
4
- data.tar.gz: e5b39c3c08552118d2ca6fffd88f9b52ca7a5653ab10bd757ae2a9e9abd32567
3
+ metadata.gz: 171cd85f8638b80c939cf1f4623ee4bfa578e2cd9adcd32828b6257f01bf0e11
4
+ data.tar.gz: 489dd00512f4a53dfe9d1b1050a51ecde9609ed438ad7e0addceab33105b5894
5
5
  SHA512:
6
- metadata.gz: 892af539b3de549434c96d746b5ffdb8b1eb06941fd88cd3a6b3b19af9f60752fda995b671fba0687c63e2d90ae90b408cf2f2114201926691cd890fe20de256
7
- data.tar.gz: 2e0231126935da9276ac0963ef9f255f7923d981c70680200ed69333b632a1f00c42861dadd29aa94b112227558d8f9f5ba3971ccf36194c26b5ec568b38a9e6
6
+ metadata.gz: 83a20a5809ed73e8a9d26465a3ec959012ab99fe28f08585b1c24bf3b714a65516e43635104463555f6dcc4478b3f66aedd6c9faa319902345cfc2143cc1c22a
7
+ data.tar.gz: 62d8fe868ba9373bd7dbe9ff17c044400d0a009ad0e08fa9871fb3c93850f7bb52fc5ec6cd7718fbbe6bbe04b9dc16c1595dc044b5c2a4e86f69cd91271d8bda
data/README.md CHANGED
@@ -86,9 +86,6 @@ ThumbGen.generate(output_path, background_url, texts, options)
86
86
  > Font files like `Roboto-BoldItalic.ttf` are bundled in the gem’s `fonts/` folder.
87
87
  Use only the filename **without extension** as the `font:` value.
88
88
 
89
- - NotoSansJP-Regular
90
- - NotoSansJP-Bold
91
- - NotoSansJP-Thin
92
89
  - PublicSans-Regular
93
90
  - PublicSans-Bold
94
91
  - PublicSans-BoldItalic
@@ -100,6 +97,18 @@ Use only the filename **without extension** as the `font:` value.
100
97
  - Roboto-Italic
101
98
  - Roboto-Thin
102
99
  - Roboto-ThinItalic
100
+ - For Japanese:
101
+ - NotoSansJP-Regular
102
+ - NotoSansJP-Bold
103
+ - NotoSansJP-Thin
104
+ - For Korean:
105
+ - NotoSansKR-Regular
106
+ - NotoSansKR-Bold
107
+ - NotoSansKR-Thin
108
+ - For Simplified Chinese
109
+ - NotoSansSC-Regular
110
+ - NotoSansSC-Bold
111
+ - NotoSansSC-Thin
103
112
 
104
113
  ---
105
114
 
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
@@ -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
@@ -0,0 +1,54 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'thumb_gen/generator'
4
+
5
+ module ThumbGen
6
+ # rubocop:disable all
7
+ module Test
8
+ def self.generate_sample
9
+ output_path = 'sample_output.jpg'
10
+ background_url = 'sample_input.jpg'
11
+ texts = [
12
+ {
13
+ text: 'ThumbGen is a Ruby gem that simplifies the creation of article thumbnails',
14
+ wrapped_width: 800,
15
+ font: 'PublicSans-Bold',
16
+ font_size: 80,
17
+ color: '#047857',
18
+ outline_color: '#f8fafc',
19
+ outline_width: 1,
20
+ gravity: 'northwest',
21
+ position_x: 40,
22
+ position_y: 120
23
+ },
24
+ {
25
+ text: '5 min read',
26
+ wrapped_width: 800,
27
+ font: 'Roboto-Italic',
28
+ font_size: 48,
29
+ color: '#09090b',
30
+ gravity: 'southwest',
31
+ position_x: 400,
32
+ position_y: 40
33
+ },
34
+ {
35
+ text: 'My Blog',
36
+ wrapped_width: 1280,
37
+ font: 'Roboto-BoldItalic',
38
+ font_size: 64,
39
+ color: '#86198f',
40
+ gravity: 'northeast',
41
+ position_x: 200,
42
+ position_y: 30
43
+ }
44
+ ]
45
+ options = {
46
+ width: 1280,
47
+ height: 720,
48
+ format: 'jpg'
49
+ }
50
+ ThumbGen::Generator.new(output_path, background_url, texts, options).generate
51
+ end
52
+ end
53
+ # rubocop:enable all
54
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ThumbGen
4
- VERSION = '0.2.0'
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.0
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - YutoYasunaga
@@ -41,6 +41,12 @@ files:
41
41
  - fonts/NotoSansJP-Bold.ttf
42
42
  - fonts/NotoSansJP-Regular.ttf
43
43
  - fonts/NotoSansJP-Thin.ttf
44
+ - fonts/NotoSansKR-Bold.ttf
45
+ - fonts/NotoSansKR-Regular.ttf
46
+ - fonts/NotoSansKR-Thin.ttf
47
+ - fonts/NotoSansSC-Bold.ttf
48
+ - fonts/NotoSansSC-Regular.ttf
49
+ - fonts/NotoSansSC-Thin.ttf
44
50
  - fonts/PublicSans-Bold.ttf
45
51
  - fonts/PublicSans-BoldItalic.ttf
46
52
  - fonts/PublicSans-Regular.ttf
@@ -54,6 +60,7 @@ files:
54
60
  - fonts/Roboto-ThinItalic.ttf
55
61
  - lib/thumb_gen.rb
56
62
  - lib/thumb_gen/generator.rb
63
+ - lib/thumb_gen/test.rb
57
64
  - lib/thumb_gen/utils.rb
58
65
  - lib/thumb_gen/version.rb
59
66
  - sample_input.jpg
@@ -80,7 +87,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
80
87
  - !ruby/object:Gem::Version
81
88
  version: '0'
82
89
  requirements: []
83
- rubygems_version: 3.6.7
90
+ rubygems_version: 3.7.0
84
91
  specification_version: 4
85
92
  summary: Auto generate customized thumbnails for articles.
86
93
  test_files: []