package com.zzhoujay.markdown.style;

import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.text.style.ReplacementSpan;

/**
 * Created by zhou on 16-7-2.
 * 代码Span
 */
public class CodeSpan extends ReplacementSpan {

    private static final float radius = 10;

    private Drawable drawable;
    private float padding;
    private int width;

    public CodeSpan(int color) {
        GradientDrawable d = new GradientDrawable();
        d.setColor(color);
        d.setCornerRadius(radius);
        drawable = d;
    }

    @Override
    public int getSize(Paint paint, CharSequence text, int start, int end, Paint.FontMetricsInt fm) {
        paint.setTypeface(Typeface.MONOSPACE);
        padding = paint.measureText("t") * 0.5f;
        width = (int) (paint.measureText(text, start, end) + padding * 2);
        return width;
    }

    @Override
    public void draw(Canvas canvas, CharSequence text, int start, int end, float x, int top, int y, int bottom, Paint paint) {
        paint.setTypeface(Typeface.MONOSPACE);
        drawable.setBounds((int) x, top, (int) x + width, bottom);
        drawable.draw(canvas);
        canvas.drawText(text, start, end, x + padding, y, paint);
    }
}

@@ -398,22 +398,21 @@
 oat 
-radius
+RADIUS
  = 10;%0A
-%0A
     
@@ -423,52 +423,237 @@
 ate 
-Drawable drawable;%0A    private float padding
+static final float PADDING_HORIZONTAL = 16;%0A    private static final float PADDING_VERTICAL = 2;%0A    private static final float MARGIN = 8;%0A    private static final float TEXT_SIZE_SCALE = 0.92f;%0A%0A    private Drawable mBackground
 ;%0A  
@@ -666,22 +666,48 @@
 ate int 
-w
+mW
 idth;
+%0A    private int mHeight;
 %0A%0A    pu
@@ -843,14 +843,14 @@
 ius(
-radius
+RADIUS
 );%0A 
@@ -856,24 +856,27 @@
         
-drawable
+mBackground
  = d;%0A  
@@ -1007,96 +1007,230 @@
     
+ mHeight =
  paint.
-setTypeface(Typeface.MONOSPACE);%0A        padding = paint.measureText(%22t%22) * 0.5f;
+getFontMetricsInt().descent - paint.getFontMetricsInt().ascent;%0A%0A        float size = paint.getTextSize();%0A        paint.setTextSize(size * TEXT_SIZE_SCALE);%0A        paint.setTypeface(Typeface.MONOSPACE);%0A
 %0A   
@@ -1234,17 +1234,18 @@
         
-w
+mW
 idth = (
@@ -1292,19 +1292,196 @@
 ) + 
-padding * 2
+PADDING_HORIZONTAL * 2 + MARGIN * 2);%0A        if (fm != null) %7B%0A            fm.top -= PADDING_VERTICAL;%0A            fm.bottom += PADDING_VERTICAL;%0A        %7D%0A%0A        paint.setTextSize(size
 );%0A 
@@ -1494,17 +1494,18 @@
  return 
-w
+mW
 idth;%0A  
@@ -1646,24 +1646,117 @@
 nt paint) %7B%0A
+        float size = paint.getTextSize();%0A        paint.setTextSize(size * TEXT_SIZE_SCALE);%0A
         pain
@@ -1790,24 +1790,25 @@
 SPACE);%0A
+%0A
         
 drawable
@@ -1799,24 +1799,27 @@
         
-drawable
+mBackground
 .setBoun
@@ -1831,22 +1831,60 @@
 nt) 
-x, top
+(x + MARGIN), (int) (top - PADDING_VERTICAL)
 , (int) 
 x + 
@@ -1883,122 +1883,266 @@
 nt) 
+(
 x + 
-w
+mW
 idth
-, bottom);%0A        drawable.draw(canvas);%0A        canvas.drawText(text, start, end, x + padding, y, paint
+ - MARGIN), (int) (top + mHeight + PADDING_VERTICAL));%0A        mBackground.draw(canvas);%0A        canvas.drawText(text, start, end, x + MARGIN + PADDING_HORIZONTAL, y - mHeight * (1 - TEXT_SIZE_SCALE) * 0.5f, paint);%0A%0A        paint.setTextSize(size
 );%0A 

improve CodeSpan