Skip to content
This repository was archived by the owner on Aug 8, 2023. It is now read-only.

Commit 1b97cc9

Browse files
committed
[core] Add more shaping unit test cases
1 parent ddbd392 commit 1b97cc9

1 file changed

Lines changed: 183 additions & 29 deletions

File tree

test/text/shaping.test.cpp

Lines changed: 183 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,12 @@ TEST(Shaping, ZWSP) {
5050
TaggedString string(u"中中\u200b中中\u200b中中\u200b中中中中中中\u200b中中", sectionOptions);
5151
auto shaping = testGetShaping(string, 5);
5252
ASSERT_EQ(shaping.lineCount, 3);
53-
ASSERT_EQ(shaping.top, -36);
54-
ASSERT_EQ(shaping.bottom, 36);
55-
ASSERT_EQ(shaping.left, -63);
56-
ASSERT_EQ(shaping.right, 63);
53+
ASSERT_FLOAT_EQ(-36.0f, shaping.top);
54+
ASSERT_FLOAT_EQ(36.0f, shaping.bottom);
55+
ASSERT_FLOAT_EQ(-63.0f, shaping.left);
56+
ASSERT_FLOAT_EQ(63.0f, shaping.right);
5757
ASSERT_EQ(shaping.writingMode, WritingModeType::Horizontal);
58+
ASSERT_FALSE(shaping.hasBaseline);
5859
}
5960

6061
// 2 lines
@@ -64,11 +65,12 @@ TEST(Shaping, ZWSP) {
6465
TaggedString string(u"中中\u200b", sectionOptions);
6566
auto shaping = testGetShaping(string, 1);
6667
ASSERT_EQ(shaping.lineCount, 2);
67-
ASSERT_EQ(shaping.top, -24);
68-
ASSERT_EQ(shaping.bottom, 24);
69-
ASSERT_EQ(shaping.left, -21);
70-
ASSERT_EQ(shaping.right, 21);
68+
ASSERT_FLOAT_EQ(-24.0f, shaping.top);
69+
ASSERT_FLOAT_EQ(24.0f, shaping.bottom);
70+
ASSERT_FLOAT_EQ(-21.0f, shaping.left);
71+
ASSERT_FLOAT_EQ(21.0f, shaping.right);
7172
ASSERT_EQ(shaping.writingMode, WritingModeType::Horizontal);
73+
ASSERT_FALSE(shaping.hasBaseline);
7274
}
7375

7476
// 1 line
@@ -77,32 +79,34 @@ TEST(Shaping, ZWSP) {
7779
TaggedString string(u"中中\u200b", sectionOptions);
7880
auto shaping = testGetShaping(string, 2);
7981
ASSERT_EQ(shaping.lineCount, 1);
80-
ASSERT_EQ(shaping.top, -12);
81-
ASSERT_EQ(shaping.bottom, 12);
82-
ASSERT_EQ(shaping.left, -21);
83-
ASSERT_EQ(shaping.right, 21);
82+
ASSERT_FLOAT_EQ(-12.0f, shaping.top);
83+
ASSERT_FLOAT_EQ(12.0f, shaping.bottom);
84+
ASSERT_FLOAT_EQ(-21.0f, shaping.left);
85+
ASSERT_FLOAT_EQ(21.0f, shaping.right);
8486
ASSERT_EQ(shaping.writingMode, WritingModeType::Horizontal);
8587
ASSERT_EQ(2, shaping.positionedGlyphs.size());
86-
EXPECT_FLOAT_EQ(-21, shaping.positionedGlyphs[0].x);
87-
EXPECT_FLOAT_EQ(-17, shaping.positionedGlyphs[0].y);
88-
EXPECT_FLOAT_EQ(0, shaping.positionedGlyphs[1].x);
89-
EXPECT_FLOAT_EQ(-17, shaping.positionedGlyphs[1].y);
88+
ASSERT_FLOAT_EQ(-21.0f, shaping.positionedGlyphs[0].x);
89+
ASSERT_FLOAT_EQ(-17.0f, shaping.positionedGlyphs[0].y);
90+
ASSERT_FLOAT_EQ(0.0f, shaping.positionedGlyphs[1].x);
91+
ASSERT_FLOAT_EQ(-17.0f, shaping.positionedGlyphs[1].y);
92+
ASSERT_FALSE(shaping.hasBaseline);
9093
}
9194

9295
// 5 'new' lines.
9396
{
9497
TaggedString string(u"\u200b\u200b\u200b\u200b\u200b", sectionOptions);
9598
auto shaping = testGetShaping(string, 1);
9699
ASSERT_EQ(shaping.lineCount, 5);
97-
ASSERT_EQ(shaping.top, -60);
98-
ASSERT_EQ(shaping.bottom, 60);
99-
ASSERT_EQ(shaping.left, 0);
100-
ASSERT_EQ(shaping.right, 0);
100+
ASSERT_FLOAT_EQ(-60.0f, shaping.top);
101+
ASSERT_FLOAT_EQ(60.0f, shaping.bottom);
102+
ASSERT_FLOAT_EQ(-0.0f, shaping.left);
103+
ASSERT_FLOAT_EQ(0.0f, shaping.right);
101104
ASSERT_EQ(shaping.writingMode, WritingModeType::Horizontal);
105+
ASSERT_FALSE(shaping.hasBaseline);
102106
}
103107
}
104108

