Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@
import com.facebook.react.views.text.PreparedLayout;
import com.facebook.react.views.text.ReactTextViewManager;
import com.facebook.react.views.text.ReactTextViewManagerCallback;
import com.facebook.react.views.text.ReactTypefaceUtils;
import com.facebook.react.views.text.TextEffectRegistry;
import com.facebook.react.views.text.TextLayoutManager;
import java.util.ArrayList;
Expand Down Expand Up @@ -553,6 +554,7 @@ private NativeArray measureLines(
return (NativeArray)
TextLayoutManager.measureLines(
mReactApplicationContext.getAssets(),
ReactTypefaceUtils.getFontWeightAdjustment(mReactApplicationContext),
attributedString,
paragraphAttributes,
PixelUtil.toPixelFromDIP(width),
Expand Down Expand Up @@ -639,6 +641,7 @@ public long measureText(

return TextLayoutManager.measureText(
mReactApplicationContext.getAssets(),
ReactTypefaceUtils.getFontWeightAdjustment(mReactApplicationContext),
attributedString,
paragraphAttributes,
getYogaSize(minWidth, maxWidth),
Expand Down Expand Up @@ -666,6 +669,7 @@ public PreparedLayout prepareTextLayout(

return TextLayoutManager.createPreparedLayout(
mReactApplicationContext.getAssets(),
ReactTypefaceUtils.getFontWeightAdjustment(mReactApplicationContext),
attributedString,
paragraphAttributes,
getYogaSize(minWidth, maxWidth),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ public constructor(
val spanned: Spannable =
TextLayoutManager.getOrCreateSpannableForText(
view.context.assets,
ReactTypefaceUtils.getFontWeightAdjustment(view.context),
attributedString,
reactTextViewManagerCallback,
TextEffectRegistry.current,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,22 @@

package com.facebook.react.views.text

import android.content.Context
import android.content.res.Configuration
import android.content.res.AssetManager
import android.graphics.Typeface
import android.os.Build
import com.facebook.react.bridge.ReadableArray
import com.facebook.react.common.ReactConstants
import com.facebook.react.common.assets.ReactFontManager
import kotlin.math.max
import kotlin.math.min

public object ReactTypefaceUtils {

private const val FONT_WEIGHT_MIN = 1
private const val FONT_WEIGHT_MAX = 1000

@JvmStatic
public fun parseFontWeight(fontWeightString: String?): Int =
when (fontWeightString) {
Expand Down Expand Up @@ -110,4 +118,33 @@ public object ReactTypefaceUtils {
ReactFontManager.getInstance().getTypeface(fontFamilyName, typefaceStyle, assetManager)
}
}

@JvmStatic
public fun getFontWeightAdjustment(context: Context): Int =
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
context.resources.configuration.fontWeightAdjustment
} else {
0
}

@JvmStatic
public fun applyFontWeightAdjustment(
typeface: Typeface?,
fontWeightAdjustment: Int,
): Typeface? {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.S || fontWeightAdjustment == 0) {
return typeface
}

if (fontWeightAdjustment == Configuration.FONT_WEIGHT_ADJUSTMENT_UNDEFINED) {
return typeface
}

val baseTypeface = typeface ?: Typeface.DEFAULT
val adjustedWeight =
min(max(baseTypeface.weight + fontWeightAdjustment, FONT_WEIGHT_MIN), FONT_WEIGHT_MAX)
val italic = baseTypeface.style and Typeface.ITALIC != 0

return Typeface.create(baseTypeface, adjustedWeight, italic)
}
}
Loading