N
  7   	ˆl–Ü4ý('”Ò~êqA
ãm\ŠbÑ¿�þ[d2¬ã�l +d€óÈ„DëÿÇÂÅÄ ?÷     	# $Id: misc.rb,v 1.17 2010/03/21 01:43:01 baror Exp $
# Copyright (C) 2009 Timothy P. Hunter
module Magick
  class RVG
    # This is a standard deep_copy method that is used in most classes.
    # Thanks to Robert Klemme.
    module Duplicatable
      def deep_copy(h = {})
        # Prevent recursion. If we reach the
        # object we started with, stop copying.
        copy = h[__id__]
        unless copy
          h[__id__] = copy = self.class.allocate
          ivars = instance_variables
          ivars.each do |ivar|
            ivalue = instance_variable_get(ivar)
            cvalue = if ivalue.is_a?(NilClass) || ivalue.is_a?(Symbol) || ivalue.is_a?(Float) || ivalue.is_a?(Integer) || ivalue.is_a?(FalseClass) || ivalue.is_a?(TrueClass)
                       ivalue
                     elsif ivalue.respond_to?(:deep_copy)
                       ivalue.deep_copy(h)
                     elsif ivalue.respond_to?(:dup)
                       ivalue.dup
                     else
                       ivalue
                     end
            copy.instance_variable_set(ivar, cvalue)
          end
          copy.freeze if frozen?
        end
        copy
      end
    end # module Duplicatable

    # Convert an array of method arguments to Float objects. If any
    # cannot be converted, raise ArgumentError and issue a message.
    def self.fmsg(*args)
      "at least one argument cannot be converted to Float (got #{args.map(&:class).join(', ')})"
    end

    def self.convert_to_float(*args)
      allow_nil = false
      if args.last == :allow_nil
        allow_nil = true
        args.pop
      end
      begin
        fargs = args.map { |a| allow_nil && a.nil? ? a : Float(a) }
      rescue ArgumentError, TypeError
        raise ArgumentError, fmsg(*args)
      end
      fargs
    end

    def self.convert_one_to_float(arg)
      begin
        farg = Float(arg)
      rescue ArgumentError, TypeError
        raise ArgumentError, "argument cannot be converted to Float (got #{arg.class})"
      end
      farg
    end
  end # class RVG
end # module Magick

module Magick
  class RVG
    class Utility
      class TextStrategy
        def initialize(context)
          @ctx = context
          @ctx.shadow.affine = @ctx.text_attrs.affine
        end

        def enquote(text)
          return text if text.length > 2 && /\A(?:\"[^\"]+\"|\'[^\']+\'|\{[^\}]+\})\z/.match(text)

          if !text['\'']
            text = '\'' + text + '\''
            return text
          elsif !text['"']
            text = '"' + text + '"'
            return text
          elsif !(text['{'] || text['}'])
            text = '{' + text + '}'
            return text
          end

          # escape existing braces, surround with braces
          text.gsub!(/[}]/) { |b| '\\' + b }
          '{' +  text + '}'
        end

        def glyph_metrics(glyph_orientation, glyph)
          gm = @ctx.shadow.get_type_metrics('a' + glyph + 'a')
          gm2 = @ctx.shadow.get_type_metrics('aa')
          h = (gm.ascent - gm.descent + 0.5).to_i
          w = gm.width - gm2.width
          if glyph_orientation.zero? || glyph_orientation == 180
            [w, h]
          else
            [h, w]
          end
        end

        def text_rel_coords(text)
          y_rel_coords = []
          x_rel_coords = []
          first_word = true
          words = text.split(::Magick::RVG::WORD_SEP)
          words.each do |word|
            unless first_word
              wx, wy = get_word_spacing
              x_rel_coords << wx
              y_rel_coords << wy
            end
            first_word = false
            word.split('').each do |glyph|
              wx, wy = get_letter_spacing(glyph)
              x_rel_coords << wx
              y_rel_coords << wy
            end
          end
          [x_rel_coords, y_rel_coords]
        end

        def shift_baseline(glyph_orientation, glyph)
          glyph_dimensions = @ctx.shadow.get_type_metrics(glyph)
          x = if glyph_orientation.zer