# SvgPathParser

# SvgPathParser

Artifact:[androidx.graphics:graphics-shapes](https://developer.android.com/jetpack/androidx/releases/graphics)  
[View Source](https://cs.android.com/search?q=file:androidx/graphics/shapes/SvgPathParser.kt+class:androidx.graphics.shapes.SvgPathParser)  
Added in[1.1.0](https://developer.android.com/jetpack/androidx/releases/graphics#1.1.0)

*** ** * ** ***

Kotlin\|[Java](https://developer.android.com/reference/androidx/graphics/shapes/SvgPathParser "View this page in Java")
Common/AllAndroid/JVM
  - Cmn  

  ```
  class SvgPathParser
  ```

*** ** * ** ***

Converts each command (beside move to) of a svg path to a list of[Cubic](https://developer.android.com/reference/kotlin/androidx/graphics/shapes/Cubic)s by calling[SvgPathParser.parseCubics](https://developer.android.com/reference/kotlin/androidx/graphics/shapes/SvgPathParser.Companion#parseCubics(kotlin.String)). Any svg path complying to the specification found at https://www.w3.org/TR/SVG/paths.html is supported. Parameters can either be split by whitespace or by commas. There is very little error handling, so use with valid paths and consult the debug logs for unexpected[Cubic](https://developer.android.com/reference/kotlin/androidx/graphics/shapes/Cubic)objects.

## Summary

|                                                                              ### Public companion functions                                                                               |
|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-----|
| [List](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-list/index.html)`<`[Feature](https://developer.android.com/reference/kotlin/androidx/graphics/shapes/Feature)`>` | [parseFeatures](https://developer.android.com/reference/kotlin/androidx/graphics/shapes/SvgPathParser#parseFeatures(kotlin.String))`(svgPath: `[String](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html)`)` Converts an SVG path string into a list of[Feature](https://developer.android.com/reference/kotlin/androidx/graphics/shapes/Feature)objects. | Cmn |

## Public companion functions

### parseFeatures

Cmn  
Added in[1.1.0](https://developer.android.com/jetpack/androidx/releases/graphics#1.1.0)  

```
fun parseFeatures(svgPath: String): List<Feature>
```

Converts an SVG path string into a list of[Feature](https://developer.android.com/reference/kotlin/androidx/graphics/shapes/Feature)objects. The polygon described in the path should be representing a single, closed, non-self-intersecting polygon. Otherwise, either an error is thrown or the[Morph](https://developer.android.com/reference/kotlin/androidx/graphics/shapes/Morph)that the polygon is used in will be distorted.

**Note:**

- Only the first shape within the SVG path is processed. Subsequent shapes or holes are ignored.

- This method is primarily intended for development and testing purposes. For production use, serialize and parse the[Feature](https://developer.android.com/reference/kotlin/androidx/graphics/shapes/Feature)objects with[FeatureSerializer](https://developer.android.com/reference/kotlin/androidx/graphics/shapes/FeatureSerializer).

**Usage:**  

```kotlin
// Do the following three *once*
val triangleSVGPath: String = "M0,0 0.5,1 1,0Z"
val triangleFeatures: List<Feature> = SvgPathParser.parseFeatures(triangleSVGPath)
val serializedTriangle: String = FeatureSerializer.serialize(triangleFeatures)

// Parse the serialized triangle features in your production code.
// You can adjust them (e.g. the type) however you want before parsing.
val features: List<Feature> = FeatureSerializer.parse(serializedTriangle)
val triangle: RoundedPolygon = RoundedPolygon(features, centerX = 0.5f, centerY = 0.5f)
Morph(triangle, ...)
```  

|                                          Parameters                                          |
|----------------------------------------------------------------------------------------------|----------------------------------------------------------------------------------|
| `svgPath: `[String](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html) | The SVG path string, typically extracted from the`d`attribute of an SVG element. |

|                                                                                          Returns                                                                                          |
|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| [List](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-list/index.html)`<`[Feature](https://developer.android.com/reference/kotlin/androidx/graphics/shapes/Feature)`>` | A list of[Feature](https://developer.android.com/reference/kotlin/androidx/graphics/shapes/Feature)objects that can be used to create[RoundedPolygon](https://developer.android.com/reference/kotlin/androidx/graphics/shapes/RoundedPolygon)s |

|                                                             Throws                                                             |
|--------------------------------------------------------------------------------------------------------------------------------|---------------------------------------------------------------------------------------|
| [kotlin.IllegalArgumentException](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-illegal-argument-exception/index.html) | - if the SVG path is invalid or represents a non-closed or self-intersecting polygon. |