Skip to content

Commit b3a7a31

Browse files
committed
Added support for underline and hr
I added support for underline tag (<u>), text-decoration(line-through, underline) and tag <hr/>.
1 parent f1266bc commit b3a7a31

19 files changed

Lines changed: 394 additions & 179 deletions

File tree

html-textview-master/HtmlSpanner/build.gradle

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ buildscript {
77
}
88
}
99

10-
apply plugin: 'android-library'
10+
apply plugin: 'com.android.library'
1111

1212
repositories {
1313
mavenCentral()
@@ -22,12 +22,12 @@ dependencies {
2222
}
2323

2424
android {
25-
compileSdkVersion 17
25+
compileSdkVersion 19
2626
buildToolsVersion "19.1.0"
2727

2828
defaultConfig {
29-
minSdkVersion 7
30-
targetSdkVersion 17
29+
minSdkVersion 9
30+
targetSdkVersion 19
3131
}
3232
}
3333

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,14 @@
2222
import java.util.HashMap;
2323
import java.util.Map;
2424

25+
import android.graphics.Color;
2526
import android.util.Log;
2627
import net.nightwhistler.htmlspanner.exception.ParsingCancelledException;
2728
import net.nightwhistler.htmlspanner.handlers.*;
2829
import net.nightwhistler.htmlspanner.handlers.attributes.AlignmentAttributeHandler;
2930

3031
import net.nightwhistler.htmlspanner.handlers.attributes.BorderAttributeHandler;
32+
import net.nightwhistler.htmlspanner.handlers.attributes.HorizontalLineHandler;
3133
import net.nightwhistler.htmlspanner.handlers.attributes.StyleAttributeHandler;
3234
import net.nightwhistler.htmlspanner.style.Style;
3335
import net.nightwhistler.htmlspanner.handlers.StyledTextHandler;
@@ -370,6 +372,8 @@ private void registerBuiltInHandlers() {
370372

371373
registerHandler("b", boldHandler);
372374
registerHandler("strong", boldHandler);
375+
//Underline added
376+
registerHandler("u",new UnderlineHandler());
373377

374378
TagNodeHandler marginHandler = new StyledTextHandler(
375379
new Style().setMarginLeft(new StyleValue(2.0f, StyleValue.Unit.EM)));
@@ -395,6 +399,18 @@ private void registerBuiltInHandlers() {
395399

396400
Style.BorderStyle borderStyle = Style.BorderStyle.valueOf("solid".toUpperCase());
397401

402+
//HR handler
403+
Style hrStyle = new Style()
404+
.setDisplayStyle(Style.DisplayStyle.BLOCK)
405+
.setMarginBottom(
406+
new StyleValue(1.0f, StyleValue.Unit.EM))
407+
.setBorderStyle(borderStyle).setBorderColor(Color.parseColor("#000000")).setBackgroundColor(backgroundColor);
408+
409+
410+
TagNodeHandler hrHandler = new HorizontalLineHandler(wrap(new StyledTextHandler(hrStyle)));
411+
412+
registerHandler("hr", hrHandler);
413+
398414
Style paragraphStyle = new Style()
399415
.setDisplayStyle(Style.DisplayStyle.BLOCK)
400416
.setMarginBottom(

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import android.text.Spannable;
1919
import android.text.Spanned;
2020
import android.text.style.ForegroundColorSpan;
21+
import android.text.style.UnderlineSpan;
2122
import android.util.Log;
2223
import net.nightwhistler.htmlspanner.spans.FontFamilySpan;
2324
import net.nightwhistler.htmlspanner.spans.LineHeightSpanImpl;

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

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package net.nightwhistler.htmlspanner.css;
22

3+
import android.content.res.Resources;
34
import android.graphics.Color;
45
import android.util.Log;
56
import com.osbcp.cssparser.PropertyValue;
@@ -25,6 +26,8 @@
2526
*/
2627
public class CSSCompiler {
2728

29+
private static final int SCREEN_DENSITY=160;
30+
2831
public static interface StyleUpdater {
2932
Style updateStyle( Style style, HtmlSpanner spanner );
3033
}
@@ -227,6 +230,28 @@ public Style updateStyle(Style style, HtmlSpanner spanner) {
227230
}
228231
}
229232

233+
//Text decoration definition
234+
if ( "text-decoration".equals(key)) {
235+
try {
236+
String temp_value=value;
237+
if(temp_value.equals("line-through")){
238+
temp_value="linethrough";
239+
}
240+
final Style.TextDecoration textDecoration = Style.TextDecoration.valueOf(temp_value.toUpperCase());
241+
return new StyleUpdater() {
242+
@Override
243+
public Style updateStyle(Style style, HtmlSpanner spanner) {
244+
Log.d("CSSCompiler", "Applying style " + key + ": " + value );
245+
return style.setTextDecoration(textDecoration);
246+
}
247+
};
248+
249+
} catch ( IllegalArgumentException i ) {
250+
Log.e("CSSCompiler", "Can't parse alignment: " + value);
251+
return null;
252+
}
253+
}
254+
230255
if ( "font-weight".equals(key)) {
231256

232257
try {
@@ -285,6 +310,18 @@ public Style updateStyle(Style style, HtmlSpanner spanner) {
285310

286311
if ( styleValue != null ) {
287312

313+
314+
if(styleValue.getUnit().equals(StyleValue.Unit.PX)){
315+
Log.i("value","int:"+styleValue.getIntValue());
316+
int dp=(int) ((styleValue.getIntValue()) / SCREEN_DENSITY);
317+
styleValue.setIntValue(dp);
318+
}
319+
// else{
320+
// Log.i("value"," float:"+styleValue.getFloatValue());
321+
// float f=styleValue.getFloatValue();
322+
// int dp=(int) (f / SCREEN_DENSITY);
323+
// styleValue.setFloatValue(Float.parseFloat(""+dp));
324+
// }
288325
return new StyleUpdater() {
289326
@Override
290327
public Style updateStyle(Style style, HtmlSpanner spanner) {
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Copyright (C) 2011 Alex Kuiper <http://www.nightwhistler.net>
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package net.nightwhistler.htmlspanner.handlers;
17+
18+
19+
import android.text.SpannableStringBuilder;
20+
import android.text.style.URLSpan;
21+
import android.text.style.UnderlineSpan;
22+
23+
import net.nightwhistler.htmlspanner.SpanStack;
24+
import net.nightwhistler.htmlspanner.TagNodeHandler;
25+
26+
import org.htmlcleaner.TagNode;
27+
28+
/**
29+
* Creates clickable links.
30+
*
31+
* @author Alex Kuiper
32+
*
33+
*/
34+
public class UnderlineHandler extends TagNodeHandler {
35+
36+
@Override
37+
public void handleTagNode(TagNode node, SpannableStringBuilder builder,
38+
int start, int end, SpanStack spanStack) {
39+
40+
spanStack.pushSpan(new UnderlineSpan(), start, end);
41+
}
42+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package net.nightwhistler.htmlspanner.handlers.attributes;
2+
3+
import android.text.SpannableStringBuilder;
4+
import android.util.Log;
5+
6+
import net.nightwhistler.htmlspanner.SpanStack;
7+
import net.nightwhistler.htmlspanner.handlers.StyledTextHandler;
8+
import net.nightwhistler.htmlspanner.spans.BorderSpan;
9+
import net.nightwhistler.htmlspanner.spans.HorizontalLineSpan;
10+
import net.nightwhistler.htmlspanner.style.Style;
11+
12+
import org.htmlcleaner.TagNode;
13+
14+
/**
15+
* Created with IntelliJ IDEA.
16+
* User: alex
17+
* Date: 6/23/13
18+
* Time: 3:36 PM
19+
* To change this template use File | Settings | File Templates.
20+
*/
21+
public class HorizontalLineHandler extends WrappingStyleHandler {
22+
23+
public HorizontalLineHandler(StyledTextHandler handler) {
24+
super(handler);
25+
}
26+
27+
@Override
28+
public void handleTagNode(TagNode node, SpannableStringBuilder builder, int start, int end,
29+
Style useStyle, SpanStack spanStack) {
30+
31+
end+=1;
32+
Log.d("HorizontalLineHandler", "Draw hr from " + start + " to " + end);
33+
spanStack.pushSpan(new HorizontalLineSpan(useStyle, start, end), start, end);
34+
appendNewLine(builder);
35+
36+
/*super.handleTagNode(node, builder, start, end, useStyle, spanStack);*/
37+
38+
}
39+
40+
41+
}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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

Comments
 (0)