Skip to content

Commit 793358f

Browse files
authored
Merge pull request #13 from SysdataSpA/feature/list_fix
Feature/list fix
2 parents a20001b + a6f699a commit 793358f

File tree

13 files changed

+282
-87
lines changed

13 files changed

+282
-87
lines changed

.gitignore

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,3 @@
1-
# Built application files
2-
*.apk
3-
*.ap_
4-
5-
# Files for the ART/Dalvik VM
6-
*.dex
7-
8-
# Java class files
9-
*.class
10-
111
# Generated files
122
bin/
133
gen/
@@ -34,22 +24,4 @@ captures/
3424

3525
# Intellij
3626
*.iml
37-
.idea/workspace.xml
38-
.idea/tasks.xml
39-
.idea/gradle.xml
40-
.idea/dictionaries
41-
.idea/libraries
42-
43-
# Keystore files
44-
*.jks
45-
46-
# External native build folder generated in Android Studio 2.2 and later
47-
.externalNativeBuild
48-
49-
# Google Services (e.g. APIs or Firebase)
50-
google-services.json
51-
52-
# Freeline
53-
freeline.py
54-
freeline/
55-
freeline_project_description.json
27+
.idea/

HtmlSpanner/src/main/java/com/sysdata/htmlspanner/HtmlSpanner.java

