epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright 2011 Google Inc. |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
| 7 | */ |
reed@android.com | bbff1d5 | 2009-06-05 16:21:03 +0000 | [diff] [blame] | 8 | #include "SkParse.h" |
| 9 | #include "SkParsePath.h" |
| 10 | |
| 11 | static inline bool is_between(int c, int min, int max) { |
| 12 | return (unsigned)(c - min) <= (unsigned)(max - min); |
| 13 | } |
| 14 | |
| 15 | static inline bool is_ws(int c) { |
| 16 | return is_between(c, 1, 32); |
| 17 | } |
| 18 | |
| 19 | static inline bool is_digit(int c) { |
| 20 | return is_between(c, '0', '9'); |
| 21 | } |
| 22 | |
| 23 | static inline bool is_sep(int c) { |
| 24 | return is_ws(c) || c == ','; |
| 25 | } |
| 26 | |
| 27 | static inline bool is_lower(int c) { |
| 28 | return is_between(c, 'a', 'z'); |
| 29 | } |
| 30 | |
| 31 | static inline int to_upper(int c) { |
| 32 | return c - 'a' + 'A'; |
| 33 | } |
| 34 | |
| 35 | static const char* skip_ws(const char str[]) { |
| 36 | SkASSERT(str); |
| 37 | while (is_ws(*str)) |
| 38 | str++; |
| 39 | return str; |
| 40 | } |
| 41 | |
| 42 | static const char* skip_sep(const char str[]) { |
| 43 | SkASSERT(str); |
| 44 | while (is_sep(*str)) |
| 45 | str++; |
| 46 | return str; |
| 47 | } |
| 48 | |
| 49 | static const char* find_points(const char str[], SkPoint value[], int count, |
| 50 | bool isRelative, SkPoint* relative) { |
| 51 | str = SkParse::FindScalars(str, &value[0].fX, count * 2); |
| 52 | if (isRelative) { |
| 53 | for (int index = 0; index < count; index++) { |
| 54 | value[index].fX += relative->fX; |
| 55 | value[index].fY += relative->fY; |
| 56 | } |
| 57 | } |
| 58 | return str; |
| 59 | } |
| 60 | |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 61 | static const char* find_scalar(const char str[], SkScalar* value, |
reed@android.com | bbff1d5 | 2009-06-05 16:21:03 +0000 | [diff] [blame] | 62 | bool isRelative, SkScalar relative) { |
| 63 | str = SkParse::FindScalar(str, value); |
| 64 | if (isRelative) { |
| 65 | *value += relative; |
| 66 | } |
| 67 | return str; |
| 68 | } |
| 69 | |
| 70 | bool SkParsePath::FromSVGString(const char data[], SkPath* result) { |
| 71 | SkPath path; |
| 72 | SkPoint f = {0, 0}; |
| 73 | SkPoint c = {0, 0}; |
| 74 | SkPoint lastc = {0, 0}; |
| 75 | SkPoint points[3]; |
| 76 | char op = '\0'; |
| 77 | char previousOp = '\0'; |
| 78 | bool relative = false; |
| 79 | for (;;) { |
scroggo | 28e80f4 | 2015-12-11 09:49:57 -0800 | [diff] [blame] | 80 | if (!data) { |
| 81 | // Truncated data |
| 82 | return false; |
| 83 | } |
reed@android.com | bbff1d5 | 2009-06-05 16:21:03 +0000 | [diff] [blame] | 84 | data = skip_ws(data); |
| 85 | if (data[0] == '\0') { |
| 86 | break; |
| 87 | } |
| 88 | char ch = data[0]; |
| 89 | if (is_digit(ch) || ch == '-' || ch == '+') { |
| 90 | if (op == '\0') { |
| 91 | return false; |
| 92 | } |
caryclark | 52ee813 | 2015-01-29 09:45:44 -0800 | [diff] [blame] | 93 | } else if (is_sep(ch)) { |
| 94 | data = skip_sep(data); |
reed@android.com | bbff1d5 | 2009-06-05 16:21:03 +0000 | [diff] [blame] | 95 | } else { |
| 96 | op = ch; |
| 97 | relative = false; |
| 98 | if (is_lower(op)) { |
| 99 | op = (char) to_upper(op); |
| 100 | relative = true; |
| 101 | } |
| 102 | data++; |
| 103 | data = skip_sep(data); |
| 104 | } |
| 105 | switch (op) { |
| 106 | case 'M': |
| 107 | data = find_points(data, points, 1, relative, &c); |
| 108 | path.moveTo(points[0]); |
| 109 | op = 'L'; |
| 110 | c = points[0]; |
| 111 | break; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 112 | case 'L': |
reed@android.com | bbff1d5 | 2009-06-05 16:21:03 +0000 | [diff] [blame] | 113 | data = find_points(data, points, 1, relative, &c); |
| 114 | path.lineTo(points[0]); |
| 115 | c = points[0]; |
| 116 | break; |
| 117 | case 'H': { |
| 118 | SkScalar x; |
| 119 | data = find_scalar(data, &x, relative, c.fX); |
| 120 | path.lineTo(x, c.fY); |
| 121 | c.fX = x; |
| 122 | } break; |
| 123 | case 'V': { |
| 124 | SkScalar y; |
| 125 | data = find_scalar(data, &y, relative, c.fY); |
| 126 | path.lineTo(c.fX, y); |
| 127 | c.fY = y; |
| 128 | } break; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 129 | case 'C': |
reed@android.com | bbff1d5 | 2009-06-05 16:21:03 +0000 | [diff] [blame] | 130 | data = find_points(data, points, 3, relative, &c); |
| 131 | goto cubicCommon; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 132 | case 'S': |
reed@android.com | bbff1d5 | 2009-06-05 16:21:03 +0000 | [diff] [blame] | 133 | data = find_points(data, &points[1], 2, relative, &c); |
| 134 | points[0] = c; |
| 135 | if (previousOp == 'C' || previousOp == 'S') { |
| 136 | points[0].fX -= lastc.fX - c.fX; |
| 137 | points[0].fY -= lastc.fY - c.fY; |
| 138 | } |
| 139 | cubicCommon: |
| 140 | path.cubicTo(points[0], points[1], points[2]); |
| 141 | lastc = points[1]; |
| 142 | c = points[2]; |
| 143 | break; |
| 144 | case 'Q': // Quadratic Bezier Curve |
| 145 | data = find_points(data, points, 2, relative, &c); |
| 146 | goto quadraticCommon; |
| 147 | case 'T': |
| 148 | data = find_points(data, &points[1], 1, relative, &c); |
| 149 | points[0] = points[1]; |
| 150 | if (previousOp == 'Q' || previousOp == 'T') { |
| 151 | points[0].fX = c.fX * 2 - lastc.fX; |
| 152 | points[0].fY = c.fY * 2 - lastc.fY; |
| 153 | } |
| 154 | quadraticCommon: |
| 155 | path.quadTo(points[0], points[1]); |
| 156 | lastc = points[0]; |
| 157 | c = points[1]; |
| 158 | break; |
| 159 | case 'Z': |
| 160 | path.close(); |
| 161 | #if 0 // !!! still a bug? |
| 162 | if (fPath.isEmpty() && (f.fX != 0 || f.fY != 0)) { |
| 163 | c.fX -= SkScalar.Epsilon; // !!! enough? |
| 164 | fPath.moveTo(c); |
| 165 | fPath.lineTo(f); |
| 166 | fPath.close(); |
| 167 | } |
| 168 | #endif |
| 169 | c = f; |
| 170 | op = '\0'; |
| 171 | break; |
| 172 | case '~': { |
| 173 | SkPoint args[2]; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 174 | data = find_points(data, args, 2, false, nullptr); |
reed@android.com | bbff1d5 | 2009-06-05 16:21:03 +0000 | [diff] [blame] | 175 | path.moveTo(args[0].fX, args[0].fY); |
| 176 | path.lineTo(args[1].fX, args[1].fY); |
| 177 | } break; |
| 178 | default: |
| 179 | return false; |
| 180 | } |
| 181 | if (previousOp == 0) { |
| 182 | f = c; |
| 183 | } |
| 184 | previousOp = op; |
| 185 | } |
| 186 | // we're good, go ahead and swap in the result |
| 187 | result->swap(path); |
| 188 | return true; |
| 189 | } |
| 190 | |
| 191 | /////////////////////////////////////////////////////////////////////////////// |
| 192 | |
reed | 220f926 | 2014-12-17 08:21:04 -0800 | [diff] [blame] | 193 | #include "SkGeometry.h" |
reed@android.com | bbff1d5 | 2009-06-05 16:21:03 +0000 | [diff] [blame] | 194 | #include "SkString.h" |
| 195 | #include "SkStream.h" |
| 196 | |
| 197 | static void write_scalar(SkWStream* stream, SkScalar value) { |
reed@android.com | a8cf0aa | 2009-09-24 20:06:03 +0000 | [diff] [blame] | 198 | char buffer[64]; |
reed@android.com | e191b16 | 2009-12-18 21:33:39 +0000 | [diff] [blame] | 199 | #ifdef SK_BUILD_FOR_WIN32 |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 200 | int len = _snprintf(buffer, sizeof(buffer), "%g", value); |
reed@android.com | e191b16 | 2009-12-18 21:33:39 +0000 | [diff] [blame] | 201 | #else |
reed@android.com | a8cf0aa | 2009-09-24 20:06:03 +0000 | [diff] [blame] | 202 | int len = snprintf(buffer, sizeof(buffer), "%g", value); |
reed@android.com | e191b16 | 2009-12-18 21:33:39 +0000 | [diff] [blame] | 203 | #endif |
reed@android.com | a8cf0aa | 2009-09-24 20:06:03 +0000 | [diff] [blame] | 204 | char* stop = buffer + len; |
reed@android.com | bbff1d5 | 2009-06-05 16:21:03 +0000 | [diff] [blame] | 205 | stream->write(buffer, stop - buffer); |
| 206 | } |
| 207 | |
| 208 | static void append_scalars(SkWStream* stream, char verb, const SkScalar data[], |
| 209 | int count) { |
| 210 | stream->write(&verb, 1); |
| 211 | write_scalar(stream, data[0]); |
| 212 | for (int i = 1; i < count; i++) { |
reed@android.com | a8cf0aa | 2009-09-24 20:06:03 +0000 | [diff] [blame] | 213 | stream->write(" ", 1); |
reed@android.com | bbff1d5 | 2009-06-05 16:21:03 +0000 | [diff] [blame] | 214 | write_scalar(stream, data[i]); |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | void SkParsePath::ToSVGString(const SkPath& path, SkString* str) { |
| 219 | SkDynamicMemoryWStream stream; |
| 220 | |
| 221 | SkPath::Iter iter(path, false); |
| 222 | SkPoint pts[4]; |
| 223 | |
| 224 | for (;;) { |
reed@google.com | 4a3b714 | 2012-05-16 17:16:46 +0000 | [diff] [blame] | 225 | switch (iter.next(pts, false)) { |
reed | 220f926 | 2014-12-17 08:21:04 -0800 | [diff] [blame] | 226 | case SkPath::kConic_Verb: { |
| 227 | const SkScalar tol = SK_Scalar1 / 1024; // how close to a quad |
| 228 | SkAutoConicToQuads quadder; |
| 229 | const SkPoint* quadPts = quadder.computeQuads(pts, iter.conicWeight(), tol); |
| 230 | for (int i = 0; i < quadder.countQuads(); ++i) { |
| 231 | append_scalars(&stream, 'Q', &quadPts[i*2 + 1].fX, 4); |
| 232 | } |
| 233 | } break; |
reed@google.com | 277c3f8 | 2013-05-31 15:17:50 +0000 | [diff] [blame] | 234 | case SkPath::kMove_Verb: |
reed@android.com | bbff1d5 | 2009-06-05 16:21:03 +0000 | [diff] [blame] | 235 | append_scalars(&stream, 'M', &pts[0].fX, 2); |
| 236 | break; |
| 237 | case SkPath::kLine_Verb: |
| 238 | append_scalars(&stream, 'L', &pts[1].fX, 2); |
| 239 | break; |
| 240 | case SkPath::kQuad_Verb: |
| 241 | append_scalars(&stream, 'Q', &pts[1].fX, 4); |
| 242 | break; |
| 243 | case SkPath::kCubic_Verb: |
| 244 | append_scalars(&stream, 'C', &pts[1].fX, 6); |
| 245 | break; |
| 246 | case SkPath::kClose_Verb: |
| 247 | stream.write("Z", 1); |
| 248 | break; |
| 249 | case SkPath::kDone_Verb: |
| 250 | str->resize(stream.getOffset()); |
| 251 | stream.copyTo(str->writable_str()); |
| 252 | return; |
| 253 | } |
| 254 | } |
| 255 | } |