Skip to content

Commit

Permalink
EffectiveTextAttributeProvider: Use Kotlin-style getters
Browse files Browse the repository at this point in the history
  • Loading branch information
cubuspl42 committed Oct 14, 2023
1 parent 50a4fa5 commit a9d4f44
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 88 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,46 +13,46 @@ interface EffectiveTextAttributeProvider {
const val UNSET = ReactFontManager.TypefaceStyle.UNSET
}

fun getTextTransform(): TextTransform
val textTransform: TextTransform

fun getEffectiveLetterSpacing(): Float
val effectiveLetterSpacing: Float

/**
* @return The effective font size, or {@link #UNSET} if not set
*/
fun getEffectiveFontSize(): Int
val effectiveFontSize: Int

fun getRole(): Role?
val role: Role?

fun getAccessibilityRole(): ReactAccessibilityDelegate.AccessibilityRole?
val accessibilityRole: ReactAccessibilityDelegate.AccessibilityRole?

fun isBackgroundColorSet(): Boolean
val isBackgroundColorSet: Boolean

fun getBackgroundColor(): Int
val backgroundColor: Int

fun isColorSet(): Boolean
val isColorSet: Boolean

fun getColor(): Int
val color: Int

fun getFontStyle(): Int
val fontStyle: Int

fun getFontWeight(): Int
val fontWeight: Int

fun getFontFamily(): String?
val fontFamily: String?

fun getFontFeatureSettings(): String?
val fontFeatureSettings: String?

fun isUnderlineTextDecorationSet(): Boolean
val isUnderlineTextDecorationSet: Boolean

fun isLineThroughTextDecorationSet(): Boolean
val isLineThroughTextDecorationSet: Boolean

fun getTextShadowOffsetDx(): Float
val textShadowOffsetDx: Float

fun getTextShadowOffsetDy(): Float
val textShadowOffsetDy: Float

fun getTextShadowRadius(): Float
val textShadowRadius: Float

fun getTextShadowColor(): Int
val textShadowColor: Int

