Skip to content

Commit

Permalink
Fix #15
Browse files Browse the repository at this point in the history
  • Loading branch information
IKupriyanov-HORIS committed Aug 6, 2024
1 parent fb6f7d9 commit 9b95d6e
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 1 deletion.
1 change: 1 addition & 0 deletions future_changes.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
### Changed

### Fixed
- fontfamily aes is not supported [[#15](https://github.com/JetBrains/lets-plot-skia/issues/15)].
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,19 @@ internal abstract class SvgAttrMapping<in TargetT : Element> {
else -> error("Unsupported float value: $this")
}

val Any.asPxSize: Float?
get() = when (this) {
is Number -> this.toFloat()
is String -> kotlin.runCatching { this.removeSuffix("px").toFloat() }.getOrNull()
else -> null.also { println("Unsupported px size value: $this") }
}

val Any.asFontFamily: List<String>
get() = (this as? String)
?.split(",")
?.map(String::trim)
?: emptyList()

internal fun asBoolean(value: Any?): Boolean {
return (value as? String)?.toBoolean() ?: false
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import org.jetbrains.letsPlot.skia.shape.Text
internal object SvgTextElementAttrMapping : SvgShapeMapping<Text>() {
override fun setAttribute(target: Text, name: String, value: Any?) {
when (name) {
"font-size" -> target.fontSize = value?.asPxSize ?: Text.DEFAULT_FONT_SIZE
"font-family" -> target.fontFamily = value?.asFontFamily ?: Text.DEFAULT_FONT_FAMILY
SvgTextElement.X.name -> target.x = value?.asFloat ?: 0.0f
SvgTextElement.Y.name -> target.y = value?.asFloat ?: 0.0f
SvgTextContent.TEXT_ANCHOR.name -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ internal class Text : Figure() {
var content: List<TextRun> by visualProp(emptyList())
var fontFamily: List<String> by visualProp(emptyList())
var fontStyle: FontStyle by visualProp(FontStyle.NORMAL)
var fontSize by visualProp(16.0f)
var fontSize by visualProp(DEFAULT_FONT_SIZE)

private val typeface by computedProp(Text::fontFamily, Text::fontStyle, managed = true) {
FontMgr.default.matchFamiliesStyle(fontFamily.toTypedArray(), fontStyle) ?: Typeface.makeDefault()
Expand Down Expand Up @@ -164,4 +164,9 @@ internal class Text : Figure() {
}

private val widthCorrectionCoef = 0f

companion object {
const val DEFAULT_FONT_SIZE: Float = 16f
val DEFAULT_FONT_FAMILY: List<String> = emptyList()
}
}

0 comments on commit 9b95d6e

Please sign in to comment.