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) {
44 return typographer_context->
CreateGlyphAtlas(context, type, data_host_buffer,
45 atlas_context, {render_frame});
54 const std::shared_ptr<GlyphAtlasContext>& atlas_context,
55 const std::vector<std::shared_ptr<TextFrame>>& frames,
56 const std::vector<std::optional<GlyphProperties>>& properties) {
58 std::vector<RenderableText> render_frames;
59 render_frames.reserve(frames.size());
60 for (
auto& frame : frames) {
61 render_frames.emplace_back(frame,
transform, properties[offset++]);
63 return typographer_context->
CreateGlyphAtlas(context, type, data_host_buffer,
64 atlas_context, render_frames);
68 SkFont font = flutter::testing::CreateTestFontOfSize(12);
69 auto blob = SkTextBlob::MakeFromString(
70 "the quick brown fox jumped over the lazy dog.", font);
73 ASSERT_EQ(frame->GetRunCount(), 1u);
74 for (
const auto& run : frame->GetRuns()) {
75 ASSERT_TRUE(run.IsValid());
76 ASSERT_EQ(run.GetGlyphCount(), 45u);
82 ASSERT_TRUE(context && context->IsValid());
90 GetContext()->GetResourceAllocator(), GetContext()->GetIdleWaiter(),
91 GetContext()->GetCapabilities()->GetMinimumUniformAlignment());
92 ASSERT_TRUE(context && context->IsValid());
93 SkFont sk_font = flutter::testing::CreateTestFontOfSize(12);
94 auto blob = SkTextBlob::MakeFromString(
"hello", sk_font);
101 ASSERT_NE(atlas,
nullptr);
102 ASSERT_NE(atlas->GetTexture(),
nullptr);
104 ASSERT_EQ(atlas->GetGlyphCount(), 4llu);
106 std::optional<impeller::ScaledFont> first_scaled_font;
107 std::optional<impeller::SubpixelGlyph> first_glyph;
109 atlas->IterateGlyphs([&](
const ScaledFont& scaled_font,
111 const Rect& rect) ->
bool {
112 first_scaled_font = scaled_font;
118 ASSERT_TRUE(first_scaled_font.has_value());
120 ->FindFontGlyphBounds(
121 {first_scaled_font.value(), first_glyph.value()})
127 GetContext()->GetResourceAllocator(), GetContext()->GetIdleWaiter(),
128 GetContext()->GetCapabilities()->GetMinimumUniformAlignment());
130 auto mapping = flutter::testing::OpenFixtureAsSkData(
"Apple Color Emoji.ttc");
132 auto mapping = flutter::testing::OpenFixtureAsSkData(
"NotoColorEmoji.ttf");
134 ASSERT_TRUE(mapping);
135 sk_sp<SkFontMgr> font_mgr = txt::GetDefaultFontManager();
136 SkFont emoji_font(font_mgr->makeFromData(mapping), 50.0);
137 SkFont sk_font = flutter::testing::CreateTestFontOfSize(12);
139 auto blob = SkTextBlob::MakeFromString(
"hello", sk_font);
150 SkTextBlob::MakeFromString(
"😀 ", emoji_font));
163 ASSERT_FALSE(color_atlas == bitmap_atlas);
171 GetContext()->GetResourceAllocator(), GetContext()->GetIdleWaiter(),
172 GetContext()->GetCapabilities()->GetMinimumUniformAlignment());
173 ASSERT_TRUE(context && context->IsValid());
174 SkFont sk_font = flutter::testing::CreateTestFontOfSize(12);
175 auto blob = SkTextBlob::MakeFromString(
"AGH", sk_font);
181 ASSERT_NE(atlas,
nullptr);
182 ASSERT_NE(atlas->GetTexture(),
nullptr);
184 EXPECT_EQ(atlas->GetTexture()->GetSize().width, 4096u);
185 EXPECT_EQ(atlas->GetTexture()->GetSize().height, 1024u);
193 GetContext()->GetResourceAllocator(), GetContext()->GetIdleWaiter(),
194 GetContext()->GetCapabilities()->GetMinimumUniformAlignment());
195 ASSERT_TRUE(context && context->IsValid());
196 SkFont sk_font = flutter::testing::CreateTestFontOfSize(12);
197 auto blob = SkTextBlob::MakeFromString(
"spooky skellingtons", sk_font);
203 ASSERT_NE(atlas,
nullptr);
204 ASSERT_NE(atlas->GetTexture(),
nullptr);
205 ASSERT_EQ(atlas, atlas_context->GetGlyphAtlas());
213 ASSERT_EQ(atlas, next_atlas);
214 ASSERT_EQ(atlas_context->GetGlyphAtlas(), atlas);
219 GetContext()->GetResourceAllocator(), GetContext()->GetIdleWaiter(),
220 GetContext()->GetCapabilities()->GetMinimumUniformAlignment());
224 ASSERT_TRUE(context && context->IsValid());
226 const char* test_string =
227 "QWERTYUIOPASDFGHJKLZXCVBNMqewrtyuiopasdfghjklzxcvbnm,.<>[]{};':"
228 "2134567890-=!@#$%^&*()_+"
229 "œ∑´®†¥¨ˆøπ““‘‘åß∂ƒ©˙∆˚¬…æ≈ç√∫˜µ≤≥≥≥≥÷¡™£¢∞§¶•ªº–≠⁄€‹›fifl‡°·‚—±Œ„´‰Á¨Ø∏”’/"
232 SkFont sk_font = flutter::testing::CreateTestFontOfSize(12);
233 auto blob = SkTextBlob::MakeFromString(test_string, sk_font);
236 size_t size_count = 8;
237 std::vector<RenderableText> render_frames;
238 for (
size_t index = 0; index < size_count; index += 1) {
239 Scalar scale = 6.0f * index / 10.0f;
244 auto atlas = context->CreateGlyphAtlas(
246 atlas_context, render_frames);
247 ASSERT_NE(atlas,
nullptr);
248 ASSERT_NE(atlas->GetTexture(),
nullptr);
250 std::set<uint16_t> unique_glyphs;
251 std::vector<uint16_t> total_glyphs;
252 atlas->IterateGlyphs([&](
const ScaledFont& scaled_font,
260 EXPECT_LE(unique_glyphs.size() * size_count, atlas->GetGlyphCount());
261 EXPECT_EQ(total_glyphs.size(), atlas->GetGlyphCount());
263 EXPECT_TRUE(atlas->GetGlyphCount() > 0);
264 EXPECT_TRUE(atlas->GetTexture()->GetSize().width > 0);
265 EXPECT_TRUE(atlas->GetTexture()->GetSize().height > 0);
270 GetContext()->GetResourceAllocator(), GetContext()->GetIdleWaiter(),
271 GetContext()->GetCapabilities()->GetMinimumUniformAlignment());
275 ASSERT_TRUE(context && context->IsValid());
276 SkFont sk_font = flutter::testing::CreateTestFontOfSize(12);
277 auto blob = SkTextBlob::MakeFromString(
"spooky 1", sk_font);
283 auto old_packer = atlas_context->GetRectPacker();
285 ASSERT_NE(atlas,
nullptr);
286 ASSERT_NE(atlas->GetTexture(),
nullptr);
287 ASSERT_EQ(atlas, atlas_context->GetGlyphAtlas());
289 auto* first_texture = atlas->GetTexture().get();
293 auto blob2 = SkTextBlob::MakeFromString(
"spooky 2", sk_font);
298 ASSERT_EQ(atlas, next_atlas);
299 auto* second_texture = next_atlas->GetTexture().get();
301 auto new_packer = atlas_context->GetRectPacker();
303 ASSERT_EQ(second_texture, first_texture);
304 ASSERT_EQ(old_packer, new_packer);
309 GetContext()->GetResourceAllocator(), GetContext()->GetIdleWaiter(),
310 GetContext()->GetCapabilities()->GetMinimumUniformAlignment());
312 auto mapping = flutter::testing::OpenFixtureAsSkData(
"Apple Color Emoji.ttc");
314 auto mapping = flutter::testing::OpenFixtureAsSkData(
"NotoColorEmoji.ttf");
316 ASSERT_TRUE(mapping);
317 sk_sp<SkFontMgr> font_mgr = txt::GetDefaultFontManager();
318 SkFont emoji_font(font_mgr->makeFromData(mapping), 50.0);
327 SkTextBlob::MakeFromString(
"😂", emoji_font));
329 SkTextBlob::MakeFromString(
"😂", emoji_font));
330 std::vector<std::optional<GlyphProperties>> properties = {
338 {frame, frame_2}, properties);
340 EXPECT_EQ(next_atlas->GetGlyphCount(), 2u);
345 GetContext()->GetResourceAllocator(), GetContext()->GetIdleWaiter(),
346 GetContext()->GetCapabilities()->GetMinimumUniformAlignment());
347 sk_sp<SkFontMgr> font_mgr = txt::GetDefaultFontManager();
348 sk_sp<SkTypeface> typeface =
349 font_mgr->matchFamilyStyle(
"Arial", SkFontStyle::Normal());
350 SkFont sk_font(typeface, 0.5f);
362 std::vector<std::optional<GlyphProperties>> properties = {
370 {frame, frame_2}, properties);
372 EXPECT_EQ(next_atlas->GetGlyphCount(), 1u);
377 ASSERT_NE(packer,
nullptr);
378 ASSERT_EQ(packer->PercentFull(), 0);
380 const SkIRect packer_area = SkIRect::MakeXYWH(0, 0, 200, 100);
383 ASSERT_TRUE(packer->AddRect(20, 20, &first_output));
386 const SkIRect first_rect =
387 SkIRect::MakeXYWH(first_output.
x(), first_output.
y(), 20, 20);
388 ASSERT_TRUE(SkIRect::Intersects(packer_area, first_rect));
395 ASSERT_TRUE(packer->AddRect(140, 90, &second_output));
396 const SkIRect second_rect =
397 SkIRect::MakeXYWH(second_output.
x(), second_output.
y(), 140, 90);
400 ASSERT_TRUE(SkIRect::Intersects(packer_area, second_rect));
401 ASSERT_FALSE(SkIRect::Intersects(first_rect, second_rect));
410 ASSERT_FALSE(packer->AddRect(50, 50, &output));
416 ASSERT_EQ(packer->PercentFull(), 0);
424 for (
auto i = 0u; i < 16; i++) {
425 skyline->AddRect(16, 16, &loc);
428 EXPECT_EQ(loc.
x(), 256 - 16);
429 EXPECT_EQ(loc.
y(), 0);
432 for (
auto i = 0u; i < 16; i++) {
433 skyline->AddRect(16, 16, &loc);
436 EXPECT_EQ(loc.
x(), 256 - 16);
437 EXPECT_EQ(loc.
y(), 16);
442 GTEST_SKIP() <<
"Atlas growth isn't supported for OpenGLES currently.";
446 GetContext()->GetResourceAllocator(), GetContext()->GetIdleWaiter(),
447 GetContext()->GetCapabilities()->GetMinimumUniformAlignment());
451 ASSERT_TRUE(context && context->IsValid());
452 SkFont sk_font = flutter::testing::CreateTestFontOfSize(12);
453 auto blob = SkTextBlob::MakeFromString(
"A", sk_font);
463 constexpr
ISize expected_sizes[13] = {
479 SkFont sk_font_small = flutter::testing::CreateTestFontOfSize(10);
481 for (
int i = 0; i < 13; i++) {
482 SkTextBlobBuilder builder;
484 auto add_char = [&](
const SkFont& sk_font,
char c) {
485 int count = sk_font.countText(&
c, 1, SkTextEncoding::kUTF8);
486 auto buffer = builder.allocRunPos(sk_font, count);
487 sk_font.textToGlyphs(&
c, 1, SkTextEncoding::kUTF8,
488 {buffer.glyphs, count});
489 sk_font.getPos({buffer.glyphs, count}, {buffer.points(), count},
493 SkFont sk_font = flutter::testing::CreateTestFontOfSize(50 + i);
494 add_char(sk_font,
'A');
495 add_char(sk_font_small,
'B');
496 auto blob = builder.make();
503 ASSERT_TRUE(!!atlas);
504 EXPECT_EQ(atlas->GetTexture()->GetTextureDescriptor().size,
511 ASSERT_EQ(atlas->GetGlyphCount(), 2u);
515 SkFont font = flutter::testing::CreateTestFontOfSize(12);
516 auto blob = SkTextBlob::MakeFromString(
517 "the quick brown fox jumped over the lazy dog.", font);
525 GetContext()->GetResourceAllocator(), GetContext()->GetIdleWaiter(),
526 GetContext()->GetCapabilities()->GetMinimumUniformAlignment());
528 auto atlas =
CreateGlyphAtlas(*GetContext(), context.get(), *data_host_buffer,
530 atlas_context, frame);
533 auto second_atlas_context =
536 EXPECT_FALSE(second_atlas_context->GetGlyphAtlas()->IsValid());
540 Matrix(), second_atlas_context, frame);
542 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, size_t minimum_uniform_alignment)
const std::shared_ptr< GlyphAtlas > & CreateOrGetGlyphAtlas(Context &context, HostBuffer &host_buffer, GlyphAtlas::Type type)
void AddTextFrame(const std::shared_ptr< TextFrame > &frame, Point position, const Matrix &transform, const 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< RenderableText > &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 &data_host_buffer, GlyphAtlas::Type type, const Matrix &transform, 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.
static constexpr Matrix MakeScale(const Vector3 &s)
const std::shared_ptr< TextFrame > text_frame
The TextFrame being rendered.
A font and a scale. Used as a key that represents a typeface within a glyph atlas.
A glyph and its subpixel position.