-
Notifications
You must be signed in to change notification settings - Fork 527
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* Improvised bullet code * Updated bullets * Fixes for MDPI devices * EOF added * Added yoffset in bullets * Nit changes * Directly added context instead of activity * Added test cases * Nit changes * Added TODO
- Loading branch information
Showing
5 changed files
with
175 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
utility/src/main/java/org/oppia/util/parser/CustomBulletSpan.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package org.oppia.util.parser | ||
|
||
import android.content.Context | ||
import android.graphics.Canvas | ||
import android.graphics.Paint | ||
import android.graphics.Path | ||
import android.graphics.Path.Direction | ||
import android.text.Layout | ||
import android.text.Spanned | ||
import android.text.style.LeadingMarginSpan | ||
import org.oppia.util.R | ||
|
||
// TODO(#562): Add screenshot tests to check whether the drawing logic works correctly on all devices. | ||
|
||
/** | ||
* Copy of [android.text.style.BulletSpan] from android SDK 28 with removed internal code. | ||
* This class helps us to customise bullet radius, gap width and offset present in rich-text. | ||
* Reference: https://github.com/davidbilik/bullet-span-sample | ||
*/ | ||
class CustomBulletSpan(context: Context) : LeadingMarginSpan { | ||
private var bulletRadius: Int = 0 | ||
private var gapWidth: Int = 0 | ||
private var yOffset: Int = 0 | ||
private var bulletLeadingMargin: Int = 0 | ||
|
||
init { | ||
bulletRadius = context.resources.getDimensionPixelSize(R.dimen.bullet_radius) | ||
gapWidth = context.resources.getDimensionPixelSize(R.dimen.bullet_gap_width) | ||
yOffset = context.resources.getDimensionPixelSize(R.dimen.bullet_y_offset) | ||
bulletLeadingMargin = context.resources.getDimensionPixelSize(R.dimen.bullet_leading_margin) | ||
} | ||
|
||
private var mBulletPath: Path? = null | ||
|
||
override fun getLeadingMargin(first: Boolean): Int { | ||
return bulletLeadingMargin | ||
} | ||
|
||
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? | ||
) { | ||
if ((text as Spanned).getSpanStart(this) == start) { | ||
val style = paint.style | ||
paint.style = Paint.Style.FILL | ||
|
||
var yPosition = if (layout != null) { | ||
val line = layout.getLineForOffset(start) | ||
layout.getLineBaseline(line).toFloat() - bulletRadius * 2f | ||
} else { | ||
(top + bottom) / 2f | ||
} | ||
yPosition += yOffset | ||
|
||
val xPosition = (x + dir * bulletRadius).toFloat() | ||
|
||
if (canvas.isHardwareAccelerated) { | ||
if (mBulletPath == null) { | ||
mBulletPath = Path() | ||
mBulletPath!!.addCircle(0.0f, 0.0f, bulletRadius.toFloat(), Direction.CW) | ||
} | ||
|
||
canvas.save() | ||
canvas.translate(xPosition, yPosition) | ||
canvas.drawPath(mBulletPath!!, paint) | ||
canvas.restore() | ||
} else { | ||
canvas.drawCircle(xPosition, yPosition, bulletRadius.toFloat(), paint) | ||
} | ||
|
||
paint.style = style | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
utility/src/main/java/org/oppia/util/parser/LiTagHandler.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package org.oppia.util.parser | ||
|
||
import android.text.Editable | ||
import android.text.Html | ||
import android.text.Spannable | ||
import android.text.Spanned | ||
import android.text.style.BulletSpan | ||
import org.xml.sax.XMLReader | ||
|
||
/** | ||
* [Html.TagHandler] implementation that processes <li> tags and creates bullets. | ||
* | ||
* Reference: https://github.com/davidbilik/bullet-span-sample | ||
*/ | ||
class LiTagHandler : Html.TagHandler { | ||
/** | ||
* Helper marker class. Based on [Html.fromHtml] implementation. | ||
*/ | ||
class Bullet | ||
|
||
override fun handleTag(opening: Boolean, tag: String, output: Editable, xmlReader: XMLReader) { | ||
if (tag == "li" && opening) { | ||
output.setSpan(Bullet(), output.length, output.length, Spannable.SPAN_INCLUSIVE_EXCLUSIVE) | ||
} | ||
if (tag == "li" && !opening) { | ||
output.append("<br>") | ||
val lastMark = output.getSpans(0, output.length, Bullet::class.java).lastOrNull() | ||
lastMark?.let { | ||
val start = output.getSpanStart(it) | ||
output.removeSpan(it) | ||
if (start != output.length) { | ||
output.setSpan(BulletSpan(), start, output.length, Spanned.SPAN_INCLUSIVE_EXCLUSIVE) | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<resources> | ||
<dimen name="bullet_radius">4dp</dimen> | ||
<dimen name="bullet_gap_width">16dp</dimen> | ||
<dimen name="bullet_y_offset">2dp</dimen> | ||
<dimen name="bullet_leading_margin">24dp</dimen> | ||
</resources> |