<commit_before>'use strict';
{compactFlatten, objectWithout, defineModule, formattedInspect, clone, max, isFunction, log, object, isNumber, isArray, isPlainObject, isString, each, isPlainObject, merge, mergeInto} = require 'art-standard-lib'
{Matrix, identityMatrix, Color, point, rect, rgbColor, isRect, isColor, perimeter, Rectangle} = require 'art-atomic'
{GradientFillStyle, Paths} = require 'art-canvas'
{rectanglePath, ellipsePath, circlePath} = Paths
{BaseClass} = require 'art-class-system'
DrawAreaCollector = require './DrawAreaCollector'

{addDirtyDrawArea} = require './ElementDrawLib'

defineModule module, ->

  (superClass) -> class ElementDrawAreaMixin extends superClass

    @setter
      dirtyDrawAreasChanged: (v) ->
        # log.error "dirtyDrawAreasChanged #{v} #{@key}"
        if v
          @_dirtyDrawAreasChangedWasTrue = v
        else if @_dirtyDrawAreasChangedWasTrue
          @onNextReady => @_dirtyDrawAreasChangedWasTrue = false
        @_dirtyDrawAreasChanged = v

    @virtualProperty

      preFilteredBaseDrawArea: (pending) ->
        {_currentPadding, _currentSize} = @getState pending
        {x, y} = _currentSize
        {w, h} = _currentPadding
        rect 0, 0, max(0, x - w), max(0, y - h)

      baseDrawArea: (pending) ->
        @getPreFilteredBaseDrawArea pending


    ###
    IN:
      elementSpaceDrawArea: required
        the area to clip
      into: [default = new Rectangle]
        If present, this rectangle will be completely replaced with new values.
    OUT: into, if present, else a new Rectangle
    ###
    clipElementSpaceArea: (elementSpaceArea, into) ->
      # log clipElementSpaceArea:
      #   clip: @clip
      #   elementSpaceArea: elementSpaceArea?.clone()
      #   into: into?.clone()
      #   currentSize: @currentSize
      if @clip
        @currentSize.intersect elementSpaceArea, into
        # log clipElementSpaceAreaA:
        #   clip: @clip
        #   elementSpaceArea: elementSpaceArea?.clone()
        #   into: into?.clone()
        #   currentSize: @currentSize
      else if elementSpaceArea != into
        {x, y, w, h} = elementSpaceArea
        if into
          into._setAll x, y, w, h
        else
          new Rectangle x, y, w, h
      else
        elementSpaceArea


    @getter
      parentSpaceDrawArea: -> @_elementToParentMatrix.transformBoundingRect(@getElementSpaceDrawArea())
      elementSpaceDrawArea: -> @_elementSpaceDrawArea ||= @_computeElementSpaceDrawArea()
      drawArea: -> @elementSpaceDrawArea
      drawAreaIn: (elementToTargetMatrix = @getElementToAbsMatrix()) -> elementToTargetMatrix.transformBoundingRect @getElementSpaceDrawArea()

      ###
      IN:
        elementSpaceDrawArea: [default = @drawArea]
          the source area to transform to parent-space and then clip
        into: [default = new Rectangle]
          If present, this rectangle will be completely replaced with new values.
      OUT: into, if present, else a new Rectangle
      ###
      drawAreaInParent: (elementSpaceDrawArea, into)->
        # log drawAreaInParent:
        #   elementSpaceDrawArea: elementSpaceDrawArea ? @elementSpaceDrawArea
        #   elementToParentMatrix: @elementToParentMatrix
        #   intoSame: elementSpaceDrawArea == into
        out = @elementToParentMatrix.transformBoundingRect elementSpaceDrawArea ? @elementSpaceDrawArea, false, into
        # log drawAreaInParent:
        #   elementSpaceDrawArea: elementSpaceDrawArea
        #   elementToParentMatrix: @elementToParentMatrix
        #   out: out.clone()
        out

      ###
      IN:
        elementSpaceDrawArea: [default = @drawArea]
          the source area to transform to parent-space and then clip
        into: [default = new Rectangle]
          If present, this rectangle will be completely replaced with new values.

        NOTE: @parent must be set

      OUT: into, if present, else a new Rectangle
      ###
      clippedDrawAreaInParent: (elementSpaceDrawArea, into)->
        drawAreaInParent = @getDrawAreaInParent elementSpaceDrawArea, into
        # log clippedDrawAreaInParent: drawAreaInParent.clone()
        @parent?.clipElementSpaceArea drawAreaInParent, drawAreaInParent
        drawAreaInParent

      ###
      IN:
        ancestor: Element instance
          if null
            return clippedDrawAreaInAbsSapce
          else
            return clippedDrawArea in ancestor's space

      OUT: new Rectangle
      ###
      clippedDrawAreaInAncestor: (ancestor) ->
        self = @

        while parent = self.parent
          drawArea = self.getClippedDrawAreaInParent drawArea, drawArea

          if parent != ancestor
            self = parent
          else
            break

        drawArea ? @drawArea<commit_msg>FIX: clip-area should take into account padding<commit_after>'use strict';
{compactFlatten, objectWithout, defineModule, formattedInspect, clone, max, isFunction, log, object, isNumber, isArray, isPlainObject, isString, each, isPlainObject, merge, mergeInto} = require 'art-standard-lib'
{Matrix, identityMatrix, Color, point, rect, rgbColor, isRect, isColor, perimeter, Rectangle} = require 'art-atomic'
{GradientFillStyle, Paths} = require 'art-canvas'
{rectanglePath, ellipsePath, circlePath} = Paths
{BaseClass} = require 'art-class-system'
DrawAreaCollector = require './DrawAreaCollector'

