rmagick-tti_converter 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +17 -1
- data/lib/rmagick/tti_converter.rb +39 -18
- data/lib/rmagick/tti_converter/version.rb +1 -1
- metadata +29 -11
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a64f34712eaf2cc0af56ca2c3bc7547e3a62dcaa
|
4
|
+
data.tar.gz: 844de44b7e6a57a6b00edb8c257448cd35db25e5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
43
|
+
It is free software, and may be redistributed under the terms specified in the LICENSE file.
|
@@ -1,43 +1,63 @@
|
|
1
|
-
require '
|
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
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
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
|
-
|
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
|
-
|
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
|
30
|
-
positions <<
|
31
|
-
start =
|
32
|
-
finish =
|
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 <<
|
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
|
72
|
+
def get_index_and_new_line_option_for_nearest_space(text, options = {})
|
53
73
|
start = options[:start] || 0
|
54
|
-
finish = options[:finish] || (start +
|
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
|
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.
|
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-
|
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.
|
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.
|
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.
|
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.
|
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.
|
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.
|
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: '
|
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: '
|
69
|
-
description: Text manipulator to
|
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: []
|