• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "include/utils/SkNoDrawCanvas.h"
11 #include "modules/skshaper/utils/FactoryHelpers.h"
12 #include "modules/svg/src/SkSVGTextPriv.h"
13 #include "tests/Test.h"
14 #include "tools/fonts/FontToolUtils.h"
15 
DEF_TEST(Svg_Text_PosProvider,r)16 DEF_TEST(Svg_Text_PosProvider, r) {
17     const auto L = [](float x) { return SkSVGLength(x); };
18     const float N = SkSVGTextContext::PosAttrs()[SkSVGTextContext::PosAttrs::kX];
19 
20     static const struct PosTestDesc {
21         size_t                   offseta;
22         std::vector<SkSVGLength> xa, ya;
23 
24         size_t                   offsetb;
25         std::vector<SkSVGLength> xb, yb;
26 
27         std::vector<SkPoint>     expected;
28     } gTests[] = {
29         {
30             0, {}, {},
31             0, {}, {},
32 
33             { {N,N} }
34         },
35 
36         {
37             0, { L(1) }, {},
38             0, {      }, {},
39 
40             { {1,N}, {N,N} }
41         },
42         {
43             0, {       }, {},
44             0, { L(10) }, {},
45 
46             { {10,N}, {N,N} }
47         },
48         {
49             0, { L( 1) }, {},
50             0, { L(10) }, {},
51 
52             { {10,N}, {N,N} }
53         },
54         {
55             0, { L( 1), L(2) }, {},
56             0, { L(10)       }, {},
57 
58             { {10,N}, {2,N}, {N,N} }
59         },
60         {
61             0, { L(1), L( 2) }, {},
62             1, {       L(20) }, {},
63 
64             { {1,N}, {20,N}, {N,N} }
65         },
66         {
67             0, { L(1), L( 2), L(3) }, {},
68             1, {       L(20)       }, {},
69 
70             { {1,N}, {20,N}, {3,N}, {N,N} }
71         },
72         {
73             0, { L(1), L(2), L( 3) }, {},
74             2, {             L(30) }, {},
75 
76             { {1,N}, {2,N}, {30,N}, {N,N} }
77         },
78         {
79             0, { L(1)              }, {},
80             2, {             L(30) }, {},
81 
82             { {1,N}, {N,N}, {30,N}, {N,N} }
83         },
84 
85 
86         {
87             0, {}, { L(4) },
88             0, {}, {      },
89 
90             { {N,4}, {N,N} }
91         },
92         {
93             0, {}, {       },
94             0, {}, { L(40) },
95 
96             { {N,40}, {N,N} }
97         },
98         {
99             0, {}, { L( 4) },
100             0, {}, { L(40) },
101 
102             { {N,40}, {N,N} }
103         },
104         {
105             0, {}, { L( 4), L(5) },
106             0, {}, { L(40)       },
107 
108             { {N,40}, {N,5}, {N,N} }
109         },
110         {
111             0, {}, { L(4), L( 5) },
112             1, {}, {       L(50) },
113 
114             { {N,4}, {N,50}, {N,N} }
115         },
116         {
117             0, {}, { L(4), L( 5), L(6) },
118             1, {}, {       L(50)       },
119 
120             { {N,4}, {N,50}, {N,6}, {N,N} }
121         },
122         {
123             0, {}, { L(4), L(5), L( 6) },
124             2, {}, {             L(60) },
125 
126             { {N,4}, {N,5}, {N,60}, {N,N} }
127         },
128         {
129             0, {}, { L(4)              },
130             2, {}, {             L(60) },
131 
132             { {N,4}, {N,N}, {N,60}, {N,N} }
133         },
134 
135         {
136             0, { L( 1), L(2)}, { L( 4)        },
137             0, { L(10)      }, { L(40), L(50) },
138 
139             { {10,40}, {2,50}, {N,N} }
140         },
141         {
142             0, { L(1), L( 2), L(3) }, { L(4), L( 5)        },
143             1, {       L(20)       }, {       L(50), L(60) },
144 
145             { {1,4}, {20,50}, {3,60}, {N,N} }
146         },
147     };
148 
149     const SkSVGTextContext::ShapedTextCallback mock_cb =
150         [](const SkSVGRenderContext&, const sk_sp<SkTextBlob>&, const SkPaint*, const SkPaint*) {};
151 
152     auto test = [&](const PosTestDesc& tst) {
153         auto a = SkSVGText::Make();
154         auto b = SkSVGTSpan::Make();
155         a->appendChild(b);
156 
157         a->setX(tst.xa);
158         a->setY(tst.ya);
159         b->setX(tst.xb);
160         b->setY(tst.yb);
161 
162         const SkSVGIDMapper mapper;
163         const SkSVGLengthContext lctx({0,0});
164         const SkSVGPresentationContext pctx;
165         SkNoDrawCanvas canvas(0, 0);
166         sk_sp<SkFontMgr> fmgr = ToolUtils::TestFontMgr();
167         sk_sp<skresources::ResourceProvider> rp;
168         sk_sp<SkShapers::Factory> shaping = SkShapers::BestAvailable();
169         const SkSVGRenderContext ctx(&canvas,
170                                      fmgr,
171                                      rp,
172                                      mapper,
173                                      lctx,
174                                      pctx,
175                                      {nullptr, nullptr},
176                                      shaping);
177 
178         SkSVGTextContext tctx(ctx, mock_cb);
179         SkSVGTextContext::ScopedPosResolver pa(*a, lctx, &tctx, tst.offseta);
180         SkSVGTextContext::ScopedPosResolver pb(*b, lctx, &tctx, tst.offsetb);
181 
182         for (size_t i = 0; i < tst.expected.size(); ++i) {
183             const auto& exp = tst.expected[i];
184             auto pos = i >= tst.offsetb ? pb.resolve(i) : pa.resolve(i);
185 
186             REPORTER_ASSERT(r, pos[SkSVGTextContext::PosAttrs::kX] == exp.fX);
187             REPORTER_ASSERT(r, pos[SkSVGTextContext::PosAttrs::kY] == exp.fY);
188         }
189     };
190 
191     for (const auto& tst : gTests) {
192         test(tst);
193     }
194 }
195