ztenghui | cf4832f | 2014-06-17 09:54:45 -0700 | [diff] [blame] | 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.graphics.Path; |
ztenghui | cf4832f | 2014-06-17 09:54:45 -0700 | [diff] [blame] | 18 | |
| 19 | /** |
| 20 | * @hide |
| 21 | */ |
| 22 | public class PathParser { |
| 23 | static final String LOGTAG = PathParser.class.getSimpleName(); |
| 24 | |
| 25 | /** |
Doris Liu | cdd23f9 | 2015-11-11 14:31:13 -0800 | [diff] [blame] | 26 | * @param pathString The string representing a path, the same as "d" string in svg file. |
ztenghui | cf4832f | 2014-06-17 09:54:45 -0700 | [diff] [blame] | 27 | * @return the generated Path object. |
| 28 | */ |
Doris Liu | cdd23f9 | 2015-11-11 14:31:13 -0800 | [diff] [blame] | 29 | public static Path createPathFromPathData(String pathString) { |
| 30 | if (pathString == null) { |
| 31 | throw new IllegalArgumentException("Path string can not be null."); |
ztenghui | cf4832f | 2014-06-17 09:54:45 -0700 | [diff] [blame] | 32 | } |
Doris Liu | cdd23f9 | 2015-11-11 14:31:13 -0800 | [diff] [blame] | 33 | Path path = new Path(); |
| 34 | boolean hasValidPathData = nParseStringForPath(path.mNativePath, pathString, |
| 35 | pathString.length()); |
| 36 | if (!hasValidPathData) { |
| 37 | throw new IllegalArgumentException("Path string: " + pathString + |
| 38 | " does not contain valid path data"); |
| 39 | } |
| 40 | return path; |
ztenghui | cf4832f | 2014-06-17 09:54:45 -0700 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | /** |
Doris Liu | 804618d | 2015-11-16 22:48:34 -0800 | [diff] [blame] | 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. |
ztenghui | cf4832f | 2014-06-17 09:54:45 -0700 | [diff] [blame] | 48 | */ |
Doris Liu | 804618d | 2015-11-16 22:48:34 -0800 | [diff] [blame] | 49 | public static void createPathFromPathData(Path outPath, PathData data) { |
| 50 | nCreatePathFromPathData(outPath.mNativePath, data.mNativePathData); |
ztenghui | cf4832f | 2014-06-17 09:54:45 -0700 | [diff] [blame] | 51 | } |
| 52 | |
ztenghui | eb034fb | 2014-06-09 13:14:19 -0700 | [diff] [blame] | 53 | /** |
Doris Liu | 804618d | 2015-11-16 22:48:34 -0800 | [diff] [blame] | 54 | * @param pathDataFrom The source path represented in PathData |
| 55 | * @param pathDataTo The target path represented in PathData |
ztenghui | eb034fb | 2014-06-09 13:14:19 -0700 | [diff] [blame] | 56 | * @return whether the <code>nodesFrom</code> can morph into <code>nodesTo</code> |
| 57 | */ |
Doris Liu | 804618d | 2015-11-16 22:48:34 -0800 | [diff] [blame] | 58 | public static boolean canMorph(PathData pathDataFrom, PathData pathDataTo) { |
| 59 | return nCanMorph(pathDataFrom.mNativePathData, pathDataTo.mNativePathData); |
ztenghui | eb034fb | 2014-06-09 13:14:19 -0700 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | /** |
Doris Liu | 804618d | 2015-11-16 22:48:34 -0800 | [diff] [blame] | 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; |
| 70 | public PathData() { |
| 71 | mNativePathData = nCreateEmptyPathData(); |
| 72 | } |
| 73 | |
| 74 | public PathData(PathData data) { |
| 75 | mNativePathData = nCreatePathData(data.mNativePathData); |
| 76 | } |
| 77 | |
| 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 | |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 85 | public long getNativePtr() { |
| 86 | return mNativePathData; |
| 87 | } |
| 88 | |
Doris Liu | 804618d | 2015-11-16 22:48:34 -0800 | [diff] [blame] | 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 | */ |
| 95 | public void setPathData(PathData source) { |
| 96 | nSetPathData(mNativePathData, source.mNativePathData); |
| 97 | } |
| 98 | |
| 99 | @Override |
| 100 | protected void finalize() throws Throwable { |
| 101 | if (mNativePathData != 0) { |
| 102 | nFinalize(mNativePathData); |
| 103 | mNativePathData = 0; |
| 104 | } |
| 105 | super.finalize(); |
| 106 | } |
Doris Liu | 766431a | 2016-02-04 22:17:11 +0000 | [diff] [blame] | 107 | |
Doris Liu | 804618d | 2015-11-16 22:48:34 -0800 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Interpolate between the <code>fromData</code> and <code>toData</code> according to the |
| 112 | * <code>fraction</code>, and put the resulting path data into <code>outData</code>. |
ztenghui | eb034fb | 2014-06-09 13:14:19 -0700 | [diff] [blame] | 113 | * |
Doris Liu | 804618d | 2015-11-16 22:48:34 -0800 | [diff] [blame] | 114 | * @param outData The resulting PathData of the interpolation |
| 115 | * @param fromData The start value as a PathData. |
| 116 | * @param toData The end value as a PathData |
| 117 | * @param fraction The fraction to interpolate. |
ztenghui | eb034fb | 2014-06-09 13:14:19 -0700 | [diff] [blame] | 118 | */ |
Doris Liu | 804618d | 2015-11-16 22:48:34 -0800 | [diff] [blame] | 119 | public static boolean interpolatePathData(PathData outData, PathData fromData, PathData toData, |
| 120 | float fraction) { |
| 121 | return nInterpolatePathData(outData.mNativePathData, fromData.mNativePathData, |
| 122 | toData.mNativePathData, fraction); |
ztenghui | eb034fb | 2014-06-09 13:14:19 -0700 | [diff] [blame] | 123 | } |
| 124 | |
Doris Liu | 804618d | 2015-11-16 22:48:34 -0800 | [diff] [blame] | 125 | // Native functions are defined below. |
Doris Liu | cdd23f9 | 2015-11-11 14:31:13 -0800 | [diff] [blame] | 126 | private static native boolean nParseStringForPath(long pathPtr, String pathString, |
| 127 | int stringLength); |
Doris Liu | 804618d | 2015-11-16 22:48:34 -0800 | [diff] [blame] | 128 | private static native void nCreatePathFromPathData(long outPathPtr, long pathData); |
| 129 | private static native long nCreateEmptyPathData(); |
| 130 | private static native long nCreatePathData(long nativePtr); |
| 131 | private static native long nCreatePathDataFromString(String pathString, int stringLength); |
| 132 | private static native boolean nInterpolatePathData(long outDataPtr, long fromDataPtr, |
| 133 | long toDataPtr, float fraction); |
| 134 | private static native void nFinalize(long nativePtr); |
| 135 | private static native boolean nCanMorph(long fromDataPtr, long toDataPtr); |
| 136 | private static native void nSetPathData(long outDataPtr, long fromDataPtr); |
ztenghui | cf4832f | 2014-06-17 09:54:45 -0700 | [diff] [blame] | 137 | } |
Doris Liu | 804618d | 2015-11-16 22:48:34 -0800 | [diff] [blame] | 138 | |
| 139 | |