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
Add 'linetype' parsing as theme option.
  • Loading branch information
OLarionova-HORIS committed Apr 2, 2024
commit 55bc2128bd4f51b9fe13eb4384af69da41691080
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,36 @@ 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)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import org.jetbrains.letsPlot.commons.values.FontFace
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.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 @@ -136,4 +138,8 @@ internal open class ThemeValuesAccess(
left = getNumber(elem, Elem.Inset.LEFT),
)
}

protected fun getLineType(elem: Map<String, Any>): LineType {
return NamedLineType.parse(elem.getValue(Elem.LINETYPE))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ object ThemeOption {
const val FILL = "fill"
const val COLOR = "color"
const val SIZE = "size"
const val LINETYPE = "linetype" // ToDo
const val LINETYPE = "linetype"
const val ARROW = "arrow" // ToDo

// text
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import org.jetbrains.letsPlot.commons.values.FontFace
import org.jetbrains.letsPlot.core.plot.base.guide.LegendDirection
import org.jetbrains.letsPlot.core.plot.base.guide.LegendJustification
import org.jetbrains.letsPlot.core.plot.base.guide.LegendPosition
import org.jetbrains.letsPlot.core.plot.base.render.linetype.NamedLineType
import org.jetbrains.letsPlot.core.plot.builder.defaultTheme.ThemeFlavor.Companion.SymbolicColor
import org.jetbrains.letsPlot.core.plot.builder.defaultTheme.values.ThemeOption.AXIS_ONTOP
import org.jetbrains.letsPlot.core.plot.builder.defaultTheme.values.ThemeOption.AXIS_TEXT
Expand Down Expand Up @@ -48,11 +49,13 @@ internal open class ThemeValuesBase : ThemeValues(VALUES) {
LINE to mapOf(
Elem.SIZE to 1.0,
Elem.COLOR to SymbolicColor.BLACK,
Elem.LINETYPE to NamedLineType.SOLID
),
RECT to mapOf(
Elem.SIZE to 1.0,
Elem.COLOR to SymbolicColor.BLACK,
Elem.FILL to SymbolicColor.WHITE
Elem.FILL to SymbolicColor.WHITE,
Elem.LINETYPE to NamedLineType.SOLID
),
TEXT to mapOf(
Elem.SIZE to Defaults.FONT_SMALL,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package org.jetbrains.letsPlot.core.plot.builder.subPlots

import org.jetbrains.letsPlot.commons.geometry.DoubleRectangle
import org.jetbrains.letsPlot.commons.geometry.DoubleVector
import org.jetbrains.letsPlot.core.plot.base.render.svg.StrokeDashArraySupport
import org.jetbrains.letsPlot.core.plot.base.render.svg.SvgComponent
import org.jetbrains.letsPlot.core.plot.base.theme.Theme
import org.jetbrains.letsPlot.core.plot.builder.FigureSvgRoot
Expand All @@ -27,7 +28,9 @@ class CompositeFigureSvgComponent constructor(
add(SvgRectElement(r).apply {
fillColor().set(plotTheme.backgroundFill())
strokeColor().set(plotTheme.backgroundColor())
strokeWidth().set(plotTheme.backgroundStrokeWidth())
val width = plotTheme.backgroundStrokeWidth()
strokeWidth().set(width)
StrokeDashArraySupport.apply(this, width, plotTheme.backgroundStrokeLineType())
})
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,42 +10,5 @@ import org.jetbrains.letsPlot.core.plot.base.render.linetype.LineType
import org.jetbrains.letsPlot.core.plot.base.render.linetype.NamedLineType

internal class LineTypeOptionConverter : Function<Any?, LineType?> {

override fun apply(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
*/

if (value == null) {
return NamedLineType.SOLID
}
if (value is LineType) {
return value
}
if (value is String && LINE_TYPE_BY_NAME.containsKey(value)) {
return LINE_TYPE_BY_NAME[value]!!
}
return if (value is Number && LINE_TYPE_BY_CODE.containsKey(value.toInt())) {
LINE_TYPE_BY_CODE[value.toInt()]!!
} else NamedLineType.SOLID

// todo: could be string of hexadecimal digits (not implemented)
}

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
}
}
}
override fun apply(value: Any?) = NamedLineType.parse(value)
}