Skip to content

Commit 19c76a2

Browse files
committed
Expanded styling options of preformat span.
1 parent 0e45077 commit 19c76a2

File tree

1 file changed

+81
-20
lines changed

1 file changed

+81
-20
lines changed

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

Lines changed: 81 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ package org.wordpress.aztec.spans
22

33
import android.graphics.Canvas
44
import android.graphics.Color
5+
import android.graphics.CornerPathEffect
56
import android.graphics.Paint
7+
import android.graphics.Path
68
import android.graphics.Rect
79
import android.text.Layout
810
import android.text.Spanned
@@ -20,7 +22,7 @@ fun createPreformatSpan(
2022
nestingLevel: Int,
2123
alignmentRendering: AlignmentRendering,
2224
attributes: AztecAttributes = AztecAttributes(),
23-
preformatStyle: BlockFormatter.PreformatStyle = BlockFormatter.PreformatStyle(0, 0f, 0, 0)
25+
preformatStyle: BlockFormatter.PreformatStyle = BlockFormatter.PreformatStyle(0, 0f, 0, 0, 0, 0, 0, 0)
2426
): AztecPreformatSpan =
2527
when (alignmentRendering) {
2628
AlignmentRendering.SPAN_LEVEL -> AztecPreformatSpanAligned(nestingLevel, attributes, preformatStyle)
@@ -58,9 +60,8 @@ open class AztecPreformatSpan(
5860

5961
val rect = Rect()
6062

61-
private val MARGIN = 16
62-
63-
override fun chooseHeight(text: CharSequence, start: Int, end: Int, spanstartv: Int, v: Int, fm: Paint.FontMetricsInt) {
63+
override fun chooseHeight(text: CharSequence, start: Int, end: Int, spanstartv: Int, v: Int,
64+
fm: Paint.FontMetricsInt) {
6465
val spanned = text as Spanned
6566
val spanStart = spanned.getSpanStart(this)
6667
val spanEnd = spanned.getSpanEnd(this)
@@ -76,35 +77,95 @@ open class AztecPreformatSpan(
7677
}
7778
}
7879

79-
override fun drawBackground(canvas: Canvas, paint: Paint, left: Int, right: Int, top: Int, baseline: Int, bottom: Int, text: CharSequence?, start: Int, end: Int, lnum: Int) {
80+
private val strokePaint = Paint().apply {
81+
isAntiAlias = true
82+
style = Paint.Style.STROKE
83+
}
84+
85+
private val fillPaint = Paint().apply {
86+
isAntiAlias = true
87+
strokeCap = Paint.Cap.ROUND
88+
}
89+
90+
private var borderPath = Path()
91+
private var fillPath = Path()
92+
93+
override fun drawBackground(canvas: Canvas, paint: Paint, left: Int, right: Int, top: Int, baseline: Int,
94+
bottom: Int, text: CharSequence?, start: Int, end: Int, lnum: Int) {
8095
val color = paint.color
8196
val alpha: Int = (preformatStyle.preformatBackgroundAlpha * 255).toInt()
82-
paint.color = Color.argb(
97+
98+
fillPaint.color = Color.argb(
8399
alpha,
84100
Color.red(preformatStyle.preformatBackground),
85101
Color.green(preformatStyle.preformatBackground),
86102
Color.blue(preformatStyle.preformatBackground)
87103
)
88-
rect.set(left, top, right, bottom)
89-
canvas.drawRect(rect, paint)
90-
paint.color = color
91-
}
92-
93-
override fun drawLeadingMargin(canvas: Canvas, paint: Paint, x: Int, dir: Int, top: Int, baseline: Int, bottom: Int, text: CharSequence, start: Int, end: Int, first: Boolean, layout: Layout) {
94-
val style = paint.style
95-
val color = paint.color
96-
97-
paint.style = Paint.Style.FILL
98-
paint.color = preformatStyle.preformatColor
104+
paint.color = fillPaint.color
105+
106+
fillPaint.pathEffect = CornerPathEffect(preformatStyle.preformatBorderRadius.toFloat())
107+
strokePaint.pathEffect = CornerPathEffect(preformatStyle.preformatBorderRadius.toFloat())
108+
109+
strokePaint.color = preformatStyle.preformatBorderColor
110+
strokePaint.strokeWidth = preformatStyle.preformatBorderThickness.toFloat()
111+
112+
val isFirstLine = top == 0
113+
114+
val isLastLine = text?.length == end
115+
116+
fillPath = Path().apply {
117+
if (isFirstLine) {
118+
moveTo(left.toFloat(), bottom.toFloat())
119+
lineTo(left.toFloat(), top.toFloat())
120+
lineTo(right.toFloat(), top.toFloat())
121+
lineTo(right.toFloat(), bottom.toFloat())
122+
} else if (isLastLine) {
123+
moveTo(left.toFloat(), top.toFloat())
124+
lineTo(left.toFloat(), bottom.toFloat())
125+
lineTo(right.toFloat(), bottom.toFloat())
126+
lineTo(right.toFloat(), top.toFloat())
127+
} else {
128+
fillPaint.pathEffect = null
129+
moveTo(left.toFloat(), top.toFloat())
130+
lineTo(right.toFloat(), top.toFloat())
131+
lineTo(right.toFloat(), bottom.toFloat())
132+
lineTo(left.toFloat(), bottom.toFloat())
133+
lineTo(left.toFloat(), top.toFloat())
134+
135+
}
136+
}
99137

100-
canvas.drawRect(x.toFloat() + MARGIN, top.toFloat(), (x + MARGIN).toFloat(), bottom.toFloat(), paint)
138+
canvas.drawPath(fillPath, fillPaint)
139+
140+
borderPath = Path().apply {
141+
if (isFirstLine) {
142+
moveTo(left.toFloat(), bottom.toFloat())
143+
lineTo(left.toFloat(), top.toFloat())
144+
lineTo(right.toFloat(), top.toFloat())
145+
lineTo(right.toFloat(), bottom.toFloat())
146+
} else if (isLastLine) {
147+
moveTo(left.toFloat(), top.toFloat())
148+
lineTo(left.toFloat(), bottom.toFloat())
149+
lineTo(right.toFloat(), bottom.toFloat())
150+
lineTo(right.toFloat(), top.toFloat())
151+
} else {
152+
moveTo(left.toFloat(), top.toFloat())
153+
lineTo(left.toFloat(), bottom.toFloat())
154+
moveTo(right.toFloat(), top.toFloat())
155+
lineTo(right.toFloat(), bottom.toFloat())
156+
}
157+
}
101158

102-
paint.style = style
159+
canvas.drawPath(borderPath, strokePaint)
103160
paint.color = color
104161
}
105162

163+
override fun drawLeadingMargin(canvas: Canvas, paint: Paint, x: Int, dir: Int, top: Int, baseline: Int,
164+
bottom: Int, text: CharSequence, start: Int, end: Int, first: Boolean,
165+
layout: Layout) = Unit
166+
106167
override fun getLeadingMargin(first: Boolean): Int {
107-
return MARGIN
168+
return preformatStyle.leadingMargin
108169
}
109170

110171
override val textFormat: ITextFormat = AztecTextFormat.FORMAT_PREFORMAT

0 commit comments

Comments
 (0)