5 #include "flutter/display_list/testing/dl_test_snippets.h"
6 #include "flutter/testing/testing.h"
7 #include "gtest/gtest.h"
16 #include "third_party/skia/include/core/SkFont.h"
17 #include "third_party/skia/include/core/SkFontMgr.h"
18 #include "third_party/skia/include/core/SkRect.h"
19 #include "third_party/skia/include/core/SkTextBlob.h"
20 #include "third_party/skia/include/core/SkTypeface.h"
21 #include "txt/platform.h"
38 const std::shared_ptr<GlyphAtlasContext>& atlas_context,
39 const std::shared_ptr<TextFrame>& frame) {
40 frame->SetPerFrameData(
scale, {0, 0},
Matrix(), std::nullopt);
42 atlas_context, {frame});
51 const std::shared_ptr<GlyphAtlasContext>& atlas_context,
52 const std::vector<std::shared_ptr<TextFrame>>& frames,
53 const std::vector<std::optional<GlyphProperties>>& properties) {
55 for (
auto& frame : frames) {
59 atlas_context, frames);
63 SkFont font = flutter::testing::CreateTestFontOfSize(12);
64 auto blob = SkTextBlob::MakeFromString(
65 "the quick brown fox jumped over the lazy dog.", font);
68 ASSERT_EQ(frame->GetRunCount(), 1u);
69 for (
const auto& run : frame->GetRuns()) {
70 ASSERT_TRUE(run.IsValid());
71 ASSERT_EQ(run.GetGlyphCount(), 45u);
77 ASSERT_TRUE(context && context->IsValid());
85 GetContext()->GetIdleWaiter());
86 ASSERT_TRUE(context && context->IsValid());
87 SkFont sk_font = flutter::testing::CreateTestFontOfSize(12);
88 auto blob = SkTextBlob::MakeFromString(
"hello", sk_font);
95 ASSERT_NE(atlas,
nullptr);
96 ASSERT_NE(atlas->GetTexture(),
nullptr);
98 ASSERT_EQ(atlas->GetGlyphCount(), 4llu);
100 std::optional<impeller::ScaledFont> first_scaled_font;
101 std::optional<impeller::SubpixelGlyph> first_glyph;
103 atlas->IterateGlyphs([&](
const ScaledFont& scaled_font,
105 const Rect& rect) ->
bool {
106 first_scaled_font = scaled_font;
112 ASSERT_TRUE(first_scaled_font.has_value());
114 ->FindFontGlyphBounds(
115 {first_scaled_font.value(), first_glyph.value()})
121 GetContext()->GetIdleWaiter());
123 auto mapping = flutter::testing::OpenFixtureAsSkData(
"Apple Color Emoji.ttc");
125 auto mapping = flutter::testing::OpenFixtureAsSkData(
"NotoColorEmoji.ttf");
127 ASSERT_TRUE(mapping);
128 sk_sp<SkFontMgr> font_mgr = txt::GetDefaultFontManager();
129 SkFont emoji_font(font_mgr->makeFromData(mapping), 50.0);
130 SkFont sk_font = flutter::testing::CreateTestFontOfSize(12);
132 auto blob = SkTextBlob::MakeFromString(
"hello", sk_font);
143 SkTextBlob::MakeFromString(
"😀 ", emoji_font));
156 ASSERT_FALSE(color_atlas == bitmap_atlas);
164 GetContext()->GetIdleWaiter());
165 ASSERT_TRUE(context && context->IsValid());
166 SkFont sk_font = flutter::testing::CreateTestFontOfSize(12);
167 auto blob = SkTextBlob::MakeFromString(
"AGH", sk_font);
173 ASSERT_NE(atlas,
nullptr);
174 ASSERT_NE(atlas->GetTexture(),
nullptr);
176 EXPECT_EQ(atlas->GetTexture()->GetSize().width, 4096u);
177 EXPECT_EQ(atlas->GetTexture()->GetSize().height, 1024u);
185 GetContext()->GetIdleWaiter());
186 ASSERT_TRUE(context && context->IsValid());
187 SkFont sk_font = flutter::testing::CreateTestFontOfSize(12);
188 auto blob = SkTextBlob::MakeFromString(
"spooky skellingtons", sk_font);
194 ASSERT_NE(atlas,
nullptr);
195 ASSERT_NE(atlas->GetTexture(),
nullptr);
196 ASSERT_EQ(atlas, atlas_context->GetGlyphAtlas());
204 ASSERT_EQ(atlas, next_atlas);
205 ASSERT_EQ(atlas_context->GetGlyphAtlas(), atlas);
210 GetContext()->GetIdleWaiter());
214 ASSERT_TRUE(context && context->IsValid());
216 const char* test_string =
217 "QWERTYUIOPASDFGHJKLZXCVBNMqewrtyuiopasdfghjklzxcvbnm,.<>[]{};':"
218 "2134567890-=!@#$%^&*()_+"
219 "œ∑´®†¥¨ˆøπ““‘‘åß∂ƒ©˙∆˚¬…æ≈ç√∫˜µ≤≥≥≥≥÷¡™£¢∞§¶•ªº–≠⁄€‹›fifl‡°·‚—±Œ„´‰Á¨Ø∏”’/"
222 SkFont sk_font = flutter::testing::CreateTestFontOfSize(12);
223 auto blob = SkTextBlob::MakeFromString(test_string, sk_font);
226 size_t size_count = 8;
227 std::vector<std::shared_ptr<TextFrame>> frames;
228 for (
size_t index = 0; index < size_count; index += 1) {
230 frames.back()->SetPerFrameData(0.6 * index, {0, 0},
Matrix(), {});
234 *host_buffer, atlas_context, frames);
235 ASSERT_NE(atlas,
nullptr);
236 ASSERT_NE(atlas->GetTexture(),
nullptr);
238 std::set<uint16_t> unique_glyphs;
239 std::vector<uint16_t> total_glyphs;
240 atlas->IterateGlyphs([&](
const ScaledFont& scaled_font,
248 EXPECT_LE(unique_glyphs.size() * size_count, atlas->GetGlyphCount());
249 EXPECT_EQ(total_glyphs.size(), atlas->GetGlyphCount());
251 EXPECT_TRUE(atlas->GetGlyphCount() > 0);
252 EXPECT_TRUE(atlas->GetTexture()->GetSize().width > 0);
253 EXPECT_TRUE(atlas->GetTexture()->GetSize().height > 0);
258 GetContext()->GetIdleWaiter());
262 ASSERT_TRUE(context && context->IsValid());
263 SkFont sk_font = flutter::testing::CreateTestFontOfSize(12);
264 auto blob = SkTextBlob::MakeFromString(
"spooky 1", sk_font);
270 auto old_packer = atlas_context->GetRectPacker();
272 ASSERT_NE(atlas,
nullptr);
273 ASSERT_NE(atlas->GetTexture(),
nullptr);
274 ASSERT_EQ(atlas, atlas_context->GetGlyphAtlas());
276 auto* first_texture = atlas->GetTexture().get();
280 auto blob2 = SkTextBlob::MakeFromString(
"spooky 2", sk_font);
285 ASSERT_EQ(atlas, next_atlas);
286 auto* second_texture = next_atlas->GetTexture().get();
288 auto new_packer = atlas_context->GetRectPacker();
290 ASSERT_EQ(second_texture, first_texture);
291 ASSERT_EQ(old_packer, new_packer);
296 GetContext()->GetIdleWaiter());
298 auto mapping = flutter::testing::OpenFixtureAsSkData(
"Apple Color Emoji.ttc");
300 auto mapping = flutter::testing::OpenFixtureAsSkData(
"NotoColorEmoji.ttf");
302 ASSERT_TRUE(mapping);
303 sk_sp<SkFontMgr> font_mgr = txt::GetDefaultFontManager();
304 SkFont emoji_font(font_mgr->makeFromData(mapping), 50.0);
313 SkTextBlob::MakeFromString(
"😂", emoji_font));
315 SkTextBlob::MakeFromString(
"😂", emoji_font));
316 std::vector<std::optional<GlyphProperties>> properties = {
324 {frame, frame_2}, properties);
326 EXPECT_EQ(next_atlas->GetGlyphCount(), 2u);
331 GetContext()->GetIdleWaiter());
332 sk_sp<SkFontMgr> font_mgr = txt::GetDefaultFontManager();
333 sk_sp<SkTypeface> typeface =
334 font_mgr->matchFamilyStyle(
"Arial", SkFontStyle::Normal());
335 SkFont sk_font(typeface, 0.5f);
347 std::vector<std::optional<GlyphProperties>> properties = {
355 {frame, frame_2}, properties);
357 EXPECT_EQ(next_atlas->GetGlyphCount(), 1u);
362 ASSERT_NE(packer,
nullptr);
363 ASSERT_EQ(packer->PercentFull(), 0);
365 const SkIRect packer_area = SkIRect::MakeXYWH(0, 0, 200, 100);
368 ASSERT_TRUE(packer->AddRect(20, 20, &first_output));
371 const SkIRect first_rect =
372 SkIRect::MakeXYWH(first_output.
x(), first_output.
y(), 20, 20);
373 ASSERT_TRUE(SkIRect::Intersects(packer_area, first_rect));
380 ASSERT_TRUE(packer->AddRect(140, 90, &second_output));
381 const SkIRect second_rect =
382 SkIRect::MakeXYWH(second_output.
x(), second_output.
y(), 140, 90);
385 ASSERT_TRUE(SkIRect::Intersects(packer_area, second_rect));
386 ASSERT_FALSE(SkIRect::Intersects(first_rect, second_rect));
395 ASSERT_FALSE(packer->AddRect(50, 50, &output));
401 ASSERT_EQ(packer->PercentFull(), 0);
409 for (
auto i = 0u; i < 16; i++) {
410 skyline->AddRect(16, 16, &loc);
413 EXPECT_EQ(loc.
x(), 256 - 16);
414 EXPECT_EQ(loc.
y(), 0);
417 for (
auto i = 0u; i < 16; i++) {
418 skyline->AddRect(16, 16, &loc);
421 EXPECT_EQ(loc.
x(), 256 - 16);
422 EXPECT_EQ(loc.
y(), 16);
427 GTEST_SKIP() <<
"Atlas growth isn't supported for OpenGLES currently.";
431 GetContext()->GetIdleWaiter());
435 ASSERT_TRUE(context && context->IsValid());
436 SkFont sk_font = flutter::testing::CreateTestFontOfSize(12);
437 auto blob = SkTextBlob::MakeFromString(
"A", sk_font);
447 constexpr
ISize expected_sizes[13] = {
463 SkFont sk_font_small = flutter::testing::CreateTestFontOfSize(10);
465 for (
int i = 0; i < 13; i++) {
466 SkTextBlobBuilder builder;
468 auto add_char = [&](
const SkFont& sk_font,
char c) {
469 int count = sk_font.countText(&c, 1, SkTextEncoding::kUTF8);
470 auto buffer = builder.allocRunPos(sk_font, count);
471 sk_font.textToGlyphs(&c, 1, SkTextEncoding::kUTF8, buffer.glyphs, count);
472 sk_font.getPos(buffer.glyphs, count, buffer.points(), {0, 0});
475 SkFont sk_font = flutter::testing::CreateTestFontOfSize(50 + i);
476 add_char(sk_font,
'A');
477 add_char(sk_font_small,
'B');
478 auto blob = builder.make();
484 ASSERT_TRUE(!!atlas);
485 EXPECT_EQ(atlas->GetTexture()->GetTextureDescriptor().size,
492 ASSERT_EQ(atlas->GetGlyphCount(), 2u);
496 SkFont font = flutter::testing::CreateTestFontOfSize(12);
497 auto blob = SkTextBlob::MakeFromString(
498 "the quick brown fox jumped over the lazy dog.", font);
502 EXPECT_FALSE(frame->IsFrameComplete());
508 GetContext()->GetIdleWaiter());
512 atlas_context, frame);
516 EXPECT_TRUE(frame->IsFrameComplete());
517 EXPECT_TRUE(frame->GetFrameBounds(0).is_placeholder);
521 atlas_context, frame);
524 EXPECT_TRUE(frame->IsFrameComplete());
525 EXPECT_FALSE(frame->GetFrameBounds(0).is_placeholder);
529 SkFont font = flutter::testing::CreateTestFontOfSize(12);
530 auto blob = SkTextBlob::MakeFromString(
531 "the quick brown fox jumped over the lazy dog.", font);
535 EXPECT_FALSE(frame->IsFrameComplete());
541 GetContext()->GetIdleWaiter());
545 atlas_context, frame);
549 EXPECT_TRUE(frame->IsFrameComplete());
550 EXPECT_TRUE(frame->GetFrameBounds(0).is_placeholder);
556 atlas_context, frame);
559 EXPECT_TRUE(frame->IsFrameComplete());
560 EXPECT_TRUE(frame->GetFrameBounds(0).is_placeholder);
564 SkFont font = flutter::testing::CreateTestFontOfSize(12);
565 auto blob = SkTextBlob::MakeFromString(
566 "the quick brown fox jumped over the lazy dog.", font);
570 EXPECT_FALSE(frame->IsFrameComplete());
576 GetContext()->GetIdleWaiter());
580 atlas_context, frame);
584 EXPECT_TRUE(frame->IsFrameComplete());
585 EXPECT_TRUE(frame->GetFrameBounds(0).is_placeholder);
588 EXPECT_EQ(frame->GetAtlasGenerationAndID().first, 1u);
590 EXPECT_EQ(frame->GetAtlasGenerationAndID().first, 0u);
595 atlas_context, frame);
598 EXPECT_TRUE(frame->IsFrameComplete());
599 EXPECT_FALSE(frame->GetFrameBounds(0).is_placeholder);
601 EXPECT_EQ(frame->GetAtlasGenerationAndID().first, 1u);
603 EXPECT_EQ(frame->GetAtlasGenerationAndID().first, 0u);
607 atlas_context->GetGlyphAtlas()->SetAtlasGeneration(2u);
610 atlas_context, frame);
612 EXPECT_EQ(frame->GetAtlasGenerationAndID().first, 2u);
616 SkFont font = flutter::testing::CreateTestFontOfSize(12);
617 auto blob = SkTextBlob::MakeFromString(
618 "the quick brown fox jumped over the lazy dog.", font);
622 EXPECT_FALSE(frame->IsFrameComplete());
628 GetContext()->GetIdleWaiter());
632 atlas_context, frame);
636 EXPECT_TRUE(frame->IsFrameComplete());
637 EXPECT_TRUE(frame->GetFrameBounds(0).is_placeholder);
640 EXPECT_EQ(frame->GetAtlasGenerationAndID().first, 1u);
642 EXPECT_EQ(frame->GetAtlasGenerationAndID().first, 0u);
646 auto second_atlas_context =
649 EXPECT_FALSE(second_atlas_context->GetGlyphAtlas()->IsValid());
653 second_atlas_context, frame);
655 EXPECT_TRUE(second_atlas_context->GetGlyphAtlas()->IsValid());
To do anything rendering related with Impeller, you need a context.
Type
Describes how the glyphs are represented in the texture.
static std::shared_ptr< HostBuffer > Create(const std::shared_ptr< Allocator > &allocator, const std::shared_ptr< const IdleWaiter > &idle_waiter)
const std::shared_ptr< GlyphAtlas > & CreateOrGetGlyphAtlas(Context &context, HostBuffer &host_buffer, GlyphAtlas::Type type) const
void AddTextFrame(const std::shared_ptr< TextFrame > &frame, Scalar scale, Point offset, const Matrix &transform, std::optional< GlyphProperties > properties)
static std::shared_ptr< RectanglePacker > Factory(int width, int height)
Return an empty packer with area specified by width and height.
The graphics context necessary to render text.
virtual std::shared_ptr< GlyphAtlas > CreateGlyphAtlas(Context &context, GlyphAtlas::Type type, HostBuffer &host_buffer, const std::shared_ptr< GlyphAtlasContext > &atlas_context, const std::vector< std::shared_ptr< TextFrame >> &text_frames) const =0
static std::shared_ptr< TypographerContext > Make()
bool NumberNear(double a, double b)
TEST(AllocationSizeTest, CanCreateTypedAllocations)
TEST_P(AiksTest, DrawAtlasNoColor)
static std::shared_ptr< GlyphAtlas > CreateGlyphAtlas(Context &context, const TypographerContext *typographer_context, HostBuffer &host_buffer, GlyphAtlas::Type type, Scalar scale, const std::shared_ptr< GlyphAtlasContext > &atlas_context, const std::shared_ptr< TextFrame > &frame)
INSTANTIATE_PLAYGROUND_SUITE(AiksTest)
std::shared_ptr< TextFrame > MakeTextFrameFromTextBlobSkia(const sk_sp< SkTextBlob > &blob)
static constexpr Color Red()
static constexpr Color Blue()
A 4x4 matrix using column-major storage.
A font and a scale. Used as a key that represents a typeface within a glyph atlas.
A glyph and its subpixel position.