| Flutter Impeller |

Public Member Functions | Static Public Member Functions | List of all members
impeller::TypographerContextSTB Class Reference

`#include <typographer_context_stb.h>`

Inheritance diagram for impeller::TypographerContextSTB:

## Public Member Functions
| TypographerContextSTB ()
| ~TypographerContextSTB () override
std::shared_ptr< GlyphAtlasContext > | CreateGlyphAtlasContext () const override
std::shared_ptr< GlyphAtlas > | CreateGlyphAtlas (Context &context, GlyphAtlas::Type type, const std::shared_ptr< GlyphAtlasContext > &atlas_context, const FontGlyphMap &font_glyph_map) const override
Public Member Functions inherited from impeller::TypographerContext
virtual | ~TypographerContext ()
virtual bool | IsValid () const

## Static Public Member Functions
static std::unique_ptr< TypographerContext > | Make ()

## Additional Inherited Members
Protected Member Functions inherited from impeller::TypographerContext
| TypographerContext ()
| Create a new context to render text that talks to an underlying graphics context. More...

## Detailed Description

Definition at line 15 of file typographer_context_stb.h.

## Constructor & Destructor Documentation

## ◆ TypographerContextSTB()

impeller::TypographerContextSTB::TypographerContextSTB | ( |  | ) |

Definition at line 33 of file typographer_context_stb.cc.

33 : TypographerContext() {}

## ◆ ~TypographerContextSTB()

impeller::TypographerContextSTB::~TypographerContextSTB | ( |  | ) |
| override default

## Member Function Documentation

## ◆ CreateGlyphAtlas()

std::shared_ptr< GlyphAtlas > impeller::TypographerContextSTB::CreateGlyphAtlas | ( | Context & | context,
|  | GlyphAtlas::Type | type,
|  | const std::shared_ptr< GlyphAtlasContext > & | atlas_context,
|  | const FontGlyphMap & | font_glyph_map
| ) |  | const
| override virtual

Implements impeller::TypographerContext.

Definition at line 375 of file typographer_context_stb.cc.

