<commit_before># $Id: misc.rb,v 1.17 2010/03/21 01:43:01 baror Exp $
# Copyright (C) 2009 <NAME>
module Magick
  class RVG
    # This is a standard deep_copy method that is used in most classes.
    # Thanks to <NAME>.
    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?(Fixnum) || 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.collect(&: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.collect { |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.zero? || glyph_orientation == 180
                glyph_dimensions.width
              else
                glyph_dimensions.ascent - glyph_dimensions.descent
              end
          case @ctx.text_attrs.baseline_shift
          when :baseline
            x = 0
          when :super
            x = -x
          when /[-+]?(\d+)%/
            m = Regexp.last_match(1) == '-' ? -1.0 : 1.0
            x = (m * x * Regexp.last_match(1).to_f / 100.0)
          else
            x = -@ctx.text_attrs.baseline_shift
          end
          x
        end

        def render_glyph(glyph_orientation, x, y, glyph)
          if glyph_orientation.zero?
            @ctx.gc.text(x, y, enquote(glyph))
          else
            @ctx.gc.push
            @ctx.gc.translate(x, y)<commit_msg>Revert "Fix RuboCop `Lint/EmptyWhen` cop"<commit_after># $Id: misc.rb,v 1.17 2010/03/21 01:43:01 baror Exp $
# Copyright (C) 2009 <NAME>
module Magick
  class RVG
    # This is a standard deep_copy method that is used in most classes.
    # Thanks to <NAME>.
    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?(Fixnum) || 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.collect(&: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.collect { |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.zero? || glyph_orientation == 180
                glyph_dimensions.width
              else
                glyph_dimensions.ascent - glyph_dimensions.descent
              end
          case @ctx.text_attrs.baseline_shift
          when :baseline
            x = 0
          when :sub

          when :super
            x = -x
          when /[-+]?(\d+)%/
            m = Regexp.last_match(1) == '-' ? -1.0 : 1.0
            x = (m * x * Regexp.last_match(1).to_f / 100.0)
          else
            x = -@ctx.text_attrs.baseline_shift
          end
          x
        end

        def render_glyph(glyph_orientation, x, y, glyph)
          if glyph_orientation.zero?
            @ctx.gc.text(x, y, enquote(glyph))
          else
            @ctx.gc.push
            @ctx.gc.translate(x, y)
{'fraction_non_alphanumeric': 0.1078786623874107, 'fraction_numerical': 0.008903613210477276, 'mean_word_length': 1.499353169469599, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1625832', 'n_tokens_mistral': 3079, 'n_tokens_neox': 2916, 'n_words': 721}