Skip to content

Commit dcb5033

Browse files
authored
Merge pull request #634 from wordpress-mobile/feature/rtl-quote-support
Feature/rtl quote support
2 parents 5b0019b + 4f53175 commit dcb5033

File tree

2 files changed

+51
-9
lines changed

2 files changed

+51
-9
lines changed

app/src/main/kotlin/org/wordpress/aztec/demo/MainActivity.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ open class MainActivity : AppCompatActivity(),
134134
private val AUDIO = "[audio src=\"https://upload.wikimedia.org/wikipedia/commons/9/94/H-Moll.ogg\"]"
135135
private val VIDEOPRESS = "[wpvideo OcobLTqC]"
136136
private val VIDEOPRESS_2 = "[wpvideo OcobLTqC w=640 h=400 autoplay=true html5only=true3]"
137+
private val QUOTE_RTL = "<blockquote>לְצַטֵט<br>same quote but LTR</blockquote>"
137138

138139
private val EXAMPLE =
139140
IMG +
@@ -161,7 +162,8 @@ open class MainActivity : AppCompatActivity(),
161162
VIDEOPRESS +
162163
VIDEOPRESS_2 +
163164
AUDIO +
164-
GUTENBERG_CODE_BLOCK
165+
GUTENBERG_CODE_BLOCK +
166+
QUOTE_RTL
165167

166168
private val isRunningTest: Boolean by lazy {
167169
try {

aztec/src/main/kotlin/org/wordpress/aztec/spans/AztecQuoteSpan.kt

Lines changed: 48 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ import android.graphics.Canvas
2121
import android.graphics.Color
2222
import android.graphics.Paint
2323
import 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
2428
import android.text.Layout
2529
import android.text.Spanned
2630
import android.text.style.LineBackgroundSpan
@@ -29,17 +33,19 @@ import android.text.style.QuoteSpan
2933
import android.text.style.UpdateLayout
3034
import org.wordpress.aztec.AztecAttributes
3135
import org.wordpress.aztec.formatting.BlockFormatter
36+
import java.util.Locale
3237

3338
class 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

Comments
 (0)