Class: String

Inherits:
Object
  • Object
show all
Defined in:
lib/ray/font.rb,
lib/ray/image.rb

Instance Method Summary collapse

Instance Method Details

#draw(opts = {}) ⇒ Object

Draws the receiver.

Parameters:

  • opts (Hash) (defaults to: {})

    a customizable set of options

Options Hash (opts):

  • :font (Ray::Font)

    The font used to render the string.

  • :on (Ray::Image)

    The image to draw on.

  • :w (Integer)

    Witdh of the image. Also called :width.

  • ;h (Integer)

    height of the image. Also called :height.

  • The (Symbol)

    encoding. Can be guessed in Ruby 1.9.

  • :color (Ray::Color)

    The color to draw the text in.

  • :background (Ray::Color)

    Background color in shaded mode.

  • :mode (Symbol)

    The drawing mode.

  • :style (Array<Symbol>)

    The different styles to apply. :italic, :bold, and :underlined.

  • :at (Array<Integer>)

    Where the image should be drawn. Defaults to (0, 0)

See Also:



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

#to_imageObject

Converts the string to an image using Ray::ImageSet.[]



95
96
97
# File 'lib/ray/image.rb', line 95

def to_image
  Ray::ImageSet[self]
end