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(
Rational(6 * index, 10), {0, 0},
Matrix(),
235 *host_buffer, atlas_context, frames);
236 ASSERT_NE(atlas,
nullptr);
237 ASSERT_NE(atlas->GetTexture(),
nullptr);
239 std::set<uint16_t> unique_glyphs;
240 std::vector<uint16_t> total_glyphs;
241 atlas->IterateGlyphs([&](
const ScaledFont& scaled_font,
249 EXPECT_LE(unique_glyphs.size() * size_count, atlas->GetGlyphCount());
250 EXPECT_EQ(total_glyphs.size(), atlas->GetGlyphCount());
252 EXPECT_TRUE(atlas->GetGlyphCount() > 0);
253 EXPECT_TRUE(atlas->GetTexture()->GetSize().width > 0);
254 EXPECT_TRUE(atlas->GetTexture()->GetSize().height > 0);
259 GetContext()->GetIdleWaiter());
263 ASSERT_TRUE(context && context->IsValid());
264 SkFont sk_font = flutter::testing::CreateTestFontOfSize(12);
265 auto blob = SkTextBlob::MakeFromString(
"spooky 1", sk_font);
271 auto old_packer = atlas_context->GetRectPacker();
273 ASSERT_NE(atlas,
nullptr);
274 ASSERT_NE(atlas->GetTexture(),
nullptr);
275 ASSERT_EQ(atlas, atlas_context->GetGlyphAtlas());
277 auto* first_texture = atlas->GetTexture().get();
281 auto blob2 = SkTextBlob::MakeFromString(
"spooky 2", sk_font);
286 ASSERT_EQ(atlas, next_atlas);
287 auto* second_texture = next_atlas->GetTexture().get();
289 auto new_packer = atlas_context->GetRectPacker();
291 ASSERT_EQ(second_texture, first_texture);
292 ASSERT_EQ(old_packer, new_packer);
297 GetContext()->GetIdleWaiter());
299 auto mapping = flutter::testing::OpenFixtureAsSkData(
"Apple Color Emoji.ttc");
301 auto mapping = flutter::testing::OpenFixtureAsSkData(
"NotoColorEmoji.ttf");
303 ASSERT_TRUE(mapping);
304 sk_sp<SkFontMgr> font_mgr = txt::GetDefaultFontManager();
305 SkFont emoji_font(font_mgr->makeFromData(mapping), 50.0);
314 SkTextBlob::MakeFromString(
"😂", emoji_font));
316 SkTextBlob::MakeFromString(
"😂", emoji_font));
317 std::vector<std::optional<GlyphProperties>> properties = {
325 atlas_context, {frame, frame_2}, properties);
327 EXPECT_EQ(next_atlas->GetGlyphCount(), 2u);
332 GetContext()->GetIdleWaiter());
333 sk_sp<SkFontMgr> font_mgr = txt::GetDefaultFontManager();
334 sk_sp<SkTypeface> typeface =
335 font_mgr->matchFamilyStyle(
"Arial", SkFontStyle::Normal());
336 SkFont sk_font(typeface, 0.5f);
348 std::vector<std::optional<GlyphProperties>> properties = {
356 atlas_context, {frame, frame_2}, properties);
358 EXPECT_EQ(next_atlas->GetGlyphCount(), 1u);
363 ASSERT_NE(packer,
nullptr);
364 ASSERT_EQ(packer->PercentFull(), 0);
366 const SkIRect packer_area = SkIRect::MakeXYWH(0, 0, 200, 100);
369 ASSERT_TRUE(packer->AddRect(20, 20, &first_output));
372 const SkIRect first_rect =
373 SkIRect::MakeXYWH(first_output.
x(), first_output.
y(), 20, 20);
374 ASSERT_TRUE(SkIRect::Intersects(packer_area, first_rect));
381 ASSERT_TRUE(packer->AddRect(140, 90, &second_output));
382 const SkIRect second_rect =
383 SkIRect::MakeXYWH(second_output.
x(), second_output.
y(), 140, 90);
386 ASSERT_TRUE(SkIRect::Intersects(packer_area, second_rect));
387 ASSERT_FALSE(SkIRect::Intersects(first_rect, second_rect));
396 ASSERT_FALSE(packer->AddRect(50, 50, &output));
402 ASSERT_EQ(packer->PercentFull(), 0);
410 for (
auto i = 0u; i < 16; i++) {
411 skyline->AddRect(16, 16, &loc);
414 EXPECT_EQ(loc.
x(), 256 - 16);
415 EXPECT_EQ(loc.
y(), 0);
418 for (
auto i = 0u; i < 16; i++) {
419 skyline->AddRect(16, 16, &loc);
422 EXPECT_EQ(loc.
x(), 256 - 16);
423 EXPECT_EQ(loc.
y(), 16);
428 GTEST_SKIP() <<
"Atlas growth isn't supported for OpenGLES currently.";
432 GetContext()->GetIdleWaiter());
436 ASSERT_TRUE(context && context->IsValid());
437 SkFont sk_font = flutter::testing::CreateTestFontOfSize(12);
438 auto blob = SkTextBlob::MakeFromString(
"A", sk_font);
448 constexpr
ISize expected_sizes[13] = {
464 SkFont sk_font_small = flutter::testing::CreateTestFontOfSize(10);
466 for (
int i = 0; i < 13; i++) {
467 SkTextBlobBuilder builder;
469 auto add_char = [&](
const SkFont& sk_font,
char c) {
470 int count = sk_font.countText(&c, 1, SkTextEncoding::kUTF8);
471 auto buffer = builder.allocRunPos(sk_font, count);
472 sk_font.textToGlyphs(&c, 1, SkTextEncoding::kUTF8, buffer.glyphs, count);
473 sk_font.getPos(buffer.glyphs, count, buffer.points(), {0, 0});
476 SkFont sk_font = flutter::testing::CreateTestFontOfSize(50 + i);
477 add_char(sk_font,
'A');
478 add_char(sk_font_small,
'B');
479 auto blob = builder.make();
485 ASSERT_TRUE(!!atlas);
486 EXPECT_EQ(atlas->GetTexture()->GetTextureDescriptor().size,
493 ASSERT_EQ(atlas->GetGlyphCount(), 2u);
497 SkFont font = flutter::testing::CreateTestFontOfSize(12);
498 auto blob = SkTextBlob::MakeFromString(
499 "the quick brown fox jumped over the lazy dog.", font);
503 EXPECT_FALSE(frame->IsFrameComplete());
509 GetContext()->GetIdleWaiter());
517 EXPECT_TRUE(frame->IsFrameComplete());
518 EXPECT_TRUE(frame->GetFrameBounds(0).is_placeholder);
525 EXPECT_TRUE(frame->IsFrameComplete());
526 EXPECT_FALSE(frame->GetFrameBounds(0).is_placeholder);
530 SkFont font = flutter::testing::CreateTestFontOfSize(12);
531 auto blob = SkTextBlob::MakeFromString(
532 "the quick brown fox jumped over the lazy dog.", font);
536 EXPECT_FALSE(frame->IsFrameComplete());
542 GetContext()->GetIdleWaiter());
550 EXPECT_TRUE(frame->IsFrameComplete());
551 EXPECT_TRUE(frame->GetFrameBounds(0).is_placeholder);
560 EXPECT_TRUE(frame->IsFrameComplete());
561 EXPECT_TRUE(frame->GetFrameBounds(0).is_placeholder);
565 SkFont font = flutter::testing::CreateTestFontOfSize(12);
566 auto blob = SkTextBlob::MakeFromString(
567 "the quick brown fox jumped over the lazy dog.", font);
571 EXPECT_FALSE(frame->IsFrameComplete());
577 GetContext()->GetIdleWaiter());
585 EXPECT_TRUE(frame->IsFrameComplete());
586 EXPECT_TRUE(frame->GetFrameBounds(0).is_placeholder);
589 EXPECT_EQ(frame->GetAtlasGenerationAndID().first, 1u);
591 EXPECT_EQ(frame->GetAtlasGenerationAndID().first, 0u);
599 EXPECT_TRUE(frame->IsFrameComplete());
600 EXPECT_FALSE(frame->GetFrameBounds(0).is_placeholder);
602 EXPECT_EQ(frame->GetAtlasGenerationAndID().first, 1u);
604 EXPECT_EQ(frame->GetAtlasGenerationAndID().first, 0u);
608 atlas_context->GetGlyphAtlas()->SetAtlasGeneration(2u);
613 EXPECT_EQ(frame->GetAtlasGenerationAndID().first, 2u);
617 SkFont font = flutter::testing::CreateTestFontOfSize(12);
618 auto blob = SkTextBlob::MakeFromString(
619 "the quick brown fox jumped over the lazy dog.", font);
623 EXPECT_FALSE(frame->IsFrameComplete());
629 GetContext()->GetIdleWaiter());
637 EXPECT_TRUE(frame->IsFrameComplete());
638 EXPECT_TRUE(frame->GetFrameBounds(0).is_placeholder);
641 EXPECT_EQ(frame->GetAtlasGenerationAndID().first, 1u);
643 EXPECT_EQ(frame->GetAtlasGenerationAndID().first, 0u);
647 auto second_atlas_context =
650 EXPECT_FALSE(second_atlas_context->GetGlyphAtlas()->IsValid());
654 Rational(1), second_atlas_context, frame);
656 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, Rational 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)
INSTANTIATE_PLAYGROUND_SUITE(AiksTest)
static std::shared_ptr< GlyphAtlas > CreateGlyphAtlas(Context &context, const TypographerContext *typographer_context, HostBuffer &host_buffer, GlyphAtlas::Type type, Rational scale, const std::shared_ptr< GlyphAtlasContext > &atlas_context, const std::shared_ptr< TextFrame > &frame)
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.