package ru.noties.markwon.spans;

import android.graphics.Canvas;
import android.graphics.Paint;
import android.support.annotation.NonNull;
import android.text.Layout;
import android.text.style.LeadingMarginSpan;

public class OrderedListItemSpan implements LeadingMarginSpan {

    private final SpannableTheme theme;
    private final String number;

    public OrderedListItemSpan(
            @NonNull SpannableTheme theme,
            @NonNull String number
    ) {
        this.theme = theme;
        this.number = number;
    }

    @Override
    public int getLeadingMargin(boolean first) {
        return theme.getBlockMargin();
    }

    @Override
    public void drawLeadingMargin(Canvas c, Paint p, int x, int dir, int top, int baseline, int bottom, CharSequence text, int start, int end, boolean first, Layout layout) {

        // if there was a line break, we don't need to draw anything
        if (!first
                || !LeadingMarginUtils.selfStart(start, text, this)) {
            return;
        }

        theme.applyListItemStyle(p);

        final int width = theme.getBlockMargin();
        final int numberWidth = (int) (p.measureText(number) + .5F);

        final int left;
        if (dir > 0) {
            left = x + (width * dir) - numberWidth;
        } else {
            left = x + (width * dir) + (width - numberWidth);
        }

        final float numberY = CanvasUtils.textCenterY(top, bottom, p);

        c.drawText(number, left, numberY, p);
    }
}

@@ -346,16 +346,199 @@
 umber;%0A%0A
+    // we will use this variable to check if our order number text exceeds block margin,%0A    // so we will use it instead of block margin%0A    // @since 1.0.3%0A    private int margin;%0A%0A
     publ
@@ -561,16 +561,16 @@
 emSpan(%0A
-
         
@@ -783,22 +783,68 @@
         
-return
+// @since 1.0.3%0A        return margin %3E 0 ? margin :
  theme.g
@@ -1289,58 +1289,8 @@
 );%0A%0A
-        final int width = theme.getBlockMargin();%0A
     
@@ -1351,24 +1351,246 @@
 r) + .5F);%0A%0A
+        // @since 1.0.3%0A        int width = theme.getBlockMargin();%0A        if (numberWidth %3E width) %7B%0A            width = numberWidth;%0A            margin = numberWidth;%0A        %7D else %7B%0A            margin = 0;%0A        %7D%0A%0A
         fina

Fix for ordered list items to be greater than theme block margin (#22)