105-
TEST(Shaping, FontWithBaseline) {
109+
TEST(Shaping, MixedFontsBothWithBaselines) {
106110
Glyph glyph1;
107111
glyph1.id = u'';
108112
glyph1.metrics.width = 18;
@@ -163,15 +167,165 @@ TEST(Shaping, FontWithBaseline) {
163167

164168
auto shaping = testGetShaping(string, 5);
165169
ASSERT_EQ(shaping.lineCount, 1);
166-
ASSERT_EQ(shaping.top, -12);
167-
ASSERT_EQ(shaping.bottom, 12);
168-
ASSERT_EQ(shaping.left, -21);
169-
ASSERT_EQ(shaping.right, 21);
170+
ASSERT_FLOAT_EQ(-12.0f, shaping.top);
171+
ASSERT_FLOAT_EQ(12.0f, shaping.bottom);
172+
ASSERT_FLOAT_EQ(-21.0f, shaping.left);
173+
ASSERT_FLOAT_EQ(21.0f, shaping.right);
170174
ASSERT_EQ(shaping.writingMode, WritingModeType::Horizontal);
171175
ASSERT_EQ(2, shaping.positionedGlyphs.size());
172-
EXPECT_FLOAT_EQ(-21, shaping.positionedGlyphs[0].x);
173-
EXPECT_FLOAT_EQ(-16, shaping.positionedGlyphs[0].y);
174-
EXPECT_FLOAT_EQ(0, shaping.positionedGlyphs[1].x);
175-
EXPECT_FLOAT_EQ(-15, shaping.positionedGlyphs[1].y);
176+
ASSERT_FLOAT_EQ(-21.0f, shaping.positionedGlyphs[0].x);
177+
ASSERT_FLOAT_EQ(-16.0f, shaping.positionedGlyphs[0].y);
178+
ASSERT_FLOAT_EQ(0.0f, shaping.positionedGlyphs[1].x);
179+
ASSERT_FLOAT_EQ(-15.0f, shaping.positionedGlyphs[1].y);
180+
ASSERT_TRUE(shaping.hasBaseline);
181+
}
182+
}
183+
184+
TEST(Shaping, MixedFontsOneWithBaselineOneWithout) {
185+
Glyph glyph1;
186+
glyph1.id = u'';
187+
glyph1.metrics.width = 18;
188+
glyph1.metrics.height = 19;
189+
glyph1.metrics.left = 2;
190+
glyph1.metrics.top = -8;
191+
glyph1.metrics.advance = 21;
192+
193+
Glyph glyph2;
194+
glyph2.id = u'';
195+
glyph2.metrics.width = 18;
196+
glyph2.metrics.height = 18;
197+
glyph2.metrics.left = 2;
198+
glyph2.metrics.top = -8;
199+
glyph2.metrics.advance = 21;
200+
201+
BiDi bidi;
202+
std::vector<SectionOptions> sectionOptions;
203+
const std::vector<std::string> fontStack1{{"font-stack1"}};
204+
sectionOptions.emplace_back(1.0f, fontStack1);
205+
Glyphs glyphData1;
206+
glyphData1.glyphs.emplace(u'', Immutable<Glyph>(makeMutable<Glyph>(std::move(glyph1))));
207+
glyphData1.ascender = 26;
208+
glyphData1.descender = -6;
209+
210+
const std::vector<std::string> fontStack2{{"font-stack2"}};
211+
sectionOptions.emplace_back(1.0f, fontStack2);
212+
Glyphs glyphData2;
213+
glyphData2.glyphs.emplace(u'', Immutable<Glyph>(makeMutable<Glyph>(std::move(glyph2))));
214+
215+
GlyphMap glyphs;
216+
glyphs.emplace(FontStackHasher()(fontStack1), std::move(glyphData1));
217+
glyphs.emplace(FontStackHasher()(fontStack2), std::move(glyphData2));
218+
219+
const auto testGetShaping = [&](const TaggedString& string, unsigned maxWidthInChars) {
220+
return getShaping(string,
221+
maxWidthInChars * ONE_EM,
222+
ONE_EM, // lineHeight
223+
style::SymbolAnchorType::Center,
224+
style::TextJustifyType::Center,
225+
0, // spacing
226+
{{0.0f, 0.0f}}, // translate
227+
WritingModeType::Horizontal,
228+
bidi,
229+
glyphs,
230+
/*allowVerticalPlacement*/ false);
231+
};
232+
233+
{
234+
std::u16string text{u"阳光\u200b"};
235+
StyledText styledText;
236+
styledText.second = std::vector<uint8_t>{0, 1, 0};
237+
styledText.first = std::move(text);
238+
239+
TaggedString string{styledText, sectionOptions};
240+
241+
auto shaping = testGetShaping(string, 5);
242+
ASSERT_EQ(shaping.lineCount, 1);
243+
ASSERT_FLOAT_EQ(-12.0f, shaping.top);
244+
ASSERT_FLOAT_EQ(12.0f, shaping.bottom);
245+
ASSERT_FLOAT_EQ(-21.0f, shaping.left);
246+
ASSERT_FLOAT_EQ(21.0f, shaping.right);
247+
ASSERT_EQ(shaping.writingMode, WritingModeType::Horizontal);
248+
ASSERT_EQ(2, shaping.positionedGlyphs.size());
249+
ASSERT_FLOAT_EQ(-21.0f, shaping.positionedGlyphs[0].x);
250+
ASSERT_FLOAT_EQ(-17.0f, shaping.positionedGlyphs[0].y);
251+
ASSERT_FLOAT_EQ(0.0f, shaping.positionedGlyphs[1].x);
252+
ASSERT_FLOAT_EQ(-17.0f, shaping.positionedGlyphs[1].y);
253+
ASSERT_FALSE(shaping.hasBaseline);
254+
}
255+
}
256+
257+
TEST(Shaping, MixedFontsWithBaselineWithFontScale) {
258+
Glyph glyph1;
259+
glyph1.id = u'';
260+
glyph1.metrics.width = 18;
261+
glyph1.metrics.height = 19;
262+
glyph1.metrics.left = 2;
263+
glyph1.metrics.top = -8;
264+
glyph1.metrics.advance = 21;
265+
266+
Glyph glyph2;
267+
glyph2.id = u'';
268+
glyph2.metrics.width = 18;
269+
glyph2.metrics.height = 18;
270+
glyph2.metrics.left = 2;
271+
glyph2.metrics.top = -8;
272+
glyph2.metrics.advance = 21;
273+
274+
BiDi bidi;
275+
std::vector<SectionOptions> sectionOptions;
276+
const std::vector<std::string> fontStack1{{"font-stack1"}};
277+
sectionOptions.emplace_back(1.0f, fontStack1);
278+
sectionOptions.back().scale = 1.5;
279+
Glyphs glyphData1;
280+
glyphData1.glyphs.emplace(u'', Immutable<Glyph>(makeMutable<Glyph>(std::move(glyph1))));
281+
glyphData1.ascender = 26;
282+
glyphData1.descender = -6;
283+
284+
const std::vector<std::string> fontStack2{{"font-stack2"}};
285+
sectionOptions.emplace_back(1.0f, fontStack2);
286+
Glyphs glyphData2;
287+
glyphData2.glyphs.emplace(u'', Immutable<Glyph>(makeMutable<Glyph>(std::move(glyph2))));
288+
glyphData2.ascender = 25;
289+
glyphData2.descender = -5;
290+
291+
GlyphMap glyphs;
292+
glyphs.emplace(FontStackHasher()(fontStack1), std::move(glyphData1));
293+
glyphs.emplace(FontStackHasher()(fontStack2), std::move(glyphData2));
294+
295+
const auto testGetShaping = [&](const TaggedString& string, unsigned maxWidthInChars) {
296+
return getShaping(string,
297+
maxWidthInChars * ONE_EM,
298+
ONE_EM, // lineHeight
299+
style::SymbolAnchorType::Center,
300+
style::TextJustifyType::Center,
301+
0, // spacing
302+
{{0.0f, 0.0f}}, // translate
303+
WritingModeType::Horizontal,
304+
bidi,
305+
glyphs,
306+
/*allowVerticalPlacement*/ false);
307+
};
308+
309+
{
310+
std::u16string text{u"阳光\u200b"};
311+
StyledText styledText;
312+
styledText.second = std::vector<uint8_t>{0, 1, 0};
313+
styledText.first = std::move(text);
314+
315+
TaggedString string{styledText, sectionOptions};
316+
317+
auto shaping = testGetShaping(string, 5);
318+
ASSERT_EQ(shaping.lineCount, 1);
319+
ASSERT_FLOAT_EQ(-18.0f, shaping.top);
320+
ASSERT_FLOAT_EQ(18.0f, shaping.bottom);
321+
ASSERT_FLOAT_EQ(-26.25f, shaping.left);
322+
ASSERT_FLOAT_EQ(26.25f, shaping.right);
323+
ASSERT_EQ(shaping.writingMode, WritingModeType::Horizontal);
324+
ASSERT_EQ(2, shaping.positionedGlyphs.size());
325+
ASSERT_FLOAT_EQ(-26.25f, shaping.positionedGlyphs[0].x);
326+
ASSERT_FLOAT_EQ(-24.0f, shaping.positionedGlyphs[0].y);
327+
ASSERT_FLOAT_EQ(5.25f, shaping.positionedGlyphs[1].x);
328+
ASSERT_FLOAT_EQ(-10.0f, shaping.positionedGlyphs[1].y);
329+
ASSERT_TRUE(shaping.hasBaseline);
176330
}
177331
}

0 commit comments

Comments
 (0)