All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.peanuuutz.fork.ui.foundation.text.TextDelegate.kt Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2020 The Android Open Source Project
 * Modifications Copyright 2022 Peanuuutz
 *
 * 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 net.peanuuutz.fork.ui.foundation.text

import androidx.compose.runtime.Immutable
import net.peanuuutz.fork.ui.ui.draw.text.Paragraph
import net.peanuuutz.fork.ui.ui.draw.text.TextStyle
import net.peanuuutz.fork.ui.ui.layout.Constraints
import net.peanuuutz.fork.ui.ui.layout.constrain
import net.peanuuutz.fork.ui.ui.unit.IntSize
import kotlin.math.ceil

@Immutable
internal class TextDelegate(
    val paragraph: Paragraph,
    val textStyle: TextStyle = TextStyle.Default,
    val overflow: TextOverflow = TextOverflow.Clip,
    val softWrap: Boolean = true,
    val maxLines: Int = Int.MAX_VALUE
) {
    private val textMeasurer: TextMeasurer = TextMeasurer()

    fun layout(
        constraints: Constraints,
        previousResult: TextLayoutResult?
    ): TextLayoutResult {
        if (previousResult != null) {
            val previousInput = previousResult.input
            if (
                paragraph == previousInput.paragraph &&
                textStyle == previousInput.textStyle &&
                overflow == previousInput.overflow &&
                softWrap == previousInput.softWrap &&
                maxLines == previousInput.maxLines &&
                constraints == previousInput.constraints
            ) {
                return previousResult
            }
        }
        val input = TextLayoutInput(
            paragraph = paragraph,
            textStyle = textStyle,
            overflow = overflow,
            softWrap = softWrap,
            maxLines = maxLines,
            constraints = constraints
        )
        val resolvedMaxWidth = if (softWrap || overflow == TextOverflow.Ellipsis) {
            constraints.maxWidth.toFloat()
        } else {
            Float.POSITIVE_INFINITY
        }
        val measuredParagraph = textMeasurer.measure(
            maxWidth = resolvedMaxWidth,
            paragraph = paragraph,
            textStyle = textStyle,
            ellipsis = overflow == TextOverflow.Ellipsis,
            maxLines = maxLines
        )
        val size = IntSize(
            width = ceil(measuredParagraph.measuredSize.width).toInt(),
            height = ceil(measuredParagraph.measuredSize.height).toInt()
        )
        return TextLayoutResult(
            input = input,
            measuredParagraph = measuredParagraph,
            size = constraints.constrain(size)
        )
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy