txt2img 0.0.4 → 0.0.5

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.
data/.gitignore CHANGED
@@ -16,3 +16,4 @@ test/tmp
16
16
  test/version_tmp
17
17
  tmp
18
18
  nbproject/*
19
+ .rvmrc
data/README.md CHANGED
@@ -4,6 +4,10 @@ Generate image from text
4
4
 
5
5
  ## Installation
6
6
 
7
+ Install Imagemagick
8
+
9
+ sudo apt-get install imagemagick
10
+
7
11
  Add this line to your application's Gemfile:
8
12
 
9
13
  gem 'txt2img'
Binary file
@@ -1,16 +1,20 @@
1
+ # encoding: utf-8
2
+
1
3
  require "txt2img/version"
4
+ require "txt2img/string_util"
2
5
  require 'quick_magick'
3
6
 
4
7
  module Txt2img
5
8
  class Txt
9
+ include StringUtil
6
10
 
7
11
  attr_accessor :txt, :font, :width, :pointsize
8
12
 
9
- def initialize(txt, font = nil, width = 20, pointsize = 30)
13
+ def initialize(txt, options = {})
10
14
  @txt = txt
11
- @font = font || File.expand_path("../../fonts/microhei.ttc", __FILE__)
12
- @width = width || 20
13
- @pointsize = pointsize || 30
15
+ @font = options[:font] || File.expand_path("../../fonts/microhei.ttc", __FILE__)
16
+ @width = options[:width] || 20
17
+ @pointsize = options[:pointsize] || 30
14
18
  end
15
19
 
16
20
  def write(path)
@@ -23,8 +27,12 @@ module Txt2img
23
27
  end
24
28
 
25
29
  # TODO optimize
30
+ # TODO NOT break English word
31
+ # TODO tune the margin and text size
26
32
  def wrap!
27
- @txt = @txt.split(/[\r\n]+/).map{|x| x.scan(/.{1,#{width}}/u).join("\n")}.join("\n")
33
+ @txt = @txt.split(/[\r\n]+/).map do |line|
34
+ wrap_by_width(line, width)
35
+ end * "\n"
28
36
  end
29
37
 
30
38
  def height
@@ -0,0 +1,38 @@
1
+ # encoding: utf-8
2
+ require 'unicode/display_width'
3
+
4
+ module Txt2img
5
+ module StringUtil
6
+ def size(string)
7
+ string.display_width
8
+ end
9
+
10
+ def split_by_width(str, width)
11
+ chars = str.chars.to_a
12
+
13
+ current_length = 0
14
+ split_index = 0
15
+
16
+ chars.each_with_index do |c, i|
17
+ char_width = self.size(c)
18
+ break if current_length + char_width > width
19
+ split_index = i + 1
20
+ current_length += char_width
21
+ end
22
+
23
+ [chars[0, split_index].join, chars[split_index..-1].join]
24
+ end
25
+
26
+ def wrap_by_width(str, width)
27
+ lines = []
28
+
29
+ while !str.empty? do
30
+ head, tail = split_by_width(str, width)
31
+ lines << head
32
+ str = tail
33
+ end
34
+
35
+ lines.join("\n")
36
+ end
37
+ end
38
+ end
@@ -1,3 +1,3 @@
1
1
  module Txt2img
2
- VERSION = "0.0.4"
2
+ VERSION = "0.0.5"
3
3
  end
@@ -15,4 +15,5 @@ Gem::Specification.new do |gem|
15
15
  gem.require_paths = ["lib", "fonts"]
16
16
  gem.version = Txt2img::VERSION
17
17
  gem.add_runtime_dependency "quick_magick_hooopo", ["~> 0.8"]
18
+ gem.add_runtime_dependency "unicode-display_width", ["~> 0.1.1"]
18
19
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: txt2img
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-02-18 00:00:00.000000000 Z
12
+ date: 2012-02-20 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: quick_magick_hooopo
16
- requirement: &15219320 !ruby/object:Gem::Requirement
16
+ requirement: &13868660 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,7 +21,18 @@ dependencies:
21
21
  version: '0.8'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *15219320
24
+ version_requirements: *13868660
25
+ - !ruby/object:Gem::Dependency
26
+ name: unicode-display_width
27
+ requirement: &13865060 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ~>
31
+ - !ruby/object:Gem::Version
32
+ version: 0.1.1
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *13865060
25
36
  description: generate image from txt
26
37
  email:
27
38
  - hoooopo@gmail.com
@@ -35,7 +46,9 @@ files:
35
46
  - README.md
36
47
  - Rakefile
37
48
  - fonts/microhei.ttc
49
+ - hello.gif
38
50
  - lib/txt2img.rb
51
+ - lib/txt2img/string_util.rb
39
52
  - lib/txt2img/version.rb
40
53
  - txt2img.gemspec
41
54
  homepage: ''