Skip to content

Commit

Permalink
Add support for text decoration line styles via PlatformTextStyle
Browse files Browse the repository at this point in the history
  • Loading branch information
m-sasha committed Jan 18, 2024
1 parent 72d8841 commit e7dd699
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* Copyright 2024 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package androidx.compose.ui.text

import kotlin.jvm.JvmInline

/**
* Defines the style of the text decoration line (e.g. underline).
*
* The decoration itself is determined via [androidx.compose.ui.text.style.TextDecoration].
*/
@ExperimentalTextApi
@JvmInline
value class TextDecorationLineStyle internal constructor(val value: Int) {

override fun toString(): String {
return when (this) {
Solid -> "Solid"
Double -> "Double"
Dotted -> "Dotted"
Dashed -> "Dashed"
Wavy -> "Wavy"
else -> "Invalid"
}
}

companion object {
/**
* Solid line.
*/
val Solid = TextDecorationLineStyle(1)

/**
* Double line.
*/
val Double = TextDecorationLineStyle(2)

/**
* Dotted line.
*/
val Dotted = TextDecorationLineStyle(3)

/**
* Dashed line.
*/
val Dashed = TextDecorationLineStyle(4)

/**
* Wavy line.
*/
val Wavy = TextDecorationLineStyle(5)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,12 @@
* limitations under the License.
*/

@file:JvmName("DesktopTextStyleKt")

package androidx.compose.ui.text

import kotlin.jvm.JvmName

/**
* Provides configuration options for behavior compatibility for TextStyle.
*/
Expand All @@ -31,6 +35,17 @@ actual class PlatformTextStyle {
this.paragraphStyle = paragraphStyle
}

/**
* Allows specifying the style of the decoration line for the text.
*/
@ExperimentalTextApi
constructor(
textDecorationLineStyle: TextDecorationLineStyle?
) : this (
spanStyle = PlatformSpanStyle(textDecorationLineStyle),
paragraphStyle = null
)

override fun equals(other: Any?): Boolean {
if (this === other) return true
if (other !is PlatformTextStyle) return false
Expand Down Expand Up @@ -78,8 +93,16 @@ actual class PlatformParagraphStyle {

/**
* Provides configuration options for behavior compatibility for SpanStyle.
*
* @param textDecorationLineStyle The style of the text decoration line.
*/
actual class PlatformSpanStyle {
actual class PlatformSpanStyle(
val textDecorationLineStyle: TextDecorationLineStyle?
) {

constructor() : this(TextDecorationLineStyle.Solid)


actual companion object {
actual val Default: PlatformSpanStyle = PlatformSpanStyle()
}
Expand All @@ -91,12 +114,12 @@ actual class PlatformSpanStyle {
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (other !is PlatformSpanStyle) return false
if (textDecorationLineStyle != other.textDecorationLineStyle) return false
return true
}

@Suppress("RedundantOverride")
override fun hashCode(): Int {
return super.hashCode()
return textDecorationLineStyle.hashCode()
}
}

Expand Down Expand Up @@ -139,5 +162,13 @@ actual fun lerp(
stop: PlatformSpanStyle,
fraction: Float
): PlatformSpanStyle {
return start
if (start.textDecorationLineStyle == stop.textDecorationLineStyle) return start

return PlatformSpanStyle(
textDecorationLineStyle = lerpDiscrete(
start.textDecorationLineStyle,
stop.textDecorationLineStyle,
fraction
)
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ import androidx.compose.ui.text.intl.LocaleList
import androidx.compose.ui.text.style.*
import androidx.compose.ui.unit.*
import org.jetbrains.skia.FontFeature
import org.jetbrains.skia.FontMetrics
import org.jetbrains.skia.Paint
import org.jetbrains.skia.paragraph.*
import org.jetbrains.skia.paragraph.ParagraphStyle
Expand Down Expand Up @@ -102,7 +101,6 @@ internal actual fun ActualParagraph(
constraints
)

@Suppress("UNUSED_PARAMETER")
internal actual fun ActualParagraph(
paragraphIntrinsics: ParagraphIntrinsics,
maxLines: Int,
Expand Down Expand Up @@ -134,6 +132,7 @@ internal data class ComputedStyle(
var localeList: LocaleList?,
var background: Color = Color.Unspecified,
var textDecoration: TextDecoration?,
var textDecorationLineStyle: TextDecorationLineStyle?,
var shadow: Shadow?,
var drawStyle: DrawStyle?,
var blendMode: BlendMode,
Expand Down Expand Up @@ -163,6 +162,7 @@ internal data class ComputedStyle(
localeList = spanStyle.localeList,
background = spanStyle.background,
textDecoration = spanStyle.textDecoration,
textDecorationLineStyle = spanStyle.platformStyle?.textDecorationLineStyle,
shadow = spanStyle.shadow,
drawStyle = spanStyle.drawStyle,
blendMode = blendMode,
Expand Down Expand Up @@ -194,7 +194,8 @@ internal data class ComputedStyle(
res.fontStyle = it.toSkFontStyle()
}
textDecoration?.let {
res.decorationStyle = it.toSkDecorationStyle(textForegroundStyle.color)
res.decorationStyle =
it.toSkDecorationStyle(textForegroundStyle.color, textDecorationLineStyle)
}
if (background != Color.Unspecified) {
res.background = Paint().also {
Expand All @@ -216,7 +217,6 @@ internal data class ComputedStyle(

res.fontSize = fontSize
fontFamily?.let {
@Suppress("UNCHECKED_CAST")
val resolved = fontFamilyResolver.resolve(
it,
fontWeight ?: FontWeight.Normal,
Expand Down Expand Up @@ -258,6 +258,11 @@ internal data class ComputedStyle(
other.textDecoration?.let { textDecoration = it }
other.shadow?.let { shadow = it }
other.drawStyle?.let { drawStyle = it }
other.platformStyle?.let { platformStyle ->
platformStyle.textDecorationLineStyle?.let {
textDecorationLineStyle = it
}
}
}
}

Expand Down Expand Up @@ -625,12 +630,16 @@ fun FontStyle.toSkFontStyle(): SkFontStyle {
}

// TODO: Remove from public
fun TextDecoration.toSkDecorationStyle(color: Color): SkDecorationStyle {
fun TextDecoration.toSkDecorationStyle(
color: Color,
textDecorationLineStyle: TextDecorationLineStyle?
): SkDecorationStyle {
val underline = contains(TextDecoration.Underline)
val overline = false
val lineThrough = contains(TextDecoration.LineThrough)
val gaps = false
val lineStyle = SkDecorationLineStyle.SOLID
val lineStyle =
textDecorationLineStyle?.toSkDecorationLineStyle() ?: SkDecorationLineStyle.SOLID
val thicknessMultiplier = 1f
return SkDecorationStyle(
underline,
Expand All @@ -643,6 +652,17 @@ fun TextDecoration.toSkDecorationStyle(color: Color): SkDecorationStyle {
)
}

private fun TextDecorationLineStyle.toSkDecorationLineStyle(): SkDecorationLineStyle {
return when (this) {
TextDecorationLineStyle.Solid -> SkDecorationLineStyle.SOLID
TextDecorationLineStyle.Double -> SkDecorationLineStyle.DOUBLE
TextDecorationLineStyle.Dotted -> SkDecorationLineStyle.DOTTED
TextDecorationLineStyle.Dashed -> SkDecorationLineStyle.DASHED
TextDecorationLineStyle.Wavy -> SkDecorationLineStyle.WAVY
else -> SkDecorationLineStyle.SOLID
}
}

// TODO: Remove from public
fun PlaceholderVerticalAlign.toSkPlaceholderAlignment(): PlaceholderAlignment {
return when (this) {
Expand Down

0 comments on commit e7dd699

Please sign in to comment.