djanowski-tti 0.2.0
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/README +2 -0
- data/lib/configurable.rb +34 -0
- data/lib/tti/helper.rb +11 -0
- data/lib/tti.rb +105 -0
- data/rails/init.rb +8 -0
- metadata +63 -0
data/README
ADDED
data/lib/configurable.rb
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
module Configurable
|
2
|
+
def self.included(base)
|
3
|
+
base.cattr_accessor :configuration
|
4
|
+
|
5
|
+
base.instance_eval(<<-EOS)
|
6
|
+
def self.configure
|
7
|
+
self.configuration ||= Configuration.new(self)
|
8
|
+
yield(configuration)
|
9
|
+
end
|
10
|
+
EOS
|
11
|
+
end
|
12
|
+
|
13
|
+
class Configuration
|
14
|
+
def initialize(configurable)
|
15
|
+
@configurable = configurable
|
16
|
+
@config = {}
|
17
|
+
end
|
18
|
+
|
19
|
+
def method_missing(method_id, *args, &block)
|
20
|
+
is_assignment = method_id.to_s[-1, 1] == '='
|
21
|
+
key = is_assignment ? method_id.to_s[0..-2].to_sym : method_id
|
22
|
+
|
23
|
+
if is_assignment
|
24
|
+
@config[key] = args.first
|
25
|
+
else
|
26
|
+
@config[key]
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def config
|
32
|
+
self.class.configuration ||= Configuration.new(self)
|
33
|
+
end
|
34
|
+
end
|
data/lib/tti/helper.rb
ADDED
data/lib/tti.rb
ADDED
@@ -0,0 +1,105 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rmagick'
|
3
|
+
require 'fileutils'
|
4
|
+
require 'uri'
|
5
|
+
require 'cgi'
|
6
|
+
require 'active_support'
|
7
|
+
require File.dirname(__FILE__) + '/configurable'
|
8
|
+
|
9
|
+
class Tti
|
10
|
+
include Configurable
|
11
|
+
|
12
|
+
attr_accessor :text, :width, :font_size
|
13
|
+
|
14
|
+
def initialize(text)
|
15
|
+
@text = text
|
16
|
+
|
17
|
+
yield(self) if block_given?
|
18
|
+
end
|
19
|
+
|
20
|
+
def text
|
21
|
+
@text.to_s
|
22
|
+
end
|
23
|
+
|
24
|
+
def font_size
|
25
|
+
@font_size ||= config.font_size
|
26
|
+
end
|
27
|
+
|
28
|
+
def save
|
29
|
+
return if text.blank?
|
30
|
+
|
31
|
+
width = width_or_computed
|
32
|
+
font, size = self.font_file, self.font_size
|
33
|
+
|
34
|
+
Magick::Image.read("caption:#{text}") do |i|
|
35
|
+
i.size = width
|
36
|
+
i.font = font
|
37
|
+
i.pointsize = size
|
38
|
+
i.antialias = true
|
39
|
+
end.first.write(path)
|
40
|
+
|
41
|
+
self
|
42
|
+
end
|
43
|
+
|
44
|
+
def filename
|
45
|
+
opts = []
|
46
|
+
opts << font unless font == 'Helvetica'
|
47
|
+
opts << "#{font_size}pt" unless font_size == config.font_size
|
48
|
+
opts << width if width_set?
|
49
|
+
|
50
|
+
"#{text}%s.png" % (!opts.empty? ? "@#{opts.join('__')}" : '')
|
51
|
+
end
|
52
|
+
|
53
|
+
def path
|
54
|
+
FileUtils.mkdir_p(config.path_prefix) if config.path_prefix
|
55
|
+
File.join(*[config.path_prefix, filename].compact)
|
56
|
+
end
|
57
|
+
|
58
|
+
def url
|
59
|
+
CGI.escape [config.url_prefix || config.path_prefix, filename].join('/')
|
60
|
+
end
|
61
|
+
|
62
|
+
def width_or_computed
|
63
|
+
width_set? ? width : computed_width
|
64
|
+
end
|
65
|
+
|
66
|
+
def width_set?
|
67
|
+
!(width.nil? || width.zero?)
|
68
|
+
end
|
69
|
+
|
70
|
+
def computed_width
|
71
|
+
@computed_width ||= begin
|
72
|
+
canvas = Magick::Image.new(1,1)
|
73
|
+
|
74
|
+
font, size = self.font_file, self.font_size
|
75
|
+
|
76
|
+
metrics = Magick::Draw.new.annotate(canvas, 0, 0, 0, 0, text) do |i|
|
77
|
+
i.pointsize = size
|
78
|
+
i.font = font
|
79
|
+
# i.gravity = Magick::CenterGravity
|
80
|
+
end.get_type_metrics(canvas, text)
|
81
|
+
|
82
|
+
metrics.width.ceil
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
def font
|
87
|
+
@font ||= config.font || 'Helvetica'
|
88
|
+
end
|
89
|
+
|
90
|
+
def to_html
|
91
|
+
save
|
92
|
+
%{<img src="#{url}" alt="#{text}" />}
|
93
|
+
end
|
94
|
+
|
95
|
+
def font_file
|
96
|
+
file = Dir["#{font}.*", "fonts/#{font}.*"].first
|
97
|
+
|
98
|
+
file ? File.join(Dir.pwd, file) : font
|
99
|
+
end
|
100
|
+
|
101
|
+
configure do |config|
|
102
|
+
config.path_prefix = 'tmp'
|
103
|
+
config.font_size = 24
|
104
|
+
end
|
105
|
+
end
|
data/rails/init.rb
ADDED
metadata
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: djanowski-tti
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.2.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Damian Janowski
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2008-06-25 00:00:00 -07:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: ""
|
17
|
+
email: damian.janowski@gmail.com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files:
|
23
|
+
- README
|
24
|
+
files:
|
25
|
+
- lib/configurable.rb
|
26
|
+
- lib/tti/helper.rb
|
27
|
+
- lib/tti.rb
|
28
|
+
- rails/init.rb
|
29
|
+
- README
|
30
|
+
has_rdoc: true
|
31
|
+
homepage:
|
32
|
+
post_install_message:
|
33
|
+
rdoc_options:
|
34
|
+
- --line-numbers
|
35
|
+
- --inline-source
|
36
|
+
- --title
|
37
|
+
- tti
|
38
|
+
- --main
|
39
|
+
- README
|
40
|
+
require_paths:
|
41
|
+
- lib
|
42
|
+
- lib/tti
|
43
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: "0"
|
48
|
+
version:
|
49
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - ">="
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: "0"
|
54
|
+
version:
|
55
|
+
requirements: []
|
56
|
+
|
57
|
+
rubyforge_project:
|
58
|
+
rubygems_version: 1.2.0
|
59
|
+
signing_key:
|
60
|
+
specification_version: 2
|
61
|
+
summary: A Ruby text-to-image generation class.
|
62
|
+
test_files: []
|
63
|
+
|