lib/ray/font.rb in ray-0.0.1 vs lib/ray/font.rb in ray-0.1.0.pre1

- old
+ new

@@ -1,108 +1,50 @@ +# -*- coding: utf-8 -*- + module Ray class Font + include Ray::TextHelper + extend Ray::ResourceSet - need_argument_count 1 - add_set(/^(.*)$/) { |filename, size| new(filename, size) } + add_set(/^(.*)$/) { |filename| new(filename) } - # @return [true, false] True if the font has no style - def normal? - style == STYLE_NORMAL - end + # @param [String] string The string which should be drawn. + # + # @option opts [Integer] :size Size of the font + # @option opts [String, nil] :encoding Encoding of the text. Guessed in + # Ruby 1.9, assumes UTF-8 otherwise. + # @option opts [Integer, Array<Symbol>] :style Flags for the font style. + # Valid symbols are :normal, :italic, :bold, and :underline. + # @option opts [Ray::Color] :color Color to draw the text in. Defaults to + # white. + # @option opts [Ray::Image] :on The image to draw on. In this case, + # it will directly draw instead of returning an image containing nothing + # but the drawn text. + # @option opts [Ray::Vector2, #to_vector2] :to or :at where to draw on the image. + # @return The surface it drew the string on. + def draw(string, opts) + enc = opts[:encoding] || (string.respond_to?(:encoding) && string.encoding.to_s) + enc ||= "UTF-8" - # @return [true, false] True if the font is italic - def italic? - (style & STYLE_ITALIC) != 0 - end + opts[:style] = parse_style(opts[:style]) - # @return [true, false] True if the font is bold - def bold? - (style & STYLE_BOLD) != 0 + simple_draw internal_string(string, enc), opts end - # @return [true, false] True if the font is underlined - def underlined? - (style & STYLE_UNDERLINE) != 0 - end - end -end + # @param [Hash] opts (See #draw) + # @return [Ray::Vector2] Size of the text using this font. + def size_of(string, opts) + enc = opts[:encoding] || (string.respond_to?(:encoding) && string.encoding.to_s) + enc ||= "UTF-8" -class String - # Draws the receiver. - # - # @option opts [Ray::Font] :font The font used to render the string. - # @option opts [Ray::Image] :on The image to draw on. - # @option opts [Integer] :w Witdh of the image. Also called :width. - # @option opts [Integer] ;h height of the image. Also called :height. - # @option opts [Symbol] The encoding. Can be guessed in Ruby 1.9. - # @option opts [Ray::Color] :color The color to draw the text in. - # @option opts [Ray::Color] :background Background color in shaded mode. - # @option opts [Symbol] :mode The drawing mode. - # @option opts [Array<Symbol>] :style The different styles to apply. - # :italic, :bold, and :underlined. - # - # @option opts [Array<Integer>] :at Where the image should be drawn. - # Defaults to (0, 0) - # - # @see Ray::Font#draw - def draw(opts = {}) - font = opts[:font] + opts[:style] = parse_style(opts[:style]) - 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 + simple_size_of internal_string(string, enc), opts 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 + # @return [Integer] Width required to draw the sring with this font. + # @see #size_of + def width_of(string, opts) + size_of(string, opts).width end - - target end end