1 /* 2 * Copyright (C) 2023 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.egg.landroid 18 19 import android.util.Log 20 import androidx.compose.ui.graphics.Path 21 import kotlin.math.cos 22 import kotlin.math.sin 23 24 fun createPolygon(radius: Float, sides: Int): Path { 25 return Path().apply { 26 moveTo(radius, 0f) 27 val angleStep = PI2f / sides 28 for (i in 1 until sides) { 29 lineTo(radius * cos(angleStep * i), radius * sin(angleStep * i)) 30 } 31 close() 32 } 33 } 34 35 fun createStar(radius1: Float, radius2: Float, points: Int): Path { 36 return Path().apply { 37 val angleStep = PI2f / points 38 moveTo(radius1, 0f) 39 lineTo(radius2 * cos(angleStep * (0.5f)), radius2 * sin(angleStep * (0.5f))) 40 for (i in 1 until points) { 41 lineTo(radius1 * cos(angleStep * i), radius1 * sin(angleStep * i)) 42 lineTo(radius2 * cos(angleStep * (i + 0.5f)), radius2 * sin(angleStep * (i + 0.5f))) 43 } 44 close() 45 } 46 } 47 48 fun Path.parseSvgPathData(d: String) { 49 Regex("([A-Z])([-.,0-9e ]+)").findAll(d.trim()).forEach { 50 val cmd = it.groups[1]!!.value 51 val args = 52 it.groups[2]?.value?.split(Regex("\\s+"))?.map { v -> v.toFloat() } ?: emptyList() 53 Log.d("Landroid", "cmd = $cmd, args = " + args.joinToString(",")) 54 when (cmd) { 55 "M" -> moveTo(args[0], args[1]) 56 "C" -> cubicTo(args[0], args[1], args[2], args[3], args[4], args[5]) 57 "L" -> lineTo(args[0], args[1]) 58 "Z" -> close() 59 else -> Log.v("Landroid", "unsupported SVG command: $cmd") 60 } 61 } 62 } 63