                 clip = makeClip clipPath
               @processedPathCnt += 1
 
-            for subPageItem in obj.pageItems when not subPageItem.clipping
+            for subPageItem in obj.pageItems by -1 when not subPageItem.clipping
               @process subPageItem, clip, opacity
 
           when "PathItem"


      prefix = @prefix stroke, fill, clip, alpha, layerNum, layerName

      if not @assSections[layerNum]?
        @assSections[layerNum] = {}

      if not @assSections[layerNum][prefix]?
        @assSections[layerNum][prefix] = []

      Array.prototype.push.apply @assSections[layerNum][prefix], ASS_createDrawingFromPoints path.pathPoints

    prefix: (stroke, fill, clip, alpha) ->
      "{\\an7\\pos(0,0)#{stroke}#{fill}#{alpha}#{clip}\\p1}"

    suffix: -> "{\\p0}"

    get: (includeEmptyLayers = false) ->
      fragments = []
      suffix = @suffix()

      for shapes, layerNum in @assSections
        if shapes?
          for prefix, paths of shapes
            fragments.push prefix
            fragments.push paths.join " "
            fragments.push suffix
            fragments.push "\n"
        else if includeEmptyLayers
            fragments.push "\n"

      fragments.pop()

  output = {
    pathCnt: null
    processedPathCnt: 0
    layers: []

    makeLayer: (emptyPrefix) ->
      layer = {
        groups: []
        currGroupIdx: -1
        currGroup: null
        emptyPrefix: null

        makeMergeGroup: ->
          {
            dirtyRects: []
            lines: {}

            addPath: (path, prefix) ->
              unless @isZeroArea path.visibleBounds
                @dirtyRects.push path.visibleBounds
                drawing = ASS_createDrawingFromPoints path.pathPoints

                if @lines[prefix]?
                  Array.prototype.push.apply @lines[prefix], drawing
                else @lines[prefix] = drawing

            isZeroArea: (bounds) ->
              return bounds[2]-bounds[0] == 0 and bounds[3]-bounds[1] == 0

            isMergeable: (path) ->
              bounds = path.visibleBounds

              if @isZeroArea bounds
                return true

              for rect in @dirtyRects
                if bounds[2] > rect[0] and bounds[0] < rect[2] and bounds[3] < rect[1] and bounds[1] > rect[3]
                  return false

              return true
          }

        addGroup: ->
          @currGroupIdx += 1
          @currGroup = @makeMergeGroup()
          @groups[@currGroupIdx] = @currGroup

        addPath: (path, prefix) ->
          unless @currGroup.isMergeable path
            @addGroup()
          @currGroup.addPath path, prefix
      }

      layer.addGroup()
      layer.emptyPrefix = emptyPrefix
      return layer

    process: ( obj, clip, opacity = 100 ) ->
      if not @pathCnt?
