<commit_before>module GM

  if false
    # declaring these functions here will make them available in the REPL.
    CATransform3DMakeRotation(0, 0, 0, 0)
    CATransform3DMakeTranslation(0, 0, 0)
    CATransform3DMakeScale(1.0, 1.0, 1.0)
    CATransform3DRotate(t, 0, 0, 0, 0)
    CATransform3DScale(t, 0, 0, 0)
    CATransform3DTranslate(t, 0, 0, 0)
  end


  # This class is meant to be created using the `Drawler` method, passing in a
  # frame and a block that is executed in the `drawRect` method.  You can either
  # accept the context as an argument to the block, or use the `@context`
  # instance variable.
  class Drawler < UIView
    class << self
      attr_accessor :draw_code
    end

    def drawRect(rect)
      @context = UIGraphicsGetCurrentContext()
      CGContextSaveGState(@context)
      draw_code = self.class.draw_code
      if draw_code
        if draw_code.arity == 0
          instance_exec(&draw_code)
        else
          instance_exec(@context, &draw_code)
        end
      end
      CGContextRestoreGState(@context)
    end

  end


  def Drawler(frame=nil, &draw_code)
    klass = Class.new(Drawler)
    klass.draw_code = draw_code
    return frame ? klass.alloc.initWithFrame(frame) : klass
  end


  class Drawing < UIView
    attr_accessor :draw

    def self.new(frame, drawing=nil)
      self.alloc.initWithFrame(frame).tap do |instance|
        instance.draw = drawing if drawing
      end
    end

    def initWithFrame(frame)
      super.tap do
        self.backgroundColor = :clear.uicolor
      end
    end

    def draw=(drawing_primitives)
      if drawing_primitives.is_a? Enumerable
        @draw = drawing_primitives
      else
        @draw = [drawing_primitives]
      end
      drawing_primitives
    end

    def drawRect(rect)
      context = UIGraphicsGetCurrentContext()
      @draw.each do |drawing|
        CGContextSaveGState(context)
        drawing.draw
        CGContextRestoreGState(context)
      end
    end

    def intrinsicContentSize
      rect = nil
      @draw.each do |drawing|
        next unless drawing.respond_to?(:frame)

        if rect
          rect = CGRectUnion(rect, drawing.frame)
        else
          rect = drawing.frame
        end
      end

      rect and rect.size or [0, 0]
    end

  end

  module D

    class Draw
      def self.attr_assigner(name, default=nil, &converter)
        # writer calls the setter
        define_method("#{name}=") { |value|
          self.send(name, value)
        }
        # combined getter/setter
        define_method(name) { |*args|
          if args.length == 1
            value = args[0]
            if converter
              value = converter.call(value)
            end
            instance_variable_set("@#{name}", value)
            return self
          elsif args.length > 1
            raise "Too many args on the dance floor"
          else
            return instance_variable_get("@#{name}") || default
          end
        }
      end

      def draw
        raise 'Implement the `draw` method'
      end
    end

    class Primitive < Draw
      attr_assigner(:line_width, 1)
      attr_assigner(:line_dash)
      attr_assigner(:color, UIColor.blackColor) { |val| val ? val.uicolor : UIColor.clearColor }
      attr_assigner(:fill, UIColor.clearColor) { |val| val ? val.uicolor : UIColor.clearColor }

      # setup the default drawing context, and perform your drawing in the block
      # you pass to this function
      def defaults(context)
        CGContextSaveGState(context)
        color.setStroke
        fill.setFill
        CGContextSetLineWidth(context, self.line_width)
        if line_dash
          CGContextSetLineDash(context, 0, line_dash.to_pointer(:float), line_dash.length)
        end
        yield
        CGContextRestoreGState(context)
      end

      def background(*args)
        NSLog('Draw#background is deprecated in favor of Draw#fill')
        fill(*args)
      end

    end

    class Line < Primitive
      attr_assigner(:p1) { |pt| SugarCube::CoreGraphics::Point(pt) }
      attr_assigner(:p2) { |pt| SugarCube::CoreGraphics::Point(pt) }

      def initialize(p1, p2)
        self.p1(p1)<commit_msg>added fill_phase, useful for patterns<commit_after>module GM

  if false
    # declaring these functions here will make them available in the REPL.
    CATransform3DMakeRotation(0, 0, 0, 0)
    CATransform3DMakeTranslation(0, 0, 0)
    CATransform3DMakeScale(1.0, 1.0, 1.0)
    CATransform3DRotate(t, 0, 0, 0, 0)
    CATransform3DScale(t, 0, 0, 0)
    CATransform3DTranslate(t, 0, 0, 0)
  end


  # This class is meant to be created using the `Drawler` method, passing in a
  # frame and a block that is executed in the `drawRect` method.  You can either
  # accept the context as an argument to the block, or use the `@context`
  # instance variable.
  class Drawler < UIView
    class << self
      attr_accessor :draw_code
    end

    def drawRect(rect)
      @context = UIGraphicsGetCurrentContext()
      CGContextSaveGState(@context)
      draw_code = self.class.draw_code
      if draw_code
        if draw_code.arity == 0
          instance_exec(&draw_code)
        else
          instance_exec(@context, &draw_code)
        end
      end
      CGContextRestoreGState(@context)
    end

  end


  def Drawler(frame=nil, &draw_code)
    klass = Class.new(Drawler)
    klass.draw_code = draw_code
    return frame ? klass.alloc.initWithFrame(frame) : klass
  end


  class Drawing < UIView
    attr_accessor :draw

    def self.new(frame, drawing=nil)
      self.alloc.initWithFrame(frame).tap do |instance|
        instance.draw = drawing if drawing
      end
    end

    def initWithFrame(frame)
      super.tap do
        self.backgroundColor = :clear.uicolor
      end
    end

    def draw=(drawing_primitives)
      if drawing_primitives.is_a? Enumerable
        @draw = drawing_primitives
      else
        @draw = [drawing_primitives]
      end
      drawing_primitives
    end

    def drawRect(rect)
      context = UIGraphicsGetCurrentContext()
      @draw.each do |drawing|
        CGContextSaveGState(context)
        drawing.draw
        CGContextRestoreGState(context)
      end
    end

    def intrinsicContentSize
      rect = nil
      @draw.each do |drawing|
        next unless drawing.respond_to?(:frame)

        if rect
          rect = CGRectUnion(rect, drawing.frame)
        else
          rect = drawing.frame
        end
      end

      rect and rect.size or [0, 0]
    end

  end

  module D

    class Draw
      def self.attr_assigner(name, default=nil, &converter)
        # writer calls the setter
        define_method("#{name}=") { |value|
          self.send(name, value)
        }
        # combined getter/setter
        define_method(name) { |*args|
          if args.length == 1
            value = args[0]
            if converter
              value = converter.call(value)
            end
            instance_variable_set("@#{name}", value)
            return self
          elsif args.length > 1
            raise "Too many args on the dance floor"
          else
            return instance_variable_get("@#{name}") || default
          end
        }
      end

      def draw
        raise 'Implement the `draw` method'
      end
    end

    class Primitive < Draw
      attr_assigner(:line_width, 1)
      attr_assigner(:line_dash)
      attr_assigner(:color, UIColor.blackColor) { |val| val ? val.uicolor : UIColor.clearColor }
      attr_assigner(:fill, UIColor.clearColor) { |val| val ? val.uicolor : UIColor.clearColor }
      attr_assigner(:fill_phase) { |val| val && SugarCube::CoreGraphics::Size(val) }

      # setup the default drawing context, and perform your drawing in the block
      # you pass to this function
      def defaults(context)
        CGContextSaveGState(context)
        color.setStroke
        fill.setFill
        if fill_phase
          CGContextSetPatternPhase(context, fill_phase)
        end
        CGContextSetLineWidth(context, self.line_width)
        if line_dash
          CGContextSetLineDash(context, 0, line_dash.to_pointer(:float), line_dash.length)
        end
        yield
        CGContextRestoreGState(context)
      end

      def background(*args)
        NSLog('Draw#background is deprecated in favor of Draw#fill')
        fill(*args)
      end

    end

    class Line < Primitive
      attr_assigner(:p1) { |pt| SugarCube::CoreGraphics::Point(pt) }
      attr_assigner(:p2) { |pt| SugarCube::CoreGraphics::Point(pt) }

      def initialize(p1, p2)
        self.p1(p1)
{'fraction_non_alphanumeric': 0.08207822533566841, 'fraction_numerical': 0.010040863981319323, 'mean_word_length': 2.245926487305798, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 5, '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': '5280035', 'n_tokens_mistral': 2739, 'n_tokens_neox': 2590, 'n_words': 688}