madrobby-textorize 0.0.6 → 0.0.7

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/bin/textorize CHANGED
@@ -1,5 +1,6 @@
1
1
  #!/usr/bin/env ruby
2
2
  require "optparse"
3
+ require "textorize"
3
4
 
4
5
  options = {}
5
6
  opts = OptionParser.new do |opts|
@@ -0,0 +1,58 @@
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,1000,100])
9
+
10
+ set_attribs options
11
+ window.setContentView @text_view
12
+ @text_view.setString string
13
+ @text_view.sizeToFit
14
+
15
+ window.display
16
+ window.orderFrontRegardless
17
+ end
18
+
19
+ def bitmap
20
+ @text_view.lockFocus
21
+ bitmap = NSBitmapImageRep.alloc.initWithFocusedViewRect(@text_view.bounds)
22
+ @text_view.unlockFocus
23
+ bitmap
24
+ end
25
+
26
+ private
27
+
28
+ def set_attribs(options)
29
+ @text_view.setHorizontallyResizable(true)
30
+ @text_view.useAllLigatures(nil)
31
+
32
+ color = (options[:color] || '0,0,0').split(',')
33
+ fgcolor = NSColor.colorWithDeviceRed_green_blue_alpha(color[0], color[1], color[2], 1)
34
+ bgcolor = (options[:background] || '1,1,1').split(',')
35
+ @text_view.setBackgroundColor(
36
+ NSColor.colorWithDeviceRed_green_blue_alpha(bgcolor[0], bgcolor[1], bgcolor[2], 1)
37
+ )
38
+ @text_view.setTextColor(fgcolor)
39
+
40
+ puts @text_view.inspect
41
+
42
+ para = NSMutableParagraphStyle.alloc.init
43
+ para.setLineSpacing(options[:lineheight])
44
+
45
+ attribs = NSMutableDictionary.alloc.init
46
+ attribs.setObject_forKey(NSFont.fontWithName_size(options[:font], options[:size]), NSFontAttributeName)
47
+ attribs.setObject_forKey(options[:kerning], NSKernAttributeName)
48
+ attribs.setObject_forKey(para, NSParagraphStyleAttributeName)
49
+ attribs.setObject_forKey(0, NSBaselineOffsetAttributeName)
50
+ attribs.setObject_forKey(options[:obliqueness], NSObliquenessAttributeName)
51
+
52
+ @text_view.setTypingAttributes(attribs)
53
+ @text_view.lowerBaseline(nil)
54
+ end
55
+
56
+ end
57
+
58
+ end
@@ -0,0 +1,38 @@
1
+ require 'osx/cocoa'
2
+
3
+ module Textorize
4
+ class Runner
5
+
6
+ def initialize(string, output, options)
7
+ app = OSX::NSApplication.sharedApplication
8
+
9
+ delegate = RunnerApplication.alloc.init
10
+ delegate.options = options
11
+ delegate.string = string
12
+ delegate.output = output
13
+
14
+ app.setDelegate delegate
15
+ app.run
16
+ end
17
+
18
+ end
19
+
20
+ class RunnerApplication < OSX::NSObject
21
+ include OSX
22
+ attr_accessor :options
23
+ attr_accessor :string
24
+ attr_accessor :output
25
+
26
+ def initialize
27
+ @window = NSWindow.alloc.initWithContentRect_styleMask_backing_defer([150, 1500, 1000, 500], NSBorderlessWindowMask, 2, 0)
28
+ end
29
+
30
+ def applicationDidFinishLaunching(notification)
31
+ renderer = Renderer.new(@window, @string, @options)
32
+ Saver.new(renderer).write_to_file(@output)
33
+ NSApplication.sharedApplication.terminate(nil)
34
+ end
35
+
36
+ end
37
+
38
+ 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,5 @@
1
+ $:.unshift File.dirname(__FILE__)
2
+
3
+ require "textorize/runner"
4
+ require "textorize/renderer"
5
+ require "textorize/saver"
@@ -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 CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: madrobby-textorize
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.6
4
+ version: 0.0.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Thomas Fuchs
@@ -23,6 +23,13 @@ extra_rdoc_files: []
23
23
 
24
24
  files:
25
25
  - Rakefile
26
+ - bin/textorize
27
+ - lib/textorize
28
+ - lib/textorize/renderer.rb
29
+ - lib/textorize/runner.rb
30
+ - lib/textorize/saver.rb
31
+ - lib/textorize.rb
32
+ - test/test_runner.rb
26
33
  has_rdoc: false
27
34
  homepage: http://github.com/madrobby/textorize
28
35
  licenses: