Skip to content

Commit 9ed44be

Browse files
committed
harmony add
1 parent e8483f5 commit 9ed44be

30 files changed

+1016
-24
lines changed

App.tsx

Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
/**
2+
* Sample React Native App
3+
* https://github.com/facebook/react-native
4+
*
5+
* @format
6+
*/
7+
8+
9+
// useEffect:模拟声明周期
10+
import React, { useState, useEffect } from 'react';
11+
import type { PropsWithChildren } from 'react';
12+
import {
13+
ActivityIndicator,
14+
Dimensions,
15+
Platform,
16+
SafeAreaView,
17+
ScrollView,
18+
StatusBar,
19+
StyleSheet,
20+
Switch,
21+
Text,
22+
TextInput,
23+
useColorScheme,
24+
View,
25+
LayoutChangeEvent,
26+
} from 'react-native';
27+
28+
import {
29+
Colors,
30+
DebugInstructions,
31+
Header,
32+
LearnMoreLinks,
33+
ReloadInstructions,
34+
} from 'react-native/Libraries/NewAppScreen';
35+
36+
37+
import rnTextSize from 'react-native-text-size';
38+
import flatHeights from 'react-native-text-size';
39+
40+
import TextSize, {
41+
// typings
42+
TSFontInfo,
43+
TSFontForStyle,
44+
TSFontSpecs,
45+
TSFontStyle,
46+
TSFontVariant,
47+
TSFontWeight,
48+
TSMeasureResult,
49+
TSTextBreakStrategy,
50+
} from 'react-native-text-size';
51+
52+
function App(): React.JSX.Element {
53+
54+
type Props = { texts: string[] }
55+
type State = { heights: number[] }
56+
57+
type Props1 = {}
58+
type State2 = { width2: number, height2: number }
59+
60+
const [heights, setHeights] = useState<number[]>([]);
61+
62+
const [width2, setWidth2] = useState<number>();
63+
const [height2, setHeight2] = useState<number>();
64+
65+
const [texts, setTexts] = useState(['I ❤️ rnTextSize', 'I ❤️ rnTextSize using flatHeights', 'Thx for flatHeights', 'test123',])
66+
const [texts2, setTexts2] = useState('I ❤️ rnTextSize')
67+
const [res, setRes] = useState<string[]>([])
68+
const [key, setKey] = useState({})
69+
const [info, setInfo] = useState<TSFontInfo>()
70+
71+
72+
const fontSpecs: TSFontSpecs = {
73+
fontFamily: undefined,
74+
fontSize: 20,
75+
fontStyle: 'italic', //正斜
76+
fontWeight: 'bold',
77+
}
78+
const fontSpecs2: TSFontSpecs = {
79+
fontFamily: 'Android',
80+
textBreakStrategy: 'balanced',
81+
}
82+
83+
const fontSpecs3: TSFontSpecs = {
84+
fontFamily: undefined,
85+
fontSize: 20,
86+
fontStyle: 'normal', //正斜
87+
fontWeight: '700',
88+
}
89+
90+
useEffect(() => {
91+
const myFun = async () => {
92+
const resp = await rnTextSize.fontFamilyNames();
93+
setRes(resp)
94+
const keyp = await rnTextSize.specsForTextStyles();
95+
setKey(keyp)
96+
const infos = await rnTextSize.fontFromSpecs(fontSpecs2);
97+
setInfo(infos)
98+
99+
setTexts(texts)
100+
const width = Dimensions.get('window').width * 0.8
101+
const newHeights = await rnTextSize.flatHeights({
102+
text: texts, // array of texts to measure, can include symbols
103+
width, // max-width of the "virtual" container
104+
...fontSpecs, // RN font specification
105+
})
106+
setHeights(newHeights);
107+
108+
setTexts2(texts2)
109+
const width2 = Dimensions.get('window').width * 0.8
110+
const newHeight2 = await rnTextSize.measure({
111+
text: texts2, // array of texts to measure, can include symbols
112+
width: width2, // max-width of the "virtual" container
113+
...fontSpecs3, // RN font specification
114+
})
115+
setHeight2(newHeight2.height);
116+
setWidth2(newHeight2.width)
117+
}
118+
myFun()
119+
}, [])
120+
121+
return (
122+
<ScrollView style={{ flexGrow: 1 }}>
123+
<View style={{ paddingLeft: 12, paddingRight: 12 }}>
124+
<Text>
125+
------------------------------
126+
---------------
127+
measure接口:[fontFamily: undefined,fontSize: 20,fontStyle: 'normal', 正斜fontWeight: '700']
128+
</Text>
129+
<View>
130+
<Text style={{
131+
width: width2,
132+
height: height2,
133+
...fontSpecs3
134+
}}>
135+
{texts2}
136+
</Text>
137+
</View>
138+
<Text>
139+
----------------
140+
flatHeights接口:[fontFamily: undefined,fontSize: 20,fontStyle: 'italic', fontWeight: 'bold']
141+
</Text>
142+
{texts.map(
143+
(text, index) => (
144+
<Text style={{ height: heights[index], ...fontSpecs }}>
145+
{text}
146+
</Text>
147+
)
148+
)}
149+
<Text>
150+
----------------fontFamilyNames接口:获取系统默认配置的字体
151+
</Text>
152+
<Text>
153+
{res}
154+
</Text>
155+
156+
<Text>
157+
----------------specsForTextStyles接口:获取系统默认配置的字体的具体信息
158+
</Text>
159+
<Text>
160+
{JSON.stringify(key)}
161+
</Text>
162+
163+
164+
<Text>
165+
----------------fontFromSpecs:返回从给定规范中获得的字体特征
166+
</Text>
167+
<Text>
168+
{JSON.stringify(info)}
169+
</Text>
170+
</View>
171+
</ScrollView>
172+
)
173+
174+
}
175+
176+
export default App;

