rmagick-tti_converter 0.0.1 → 0.0.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
  SHA1:
3
- metadata.gz: e452b48be147ad3741463f34d920e258a76e5f90
4
- data.tar.gz: 0d4369e91dfc2a57a1c9cfd2249a36e623d4f3eb
3
+ metadata.gz: a64f34712eaf2cc0af56ca2c3bc7547e3a62dcaa
4
+ data.tar.gz: 844de44b7e6a57a6b00edb8c257448cd35db25e5
5
5
  SHA512:
6
- metadata.gz: 5a6e024bba3d89f385274a28e84d20ba68c0e59bb1110832a93aa39c8d3010898df09a59e5e7d6fbb1a30fb5d07fc860da88376d914928b43e52744246d0623d
7
- data.tar.gz: 7193eb0dbc7937dba94e251c9ad934577736f0ec183058dee72e9d2385804d9d0dc0c1ccd2366d3b9aaea4ed6a23632cd692cbbf9e0d4e5a8ff2bb5b8f429b63
6
+ metadata.gz: 6b7a9e0f3fa2f9d25895af6c6d2a9261b17f0355352b39fcbd693e01a00dd1305cf894780d30d6bb27685b14023bffb428c9233bceb9f4b2861a24d41221b85b
7
+ data.tar.gz: ec3693efe4dba6290e6ce6c57f4f384774c3c144db174005c69fa008935e12a5e1301a2fb4917f4f3ea343bc6e9372a4cf097abec3815497726e4b46eb86918a
data/README.md CHANGED
@@ -1,5 +1,21 @@
1
1
  ## Text to image Converter
