• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2014 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5  * in compliance with the License. You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software distributed under the License
10  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11  * or implied. See the License for the specific language governing permissions and limitations under
12  * the License.
13  */
14 
15 package android.util;
16 
17 import android.compat.annotation.UnsupportedAppUsage;
18 import android.graphics.Path;
19 import android.os.Build;
20 
21 import dalvik.annotation.optimization.FastNative;
22 
23 /**
24  * @hide
25  */
26 public class PathParser {
27     static final String LOGTAG = PathParser.class.getSimpleName();
28 
29     /**
30      * @param pathString The string representing a path, the same as "d" string in svg file.
31      * @return the generated Path object.
32      */
33     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
createPathFromPathData(String pathString)34     public static Path createPathFromPathData(String pathString) {
35         if (pathString == null) {
36             throw new IllegalArgumentException("Path string can not be null.");
37         }
38         Path path = new Path();
39         nParseStringForPath(path.mNativePath, pathString, pathString.length());
40         return path;
41     }
42 
43     /**
44      * Interpret PathData as path commands and insert the commands to the given path.
45      *
46      * @param data The source PathData to be converted.
47      * @param outPath The Path object where path commands will be inserted.
48      */
createPathFromPathData(Path outPath, PathData data)49     public static void createPathFromPathData(Path outPath, PathData data) {
50         nCreatePathFromPathData(outPath.mNativePath, data.mNativePathData);
51     }
52 
53     /**
54      * @param pathDataFrom The source path represented in PathData
55      * @param pathDataTo The target path represented in PathData
56      * @return whether the <code>nodesFrom</code> can morph into <code>nodesTo</code>
57      */
canMorph(PathData pathDataFrom, PathData pathDataTo)58     public static boolean canMorph(PathData pathDataFrom, PathData pathDataTo) {
59         return nCanMorph(pathDataFrom.mNativePathData, pathDataTo.mNativePathData);
60     }
61 
62     /**
63      * PathData class is a wrapper around the native PathData object, which contains
64      * the result of parsing a path string. Specifically, there are verbs and points
65      * associated with each verb stored in PathData. This data can then be used to
66      * generate commands to manipulate a Path.
67      */
68     public static class PathData {
69         long mNativePathData = 0;
PathData()70         public PathData() {
71             mNativePathData = nCreateEmptyPathData();
72         }
73 
PathData(PathData data)74         public PathData(PathData data) {
75             mNativePathData = nCreatePathData(data.mNativePathData);
76         }
77 
PathData(String pathString)78         public PathData(String pathString) {
79             mNativePathData = nCreatePathDataFromString(pathString, pathString.length());
80             if (mNativePathData == 0) {
81                 throw new IllegalArgumentException("Invalid pathData: " + pathString);
82             }
83         }
84 
getNativePtr()85         public long getNativePtr() {
86             return mNativePathData;
87         }
88 
89         /**
90          * Update the path data to match the source.
91          * Before calling this, make sure canMorph(target, source) is true.
92          *
93          * @param source The source path represented in PathData
94          */
setPathData(PathData source)95         public void setPathData(PathData source) {
96             nSetPathData(mNativePathData, source.mNativePathData);
97         }
98 
99         @Override
finalize()100         protected void finalize() throws Throwable {
101             if (mNativePathData != 0) {
102                 nFinalize(mNativePathData);
103                 mNativePathData = 0;
104             }
105             super.finalize();
106         }
107     }
108 
109     /**
110      * Interpolate between the <code>fromData</code> and <code>toData</code> according to the
111      * <code>fraction</code>, and put the resulting path data into <code>outData</code>.
112      *
113      * @param outData The resulting PathData of the interpolation
114      * @param fromData The start value as a PathData.
115      * @param toData The end value as a PathData
116      * @param fraction The fraction to interpolate.
117      */
interpolatePathData(PathData outData, PathData fromData, PathData toData, float fraction)118     public static boolean interpolatePathData(PathData outData, PathData fromData, PathData toData,
119             float fraction) {
120         return nInterpolatePathData(outData.mNativePathData, fromData.mNativePathData,
121                 toData.mNativePathData, fraction);
122     }
123 
124     // Native functions are defined below.
nParseStringForPath(long pathPtr, String pathString, int stringLength)125     private static native void nParseStringForPath(long pathPtr, String pathString,
126             int stringLength);
nCreatePathDataFromString(String pathString, int stringLength)127     private static native long nCreatePathDataFromString(String pathString, int stringLength);
128 
129     // ----------------- @FastNative -----------------------
130 
131     @FastNative
nCreatePathFromPathData(long outPathPtr, long pathData)132     private static native void nCreatePathFromPathData(long outPathPtr, long pathData);
133     @FastNative
nCreateEmptyPathData()134     private static native long nCreateEmptyPathData();
135     @FastNative
nCreatePathData(long nativePtr)136     private static native long nCreatePathData(long nativePtr);
137     @FastNative
nInterpolatePathData(long outDataPtr, long fromDataPtr, long toDataPtr, float fraction)138     private static native boolean nInterpolatePathData(long outDataPtr, long fromDataPtr,
139             long toDataPtr, float fraction);
140     @FastNative
nFinalize(long nativePtr)141     private static native void nFinalize(long nativePtr);
142     @FastNative
nCanMorph(long fromDataPtr, long toDataPtr)143     private static native boolean nCanMorph(long fromDataPtr, long toDataPtr);
144     @FastNative
nSetPathData(long outDataPtr, long fromDataPtr)145     private static native void nSetPathData(long outDataPtr, long fromDataPtr);
146 }
147 
148 
149