ParsePathTest.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*
  2. * Copyright 2011 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 "include/utils/SkParsePath.h"
  8. #include "tests/Test.h"
  9. static void test_to_from(skiatest::Reporter* reporter, const SkPath& path) {
  10. SkString str, str2;
  11. SkParsePath::ToSVGString(path, &str);
  12. SkPath path2;
  13. bool success = SkParsePath::FromSVGString(str.c_str(), &path2);
  14. REPORTER_ASSERT(reporter, success);
  15. SkParsePath::ToSVGString(path2, &str2);
  16. REPORTER_ASSERT(reporter, str == str2);
  17. #if 0 // closed paths are not equal, the iter explicitly gives the closing
  18. // edge, even if it is not in the path.
  19. REPORTER_ASSERT(reporter, path == path2);
  20. if (path != path2) {
  21. SkDebugf("str1=%s\nstr2=%s\n", str.c_str(), str2.c_str());
  22. }
  23. #endif
  24. }
  25. static struct {
  26. const char* fStr;
  27. const SkRect fBounds;
  28. } gRec[] = {
  29. { "M1,1 l-2.58-2.828-3.82-0.113, 1.9-3.3223-1.08-3.6702, 3.75,0.7744,3.16-2.1551,"
  30. "0.42,3.8008,3.02,2.3384-3.48,1.574-1.29,3.601z",
  31. { -5.39999962f, -10.3142f, 5.77000046f, 1.f } },
  32. { "", { 0, 0, 0, 0 } },
  33. { "M0,0L10,10", { 0, 0, SkIntToScalar(10), SkIntToScalar(10) } },
  34. { "M-5.5,-0.5 Q 0 0 6,6.50",
  35. { -5.5f, -0.5f,
  36. 6, 6.5f } }
  37. };
  38. DEF_TEST(ParsePath, reporter) {
  39. for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); i++) {
  40. SkPath path;
  41. bool success = SkParsePath::FromSVGString(gRec[i].fStr, &path);
  42. REPORTER_ASSERT(reporter, success);
  43. const SkRect& expectedBounds = gRec[i].fBounds;
  44. const SkRect& pathBounds = path.getBounds();
  45. REPORTER_ASSERT(reporter, expectedBounds == pathBounds);
  46. test_to_from(reporter, path);
  47. }
  48. SkRect r;
  49. r.set(0, 0, 10, 10.5f);
  50. SkPath p;
  51. p.addRect(r);
  52. test_to_from(reporter, p);
  53. p.addOval(r);
  54. test_to_from(reporter, p);
  55. p.addRoundRect(r, 4, 4.5f);
  56. test_to_from(reporter, p);
  57. }
  58. DEF_TEST(ParsePath_invalid, r) {
  59. SkPath path;
  60. // This is an invalid SVG string, but the test verifies that we do not
  61. // crash.
  62. bool success = SkParsePath::FromSVGString("M 5", &path);
  63. REPORTER_ASSERT(r, !success);
  64. }
  65. #include "include/utils/SkRandom.h"
  66. #include "tools/random_parse_path.h"
  67. DEF_TEST(ParsePathRandom, r) {
  68. SkRandom rand;
  69. for (int index = 0; index < 1000; ++index) {
  70. SkPath path, path2;
  71. SkString spec;
  72. uint32_t count = rand.nextRangeU(0, 10);
  73. for (uint32_t i = 0; i < count; ++i) {
  74. spec.append(MakeRandomParsePathPiece(&rand));
  75. }
  76. bool success = SkParsePath::FromSVGString(spec.c_str(), &path);
  77. REPORTER_ASSERT(r, success);
  78. }
  79. }
  80. DEF_TEST(ParsePathOptionalCommand, r) {
  81. struct {
  82. const char* fStr;
  83. int fVerbs;
  84. int fPoints;
  85. } gTests[] = {
  86. { "", 0, 0 },
  87. { "H100 200 ", 3, 3 },
  88. { "H-100-200", 3, 3 },
  89. { "H+100+200", 3, 3 },
  90. { "H.10.20" , 3, 3 },
  91. { "H-.10-.20", 3, 3 },
  92. { "H+.10+.20", 3, 3 },
  93. { "L100 100 200 200" , 3, 3 },
  94. { "L-100-100-200-200", 3, 3 },
  95. { "L+100+100+200+200", 3, 3 },
  96. { "L.10.10.20.20" , 3, 3 },
  97. { "L-.10-.10-.20-.20", 3, 3 },
  98. { "L+.10+.10+.20+.20", 3, 3 },
  99. { "C100 100 200 200 300 300 400 400 500 500 600 600" , 3, 7 },
  100. { "C100-100-200-200-300-300-400-400-500-500-600-600" , 3, 7 },
  101. { "C100+100+200+200+300+300+400+400+500+500+600+600" , 3, 7 },
  102. { "C.10.10.20.20.30.30.40.40.50.50.60.60" , 3, 7 },
  103. { "C-.10-.10-.20-.20-.30-.30-.40-.40-.50-.50-.60-.60", 3, 7 },
  104. { "C+.10+.10+.20+.20+.30+.30+.40+.40+.50+.50+.60+.60", 3, 7 },
  105. { "c-1.49.71-2.12 2.5-1.4 4 .71 1.49 2.5 2.12 4 1.4z", 4, 7 },
  106. };
  107. SkPath path;
  108. for (size_t i = 0; i < SK_ARRAY_COUNT(gTests); ++i) {
  109. REPORTER_ASSERT(r, SkParsePath::FromSVGString(gTests[i].fStr, &path));
  110. REPORTER_ASSERT(r, path.countVerbs() == gTests[i].fVerbs);
  111. REPORTER_ASSERT(r, path.countPoints() == gTests[i].fPoints);
  112. }
  113. }