379 {
380 TRACE_EVENT0("impeller", __FUNCTION__);
381 if (!IsValid()) {
382 return nullptr;
383 }
384 auto& atlas_context_stb = GlyphAtlasContextSTB::Cast(*atlas_context);
385 std::shared_ptr<GlyphAtlas> last_atlas = atlas_context->GetGlyphAtlas();
386
387 if (font_glyph_map.empty()) {
388 return last_atlas;
389 }
390
391 // ---------------------------------------------------------------------------
392 // Step 1: Determine if the atlas type and font glyph pairs are compatible
393 // with the current atlas and reuse if possible.
394 // ---------------------------------------------------------------------------
395 std::vector<FontGlyphPair> new_glyphs;
396 for (const auto& font_value : font_glyph_map) {
397 const ScaledFont& scaled_font = font_value.first;
398 const FontGlyphAtlas* font_glyph_atlas =
399 last_atlas->GetFontGlyphAtlas(scaled_font.font, scaled_font.scale);
400 if (font_glyph_atlas) {
401 for (const Glyph& glyph : font_value.second) {
402 if (!font_glyph_atlas->FindGlyphBounds(glyph)) {
403 new_glyphs.emplace_back(scaled_font, glyph);
404 }
405 }
406 } else {
407 for (const Glyph& glyph : font_value.second) {
408 new_glyphs.emplace_back(scaled_font, glyph);
409 }
410 }
411 }
412 if (last_atlas->GetType() == type && new_glyphs.size() == 0) {
413 return last_atlas;
414 }
415
416 // ---------------------------------------------------------------------------
417 // Step 2: Determine if the additional missing glyphs can be appended to the
418 // existing bitmap without recreating the atlas. This requires that
419 // the type is identical.
420 // ---------------------------------------------------------------------------
421 std::vector<Rect> glyph_positions;
422 if (last_atlas->GetType() == type &&
423 CanAppendToExistingAtlas(last_atlas, new_glyphs, glyph_positions,
424 atlas_context->GetAtlasSize(),
425 atlas_context->GetRectPacker())) {
426 // The old bitmap will be reused and only the additional glyphs will be
427 // added.
428
429 // ---------------------------------------------------------------------------
430 // Step 3a: Record the positions in the glyph atlas of the newly added
431 // glyphs.
432 // ---------------------------------------------------------------------------
433 for (size_t i = 0, count = glyph_positions.size(); i < count; i++) {
434 last_atlas->AddTypefaceGlyphPosition(new_glyphs[i], glyph_positions[i]);
435 }
436
437 // ---------------------------------------------------------------------------
438 // Step 4a: Draw new font-glyph pairs into the existing bitmap.
439 // ---------------------------------------------------------------------------
440 // auto bitmap = atlas_context->GetBitmap();
441 auto bitmap = atlas_context_stb.GetBitmap();
442 if (!UpdateAtlasBitmap(*last_atlas, bitmap, new_glyphs)) {
443 return nullptr;
444 }
445
446 // ---------------------------------------------------------------------------
447 // Step 5a: Update the existing texture with the updated bitmap.
448 // ---------------------------------------------------------------------------
449 if (!UpdateGlyphTextureAtlas(bitmap, last_atlas->GetTexture())) {
450 return nullptr;
451 }
452 return last_atlas;
453 }
454 // A new glyph atlas must be created.
455
456 // ---------------------------------------------------------------------------
457 // Step 3b: Get the optimum size of the texture atlas.
458 // ---------------------------------------------------------------------------
459 std::vector<FontGlyphPair> font_glyph_pairs;
460 font_glyph_pairs.reserve(std::accumulate(
461 font_glyph_map.begin(), font_glyph_map.end(), 0,
462 [](const int a, const auto& b) { return a + b.second.size(); }));
463 for (const auto& font_value : font_glyph_map) {
464 const ScaledFont& scaled_font = font_value.first;
465 for (const Glyph& glyph : font_value.second) {
466 font_glyph_pairs.push_back({scaled_font, glyph});
467 }
468 }
469 auto glyph_atlas = std::make_shared<GlyphAtlas>(type);
470 auto atlas_size = OptimumAtlasSizeForFontGlyphPairs(
471 font_glyph_pairs, //
472 glyph_positions, //
473 atlas_context, //
474 type, //
475 context.GetResourceAllocator()->GetMaxTextureSizeSupported() //
476 );
477
478 atlas_context->UpdateGlyphAtlas(glyph_atlas, atlas_size);
479 if (atlas_size.IsEmpty()) {
480 return nullptr;
481 }
482
483 // ---------------------------------------------------------------------------
484 // Step 4b: Find location of font-glyph pairs in the atlas. We have this from
485 // the last step. So no need to do create another rect packer. But just do a
486 // sanity check of counts. This could also be just an assertion as only a
487 // construction issue would cause such a failure.
488 // ---------------------------------------------------------------------------
489 if (glyph_positions.size() != font_glyph_pairs.size()) {
490 return nullptr;
491 }
492
493 // ---------------------------------------------------------------------------
494 // Step 5b: Record the positions in the glyph atlas.
495 // ---------------------------------------------------------------------------
496 {
497 size_t i = 0;
498 for (auto it = font_glyph_pairs.begin(); it != font_glyph_pairs.end();
499 ++i, ++it) {
500 glyph_atlas->AddTypefaceGlyphPosition(*it, glyph_positions[i]);
501 }
502 }
503
504 // ---------------------------------------------------------------------------
505 // Step 6b: Draw font-glyph pairs in the correct spot in the atlas.
506 // ---------------------------------------------------------------------------
507 auto bitmap = CreateAtlasBitmap(*glyph_atlas, atlas_size);
508 if (!bitmap) {
509 return nullptr;
510 }
511 atlas_context_stb.UpdateBitmap(bitmap);
512
513 // ---------------------------------------------------------------------------
514 // Step 7b: Upload the atlas as a texture.
515 // ---------------------------------------------------------------------------
516 PixelFormat format;
517 switch (type) {
518 case GlyphAtlas::Type::kAlphaBitmap:
519 format = context.GetCapabilities()->GetDefaultGlyphAtlasFormat();
520 break;
521 case GlyphAtlas::Type::kColorBitmap:
522 format = DISABLE_COLOR_FONT_SUPPORT
523 ? context.GetCapabilities()->GetDefaultGlyphAtlasFormat()
524 : PixelFormat::kR8G8B8A8UNormInt;
525 break;
526 }
527 auto texture = UploadGlyphTextureAtlas(context.GetResourceAllocator(), bitmap,
528 atlas_size, format);
529 if (!texture) {
530 return nullptr;
531 }
532
533 // ---------------------------------------------------------------------------
534 // Step 8b: Record the texture in the glyph atlas.
535 // ---------------------------------------------------------------------------
536 glyph_atlas->SetTexture(std::move(texture));
537
538 return glyph_atlas;
539 }