harmony/text_size.har

11.8 KB
Binary file not shown.

harmony/text_size/BuildProfile.ets

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export default class BuildProfile {
2+
static readonly BUNDLE_NAME = 'com.rnoh.tester';
3+
static readonly VERSION_CODE = 1000000;
4+
static readonly VERSION_NAME = '1.0.0';
5+
static readonly HAR_VERSION = '1.0.0';
6+
static readonly BUILD_MODE_NAME = 'Debug';
7+
static readonly DEBUG = true;
8+
}

harmony/text_size/Index.ets

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './ts'

harmony/text_size/build-profile.json5

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"apiType": "stageMode",
3+
"targets": [
4+
{
5+
"name": "default",
6+
"runtimeOS": "HarmonyOS"
7+
}
8+
]
9+
}

harmony/text_size/hvigorfile.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// Script for compiling build behavior. It is built in the build plug-in and cannot be modified currently.
2+
export { harTasks } from '@ohos/hvigor-ohos-plugin';
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Define project specific obfuscation rules here.
2+
# You can include the obfuscation configuration files in the current module's build-profile.json5.
3+
#
4+
# For more details, see
5+
# https://gitee.com/openharmony/arkcompiler_ets_frontend/blob/master/arkguard/README.md
6+
7+
# Obfuscation options:
8+
# -disable-obfuscation: disable all obfuscations
9+
# -enable-property-obfuscation: obfuscate the property names
10+
# -enable-toplevel-obfuscation: obfuscate the names in the global scope
11+
# -compact: remove unnecessary blank spaces and all line feeds
12+
# -remove-log: remove all console.* statements
13+
# -print-namecache: print the name cache that contains the mapping from the old names to new names
14+
# -apply-namecache: reuse the given cache file
15+
16+
# Keep options:
17+
# -keep-property-name: specifies property names that you want to keep
18+
# -keep-global-name: specifies names that you want to keep in the global scope

