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

net.peanuuutz.fork.ui.foundation.text.TextLayoutResult.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 androidx.compose.runtime.Stable
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.unit.IntSize
import net.peanuuutz.fork.util.fork.InternalForkApi

// -------- TextLayoutResult --------

@Immutable
class TextLayoutResult @InternalForkApi constructor(
    val input: TextLayoutInput,
    val measuredParagraph: MeasuredParagraph,
    val size: IntSize
) {
    override fun equals(other: Any?): Boolean {
        if (this === other) return true
        if (other !is TextLayoutResult) return false
        if (input != other.input) return false
        if (measuredParagraph != other.measuredParagraph) return false
        if (size != other.size) return false
        return true
    }

    override fun hashCode(): Int {
        var result = input.hashCode()
        result = 31 * result + measuredParagraph.hashCode()
        result = 31 * result + size.hashCode()
        return result
    }

    override fun toString(): String {
        return "TextLayoutResult(input=$input, measuredParagraph=$measuredParagraph, size=$size)"
    }
}

@Stable
val TextLayoutResult.hasOverflowWidth: Boolean
    get() = size.width < measuredParagraph.measuredSize.width

@Stable
val TextLayoutResult.hasOverflowHeight: Boolean
    get() = measuredParagraph.exceedMaxLines || size.height < measuredParagraph.measuredSize.height

@Stable
val TextLayoutResult.hasOverflow: Boolean
    get() = hasOverflowWidth || hasOverflowHeight

@Stable
val TextLayoutResult.lineCount: Int
    get() = measuredParagraph.lines.size

// -------- TextLayoutInput --------

@Immutable
class TextLayoutInput @InternalForkApi constructor(
    val paragraph: Paragraph,
    val textStyle: TextStyle,
    val overflow: TextOverflow,
    val softWrap: Boolean,
    val maxLines: Int,
    val constraints: Constraints
) {
    override fun equals(other: Any?): Boolean {
        if (this === other) return true
        if (other !is TextLayoutInput) return false
        if (paragraph != other.paragraph) return false
        if (textStyle != other.textStyle) return false
        if (overflow != other.overflow) return false
        if (softWrap != other.softWrap) return false
        if (maxLines != other.maxLines) return false
        if (constraints != other.constraints) return false
        return true
    }

    override fun hashCode(): Int {
        var result = paragraph.hashCode()
        result = 31 * result + textStyle.hashCode()
        result = 31 * result + overflow.hashCode()
        result = 31 * result + softWrap.hashCode()
        result = 31 * result + maxLines
        result = 31 * result + constraints.hashCode()
        return result
    }

    override fun toString(): String {
        return "TextLayoutInput(paragraph=$paragraph, textStyle=$textStyle, " +
                "overflow=$overflow, softWrap=$softWrap, maxLines=$maxLines, " +
                "constraints=$constraints)"
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy