Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion impeller/toolkit/interop/impeller.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
93 changes: 93 additions & 0 deletions impeller/toolkit/interop/impeller_unittests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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
45 changes: 26 additions & 19 deletions impeller/toolkit/interop/paragraph_builder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,46 +10,53 @@

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;
}
txt_paragraph->Layout(width);
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
10 changes: 8 additions & 2 deletions impeller/toolkit/interop/paragraph_builder.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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
Expand Down
11 changes: 8 additions & 3 deletions impeller/toolkit/interop/paragraph_style.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 3 additions & 1 deletion impeller/toolkit/interop/paragraph_style.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down