SubsetPath.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /*
  2. * Copyright 2015 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. #include "src/core/SkMathPriv.h"
  8. #include "tests/SubsetPath.h"
  9. SubsetPath::SubsetPath(const SkPath& path)
  10. : fPath(path)
  11. , fSubset(1) {
  12. }
  13. int SubsetPath::range(int* end) const {
  14. int leadingZero = SkCLZ(fSubset);
  15. int parts = 1 << (31 - leadingZero);
  16. int partIndex = fSubset - parts;
  17. SkASSERT(partIndex >= 0);
  18. int count = fSelected.count();
  19. int start = count * partIndex / parts;
  20. *end = count * (partIndex + 1) / parts;
  21. return start;
  22. }
  23. bool SubsetPath::subset(bool testFailed, SkPath* sub) {
  24. int start, end;
  25. if (!testFailed) {
  26. start = range(&end);
  27. for (; start < end; ++start) {
  28. fSelected[start] = true;
  29. }
  30. }
  31. do {
  32. do {
  33. ++fSubset;
  34. start = range(&end);
  35. // SkDebugf("%d s=%d e=%d t=%d\n", fSubset, start, end, fTries);
  36. if (end - start > 1) {
  37. fTries = fSelected.count();
  38. } else if (end - start == 1) {
  39. if (--fTries <= 0) {
  40. return false;
  41. }
  42. }
  43. } while (start == end);
  44. } while (!fSelected[start]);
  45. for (; start < end; ++start) {
  46. fSelected[start] = false;
  47. }
  48. #if 1
  49. SkDebugf("selected: ");
  50. for (int index = 0; index < fSelected.count(); ++index) {
  51. SkDebugf("%c", fSelected[index] ? 'x' : '-');
  52. }
  53. #endif
  54. *sub = getSubsetPath();
  55. return true;
  56. }
  57. SubsetContours::SubsetContours(const SkPath& path)
  58. : SubsetPath(path) {
  59. SkPath::RawIter iter(fPath);
  60. uint8_t verb;
  61. SkPoint pts[4];
  62. bool foundCurve = false;
  63. int contourCount = 0;
  64. while ((verb = iter.next(pts)) != SkPath::kDone_Verb) {
  65. switch (verb) {
  66. case SkPath::kMove_Verb:
  67. break;
  68. case SkPath::kLine_Verb:
  69. case SkPath::kQuad_Verb:
  70. case SkPath::kConic_Verb:
  71. case SkPath::kCubic_Verb:
  72. foundCurve = true;
  73. break;
  74. case SkPath::kClose_Verb:
  75. ++contourCount;
  76. foundCurve = false;
  77. break;
  78. default:
  79. SkDEBUGFAIL("bad verb");
  80. return;
  81. }
  82. }
  83. contourCount += foundCurve;
  84. for (int index = 0; index < contourCount; ++index) {
  85. *fSelected.append() = true;
  86. }
  87. fTries = contourCount;
  88. }
  89. SkPath SubsetContours::getSubsetPath() const {
  90. SkPath result;
  91. result.setFillType(fPath.getFillType());
  92. if (!fSelected.count()) {
  93. return result;
  94. }
  95. SkPath::RawIter iter(fPath);
  96. uint8_t verb;
  97. SkPoint pts[4];
  98. int contourCount = 0;
  99. bool enabled = fSelected[0];
  100. bool addMoveTo = true;
  101. while ((verb = iter.next(pts)) != SkPath::kDone_Verb) {
  102. if (enabled && addMoveTo) {
  103. result.moveTo(pts[0]);
  104. addMoveTo = false;
  105. }
  106. switch (verb) {
  107. case SkPath::kMove_Verb:
  108. break;
  109. case SkPath::kLine_Verb:
  110. if (enabled) {
  111. result.lineTo(pts[1]);
  112. }
  113. break;
  114. case SkPath::kQuad_Verb:
  115. if (enabled) {
  116. result.quadTo(pts[1], pts[2]);
  117. }
  118. break;
  119. case SkPath::kConic_Verb:
  120. if (enabled) {
  121. result.conicTo(pts[1], pts[2], iter.conicWeight());
  122. }
  123. break;
  124. case SkPath::kCubic_Verb:
  125. if (enabled) {
  126. result.cubicTo(pts[1], pts[2], pts[3]);
  127. }
  128. break;
  129. case SkPath::kClose_Verb:
  130. if (enabled) {
  131. result.close();
  132. }
  133. if (++contourCount >= fSelected.count()) {
  134. break;
  135. }
  136. enabled = fSelected[contourCount];
  137. addMoveTo = true;
  138. continue;
  139. default:
  140. SkDEBUGFAIL("bad verb");
  141. return result;
  142. }
  143. }
  144. return result;
  145. }
  146. SubsetVerbs::SubsetVerbs(const SkPath& path)
  147. : SubsetPath(path) {
  148. SkPath::RawIter iter(fPath);
  149. uint8_t verb;
  150. SkPoint pts[4];
  151. int verbCount = 0;
  152. while ((verb = iter.next(pts)) != SkPath::kDone_Verb) {
  153. switch (verb) {
  154. case SkPath::kMove_Verb:
  155. break;
  156. case SkPath::kLine_Verb:
  157. case SkPath::kQuad_Verb:
  158. case SkPath::kConic_Verb:
  159. case SkPath::kCubic_Verb:
  160. ++verbCount;
  161. break;
  162. case SkPath::kClose_Verb:
  163. break;
  164. default:
  165. SkDEBUGFAIL("bad verb");
  166. return;
  167. }
  168. }
  169. for (int index = 0; index < verbCount; ++index) {
  170. *fSelected.append() = true;
  171. }
  172. fTries = verbCount;
  173. }
  174. SkPath SubsetVerbs::getSubsetPath() const {
  175. SkPath result;
  176. result.setFillType(fPath.getFillType());
  177. if (!fSelected.count()) {
  178. return result;
  179. }
  180. SkPath::RawIter iter(fPath);
  181. uint8_t verb;
  182. SkPoint pts[4];
  183. int verbIndex = 0;
  184. bool addMoveTo = true;
  185. bool addLineTo = false;
  186. while ((verb = iter.next(pts)) != SkPath::kDone_Verb) {
  187. bool enabled = SkPath::kLine_Verb <= verb && verb <= SkPath::kCubic_Verb
  188. ? fSelected[verbIndex++] : false;
  189. if (enabled) {
  190. if (addMoveTo) {
  191. result.moveTo(pts[0]);
  192. addMoveTo = false;
  193. } else if (addLineTo) {
  194. result.lineTo(pts[0]);
  195. addLineTo = false;
  196. }
  197. }
  198. switch (verb) {
  199. case SkPath::kMove_Verb:
  200. break;
  201. case SkPath::kLine_Verb:
  202. if (enabled) {
  203. result.lineTo(pts[1]);
  204. }
  205. break;
  206. case SkPath::kQuad_Verb:
  207. if (enabled) {
  208. result.quadTo(pts[1], pts[2]);
  209. }
  210. break;
  211. case SkPath::kConic_Verb:
  212. if (enabled) {
  213. result.conicTo(pts[1], pts[2], iter.conicWeight());
  214. }
  215. break;
  216. case SkPath::kCubic_Verb:
  217. if (enabled) {
  218. result.cubicTo(pts[1], pts[2], pts[3]);
  219. }
  220. break;
  221. case SkPath::kClose_Verb:
  222. result.close();
  223. addMoveTo = true;
  224. addLineTo = false;
  225. continue;
  226. default:
  227. SkDEBUGFAIL("bad verb");
  228. return result;
  229. }
  230. addLineTo = !enabled;
  231. }
  232. return result;
  233. }