rendered paste bodyrequire 'osx/cocoa'
module Stuff
module TextGenerator
class Processor
def self.generate_text(options)
app = OSX::NSApplication.sharedApplication
delegate = ProcessorApplication.alloc.init
delegate.options = options
app.setDelegate(delegate)
app.run
end
end
class ProcessorApplication < OSX::NSObject
include OSX
attr_accessor :options
def initialize
rect = [-16000, -16000, 5000, 5000]
@window = NSWindow.alloc.initWithContentRect_styleMask_backing_defer(rect, NSBorderlessWindowMask, 2, 0)
#win.setOpaque(false)
@window.setAlphaValue(1.0)
end
def applicationDidFinishLaunching(notification)
@options[:strings].each do |string|
view = draw_string(string, @options)
save(view, 'test.png')
end
NSApplication.sharedApplication.terminate(nil)
end
def draw_string(string, options)
puts "Drawing: #{string}"
text_view = NSTextView.alloc.initWithFrame([0,0,0,0])
text_view.setHorizontallyResizable(true)
text_view.setTextColor(NSColor.blackColor)
text_view.setFont(NSFont.fontWithName_size(options[:font], options[:size]))
#@text_view.setBackgroundColor(NSColor.clearColor)
text_view.setBackgroundColor(NSColor.whiteColor)
if (options[:kerning]!=0)
method = options[:kerning] > 0 ? 'loosenKerning' : 'tightenKerning'
options[:kerning].abs.times do
text_view.send(method, nil)
end
end
text_view.setString(string)
text_view.sizeToFit
@window.setContentView(text_view)
@window.display
@window.orderFrontRegardless
return text_view
end
def save(view, file)
view.lockFocus
bitmap = NSBitmapImageRep.alloc.initWithFocusedViewRect(view.bounds)
view.unlockFocus
png = bitmap.representationUsingType_properties(NSPNGFileType, nil)
png.writeToFile_atomically(file, true)
end
end
end
end