|
| 1 | +/* |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + */ |
| 7 | + |
| 8 | +package com.facebook.react.uimanager.style |
| 9 | + |
| 10 | +import com.facebook.react.bridge.ReadableMap |
| 11 | +import com.facebook.react.bridge.ReadableType |
| 12 | +import com.facebook.react.uimanager.LengthPercentage |
| 13 | + |
| 14 | +private fun getOptionalLengthPercentage(map: ReadableMap, key: String): LengthPercentage? { |
| 15 | + return if (map.hasKey(key)) { |
| 16 | + LengthPercentage.setFromDynamic(map.getDynamic(key)) |
| 17 | + } else { |
| 18 | + null |
| 19 | + } |
| 20 | +} |
| 21 | + |
| 22 | +public data class CircleShape( |
| 23 | + val r: LengthPercentage? = null, |
| 24 | + val cx: LengthPercentage? = null, |
| 25 | + val cy: LengthPercentage? = null, |
| 26 | +) { |
| 27 | + public companion object { |
| 28 | + public fun parse(map: ReadableMap): CircleShape? { |
| 29 | + val r = getOptionalLengthPercentage(map, "r") |
| 30 | + val cx = getOptionalLengthPercentage(map, "cx") |
| 31 | + val cy = getOptionalLengthPercentage(map, "cy") |
| 32 | + return CircleShape(r, cx, cy) |
| 33 | + } |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +public data class EllipseShape( |
| 38 | + val rx: LengthPercentage? = null, |
| 39 | + val ry: LengthPercentage? = null, |
| 40 | + val cx: LengthPercentage? = null, |
| 41 | + val cy: LengthPercentage? = null, |
| 42 | +) { |
| 43 | + public companion object { |
| 44 | + public fun parse(map: ReadableMap): EllipseShape? { |
| 45 | + val rx = getOptionalLengthPercentage(map, "rx") |
| 46 | + val ry = getOptionalLengthPercentage(map, "ry") |
| 47 | + val cx = getOptionalLengthPercentage(map, "cx") |
| 48 | + val cy = getOptionalLengthPercentage(map, "cy") |
| 49 | + return EllipseShape(rx, ry, cx, cy) |
| 50 | + } |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +public data class InsetShape( |
| 55 | + val top: LengthPercentage, |
| 56 | + val right: LengthPercentage, |
| 57 | + val bottom: LengthPercentage, |
| 58 | + val left: LengthPercentage, |
| 59 | + val borderRadius: LengthPercentage? = null, |
| 60 | +) { |
| 61 | + public companion object { |
| 62 | + public fun parse(map: ReadableMap): InsetShape? { |
| 63 | + val top = getOptionalLengthPercentage(map, "top") ?: return null |
| 64 | + val right = getOptionalLengthPercentage(map, "right") ?: return null |
| 65 | + val bottom = getOptionalLengthPercentage(map, "bottom") ?: return null |
| 66 | + val left = getOptionalLengthPercentage(map, "left") ?: return null |
| 67 | + val borderRadius = getOptionalLengthPercentage(map, "borderRadius") |
| 68 | + return InsetShape(top, right, bottom, left, borderRadius) |
| 69 | + } |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +public enum class FillRule { |
| 74 | + NonZero, |
| 75 | + EvenOdd; |
| 76 | + |
| 77 | + public companion object { |
| 78 | + public fun fromString(value: String): FillRule { |
| 79 | + return when (value.lowercase()) { |
| 80 | + "nonzero" -> NonZero |
| 81 | + "evenodd" -> EvenOdd |
| 82 | + else -> NonZero |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +public data class PolygonShape( |
| 89 | + val points: List<Pair<LengthPercentage, LengthPercentage>>, |
| 90 | + val fillRule: FillRule? = null, |
| 91 | +) { |
| 92 | + public companion object { |
| 93 | + public fun parse(map: ReadableMap): PolygonShape? { |
| 94 | + if (!map.hasKey("points")) return null |
| 95 | + val pointsArray = map.getArray("points") ?: return null |
| 96 | + val points = mutableListOf<Pair<LengthPercentage, LengthPercentage>>() |
| 97 | + |
| 98 | + for (i in 0 until pointsArray.size()) { |
| 99 | + val pointMap = pointsArray.getMap(i) ?: continue |
| 100 | + val x = getOptionalLengthPercentage(pointMap, "x") ?: continue |
| 101 | + val y = getOptionalLengthPercentage(pointMap, "y") ?: continue |
| 102 | + points.add(Pair(x, y)) |
| 103 | + } |
| 104 | + |
| 105 | + val fillRule = |
| 106 | + if (map.hasKey("fillRule")) { |
| 107 | + FillRule.fromString(map.getString("fillRule") ?: "nonzero") |
| 108 | + } else { |
| 109 | + null |
| 110 | + } |
| 111 | + |
| 112 | + return PolygonShape(points, fillRule) |
| 113 | + } |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +public data class RectShape( |
| 118 | + val top: LengthPercentage, |
| 119 | + val right: LengthPercentage, |
| 120 | + val bottom: LengthPercentage, |
| 121 | + val left: LengthPercentage, |
| 122 | + val borderRadius: LengthPercentage? = null, |
| 123 | +) { |
| 124 | + public companion object { |
| 125 | + public fun parse(map: ReadableMap): RectShape? { |
| 126 | + val top = getOptionalLengthPercentage(map, "top") ?: return null |
| 127 | + val right = getOptionalLengthPercentage(map, "right") ?: return null |
| 128 | + val bottom = getOptionalLengthPercentage(map, "bottom") ?: return null |
| 129 | + val left = getOptionalLengthPercentage(map, "left") ?: return null |
| 130 | + val borderRadius = getOptionalLengthPercentage(map, "borderRadius") |
| 131 | + return RectShape(top, right, bottom, left, borderRadius) |
| 132 | + } |
| 133 | + } |
| 134 | +} |
| 135 | + |
| 136 | +public data class XywhShape( |
| 137 | + val x: LengthPercentage, |
| 138 | + val y: LengthPercentage, |
| 139 | + val width: LengthPercentage, |
| 140 | + val height: LengthPercentage, |
| 141 | + val borderRadius: LengthPercentage? = null, |
| 142 | +) { |
| 143 | + public companion object { |
| 144 | + public fun parse(map: ReadableMap): XywhShape? { |
| 145 | + val x = getOptionalLengthPercentage(map, "x") ?: return null |
| 146 | + val y = getOptionalLengthPercentage(map, "y") ?: return null |
| 147 | + val width = getOptionalLengthPercentage(map, "width") ?: return null |
| 148 | + val height = getOptionalLengthPercentage(map, "height") ?: return null |
| 149 | + val borderRadius = getOptionalLengthPercentage(map, "borderRadius") |
| 150 | + return XywhShape(x, y, width, height, borderRadius) |
| 151 | + } |
| 152 | + } |
| 153 | +} |
| 154 | + |
| 155 | +public sealed class BasicShape { |
| 156 | + public data class Circle(val shape: CircleShape) : BasicShape() |
| 157 | + |
| 158 | + public data class Ellipse(val shape: EllipseShape) : BasicShape() |
| 159 | + |
| 160 | + public data class Inset(val shape: InsetShape) : BasicShape() |
| 161 | + |
| 162 | + public data class Polygon(val shape: PolygonShape) : BasicShape() |
| 163 | + |
| 164 | + public data class Rect(val shape: RectShape) : BasicShape() |
| 165 | + |
| 166 | + public data class Xywh(val shape: XywhShape) : BasicShape() |
| 167 | + |
| 168 | + public companion object { |
| 169 | + public fun parse(map: ReadableMap): BasicShape? { |
| 170 | + if (!map.hasKey("type")) return null |
| 171 | + val type = map.getString("type") ?: return null |
| 172 | + |
| 173 | + return when (type.lowercase()) { |
| 174 | + "circle" -> { |
| 175 | + val circle = CircleShape.parse(map) ?: return null |
| 176 | + Circle(circle) |
| 177 | + } |
| 178 | + "ellipse" -> { |
| 179 | + val ellipse = EllipseShape.parse(map) ?: return null |
| 180 | + Ellipse(ellipse) |
| 181 | + } |
| 182 | + "inset" -> { |
| 183 | + val inset = InsetShape.parse(map) ?: return null |
| 184 | + Inset(inset) |
| 185 | + } |
| 186 | + "polygon" -> { |
| 187 | + val polygon = PolygonShape.parse(map) ?: return null |
| 188 | + Polygon(polygon) |
| 189 | + } |
| 190 | + "rect" -> { |
| 191 | + val rect = RectShape.parse(map) ?: return null |
| 192 | + Rect(rect) |
| 193 | + } |
| 194 | + "xywh" -> { |
| 195 | + val xywh = XywhShape.parse(map) ?: return null |
| 196 | + Xywh(xywh) |
| 197 | + } |
| 198 | + else -> null |
| 199 | + } |
| 200 | + } |
| 201 | + } |
| 202 | +} |
| 203 | + |
| 204 | +public enum class GeometryBox { |
| 205 | + MarginBox, |
| 206 | + BorderBox, |
| 207 | + ContentBox, |
| 208 | + PaddingBox, |
| 209 | + FillBox, |
| 210 | + StrokeBox, |
| 211 | + ViewBox; |
| 212 | + |
| 213 | + public companion object { |
| 214 | + public fun fromString(value: String): GeometryBox { |
| 215 | + return when (value.lowercase()) { |
| 216 | + "margin-box" -> MarginBox |
| 217 | + "border-box" -> BorderBox |
| 218 | + "content-box" -> ContentBox |
| 219 | + "padding-box" -> PaddingBox |
| 220 | + "fill-box" -> FillBox |
| 221 | + "stroke-box" -> StrokeBox |
| 222 | + "view-box" -> ViewBox |
| 223 | + else -> BorderBox |
| 224 | + } |
| 225 | + } |
| 226 | + } |
| 227 | +} |
| 228 | + |
| 229 | +public data class ClipPath( |
| 230 | + val shape: BasicShape? = null, |
| 231 | + val geometryBox: GeometryBox? = null, |
| 232 | +) { |
| 233 | + public companion object { |
| 234 | + public fun parse(map: ReadableMap?): ClipPath? { |
| 235 | + if (map == null) return null |
| 236 | + |
| 237 | + val shape = |
| 238 | + if (map.hasKey("shape") && map.getType("shape") == ReadableType.Map) { |
| 239 | + val shapeMap = map.getMap("shape") |
| 240 | + if (shapeMap != null) BasicShape.parse(shapeMap) else null |
| 241 | + } else { |
| 242 | + null |
| 243 | + } |
| 244 | + |
| 245 | + val geometryBox = |
| 246 | + if (map.hasKey("geometryBox")) { |
| 247 | + GeometryBox.fromString(map.getString("geometryBox") ?: "border-box") |
| 248 | + } else { |
| 249 | + null |
| 250 | + } |
| 251 | + |
| 252 | + return ClipPath(shape, geometryBox) |
| 253 | + } |
| 254 | + } |
| 255 | +} |
0 commit comments