Skip to content

Commit

Permalink
feat: support for textVariableAnchorOffset (does not work yet) (#149)
Browse files Browse the repository at this point in the history
  • Loading branch information
sargunv authored Dec 20, 2024
1 parent c9256b7 commit 1c52f8e
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 10 deletions.
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

0 comments on commit 1c52f8e

Please sign in to comment.