Skip to content

Commit eb52a44

Browse files
author
Salvatore Ranieri
committed
fixed line height size
1 parent 90d562c commit eb52a44

5 files changed

Lines changed: 39 additions & 24 deletions

File tree

html-textview-master/HtmlSpanner/src/main/java/net/nightwhistler/htmlspanner/HtmlSpanner.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public class HtmlSpanner {
7171
* Temporary constant for the width of 1 horizontal em
7272
* Used for calculating margins.
7373
*/
74-
public static final int HORIZONTAL_EM_WIDTH = 5;
74+
public static final int HORIZONTAL_EM_WIDTH = 10;
7575

7676

7777
private Map<String, TagNodeHandler> handlers;
@@ -88,6 +88,8 @@ public class HtmlSpanner {
8888

8989
private float textSize;
9090

91+
private boolean textAlignCenter;
92+
9193
/**
9294
* Switch to determine if CSS is used
9395
*/
@@ -298,7 +300,7 @@ public Spannable fromHtml(String html) {
298300
html=replaceHtmlTags(html);
299301
}
300302
}
301-
Log.i("HTML_UPDATE",html);
303+
Log.i("HTML_UPDATE_1",html);
302304
return fromTagNode(this.htmlCleaner.clean(html), null);
303305
}
304306

html-textview-master/HtmlSpanner/src/main/java/net/nightwhistler/htmlspanner/spans/LineHeightSpanImpl.java

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -33,26 +33,31 @@ public void chooseHeight(CharSequence text, int start, int end, int spanstartv,
3333
Log.i("fmTop",""+fm.top);
3434
Log.i("fmBottom",""+fm.bottom);
3535

36-
if(ascent==0&&descent==0){
37-
38-
int top = Math.abs(fm.top)-Math.abs(fm.ascent);
39-
int bottom= Math.abs(fm.bottom)-Math.abs(fm.descent);
40-
int lineHeight = Math.abs(fm.ascent)-fm.descent;
41-
42-
mSize = mSize-lineHeight > 0 ? mSize-lineHeight : 0;
43-
44-
Log.i("mSize",""+mSize);
45-
46-
ascent=fm.top -= mSize/2;
47-
descent=fm.bottom += mSize/2;
48-
49-
fm.top -= top;
50-
fm.bottom += bottom;
51-
}
52-
53-
if(mSize > 0) {
54-
fm.ascent = ascent;
55-
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);
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;
5661
}
5762
}
5863

html-textview-master/HtmlSpanner/src/main/java/net/nightwhistler/htmlspanner/style/StyleCallback.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package net.nightwhistler.htmlspanner.style;
22

