|
1 | 1 | package net.nightwhistler.htmlspanner.spans; |
2 | 2 |
|
3 | 3 | import android.graphics.Paint; |
| 4 | +import android.graphics.Rect; |
| 5 | +import android.text.TextPaint; |
4 | 6 | import android.text.style.LineHeightSpan; |
| 7 | +import android.util.Log; |
5 | 8 |
|
6 | 9 | /** |
7 | 10 | * Created by Salvatore on 06/12/2017. |
8 | 11 | */ |
9 | 12 |
|
10 | 13 | public class LineHeightSpanImpl implements LineHeightSpan { |
11 | 14 |
|
12 | | - private final int value; |
13 | | - private int ascent,descent; |
| 15 | + //private final int value; |
| 16 | + private int mSize; |
| 17 | + private static float sProportion = 0; |
| 18 | + private int ascent=0; |
| 19 | + private int descent=0; |
| 20 | + |
14 | 21 |
|
15 | 22 | public LineHeightSpanImpl(int value) { |
16 | | - this.value = value; |
17 | | - ascent=descent=0; |
| 23 | + //this.value = value; |
| 24 | + mSize=value; |
18 | 25 | } |
19 | 26 |
|
20 | 27 | @Override |
21 | 28 | public void chooseHeight(CharSequence text, int start, int end, int spanstartv, int v, |
22 | 29 | Paint.FontMetricsInt fm) { |
23 | 30 |
|
24 | | - if(ascent==0&&descent==0){ |
25 | | - ascent=fm.ascent -= value/2; |
26 | | - descent=fm.descent += value/2; |
27 | | - } |
| 31 | + Log.i("fmAscent",""+fm.ascent); |
| 32 | + Log.i("fmDescent",""+fm.descent); |
| 33 | + Log.i("fmTop",""+fm.top); |
| 34 | + Log.i("fmBottom",""+fm.bottom); |
28 | 35 |
|
29 | | - fm.ascent=ascent; |
30 | | - fm.descent=descent; |
| 36 | + if (fm.descent > mSize) { |
| 37 | + // Show as much descent as possible |
| 38 | + fm.bottom = fm.descent = Math.min(mSize, fm.descent); |
| 39 | + fm.top = fm.ascent = 0; |
| 40 | + } else if (-fm.ascent + fm.descent > mSize) { |
| 41 | + // Show all descent, and as much ascent as possible |
| 42 | + fm.bottom = fm.descent; |
| 43 | + fm.top = fm.ascent = -mSize + fm.descent; |
| 44 | + } else if (-fm.ascent + fm.bottom > mSize) { |
| 45 | + // Show all ascent, descent, as much bottom as possible |
| 46 | + fm.top = fm.ascent; |
| 47 | + fm.bottom = fm.ascent + mSize; |
| 48 | + } else if (-fm.top + fm.bottom > mSize) { |
| 49 | + // Show all ascent, descent, bottom, as much top as possible |
| 50 | + fm.top = fm.bottom - mSize; |
| 51 | + } else { |
| 52 | + // Show proportionally additional ascent / top & descent / bottom |
| 53 | + final int additional = mSize - (-fm.top + fm.bottom); |
31 | 54 |
|
| 55 | + // Round up for the negative values and down for the positive values (arbritary choice) |
| 56 | + // So that bottom - top equals additional even if it's an odd number. |
| 57 | + fm.top -= Math.ceil(additional / 2.0f); |
| 58 | + fm.bottom += Math.floor(additional / 2.0f); |
| 59 | + fm.ascent = fm.top; |
| 60 | + fm.descent = fm.bottom; |
| 61 | + } |
32 | 62 | } |
33 | 63 |
|
34 | 64 | } |
0 commit comments