Skip to content

Commit

Permalink
Merge branch 'develop' into technical-analytics-milestone-1
Browse files Browse the repository at this point in the history
  • Loading branch information
kkmurerwa authored Nov 14, 2023
2 parents b9f28ba + 642a778 commit c0338b0
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 20 deletions.
4 changes: 2 additions & 2 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,9 @@ git_repository(
# to correctly size in-line SVGs (such as those needed for LaTeX-based math expressions).
git_repository(
name = "androidsvg",
commit = "1265eb1087056cf3fc2e10442e5545bc65c109ce",
commit = "5bc9c7553e94c3476e8ea32baea3c77567228fcd",
remote = "https://github.com/oppia/androidsvg",
shallow_since = "1686302944 -0700",
shallow_since = "1686304726 -0700",
)

git_repository(
Expand Down
2 changes: 1 addition & 1 deletion utility/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ dependencies {
'androidx.appcompat:appcompat:1.0.2',
'androidx.lifecycle:lifecycle-livedata-ktx:2.2.0-alpha03',
'androidx.work:work-runtime-ktx:2.4.0',
'com.github.oppia:androidsvg:1265eb1087056cf3fc2e10442e5545bc65c109ce',
'com.github.oppia:androidsvg:5bc9c7553e94c3476e8ea32baea3c77567228fcd',
'com.github.oppia:kotlitex:43139c140833c7120f351d63d74b42c253d2b213',
'com.github.bumptech.glide:glide:4.11.0',
'com.google.dagger:dagger:2.24',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package org.oppia.android.util.parser.svg
import android.graphics.Picture
import android.graphics.RectF
import android.text.TextPaint
import com.caverock.androidsvg.RenderOptions
import com.caverock.androidsvg.SVG
import com.caverock.androidsvg.utils.RenderOptionsBase
import com.caverock.androidsvg.androidrendering.RenderOptions
import com.caverock.androidsvg.androidrendering.RenderOptionsBase
import com.caverock.androidsvg.androidrendering.SVG
import org.oppia.android.util.parser.image.ImageTransformation

/**
Expand Down Expand Up @@ -39,18 +39,14 @@ class ScalableVectorGraphic {
}

/**
* Returns the [SvgSizeSpecs] corresponding to this SVG, based on the specified [textPaint]. If a
* [TextPaint] is supplied, the returns specs will include text-based adjustments (for in-line
* images). Otherwise, the returned specs will be arranged for rendering the SVG in a standalone
* manner.
* Returns the [SvgSizeSpecs] corresponding to this SVG.
*
* The returned specs will be arranged for rendering the SVG in a standalone manner.
*/
fun computeSizeSpecs(textPaint: TextPaint?): SvgSizeSpecs {
val options = RenderOptionsBase().also { if (textPaint != null) it.textPaint(textPaint) }
fun computeSizeSpecs(): SvgSizeSpecs {
val options = RenderOptionsBase()
val documentWidth = parsedSvg.value.getDocumentWidthOrNull(options)
val documentHeight = parsedSvg.value.getDocumentHeightOrNull(options)
val verticalAlignment = if (textPaint != null) {
adjustAlignmentForAndroid(parsedSvg.value.getVerticalAlignment(options))
} else 0f

val viewBox: RectF? = parsedSvg.value.documentViewBox
val viewBoxWidth = viewBox?.width()
Expand All @@ -75,16 +71,54 @@ class ScalableVectorGraphic {
intrinsicHeight,
renderedWidth = imageFileNameWidth ?: intrinsicWidth,
renderedHeight = imageFileNameHeight ?: intrinsicHeight,
verticalAlignment = 0f
)
}

/**
* Returns the [SvgSizeSpecs] corresponding to this SVG, based on the specified [textPaint].
* Based on the supplied [TextPaint], the returned specs will include text-based adjustments
* (for in-line images).
*/
fun computeSizeSpecsForTextPicture(textPaint: TextPaint?): SvgSizeSpecs {
val options = textPaint?.let { RenderOptionsBase().textPaint(it) } ?: RenderOptionsBase()
val documentWidth = parsedSvg.value.getDocumentWidthOrNull(options)
val documentHeight = parsedSvg.value.getDocumentHeightOrNull(options)

val imageFileNameWidth = extractedWidth?.toFloat()
val imageFileNameHeight = extractedHeight?.toFloat()

val fontMetrics = textPaint?.fontMetrics
val fontHeight = fontMetrics?.descent?.minus(fontMetrics.ascent) ?: 0f

val adjustedWidth =
imageFileNameWidth?.convertExToPx(fontHeight) ?: documentWidth ?: DEFAULT_SIZE_PX
val adjustedHeight =
imageFileNameHeight?.convertExToPx(fontHeight) ?: documentHeight ?: DEFAULT_SIZE_PX

val verticalAlignment = textPaint?.let {
adjustAlignmentForAndroid(parsedSvg.value.getVerticalAlignment(options))
} ?: 0f

return SvgSizeSpecs(
adjustedWidth,
adjustedHeight,
renderedWidth = adjustedWidth,
renderedHeight = adjustedHeight,
verticalAlignment
)
}

private fun Float.convertExToPx(fontHeight: Float): Float {
return this * fontHeight * 0.5f
}

/**
* Returns an Android [Picture] including the draw instructions for rendering this SVG within a
* line of text whose size and style is configured by the provided [textPaint].
*/
fun renderToTextPicture(textPaint: TextPaint): Picture {
return computeSizeSpecs(textPaint).let { (width, height, _) ->
return computeSizeSpecsForTextPicture(textPaint).let { (width, height, _) ->
val options =
RenderOptions().textPaint(textPaint).viewPort(0f, 0f, width, height) as RenderOptions
parsedSvg.value.renderToPicture(options)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,17 @@ abstract class SvgPictureDrawable(
* when [textPaint] is null and text rendering when otherwise.
*/
protected fun reinitialize(textPaint: TextPaint?) {
picture = textPaint?.let {
scalableVectorGraphic.renderToTextPicture(it)
} ?: scalableVectorGraphic.renderToBlockPicture()
val newPicture = if (textPaint != null) {
intrinsicSize = scalableVectorGraphic.computeSizeSpecsForTextPicture(textPaint)
scalableVectorGraphic.renderToTextPicture(textPaint)
} else {
intrinsicSize = scalableVectorGraphic.computeSizeSpecs()
scalableVectorGraphic.renderToBlockPicture()
}

picture = newPicture

// TODO(#4246): Fix both SVG rendering performance and upscaling to ensure images aren't blurry.
intrinsicSize = scalableVectorGraphic.computeSizeSpecs(textPaint)
if (scalableVectorGraphic.shouldBeRenderedAsBitmap()) {
recomputeBitmap()
}
Expand Down

0 comments on commit c0338b0

Please sign in to comment.