Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Alan committed May 5, 2008
0 parents commit f4a61b0
Show file tree
Hide file tree
Showing 9 changed files with 195 additions and 0 deletions.
20 changes: 20 additions & 0 deletions MIT-LICENSE
@@ -0,0 +1,20 @@
Copyright (c) 2008 Norbauer Inc.

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
13 changes: 13 additions & 0 deletions README
@@ -0,0 +1,13 @@
Letterpress
==========

Introduction goes here.


Example
=======

Example goes here.


Copyright (c) 2008 Norbauer Inc, released under the MIT license
22 changes: 22 additions & 0 deletions Rakefile
@@ -0,0 +1,22 @@
require 'rake'
require 'rake/testtask'
require 'rake/rdoctask'

desc 'Default: run unit tests.'
task :default => :test

desc 'Test the letterpress plugin.'
Rake::TestTask.new(:test) do |t|
t.libs << 'lib'
t.pattern = 'test/**/*_test.rb'
t.verbose = true
end

desc 'Generate documentation for the letterpress plugin.'
Rake::RDocTask.new(:rdoc) do |rdoc|
rdoc.rdoc_dir = 'rdoc'
rdoc.title = 'Letterpress'
rdoc.options << '--line-numbers' << '--inline-source'
rdoc.rdoc_files.include('README')
rdoc.rdoc_files.include('lib/**/*.rb')
end
3 changes: 3 additions & 0 deletions init.rb
@@ -0,0 +1,3 @@
config.after_initialize do
ActionController::Base.helper(Letterpress)
end
1 change: 1 addition & 0 deletions install.rb
@@ -0,0 +1 @@
FileUtils.mkpath(Letterpress::Config.fonts_dir)
65 changes: 65 additions & 0 deletions lib/image_magick_text.rb
@@ -0,0 +1,65 @@
require 'digest/md5'
require 'pathname'

class ImageMagickText
@@attributes = :body, :background_color, :font, :fill, :size, :format
attr_accessor *@@attributes

def initialize(body)
@body = body
end

def to_s
@body.to_s
end

def render(output_dir)
if @body.nil?
raise RuntimeError.new("Text body must be set before rendering the image")
elsif @format.nil?
raise RuntimeError.new("Image format must be set before rendering the image")
end

file_name = generate_filename
image_path = Pathname.new(File.join(Rails.root, 'public', 'images', output_dir, file_name))

write(image_path) unless image_path.exist?

return File.join(output_dir, file_name)
end

# ------------------------------ private ------------------------------
private

def write(image)
unless image.parent.exist?
image.parent.mkpath # like mkdir, but creates intermediate directories
end

command = 'convert '
command += %Q( -background "#{@background_color}") unless @background_color.nil?
command += %Q( -font "#{@font}") unless @font.nil?
command += %Q( -fill "#{@fill}") unless @fill.nil?
command += %Q( -pointsize #{@size}) unless @size.nil?

command += %Q( label:"#{@body}")
command += %Q( "#{image}")

Rails.logger.debug("Calling ImageMagick with command: #{command}")
Kernel.system(command)
end

# Generate a unique filename for this set of text and options. There
# needs to be a different filename for the same set of text but different
# options, because we will be generating a different file if even just
# the colors are different.
def generate_filename
string = ''
for attribute in @@attributes
attr_s = attribute.to_s
string += attr_s + instance_variable_get("@#{attr_s}").to_s
end

Digest::MD5.hexdigest(string) + ".#{@format.to_s}"
end
end
55 changes: 55 additions & 0 deletions lib/letterpress.rb
@@ -0,0 +1,55 @@
if Rails.env == 'development'
load 'image_magick_text.rb'
else
require 'image_magick_text'
end

module Letterpress
module Config
# Directory to look for fonts
mattr_accessor :fonts_dir
self.fonts_dir = File.join(Rails.root, 'lib', 'fonts')

# Directory to output the images, relative to the RAILS_ROOT/public/images directory
mattr_accessor :images_dir
self.images_dir = 'letterpress'

# Default file format used to render the image (can be anything ImageMagick supports)
mattr_accessor :image_format
self.image_format = 'png'
end

def letterpress(text, options = {})
options.symbolize_keys!

text = ImageMagickText.new(text)

if font = options.delete(:font)
paths = [ File.join(Config.fonts_dir, font),
File.join(Config.fonts_dir, font + '.ttf') ]
unless path = paths.find { |path| File.exists?(path) }
raise ArgumentError.new("Invalid font specified: #{font}")
end
text.font = path
end

if color = options.delete(:color)
text.fill = color
end

if background_color = options.delete(:background_color)
text.background_color = background_color
end

if size = options.delete(:size)
text.size = size
end

text.format = options.delete(:format) || Config.image_format

file_name = text.render(Config.images_dir)
options = {:alt => text}.merge(options)

image_tag(file_name, options)
end
end
8 changes: 8 additions & 0 deletions tasks/letterpress_tasks.rake
@@ -0,0 +1,8 @@
namespace :letterpress do
namespace :images do
desc "Clears previously generated images"
task :clear => :environment do
FileUtils.rm(Dir["public/images/#{Letterpress::Config.images_dir}/[^.]*"])
end
end
end
8 changes: 8 additions & 0 deletions test/letterpress_test.rb
@@ -0,0 +1,8 @@
require 'test/unit'

class LetterpressTest < Test::Unit::TestCase
# Replace this with your real tests.
def test_this_plugin
flunk
end
end

0 comments on commit f4a61b0

Please sign in to comment.