[Impeller] libImpeller: Initialize the base paragraph style lazily. (flutter/engine#55891)
Thanks to the @lyceel for the failing test case. <img width="1136" alt="Screenshot 2024-10-15 at 4 01 27â¯PM" src="https://github.com/user-attachments/assets/eeecba1d-eecb-4f29-878f-caf4c674a836">
This commit is contained in:
@@ -1009,7 +1009,8 @@ void ImpellerDisplayListBuilderDrawParagraph(ImpellerDisplayListBuilder builder,
|
||||
IMPELLER_EXTERN_C
|
||||
ImpellerParagraphBuilder ImpellerParagraphBuilderNew(
|
||||
ImpellerTypographyContext context) {
|
||||
auto builder = Create<ParagraphBuilder>(*GetPeer(context));
|
||||
auto builder =
|
||||
Create<ParagraphBuilder>(Ref<TypographyContext>(GetPeer(context)));
|
||||
if (!builder->IsValid()) {
|
||||
VALIDATION_LOG << "Could not create valid paragraph builder.";
|
||||
return nullptr;
|
||||
|
||||
@@ -245,4 +245,97 @@ TEST_P(InteropPlaygroundTest, CanCreateParagraphs) {
|
||||
}));
|
||||
}
|
||||
|
||||
static void DrawTextFrame(ImpellerTypographyContext tc,
|
||||
ImpellerDisplayListBuilder builder,
|
||||
ImpellerParagraphStyle p_style,
|
||||
ImpellerPaint bg,
|
||||
ImpellerColor color,
|
||||
ImpellerTextAlignment align,
|
||||
float x_offset) {
|
||||
const char text[] =
|
||||
"Lorem ipsum dolor sit amet, consectetur adipiscing elit.";
|
||||
|
||||
ImpellerPaint fg = ImpellerPaintNew();
|
||||
|
||||
// Draw a box.
|
||||
ImpellerPaintSetColor(fg, &color);
|
||||
ImpellerPaintSetDrawStyle(fg, kImpellerDrawStyleStroke);
|
||||
ImpellerRect box_rect = {10 + x_offset, 10, 200, 200};
|
||||
ImpellerDisplayListBuilderDrawRect(builder, &box_rect, fg);
|
||||
|
||||
// Draw text.
|
||||
ImpellerPaintSetDrawStyle(fg, kImpellerDrawStyleFill);
|
||||
ImpellerParagraphStyleSetForeground(p_style, fg);
|
||||
ImpellerParagraphStyleSetBackground(p_style, bg);
|
||||
ImpellerParagraphStyleSetTextAlignment(p_style, align);
|
||||
ImpellerParagraphBuilder p_builder = ImpellerParagraphBuilderNew(tc);
|
||||
ImpellerParagraphBuilderPushStyle(p_builder, p_style);
|
||||
ImpellerParagraphBuilderAddText(p_builder, (const uint8_t*)text,
|
||||
sizeof(text));
|
||||
ImpellerParagraph left_p = ImpellerParagraphBuilderBuildParagraphNew(
|
||||
p_builder, box_rect.width - 20.0);
|
||||
ImpellerPoint pt = {20.0f + x_offset, 20.0f};
|
||||
float w = ImpellerParagraphGetMaxWidth(left_p);
|
||||
float h = ImpellerParagraphGetHeight(left_p);
|
||||
ImpellerDisplayListBuilderDrawParagraph(builder, left_p, &pt);
|
||||
ImpellerPaintSetDrawStyle(fg, kImpellerDrawStyleStroke);
|
||||
|
||||
// Draw an inner box around the paragraph layout.
|
||||
ImpellerRect inner_box_rect = {pt.x, pt.y, w, h};
|
||||
ImpellerDisplayListBuilderDrawRect(builder, &inner_box_rect, fg);
|
||||
|
||||
ImpellerParagraphRelease(left_p);
|
||||
ImpellerParagraphBuilderRelease(p_builder);
|
||||
ImpellerPaintRelease(fg);
|
||||
}
|
||||
|
||||
TEST_P(InteropPlaygroundTest, CanRenderTextAlignments) {
|
||||
ImpellerTypographyContext tc = ImpellerTypographyContextNew();
|
||||
|
||||
ImpellerDisplayList dl = NULL;
|
||||
|
||||
{
|
||||
ImpellerDisplayListBuilder builder = ImpellerDisplayListBuilderNew(NULL);
|
||||
ImpellerPaint bg = ImpellerPaintNew();
|
||||
ImpellerParagraphStyle p_style = ImpellerParagraphStyleNew();
|
||||
ImpellerParagraphStyleSetFontFamily(p_style, "Roboto");
|
||||
ImpellerParagraphStyleSetFontSize(p_style, 24.0);
|
||||
ImpellerParagraphStyleSetFontWeight(p_style, kImpellerFontWeight400);
|
||||
|
||||
// Clear the background to a white color.
|
||||
ImpellerColor clear_color = {1.0, 1.0, 1.0, 1.0};
|
||||
ImpellerPaintSetColor(bg, &clear_color);
|
||||
ImpellerDisplayListBuilderDrawPaint(builder, bg);
|
||||
|
||||
// Draw red, left-aligned text.
|
||||
ImpellerColor red = {1.0, 0.0, 0.0, 1.0};
|
||||
DrawTextFrame(tc, builder, p_style, bg, red, kImpellerTextAlignmentLeft,
|
||||
0.0);
|
||||
|
||||
// Draw green, centered text.
|
||||
ImpellerColor green = {0.0, 1.0, 0.0, 1.0};
|
||||
DrawTextFrame(tc, builder, p_style, bg, green, kImpellerTextAlignmentCenter,
|
||||
220.0);
|
||||
|
||||
// Draw blue, right-aligned text.
|
||||
ImpellerColor blue = {0.0, 0.0, 1.0, 1.0};
|
||||
DrawTextFrame(tc, builder, p_style, bg, blue, kImpellerTextAlignmentRight,
|
||||
440.0);
|
||||
|
||||
dl = ImpellerDisplayListBuilderCreateDisplayListNew(builder);
|
||||
|
||||
ImpellerPaintRelease(bg);
|
||||
ImpellerDisplayListBuilderRelease(builder);
|
||||
}
|
||||
|
||||
ASSERT_TRUE(
|
||||
OpenPlaygroundHere([&](const auto& context, const auto& surface) -> bool {
|
||||
ImpellerSurfaceDrawDisplayList(surface.GetC(), dl);
|
||||
return true;
|
||||
}));
|
||||
|
||||
ImpellerDisplayListRelease(dl);
|
||||
ImpellerTypographyContextRelease(tc);
|
||||
}
|
||||
|
||||
} // namespace impeller::interop::testing
|
||||
|
||||
@@ -10,41 +10,29 @@
|
||||
|
||||
namespace impeller::interop {
|
||||
|
||||
ParagraphBuilder::ParagraphBuilder(const TypographyContext& context) {
|
||||
if (!context.IsValid()) {
|
||||
VALIDATION_LOG << "Invalid typography context.";
|
||||
return;
|
||||
}
|
||||
|
||||
static txt::ParagraphStyle kBaseStyle;
|
||||
|
||||
builder_ = std::make_unique<txt::ParagraphBuilderSkia>(
|
||||
kBaseStyle, //
|
||||
context.GetFontCollection(), //
|
||||
true // is impeller enabled
|
||||
);
|
||||
}
|
||||
ParagraphBuilder::ParagraphBuilder(ScopedObject<TypographyContext> context)
|
||||
: context_(std::move(context)) {}
|
||||
|
||||
ParagraphBuilder::~ParagraphBuilder() = default;
|
||||
|
||||
bool ParagraphBuilder::IsValid() const {
|
||||
return !!builder_;
|
||||
return !!context_;
|
||||
}
|
||||
|
||||
void ParagraphBuilder::PushStyle(const ParagraphStyle& style) {
|
||||
builder_->PushStyle(style.CreateTextStyle());
|
||||
GetBuilder(style.GetParagraphStyle())->PushStyle(style.CreateTextStyle());
|
||||
}
|
||||
|
||||
void ParagraphBuilder::PopStyle() {
|
||||
builder_->Pop();
|
||||
GetBuilder()->Pop();
|
||||
}
|
||||
|
||||
void ParagraphBuilder::AddText(const uint8_t* data, size_t byte_length) {
|
||||
builder_->AddText(data, byte_length);
|
||||
GetBuilder()->AddText(data, byte_length);
|
||||
}
|
||||
|
||||
ScopedObject<Paragraph> ParagraphBuilder::Build(Scalar width) const {
|
||||
auto txt_paragraph = builder_->Build();
|
||||
auto txt_paragraph = GetBuilder()->Build();
|
||||
if (!txt_paragraph) {
|
||||
return nullptr;
|
||||
}
|
||||
@@ -52,4 +40,23 @@ ScopedObject<Paragraph> ParagraphBuilder::Build(Scalar width) const {
|
||||
return Create<Paragraph>(std::move(txt_paragraph));
|
||||
}
|
||||
|
||||
const std::unique_ptr<txt::ParagraphBuilder>& ParagraphBuilder::GetBuilder(
|
||||
const txt::ParagraphStyle& style) const {
|
||||
if (lazy_builder_) {
|
||||
return lazy_builder_;
|
||||
}
|
||||
lazy_builder_ = std::make_unique<txt::ParagraphBuilderSkia>(
|
||||
style, //
|
||||
context_->GetFontCollection(), //
|
||||
true // is impeller enabled
|
||||
);
|
||||
return lazy_builder_;
|
||||
}
|
||||
|
||||
const std::unique_ptr<txt::ParagraphBuilder>& ParagraphBuilder::GetBuilder()
|
||||
const {
|
||||
static txt::ParagraphStyle kDefaultStyle;
|
||||
return GetBuilder(kDefaultStyle);
|
||||
}
|
||||
|
||||
} // namespace impeller::interop
|
||||
|
||||
@@ -20,7 +20,7 @@ class ParagraphBuilder final
|
||||
: public Object<ParagraphBuilder,
|
||||
IMPELLER_INTERNAL_HANDLE_NAME(ImpellerParagraphBuilder)> {
|
||||
public:
|
||||
explicit ParagraphBuilder(const TypographyContext& context);
|
||||
explicit ParagraphBuilder(ScopedObject<TypographyContext> context);
|
||||
|
||||
~ParagraphBuilder() override;
|
||||
|
||||
@@ -39,7 +39,13 @@ class ParagraphBuilder final
|
||||
ScopedObject<Paragraph> Build(Scalar width) const;
|
||||
|
||||
private:
|
||||
std::unique_ptr<txt::ParagraphBuilder> builder_;
|
||||
ScopedObject<TypographyContext> context_;
|
||||
mutable std::unique_ptr<txt::ParagraphBuilder> lazy_builder_;
|
||||
|
||||
const std::unique_ptr<txt::ParagraphBuilder>& GetBuilder(
|
||||
const txt::ParagraphStyle& style) const;
|
||||
|
||||
const std::unique_ptr<txt::ParagraphBuilder>& GetBuilder() const;
|
||||
};
|
||||
|
||||
} // namespace impeller::interop
|
||||
|
||||
@@ -51,18 +51,23 @@ void ParagraphStyle::SetForeground(ScopedObject<Paint> paint) {
|
||||
}
|
||||
|
||||
void ParagraphStyle::SetBackground(ScopedObject<Paint> paint) {
|
||||
backgrond_ = std::move(paint);
|
||||
background_ = std::move(paint);
|
||||
}
|
||||
|
||||
txt::TextStyle ParagraphStyle::CreateTextStyle() const {
|
||||
auto style = style_.GetTextStyle();
|
||||
|
||||
if (foreground_) {
|
||||
style.foreground = foreground_->GetPaint();
|
||||
}
|
||||
if (backgrond_) {
|
||||
style.background = backgrond_->GetPaint();
|
||||
if (background_) {
|
||||
style.background = background_->GetPaint();
|
||||
}
|
||||
return style;
|
||||
}
|
||||
|
||||
const txt::ParagraphStyle& ParagraphStyle::GetParagraphStyle() const {
|
||||
return style_;
|
||||
}
|
||||
|
||||
} // namespace impeller::interop
|
||||
|
||||
@@ -48,10 +48,12 @@ class ParagraphStyle final
|
||||
|
||||
txt::TextStyle CreateTextStyle() const;
|
||||
|
||||
const txt::ParagraphStyle& GetParagraphStyle() const;
|
||||
|
||||
private:
|
||||
txt::ParagraphStyle style_;
|
||||
ScopedObject<Paint> foreground_;
|
||||
ScopedObject<Paint> backgrond_;
|
||||
ScopedObject<Paint> background_;
|
||||
};
|
||||
|
||||
} // namespace impeller::interop
|
||||
|
||||
Reference in New Issue
Block a user