harmony/text_size/oh-package.json5

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"license": "MIT",
3+
"devDependencies": {
4+
},
5+
"author": "",
6+
"name": "rnoh-text-size",
7+
"description": "Please describe the basic information.",
8+
"main": "index.ets",
9+
"version": "1.0.0",
10+
"dependencies": {
11+
"rnoh": "file:../rnoh"
12+
}
13+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Copyright (c) Meta Platforms, Inc. and affiliates.
2+
#
3+
# This source code is licensed under the MIT license found in the
4+
# LICENSE file in the root directory of this source tree.
5+
6+
cmake_minimum_required(VERSION 3.13)
7+
set(CMAKE_VERBOSE_MAKEFILE on)
8+
9+
file(GLOB rnoh_text_size_SRC CONFIGURE_DEPENDS *.cpp)
10+
add_library(rnoh_text_size SHARED ${rnoh_text_size_SRC})
11+
target_include_directories(rnoh_text_size PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
12+
target_link_libraries(rnoh_text_size PUBLIC rnoh)
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/**
2+
* MIT License
3+
*
4+
* Copyright (C) 2024 Huawei Device Co., Ltd.
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
25+
# pragma once
26+
# include "RNOH/Package.h"
27+
# include "RNTextSizeTurboModule.h"
28+
29+
using namespace rnoh;
30+
using namespace facebook;
31+
32+
class RNTextSizeTurboModuleFactoryDelegate : public TurboModuleFactoryDelegate {
33+
public:
34+
SharedTurboModule createTurboModule(Context ctx, const std::string &name) const override
35+
{
36+
if (name == "RTNTextSize") {
37+
return std::make_shared<RNTextSizeTurboModule>(ctx, name);
38+
}
39+
return nullptr;
40+
};
41+
};
42+
namespace rnoh {
43+
class RNTextSizePackage : public Package {
44+
public:
45+
RNTextSizePackage(Package::Context ctx) : Package(ctx) {}
46+
std::unique_ptr<TurboModuleFactoryDelegate> createTurboModuleFactoryDelegate() override
47+
{
48+
return std::make_unique<RNTextSizeTurboModuleFactoryDelegate>();
49+
}
50+
};
51+
} // namespace rnoh
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/**
2+
* MIT License
3+
*
4+
* Copyright (C) 2023 Huawei Device Co., Ltd.
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
25+
#include "RNTextSizeTurbomodule.h"
26+
#include "RNOH/ArkTSTurboModule.h"
27+
28+
using namespace rnoh;
29+
using namespace facebook;
30+
31+
static jsi::Value __hostFunction_RNTextSizeTurboModule_measure(jsi::Runtime &rt,
32+
react::TurboModule &turboModule,
33+
const jsi::Value *args, size_t count)
34+
{
35+
return static_cast<ArkTSTurboModule &>(turboModule).callAsync(rt, "measure", args, count);
36+
}
37+
static jsi::Value __hostFunction_RNTextSizeTurboModule_flatHeights(jsi::Runtime &rt,
38+
react::TurboModule &turboModule,
39+
const jsi::Value *args, size_t count)
40+
{
41+
return static_cast<ArkTSTurboModule &>(turboModule).callAsync(rt, "flatHeights", args, count);
42+
}
43+
static jsi::Value __hostFunction_RNTextSizeTurboModule_specsForTextStyles(jsi::Runtime &rt,
44+
react::TurboModule &turboModule,
45+
const jsi::Value *args, size_t count)
46+
{
47+
return static_cast<ArkTSTurboModule &>(turboModule).callAsync(rt, "specsForTextStyles", args, count);
48+
}
49+
static jsi::Value __hostFunction_RNTextSizeTurboModule_fontFromSpecs(jsi::Runtime &rt,
50+
react::TurboModule &turboModule,
51+
const jsi::Value *args, size_t count)
52+
{
53+
return static_cast<ArkTSTurboModule &>(turboModule).callAsync(rt, "fontFromSpecs", args, count);
54+
}
55+
static jsi::Value __hostFunction_RNTextSizeTurboModule_fontFamilyNames(jsi::Runtime &rt,
56+
react::TurboModule &turboModule,
57+
const jsi::Value *args, size_t count)
58+
{
59+
return static_cast<ArkTSTurboModule &>(turboModule).callAsync(rt, "fontFamilyNames", args, count);
60+
}
61+
static jsi::Value __hostFunction_RNTextSizeTurboModule_fontNamesForFamilyName(jsi::Runtime &rt,
62+
react::TurboModule &turboModule,
63+
const jsi::Value *args, size_t count)
64+
{
65+
return static_cast<ArkTSTurboModule &>(turboModule).callAsync(rt, "fontNamesForFamilyName", args, count);
66+
}
67+
68+
69+
RNTextSizeTurboModule::RNTextSizeTurboModule(const ArkTSTurboModule::Context ctx, const std::string name)
70+
: ArkTSTurboModule(ctx, name)
71+
{
72+
methodMap_["measure"] = MethodMetadata{1, __hostFunction_RNTextSizeTurboModule_measure};
73+
methodMap_["flatHeights"] = MethodMetadata{1, __hostFunction_RNTextSizeTurboModule_flatHeights};
74+
methodMap_["specsForTextStyles"] = MethodMetadata{0, __hostFunction_RNTextSizeTurboModule_specsForTextStyles};
75+
methodMap_["fontFromSpecs"] = MethodMetadata{1, __hostFunction_RNTextSizeTurboModule_fontFromSpecs};
76+
methodMap_["fontFamilyNames"] = MethodMetadata{0, __hostFunction_RNTextSizeTurboModule_fontFamilyNames};
77+
methodMap_["fontNamesForFamilyName"] = MethodMetadata{1, __hostFunction_RNTextSizeTurboModule_fontNamesForFamilyName};
78+
}

0 commit comments

Comments
 (0)