Skip to content

Commit

Permalink
feat(ohos): optimize text measure
Browse files Browse the repository at this point in the history
  • Loading branch information
sohotz committed Jan 13, 2025
1 parent 1f20257 commit 7cda938
Show file tree
Hide file tree
Showing 43 changed files with 1,807 additions and 672 deletions.
21 changes: 11 additions & 10 deletions docs/development/native-integration.md
Original file line number Diff line number Diff line change
Expand Up @@ -290,21 +290,21 @@ Ohos Demo:源码依赖 Hippy。体验方法:DevEco 打开 hippy 项目根目

## 接入方式一:Har包快速接入

1. 创建一个 Ohos 工程
### 1. 创建一个 Ohos 工程

2. Har 包集成
### 2. Har 包集成

- 配置 oh-package.json5
- 配置 oh-package.json5

```json
"dependencies": {
"hippy": "1.3.0"
}
```

3. 初始化代码
### 3. 初始化代码

- 获取 libhippy.so 接口对象和 UIAbility context
- 获取 libhippy.so 接口对象和 UIAbility context

```TypeScript
import libHippy from 'libhippy.so'
Expand All @@ -314,15 +314,15 @@ Ohos Demo:源码依赖 Hippy。体验方法:DevEco 打开 hippy 项目根目

> 注:App 直接集成 Hippy,context 使用 UIAbility context;如果 App 在一个模块里集成 Hippy,js 等资源也集成在模块里,context 使用 getContext().createModuleContext("moduleName"),否则会找不到 js 等资源。

- 创建 HippyEngine、初始化 HippyEngine、加载业务 bundle
- 创建 HippyEngine、初始化 HippyEngine、加载业务 bundle

```TypeScript
this.hippyEngine = createHippyEngine(params)
this.hippyEngine.initEngine()
this.hippyEngine?.loadModule()
```

- 组装 HippyRoot 组件
- 组装 HippyRoot 组件

```TypeScript
HippyRoot({
Expand All @@ -340,11 +340,12 @@ Ohos Demo:源码依赖 Hippy。体验方法:DevEco 打开 hippy 项目根目

> 源码接入主要为了方便在 App 项目里直接调试 Hippy 代码(c++ 和 ets 代码)。

1. 创建一个 Ohos 工程
### 1. 创建一个 Ohos 工程

2. Hippy 代码集成
### 2. Hippy 代码集成

- 拉取 hippy 代码到项目里(比如:根目录下)

> https://github.com/sohotz/Hippy.git,分支:main

- 配置 oh-package.json5
Expand All @@ -355,7 +356,7 @@ Ohos Demo:源码依赖 Hippy。体验方法:DevEco 打开 hippy 项目根目
}
```

3. 初始化代码
### 3. 初始化代码

- 获取 libhippy.so 接口对象和 UIAbility context

Expand Down
4 changes: 2 additions & 2 deletions docs/development/native-module.md
Original file line number Diff line number Diff line change
Expand Up @@ -386,8 +386,8 @@ export class ExampleNativeTurboModule extends HippyNativeModuleBase {
}

public getTurboConfig(): TurboConfig {
return new TurboConfig();
}
return new TurboConfig();
}

public printTurboConfig(turboConfig: TurboConfig): string {
return turboConfig.info;
Expand Down
4 changes: 2 additions & 2 deletions docs/feature/feature2.0/jsi.md
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,8 @@ export class ExampleNativeTurboModule extends HippyNativeModuleBase {
}

public getTurboConfig(): TurboConfig {
return new TurboConfig();
}
return new TurboConfig();
}

public printTurboConfig(turboConfig: TurboConfig): string {
return turboConfig.info;
Expand Down
2 changes: 2 additions & 0 deletions dom/include/dom/root_node.h
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ class RootNode : public DomNode {
return persistent_map_;
}

std::vector<std::weak_ptr<DomNode>> GetAllTextNodes();

private:
static void MarkLayoutNodeDirty(const std::vector<std::shared_ptr<DomNode>>& nodes);

Expand Down
11 changes: 11 additions & 0 deletions dom/src/dom/root_node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -577,5 +577,16 @@ void RootNode::Traverse(const std::function<void(const std::shared_ptr<DomNode>&
}
}

std::vector<std::weak_ptr<DomNode>> RootNode::GetAllTextNodes() {
std::vector<std::weak_ptr<DomNode>> textNodes;
for (auto it = nodes_.begin(); it != nodes_.end(); it++) {
auto node = it->second.lock();
if (node && node->GetViewName() == "Text") {
textNodes.emplace_back(node);
}
}
return textNodes;
}

} // namespace dom
} // namespace hippy
12 changes: 12 additions & 0 deletions framework/examples/ohos-demo/src/main/ets/pages/PageManagement.ets
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,18 @@ struct PageManagement {
return true
}

onPageShow(): void {
for (let params of this.pageitems) {
params.hippyEngine?.onEngineResume()
}
}

onPageHide(): void {
for (let params of this.pageitems) {
params.hippyEngine?.onEnginePause()
}
}

build() {
Column() {
Row() {
Expand Down
8 changes: 8 additions & 0 deletions framework/examples/ohos-har-demo/src/main/ets/pages/Index.ets
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,14 @@ struct Index {
})
}

onPageShow(): void {
this.hippyEngine?.onEngineResume()
}

onPageHide(): void {
this.hippyEngine?.onEnginePause()
}

build() {
Column() {
HippyRoot({
Expand Down
11 changes: 11 additions & 0 deletions framework/ohos/src/main/cpp/impl/connector/src/setting_napi.cc
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,16 @@ static napi_value SetFlags(napi_env env, napi_callback_info info) {
return arkTs.GetUndefined();
}

static napi_value SetDensity(napi_env env, napi_callback_info info) {
ArkTS arkTs(env);
auto args = arkTs.GetCallbackArgs(info);
float density = (float)arkTs.GetDouble(args[0]);

HRPixelUtils::SetDensity(density);

return arkTs.GetUndefined();
}

static napi_value SetDensityScale(napi_env env, napi_callback_info info) {
ArkTS arkTs(env);
auto args = arkTs.GetCallbackArgs(info);
Expand All @@ -64,6 +74,7 @@ static napi_value SetFontSizeScale(napi_env env, napi_callback_info info) {
}

REGISTER_OH_NAPI("Setting", "Setting_SetFlags", SetFlags)
REGISTER_OH_NAPI("Setting", "Setting_SetDensity", SetDensity)
REGISTER_OH_NAPI("Setting", "Setting_SetDensityScale", SetDensityScale)
REGISTER_OH_NAPI("Setting", "Setting_SetFontSizeScale", SetFontSizeScale)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ set(CMAKE_CXX_STANDARD 17)
add_library(${PROJECT_NAME} STATIC)
target_include_directories(${PROJECT_NAME} PUBLIC include)
target_compile_options(${PROJECT_NAME} PRIVATE ${COMPILE_OPTIONS})
target_compile_definitions(${PROJECT_NAME} PUBLIC "OHOS_DRAW_TEXT")
#target_compile_definitions(${PROJECT_NAME} INTERFACE "OHOS_NATIVE_RENDER")
# endregion

Expand Down Expand Up @@ -121,6 +122,9 @@ set(SOURCE_SET
src/components/waterfall_item_view.cc
src/components/waterfall_view.cc
src/recycle/recycle_view.cc
src/text_measure/font_collection_manager.cc
src/text_measure/text_measurer.cc
src/text_measure/text_measure_manager.cc
)
set(PUBLIC_SOURCE_SET
src/native_render_provider_capi.cc
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ inline namespace native {
class ArkUINodeDelegate {
public:
virtual ~ArkUINodeDelegate() = default;
virtual void OnClick() {}
virtual void OnClick(const HRPosition &position) {}
virtual void OnTouch(int32_t actionType, const HRPosition &screenPosition) {}
virtual void OnAppear() {}
virtual void OnDisappear() {}
Expand Down Expand Up @@ -71,6 +71,8 @@ class ArkUINode {
void InsertChild(ArkUINode *child, int32_t index);
void RemoveChild(ArkUINode *child);
void RemoveSelfFromParent();
void ReplaceSelfFromParent(ArkUINode *newNode);
bool HasParent();

void SetDefaultAttributes();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#pragma once

#include "renderer/arkui/arkui_node.h"
#include <arkui/styled_string.h>

namespace hippy {
inline namespace render {
Expand All @@ -31,7 +32,8 @@ inline namespace native {
class TextNode : public ArkUINode {
private:
enum class AttributeFlag {
TEXT_CONTENT = 0,
TEXT_CONTENT_WITH_STYLED_STRING = 0,
TEXT_CONTENT,
FONT_COLOR,
FONT_SIZE,
FONT_STYLE,
Expand All @@ -58,6 +60,7 @@ class TextNode : public ArkUINode {
TextNode();
~TextNode() override;

TextNode &SetTextContentWithStyledString(const ArkUI_StyledString *styledString);
TextNode &SetTextContent(const std::string &text);
TextNode &SetFontColor(uint32_t fontColor);
TextNode &ResetFontColor();
Expand All @@ -84,7 +87,15 @@ class TextNode : public ArkUINode {
TextNode &SetTextHeightAdaptivePolicy(int32_t policyType);
TextNode &SetTextIndent(float textIndent);

void ResetTextContentWithStyledStringAttribute();
void ResetAllAttributes() override;

bool HasStyledString() {
return hasStyledString_;
}

private:
bool hasStyledString_ = false;
};

} // namespace native
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ class BaseView : public ArkUINodeDelegate, public std::enable_shared_from_this<B
void AddSubRenderView(std::shared_ptr<BaseView> &subView, int32_t index);
void RemoveSubView(std::shared_ptr<BaseView> &subView);
void RemoveFromParentView();

void SetRenderViewFrame(const HRRect &frame, const HRPadding &padding = HRPadding(0, 0, 0, 0));
void UpdateEventListener(HippyValueObjectType &newEvents);
bool CheckRegisteredEvent(std::string &eventName);
Expand All @@ -91,7 +92,7 @@ class BaseView : public ArkUINodeDelegate, public std::enable_shared_from_this<B

void SetPosition(const HRPosition &position);

virtual void OnClick() override;
virtual void OnClick(const HRPosition &position) override;
virtual void OnTouch(int32_t actionType, const HRPosition &screenPosition) override;
virtual void OnAppear() override;
virtual void OnDisappear() override;
Expand All @@ -105,6 +106,7 @@ class BaseView : public ArkUINodeDelegate, public std::enable_shared_from_this<B
virtual void OnChildReusedImpl(std::shared_ptr<BaseView> const &childView, int index) {}
void UpdateRenderViewFrame(const HRRect &frame, const HRPadding &padding);
virtual void UpdateRenderViewFrameImpl(const HRRect &frame, const HRPadding &padding);
virtual bool IsValidFrame(const HRRect &frame) { return true; }
virtual bool HandleGestureBySelf() { return false; }

protected:
Expand All @@ -114,7 +116,7 @@ class BaseView : public ArkUINodeDelegate, public std::enable_shared_from_this<B
bool SetShadowProp(const std::string &propKey, const HippyValue &propValue);
bool SetEventProp(const std::string &propKey, const HippyValue &propValue);

void SetClickable(bool flag);
virtual void SetClickable(bool flag);
void SetLongClickable(bool flag);
void SetPressIn(bool flag);
void SetPressOut(bool flag);
Expand All @@ -128,6 +130,7 @@ class BaseView : public ArkUINodeDelegate, public std::enable_shared_from_this<B
void SetDetachedFromWindowHandle(bool flag);

void UpdateLazyProps();
void UpdateLazyAll();

void HandleInterceptPullUp();
int64_t GetTimeMilliSeconds();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class RichTextImageSpanView : public BaseView {
bool ReuseArkUINodeImpl(std::shared_ptr<RecycleView> &recycleView) override;
bool SetPropImpl(const std::string &propKey, const HippyValue &propValue) override;
void UpdateRenderViewFrameImpl(const HRRect &frame, const HRPadding &padding) override;
bool IsValidFrame(const HRRect &frame) override;

private:
void FetchAltImage(const std::string &imageUrl);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ class RichTextSpanView : public BaseView {
private:
void ClearProps();

virtual void SetClickable(bool flag) override;

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunused-private-field"

std::shared_ptr<SpanNode> spanNode_;

std::optional<std::string> text_;
Expand All @@ -67,6 +72,8 @@ class RichTextSpanView : public BaseView {

bool toSetTextDecoration_ = false;
bool toSetTextShadow = false;

#pragma clang diagnostic pop
};

} // namespace native
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

#pragma once

#include "renderer/arkui/stack_node.h"
#include "renderer/components/base_view.h"
#include "renderer/arkui/text_node.h"
#include <optional>
Expand All @@ -35,7 +36,7 @@ class RichTextView : public BaseView {
RichTextView(std::shared_ptr<NativeRenderContext> &ctx);
~RichTextView();

TextNode *GetLocalRootArkUINode() override;
ArkUINode *GetLocalRootArkUINode() override;
void CreateArkUINodeImpl() override;
void DestroyArkUINodeImpl() override;
bool RecycleArkUINodeImpl(std::shared_ptr<RecycleView> &recycleView) override;
Expand All @@ -48,11 +49,35 @@ class RichTextView : public BaseView {
void OnChildRemovedImpl(std::shared_ptr<BaseView> const &childView, int32_t index) override;

void SendTextEllipsizedEvent();

#ifdef OHOS_DRAW_TEXT
void RegisterSpanClickEvent(const std::shared_ptr<BaseView> spanView);
void UnregisterSpanClickEvent(const std::shared_ptr<BaseView> spanView);
#endif

private:
void ClearProps();

std::shared_ptr<TextNode> textNode_;
#ifdef OHOS_DRAW_TEXT
void UpdateDrawTextContent();
virtual void SetClickable(bool flag) override;
virtual void OnClick(const HRPosition &position) override;
std::shared_ptr<BaseView> GetTextSpanView(int spanIndex);
#endif

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunused-private-field"

#ifdef OHOS_DRAW_TEXT
float drawTextWidth_ = 0;
std::shared_ptr<TextMeasurer> oldUsedTextMeasurerHolder_ = nullptr;
// 问题:绘制包含ImageSpan的Text组件时,ImageSpan可以作为child加到Text上,但是ImageSpan的x和y不生效。
// 解决方法:套了一层容器组件,用来解决ImageSpan位置不生效的问题。
std::shared_ptr<StackNode> containerNode_ = nullptr;
std::set<std::shared_ptr<BaseView>> clickableSpanViews_;
#endif

std::shared_ptr<TextNode> textNode_ = nullptr;

std::optional<std::string> text_;
std::optional<uint32_t> color_;
Expand All @@ -79,6 +104,8 @@ class RichTextView : public BaseView {

bool isListenEllipsized_ = false;
bool toSendEllipsizedEvent_ = false;

#pragma clang diagnostic pop
};

} // namespace native
Expand Down
Loading

0 comments on commit 7cda938

Please sign in to comment.