I figured the reason why the font was still scaling despite the
CompositionLocalProvider
density. It’s because I was using
dimensionResource(id = <http://R.dimen.xyz|R.dimen.xyz>)
to read the values which internally scales the raw value to TextUnit.
To solve this this is what I did:
@Composable
private fun getRawDimensionInDp(@DimenRes id: Int): Float {
val context = LocalContext.current
val value = TypedValue()
context.resources.getValue(id, value, true)
return TypedValue.complexToFloat(value.data)
}
@Composable
private fun getResolvedFontSize(@DimenRes id: Int): TextUnit {
getRawDimensionInDp(id = id).sp
}
Here I am reading the value as it is, meaning that 12.dp from dimens.xml will be read as
12.0
and then I convert it to 12.0.sp which enforces the scaling.