random_parse_path.cpp 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. * Copyright 2016 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/SkRandom.h"
  8. #include "tools/random_parse_path.h"
  9. const struct Legal {
  10. char fSymbol;
  11. int fScalars;
  12. } gLegal[] = {
  13. { 'M', 2 },
  14. { 'H', 1 },
  15. { 'V', 1 },
  16. { 'L', 2 },
  17. { 'Q', 4 },
  18. { 'T', 2 },
  19. { 'C', 6 },
  20. { 'S', 4 },
  21. { 'A', 4 },
  22. { 'Z', 0 },
  23. };
  24. bool gEasy = false; // set to true while debugging to suppress unusual whitespace
  25. // mostly do nothing, then bias towards spaces
  26. const char gWhiteSpace[] = { 0, 0, 0, 0, 0, 0, 0, 0, ' ', ' ', ' ', ' ', 0x09, 0x0D, 0x0A };
  27. static void add_white(SkRandom* rand, SkString* atom) {
  28. if (gEasy) {
  29. atom->append(" ");
  30. return;
  31. }
  32. int reps = rand->nextRangeU(0, 2);
  33. for (int rep = 0; rep < reps; ++rep) {
  34. int index = rand->nextRangeU(0, (int) SK_ARRAY_COUNT(gWhiteSpace) - 1);
  35. if (gWhiteSpace[index]) {
  36. atom->append(&gWhiteSpace[index], 1);
  37. }
  38. }
  39. }
  40. static void add_comma(SkRandom* rand, SkString* atom) {
  41. if (gEasy) {
  42. atom->append(",");
  43. return;
  44. }
  45. size_t count = atom->size();
  46. add_white(rand, atom);
  47. if (rand->nextBool()) {
  48. atom->append(",");
  49. }
  50. do {
  51. add_white(rand, atom);
  52. } while (count == atom->size());
  53. }
  54. static void add_some_white(SkRandom* rand, SkString* atom) {
  55. size_t count = atom->size();
  56. do {
  57. add_white(rand, atom);
  58. } while (count == atom->size());
  59. }
  60. SkString MakeRandomParsePathPiece(SkRandom* rand) {
  61. SkString atom;
  62. int index = rand->nextRangeU(0, (int) SK_ARRAY_COUNT(gLegal) - 1);
  63. const Legal& legal = gLegal[index];
  64. gEasy ? atom.append("\n") : add_white(rand, &atom);
  65. char symbol = legal.fSymbol | (rand->nextBool() ? 0x20 : 0);
  66. atom.append(&symbol, 1);
  67. int reps = rand->nextRangeU(1, 3);
  68. for (int rep = 0; rep < reps; ++rep) {
  69. for (int index = 0; index < legal.fScalars; ++index) {
  70. SkScalar coord = rand->nextRangeF(0, 100);
  71. add_white(rand, &atom);
  72. atom.appendScalar(coord);
  73. if (rep < reps - 1 && index < legal.fScalars - 1) {
  74. add_comma(rand, &atom);
  75. } else {
  76. add_some_white(rand, &atom);
  77. }
  78. if ('A' == legal.fSymbol && 1 == index) {
  79. atom.appendScalar(rand->nextRangeF(-720, 720));
  80. add_comma(rand, &atom);
  81. atom.appendU32(rand->nextRangeU(0, 1));
  82. add_comma(rand, &atom);
  83. atom.appendU32(rand->nextRangeU(0, 1));
  84. add_comma(rand, &atom);
  85. }
  86. }
  87. }
  88. return atom;
  89. }