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

commonMain.ru.casperix.multiplatform.text.StringMetrics.kt Maven / Gradle / Ivy

The newest version!
package ru.casperix.multiplatform.text

import ru.casperix.math.axis_aligned.dimensionOf
import ru.casperix.math.axis_aligned.float32.Dimension2f

data class StringMetrics(val size: Dimension2f, val advances:List) {
    companion object {
        fun calculate(line:String, widthByIndex:(Int)->Float, charHeight:Float):StringMetrics {
            val widths = line.indices.map {
                widthByIndex(it)
            }

            var last = 0f
            val advances = widths.map {
                val back = last
                last += it
                back
            }

            val imageWidth = widths.sum()
            val symbolSize = dimensionOf(imageWidth, charHeight)
            return StringMetrics(symbolSize, advances)
        }

        fun calculate(charsWidth: (CharArray, Int, Int) -> Float, line: String, height:Float): StringMetrics {
            val chars = line.toCharArray()
            val widths = line.mapIndexed { index, c ->
                charsWidth(chars, index, index+1)
            }

            var last = 0f
            val advances = widths.map {
                val back = last
                last += it
                back
            }

            val width = widths.sum()
            return StringMetrics( dimensionOf(width, height), advances)

        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy