jney-labble 0.0.1

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.
File without changes
data/LICENSE ADDED
@@ -0,0 +1,18 @@
1
+ Copyright (c) 2008 Jean-Sébastien Ney
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
4
+ this software and associated documentation files (the "Software"), to deal in
5
+ the Software without restriction, including without limitation the rights to
6
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
7
+ the Software, and to permit persons to whom the Software is furnished to do so,
8
+ subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in all
11
+ copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
15
+ FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
16
+ COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
17
+ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
18
+ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,20 @@
1
+ h1. Labble
2
+
3
+ h2. Summary
4
+
5
+ Labble has the following dependencies:
6
+
7
+ * rmagick >= 2.1.0
8
+ * rspec >= 1.1.4 (for specs only, not runtime)
9
+
10
+ h2. Installation
11
+
12
+ To install the labble gem run the following:
13
+
14
+ sudo gem install jney-labble
15
+
16
+ And to enable the label to image in your project just require @labble@:
17
+
18
+ require 'labble'
19
+
20
+ Labble::Image.new("some text", "background-color" => "#000").generate("/some/path")
@@ -0,0 +1,23 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'rake/rdoctask'
4
+ require 'spec/rake/spectask'
5
+
6
+ desc 'Run the specs'
7
+ Spec::Rake::SpecTask.new(:spec) do |t|
8
+ t.spec_opts = ['--colour --format progress --loadby mtime --reverse']
9
+ t.spec_files = FileList['spec/**/*_spec.rb']
10
+ end
11
+
12
+ desc 'Generate RDoc documentation.'
13
+ Rake::RDocTask.new(:rdoc) do |rdoc|
14
+ rdoc.rdoc_files.include('README.rdoc', 'LICENSE', 'Rakefile').
15
+ include('lib/**/*.rb')
16
+
17
+ rdoc.main = "README.rdoc"
18
+ rdoc.title = "labble documentation"
19
+
20
+ rdoc.rdoc_dir = 'doc' # rdoc output folder
21
+ rdoc.options << '--inline-source' << '--charset=UTF-8'
22
+ rdoc.options << '--webcvs=http://github.com/jney/labble/tree/master/'
23
+ end
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require 'labble'
@@ -0,0 +1,10 @@
1
+ begin
2
+ require 'rvg/rvg'
3
+ # include Magick
4
+ rescue LoadError
5
+ raise RequiredLibraryNotFoundError.new('Magick could not be loaded')
6
+ end
7
+ require "labble/defaults"
8
+ require "labble/errors"
9
+ require "labble/validations"
10
+ require "labble/image"
@@ -0,0 +1,12 @@
1
+ module Labble
2
+ module Defaults
3
+ ALLOWED_EXTENSIONS = %w(gif jpeg jpg png)
4
+ ALLOWED_STYLE_FIELDS = %w(background-color color font-family font-size height width).sort
5
+ DEFAULT_STYLE_VALUES = {
6
+ "background-color" => "transparent",
7
+ "color" => "#000000",
8
+ "font-family" => "Arial",
9
+ "font-size" => "12px"
10
+ }
11
+ end
12
+ end
@@ -0,0 +1,7 @@
1
+ module Labble
2
+ module Errors
3
+ class UnsupportedExtensionError < StandardError; end
4
+ class UnsupportedDimensionError < StandardError; end
5
+ end
6
+ end
7
+
@@ -0,0 +1,77 @@
1
+ module Labble
2
+ class Image
3
+ include Labble::Defaults
4
+ include Labble::Errors
5
+ include Labble::Validations
6
+
7
+ attr_accessor :text
8
+ attr_accessor :style
9
+
10
+ def initialize(text, style={})
11
+ @text = text
12
+ @style = DEFAULT_STYLE_VALUES.merge(args_to_string(style))
13
+ end
14
+
15
+ def generate(path=nil)
16
+ _self_ = self
17
+ # check extension
18
+ if path && !valid_extension?(path)
19
+ raise UnsupportedExtensionError
20
+ end
21
+ # first generate the image in a temp directory
22
+ image = Magick::Image.read("label:#{@text.to_s}") do |img|
23
+ _self_.rmagick_compatible_style.each do |key, value|
24
+ # if key must have a float value
25
+ if %w(pointsize).include?(key)
26
+ eval("img.#{key} = #{value}")
27
+ else
28
+ eval("img.#{key} = '#{value}'")
29
+ end
30
+ end
31
+ end.first
32
+
33
+ if path
34
+ Thread.abort_on_exception = true
35
+ Thread.new { image.write(path) }
36
+ end
37
+
38
+ return image
39
+ end
40
+
41
+ def rmagick_compatible_style
42
+ {
43
+ "antialias" => true,
44
+ "background_color" => @style["background-color"],
45
+ "fill" => @style["color"],
46
+ "font" => @style["font-family"],
47
+ "pointsize" => valid_size?(@style["font-size"]),
48
+ "size" => style_size
49
+ }.delete_if {|key, value| value.nil? || value == "" }
50
+ end
51
+
52
+ def style_height
53
+ if(!@style["height"].nil?)
54
+ valid_size?(@style["height"]) ||
55
+ raise(UnsupportedDimensionError, "Height style must be set in pixel. ex: height:12px") && nil
56
+ end
57
+ end
58
+
59
+ def style_size
60
+ if style_height || style_width
61
+ "#{style_width.to_s}x#{style_height.to_s}"
62
+ end
63
+ end
64
+
65
+ def style_width
66
+ if(!@style["width"].nil?)
67
+ valid_size?(@style["width"]) ||
68
+ raise(UnsupportedDimensionError, "Width style must be set in pixel. ex: width:12px") && nil
69
+ end
70
+ end
71
+
72
+ private
73
+ def args_to_string(args)
74
+ return args.inject({}){ |h,(k,v)| h[k.to_s]=v; h }
75
+ end
76
+ end
77
+ end
@@ -0,0 +1,20 @@
1
+ module Labble
2
+ module Validations
3
+ #
4
+ def valid_color?(str)
5
+ str.match(/\A(transparent|\#[a-fA-F\d]{3}|\#[a-fA-F\d]{6})\z/)[1] rescue false
6
+ end
7
+
8
+ #
9
+ def valid_extension?(str)
10
+ Labble::Defaults::ALLOWED_EXTENSIONS.include?(str.split('.').last.downcase)
11
+ end
12
+
13
+ # check if sizes are formated with pixels
14
+ # valid_size?("12px") #=> true
15
+ # valid_size?("12em") #=> false
16
+ def valid_size?(str)
17
+ str.match(/\A([0-9]*)px\z/)[1] rescue false
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,9 @@
1
+ module Labble
2
+ module VERSION #:nodoc:
3
+ MAJOR = 0
4
+ MINOR = 0
5
+ TINY = 1
6
+
7
+ STRING = [MAJOR, MINOR, TINY].join('.')
8
+ end
9
+ end
@@ -0,0 +1,51 @@
1
+ require File.join(File.dirname(__FILE__), *%w[spec_helper])
2
+
3
+ describe "Image" do
4
+
5
+ before(:each) do
6
+ @image = Labble::Image.new('test')
7
+ end
8
+
9
+ describe "Initialize" do
10
+ it "should initialize text" do
11
+ image = Labble::Image.new("test")
12
+ image.text.should == "test"
13
+ end
14
+
15
+ it "should initialize default style" do
16
+ image = Labble::Image.new("test")
17
+ image.style.should == {
18
+ "background-color" => "transparent",
19
+ "color" => "#000000",
20
+ "font-family" => "Arial",
21
+ "font-size" => "12px"
22
+ }
23
+ end
24
+
25
+ it "should initialize style" do
26
+ image = Labble::Image.new("test", "background-color" => "#333", :color => "#999")
27
+ image.style.should == {
28
+ "background-color" => "#333",
29
+ "color" => "#999",
30
+ "font-family" => "Arial",
31
+ "font-size" => "12px"
32
+ }
33
+ end
34
+ end
35
+
36
+ describe "Generate an image" do
37
+ it "should initialize a default format" do
38
+ image = Labble::Image.new('test').generate(File.join(File.dirname(__FILE__), *%w[.. temp test.png]))
39
+ end
40
+
41
+ # it "should initialize a format" do
42
+ # image = Labble::Image.new(:format => 'jpeg')
43
+ # image.format.should == "jpeg"
44
+ # end
45
+ #
46
+ # it "should raise an error when form does not exists" do
47
+ # lambda {Labble::Image.new(:format => 'test')}.should raise_error
48
+ # end
49
+ end
50
+
51
+ end
@@ -0,0 +1,8 @@
1
+ require 'rubygems'
2
+ require 'spec'
3
+
4
+ Spec::Runner.configure do |config|
5
+ end
6
+
7
+ $:.unshift "#{File.dirname(__FILE__)}/../lib"
8
+ require 'labble'
metadata ADDED
@@ -0,0 +1,82 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jney-labble
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - "Jean-S\xC3\xA9bastien Ney"
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-12-01 00:00:00 -08:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rmagick
17
+ version_requirement:
18
+ version_requirements: !ruby/object:Gem::Requirement
19
+ requirements:
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 2.7.2
23
+ version:
24
+ description: Generate an image from a text and css like params.
25
+ email: jeansebastien.ney@gmail.com
26
+ executables: []
27
+
28
+ extensions: []
29
+
30
+ extra_rdoc_files:
31
+ - README.textile
32
+ - Rakefile
33
+ - LICENSE
34
+ - CHANGELOG
35
+ files:
36
+ - README.textile
37
+ - Rakefile
38
+ - LICENSE
39
+ - CHANGELOG
40
+ - init.rb
41
+ - lib
42
+ - lib/labble
43
+ - lib/labble.rb
44
+ - lib/labble/defaults.rb
45
+ - lib/labble/errors.rb
46
+ - lib/labble/image.rb
47
+ - lib/labble/validations.rb
48
+ - lib/labble/version.rb
49
+ - spec
50
+ - spec/image_spec.rb
51
+ - spec/spec_helper.rb
52
+ has_rdoc: true
53
+ homepage: http://github.com/jney/labble
54
+ post_install_message:
55
+ rdoc_options:
56
+ - --main
57
+ - README.textile
58
+ - --inline-source
59
+ - --charset=UTF-8
60
+ require_paths:
61
+ - lib
62
+ required_ruby_version: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ version: "0"
67
+ version:
68
+ required_rubygems_version: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: "0"
73
+ version:
74
+ requirements: []
75
+
76
+ rubyforge_project:
77
+ rubygems_version: 1.2.0
78
+ signing_key:
79
+ specification_version: 2
80
+ summary: Generate an image from a text and css like params
81
+ test_files: []
82
+