Skip to content

Commit 665fd5d

Browse files
authored
Merge pull request #4 from SysdataSpA/develop
Develop
2 parents 43684db + 7a96ee0 commit 665fd5d

6 files changed

Lines changed: 67 additions & 40 deletions

File tree

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

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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
*/
@@ -153,7 +155,6 @@ public class HtmlSpanner {
153155
htmlTagsDictionary.put("Ú", "Ú");
154156
htmlTagsDictionary.put("<h1>","<h1 style=\"font-weight:bold\">");
155157
htmlTagsDictionary.put("<h2>","<h2 style=\"font-weight:bold\">");
156-
htmlTagsDictionary.put("<ul>","<br><ul>");
157158
}
158159

159160

@@ -204,6 +205,10 @@ public void setTextSize(float textSize) {
204205
this.textSize = textSize;
205206
}
206207

208+
public float getTextSize() {
209+
return textSize;
210+
}
211+
207212
/**
208213
* Switch to specify whether excess whitespace should be stripped from the
209214
* input.
@@ -295,7 +300,7 @@ public Spannable fromHtml(String html) {
295300
html=replaceHtmlTags(html);
296301
}
297302
}
298-
Log.i("HTML",html);
303+
Log.i("HTML_UPDATE_1",html);
299304
return fromTagNode(this.htmlCleaner.clean(html), null);
300305
}
301306

@@ -471,8 +476,12 @@ private void registerBuiltInHandlers() {
471476
new Style().setMarginLeft(new StyleValue(2.0f, StyleValue.Unit.EM)));
472477

473478
registerHandler("blockquote", marginHandler);
474-
registerHandler("ul", marginHandler);
475-
registerHandler("ol", marginHandler);
479+
480+
TagNodeHandler listHandler = new StyledTextHandler(new Style()
481+
.setDisplayStyle(Style.DisplayStyle.BLOCK));
482+
483+
registerHandler("ul", listHandler);
484+
registerHandler("ol", listHandler);
476485

477486
TagNodeHandler monSpaceHandler = wrap(new MonoSpaceHandler());
478487

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,64 @@
11
package net.nightwhistler.htmlspanner.spans;
22

33
import android.graphics.Paint;
4+
import android.graphics.Rect;
5+
import android.text.TextPaint;
46
import android.text.style.LineHeightSpan;
7+
import android.util.Log;
58

69
/**
710
* Created by Salvatore on 06/12/2017.
811
*/
912

1013
public class LineHeightSpanImpl implements LineHeightSpan {
1114

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+
1421

1522
public LineHeightSpanImpl(int value) {
16-
this.value = value;
17-
ascent=descent=0;
23+
//this.value = value;
24+
mSize=value;
1825
}
1926

2027
@Override
2128
public void chooseHeight(CharSequence text, int start, int end, int spanstartv, int v,
2229
Paint.FontMetricsInt fm) {
2330

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);
2835

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);
3154

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+
}
3262
}
3363

3464
}

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 & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +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
2-
<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
3-
thing</big> (<code>big</code> markup used)<br/>
4-
<small>this is not that important</small>
5-
(<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
6-
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)
7-
<div style="line-height:24px"><br/>&nbsp;</div>
8-
<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)
9-
<ul>
10-
<li>Test: <span>large size</span> (<code>font size=30px</code> markup used)</li>
11-
<li>Test: <span style="color:red">red text</span> (<code>font color=red</code> markup used)</li>
12-
</ul>
13-
a<sub>B</sub> (<code>sub</code> markup used)<br/>a<sup>C</sup> (<code>sup</code> markup used)
14-
</div>
15-
<ol>
16-
<li>list element 1</li>
17-
<li>list element 2</li>
18-
</ol>
19-
<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>
20-
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>
21-
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>
22-
<p style="text-align:center">centered text</p>
23-
<p style="text-align:right">right text</p>deleted text
24-
</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: 6 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;
@@ -40,6 +44,8 @@ protected void onCreate(Bundle savedInstanceState) {
4044
String html=loadStringFromAssetFile(this,"example.html");
4145
TextView tv = (TextView) findViewById(R.id.twxt);
4246
int col=tv.getSolidColor();
47+
int fontHeight = tv.getPaint().getFontMetricsInt(null);
48+
Log.i("font height",""+fontHeight);
4349
HtmlSpanner htmlSpanner=new HtmlSpanner(tv.getTextColors().getDefaultColor(),tv.getTextSize());
4450
htmlSpanner.setBackgroundColor(col);
4551
tv.setText(htmlSpanner.fromHtml(html));

html-textview-master/example/src/main/res/layout/activity_main.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
<TextView
1818
android:layout_width="match_parent"
1919
android:layout_height="wrap_content"
20-
android:id="@+id/twxt"/>
20+
android:id="@+id/twxt"
21+
android:textAlignment="center"/>
2122

2223
<View
2324
android:layout_width="match_parent"

0 commit comments

Comments
 (0)