References impeller::saturated::b, impeller::CanAppendToExistingAtlas(), impeller::BackendCast< GlyphAtlasContextSTB, GlyphAtlasContext >::Cast(), impeller::CreateAtlasBitmap(), DISABLE_COLOR_FONT_SUPPORT, impeller::FontGlyphAtlas::FindGlyphBounds(), impeller::ScaledFont::font, impeller::Context::GetCapabilities(), impeller::Context::GetResourceAllocator(), impeller::TypographerContext::IsValid(), impeller::GlyphAtlas::kAlphaBitmap, impeller::GlyphAtlas::kColorBitmap, impeller::kR8G8B8A8UNormInt, impeller::OptimumAtlasSizeForFontGlyphPairs(), impeller::ScaledFont::scale, impeller::UpdateAtlasBitmap(), impeller::UpdateGlyphTextureAtlas(), and impeller::UploadGlyphTextureAtlas().

## ◆ CreateGlyphAtlasContext()

std::shared_ptr< GlyphAtlasContext > impeller::TypographerContextSTB::CreateGlyphAtlasContext | ( |  | ) | const
| override virtual

Implements impeller::TypographerContext.

Definition at line 38 of file typographer_context_stb.cc.

38 {
39 return std::make_shared<GlyphAtlasContextSTB>();
40 }

## ◆ Make()

std::unique_ptr< TypographerContext > impeller::TypographerContextSTB::Make | ( |  | ) |
| static

Definition at line 29 of file typographer_context_stb.cc.

29 {
30 return std::make_unique<TypographerContextSTB>();
31 }

Referenced by impeller::testing::TEST_P().

The documentation for this class was generated from the following files:

- impeller/typographer/backends/stb/typographer_context_stb.h
- impeller/typographer/backends/stb/typographer_context_stb.cc

impeller::GlyphAtlas::Type::kColorBitmap
@ kColorBitmap
impeller::GlyphAtlas::Type::kAlphaBitmap
@ kAlphaBitmap
impeller::CreateAtlasBitmap
static std::shared_ptr< SkBitmap > CreateAtlasBitmap(const GlyphAtlas &atlas, const ISize &atlas_size)
Definition: typographer_context_skia.cc:217
impeller::PixelFormat::kR8G8B8A8UNormInt
@ kR8G8B8A8UNormInt
impeller::UpdateAtlasBitmap
static bool UpdateAtlasBitmap(const GlyphAtlas &atlas, const std::shared_ptr< SkBitmap > &bitmap, const std::vector< FontGlyphPair > &new_pairs)
Definition: typographer_context_skia.cc:190
impeller::TypographerContext::TypographerContext
TypographerContext()
Create a new context to render text that talks to an underlying graphics context.
Definition: typographer_context.cc:11
impeller::PixelFormat
PixelFormat
The Pixel formats supported by Impeller. The naming convention denotes the usage of the component,...
Definition: formats.h:100
impeller::CanAppendToExistingAtlas
static bool CanAppendToExistingAtlas(const std::shared_ptr< GlyphAtlas > &atlas, const std::vector< FontGlyphPair > &extra_pairs, std::vector< Rect > &glyph_positions, ISize atlas_size, const std::shared_ptr< RectanglePacker > &rect_packer)
Definition: typographer_context_skia.cc:80
impeller::UpdateGlyphTextureAtlas
static bool UpdateGlyphTextureAtlas(std::shared_ptr< SkBitmap > bitmap, const std::shared_ptr< Texture > &texture)
Definition: typographer_context_skia.cc:260
impeller::UploadGlyphTextureAtlas
static std::shared_ptr< Texture > UploadGlyphTextureAtlas(const std::shared_ptr< Allocator > &allocator, std::shared_ptr< SkBitmap > bitmap, const ISize &atlas_size, PixelFormat format)
Definition: typographer_context_skia.cc:276
DISABLE_COLOR_FONT_SUPPORT
#define DISABLE_COLOR_FONT_SUPPORT
Definition: typographer_context_stb.cc:18
impeller::saturated::b
SI b
Definition: saturated_math.h:87
impeller::BackendCast< GlyphAtlasContextSTB, GlyphAtlasContext >::Cast
static GlyphAtlasContextSTB & Cast(GlyphAtlasContext &base)
Definition: backend_cast.h:13
impeller::OptimumAtlasSizeForFontGlyphPairs
static ISize OptimumAtlasSizeForFontGlyphPairs(const std::vector< FontGlyphPair > &pairs, std::vector< Rect > &glyph_positions, const std::shared_ptr< GlyphAtlasContext > &atlas_context, GlyphAtlas::Type type, const ISize &max_texture_size)
Definition: typographer_context_skia.cc:118
impeller::TypographerContext::IsValid
virtual bool IsValid() const
Definition: typographer_context.cc:17