21
21
import android .content .res .Resources ;
22
22
import android .graphics .Canvas ;
23
23
import android .graphics .Paint ;
24
+ import android .graphics .Rect ;
24
25
import android .graphics .Typeface ;
25
26
import android .graphics .drawable .Drawable ;
26
27
import android .support .annotation .NonNull ;
31
32
import android .text .SpannableStringBuilder ;
32
33
import android .text .Spanned ;
33
34
import android .text .TextUtils ;
35
+ import android .text .style .BackgroundColorSpan ;
34
36
import android .text .style .ForegroundColorSpan ;
35
37
import android .text .style .ImageSpan ;
38
+ import android .text .style .LineBackgroundSpan ;
36
39
import android .text .style .ParagraphStyle ;
37
40
import android .text .style .QuoteSpan ;
38
41
import android .text .style .RelativeSizeSpan ;
45
48
import android .text .style .URLSpan ;
46
49
import android .text .style .UnderlineSpan ;
47
50
import android .text .util .Linkify ;
51
+ import android .util .DisplayMetrics ;
48
52
import android .util .Pair ;
49
53
50
54
import com .zulip .android .R ;
66
70
import java .util .List ;
67
71
import java .util .Locale ;
68
72
69
- import static android .R .attr .id ;
70
-
71
73
public class CustomHtmlToSpannedConverter implements ContentHandler {
72
74
73
75
private static final float [] HEADER_SIZES = {1.5f , 1.4f , 1.3f , 1.2f , 1.1f ,
@@ -84,6 +86,7 @@ public class CustomHtmlToSpannedConverter implements ContentHandler {
84
86
private Html .TagHandler mTagHandler ;
85
87
private Html .ImageGetter mEmojiGetter ;
86
88
private String mBaseUri ;
89
+ private boolean isCode ;
87
90
88
91
public CustomHtmlToSpannedConverter (String source ,
89
92
Html .ImageGetter imageGetter , Html .TagHandler tagHandler ,
@@ -523,8 +526,10 @@ private void handleStartTag(String tag, Attributes attributes) {
523
526
} else if (tag .equalsIgnoreCase ("sub" )) {
524
527
start (mSpannableStringBuilder , new Sub ());
525
528
} else if (tag .equalsIgnoreCase ("code" )) {
529
+ isCode = true ;
526
530
start (mSpannableStringBuilder , new InlineCode ());
527
531
} else if (tag .equalsIgnoreCase ("pre" )) {
532
+ isCode = true ;
528
533
start (mSpannableStringBuilder , new CodeBlock ());
529
534
} else if (tag .equalsIgnoreCase ("del" )) {
530
535
start (mSpannableStringBuilder , new StrikeThrough ());
@@ -601,11 +606,17 @@ private void handleEndTag(String tag) {
601
606
} else if (tag .equalsIgnoreCase ("code" )) {
602
607
endMultiple (mSpannableStringBuilder , InlineCode .class ,
603
608
new Object []{new TypefaceSpan (MONOSPACE ),
604
- new ForegroundColorSpan (0xffdd1144 ) // pink
609
+ new ForegroundColorSpan (0xffdd1144 ),
610
+ new BackgroundColorSpan (0xfff7f7f9 )
605
611
});
612
+ isCode = false ;
606
613
} else if (tag .equalsIgnoreCase ("pre" )) {
607
- end (mSpannableStringBuilder , CodeBlock .class , new TypefaceSpan (
608
- MONOSPACE ));
614
+ endMultiple (mSpannableStringBuilder , CodeBlock .class , new Object []{
615
+ new TypefaceSpan (MONOSPACE ),
616
+ new ForegroundColorSpan (0xff000000 ),
617
+ new CodeBlockLine (0xfff5f5f5 )
618
+ });
619
+ isCode = false ;
609
620
} else if (tag .equalsIgnoreCase ("del" )) {
610
621
end (mSpannableStringBuilder , StrikeThrough .class , new StrikethroughSpan ());
611
622
} else if (tag .equalsIgnoreCase ("s" )) {
@@ -676,8 +687,14 @@ public void characters(char ch[], int start, int length)
676
687
pred = sb .charAt (len - 1 );
677
688
}
678
689
679
- if (pred != ' ' && pred != '\n' ) {
680
- sb .append (' ' );
690
+ if (!isCode ) {
691
+ if (pred != ' ' && pred != '\n' ) {
692
+ sb .append (' ' );
693
+ }
694
+ } else {
695
+ if ((pred != ' ' && c == ' ' ) || (pred != '\n' && c == '\n' )) {
696
+ sb .append (c );
697
+ }
681
698
}
682
699
} else {
683
700
sb .append (c );
@@ -760,6 +777,26 @@ public Header(int level) {
760
777
}
761
778
}
762
779
780
+ private static class CodeBlockLine implements LineBackgroundSpan {
781
+ private int fillColor ;
782
+
783
+ public CodeBlockLine (int fillColor ) {
784
+ this .fillColor = fillColor ;
785
+ }
786
+
787
+ @ Override
788
+ public void drawBackground (Canvas c , Paint p , int left , int right , int top , int baseline ,
789
+ int bottom , CharSequence text , int start , int end , int lnum ) {
790
+ int paddingInPx = ConvertDpPx .convertDpToPixel (12 );
791
+ Rect rect = new Rect (left - paddingInPx , top - paddingInPx ,
792
+ right + paddingInPx , bottom + paddingInPx );
793
+ final int paintColor = p .getColor ();
794
+ p .setColor (this .fillColor );
795
+ c .drawRect (rect , p );
796
+ p .setColor (paintColor );
797
+ }
798
+ }
799
+
763
800
private class CustomQuoteSpan extends QuoteSpan {
764
801
private static final int STRIPE_WIDTH = 10 ;
765
802
private static final int GAP_WIDTH = 20 ;
0 commit comments