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

commonMain.io.nacular.doodle.Utils.kt Maven / Gradle / Ivy

There is a newer version: 0.10.2
Show newest version
package io.nacular.doodle

import io.nacular.doodle.dom.HTMLElement
import io.nacular.doodle.dom.HtmlFactory
import io.nacular.doodle.dom.defaultFontFamily
import io.nacular.doodle.dom.defaultFontSize
import io.nacular.doodle.dom.defaultFontWeight
import io.nacular.doodle.dom.setFont
import io.nacular.doodle.drawing.Font
import io.nacular.doodle.drawing.FontInfo

internal interface FontSerializer {
    operator fun invoke(font: Font?   ): String
    operator fun invoke(info: FontInfo): String
}

internal class FontSerializerImpl(htmlFactory: HtmlFactory): FontSerializer {
    private val element = htmlFactory.create()

    override fun invoke(font: Font?): String = when {
        font != null -> element.run {
            style.setFont(font)

            style.run { "$fontStyle $fontVariant $fontWeight $fontSize $fontFamily" }
        }
        else -> "$defaultFontWeight ${defaultFontSize}px $defaultFontFamily"
    }

    override fun invoke(info: FontInfo) = invoke(object: Font {
        override val size   get() = info.size
        override val style  get() = info.style
        override val weight get() = info.weight
        override val family get() = info.families.joinToString()
    })
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy