blob: 29a72fdf2288979a0a1588923e266e3e9ea0dea8 [file] [log] [blame]
ztenghuicf4832f2014-06-17 09:54:45 -07001/*
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;
ztenghuicf4832f2014-06-17 09:54:45 -070018
19/**
20 * @hide
21 */
22public class PathParser {
23 static final String LOGTAG = PathParser.class.getSimpleName();
24
25 /**
Doris Liucdd23f92015-11-11 14:31:13 -080026 * @param pathString The string representing a path, the same as "d" string in svg file.
ztenghuicf4832f2014-06-17 09:54:45 -070027 * @return the generated Path object.
28 */
Doris Liucdd23f92015-11-11 14:31:13 -080029 public static Path createPathFromPathData(String pathString) {
30 if (pathString == null) {
31 throw new IllegalArgumentException("Path string can not be null.");
ztenghuicf4832f2014-06-17 09:54:45 -070032 }
Doris Liucdd23f92015-11-11 14:31:13 -080033 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;
ztenghuicf4832f2014-06-17 09:54:45 -070041 }
42
43 /**
Doris Liu804618d2015-11-16 22:48:34 -080044 * 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.
ztenghuicf4832f2014-06-17 09:54:45 -070048 */
Doris Liu804618d2015-11-16 22:48:34 -080049 public static void createPathFromPathData(Path outPath, PathData data) {
50 nCreatePathFromPathData(outPath.mNativePath, data.mNativePathData);
ztenghuicf4832f2014-06-17 09:54:45 -070051 }
52
ztenghuieb034fb2014-06-09 13:14:19 -070053 /**
Doris Liu804618d2015-11-16 22:48:34 -080054 * @param pathDataFrom The source path represented in PathData
55 * @param pathDataTo The target path represented in PathData
ztenghuieb034fb2014-06-09 13:14:19 -070056 * @return whether the <code>nodesFrom</code> can morph into <code>nodesTo</code>
57 */
Doris Liu804618d2015-11-16 22:48:34 -080058 public static boolean canMorph(PathData pathDataFrom, PathData pathDataTo) {
59 return nCanMorph(pathDataFrom.mNativePathData, pathDataTo.mNativePathData);
ztenghuieb034fb2014-06-09 13:14:19 -070060 }
61
62 /**
Doris Liu804618d2015-11-16 22:48:34 -080063 * 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 Liu4bbc2932015-12-01 17:59:40 -080085 public long getNativePtr() {
86 return mNativePathData;
87 }
88
Doris Liu804618d2015-11-16 22:48:34 -080089 /**
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 Liu766431a2016-02-04 22:17:11 +0000107
Doris Liu804618d2015-11-16 22:48:34 -0800108 }
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>.
ztenghuieb034fb2014-06-09 13:14:19 -0700113 *
Doris Liu804618d2015-11-16 22:48:34 -0800114 * @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.
ztenghuieb034fb2014-06-09 13:14:19 -0700118 */
Doris Liu804618d2015-11-16 22:48:34 -0800119 public static boolean interpolatePathData(PathData outData, PathData fromData, PathData toData,
120 float fraction) {
121 return nInterpolatePathData(outData.mNativePathData, fromData.mNativePathData,
122 toData.mNativePathData, fraction);
ztenghuieb034fb2014-06-09 13:14:19 -0700123 }
124
Doris Liu804618d2015-11-16 22:48:34 -0800125 // Native functions are defined below.
Doris Liucdd23f92015-11-11 14:31:13 -0800126 private static native boolean nParseStringForPath(long pathPtr, String pathString,
127 int stringLength);
Doris Liu804618d2015-11-16 22:48:34 -0800128 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);
ztenghuicf4832f2014-06-17 09:54:45 -0700137}
Doris Liu804618d2015-11-16 22:48:34 -0800138
139