private void paintBackground(
      Graphics2D g, Rectangle clip, int startVisualLine, int endVisualLine) {
    int lineCount = myEditor.getVisibleLineCount();
    final Map<Integer, Couple<Integer>> virtualSelectionMap =
        createVirtualSelectionMap(startVisualLine, endVisualLine);
    for (int visualLine = startVisualLine; visualLine <= endVisualLine; visualLine++) {
      int y = myView.visualLineToY(visualLine);
      LineLayout prefixLayout = myView.getPrefixLayout();
      if (visualLine == 0 && prefixLayout != null) {
        paintBackground(g, myView.getPrefixAttributes(), 0, y, prefixLayout.getWidth());
      }
      if (visualLine >= lineCount) break;
      paintLineFragments(
          g,
          clip,
          visualLine,
          y,
          new LineFragmentPainter() {
            @Override
            public void paintBeforeLineStart(
                Graphics2D g, TextAttributes attributes, int columnEnd, float xEnd, int y) {
              paintBackground(g, attributes, 0, y, xEnd);
              paintSelectionOnSecondSoftWrapLineIfNecessary(g, columnEnd, xEnd, y);
            }

            @Override
            public void paint(
                Graphics2D g,
                VisualLineFragmentsIterator.Fragment fragment,
                int start,
                int end,
                TextAttributes attributes,
                float xStart,
                float xEnd,
                int y) {
              paintBackground(g, attributes, xStart, y, xEnd - xStart);
            }

            @Override
            public void paintAfterLineEnd(
                Graphics2D g, Rectangle clip, IterationState it, int columnStart, float x, int y) {
              paintBackground(
                  g, it.getPastLineEndBackgroundAttributes(), x, y, clip.x + clip.width - x);
              int offset = it.getEndOffset();
              SoftWrap softWrap = myEditor.getSoftWrapModel().getSoftWrap(offset);
              if (softWrap == null) {
                paintVirtualSelectionIfNecessary(
                    g, virtualSelectionMap, columnStart, x, clip.x + clip.width, y);
              } else {
                paintSelectionOnFirstSoftWrapLineIfNecessary(
                    g, columnStart, x, clip.x + clip.width, y);
              }
            }
          });
    }
  }
  private void paintTextWithEffects(
      Graphics2D g, Rectangle clip, int startVisualLine, int endVisualLine) {
    final CharSequence text = myDocument.getImmutableCharSequence();
    final EditorImpl.LineWhitespacePaintingStrategy whitespacePaintingStrategy =
        myEditor.new LineWhitespacePaintingStrategy();
    int lineCount = myEditor.getVisibleLineCount();
    for (int visualLine = startVisualLine; visualLine <= endVisualLine; visualLine++) {
      int y = myView.visualLineToY(visualLine) + myView.getAscent();
      LineLayout prefixLayout = myView.getPrefixLayout();
      if (visualLine == 0 && prefixLayout != null) {
        g.setColor(myView.getPrefixAttributes().getForegroundColor());
        paintLineLayoutWithEffect(
            g,
            prefixLayout,
            0,
            y,
            myView.getPrefixAttributes().getEffectColor(),
            myView.getPrefixAttributes().getEffectType());
      }
      if (visualLine >= lineCount) break;

      final int[] currentLogicalLine = new int[] {-1};

      paintLineFragments(
          g,
          clip,
          visualLine,
          y,
          new LineFragmentPainter() {
            @Override
            public void paintBeforeLineStart(
                Graphics2D g, TextAttributes attributes, int columnEnd, float xEnd, int y) {
              SoftWrapModelImpl softWrapModel = myEditor.getSoftWrapModel();
              int symbolWidth =
                  softWrapModel.getMinDrawingWidthInPixels(SoftWrapDrawingType.AFTER_SOFT_WRAP);
              softWrapModel.paint(
                  g,
                  SoftWrapDrawingType.AFTER_SOFT_WRAP,
                  (int) xEnd - symbolWidth,
                  y - myView.getAscent(),
                  myView.getLineHeight());
            }

            @Override
            public void paint(
                Graphics2D g,
                VisualLineFragmentsIterator.Fragment fragment,
                int start,
                int end,
                TextAttributes attributes,
                float xStart,
                float xEnd,
                int y) {
              if (attributes != null && attributes.getForegroundColor() != null) {
                g.setColor(attributes.getForegroundColor());
                fragment.draw(g, xStart, y, start, end);
              }
              if (fragment.getCurrentFoldRegion() == null) {
                int logicalLine = fragment.getStartLogicalLine();
                if (logicalLine != currentLogicalLine[0]) {
                  whitespacePaintingStrategy.update(
                      text,
                      myDocument.getLineStartOffset(logicalLine),
                      myDocument.getLineEndOffset(logicalLine));
                  currentLogicalLine[0] = logicalLine;
                }
                paintWhitespace(
                    g, text, xStart, y, start, end, whitespacePaintingStrategy, fragment);
              }
              if (attributes != null
                  && hasTextEffect(attributes.getEffectColor(), attributes.getEffectType())) {
                paintTextEffect(
                    g, xStart, xEnd, y, attributes.getEffectColor(), attributes.getEffectType());
              }
            }

            @Override
            public void paintAfterLineEnd(
                Graphics2D g,
                Rectangle clip,
                IterationState iterationState,
                int columnStart,
                float x,
                int y) {
              int offset = iterationState.getEndOffset();
              SoftWrapModelImpl softWrapModel = myEditor.getSoftWrapModel();
              if (softWrapModel.getSoftWrap(offset) == null) {
                int logicalLine = myDocument.getLineNumber(offset);
                paintLineExtensions(g, logicalLine, x, y);
              } else {
                softWrapModel.paint(
                    g,
                    SoftWrapDrawingType.BEFORE_SOFT_WRAP_LINE_FEED,
                    (int) x,
                    y - myView.getAscent(),
                    myView.getLineHeight());
              }
            }
          });
    }
  }