Skip to content

Commit 534d2c8

Browse files
authored
refactor: JsiSkParagraph to use JsiSkWrappingSharedPtrHostObject (#3044)
1 parent b7743d8 commit 534d2c8

File tree

2 files changed

+21
-30
lines changed

2 files changed

+21
-30
lines changed

packages/skia/cpp/api/JsiSkParagraph.h

Lines changed: 17 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,19 @@
2424
namespace RNSkia {
2525

2626
namespace jsi = facebook::jsi;
27-
2827
namespace para = skia::textlayout;
2928

3029
/**
31-
Implementation of the Paragraph object in JSI
30+
* Implementation of the Paragraph object in JSI
3231
*/
33-
class JsiSkParagraph : public JsiSkHostObject {
32+
class JsiSkParagraph
33+
: public JsiSkWrappingSharedPtrHostObject<para::Paragraph> {
3434
public:
3535
EXPORT_JSI_API_TYPENAME(JsiSkParagraph, Paragraph)
3636

3737
JSI_HOST_FUNCTION(layout) {
3838
auto width = getArgumentAsNumber(runtime, arguments, count, 0);
39-
_paragraph->layout(width);
39+
getObject()->layout(width);
4040
return jsi::Value::undefined();
4141
}
4242

@@ -45,43 +45,43 @@ class JsiSkParagraph : public JsiSkHostObject {
4545
getArgumentAsHostObject<JsiSkCanvas>(runtime, arguments, count, 0);
4646
auto x = getArgumentAsNumber(runtime, arguments, count, 1);
4747
auto y = getArgumentAsNumber(runtime, arguments, count, 2);
48-
_paragraph->paint(jsiCanvas->getCanvas(), x, y);
48+
getObject()->paint(jsiCanvas->getCanvas(), x, y);
4949
return jsi::Value::undefined();
5050
}
5151

5252
JSI_HOST_FUNCTION(getHeight) {
53-
return static_cast<double>(_paragraph->getHeight());
53+
return static_cast<double>(getObject()->getHeight());
5454
}
5555

5656
JSI_HOST_FUNCTION(getMaxWidth) {
57-
return static_cast<double>(_paragraph->getMaxWidth());
57+
return static_cast<double>(getObject()->getMaxWidth());
5858
}
5959

6060
JSI_HOST_FUNCTION(getMaxIntrinsicWidth) {
61-
return static_cast<double>(_paragraph->getMaxIntrinsicWidth());
61+
return static_cast<double>(getObject()->getMaxIntrinsicWidth());
6262
}
6363

6464
JSI_HOST_FUNCTION(getMinIntrinsicWidth) {
65-
return static_cast<double>(_paragraph->getMinIntrinsicWidth());
65+
return static_cast<double>(getObject()->getMinIntrinsicWidth());
6666
}
6767

6868
JSI_HOST_FUNCTION(getLongestLine) {
69-
return static_cast<double>(_paragraph->getLongestLine());
69+
return static_cast<double>(getObject()->getLongestLine());
7070
}
7171

7272
JSI_HOST_FUNCTION(getGlyphPositionAtCoordinate) {
7373
auto dx = getArgumentAsNumber(runtime, arguments, count, 0);
7474
auto dy = getArgumentAsNumber(runtime, arguments, count, 1);
75-
auto result = _paragraph->getGlyphPositionAtCoordinate(dx, dy);
75+
auto result = getObject()->getGlyphPositionAtCoordinate(dx, dy);
7676
return result.position;
7777
}
7878

7979
JSI_HOST_FUNCTION(getRectsForRange) {
8080
auto start = getArgumentAsNumber(runtime, arguments, count, 0);
8181
auto end = getArgumentAsNumber(runtime, arguments, count, 1);
8282
auto result =
83-
_paragraph->getRectsForRange(start, end, para::RectHeightStyle::kTight,
84-
para::RectWidthStyle::kTight);
83+
getObject()->getRectsForRange(start, end, para::RectHeightStyle::kTight,
84+
para::RectWidthStyle::kTight);
8585
auto returnValue = jsi::Array(runtime, result.size());
8686
for (size_t i = 0; i < result.size(); ++i) {
8787
returnValue.setValueAtIndex(
@@ -93,7 +93,7 @@ class JsiSkParagraph : public JsiSkHostObject {
9393

9494
JSI_HOST_FUNCTION(getLineMetrics) {
9595
std::vector<para::LineMetrics> metrics;
96-
_paragraph->getLineMetrics(metrics);
96+
getObject()->getLineMetrics(metrics);
9797
auto returnValue = jsi::Array(runtime, metrics.size());
9898
auto height = 0;
9999
for (size_t i = 0; i < metrics.size(); ++i) {
@@ -110,7 +110,7 @@ class JsiSkParagraph : public JsiSkHostObject {
110110

111111
JSI_HOST_FUNCTION(getRectsForPlaceholders) {
112112
std::vector<para::TextBox> placeholderInfos =
113-
_paragraph->getRectsForPlaceholders();
113+
getObject()->getRectsForPlaceholders();
114114
auto returnValue = jsi::Array(runtime, placeholderInfos.size());
115115
for (size_t i = 0; i < placeholderInfos.size(); ++i) {
116116
auto obj = jsi::Object(runtime);
@@ -124,12 +124,6 @@ class JsiSkParagraph : public JsiSkHostObject {
124124
return returnValue;
125125
}
126126

127-
JSI_HOST_FUNCTION(dispose) {
128-
_paragraph = nullptr;
129-
130-
return jsi::Value::undefined();
131-
}
132-
133127
JSI_EXPORT_FUNCTIONS(JSI_EXPORT_FUNC(JsiSkParagraph, layout),
134128
JSI_EXPORT_FUNC(JsiSkParagraph, paint),
135129
JSI_EXPORT_FUNC(JsiSkParagraph, getMaxWidth),
@@ -146,12 +140,8 @@ class JsiSkParagraph : public JsiSkHostObject {
146140

147141
explicit JsiSkParagraph(std::shared_ptr<RNSkPlatformContext> context,
148142
para::ParagraphBuilder *paragraphBuilder)
149-
: JsiSkHostObject(std::move(context)) {
150-
_paragraph = paragraphBuilder->Build();
151-
}
152-
153-
public:
154-
std::unique_ptr<para::Paragraph> _paragraph;
143+
: JsiSkWrappingSharedPtrHostObject<para::Paragraph>(
144+
std::move(context), std::move(paragraphBuilder->Build())) {}
155145
};
156146

157147
} // namespace RNSkia

packages/skia/cpp/api/recorder/Drawings.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -874,9 +874,10 @@ class ParagraphCmd : public Command {
874874
}
875875

876876
void draw(DrawingCtx *ctx) {
877-
if (props.paragraph && props.paragraph->_paragraph) {
878-
props.paragraph->_paragraph->layout(props.width);
879-
props.paragraph->_paragraph->paint(ctx->canvas, props.x, props.y);
877+
if (props.paragraph) {
878+
auto paragraph = props.paragraph->getObject();
879+
paragraph->layout(props.width);
880+
paragraph->paint(ctx->canvas, props.x, props.y);
880881
}
881882
}
882883
};

0 commit comments

Comments
 (0)