Florin Malita | dec7802 | 2020-12-17 16:36:54 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2020 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 <vector> |
| 9 | |
| 10 | #include "modules/svg/src/SkSVGTextPriv.h" |
| 11 | #include "tests/Test.h" |
| 12 | |
| 13 | DEF_TEST(Svg_Text_PosProvider, r) { |
| 14 | const auto L = [](float x) { return SkSVGLength(x); }; |
| 15 | const float N = SkSVGTextContext::PosAttrs()[SkSVGTextContext::PosAttrs::kX]; |
| 16 | |
| 17 | static const struct PosTestDesc { |
| 18 | size_t offseta; |
| 19 | std::vector<SkSVGLength> xa, ya; |
| 20 | |
| 21 | size_t offsetb; |
| 22 | std::vector<SkSVGLength> xb, yb; |
| 23 | |
| 24 | std::vector<SkPoint> expected; |
| 25 | } gTests[] = { |
| 26 | { |
| 27 | 0, {}, {}, |
| 28 | 0, {}, {}, |
| 29 | |
| 30 | { {N,N} } |
| 31 | }, |
| 32 | |
| 33 | { |
| 34 | 0, { L(1) }, {}, |
| 35 | 0, { }, {}, |
| 36 | |
| 37 | { {1,N}, {N,N} } |
| 38 | }, |
| 39 | { |
| 40 | 0, { }, {}, |
| 41 | 0, { L(10) }, {}, |
| 42 | |
| 43 | { {10,N}, {N,N} } |
| 44 | }, |
| 45 | { |
| 46 | 0, { L( 1) }, {}, |
| 47 | 0, { L(10) }, {}, |
| 48 | |
| 49 | { {10,N}, {N,N} } |
| 50 | }, |
| 51 | { |
| 52 | 0, { L( 1), L(2) }, {}, |
| 53 | 0, { L(10) }, {}, |
| 54 | |
| 55 | { {10,N}, {2,N}, {N,N} } |
| 56 | }, |
| 57 | { |
| 58 | 0, { L(1), L( 2) }, {}, |
| 59 | 1, { L(20) }, {}, |
| 60 | |
| 61 | { {1,N}, {20,N}, {N,N} } |
| 62 | }, |
| 63 | { |
| 64 | 0, { L(1), L( 2), L(3) }, {}, |
| 65 | 1, { L(20) }, {}, |
| 66 | |
| 67 | { {1,N}, {20,N}, {3,N}, {N,N} } |
| 68 | }, |
| 69 | { |
| 70 | 0, { L(1), L(2), L( 3) }, {}, |
| 71 | 2, { L(30) }, {}, |
| 72 | |
| 73 | { {1,N}, {2,N}, {30,N}, {N,N} } |
| 74 | }, |
| 75 | { |
| 76 | 0, { L(1) }, {}, |
| 77 | 2, { L(30) }, {}, |
| 78 | |
| 79 | { {1,N}, {N,N}, {30,N}, {N,N} } |
| 80 | }, |
| 81 | |
| 82 | |
| 83 | { |
| 84 | 0, {}, { L(4) }, |
| 85 | 0, {}, { }, |
| 86 | |
| 87 | { {N,4}, {N,N} } |
| 88 | }, |
| 89 | { |
| 90 | 0, {}, { }, |
| 91 | 0, {}, { L(40) }, |
| 92 | |
| 93 | { {N,40}, {N,N} } |
| 94 | }, |
| 95 | { |
| 96 | 0, {}, { L( 4) }, |
| 97 | 0, {}, { L(40) }, |
| 98 | |
| 99 | { {N,40}, {N,N} } |
| 100 | }, |
| 101 | { |
| 102 | 0, {}, { L( 4), L(5) }, |
| 103 | 0, {}, { L(40) }, |
| 104 | |
| 105 | { {N,40}, {N,5}, {N,N} } |
| 106 | }, |
| 107 | { |
| 108 | 0, {}, { L(4), L( 5) }, |
| 109 | 1, {}, { L(50) }, |
| 110 | |
| 111 | { {N,4}, {N,50}, {N,N} } |
| 112 | }, |
| 113 | { |
| 114 | 0, {}, { L(4), L( 5), L(6) }, |
| 115 | 1, {}, { L(50) }, |
| 116 | |
| 117 | { {N,4}, {N,50}, {N,6}, {N,N} } |
| 118 | }, |
| 119 | { |
| 120 | 0, {}, { L(4), L(5), L( 6) }, |
| 121 | 2, {}, { L(60) }, |
| 122 | |
| 123 | { {N,4}, {N,5}, {N,60}, {N,N} } |
| 124 | }, |
| 125 | { |
| 126 | 0, {}, { L(4) }, |
| 127 | 2, {}, { L(60) }, |
| 128 | |
| 129 | { {N,4}, {N,N}, {N,60}, {N,N} } |
| 130 | }, |
| 131 | |
| 132 | { |
| 133 | 0, { L( 1), L(2)}, { L( 4) }, |
| 134 | 0, { L(10) }, { L(40), L(50) }, |
| 135 | |
| 136 | { {10,40}, {2,50}, {N,N} } |
| 137 | }, |
| 138 | { |
| 139 | 0, { L(1), L( 2), L(3) }, { L(4), L( 5) }, |
| 140 | 1, { L(20) }, { L(50), L(60) }, |
| 141 | |
| 142 | { {1,4}, {20,50}, {3,60}, {N,N} } |
| 143 | }, |
| 144 | }; |
| 145 | |
| 146 | auto test = [&](const PosTestDesc& tst) { |
| 147 | auto a = SkSVGText::Make(); |
| 148 | auto b = SkSVGTSpan::Make(); |
| 149 | a->appendChild(b); |
| 150 | |
| 151 | a->setX(tst.xa); |
| 152 | a->setY(tst.ya); |
| 153 | b->setX(tst.xb); |
| 154 | b->setY(tst.yb); |
| 155 | |
| 156 | SkSVGTextContext tctx(SkSVGPresentationContext(), nullptr); |
| 157 | SkSVGLengthContext lctx({0,0}); |
| 158 | SkSVGTextContext::ScopedPosResolver pa(*a, lctx, &tctx, tst.offseta); |
| 159 | SkSVGTextContext::ScopedPosResolver pb(*b, lctx, &tctx, tst.offsetb); |
| 160 | |
| 161 | for (size_t i = 0; i < tst.expected.size(); ++i) { |
| 162 | const auto& exp = tst.expected[i]; |
| 163 | auto pos = i >= tst.offsetb ? pb.resolve(i) : pa.resolve(i); |
| 164 | |
| 165 | REPORTER_ASSERT(r, pos[SkSVGTextContext::PosAttrs::kX] == exp.fX); |
| 166 | REPORTER_ASSERT(r, pos[SkSVGTextContext::PosAttrs::kY] == exp.fY); |
| 167 | } |
| 168 | }; |
| 169 | |
| 170 | for (const auto& tst : gTests) { |
| 171 | test(tst); |
| 172 | } |
| 173 | } |