Class: String
- Inherits:
-
Object
- Object
- String
- Defined in:
- lib/ray/font.rb,
lib/ray/image.rb
Instance Method Summary collapse
-
#draw(opts = {}) ⇒ Object
Draws the receiver.
-
#to_image ⇒ Object
Converts the string to an image using Ray::ImageSet.[].
Instance Method Details
#draw(opts = {}) ⇒ Object
Draws the receiver.
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/ray/font.rb', line 47 def draw(opts = {}) font = opts[:font] lines = split(/\r\n|\n|\r/) line_skip = font.line_skip target = opts[:on] string_encoding = opts[:encoding] string_encoding ||= if respond_to? :encoding # Ruby 1.9 case encoding.to_s when /^utf-?8$/i :utf8 when /^iso-8859-/i :latin1 else nil end else nil end target ||= Ray::Image.new(:height => opts[:height] || opts[:h] || line_skip * lines.size, :width => opts[:width] || opts[:w] || lines.map { |i| font.size_of(self, string_encoding).width }.max) color = opts[:color] background = opts[:background] mode = opts[:mode] if styles = opts[:style] font.style = styles.inject(0) do |flags, style| flags |= case style when :italic Ray::Font::STYLE_ITALIC when :bold Ray::Font::STYLE_BOLD when :underlined Ray::Font::STYLE_UNDERLINE else raise "Unknown flag #{style}" end end end x, y = opts[:at] x ||= 0 y ||= 0 lines.each do |line| font.draw(line, :on => target, :at => [x, y], :encoding => string_encoding, :color => color, :background => background, :mode => mode) y += line_skip end target end |