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 15package android.util; 16 17import android.graphics.Path; 18 19/** 20 * @hide 21 */ 22public class PathParser { 23 static final String LOGTAG = PathParser.class.getSimpleName(); 24 25 /** 26 * @param pathString The string representing a path, the same as "d" string in svg file. 27 * @return the generated Path object. 28 */ 29 public static Path createPathFromPathData(String pathString) { 30 if (pathString == null) { 31 throw new IllegalArgumentException("Path string can not be null."); 32 } 33 Path path = new Path(); 34 nParseStringForPath(path.mNativePath, pathString, pathString.length()); 35 return path; 36 } 37 38 /** 39 * Interpret PathData as path commands and insert the commands to the given path. 40 * 41 * @param data The source PathData to be converted. 42 * @param outPath The Path object where path commands will be inserted. 43 */ 44 public static void createPathFromPathData(Path outPath, PathData data) { 45 nCreatePathFromPathData(outPath.mNativePath, data.mNativePathData); 46 } 47 48 /** 49 * @param pathDataFrom The source path represented in PathData 50 * @param pathDataTo The target path represented in PathData 51 * @return whether the <code>nodesFrom</code> can morph into <code>nodesTo</code> 52 */ 53 public static boolean canMorph(PathData pathDataFrom, PathData pathDataTo) { 54 return nCanMorph(pathDataFrom.mNativePathData, pathDataTo.mNativePathData); 55 } 56 57 /** 58 * PathData class is a wrapper around the native PathData object, which contains 59 * the result of parsing a path string. Specifically, there are verbs and points 60 * associated with each verb stored in PathData. This data can then be used to 61 * generate commands to manipulate a Path. 62 */ 63 public static class PathData { 64 long mNativePathData = 0; 65 public PathData() { 66 mNativePathData = nCreateEmptyPathData(); 67 } 68 69 public PathData(PathData data) { 70 mNativePathData = nCreatePathData(data.mNativePathData); 71 } 72 73 public PathData(String pathString) { 74 mNativePathData = nCreatePathDataFromString(pathString, pathString.length()); 75 if (mNativePathData == 0) { 76 throw new IllegalArgumentException("Invalid pathData: " + pathString); 77 } 78 } 79 80 public long getNativePtr() { 81 return mNativePathData; 82 } 83 84 /** 85 * Update the path data to match the source. 86 * Before calling this, make sure canMorph(target, source) is true. 87 * 88 * @param source The source path represented in PathData 89 */ 90 public void setPathData(PathData source) { 91 nSetPathData(mNativePathData, source.mNativePathData); 92 } 93 94 @Override 95 protected void finalize() throws Throwable { 96 if (mNativePathData != 0) { 97 nFinalize(mNativePathData); 98 mNativePathData = 0; 99 } 100 super.finalize(); 101 } 102 } 103 104 /** 105 * Interpolate between the <code>fromData</code> and <code>toData</code> according to the 106 * <code>fraction</code>, and put the resulting path data into <code>outData</code>. 107 * 108 * @param outData The resulting PathData of the interpolation 109 * @param fromData The start value as a PathData. 110 * @param toData The end value as a PathData 111 * @param fraction The fraction to interpolate. 112 */ 113 public static boolean interpolatePathData(PathData outData, PathData fromData, PathData toData, 114 float fraction) { 115 return nInterpolatePathData(outData.mNativePathData, fromData.mNativePathData, 116 toData.mNativePathData, fraction); 117 } 118 119 // Native functions are defined below. 120 private static native void nParseStringForPath(long pathPtr, String pathString, 121 int stringLength); 122 private static native void nCreatePathFromPathData(long outPathPtr, long pathData); 123 private static native long nCreateEmptyPathData(); 124 private static native long nCreatePathData(long nativePtr); 125 private static native long nCreatePathDataFromString(String pathString, int stringLength); 126 private static native boolean nInterpolatePathData(long outDataPtr, long fromDataPtr, 127 long toDataPtr, float fraction); 128 private static native void nFinalize(long nativePtr); 129 private static native boolean nCanMorph(long fromDataPtr, long toDataPtr); 130 private static native void nSetPathData(long outDataPtr, long fromDataPtr); 131} 132 133 134