diff options
author | reed@android.com <reed@android.com@2bbb7eff-a529-9590-31e7-b0007b416f81> | 2009-06-05 16:21:03 +0000 |
---|---|---|
committer | reed@android.com <reed@android.com@2bbb7eff-a529-9590-31e7-b0007b416f81> | 2009-06-05 16:21:03 +0000 |
commit | bbff1d507aa3f470139e2df409e94074a2f9425f (patch) | |
tree | 1c58cdd9211057604ad3b3c804637e784f83ce3c /src | |
parent | ec3d6e5a2fada32dfc762ed82ca6155746f664a3 (diff) |
add SkParsePath to go to/from SVG strings (e.g. "M0,0 L10,20")
git-svn-id: http://skia.googlecode.com/svn/trunk@203 2bbb7eff-a529-9590-31e7-b0007b416f81
Diffstat (limited to 'src')
-rw-r--r-- | src/core/SkPath.cpp | 55 | ||||
-rw-r--r-- | src/utils/SkParse.cpp (renamed from src/xml/SkParse.cpp) | 0 | ||||
-rw-r--r-- | src/utils/SkParseColor.cpp (renamed from src/xml/SkParseColor.cpp) | 0 | ||||
-rw-r--r-- | src/utils/SkParsePath.cpp | 232 | ||||
-rw-r--r-- | src/utils/utils_files.mk | 3 |
5 files changed, 235 insertions, 55 deletions
diff --git a/src/core/SkPath.cpp b/src/core/SkPath.cpp index 2127bb4581..082d2e8667 100644 --- a/src/core/SkPath.cpp +++ b/src/core/SkPath.cpp @@ -1220,61 +1220,6 @@ void SkPath::unflatten(SkFlattenableReadBuffer& buffer) { } /////////////////////////////////////////////////////////////////////////////// - -#include "SkString.h" -#include "SkStream.h" - -static void write_scalar(SkWStream* stream, SkScalar value) { - char buffer[SkStrAppendScalar_MaxSize]; - char* stop = SkStrAppendScalar(buffer, value); - stream->write(buffer, stop - buffer); -} - -static void append_scalars(SkWStream* stream, char verb, const SkScalar data[], - int count) { - stream->write(&verb, 1); - write_scalar(stream, data[0]); - for (int i = 1; i < count; i++) { - if (data[i] >= 0) { - // can skip the separater if data[i] is negative - stream->write(" ", 1); - } - write_scalar(stream, data[i]); - } -} - -void SkPath::toString(SkString* str) const { - SkDynamicMemoryWStream stream; - - SkPath::Iter iter(*this, false); - SkPoint pts[4]; - - for (;;) { - switch (iter.next(pts)) { - case SkPath::kMove_Verb: - append_scalars(&stream, 'M', &pts[0].fX, 2); - break; - case SkPath::kLine_Verb: - append_scalars(&stream, 'L', &pts[1].fX, 2); - break; - case SkPath::kQuad_Verb: - append_scalars(&stream, 'Q', &pts[1].fX, 4); - break; - case SkPath::kCubic_Verb: - append_scalars(&stream, 'C', &pts[1].fX, 6); - break; - case SkPath::kClose_Verb: - stream.write("Z", 1); - break; - case SkPath::kDone_Verb: - str->resize(stream.getOffset()); - stream.copyTo(str->writable_str()); - return; - } - } -} - -/////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////// #ifdef SK_DEBUG diff --git a/src/xml/SkParse.cpp b/src/utils/SkParse.cpp index e7402801a6..e7402801a6 100644 --- a/src/xml/SkParse.cpp +++ b/src/utils/SkParse.cpp diff --git a/src/xml/SkParseColor.cpp b/src/utils/SkParseColor.cpp index c451253edd..c451253edd 100644 --- a/src/xml/SkParseColor.cpp +++ b/src/utils/SkParseColor.cpp diff --git a/src/utils/SkParsePath.cpp b/src/utils/SkParsePath.cpp new file mode 100644 index 0000000000..df98436364 --- /dev/null +++ b/src/utils/SkParsePath.cpp @@ -0,0 +1,232 @@ +#include "SkParse.h" +#include "SkParsePath.h" + +static inline bool is_between(int c, int min, int max) { + return (unsigned)(c - min) <= (unsigned)(max - min); +} + +static inline bool is_ws(int c) { + return is_between(c, 1, 32); +} + +static inline bool is_digit(int c) { + return is_between(c, '0', '9'); +} + +static inline bool is_sep(int c) { + return is_ws(c) || c == ','; +} + +static inline bool is_lower(int c) { + return is_between(c, 'a', 'z'); +} + +static inline int to_upper(int c) { + return c - 'a' + 'A'; +} + +static const char* skip_ws(const char str[]) { + SkASSERT(str); + while (is_ws(*str)) + str++; + return str; +} + +static const char* skip_sep(const char str[]) { + SkASSERT(str); + while (is_sep(*str)) + str++; + return str; +} + +static const char* find_points(const char str[], SkPoint value[], int count, + bool isRelative, SkPoint* relative) { + str = SkParse::FindScalars(str, &value[0].fX, count * 2); + if (isRelative) { + for (int index = 0; index < count; index++) { + value[index].fX += relative->fX; + value[index].fY += relative->fY; + } + } + return str; +} + +static const char* find_scalar(const char str[], SkScalar* value, + bool isRelative, SkScalar relative) { + str = SkParse::FindScalar(str, value); + if (isRelative) { + *value += relative; + } + return str; +} + +bool SkParsePath::FromSVGString(const char data[], SkPath* result) { + SkPath path; + SkPoint f = {0, 0}; + SkPoint c = {0, 0}; + SkPoint lastc = {0, 0}; + SkPoint points[3]; + char op = '\0'; + char previousOp = '\0'; + bool relative = false; + for (;;) { + data = skip_ws(data); + if (data[0] == '\0') { + break; + } + char ch = data[0]; + if (is_digit(ch) || ch == '-' || ch == '+') { + if (op == '\0') { + return false; + } + } else { + op = ch; + relative = false; + if (is_lower(op)) { + op = (char) to_upper(op); + relative = true; + } + data++; + data = skip_sep(data); + } + switch (op) { + case 'M': + data = find_points(data, points, 1, relative, &c); + path.moveTo(points[0]); + op = 'L'; + c = points[0]; + break; + case 'L': + data = find_points(data, points, 1, relative, &c); + path.lineTo(points[0]); + c = points[0]; + break; + case 'H': { + SkScalar x; + data = find_scalar(data, &x, relative, c.fX); + path.lineTo(x, c.fY); + c.fX = x; + } break; + case 'V': { + SkScalar y; + data = find_scalar(data, &y, relative, c.fY); + path.lineTo(c.fX, y); + c.fY = y; + } break; + case 'C': + data = find_points(data, points, 3, relative, &c); + goto cubicCommon; + case 'S': + data = find_points(data, &points[1], 2, relative, &c); + points[0] = c; + if (previousOp == 'C' || previousOp == 'S') { + points[0].fX -= lastc.fX - c.fX; + points[0].fY -= lastc.fY - c.fY; + } + cubicCommon: + path.cubicTo(points[0], points[1], points[2]); + lastc = points[1]; + c = points[2]; + break; + case 'Q': // Quadratic Bezier Curve + data = find_points(data, points, 2, relative, &c); + goto quadraticCommon; + case 'T': + data = find_points(data, &points[1], 1, relative, &c); + points[0] = points[1]; + if (previousOp == 'Q' || previousOp == 'T') { + points[0].fX = c.fX * 2 - lastc.fX; + points[0].fY = c.fY * 2 - lastc.fY; + } + quadraticCommon: + path.quadTo(points[0], points[1]); + lastc = points[0]; + c = points[1]; + break; + case 'Z': + path.close(); +#if 0 // !!! still a bug? + if (fPath.isEmpty() && (f.fX != 0 || f.fY != 0)) { + c.fX -= SkScalar.Epsilon; // !!! enough? + fPath.moveTo(c); + fPath.lineTo(f); + fPath.close(); + } +#endif + c = f; + op = '\0'; + break; + case '~': { + SkPoint args[2]; + data = find_points(data, args, 2, false, NULL); + path.moveTo(args[0].fX, args[0].fY); + path.lineTo(args[1].fX, args[1].fY); + } break; + default: + return false; + } + if (previousOp == 0) { + f = c; + } + previousOp = op; + } + // we're good, go ahead and swap in the result + result->swap(path); + return true; +} + +/////////////////////////////////////////////////////////////////////////////// + +#include "SkString.h" +#include "SkStream.h" + +static void write_scalar(SkWStream* stream, SkScalar value) { + char buffer[SkStrAppendScalar_MaxSize]; + char* stop = SkStrAppendScalar(buffer, value); + stream->write(buffer, stop - buffer); +} + +static void append_scalars(SkWStream* stream, char verb, const SkScalar data[], + int count) { + stream->write(&verb, 1); + write_scalar(stream, data[0]); + for (int i = 1; i < count; i++) { + if (data[i] >= 0) { + // can skip the separater if data[i] is negative + stream->write(" ", 1); + } + write_scalar(stream, data[i]); + } +} + +void SkParsePath::ToSVGString(const SkPath& path, SkString* str) { + SkDynamicMemoryWStream stream; + + SkPath::Iter iter(path, false); + SkPoint pts[4]; + + for (;;) { + switch (iter.next(pts)) { + case SkPath::kMove_Verb: + append_scalars(&stream, 'M', &pts[0].fX, 2); + break; + case SkPath::kLine_Verb: + append_scalars(&stream, 'L', &pts[1].fX, 2); + break; + case SkPath::kQuad_Verb: + append_scalars(&stream, 'Q', &pts[1].fX, 4); + break; + case SkPath::kCubic_Verb: + append_scalars(&stream, 'C', &pts[1].fX, 6); + break; + case SkPath::kClose_Verb: + stream.write("Z", 1); + break; + case SkPath::kDone_Verb: + str->resize(stream.getOffset()); + stream.copyTo(str->writable_str()); + return; + } + } +} + diff --git a/src/utils/utils_files.mk b/src/utils/utils_files.mk index a8c9496a86..a744af0978 100644 --- a/src/utils/utils_files.mk +++ b/src/utils/utils_files.mk @@ -5,6 +5,9 @@ SOURCE := \ SkDumpCanvas.cpp \ SkInterpolator.cpp \ SkNinePatch.cpp \ + SkParse.cpp \ + SkParseColor.cpp \ + SkParsePath.cpp \ SkProxyCanvas.cpp \ SkSfntUtils.cpp \ SkUnitMappers.cpp |