Lines changed: 89 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,10 @@ public class HtmlSpanner {
9292

9393
private float textSize;
9494

95-
private boolean textAlignCenter;
95+
/**
96+
* This parameter is used to modify table header text centering, by default is true
97+
*/
98+
private Boolean tableHeaderCenter = null;
9699

97100
/**
98101
* Switch to determine if CSS is used
@@ -177,18 +180,35 @@ public HtmlSpanner(int textColor,float textSize) {
177180
* @param cleaner
178181
*/
179182
public HtmlSpanner(HtmlCleaner cleaner, FontResolver fontResolver,int textColor, float textSize) {
183+
initBaseComponents(cleaner, fontResolver);
184+
setTextColor(textColor);
185+
setTextSize(textSize);
186+
calculateBaseDimensions(textSize);
187+
registerBuiltInHandlers();
188+
}
189+
190+
private HtmlSpanner(Builder builder) {
191+
initBaseComponents(createHtmlCleaner(), new SystemFontResolver());
192+
setTextColor(builder.textColor);
193+
setTextSize(builder.textSize);
194+
setBackgroundColor(builder.backgroundColor);
195+
this.tableHeaderCenter = builder.tableHeaderCenter;
196+
calculateBaseDimensions(textSize);
197+
registerBuiltInHandlers();
198+
}
199+
200+
private void initBaseComponents(HtmlCleaner cleaner, FontResolver fontResolver) {
201+
this.handlers = new HashMap<>();
180202
this.htmlCleaner = cleaner;
181203
this.fontResolver = fontResolver;
182-
this.handlers = new HashMap<>();
183-
this.textColor=textColor;
184-
this.textSize=textSize;
204+
}
205+
206+
private void calculateBaseDimensions(float textSize) {
185207
Paint paint = new Paint();
186208
paint.setTextSize(textSize);
187209
NUMBER_WIDTH = Math.round(paint.measureText("4."));
188-
if(NUMBER_WIDTH <= 0)
189210
BULLET_WIDTH = Math.round(paint.measureText("\u2022"));
190211
BLANK_WIDTH = Math.round(paint.measureText(" "));
191-
registerBuiltInHandlers();
192212
}
193213

194214
public FontResolver getFontResolver() {
@@ -219,6 +239,10 @@ public float getTextSize() {
219239
return textSize;
220240
}
221241

242+
public void setTableHeaderCentered(boolean tableHeaderCenter) {
243+
this.tableHeaderCenter = tableHeaderCenter;
244+
}
245+
222246
/**
223247
* Switch to specify whether excess whitespace should be stripped from the
224248
* input.
@@ -543,6 +567,9 @@ private void registerBuiltInHandlers() {
543567
TableHandler tableHandler=new TableHandler();
544568
tableHandler.setTextSize(textSize * 0.83f);
545569
tableHandler.setTextColor(textColor);
570+
if(tableHeaderCenter != null){
571+
tableHandler.setHeaderCentered(tableHeaderCenter);
572+
}
546573
registerHandler("table",tableHandler);
547574

548575
registerHandler("h1", wrap(new HeaderHandler(2f, 0.5f)));
@@ -602,4 +629,60 @@ public static interface CancellationCallback {
602629
boolean isCancelled();
603630
}
604631

632+
/**
633+
* Builder class for {@link HtmlSpanner}
634+
*/
635+
public static final class Builder {
636+
private int backgroundColor;
637+
private int textColor;
638+
private float textSize;
639+
private Boolean tableHeaderCenter = null;
640+
641+
public Builder() {
642+
}
643+
644+
/**
645+
* Set the background color, used for paragraphs and div
646+
* @param value the backgournd color as int
647+
* @return
648+
*/
649+
public Builder backgroundColor(int value) {
650+
backgroundColor = value;
651+
return this;
652+
}
653+
654+
/**
655+
* Set the text color
656+
* @param value the text color as int
657+
* @return
658+
*/
659+
public Builder textColor(int value) {
660+
textColor = value;
661+
return this;
662+
}
663+
664+
/**
665+
* Set the text size
666+
* @param value the text size
667+
* @return
668+
*/
669+
public Builder textSize(float value) {
670+
textSize = value;
671+
return this;
672+
}
673+
674+
/**
675+
* use this method if you want to manage the table header text centering, by default is set to true
676+
* @param value
677+
* @return
678+
*/
679+
public Builder tableHeaderCenter(boolean value) {
680+
tableHeaderCenter = value;
681+
return this;
682+
}
683+
684+
public HtmlSpanner build() {
685+
return new HtmlSpanner(this);
686+
}
687+
}
605688
}

HtmlSpanner/src/main/java/com/sysdata/htmlspanner/handlers/StyledTextHandler.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -109,11 +109,8 @@ public void handleTagNode(TagNode node, SpannableStringBuilder builder, int star
109109

110110
//If we have a line height attribute, we apply it to the element
111111
if (useStyle.getLineHeight() != null && useStyle.getBackgroundColor()==null){
112-
StyleValue styleValue = useStyle.getLineHeight();
113-
if ( styleValue.getIntValue() > 0 ) {
114-
stack.pushSpan(new StyleCallback(getSpanner().getFontResolver()
115-
.getDefaultFont(),useStyle,start,builder.length()));
116-
}
112+
stack.pushSpan(new StyleCallback(getSpanner().getFontResolver()
113+
.getDefaultFont(),useStyle,start,builder.length()));
117114
}
118115

119116
if ( builder.length() > start ) {

HtmlSpanner/src/main/java/com/sysdata/htmlspanner/handlers/TableHandler.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,16 @@ public class TableHandler extends TagNodeHandler {
5454

5555
private static final int PADDING = 5;
5656
private boolean hasHeader;
57+
private boolean isHeaderCentered = true;
58+
59+
/**
60+
* Set if the header text has to be centered, by default is true
61+
*
62+
* @param headerCentered
63+
*/
64+
public void setHeaderCentered(boolean headerCentered) {
65+
isHeaderCentered = headerCentered;
66+
}
5767

5868
/**
5969
* Sets how wide the table should be.
@@ -117,7 +127,7 @@ private void readNode(Object node, Table table) {
117127
}
118128

119129
if (tagNode.getName().equals("th")) {
120-
hasHeader = true;
130+
hasHeader = isHeaderCentered;
121131
Spanned result = this.getSpanner().fromTagNode(tagNode, null);
122132
table.addCell(result);
123133
return;
@@ -199,8 +209,8 @@ public void handleTagNode(TagNode node, SpannableStringBuilder builder,
199209
drawable.getIntrinsicHeight());
200210

201211
int index = i == 0 ? start : builder.length() - 1;
202-
builder.setSpan(new ImageSpan(drawable), index, builder.length(),Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
203212
builder.append("\n");
213+
builder.setSpan(new ImageSpan(drawable), index, builder.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
204214
}
205215

206216
/*
@@ -222,8 +232,6 @@ public Alignment getAlignment() {
222232
return Alignment.ALIGN_CENTER;
223233
}
224234
}, start, builder.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
225-
226-
builder.append("\n");
227235
}
228236

229237
/**

HtmlSpanner/src/main/java/com/sysdata/htmlspanner/spans/LineHeightSpanImpl.java

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,34 @@
44
import android.text.style.LineHeightSpan;
55
import android.util.Log;
66

7+
import com.sysdata.htmlspanner.style.StyleValue;
8+
79
/**
810
* Created by Salvatore on 06/12/2017.
911
*/
1012

1113
public class LineHeightSpanImpl implements LineHeightSpan {
1214

13-
//private final int value;
1415
private int mSize;
15-
private static float sProportion = 0;
16-
private int ascent=0;
17-
private int descent=0;
1816

1917

2018
public LineHeightSpanImpl(int value) {
21-
//this.value = value;
2219
mSize=value;
2320
}
2421

22+
public LineHeightSpanImpl(float value, StyleValue.Unit unit, Float textSize){
23+
switch (unit){
24+
case PX:
25+
mSize = (int) value;
26+
break;
27+
case PERCENTAGE:
28+
case EM:
29+
if(textSize != null && textSize > 0){
30+
mSize = (int) (textSize * value);
31+
}
32+
}
33+
}
34+
2535
@Override
2636
public void chooseHeight(CharSequence text, int start, int end, int spanstartv, int v,
2737
Paint.FontMetricsInt fm) {

HtmlSpanner/src/main/java/com/sysdata/htmlspanner/style/StyleCallback.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,16 @@ public void applySpan(HtmlSpanner spanner, SpannableStringBuilder builder) {
8585
}
8686

8787
//If there's a line height, we use an implementation of LineHeightSpan to draw space behind the text
88-
if ( useStyle.getLineHeight() != null) {
88+
if (useStyle.getLineHeight() != null) {
8989
//Log.d("StyleCallback", "Applying LineHeightSpan with value " + useStyle.getLineHeight().getIntValue() + " from " + start + " to " + end + " on text " + builder.subSequence(start, end));
90-
builder.setSpan(new LineHeightSpanImpl((int)Math.ceil(useStyle.getLineHeight().getIntValue() * SCREEN_DENSITY)),start,end,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
90+
StyleValue lineHeight = useStyle.getLineHeight();
91+
LineHeightSpanImpl span;
92+
if(lineHeight.getUnit() == StyleValue.Unit.PX){
93+
span = new LineHeightSpanImpl((int) Math.ceil(lineHeight.getIntValue() * SCREEN_DENSITY));
94+
} else {
95+
span = new LineHeightSpanImpl(lineHeight.getFloatValue(), lineHeight.getUnit(), spanner.getTextSize());
96+
}
97+
builder.setSpan(span,start,end,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
9198
}
9299

93100
//If there is a border, the BorderSpan will also draw the background colour if needed.

HtmlSpanner/src/main/java/com/sysdata/htmlspanner/style/StyleValue.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,13 @@ public static StyleValue parse( String value ) {
2424
return new StyleValue(0f, Unit.EM);
2525
}
2626

27+
// removing extra spaces
28+
value = value.replaceAll("\\s+","");
29+
2730
if ( value.endsWith("px") ) {
2831

2932
try {
30-
final Integer intValue = Integer.parseInt( value.substring(0, value.length() -2) );
33+
final int intValue = Integer.parseInt( value.substring(0, value.length() -2) );
3134
return new StyleValue(intValue);
3235
} catch (NumberFormatException nfe ) {
3336
Log.e("StyleValue", "Can't parse value: " + value );

0 commit comments

Comments
 (0)