-
Notifications
You must be signed in to change notification settings - Fork 501
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
* Added Svgencoder * Update UrlImageParser.kt * updated kdoc * Update SvgSoftwareLayerSetter.kt * Update SvgEncoder.kt * Update HtmlParserTest.kt * Update RepositoryGlideModule.kt * Update RepositoryGlideModule.kt * Update RepositoryGlideModule.kt * Update SvgSoftwareLayerSetter.kt * Update RepositoryGlideModule.kt * Update UrlImageParser.kt * update changes. * updated changes. * updated nit changes * Fixed lint errors * updated new changes * removed encoder file. * nit changes * Update GlideImageLoader.kt * updated from PictureDrawable to Picture. * Update UrlImageParser.kt * Update GlideImageLoader.kt
- Loading branch information
Showing
8 changed files
with
115 additions
and
20 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
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
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
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 |
---|---|---|
@@ -1,10 +1,13 @@ | ||
package org.oppia.util.parser | ||
|
||
import android.graphics.Bitmap | ||
import android.graphics.Picture | ||
import com.bumptech.glide.request.target.CustomTarget | ||
|
||
/** Loads an image from the provided URL into the specified target, optionally caching it. */ | ||
interface ImageLoader { | ||
|
||
fun load(imageUrl: String, target: CustomTarget<Bitmap>) | ||
|
||
fun loadSvg(imageUrl: String, target: CustomTarget<Picture>) | ||
} |
7 changes: 6 additions & 1 deletion
7
utility/src/main/java/org/oppia/util/parser/RepositoryGlideModule.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 |
---|---|---|
@@ -1,17 +1,22 @@ | ||
package org.oppia.util.parser | ||
|
||
import android.content.Context | ||
import android.graphics.Picture | ||
import com.bumptech.glide.Glide | ||
import com.bumptech.glide.Registry | ||
import com.bumptech.glide.annotation.GlideModule | ||
import com.bumptech.glide.module.AppGlideModule | ||
import com.caverock.androidsvg.SVG | ||
import java.io.InputStream | ||
import org.oppia.util.caching.AssetRepository | ||
|
||
/** Custom [AppGlideModule] to enable loading images from [AssetRepository] via Glide. */ | ||
@GlideModule | ||
class RepositoryGlideModule : AppGlideModule() { | ||
override fun registerComponents(context: Context, glide: Glide, registry: Registry) { | ||
registry.prepend(ImageAssetFetcher::class.java, InputStream::class.java, RepositoryModelLoader.Factory()) | ||
// TODO(#1039): Introduce custom type OppiaImage for rendering Bitmap and Svg. | ||
registry.register(SVG::class.java, Picture::class.java, SvgDrawableTranscoder()) | ||
.append(InputStream::class.java, SVG::class.java, SvgDecoder()) | ||
.append(ImageAssetFetcher::class.java, InputStream::class.java, RepositoryModelLoader.Factory()) | ||
} | ||
} |
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,31 @@ | ||
package org.oppia.util.parser | ||
|
||
import com.bumptech.glide.load.Options | ||
import com.bumptech.glide.load.ResourceDecoder | ||
import com.bumptech.glide.load.engine.Resource | ||
import com.bumptech.glide.load.resource.SimpleResource | ||
import com.caverock.androidsvg.SVG | ||
import com.caverock.androidsvg.SVGParseException | ||
import java.io.IOException | ||
import java.io.InputStream | ||
|
||
/** Decodes an SVG internal representation from an {@link InputStream}. */ | ||
class SvgDecoder : ResourceDecoder<InputStream?, SVG?> { | ||
|
||
override fun handles(source: InputStream, options: Options): Boolean { | ||
return true | ||
} | ||
|
||
override fun decode( | ||
source: InputStream, | ||
width: Int, | ||
height: Int, | ||
options: Options | ||
): Resource<SVG?>? { | ||
return try { | ||
SimpleResource(source.use { SVG.getFromInputStream(it) }) | ||
} catch (ex: SVGParseException) { | ||
throw IOException("Cannot load SVG from stream", ex) | ||
} | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
utility/src/main/java/org/oppia/util/parser/SvgDrawableTranscoder.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,20 @@ | ||
package org.oppia.util.parser | ||
|
||
import android.graphics.Picture | ||
import com.bumptech.glide.load.Options | ||
import com.bumptech.glide.load.engine.Resource | ||
import com.bumptech.glide.load.resource.SimpleResource | ||
import com.bumptech.glide.load.resource.transcode.ResourceTranscoder | ||
import com.caverock.androidsvg.SVG | ||
|
||
/** SvgDrawableTranscoder converts SVG to PictureDrawable. */ | ||
class SvgDrawableTranscoder : ResourceTranscoder<SVG?, Picture?> { | ||
override fun transcode( | ||
toTranscode: Resource<SVG?>, | ||
options: Options | ||
): Resource<Picture?>? { | ||
val svg: SVG = toTranscode.get() | ||
val picture = svg.renderToPicture() | ||
return SimpleResource(picture) | ||
} | ||
} |
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