textorize 0.21

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ Copyright (c) 2009 Thomas Fuchs
2
+ http://script.aculo.us/thomas
3
+
4
+ Permission is hereby granted, free of charge, to any person obtaining
5
+ a copy of this software and associated documentation files (the
6
+ "Software"), to deal in the Software without restriction, including
7
+ without limitation the rights to use, copy, modify, merge, publish,
8
+ distribute, sublicense, and/or sell copies of the Software, and to
9
+ permit persons to whom the Software is furnished to do so, subject to
10
+ the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be
13
+ included in all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,16 @@
1
+ require "rubygems"
2
+ require "rake/testtask"
3
+ require "rake/gempackagetask"
4
+
5
+ task :default => :test
6
+
7
+ Rake::TestTask.new do |t|
8
+ t.libs << "test"
9
+ t.test_files = FileList["test/test_*.rb"]
10
+ t.verbose = true
11
+ end
12
+
13
+ Rake::GemPackageTask.new(eval(IO.read(File.join(File.dirname(__FILE__), "textorize.gemspec")))) do |pkg|
14
+ pkg.need_zip = true
15
+ pkg.need_tar = true
16
+ end
data/bin/textorize ADDED
@@ -0,0 +1,66 @@
1
+ #!/usr/bin/env ruby
2
+ require "optparse"
3
+ require "textorize"
4
+
5
+ options = {}
6
+ opts = OptionParser.new do |opts|
7
+ opts.banner = "Usage: #$0 [options] string"
8
+
9
+ opts.on('-f', '--font=[FONT]', String, 'Font name') do |v|
10
+ options[:font] = v
11
+ end
12
+
13
+ opts.on('-s', '--size=[SIZE]', Float, 'Font size in point') do |v|
14
+ options[:size] = v
15
+ end
16
+
17
+ opts.on('-l', '--lineheight=[HEIGHT]', Float, 'Line height in point') do |v|
18
+ options[:lineheight] = v
19
+ end
20
+
21
+ opts.on('--list-fonts', 'List available fonts') do |v|
22
+ options[:util] = 'list-fonts'
23
+ end
24
+
25
+ opts.on('-k', '--kerning=[VALUE]', Float, 'Kerning adjustment') do |v|
26
+ options[:kerning] = v
27
+ end
28
+
29
+ opts.on('-o', '--output=[FILENAME]', String, 'Specify filename for saving') do |v|
30
+ options[:output] = v
31
+ end
32
+
33
+ opts.on('-b', '--obliqueness=[ANGLE]', Float, 'Slant angle') do |v|
34
+ options[:obliqueness] = v
35
+ end
36
+
37
+ opts.on('-c', '--color=[COLOR]', String, 'Render text in specific color (CSS color value)') do |v|
38
+ options[:color] = v
39
+ end
40
+
41
+ opts.on('-g', '--background=[COLOR]', String, 'Render background in specific color (CSS color value)') do |v|
42
+ options[:background] = v
43
+ end
44
+
45
+ opts.on_tail('-h', '--help', 'Display this message and exit') do
46
+ puts opts
47
+ exit
48
+ end
49
+
50
+ if !ARGV.first
51
+ puts opts
52
+ exit
53
+ end
54
+ end.parse!
55
+
56
+ options[:font] ||= 'Arial'
57
+ options[:size] ||= 28.0
58
+ options[:kerning] ||= 0
59
+ options[:lineheight] ||= options[:size]
60
+ options[:output] ||= 'output.png'
61
+ options[:strings] = ARGV.first
62
+ options[:width] ||= 250
63
+ options[:obliqueness] ||= 0
64
+ options[:util] ||= ''
65
+
66
+ renderer = Textorize::Runner.new(options[:strings], options[:output], options)
@@ -0,0 +1,64 @@
1
+ # this code taken from limechat
2
+ # http://github.com/psychs/limechat
3
+ # LimeChat is copyrighted free software by Satoshi Nakagawa <psychs@limechat.net>.
4
+
5
+ module OSX
6
+ class NSColor
7
+ def self.from_rgb(red, green, blue, alpha=1.0)
8
+ NSColor.colorWithCalibratedRed_green_blue_alpha(red/255.0, green/255.0, blue/255.0, alpha)
9
+ end
10
+
11
+ HTML4_KEYWORDS = {
12
+ # taken from the CSS3 Color Module
13
+ # http://www.w3.org/TR/css3-color/#html4
14
+ # note that we don't support SVG color keywords
15
+ 'black' => from_rgb(0, 0, 0),
16
+ 'silver' => from_rgb(0xC0, 0xC0, 0xC0),
17
+ 'gray' => from_rgb(0x80, 0x80, 0x80),
18
+ 'white' => from_rgb(0xFF, 0xFF, 0xFF),
19
+ 'maroon' => from_rgb(0x80, 0, 0),
20
+ 'red' => from_rgb(0xFF, 0, 0),
21
+ 'purple' => from_rgb(0x80, 0, 0x80),
22
+ 'fuchsia' => from_rgb(0xFF, 0, 0xFF),
23
+ 'green' => from_rgb(0, 0x80, 0),
24
+ 'lime' => from_rgb(0, 0xFF, 0),
25
+ 'olive' => from_rgb(0x80, 0x80, 0),
26
+ 'yellow' => from_rgb(0xFF, 0xFF, 0),
27
+ 'navy' => from_rgb(0, 0, 0x80),
28
+ 'blue' => from_rgb(0, 0, 0xFF),
29
+ 'teal' => from_rgb(0, 0x80, 0x80),
30
+ 'aqua' => from_rgb(0, 0xFF, 0xFF),
31
+ 'transparent' => from_rgb(0, 0, 0, 0)
32
+ }
33
+
34
+ def self.from_css(str)
35
+ case str
36
+ when /^#([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})$/i
37
+ r = $1.to_i(16)
38
+ g = $2.to_i(16)
39
+ b = $3.to_i(16)
40
+ from_rgb(r, g, b)
41
+ when /^#([0-9a-f])([0-9a-f])([0-9a-f])$/i
42
+ r = ($1*2).to_i(16)
43
+ g = ($2*2).to_i(16)
44
+ b = ($3*2).to_i(16)
45
+ from_rgb(r, g, b)
46
+ when /^rgb\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*\)$/
47
+ r = $1.to_i
48
+ g = $2.to_i
49
+ b = $3.to_i
50
+ from_rgb(r, g, b)
51
+ when /^rgba\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d*(?:\.\d+))\s*\)$/
52
+ r = $1.to_i
53
+ g = $2.to_i
54
+ b = $3.to_i
55
+ a = $4.to_f
56
+ from_rgb(r, g, b, a)
57
+ else
58
+ HTML4_KEYWORDS[str]
59
+ end
60
+ rescue
61
+ nil
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,55 @@
1
+ require 'osx/cocoa'
2
+
3
+ module Textorize
4
+ class Renderer
5
+ include OSX
6
+
7
+ def initialize(window, string, options)
8
+ @text_view = NSTextView.alloc.initWithFrame([0,0,0,0])
9
+
10
+ window.opaque = false
11
+ window.backgroundColor = NSColor.from_css('transparent')
12
+
13
+ set_attr_and_text options, string
14
+ window.contentView = @text_view
15
+ @text_view.sizeToFit
16
+
17
+ window.display
18
+ window.orderFrontRegardless
19
+ end
20
+
21
+ def bitmap
22
+ @text_view.lockFocus
23
+ bitmap = NSBitmapImageRep.alloc.initWithFocusedViewRect(@text_view.bounds)
24
+ @text_view.unlockFocus
25
+ bitmap
26
+ end
27
+
28
+ private
29
+
30
+ def set_attr_and_text(options, string)
31
+ @text_view.horizontallyResizable = true
32
+ @text_view.useAllLigatures(nil)
33
+
34
+ para = NSMutableParagraphStyle.alloc.init
35
+ para.lineSpacing = options[:lineheight]
36
+
37
+ @text_view.typingAttributes = {
38
+ NSFontAttributeName => NSFont.fontWithName_size(options[:font], options[:size]),
39
+ NSKernAttributeName => options[:kerning],
40
+ NSParagraphStyleAttributeName => para,
41
+ NSBaselineOffsetAttributeName => 0,
42
+ NSObliquenessAttributeName => options[:obliqueness]
43
+ }
44
+
45
+ @text_view.lowerBaseline(nil)
46
+
47
+ @text_view.string = string
48
+ @text_view.textColor = NSColor.from_css(options[:color] || 'black')
49
+ @text_view.backgroundColor = NSColor.from_css(options[:background] || 'white')
50
+ @text_view.drawsBackground = true
51
+ end
52
+
53
+ end
54
+
55
+ end
@@ -0,0 +1,47 @@
1
+ require 'osx/cocoa'
2
+
3
+ module Textorize
4
+ class Runner
5
+
6
+ def initialize(string, output, options)
7
+ app = OSX::NSApplication.sharedApplication
8
+ app.delegate = RunnerApplication.alloc.initWithString_output_options(string, output, options)
9
+ app.run
10
+ end
11
+
12
+ end
13
+
14
+ class RunnerApplication < OSX::NSObject
15
+ include OSX
16
+
17
+ def initWithString_output_options(string, output, options)
18
+ if init
19
+ @string, @output, @options = string, output, options
20
+ @window = NSWindow.alloc.objc_send(
21
+ :initWithContentRect, [-2000, -2000, 2000, 2000],
22
+ :styleMask, NSBorderlessWindowMask,
23
+ :backing, 2,
24
+ :defer, 0)
25
+
26
+ self
27
+ end
28
+ end
29
+
30
+ def list_fonts
31
+ fonts = NSFontManager.sharedFontManager.availableFontFamilies
32
+ puts fonts.sort.join("\n")
33
+ end
34
+
35
+ def applicationDidFinishLaunching(notification)
36
+ if @options[:util] == 'list-fonts'
37
+ list_fonts
38
+ else
39
+ renderer = Renderer.new(@window, @string, @options)
40
+ Saver.new(renderer).write_to_file(@output)
41
+ end
42
+ NSApplication.sharedApplication.terminate(nil)
43
+ end
44
+
45
+ end
46
+
47
+ end
@@ -0,0 +1,16 @@
1
+ module Textorize
2
+ class Saver
3
+ include OSX
4
+ attr_reader :png
5
+
6
+ def initialize(renderer)
7
+ bitmap = renderer.bitmap
8
+ @png = bitmap.representationUsingType_properties(NSPNGFileType, nil)
9
+ end
10
+
11
+ def write_to_file(file)
12
+ @png.writeToFile_atomically(file, true)
13
+ end
14
+
15
+ end
16
+ end
data/lib/textorize.rb ADDED
@@ -0,0 +1,6 @@
1
+ $:.unshift File.dirname(__FILE__)
2
+
3
+ require "textorize/runner"
4
+ require "textorize/renderer"
5
+ require "textorize/saver"
6
+ require "textorize/color"
@@ -0,0 +1,18 @@
1
+ require File.join(File.dirname(__FILE__), *%w".. lib textorize")
2
+ require "test/unit"
3
+
4
+ class RunnerTest < Test::Unit::TestCase
5
+
6
+ def test_runner
7
+ options = {}
8
+ options[:font] ||= 'Arial'
9
+ options[:size] ||= 28.0
10
+ options[:kerning] ||= 0
11
+ options[:lineheight] ||= options[:size]
12
+ options[:width] ||= 250
13
+ options[:obliqueness] ||= 0
14
+
15
+ Textorize::Runner.new 'Hallo!', 'output.png', options
16
+ end
17
+
18
+ end
metadata ADDED
@@ -0,0 +1,62 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: textorize
3
+ version: !ruby/object:Gem::Version
4
+ version: "0.21"
5
+ platform: ruby
6
+ authors:
7
+ - Thomas Fuchs
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-10-06 00:00:00 +02:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: Textorize is a OS X utility to render subpixel antialised strings into PNG files.
17
+ email: thomas@fesch.at
18
+ executables:
19
+ - textorize
20
+ extensions: []
21
+
22
+ extra_rdoc_files: []
23
+
24
+ files:
25
+ - Rakefile
26
+ - LICENSE
27
+ - bin/textorize
28
+ - lib/textorize
29
+ - lib/textorize/renderer.rb
30
+ - lib/textorize/runner.rb
31
+ - lib/textorize/saver.rb
32
+ - lib/textorize/color.rb
33
+ - lib/textorize.rb
34
+ - test/test_runner.rb
35
+ has_rdoc: false
36
+ homepage: http://github.com/madrobby/textorize
37
+ post_install_message:
38
+ rdoc_options: []
39
+
40
+ require_paths:
41
+ - lib
42
+ required_ruby_version: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: "0"
47
+ version:
48
+ required_rubygems_version: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: "0"
53
+ version:
54
+ requirements: []
55
+
56
+ rubyforge_project:
57
+ rubygems_version: 1.3.1
58
+ signing_key:
59
+ specification_version: 2
60
+ summary: OS X subpixel antialiased PNG string renderer
61
+ test_files:
62
+ - test/test_runner.rb