robertphillips@google.com | d3d377f | 2012-12-07 20:56:13 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2012 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
| 8 | #include "path_utils.h" |
| 9 | #include "SkPath.h" |
| 10 | #include "SkStream.h" |
| 11 | |
| 12 | namespace sk_tools { |
| 13 | static int gCurPathID = 0; |
| 14 | |
| 15 | void dump_path_prefix(SkFILEWStream* pathStream) { |
| 16 | if (NULL == pathStream) { |
| 17 | return; |
| 18 | } |
skia.committer@gmail.com | c1f224a | 2012-12-08 02:01:38 +0000 | [diff] [blame] | 19 | |
robertphillips@google.com | d3d377f | 2012-12-07 20:56:13 +0000 | [diff] [blame] | 20 | pathStream->writeText("#include \"SkScalar.h\"\n"); |
| 21 | pathStream->writeText("#include \"SkPoint.h\"\n"); |
| 22 | pathStream->writeText("#include \"SkBitmap.h\"\n"); |
| 23 | pathStream->writeText("#include \"SkDevice.h\"\n"); |
| 24 | pathStream->writeText("#include \"SkString.h\"\n"); |
| 25 | pathStream->writeText("#include \"SkImageEncoder.h\"\n"); |
| 26 | } |
| 27 | |
| 28 | void dump_path(SkFILEWStream* pathStream, const SkPath& path) { |
| 29 | if (NULL == pathStream) { |
| 30 | return; |
| 31 | } |
| 32 | |
| 33 | static const int kMaxPts = 200; |
| 34 | static const int kMaxVerbs = 200; |
| 35 | |
| 36 | int numPts = path.countPoints(); |
| 37 | int numVerbs = path.countVerbs(); |
| 38 | |
| 39 | SkASSERT(numPts <= kMaxPts); |
| 40 | SkASSERT(numVerbs <= kMaxVerbs); |
| 41 | |
| 42 | SkPoint pts[kMaxPts]; |
| 43 | uint8_t verbs[kMaxVerbs]; |
| 44 | |
| 45 | path.getPoints(pts, kMaxPts); |
| 46 | path.getVerbs(verbs, kMaxVerbs); |
| 47 | |
| 48 | const char* gStrs[] = { |
| 49 | "kMove_Verb", |
| 50 | "kLine_Verb", |
| 51 | "kQuad_Verb", |
| 52 | "kCubic_Verb", |
| 53 | "kClose_Verb", |
| 54 | "kDone_Verb" |
| 55 | }; |
| 56 | |
| 57 | pathStream->writeText("static const int numPts"); |
| 58 | pathStream->writeDecAsText(gCurPathID); |
| 59 | pathStream->writeText(" = "); |
| 60 | pathStream->writeDecAsText(numPts); |
| 61 | pathStream->writeText(";\n"); |
| 62 | |
| 63 | pathStream->writeText("SkPoint pts"); |
| 64 | pathStream->writeDecAsText(gCurPathID); |
| 65 | pathStream->writeText("[] = {\n"); |
| 66 | |
| 67 | for (int i = 0; i < numPts; ++i) { |
| 68 | SkString temp; |
| 69 | |
| 70 | pathStream->writeText(" { "); |
| 71 | temp.appendScalar(pts[i].fX); |
| 72 | temp.append("f, "); |
| 73 | temp.appendScalar(pts[i].fY); |
| 74 | temp.append("f },\n"); |
| 75 | pathStream->writeText(temp.c_str()); |
| 76 | } |
| 77 | pathStream->writeText("};\n"); |
| 78 | |
| 79 | pathStream->writeText("static const int numVerbs"); |
| 80 | pathStream->writeDecAsText(gCurPathID); |
| 81 | pathStream->writeText(" = "); |
| 82 | pathStream->writeDecAsText(numVerbs); |
| 83 | pathStream->writeText(";\n"); |
skia.committer@gmail.com | c1f224a | 2012-12-08 02:01:38 +0000 | [diff] [blame] | 84 | |
robertphillips@google.com | d3d377f | 2012-12-07 20:56:13 +0000 | [diff] [blame] | 85 | pathStream->writeText("uint8_t verbs"); |
| 86 | pathStream->writeDecAsText(gCurPathID); |
| 87 | pathStream->writeText("[] = {\n"); |
| 88 | |
| 89 | for (int i = 0; i < numVerbs; ++i) { |
| 90 | pathStream->writeText("\tSkPath::"); |
| 91 | pathStream->writeText(gStrs[verbs[i]]); |
| 92 | pathStream->writeText(",\n"); |
| 93 | } |
| 94 | pathStream->writeText("};\n"); |
| 95 | |
| 96 | gCurPathID++; |
| 97 | } |
| 98 | |
| 99 | void dump_path_suffix(SkFILEWStream* pathStream) { |
| 100 | if (NULL == pathStream) { |
| 101 | return; |
| 102 | } |
| 103 | |
| 104 | pathStream->writeText("int numPaths = "); |
| 105 | pathStream->writeDecAsText(gCurPathID); |
| 106 | pathStream->writeText(";\n"); |
| 107 | |
| 108 | pathStream->writeText("int sizes[] = {\n"); |
| 109 | for (int i = 0; i < gCurPathID; ++i) { |
| 110 | pathStream->writeText("\t numPts"); |
| 111 | pathStream->writeDecAsText(i); |
| 112 | pathStream->writeText(", numVerbs"); |
| 113 | pathStream->writeDecAsText(i); |
| 114 | pathStream->writeText(",\n"); |
| 115 | } |
| 116 | pathStream->writeText("};\n"); |
| 117 | |
| 118 | pathStream->writeText("const SkPoint* points[] = {\n"); |
| 119 | for (int i = 0; i < gCurPathID; ++i) { |
| 120 | pathStream->writeText("\tpts"); |
| 121 | pathStream->writeDecAsText(i); |
| 122 | pathStream->writeText(",\n"); |
| 123 | } |
| 124 | pathStream->writeText("};\n"); |
| 125 | |
| 126 | pathStream->writeText("const uint8_t* verbs[] = {\n"); |
| 127 | for (int i = 0; i < gCurPathID; ++i) { |
| 128 | pathStream->writeText("\t(const uint8_t*)verbs"); |
| 129 | pathStream->writeDecAsText(i); |
| 130 | pathStream->writeText(",\n"); |
| 131 | } |
| 132 | pathStream->writeText("};\n"); |
| 133 | } |
| 134 | } |