fun getEffectiveLineHeight(): Float
val effectiveLineHeight: Float
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,74 +14,92 @@ class HierarchicTextAttributeProvider(
private val parentTextAttributes: TextAttributes?,
private val textAttributes: TextAttributes
) : EffectiveTextAttributeProvider {
override fun getTextTransform(): TextTransform = textAttributes.textTransform
override val textTransform: TextTransform
get() = textAttributes.textTransform

override fun getRole(): ReactAccessibilityDelegate.Role? = textShadowNode.role
override val role: ReactAccessibilityDelegate.Role?
get() = textShadowNode.role

override fun getAccessibilityRole(): ReactAccessibilityDelegate.AccessibilityRole? =
textShadowNode.accessibilityRole
override val accessibilityRole: ReactAccessibilityDelegate.AccessibilityRole?
get() = textShadowNode.accessibilityRole

override fun isBackgroundColorSet(): Boolean = textShadowNode.isBackgroundColorSet
override val isBackgroundColorSet: Boolean
get() = textShadowNode.isBackgroundColorSet

override fun getBackgroundColor(): Int = textShadowNode.backgroundColor
override val backgroundColor: Int
get() = textShadowNode.backgroundColor

override fun isColorSet(): Boolean = textShadowNode.isColorSet
override val isColorSet: Boolean
get() = textShadowNode.isColorSet

override fun getColor(): Int = textShadowNode.color
override val color: Int
get() = textShadowNode.color

override fun getFontStyle(): Int = textShadowNode.fontStyle
override val fontStyle: Int
get() = textShadowNode.fontStyle

override fun getFontWeight(): Int = textShadowNode.fontWeight
override val fontWeight: Int
get() = textShadowNode.fontWeight

override fun getFontFamily(): String = textShadowNode.fontFamily
override val fontFamily: String
get() = textShadowNode.fontFamily

override fun getFontFeatureSettings(): String = textShadowNode.fontFeatureSettings
override val fontFeatureSettings: String
get() = textShadowNode.fontFeatureSettings

override fun isUnderlineTextDecorationSet(): Boolean = textShadowNode.isUnderlineTextDecorationSet
override val isUnderlineTextDecorationSet: Boolean
get() = textShadowNode.isUnderlineTextDecorationSet

override fun isLineThroughTextDecorationSet(): Boolean =
textShadowNode.isLineThroughTextDecorationSet
override val isLineThroughTextDecorationSet: Boolean
get() = textShadowNode.isLineThroughTextDecorationSet

override fun getTextShadowOffsetDx(): Float = textShadowNode.textShadowOffsetDx
override val textShadowOffsetDx: Float
get() = textShadowNode.textShadowOffsetDx

override fun getTextShadowOffsetDy(): Float = textShadowNode.textShadowOffsetDy
override val textShadowOffsetDy: Float
get() = textShadowNode.textShadowOffsetDy

override fun getTextShadowRadius(): Float = textShadowNode.textShadowRadius
override val textShadowRadius: Float
get() = textShadowNode.textShadowRadius

override fun getTextShadowColor(): Int = textShadowNode.textShadowColor
override val textShadowColor: Int
get() = textShadowNode.textShadowColor

override fun getEffectiveLetterSpacing(): Float {
val letterSpacing = textAttributes.effectiveLetterSpacing
override val effectiveLetterSpacing: Float
get() {
val letterSpacing = textAttributes.effectiveLetterSpacing

val isParentLetterSpacingDifferent =
parentTextAttributes == null || parentTextAttributes.effectiveLetterSpacing != letterSpacing
val isParentLetterSpacingDifferent =
parentTextAttributes == null || parentTextAttributes.effectiveLetterSpacing != letterSpacing

return if (!letterSpacing.isNaN() && isParentLetterSpacingDifferent) {
letterSpacing
} else {
Float.NaN
return if (!letterSpacing.isNaN() && isParentLetterSpacingDifferent) {
letterSpacing
} else {
Float.NaN
}
}
}

override fun getEffectiveFontSize(): Int {
val fontSize = textAttributes.effectiveFontSize
override val effectiveFontSize: Int
get() {
val fontSize = textAttributes.effectiveFontSize

return if (parentTextAttributes == null || parentTextAttributes.effectiveFontSize != fontSize) {
fontSize
} else {
EffectiveTextAttributeProvider.UNSET
return if (parentTextAttributes == null || parentTextAttributes.effectiveFontSize != fontSize) {
fontSize
} else {
EffectiveTextAttributeProvider.UNSET
}
}
}

override fun getEffectiveLineHeight(): Float {
val lineHeight = textAttributes.effectiveLineHeight
val isParentLineHeightDifferent =
parentTextAttributes == null || parentTextAttributes.effectiveLineHeight != lineHeight

return if (!lineHeight.isNaN() && isParentLineHeightDifferent) {
lineHeight
} else {
Float.NaN
override val effectiveLineHeight: Float
get() {
val lineHeight = textAttributes.effectiveLineHeight
val isParentLineHeightDifferent =
parentTextAttributes == null || parentTextAttributes.effectiveLineHeight != lineHeight

return if (!lineHeight.isNaN() && isParentLineHeightDifferent) {
lineHeight
} else {
Float.NaN
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ object TextLayoutUtils {
fun addText(
sb: SpannableStringBuilder, text: String?, textAttributeProvider: EffectiveTextAttributeProvider
) {
sb.append(TextTransform.apply(text, textAttributeProvider.getTextTransform()))
sb.append(TextTransform.apply(text, textAttributeProvider.textTransform))
}

fun addInlineViewPlaceholderSpan(
Expand Down Expand Up @@ -114,8 +114,8 @@ object TextLayoutUtils {
end: Int
) {
val roleIsLink =
textAttributeProvider.getRole()?.let { it == ReactAccessibilityDelegate.Role.LINK }
?: (textAttributeProvider.getAccessibilityRole() == ReactAccessibilityDelegate.AccessibilityRole.LINK)
textAttributeProvider.role?.let { it == ReactAccessibilityDelegate.Role.LINK }
?: (textAttributeProvider.accessibilityRole == ReactAccessibilityDelegate.AccessibilityRole.LINK)
if (roleIsLink) {
ops.add(SetSpanOperation(start, end, ReactClickableSpan(reactTag)))
}
Expand All @@ -127,10 +127,10 @@ object TextLayoutUtils {
start: Int,
end: Int
) {
if (textAttributeProvider.isColorSet()) {
if (textAttributeProvider.isColorSet) {
ops.add(
SetSpanOperation(
start, end, ReactForegroundColorSpan(textAttributeProvider.getColor())
start, end, ReactForegroundColorSpan(textAttributeProvider.color)
)
)
}
Expand All @@ -142,10 +142,10 @@ object TextLayoutUtils {
start: Int,
end: Int
) {
if (textAttributeProvider.isBackgroundColorSet()) {
if (textAttributeProvider.isBackgroundColorSet) {
ops.add(
SetSpanOperation(
start, end, ReactBackgroundColorSpan(textAttributeProvider.getBackgroundColor())
start, end, ReactBackgroundColorSpan(textAttributeProvider.backgroundColor)
)
)
}
Expand All @@ -157,7 +157,7 @@ object TextLayoutUtils {
start: Int,
end: Int
) {
val effectiveLetterSpacing = textAttributeProvider.getEffectiveLetterSpacing()
val effectiveLetterSpacing = textAttributeProvider.effectiveLetterSpacing

if (!effectiveLetterSpacing.isNaN()) {
ops.add(SetSpanOperation(start, end, CustomLetterSpacingSpan(effectiveLetterSpacing)))
Expand All @@ -171,7 +171,7 @@ object TextLayoutUtils {
start: Int,
end: Int
) {
val effectiveFontSize = textAttributeProvider.getEffectiveFontSize()
val effectiveFontSize = textAttributeProvider.effectiveFontSize

if (effectiveFontSize != UNSET) {
ops.add(SetSpanOperation(start, end, ReactAbsoluteSizeSpan(effectiveFontSize)))
Expand All @@ -185,17 +185,17 @@ object TextLayoutUtils {
start: Int,
end: Int
) {
val fontStyle = textAttributeProvider.getFontStyle()
val fontWeight = textAttributeProvider.getFontWeight()
val fontFamily = textAttributeProvider.getFontFamily()
val fontStyle = textAttributeProvider.fontStyle
val fontWeight = textAttributeProvider.fontWeight
val fontFamily = textAttributeProvider.fontFamily

if (fontStyle != UNSET || fontWeight != UNSET || fontFamily != null) {
ops.add(
SetSpanOperation(
start, end, CustomStyleSpan(
fontStyle,
fontWeight,
textAttributeProvider.getFontFeatureSettings(),
textAttributeProvider.fontFeatureSettings,
fontFamily,
context.assets
)
Expand All @@ -210,7 +210,7 @@ object TextLayoutUtils {
start: Int,
end: Int
) {
if (textAttributeProvider.isUnderlineTextDecorationSet()) {
if (textAttributeProvider.isUnderlineTextDecorationSet) {
ops.add(SetSpanOperation(start, end, ReactUnderlineSpan()))
}
}
Expand All @@ -221,7 +221,7 @@ object TextLayoutUtils {
start: Int,
end: Int
) {
if (textAttributeProvider.isLineThroughTextDecorationSet()) {
if (textAttributeProvider.isLineThroughTextDecorationSet) {
ops.add(SetSpanOperation(start, end, ReactStrikethroughSpan()))
}
}
Expand All @@ -233,18 +233,18 @@ object TextLayoutUtils {
end: Int
) {
val hasTextShadowOffset =
textAttributeProvider.getTextShadowOffsetDx() != 0f || textAttributeProvider.getTextShadowOffsetDy() != 0f
val hasTextShadowRadius = textAttributeProvider.getTextShadowRadius() != 0f
val hasTextShadowColorAlpha = Color.alpha(textAttributeProvider.getTextShadowColor()) != 0
textAttributeProvider.textShadowOffsetDx != 0f || textAttributeProvider.textShadowOffsetDy != 0f
val hasTextShadowRadius = textAttributeProvider.textShadowRadius != 0f
val hasTextShadowColorAlpha = Color.alpha(textAttributeProvider.textShadowColor) != 0

if ((hasTextShadowOffset || hasTextShadowRadius) && hasTextShadowColorAlpha) {
ops.add(
SetSpanOperation(
start, end, ShadowStyleSpan(
textAttributeProvider.getTextShadowOffsetDx(),
textAttributeProvider.getTextShadowOffsetDy(),
textAttributeProvider.getTextShadowRadius(),
textAttributeProvider.getTextShadowColor()
textAttributeProvider.textShadowOffsetDx,
textAttributeProvider.textShadowOffsetDy,
textAttributeProvider.textShadowRadius,
textAttributeProvider.textShadowColor
)
)
)
Expand All @@ -257,7 +257,7 @@ object TextLayoutUtils {
start: Int,
end: Int
) {
val effectiveLineHeight = textAttributeProvider.getEffectiveLineHeight()
val effectiveLineHeight = textAttributeProvider.effectiveLineHeight
if (!effectiveLineHeight.isNaN()) {
ops.add(SetSpanOperation(start, end, CustomLineHeightSpan(effectiveLineHeight)))
}
Expand Down

0 comments on commit a9d4f44

Please sign in to comment.