Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add 'linetype' for theme elements #1072

Merged
merged 14 commits into from
Apr 3, 2024
Merged
Prev Previous commit
Next Next commit
Cleanup linetype parse function.
  • Loading branch information
OLarionova-HORIS committed Apr 2, 2024
commit da47377acd6ed34c1909969102be727b40507709
Original file line number Diff line number Diff line change
Expand Up @@ -39,35 +39,24 @@ enum class NamedLineType(val code: Int, private val myDashArray: List<Double>?)
throw IllegalStateException("No dash array in " + name.lowercase() + " linetype")
}

companion object {
private val LINE_TYPE_BY_CODE = HashMap<Int, NamedLineType>()
private val LINE_TYPE_BY_NAME = HashMap<String, NamedLineType>()

init {
for (lineType in NamedLineType.values()) {
LINE_TYPE_BY_CODE[lineType.code] = lineType
LINE_TYPE_BY_NAME[lineType.name.lowercase()] = lineType
}
}

fun parse(value: Any?): LineType {
/*
* Line type is specified with either an integer (code 0..6), a name, or with a string of
* an even number (up to eight) of hexadecimal digits which give the lengths in
* consecutive positions in the string.
* <p/>
* Codes and names:
* 0 = blank, 1 = solid, 2 = dashed, 3 = dotted, 4 = dotdash, 5 = longdash, 6 = twodash
*/
return when {
value == null -> SOLID
value is LineType -> value
value is String && LINE_TYPE_BY_NAME.containsKey(value) -> LINE_TYPE_BY_NAME[value]!!
value is Number && LINE_TYPE_BY_CODE.containsKey(value.toInt()) -> LINE_TYPE_BY_CODE[value.toInt()]!!
else -> SOLID
}

// todo: could be string of hexadecimal digits (not implemented)
}
}
private val LINE_TYPE_BY_CODE = NamedLineType.values().associateBy { it.code }
private val LINE_TYPE_BY_NAME = NamedLineType.values().associateBy { it.name.lowercase() }

fun parse(value: Any?): LineType {
/*
* The line type is specified by either an integer (code 0..6) or a name.
* Codes and names:
* 0 = blank, 1 = solid, 2 = dashed, 3 = dotted, 4 = dotdash, 5 = longdash, 6 = twodash
*
* ToDo: could be string of hexadecimal digits (not implemented)
*/
return when {
value == null -> NamedLineType.SOLID
value is LineType -> value
value is String && LINE_TYPE_BY_NAME.containsKey(value) -> LINE_TYPE_BY_NAME[value]!!
value is Number && LINE_TYPE_BY_CODE.containsKey(value.toInt()) -> LINE_TYPE_BY_CODE[value.toInt()]!!
else -> NamedLineType.SOLID
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import org.jetbrains.letsPlot.commons.values.FontFamily
import org.jetbrains.letsPlot.core.plot.base.layout.TextJustification
import org.jetbrains.letsPlot.core.plot.base.layout.Thickness
import org.jetbrains.letsPlot.core.plot.base.render.linetype.LineType
import org.jetbrains.letsPlot.core.plot.base.render.linetype.NamedLineType
import org.jetbrains.letsPlot.core.plot.base.render.linetype.parse
import org.jetbrains.letsPlot.core.plot.base.theme.FontFamilyRegistry
import org.jetbrains.letsPlot.core.plot.base.theme.ThemeTextStyle
import org.jetbrains.letsPlot.core.plot.builder.defaultTheme.values.ThemeOption.Elem
Expand Down Expand Up @@ -140,6 +140,6 @@ internal open class ThemeValuesAccess(
}

protected fun getLineType(elem: Map<String, Any>): LineType {
return NamedLineType.parse(elem.getValue(Elem.LINETYPE))
return parse(elem.getValue(Elem.LINETYPE))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ package org.jetbrains.letsPlot.core.spec.conversion

import org.jetbrains.letsPlot.commons.intern.function.Function
import org.jetbrains.letsPlot.core.plot.base.render.linetype.LineType
import org.jetbrains.letsPlot.core.plot.base.render.linetype.NamedLineType
import org.jetbrains.letsPlot.core.plot.base.render.linetype.parse

internal class LineTypeOptionConverter : Function<Any?, LineType?> {
override fun apply(value: Any?) = NamedLineType.parse(value)
override fun apply(value: Any?) = parse(value)
}