|
8 | 8 | #include "impeller/aiks/image.h" |
9 | 9 | #include "impeller/geometry/geometry_unittests.h" |
10 | 10 | #include "impeller/geometry/path_builder.h" |
| 11 | +#include "impeller/typographer/backends/skia/text_frame_skia.h" |
| 12 | +#include "impeller/typographer/backends/skia/text_render_context_skia.h" |
| 13 | +#include "third_party/skia/include/core/SkData.h" |
11 | 14 |
|
12 | 15 | namespace impeller { |
13 | 16 | namespace testing { |
@@ -283,5 +286,114 @@ TEST_F(AiksTest, CanRenderDifferencePaths) { |
283 | 286 | ASSERT_TRUE(OpenPlaygroundHere(canvas.EndRecordingAsPicture())); |
284 | 287 | } |
285 | 288 |
|
| 289 | +static sk_sp<SkData> OpenFixtureAsSkData(const char* fixture_name) { |
| 290 | + auto mapping = flutter::testing::OpenFixtureAsMapping(fixture_name); |
| 291 | + if (!mapping) { |
| 292 | + return nullptr; |
| 293 | + } |
| 294 | + return SkData::MakeWithProc( |
| 295 | + mapping->GetMapping(), mapping->GetSize(), |
| 296 | + [](const void* ptr, void* context) { |
| 297 | + delete reinterpret_cast<fml::Mapping*>(context); |
| 298 | + }, |
| 299 | + mapping.release()); |
| 300 | +} |
| 301 | + |
| 302 | +TEST_F(AiksTest, CanRenderTextFrame) { |
| 303 | + Canvas canvas; |
| 304 | + |
| 305 | + Scalar baseline = 200.0; |
| 306 | + Point text_position = {100, baseline}; |
| 307 | + |
| 308 | + // Draw the baseline. |
| 309 | + canvas.DrawRect({50, baseline, 900, 10}, |
| 310 | + Paint{.color = Color::Aqua().WithAlpha(0.25)}); |
| 311 | + |
| 312 | + // Mark the point at which the text is drawn. |
| 313 | + canvas.DrawCircle(text_position, 5.0, |
| 314 | + Paint{.color = Color::Red().WithAlpha(0.25)}); |
| 315 | + |
| 316 | + // Construct the text blob. |
| 317 | + auto mapping = OpenFixtureAsSkData("Roboto-Regular.ttf"); |
| 318 | + ASSERT_TRUE(mapping); |
| 319 | + SkFont sk_font(SkTypeface::MakeFromData(mapping), 50.0); |
| 320 | + auto blob = SkTextBlob::MakeFromString( |
| 321 | + "the quick brown fox jumped over the lazy dog!.?", sk_font); |
| 322 | + ASSERT_TRUE(blob); |
| 323 | + |
| 324 | + // Create the Impeller text frame and draw it at the designated baseline. |
| 325 | + auto frame = TextFrameFromTextBlob(blob); |
| 326 | + TextRenderContextSkia text_context(GetContext()); |
| 327 | + ASSERT_TRUE(text_context.IsValid()); |
| 328 | + auto atlas = text_context.CreateGlyphAtlas(frame); |
| 329 | + ASSERT_NE(atlas, nullptr); |
| 330 | + canvas.DrawTextFrame(std::move(frame), std::move(atlas), text_position); |
| 331 | + ASSERT_TRUE(OpenPlaygroundHere(canvas.EndRecordingAsPicture())); |
| 332 | +} |
| 333 | + |
| 334 | +TEST_F(AiksTest, CanRenderItalicizedText) { |
| 335 | + Canvas canvas; |
| 336 | + |
| 337 | + Scalar baseline = 200.0; |
| 338 | + Point text_position = {100, baseline}; |
| 339 | + |
| 340 | + // Draw the baseline. |
| 341 | + canvas.DrawRect({50, baseline, 900, 10}, |
| 342 | + Paint{.color = Color::Aqua().WithAlpha(0.25)}); |
| 343 | + |
| 344 | + // Mark the point at which the text is drawn. |
| 345 | + canvas.DrawCircle(text_position, 5.0, |
| 346 | + Paint{.color = Color::Red().WithAlpha(0.25)}); |
| 347 | + |
| 348 | + // Construct the text blob. |
| 349 | + auto mapping = OpenFixtureAsSkData("HomemadeApple.ttf"); |
| 350 | + ASSERT_TRUE(mapping); |
| 351 | + SkFont sk_font(SkTypeface::MakeFromData(mapping), 50.0); |
| 352 | + auto blob = SkTextBlob::MakeFromString( |
| 353 | + "the quick brown fox jumped over the lazy dog!.?", sk_font); |
| 354 | + ASSERT_TRUE(blob); |
| 355 | + |
| 356 | + // Create the Impeller text frame and draw it at the designated baseline. |
| 357 | + auto frame = TextFrameFromTextBlob(blob); |
| 358 | + TextRenderContextSkia text_context(GetContext()); |
| 359 | + ASSERT_TRUE(text_context.IsValid()); |
| 360 | + auto atlas = text_context.CreateGlyphAtlas(frame); |
| 361 | + ASSERT_NE(atlas, nullptr); |
| 362 | + canvas.DrawTextFrame(std::move(frame), std::move(atlas), text_position); |
| 363 | + ASSERT_TRUE(OpenPlaygroundHere(canvas.EndRecordingAsPicture())); |
| 364 | +} |
| 365 | + |
| 366 | +TEST_F(AiksTest, CanRenderEmojiTextFrame) { |
| 367 | + Canvas canvas; |
| 368 | + |
| 369 | + Scalar baseline = 200.0; |
| 370 | + Point text_position = {100, baseline}; |
| 371 | + |
| 372 | + // Draw the baseline. |
| 373 | + canvas.DrawRect({50, baseline, 900, 10}, |
| 374 | + Paint{.color = Color::Aqua().WithAlpha(0.25)}); |
| 375 | + |
| 376 | + // Mark the point at which the text is drawn. |
| 377 | + canvas.DrawCircle(text_position, 5.0, |
| 378 | + Paint{.color = Color::Red().WithAlpha(0.25)}); |
| 379 | + |
| 380 | + // Construct the text blob. |
| 381 | + auto mapping = OpenFixtureAsSkData("NotoColorEmoji.ttf"); |
| 382 | + ASSERT_TRUE(mapping); |
| 383 | + SkFont sk_font(SkTypeface::MakeFromData(mapping), 50.0); |
| 384 | + auto blob = SkTextBlob::MakeFromString( |
| 385 | + "😀 😃 😄 😁 😆 😅 😂 🤣 🥲 ☺️ 😊", sk_font); |
| 386 | + ASSERT_TRUE(blob); |
| 387 | + |
| 388 | + // Create the Impeller text frame and draw it at the designated baseline. |
| 389 | + auto frame = TextFrameFromTextBlob(blob); |
| 390 | + TextRenderContextSkia text_context(GetContext()); |
| 391 | + ASSERT_TRUE(text_context.IsValid()); |
| 392 | + auto atlas = text_context.CreateGlyphAtlas(frame); |
| 393 | + ASSERT_NE(atlas, nullptr); |
| 394 | + canvas.DrawTextFrame(std::move(frame), std::move(atlas), text_position); |
| 395 | + ASSERT_TRUE(OpenPlaygroundHere(canvas.EndRecordingAsPicture())); |
| 396 | +} |
| 397 | + |
286 | 398 | } // namespace testing |
287 | 399 | } // namespace impeller |
0 commit comments