FeatureSerializer
-
Cmn
class FeatureSerializer
The FeatureSerializer is used to both serialize and parse Feature objects. This is beneficial when you want to re-use RoundedPolygon objects created by SvgPathParser, as parsing serialized Feature objects is more performant than using the svg path import.
Example:
// 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, ...)Summary
Public companion functions |
||
|---|---|---|
List<Feature> |
Parses a serialized string representation of |
Cmn
|
String |
Serializes a list of |
Cmn
|
Public companion functions
parse
fun parse(serializedFeatures: String): List<Feature>
Parses a serialized string representation of Feature into their object representations, adhering to version 1 of the feature serialization format.
The serialized string must adhere to the format generated by FeatureSerializer.serialize. This format consists of:
-
Version Identifier: A 'V' followed by the version number (e.g., 'V1').
-
Feature Serialization: Each
Featureis serialized individually and concatenated. -
Feature Tag: A single-character tag identifies the feature type:
-
'n': Edge
-
'x': Convex Corner
-
'o': Concave Corner
-
Any other tags unknown to version 1 will default to an edge interpretation.
-
Cubic Serialization: Each
Cubicwithin a feature is serialized as a sequence of comma-separated x,y coordinates. Subsequent cubics within a feature omit their first anchor points, as they're identical to their predecessors' last anchor points.
Note: The current version (1) of the serialization format is stable. However, future versions may introduce incompatible changes with version 1. The default behavior for unknown or missing versions will be version 1 parsing. If you have a later version string, update the library to the latest version.
| Throws | |
|---|---|
kotlin.IllegalArgumentException |
|
kotlin.IllegalArgumentException |
|
kotlin.NumberFormatException |
|
serialize
fun serialize(features: List<Feature>): String
Serializes a list of Feature objects into a string representation, adhering to version 1 of the feature serialization format.
Format:
-
Version Identifier: A 'V' followed by the version number (e.g., 'V1').
-
Feature Serialization: Each
Featureis serialized individually and concatenated. -
Feature Tag: A single-character tag identifies the feature type:
-
'n': Edge
-
'x': Convex Corner
-
'o': Concave Corner
-
Cubic Serialization: Each
Cubicwithin a feature is serialized as a sequence of comma-separated x,y coordinates. Subsequent cubics within a feature omit their first anchor points, as they're identical to their predecessors' last anchor points.
Example: Given two features:
-
An edge with one
Cubic: { (0, 0), (1, 1), (2, 2), (3, 3) } -
A convex corner with two
Cubicobjects: { (0, 0), (1, 1), (2, 2), (3, 3) }, { (3, 3), (4, 4), (5, 5), (6, 6) }
The serialized string would be:
V1 n 0,0, 1,1, 2,2, 3,3 x 0,0, 1,1, 2,2, 3,3, 4,4, 5,5, 6,6