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

feat: support for textVariableAnchorOffset (does not work yet) #149

Merged
merged 1 commit into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import dev.sargunv.maplibrecompose.core.expression.TextJustify
import dev.sargunv.maplibrecompose.core.expression.TextPitchAlignment
import dev.sargunv.maplibrecompose.core.expression.TextRotationAlignment
import dev.sargunv.maplibrecompose.core.expression.TextTransform
import dev.sargunv.maplibrecompose.core.expression.TextVariableAnchorOffsetValue
import dev.sargunv.maplibrecompose.core.expression.TextWritingMode
import dev.sargunv.maplibrecompose.core.expression.TranslateAnchor
import dev.sargunv.maplibrecompose.core.source.Source
Expand Down Expand Up @@ -198,7 +199,9 @@ internal actual class SymbolLayer actual constructor(id: String, source: Source)
impl.setProperties(PropertyFactory.textVariableAnchor(variableAnchor.toMLNExpression()))
}

actual fun setTextVariableAnchorOffset(variableAnchorOffset: Expression<ListValue<*>>) {
actual fun setTextVariableAnchorOffset(
variableAnchorOffset: Expression<TextVariableAnchorOffsetValue>
) {
impl.setProperties(
PropertyFactory.textVariableAnchorOffset(variableAnchorOffset.toMLNExpression())
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import dev.sargunv.maplibrecompose.core.expression.TextPitchAlignment
import dev.sargunv.maplibrecompose.core.expression.TextRotationAlignment
import dev.sargunv.maplibrecompose.core.expression.TextTransform
import dev.sargunv.maplibrecompose.core.expression.TextUnitValue
import dev.sargunv.maplibrecompose.core.expression.TextVariableAnchorOffsetValue
import dev.sargunv.maplibrecompose.core.expression.TextWritingMode
import dev.sargunv.maplibrecompose.core.expression.TranslateAnchor
import dev.sargunv.maplibrecompose.core.expression.ZeroPadding
Expand Down Expand Up @@ -452,7 +453,7 @@ public fun SymbolLayer(
textOffset: Expression<TextOffsetValue> = textOffset(0f.em, 0f.em),
textVariableAnchor: Expression<ListValue<EnumValue<SymbolAnchor>>> = nil(),
textRadialOffset: Expression<TextUnitValue> = const(0f.em),
textVariableAnchorOffset: Expression<ListValue<*>> = nil(), // TODO proper type
textVariableAnchorOffset: Expression<TextVariableAnchorOffsetValue> = nil(),

// text collision
textPadding: Expression<DpValue> = const(2.dp),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,16 +83,31 @@ public sealed interface ColorValue : ExpressionValue, InterpolateableValue<Color

/**
* Represents an [Expression] that resolves to a map value (corresponds to a JSON object). See
* [ExpressionsDsl.literal].
* [ExpressionsDsl.const].
*/
public sealed interface MapValue<out T : ExpressionValue> : ExpressionValue

/**
* Represents an [Expression] that resolves to a list value (corresponds to a JSON array). See
* [ExpressionsDsl.literal].
* [ExpressionsDsl.const].
*/
public sealed interface ListValue<out T : ExpressionValue> : ExpressionValue

/**
* Represents an [Expression] that resolves to a list value (corresponds to a JSON array) of
* alternating types.
*/
public sealed interface TupleListValue<out T1 : ExpressionValue, out T2 : ExpressionValue> :
ListValue<ExpressionValue>

/**
* Represents an [Expression] that resolves to an alternating list of [SymbolAnchor] and
* [OffsetValue].
*
* See [SymbolLayer][dev.sargunv.maplibrecompose.compose.layer.SymbolLayer].
*/
public typealias TextVariableAnchorOffsetValue = TupleListValue<SymbolAnchor, OffsetValue>

/**
* Represents an [Expression] that resolves to a list of numbers.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,16 +70,40 @@ public object ExpressionsDsl {
public fun const(padding: PaddingValues.Absolute): Expression<PaddingValue> =
Expression.ofPadding(padding)

internal fun literal(list: List<Any?>): Expression<ListValue<*>> =
Expression.ofList(listOf("literal", list)).cast()

/** Creates a literal expression for a list of strings. */
@JvmName("constStringList")
public fun const(list: List<String>): Expression<ListValue<StringValue>> =
Expression.ofList(listOf("literal", list)).cast()
public fun const(list: List<String>): Expression<ListValue<StringValue>> = literal(list).cast()

/** Creates a literal expression for a list of numbers. */
@JvmName("constNumberList")
public fun const(list: List<Number>): Expression<VectorValue<FloatValue>> =
Expression.ofList(listOf("literal", list)).cast()
public fun const(list: List<Number>): Expression<VectorValue<FloatValue>> = literal(list).cast()

/**
* Creates a literal expression for [TextVariableAnchorOffsetValue], used by
* [SymbolLayer][dev.sargunv.maplibrecompose.compose.layer.SymbolLayer]'s
* `textVariableAnchorOffset` parameter.
*
* The offset is measured in a multipler of the text size (EM). It's in [Offset] instead of
* [textOffset] because of technical limitations in MapLibre.
*/
public fun textVariableAnchorOffset(
vararg pairs: Pair<SymbolAnchor, Offset>
): Expression<TextVariableAnchorOffsetValue> {
return literal(
buildList {
pairs.forEach { (anchor, offset) ->
add(anchor.stringConst)
add(offset)
}
}
)
.cast()
}

/** Creates a literal expression for 2D [TextUnit] offset. */
public fun textOffset(x: TextUnit, y: TextUnit): Expression<TextOffsetValue> {
require(x.type == y.type) { "x and y must have the same TextUnitType" }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import dev.sargunv.maplibrecompose.core.expression.TextJustify
import dev.sargunv.maplibrecompose.core.expression.TextPitchAlignment
import dev.sargunv.maplibrecompose.core.expression.TextRotationAlignment
import dev.sargunv.maplibrecompose.core.expression.TextTransform
import dev.sargunv.maplibrecompose.core.expression.TextVariableAnchorOffsetValue
import dev.sargunv.maplibrecompose.core.expression.TextWritingMode
import dev.sargunv.maplibrecompose.core.expression.TranslateAnchor
import dev.sargunv.maplibrecompose.core.source.Source
Expand Down Expand Up @@ -109,7 +110,7 @@ internal expect class SymbolLayer(id: String, source: Source) : FeatureLayer {

fun setTextVariableAnchor(variableAnchor: Expression<ListValue<EnumValue<SymbolAnchor>>>)

@JsOnlyApi fun setTextVariableAnchorOffset(variableAnchorOffset: Expression<ListValue<*>>)
fun setTextVariableAnchorOffset(variableAnchorOffset: Expression<TextVariableAnchorOffsetValue>)

fun setTextAnchor(anchor: Expression<EnumValue<SymbolAnchor>>)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import dev.sargunv.maplibrecompose.core.expression.TextJustify
import dev.sargunv.maplibrecompose.core.expression.TextPitchAlignment
import dev.sargunv.maplibrecompose.core.expression.TextRotationAlignment
import dev.sargunv.maplibrecompose.core.expression.TextTransform
import dev.sargunv.maplibrecompose.core.expression.TextVariableAnchorOffsetValue
import dev.sargunv.maplibrecompose.core.expression.TextWritingMode
import dev.sargunv.maplibrecompose.core.expression.TranslateAnchor
import dev.sargunv.maplibrecompose.core.source.Source
Expand Down Expand Up @@ -203,7 +204,9 @@ internal actual class SymbolLayer actual constructor(id: String, source: Source)
impl.textVariableAnchor = variableAnchor.toNSExpression()
}

actual fun setTextVariableAnchorOffset(variableAnchorOffset: Expression<ListValue<*>>) {
actual fun setTextVariableAnchorOffset(
variableAnchorOffset: Expression<TextVariableAnchorOffsetValue>
) {
impl.textVariableAnchorOffset = variableAnchorOffset.toNSExpression()
}

Expand Down
Loading