3+
import android.content.res.Resources;
34
import android.text.Spannable;
45
import android.text.SpannableStringBuilder;
56
import android.text.style.*;
7+
import android.util.DisplayMetrics;
68
import android.util.Log;
79
import net.nightwhistler.htmlspanner.FontFamily;
810
import net.nightwhistler.htmlspanner.HtmlSpanner;
@@ -26,6 +28,8 @@ public class StyleCallback implements SpanCallback {
2628
private FontFamily defaultFont;
2729
private Style useStyle;
2830

31+
private static final float SCREEN_DENSITY=(Resources.getSystem().getDisplayMetrics().densityDpi)/ DisplayMetrics.DENSITY_DEFAULT;
32+
2933
public StyleCallback( FontFamily defaultFont, Style style, int start, int end ) {
3034
this.defaultFont = defaultFont;
3135
this.useStyle = style;
@@ -78,7 +82,7 @@ public void applySpan(HtmlSpanner spanner, SpannableStringBuilder builder) {
7882
//If there's a line height, we use an implementation of LineHeightSpan to draw space behind the text
7983
if ( useStyle.getLineHeight() != null) {
8084
//Log.d("StyleCallback", "Applying LineHeightSpan with value " + useStyle.getLineHeight().getIntValue() + " from " + start + " to " + end + " on text " + builder.subSequence(start, end));
81-
builder.setSpan(new LineHeightSpanImpl(useStyle.getLineHeight().getIntValue()),start,end,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
85+
builder.setSpan(new LineHeightSpanImpl((int)Math.ceil(useStyle.getLineHeight().getIntValue() * SCREEN_DENSITY)),start,end,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
8286
}
8387

8488
//If there is a border, the BorderSpan will also draw the background colour if needed.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<h1>That's a header 1</h1><h2>That's a header 2</h2><h3>That's a header 3</h3><h4>That's a header 4</h4><h5>That's a header 5</h5><h6>That's a header 6</h6>NomeCognome<br />MarioRossi<div style="text-align:center">Some text displayed in center position into a paragraph <span style="background-color:blue; color:white">other text</span> (<code>span</code> markup used);<br /><big>big thing</big> (<code>big</code> markup used)<br /><small>this is not that important</small> (<code>small</code> markup used)<br />this is <em>very</em> simple (<code>em</code> markup used for emphasizing a word)<br /><cite>Origin of Species</cite> (a book title;<code>cite</code> markup used)<br /><em>Homo sapiens</em> (should appear in italics; <code>i</code> markup used)<br /><br /><strong>bolded</strong> (<code>b</code> markup used - just bolding with unspecified semantics)<div style="line-height:24px"><br />&nbsp;</div><tt>text in monospace font</tt> (<code>tt</code> markup used)<br /><u>underlined</u> text (<code>u</code> markup used)<br /><strong>bolded</strong> (<code>strong</code> markup used)<ul><li>Test: <span>large size</span> (<code>font size=30px</code> markup used)</li><li>Test: <span style="color:red">red text</span> (<code>font color=red</code> markup used)</li></ul>a<sub>B</sub> (<code>sub</code> markup used)<br />a<sup>C</sup> (<code>sup</code> markup used)</div><ol><li>list element 1</li><li>list element 2</li></ol><div style="line-height:50px">that's a div body and a line-height<br />with a <code>br</code> inside and a font styling<br /><span style="font-size:30px">large size</span> (<code>style=&quot;font-size:30px&quot;</code> markup used)<br /><span style="font-family:Courier">Courier font</span> (<code>style=&quot;font-family:Courier&quot;</code> markup used)<br /><span style="color:red">red text</span> (<code>style=&quot;color:red&quot;</code> markup used)<br /><em>italic text</em> (<code>style=&quot;font-style:italic&quot;</code> markup used)<p style="text-align:left">left text</p><p style="text-align:center">centered text</p><p style="text-align:right">right text</p>deleted text</div>
1+
<h1>That's a header 1</h1><h2>That's a header 2</h2><h3>That's a header 3</h3><h4>That's a header 4</h4><h5>That's a header 5</h5><h6>That's a header 6</h6>NomeCognome<br />MarioRossi<div style="line-height:30px">Some text displayed in center position into a paragraph <span style="background-color:blue; color:white">other text</span> (<code>span</code> markup used);<br /><big>big thing</big> (<code>big</code> markup used)<br /><small>this is not that important</small> (<code>small</code> markup used)<br />this is <em>very</em> simple (<code>em</code> markup used for emphasizing a word)<br /><cite>Origin of Species</cite> (a book title;<code>cite</code> markup used)<br /><em>Homo sapiens</em> (should appear in italics; <code>i</code> markup used)<br /><br /><strong>bolded</strong> (<code>b</code> markup used - just bolding with unspecified semantics)<div style="line-height:24px"><br />&nbsp;</div><tt>text in monospace font</tt> (<code>tt</code> markup used)<br /><u>underlined</u> text (<code>u</code> markup used)<br /><strong>bolded</strong> (<code>strong</code> markup used)<ul><li>Test: <span>large size</span> (<code>font size=30px</code> markup used)</li><li>Test: <span style="color:red">red text</span> (<code>font color=red</code> markup used)</li></ul>a<sub>B</sub> (<code>sub</code> markup used)<br />a<sup>C</sup> (<code>sup</code> markup used)</div><ol style="list-style-position:inside;"><li>list element 1</li><li>list element 2</li></ol><div style="line-height:50px">that's a div body and a line-height<br />with a <code>br</code> inside and a font styling<br /><span style="font-size:30px">large size</span> (<code>style=&quot;font-size:30px&quot;</code> markup used)<br /><span style="font-family:Courier">Courier font</span> (<code>style=&quot;font-family:Courier&quot;</code> markup used)<br /><span style="color:red">red text</span> (<code>style=&quot;color:red&quot;</code> markup used)<br /><em>italic text</em> (<code>style=&quot;font-style:italic&quot;</code> markup used)<p style="text-align:left">left text</p><p style="text-align:center">centered text</p><p style="text-align:right">right text</p>deleted text</div>

html-textview-master/example/src/main/java/org/sufficientlysecure/htmltextview/example/MainActivity.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,13 @@
1818

1919
import android.app.Activity;
2020
import android.content.Context;
21+
import android.graphics.Paint;
2122
import android.os.Bundle;
23+
import android.text.Layout;
2224
import android.text.method.LinkMovementMethod;
2325
import android.util.Log;
26+
import android.view.Gravity;
27+
import android.view.View;
2428
import android.widget.TextView;
2529

2630
import net.nightwhistler.htmlspanner.HtmlSpanner;

0 commit comments

Comments
 (0)