6 #include "flutter/impeller/renderer/testing/mocks.h"
7 #include "flutter/testing/testing.h"
12 #include "third_party/googletest/googletest/include/gtest/gtest.h"
13 #include "txt/platform.h"
15 #pragma GCC diagnostic ignored "-Wunreachable-code"
23 using ::testing::Return;
26 std::shared_ptr<TextFrame> MakeTextFrame(
const std::string& text,
27 const std::string_view& font_fixture,
29 auto c_font_fixture = std::string(font_fixture);
30 auto mapping = flutter::testing::OpenFixtureAsSkData(c_font_fixture.c_str());
34 sk_sp<SkFontMgr> font_mgr = txt::GetDefaultFontManager();
35 SkFont sk_font(font_mgr->makeFromData(mapping), font_size);
36 auto blob = SkTextBlob::MakeFromString(text.c_str(), sk_font);
46 const TypographerContext* typographer_context,
47 HostBuffer& host_buffer,
50 const std::shared_ptr<GlyphAtlasContext>& atlas_context,
51 const std::shared_ptr<TextFrame>& frame) {
52 frame->SetPerFrameData(
56 return typographer_context->CreateGlyphAtlas(context,
type, host_buffer,
57 atlas_context, {frame});
60 Rect PerVertexDataPositionToRect(
61 GlyphAtlasPipeline::VertexShader::PerVertexData
data[6]) {
66 for (
int i = 0; i < 6; ++i) {
67 right = std::max(right,
data[i].position.x);
68 left = std::min(left,
data[i].position.x);
69 top = std::min(top,
data[i].position.y);
70 bottom = std::max(bottom,
data[i].position.y);
76 Rect PerVertexDataUVToRect(
77 GlyphAtlasPipeline::VertexShader::PerVertexData
data[6],
83 for (
int i = 0; i < 6; ++i) {
84 right = std::max(right,
data[i].uv.x * texture_size.width);
85 left = std::min(left,
data[i].uv.x * texture_size.width);
86 top = std::min(top,
data[i].uv.y * texture_size.height);
87 bottom = std::max(bottom,
data[i].uv.y * texture_size.height);
93 double GetAspectRatio(
Rect rect) {
94 return static_cast<double>(rect.GetWidth()) / rect.GetHeight();
100 GTEST_SKIP() <<
"Results aren't stable across linux and macos.";
103 GlyphAtlasPipeline::VertexShader::PerVertexData
data[6];
105 std::shared_ptr<TextFrame> text_frame =
106 MakeTextFrame(
"1",
"ahem.ttf", 50);
109 std::shared_ptr<GlyphAtlasContext> atlas_context =
112 GetContext()->GetResourceAllocator(), GetContext()->GetIdleWaiter());
113 ASSERT_TRUE(context && context->IsValid());
114 std::shared_ptr<GlyphAtlas> atlas =
117 atlas_context, text_frame);
119 ISize texture_size = atlas->GetTexture()->GetSize();
123 std::nullopt, atlas);
125 Rect position_rect = PerVertexDataPositionToRect(
data);
126 Rect uv_rect = PerVertexDataUVToRect(
data, texture_size);
135 #ifndef FML_OS_MACOSX
136 GTEST_SKIP() <<
"Results aren't stable across linux and macos.";
139 GlyphAtlasPipeline::VertexShader::PerVertexData
data[6];
141 std::shared_ptr<TextFrame> text_frame =
142 MakeTextFrame(
"1",
"ahem.ttf", 50);
145 std::shared_ptr<GlyphAtlasContext> atlas_context =
148 GetContext()->GetResourceAllocator(), GetContext()->GetIdleWaiter());
149 ASSERT_TRUE(context && context->IsValid());
152 *GetContext(), context.get(), *host_buffer,
155 ISize texture_size = atlas->GetTexture()->GetSize();
157 data, text_frame, font_scale,
160 std::nullopt, atlas);
162 Rect position_rect = PerVertexDataPositionToRect(
data);
163 Rect uv_rect = PerVertexDataUVToRect(
data, texture_size);
169 std::shared_ptr<TextFrame> text_frame =
170 MakeTextFrame(
"th",
"ahem.ttf", 50);
173 std::shared_ptr<GlyphAtlasContext> atlas_context =
176 GetContext()->GetResourceAllocator(), GetContext()->GetIdleWaiter());
177 ASSERT_TRUE(context && context->IsValid());
179 for (
int i = 0; i <= 1000; ++i) {
180 Scalar font_scale = 0.440 + (i / 1000.0);
181 Rect position_rect[2];
185 GlyphAtlasPipeline::VertexShader::PerVertexData
data[12];
186 std::shared_ptr<GlyphAtlas> atlas =
189 atlas_context, text_frame);
190 ISize texture_size = atlas->GetTexture()->GetSize();
193 data, text_frame, font_scale,
196 std::nullopt, atlas);
197 position_rect[0] = PerVertexDataPositionToRect(
data);
198 uv_rect[0] = PerVertexDataUVToRect(
data, texture_size);
199 position_rect[1] = PerVertexDataPositionToRect(
data + 6);
200 uv_rect[1] = PerVertexDataUVToRect(
data + 6, texture_size);
202 EXPECT_NEAR(GetAspectRatio(position_rect[1]), GetAspectRatio(uv_rect[1]),
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)
static void ComputeVertexData(GlyphAtlasPipeline::VertexShader::PerVertexData *vtx_contents, const std::shared_ptr< TextFrame > &frame, Scalar scale, const Matrix &entity_transform, Vector2 offset, std::optional< GlyphProperties > glyph_properties, const std::shared_ptr< GlyphAtlas > &atlas)
static Scalar RoundScaledFontSize(Scalar scale)
static std::shared_ptr< TypographerContext > Make()
#define EXPECT_RECT_NEAR(a, b)
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)
A 4x4 matrix using column-major storage.
static constexpr Matrix MakeScale(const Vector3 &s)
constexpr static TRect MakeXYWH(Type x, Type y, Type width, Type height)
constexpr static TRect MakeLTRB(Type left, Type top, Type right, Type bottom)
std::shared_ptr< const fml::Mapping > data