2020import java .io .InputStream ;
2121import java .io .Reader ;
2222import java .util .HashMap ;
23+ import java .util .LinkedHashMap ;
2324import java .util .Map ;
2425
2526import android .graphics .Color ;
27+ import android .text .TextUtils ;
2628import android .util .Log ;
2729import net .nightwhistler .htmlspanner .exception .ParsingCancelledException ;
2830import net .nightwhistler .htmlspanner .handlers .*;
@@ -70,6 +72,11 @@ public class HtmlSpanner {
7072 private FontResolver fontResolver ;
7173
7274 private int backgroundColor ;
75+
76+ private int textColor ;
77+
78+ private float textSize ;
79+
7380 /**
7481 * Switch to determine if CSS is used
7582 */
@@ -79,13 +86,70 @@ public class HtmlSpanner {
7986 * If CSS colours are used
8087 */
8188 private boolean useColoursFromStyle = true ;
89+ private static Map <String , String > htmlTagsDictionary ;
90+
91+ static {
92+ htmlTagsDictionary = new LinkedHashMap <>();
93+ htmlTagsDictionary .put ("\r \n " , "\n " );
94+ htmlTagsDictionary .put ("\r " , "\n " );
95+ htmlTagsDictionary .put ("\n " ,"<br>" );
96+ htmlTagsDictionary .put (">" , ">" );
97+ htmlTagsDictionary .put ("<" , "<" );
98+ htmlTagsDictionary .put ("•" , "•" );
99+ htmlTagsDictionary .put ("'" , "'" );
100+ htmlTagsDictionary .put ("€" , "€" );
101+ htmlTagsDictionary .put ("$" , "$" );
102+ htmlTagsDictionary .put (" " , " " );
103+ htmlTagsDictionary .put ("’" , "'" );
104+ htmlTagsDictionary .put ("‘" , "'" );
105+ htmlTagsDictionary .put ("“" , "\" " );
106+ htmlTagsDictionary .put ("”" , "\" " );
107+ htmlTagsDictionary .put ("–" , "-" );
108+ htmlTagsDictionary .put ("_" , "_" );
109+ htmlTagsDictionary .put ("©" , "©" );
110+ htmlTagsDictionary .put ("÷" , "÷" );
111+ htmlTagsDictionary .put ("µ" , "µ" );
112+ htmlTagsDictionary .put ("·" , "·" );
113+ htmlTagsDictionary .put ("¶" , "¶" );
114+ htmlTagsDictionary .put ("±" , "±" );
115+ htmlTagsDictionary .put ("®" , "®" );
116+ htmlTagsDictionary .put ("§" , "§" );
117+ htmlTagsDictionary .put ("™" , "™" );
118+ htmlTagsDictionary .put ("¥" , "¥" );
119+ htmlTagsDictionary .put ("£" , "£" );
120+ htmlTagsDictionary .put ("»" , ">>" );
121+ htmlTagsDictionary .put ("«" , "<<" );
122+ htmlTagsDictionary .put ("…" , "..." );
123+ htmlTagsDictionary .put ("à" , "à" );
124+ htmlTagsDictionary .put ("è" , "è" );
125+ htmlTagsDictionary .put ("ì" , "ì" );
126+ htmlTagsDictionary .put ("ò" , "ò" );
127+ htmlTagsDictionary .put ("ù" , "ù" );
128+ htmlTagsDictionary .put ("á" , "á" );
129+ htmlTagsDictionary .put ("é" , "é" );
130+ htmlTagsDictionary .put ("í" , "í" );
131+ htmlTagsDictionary .put ("ó" , "ó" );
132+ htmlTagsDictionary .put ("ú" , "ú" );
133+ htmlTagsDictionary .put ("À" , "À" );
134+ htmlTagsDictionary .put ("È" , "È" );
135+ htmlTagsDictionary .put ("Ì" , "Ì" );
136+ htmlTagsDictionary .put ("Ò" , "Ò" );
137+ htmlTagsDictionary .put ("Ù" , "Ù" );
138+ htmlTagsDictionary .put ("Á" , "Á" );
139+ htmlTagsDictionary .put ("É" , "É" );
140+ htmlTagsDictionary .put ("Í" , "Í" );
141+ htmlTagsDictionary .put ("Ó" , "Ó" );
142+ htmlTagsDictionary .put ("Ú" , "Ú" );
143+ htmlTagsDictionary .put ("<h1>" ,"<h1 style=\" font-weight:bold\" >" );
144+ htmlTagsDictionary .put ("<h2>" ,"<h2 style=\" font-weight:bold\" >" );
145+ }
82146
83147
84148 /**
85149 * Creates a new HtmlSpanner using a default HtmlCleaner instance.
86150 */
87- public HtmlSpanner () {
88- this (createHtmlCleaner (), new SystemFontResolver ());
151+ public HtmlSpanner (int textColor , float textSize ) {
152+ this (createHtmlCleaner (), new SystemFontResolver (), textColor , textSize );
89153 }
90154
91155 /**
@@ -95,11 +159,12 @@ public HtmlSpanner() {
95159 *
96160 * @param cleaner
97161 */
98- public HtmlSpanner (HtmlCleaner cleaner , FontResolver fontResolver ) {
162+ public HtmlSpanner (HtmlCleaner cleaner , FontResolver fontResolver , int textColor , float textSize ) {
99163 this .htmlCleaner = cleaner ;
100164 this .fontResolver = fontResolver ;
101165 this .handlers = new HashMap <String , TagNodeHandler >();
102-
166+ this .textColor =textColor ;
167+ this .textSize =textSize ;
103168 registerBuiltInHandlers ();
104169 }
105170
@@ -119,6 +184,14 @@ public void setBackgroundColor(int backgroundColor) {
119184 this .backgroundColor = backgroundColor ;
120185 }
121186
187+ public void setTextColor (int textColor ) {
188+ this .textColor = textColor ;
189+ }
190+
191+ public void setTextSize (float textSize ) {
192+ this .textSize = textSize ;
193+ }
194+
122195 /**
123196 * Switch to specify whether excess whitespace should be stripped from the
124197 * input.
@@ -205,6 +278,12 @@ public void unregisterHandler(String tagName) {
205278 * @return a Spanned version of the text.
206279 */
207280 public Spannable fromHtml (String html ) {
281+ if (html !=null ){
282+ if (!TextUtils .isEmpty (html )){
283+ html =replaceHtmlTags (html );
284+ }
285+ }
286+ Log .i ("HTML" ,html );
208287 return fromTagNode (this .htmlCleaner .clean (html ), null );
209288 }
210289
@@ -371,6 +450,7 @@ private void registerBuiltInHandlers() {
371450 new Style ().setFontWeight (Style .FontWeight .BOLD ));
372451
373452 registerHandler ("b" , boldHandler );
453+ registerHandler ("bold" , boldHandler );
374454 registerHandler ("strong" , boldHandler );
375455 //Underline added
376456 registerHandler ("u" ,new UnderlineHandler ());
@@ -396,16 +476,13 @@ private void registerBuiltInHandlers() {
396476 TagNodeHandler brHandler = new NewLineHandler (1 , inlineAlignment );
397477
398478 registerHandler ("br" , brHandler );
479+ registerHandler ("br/" , brHandler );
399480
400481 Style .BorderStyle borderStyle = Style .BorderStyle .valueOf ("solid" .toUpperCase ());
401482
402483 //HR handler
403484 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-
485+ .setDisplayStyle (Style .DisplayStyle .BLOCK );
409486
410487 TagNodeHandler hrHandler = new HorizontalLineHandler (wrap (new StyledTextHandler (hrStyle )));
411488
@@ -433,15 +510,16 @@ private void registerBuiltInHandlers() {
433510 registerHandler ("span" ,spanHandler );
434511
435512 TableHandler tableHandler =new TableHandler ();
436- tableHandler .setTextSize (26f );
513+ tableHandler .setTextSize (textSize * 0.83f );
514+ tableHandler .setTextColor (textColor );
437515 registerHandler ("table" ,tableHandler );
438516
439- registerHandler ("h1" , wrap (new HeaderHandler (1.5f , 0.5f )));
440- registerHandler ("h2" , wrap (new HeaderHandler (1.4f , 0.6f )));
441- registerHandler ("h3" , wrap (new HeaderHandler (1.3f , 0.7f )));
442- registerHandler ("h4" , wrap (new HeaderHandler (1.2f , 0.8f )));
443- registerHandler ("h5" , wrap (new HeaderHandler (1.1f , 0.9f )));
444- registerHandler ("h6" , wrap (new HeaderHandler (1f , 1f )));
517+ registerHandler ("h1" , wrap (new HeaderHandler (2f , 0.5f )));
518+ registerHandler ("h2" , wrap (new HeaderHandler (1.5f , 0.6f )));
519+ registerHandler ("h3" , wrap (new HeaderHandler (1.17f , 0.7f )));
520+ registerHandler ("h4" , wrap (new HeaderHandler (1.12f , 0.8f )));
521+ registerHandler ("h5" , wrap (new HeaderHandler (0.83f , 0.9f )));
522+ registerHandler ("h6" , wrap (new HeaderHandler (0.75f , 1f )));
445523
446524 TagNodeHandler preHandler = new PreHandler ();
447525 registerHandler ("pre" , preHandler );
@@ -476,6 +554,19 @@ private void registerBuiltInHandlers() {
476554
477555 }
478556
557+ public static String replaceHtmlTags (String inputString ) {
558+ if (inputString == null ) {
559+ return null ;
560+ }
561+
562+ // before apply typefaces, replace all tags
563+ for (Map .Entry <String , String > entry : htmlTagsDictionary .entrySet ()) {
564+ inputString = inputString .replace (entry .getKey (), entry .getValue ());
565+ inputString = inputString .replace (entry .getKey ().toUpperCase (), entry .getValue ());
566+ }
567+ return inputString ;
568+ }
569+
479570 public static interface CancellationCallback {
480571 boolean isCancelled ();
481572 }
0 commit comments