|
| 1 | +package net.nightwhistler.htmlspanner.spans; |
| 2 | + |
| 3 | +import android.graphics.Canvas; |
| 4 | +import android.graphics.Color; |
| 5 | +import android.graphics.Paint; |
| 6 | +import android.text.style.LineBackgroundSpan; |
| 7 | +import android.util.Log; |
| 8 | + |
| 9 | +import net.nightwhistler.htmlspanner.HtmlSpanner; |
| 10 | +import net.nightwhistler.htmlspanner.style.Style; |
| 11 | +import net.nightwhistler.htmlspanner.style.StyleValue; |
| 12 | + |
| 13 | +/** |
| 14 | + * Created with IntelliJ IDEA. |
| 15 | + * User: alex |
| 16 | + * Date: 6/23/13 |
| 17 | + * Time: 3:35 PM |
| 18 | + * To change this template use File | Settings | File Templates. |
| 19 | + */ |
| 20 | +public class HorizontalLineSpan implements LineBackgroundSpan { |
| 21 | + |
| 22 | + private int start; |
| 23 | + private int end; |
| 24 | + |
| 25 | + private Style style; |
| 26 | + |
| 27 | + public HorizontalLineSpan(Style style, int start, int end) { |
| 28 | + this.start = start; |
| 29 | + this.end = end; |
| 30 | + |
| 31 | + this.style = style; |
| 32 | + } |
| 33 | + |
| 34 | + |
| 35 | + @Override |
| 36 | + public void drawBackground(Canvas c, Paint p, |
| 37 | + int left, int right, |
| 38 | + int top, int baseline, int bottom, |
| 39 | + CharSequence text, int start, int end, |
| 40 | + int lnum) { |
| 41 | + |
| 42 | + int baseMargin = 0; |
| 43 | + |
| 44 | + if ( style.getMarginLeft() != null ) { |
| 45 | + StyleValue styleValue = style.getMarginLeft(); |
| 46 | + |
| 47 | + if ( styleValue.getUnit() == StyleValue.Unit.PX ) { |
| 48 | + if ( styleValue.getIntValue() > 0 ) { |
| 49 | + baseMargin = styleValue.getIntValue(); |
| 50 | + } |
| 51 | + } else if ( styleValue.getFloatValue() > 0f ) { |
| 52 | + baseMargin = (int) (styleValue.getFloatValue() * HtmlSpanner.HORIZONTAL_EM_WIDTH); |
| 53 | + } |
| 54 | + |
| 55 | + //Leave a little bit of room |
| 56 | + baseMargin--; |
| 57 | + } |
| 58 | + |
| 59 | + if ( baseMargin > 0 ) { |
| 60 | + left = left + baseMargin; |
| 61 | + } |
| 62 | + |
| 63 | + int originalColor = p.getColor(); |
| 64 | + float originalStrokeWidth = p.getStrokeWidth(); |
| 65 | + |
| 66 | + p.setColor(Color.parseColor("#000000")); |
| 67 | + if (style.getBorderColor() != null ) { |
| 68 | + p.setColor( style.getBorderColor() ); |
| 69 | + } |
| 70 | + |
| 71 | + int strokeWidth; |
| 72 | + |
| 73 | + if ( style.getBorderWidth() != null && style.getBorderWidth().getUnit() == StyleValue.Unit.PX ) { |
| 74 | + strokeWidth = style.getBorderWidth().getIntValue(); |
| 75 | + } else { |
| 76 | + strokeWidth = 1; |
| 77 | + } |
| 78 | + |
| 79 | + p.setStrokeWidth( strokeWidth ); |
| 80 | + right -= strokeWidth; |
| 81 | + |
| 82 | + p.setStyle(Paint.Style.STROKE); |
| 83 | + |
| 84 | + Log.d("HorizontalSpan", "Drawing line"); |
| 85 | + |
| 86 | + int center=(bottom+top)/2; |
| 87 | + c.drawLine(left, center, right, center, p); |
| 88 | + |
| 89 | + p.setColor(originalColor); |
| 90 | + p.setStrokeWidth(originalStrokeWidth); |
| 91 | + } |
| 92 | + |
| 93 | + |
| 94 | + |
| 95 | +} |
| 96 | + |
0 commit comments