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