Skip to content

Commit

Permalink
[resources] Add functions to retrieve bytes from drawable or font res…
Browse files Browse the repository at this point in the history
…ources.
  • Loading branch information
terrakok committed Apr 18, 2024
1 parent cc29fda commit 47bd458
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package org.jetbrains.compose.resources

import androidx.compose.runtime.Composable
import androidx.compose.runtime.Immutable
import androidx.compose.runtime.getValue
import androidx.compose.ui.text.font.*

/**
Expand Down Expand Up @@ -32,4 +33,21 @@ expect fun Font(
resource: FontResource,
weight: FontWeight = FontWeight.Normal,
style: FontStyle = FontStyle.Normal
): Font
): Font

/**
* Retrieves an ByteArray using the specified font resource.
*
* @param resource The font resource to be used.
* @return The ByteArray loaded from the resource.
*/
@ExperimentalResourceApi
@Composable
fun getFontResourceBytes(resource: FontResource): ByteArray {
val resourceReader = LocalResourceReader.current
val bytes by rememberResourceState(resource, resourceReader, { ByteArray(0) }) { env ->
val item = resource.getResourceItemByEnvironment(env)
resourceReader.read(item.path)
}
return bytes
}
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ internal expect fun SvgElement.toSvgPainter(density: Density): Painter

private val emptySvgPainter: Painter by lazy { BitmapPainter(emptyImageBitmap) }

@OptIn(ExperimentalResourceApi::class)
@Composable
private fun svgPainter(resource: DrawableResource): Painter {
val resourceReader = LocalResourceReader.current
Expand All @@ -110,6 +109,23 @@ private fun svgPainter(resource: DrawableResource): Painter {
return svgPainter
}

/**
* Retrieves an ByteArray using the specified drawable resource.
*
* @param resource The drawable resource to be used.
* @return The ByteArray loaded from the resource.
*/
@ExperimentalResourceApi
@Composable
fun getDrawableResourceBytes(resource: DrawableResource): ByteArray {
val resourceReader = LocalResourceReader.current
val bytes by rememberResourceState(resource, resourceReader, { ByteArray(0) }) { env ->
val item = resource.getResourceItemByEnvironment(env)
resourceReader.read(item.path)
}
return bytes
}

internal expect fun ByteArray.toImageBitmap(): ImageBitmap
internal expect fun ByteArray.toXmlElement(): Element
internal expect fun ByteArray.toSvgElement(): SvgElement
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -304,4 +304,20 @@ class ComposeResourceTest {
assertTrue(uri1.endsWith("/1.png"))
assertTrue(uri2.endsWith("/2.png"))
}

@OptIn(ExperimentalResourceApi::class)
@Test
fun testGetResourceBytes() = runComposeUiTest {
var imageBytes = ByteArray(0)
var fontBytes = ByteArray(0)
setContent {
CompositionLocalProvider(LocalComposeEnvironment provides TestComposeEnvironment) {
imageBytes = getDrawableResourceBytes(TestDrawableResource("1.png"))
fontBytes = getFontResourceBytes(TestFontResource("font_awesome.otf"))
}
}
waitForIdle()
assertEquals(946, imageBytes.size)
assertEquals(134808, fontBytes.size)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ internal fun TestDrawableResource(path: String) = DrawableResource(
setOf(ResourceItem(emptySet(), path, -1, -1))
)

internal fun TestFontResource(path: String) = FontResource(
path,
setOf(ResourceItem(emptySet(), path, -1, -1))
)

internal fun parsePluralSamples(samples: String): List<Int> {
return samples.split(',').flatMap {
val range = it.trim()
Expand Down

0 comments on commit 47bd458

Please sign in to comment.