@@ -21,6 +21,10 @@ import android.graphics.Canvas
2121import android.graphics.Color
2222import android.graphics.Paint
2323import android.graphics.Rect
24+ import android.support.v4.text.TextDirectionHeuristicCompat
25+ import android.support.v4.text.TextDirectionHeuristicsCompat
26+ import android.support.v4.text.TextUtilsCompat
27+ import android.support.v4.view.ViewCompat
2428import android.text.Layout
2529import android.text.Spanned
2630import android.text.style.LineBackgroundSpan
@@ -29,17 +33,19 @@ import android.text.style.QuoteSpan
2933import android.text.style.UpdateLayout
3034import org.wordpress.aztec.AztecAttributes
3135import org.wordpress.aztec.formatting.BlockFormatter
36+ import java.util.Locale
3237
3338class AztecQuoteSpan (
3439 override var nestingLevel : Int ,
3540 override var attributes : AztecAttributes = AztecAttributes (),
3641 var quoteStyle : BlockFormatter .QuoteStyle = BlockFormatter .QuoteStyle (0, 0, 0f, 0, 0, 0, 0),
37- override var align : Layout .Alignment ? = null
38- ) : QuoteSpan(), LineBackgroundSpan, IAztecBlockSpan, LineHeightSpan, UpdateLayout {
42+ override var align : Layout .Alignment ? = null )
43+ : QuoteSpan (), LineBackgroundSpan , IAztecBlockSpan , LineHeightSpan , UpdateLayout {
44+
3945 override var endBeforeBleed: Int = - 1
4046 override var startBeforeCollapse: Int = - 1
4147
42- val rect = Rect ()
48+ private val rect = Rect ()
4349
4450 override val TAG : String = " blockquote"
4551
@@ -71,16 +77,27 @@ class AztecQuoteSpan(
7177
7278 p.style = Paint .Style .FILL
7379 p.color = quoteStyle.quoteColor
74- c.drawRect(x.toFloat() + quoteStyle.quoteMargin, top.toFloat(),
75- (x + quoteStyle.quoteMargin + dir * quoteStyle.quoteWidth).toFloat(), bottom.toFloat(), p)
80+
81+ val marginStart: Float
82+ val marginEnd: Float
83+
84+ if (isRtlQuote(text, start, end)) {
85+ marginStart = (x - quoteStyle.quoteMargin + dir * quoteStyle.quoteWidth).toFloat()
86+ marginEnd = (x - quoteStyle.quoteMargin).toFloat()
87+ } else {
88+ marginStart = (x + quoteStyle.quoteMargin).toFloat()
89+ marginEnd = (x + quoteStyle.quoteMargin + dir * quoteStyle.quoteWidth).toFloat()
90+ }
91+
92+ c.drawRect(marginStart, top.toFloat(), marginEnd, bottom.toFloat(), p)
7693
7794 p.style = style
7895 p.color = color
7996 }
8097
8198 override fun drawBackground (c : Canvas , p : Paint , left : Int , right : Int ,
8299 top : Int , baseline : Int , bottom : Int ,
83- text : CharSequence? , start : Int , end : Int ,
100+ text : CharSequence , start : Int , end : Int ,
84101 lnum : Int ) {
85102 val alpha: Int = (quoteStyle.quoteBackgroundAlpha * 255 ).toInt()
86103
@@ -90,9 +107,32 @@ class AztecQuoteSpan(
90107 Color .red(quoteStyle.quoteBackground),
91108 Color .green(quoteStyle.quoteBackground),
92109 Color .blue(quoteStyle.quoteBackground))
93- rect.set(left + quoteStyle.quoteMargin, top, right, bottom)
110+
111+ val quoteBackgroundStart: Int
112+ val quoteBackgroundEnd: Int
113+
114+ if (isRtlQuote(text, start, end)) {
115+ quoteBackgroundStart = left
116+ quoteBackgroundEnd = right - quoteStyle.quoteMargin
117+ } else {
118+ quoteBackgroundStart = left + quoteStyle.quoteMargin
119+ quoteBackgroundEnd = right
120+ }
121+
122+ rect.set(quoteBackgroundStart, top, quoteBackgroundEnd, bottom)
94123
95124 c.drawRect(rect, p)
96125 p.color = paintColor
97126 }
98- }
127+
128+ private fun isRtlQuote (text : CharSequence , start : Int , end : Int ): Boolean {
129+ val textDirectionHeuristic: TextDirectionHeuristicCompat =
130+ if (TextUtilsCompat .getLayoutDirectionFromLocale(Locale .getDefault()) == ViewCompat .LAYOUT_DIRECTION_RTL ) {
131+ TextDirectionHeuristicsCompat .FIRSTSTRONG_RTL
132+ } else {
133+ TextDirectionHeuristicsCompat .FIRSTSTRONG_LTR
134+ }
135+ return textDirectionHeuristic.isRtl(text, start, end - start)
136+ }
137+
138+ }
0 commit comments