{addDirtyDrawArea} = require './ElementDrawLib'

defineModule module, ->

  (superClass) -> class ElementDrawAreaMixin extends superClass

    @setter
      dirtyDrawAreasChanged: (v) ->
        if v
          @_dirtyDrawAreasChangedWasTrue = v
        else if @_dirtyDrawAreasChangedWasTrue
          @onNextReady => @_dirtyDrawAreasChangedWasTrue = false
        @_dirtyDrawAreasChanged = v

    @virtualProperty

      preFilteredBaseDrawArea: (pending) ->
        {_currentPadding, _currentSize} = @getState pending
        {x, y} = _currentSize
        {w, h} = _currentPadding
        rect 0, 0, max(0, x - w), max(0, y - h)

      baseDrawArea: (pending) ->
        @getPreFilteredBaseDrawArea pending

    ###
    IN:
      elementSpaceDrawArea: required
        the area to clip
      into: [default = new Rectangle]
        If present, this rectangle will be completely replaced with new values.
    OUT: into, if present, else a new Rectangle
    ###
    clipElementSpaceArea: (elementSpaceArea, into) ->
      if @clip
        if @padding
          @paddedArea.intersect elementSpaceArea, into
        else
          @currentSize.intersect elementSpaceArea, into

      else if elementSpaceArea != into
        {x, y, w, h} = elementSpaceArea
        if into
          into._setAll x, y, w, h
        else
          new Rectangle x, y, w, h
      else
        elementSpaceArea


    @getter
      parentSpaceDrawArea: -> @_elementToParentMatrix.transformBoundingRect(@getElementSpaceDrawArea())
      elementSpaceDrawArea: -> @_elementSpaceDrawArea ||= @_computeElementSpaceDrawArea()
      drawArea: -> @elementSpaceDrawArea
      drawAreaIn: (elementToTargetMatrix = @getElementToAbsMatrix()) -> elementToTargetMatrix.transformBoundingRect @getElementSpaceDrawArea()

      ###
      IN:
        elementSpaceDrawArea: [default = @drawArea]
          the source area to transform to parent-space and then clip
        into: [default = new Rectangle]
          If present, this rectangle will be completely replaced with new values.
      OUT: into, if present, else a new Rectangle
      ###
      drawAreaInParent: (elementSpaceDrawArea, into)->
        @elementToParentMatrix.transformBoundingRect elementSpaceDrawArea ? @elementSpaceDrawArea, false, into

      ###
      IN:
        elementSpaceDrawArea: [default = @drawArea]
          the source area to transform to parent-space and then clip
        into: [default = new Rectangle]
          If present, this rectangle will be completely replaced with new values.

        NOTE: @parent must be set

      OUT: into, if present, else a new Rectangle
      ###
      clippedDrawAreaInParent: (elementSpaceDrawArea, into)->
        drawAreaInParent = @getDrawAreaInParent elementSpaceDrawArea, into
        @parent?.clipElementSpaceArea drawAreaInParent, drawAreaInParent
        drawAreaInParent

      ###
      IN:
        ancestor: Element instance
          if null
            return clippedDrawAreaInAbsSapce
          else
            return clippedDrawArea in ancestor's space

      OUT: new Rectangle
      ###
      clippedDrawAreaInAncestor: (ancestor) ->
        self = @

        while parent = self.parent
          drawArea = self.getClippedDrawAreaInParent drawArea, drawArea

          if parent != ancestor
            self = parent
          else
            break

        drawArea ? @drawArea
{'fraction_non_alphanumeric': 0.081539166284929, 'fraction_numerical': 0.0009161704076958314, 'mean_word_length': 2.7352437981180495, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 31, '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': '934478', 'n_tokens_mistral': 2408, 'n_tokens_neox': 2351, 'n_words': 674}