2
+ [![Gem Version](https://badge.fury.io/rb/rmagick-tti_converter.svg)](http://badge.fury.io/rb/rmagick-tti_converter)
2
3
 
4
+ ## Installation
5
+
6
+ Add this line to your application's Gemfile:
7
+
8
+ ```ruby
9
+ gem 'rmagick-tti_converter', '~> 0.0.1'
10
+ ```
11
+
12
+ And then execute:
13
+
14
+ $ bundle
15
+
16
+ Or install it yourself as:
17
+
18
+ $ gem install rmagick-tti_converter
3
19
 
4
20
  ## Usage
5
21
 
@@ -24,4 +40,4 @@ text_wrapper.make_image(source_text, file_name: "func_descr.jpg")
24
40
 
25
41
  ## License
26
42
 
27
- It is free software, and may be redistributed under the terms specified in the MIT-LICENSE file.
43
+ It is free software, and may be redistributed under the terms specified in the LICENSE file.
@@ -1,43 +1,63 @@
1
- require 'RMagick'
1
+ require 'rmagick'
2
2
  require 'rmagick/tti_converter/version'
3
+ require 'fileutils'
3
4
 
4
5
  module Magick
5
6
  class TTIConverter
7
+
8
+ # Creates a TTIConverter object. +path+ is the path for
9
+ # images that will generate.
10
+
6
11
  def initialize(path)
7
- @path = path
8
- unless File.exists?(@path)
9
- @path = "./tmp/"
10
- Dir.mkdir("./tmp") unless File.exists?("./tmp")
12
+ if path.match(/\A[.]{0,2}[a-zA-Z0-9\/\-_]+\z/)
13
+ @path = path
14
+ else
15
+ @path = "tmp/"
11
16
  end
17
+ FileUtils.mkdir_p(@path) unless File.exists?(@path)
12
18
  end
13
19
 
20
+ # Generates an image with text contents.
21
+ #
22
+ # +text+ is the string that shoulld be converted to image
23
+ #
24
+ # Options:
25
+ #
26
+ # +file_name+ - The file name that will be assigned for image
27
+ # +optimal_width+ - The width attribute for maximum length of the string into one line.
28
+ # If +optimal_width+ not in between 40 or 160 then it set to 80
29
+
14
30
  def make_image(text, options = {})
15
31
  file_name = options[:file_name] || "image.jpg"
32
+ optimal_width = options[:optimal_width] &&
33
+ options[:optimal_width].between?(40, 160) ?
34
+ options[:optimal_width] : 80
35
+ @line_width = optimal_width
16
36
  image = Magick::Image.new(50,50)
17
37
  draw = Magick::Draw.new
18
- optimal_width = 80
38
+ draw.fill("hsla(90%, 20%, 10%, 1.0)")
19
39
  start = 0
20
40
  finish = optimal_width
21
41
  positions = []
22
42
  edited_text = text.dup
23
43
 
24
- while positions.length < text.length / optimal_width
25
- space_index = get_index_nearest_space(text, {
44
+ while positions.length < text.length.to_f / optimal_width + 1
45
+ number_and_new_line_option = get_index_and_new_line_option_for_nearest_space(text, {
26
46
  start: start,
27
47
  finish: finish
28
48
  })
29
- if space_index != -1
30
- positions << space_index
31
- start = space_index + 1
32
- finish = space_index + optimal_width
49
+ if number_and_new_line_option[0] != -1
50
+ positions << number_and_new_line_option
51
+ start = number_and_new_line_option[0] + 1
52
+ finish = number_and_new_line_option[0] + optimal_width
33
53
  else
34
- positions << finish
54
+ positions << number_and_new_line_option
35
55
  start = start + optimal_width + 1
36
56
  finish = start + optimal_width
37
57
  end
38
58
  end
39
59
 
40
- positions.each { |pos| edited_text[pos] = "\n" }
60
+ positions.each { |pos| edited_text[pos[0]] = "\n" if pos[1] }
41
61
  current_metrics = draw.get_multiline_type_metrics(image, edited_text)
42
62
  width = current_metrics.width
43
63
  height = current_metrics.height
@@ -49,14 +69,15 @@ module Magick
49
69
 
50
70
  private
51
71
 
52
- def get_index_nearest_space(text, options = {})
72
+ def get_index_and_new_line_option_for_nearest_space(text, options = {})
53
73
  start = options[:start] || 0
54
- finish = options[:finish] || (start + 80)
74
+ finish = options[:finish] || (start + @line_width)
55
75
  current_pos = finish
56
76
 
57
77
  loop do
58
- return -1 if current_pos - start < 0
59
- return current_pos if text[current_pos] == " "
78
+ return [-1, true] if current_pos - start < 0
79
+ return [current_pos, true] if text[current_pos] == " "
80
+ return [current_pos, false] if text[current_pos] == "\n"
60
81
  current_pos -= 1
61
82
  end
62
83
  end
@@ -1,5 +1,5 @@
1
1
  module Magick
2
2
  class TTIConverter
3
- VERSION = '0.0.1'
3
+ VERSION = '0.0.2'
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rmagick-tti_converter
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rushan Alyautdinov
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-07-22 00:00:00.000000000 Z
11
+ date: 2015-11-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rmagick
@@ -16,57 +16,75 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '2.13'
19
+ version: '2.15'
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 2.15.4
20
23
  type: :runtime
21
24
  prerelease: false
22
25
  version_requirements: !ruby/object:Gem::Requirement
23
26
  requirements:
24
27
  - - "~>"
25
28
  - !ruby/object:Gem::Version
26
- version: '2.13'
29
+ version: '2.15'
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: 2.15.4
27
33
  - !ruby/object:Gem::Dependency
28
34
  name: bundler
29
35
  requirement: !ruby/object:Gem::Requirement
30
36
  requirements:
31
37
  - - "~>"
32
38
  - !ruby/object:Gem::Version
33
- version: '1.8'
39
+ version: '1.10'
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: 1.10.6
34
43
  type: :development
35
44
  prerelease: false
36
45
  version_requirements: !ruby/object:Gem::Requirement
37
46
  requirements:
38
47
  - - "~>"
39
48
  - !ruby/object:Gem::Version
40
- version: '1.8'
49
+ version: '1.10'
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: 1.10.6
41
53
  - !ruby/object:Gem::Dependency
42
54
  name: rake
43
55
  requirement: !ruby/object:Gem::Requirement
44
56
  requirements:
45
57
  - - "~>"
46
58
  - !ruby/object:Gem::Version
47
- version: '10.0'
59
+ version: '10.4'
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: 10.4.2
48
63
  type: :development
49
64
  prerelease: false
50
65
  version_requirements: !ruby/object:Gem::Requirement
51
66
  requirements:
52
67
  - - "~>"
53
68
  - !ruby/object:Gem::Version
54
- version: '10.0'
69
+ version: '10.4'
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: 10.4.2
55
73
  - !ruby/object:Gem::Dependency
56
74
  name: rspec
57
75
  requirement: !ruby/object:Gem::Requirement
58
76
  requirements:
59
77
  - - "~>"
60
78
  - !ruby/object:Gem::Version
61
- version: '1.0'
79
+ version: '3.3'
62
80
  type: :development
63
81
  prerelease: false
64
82
  version_requirements: !ruby/object:Gem::Requirement
65
83
  requirements:
66
84
  - - "~>"
67
85
  - !ruby/object:Gem::Version
68
- version: '1.0'
69
- description: Text manipulator to make image with text content
86
+ version: '3.3'
87
+ description: Text manipulator to generate image file with text content
70
88
  email: rushan_oline@mail.ru
71
89
  executables: []
72
90
  extensions: []