!16989 字体引擎相关代码优化

Merge pull request !16989 from zhuxu/11_11
This commit is contained in:
openharmony_ci 2024-11-11 08:30:42 +00:00 committed by Gitee
commit 9151ba7bca
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
32 changed files with 316 additions and 252 deletions

View File

@ -13,12 +13,12 @@
* limitations under the License.
*/
#include "text_enum_napi.h"
#include <map>
#include <vector>
#include "font_parser.h"
#include "text_enum_napi.h"
#include "utils/log.h"
#include "utils/text_log.h"
namespace OHOS::Rosen {
struct JsEnumInt {
@ -26,7 +26,7 @@ struct JsEnumInt {
size_t enumInt;
};
static const std::vector<struct JsEnumInt> g_textAlign = {
const std::vector<struct JsEnumInt> TEXT_ALIGN = {
{ "LEFT", static_cast<size_t>(TextAlign::LEFT) },
{ "RIGHT", static_cast<size_t>(TextAlign::RIGHT) },
{ "CENTER", static_cast<size_t>(TextAlign::CENTER) },
@ -35,7 +35,7 @@ static const std::vector<struct JsEnumInt> g_textAlign = {
{ "END", static_cast<size_t>(TextAlign::END) },
};
static const std::vector<struct JsEnumInt> g_textDecorationStyle = {
const std::vector<struct JsEnumInt> TEXT_DECORATION_STYLE = {
{ "SOLID", static_cast<size_t>(TextDecorationStyle::SOLID) },
{ "DOUBLE", static_cast<size_t>(TextDecorationStyle::DOUBLE) },
{ "DOTTED", static_cast<size_t>(TextDecorationStyle::DOTTED) },
@ -43,7 +43,7 @@ static const std::vector<struct JsEnumInt> g_textDecorationStyle = {
{ "WAVY", static_cast<size_t>(TextDecorationStyle::WAVY) },
};
static const std::vector<struct JsEnumInt> g_fontWeight = {
const std::vector<struct JsEnumInt> FONT_WEIGHT = {
{ "W100", static_cast<size_t>(FontWeight::W100) },
{ "W200", static_cast<size_t>(FontWeight::W200) },
{ "W300", static_cast<size_t>(FontWeight::W300) },
@ -55,48 +55,48 @@ static const std::vector<struct JsEnumInt> g_fontWeight = {
{ "W900", static_cast<size_t>(FontWeight::W900) },
};
static const std::vector<struct JsEnumInt> g_fontStyle = {
const std::vector<struct JsEnumInt> FONT_STYLE = {
{ "NORMAL", static_cast<size_t>(FontStyle::NORMAL) },
{ "ITALIC", static_cast<size_t>(FontStyle::ITALIC) },
{ "OBLIQUE", static_cast<size_t>(FontStyle::OBLIQUE) },
};
static const std::vector<struct JsEnumInt> g_textBaseline = {
const std::vector<struct JsEnumInt> TEXT_BASE_LINE = {
{ "ALPHABETIC", static_cast<size_t>(TextBaseline::ALPHABETIC) },
{ "IDEOGRAPHIC", static_cast<size_t>(TextBaseline::IDEOGRAPHIC) },
};
static const std::vector<struct JsEnumInt> g_textDirection = {
const std::vector<struct JsEnumInt> TEXT_DIRECTION = {
{ "RTL", static_cast<size_t>(TextDirection::RTL) },
{ "LTR", static_cast<size_t>(TextDirection::LTR) },
};
static const std::vector<struct JsEnumInt> g_wordBreakType = {
const std::vector<struct JsEnumInt> WORD_BREAK_TYPE = {
{ "NORMAL", static_cast<size_t>(WordBreakType::NORMAL) },
{ "BREAK_ALL", static_cast<size_t>(WordBreakType::BREAK_ALL) },
{ "BREAK_WORD", static_cast<size_t>(WordBreakType::BREAK_WORD) },
};
static const std::vector<struct JsEnumInt> g_breakStrategy = {
const std::vector<struct JsEnumInt> BREAK_STRATEGY = {
{ "GREEDY", static_cast<size_t>(BreakStrategy::GREEDY) },
{ "HIGH_QUALITY", static_cast<size_t>(BreakStrategy::HIGH_QUALITY) },
{ "BALANCED", static_cast<size_t>(BreakStrategy::BALANCED) },
};
static const std::vector<struct JsEnumInt> g_ellipsisModal = {
const std::vector<struct JsEnumInt> ELLIPSIS_MODAL = {
{ "START", static_cast<size_t>(EllipsisModal::HEAD) },
{ "MIDDLE", static_cast<size_t>(EllipsisModal::MIDDLE) },
{ "END", static_cast<size_t>(EllipsisModal::TAIL) },
};
static const std::vector<struct JsEnumInt> g_textDecoration = {
const std::vector<struct JsEnumInt> TEXT_DECORATION = {
{ "NONE", static_cast<size_t>(TextDecoration::NONE) },
{ "UNDERLINE", static_cast<size_t>(TextDecoration::UNDERLINE) },
{ "OVERLINE", static_cast<size_t>(TextDecoration::OVERLINE) },
{ "LINE_THROUGH", static_cast<size_t>(TextDecoration::LINE_THROUGH) },
};
static const std::vector<struct JsEnumInt> g_placeholderVerticalAlignment = {
const std::vector<struct JsEnumInt> PLACEHOLDER_ALIGNMENT = {
{ "OFFSET_AT_BASELINE", static_cast<size_t>(PlaceholderVerticalAlignment::OFFSET_AT_BASELINE) },
{ "ABOVE_BASELINE", static_cast<size_t>(PlaceholderVerticalAlignment::ABOVE_BASELINE) },
{ "BELOW_BASELINE", static_cast<size_t>(PlaceholderVerticalAlignment::BELOW_BASELINE) },
@ -105,12 +105,12 @@ static const std::vector<struct JsEnumInt> g_placeholderVerticalAlignment = {
{ "CENTER_OF_ROW_BOX", static_cast<size_t>(PlaceholderVerticalAlignment::CENTER_OF_ROW_BOX) },
};
static const std::vector<struct JsEnumInt> g_rectWidthStyle = {
const std::vector<struct JsEnumInt> RECT_WIDTH_STYLE = {
{ "TIGHT", static_cast<size_t>(TextRectWidthStyle::TIGHT) },
{ "MAX", static_cast<size_t>(TextRectWidthStyle::MAX) },
};
static const std::vector<struct JsEnumInt> g_rectHeightStyle = {
const std::vector<struct JsEnumInt> RECT_HEIGHT_STYLE = {
{ "TIGHT", static_cast<size_t>(TextRectHeightStyle::TIGHT) },
{ "MAX", static_cast<size_t>(TextRectHeightStyle::COVER_TOP_AND_BOTTOM) },
{ "INCLUDE_LINE_SPACE_MIDDLE", static_cast<size_t>(TextRectHeightStyle::COVER_HALF_TOP_AND_BOTTOM) },
@ -119,12 +119,12 @@ static const std::vector<struct JsEnumInt> g_rectHeightStyle = {
{ "STRUT", static_cast<size_t>(TextRectHeightStyle::FOLLOW_BY_STRUT) },
};
static const std::vector<struct JsEnumInt> g_affinity = {
static const std::vector<struct JsEnumInt> AFFINITY = {
{ "UPSTREAM", static_cast<size_t>(Affinity::PREV) },
{ "DOWNSTREAM", static_cast<size_t>(Affinity::NEXT) },
};
static const std::vector<struct JsEnumInt> g_fontWidth = {
static const std::vector<struct JsEnumInt> FONTWIDTH = {
{ "ULTRA_CONDENSED", static_cast<size_t>(FontWidth::ULTRA_CONDENSED) },
{ "EXTRA_CONDENSED", static_cast<size_t>(FontWidth::EXTRA_CONDENSED) },
{ "CONDENSED", static_cast<size_t>(FontWidth::CONDENSED) },
@ -136,7 +136,7 @@ static const std::vector<struct JsEnumInt> g_fontWidth = {
{ "ULTRA_EXPANDED", static_cast<size_t>(FontWidth::ULTRA_EXPANDED) },
};
static const std::vector<struct JsEnumInt> g_textHeightBehavior = {
static const std::vector<struct JsEnumInt> TEXTHEIGHTBEHAVIOR = {
{ "ALL", static_cast<size_t>(TextHeightBehavior::ALL) },
{ "DISABLE_FIRST_ASCENT", static_cast<size_t>(TextHeightBehavior::DISABLE_FIRST_ASCENT) },
{ "DISABLE_LAST_ASCENT", static_cast<size_t>(TextHeightBehavior::DISABLE_LAST_ASCENT) },
@ -150,29 +150,29 @@ static const std::vector<struct JsEnumInt> g_systemFontType = {
{ "INSTALLED", static_cast<size_t>(TextEngine::FontParser::SystemFontType::INSTALLED) },
};
static const std::map<std::string_view, const std::vector<struct JsEnumInt>&> g_intEnumClassMap = {
{ "TextAlign", g_textAlign },
{ "TextDecorationStyle", g_textDecorationStyle },
{ "FontWeight", g_fontWeight },
{ "FontStyle", g_fontStyle },
{ "TextBaseline", g_textBaseline },
{ "TextDirection", g_textDirection },
{ "WordBreak", g_wordBreakType },
{ "BreakStrategy", g_breakStrategy },
{ "EllipsisMode", g_ellipsisModal },
{ "TextDecorationType", g_textDecoration },
{ "PlaceholderAlignment", g_placeholderVerticalAlignment },
{ "RectWidthStyle", g_rectWidthStyle },
{ "RectHeightStyle", g_rectHeightStyle },
{ "Affinity", g_affinity },
{ "FontWidth", g_fontWidth },
{ "TextHeightBehavior", g_textHeightBehavior },
const std::map<std::string_view, const std::vector<struct JsEnumInt>&> INT_ENUM_CLASS_MAP = {
{ "TextAlign", TEXT_ALIGN },
{ "TextDecorationStyle", TEXT_DECORATION_STYLE },
{ "FontWeight", FONT_WEIGHT },
{ "FontStyle", FONT_STYLE },
{ "TextBaseline", TEXT_BASE_LINE },
{ "TextDirection", TEXT_DIRECTION },
{ "WordBreak", WORD_BREAK_TYPE },
{ "BreakStrategy", BREAK_STRATEGY },
{ "EllipsisMode", ELLIPSIS_MODAL },
{ "TextDecorationType", TEXT_DECORATION },
{ "PlaceholderAlignment", PLACEHOLDER_ALIGNMENT },
{ "RectWidthStyle", RECT_WIDTH_STYLE },
{ "RectHeightStyle", RECT_HEIGHT_STYLE },
{ "Affinity", AFFINITY },
{ "FontWidth", FONTWIDTH },
{ "TextHeightBehavior", TEXTHEIGHTBEHAVIOR },
{ "SystemFontType", g_systemFontType },
};
napi_value JsEnum::JsEnumIntInit(napi_env env, napi_value exports)
{
for (auto it = g_intEnumClassMap.begin(); it != g_intEnumClassMap.end(); it++) {
for (auto it = INT_ENUM_CLASS_MAP.begin(); it != INT_ENUM_CLASS_MAP.end(); it++) {
auto &enumClassName = it->first;
auto &enumItemVec = it->second;
auto vecSize = enumItemVec.size();
@ -199,13 +199,13 @@ napi_value JsEnum::JsEnumIntInit(napi_env env, napi_value exports)
napi_status napiStatus = napi_define_class(env, enumClassName.data(), NAPI_AUTO_LENGTH, napiConstructor,
nullptr, property.size(), property.data(), &result);
if (napiStatus != napi_ok) {
LOGE("napi_define_class falied");
TEXT_LOGE("napi_define_class falied");
return nullptr;
}
napiStatus = napi_set_named_property(env, exports, enumClassName.data(), result);
if (napiStatus != napi_ok) {
LOGE("napi_set_named_property falied");
TEXT_LOGE("napi_set_named_property falied");
return nullptr;
}
}

View File

@ -18,6 +18,7 @@
#include "ability.h"
#include "napi_async_work.h"
#include "utils/text_log.h"
namespace OHOS::Rosen {
namespace {
@ -52,6 +53,7 @@ bool ParseContextFilePath(napi_env env, napi_value* argv, sptr<FontArgumentsConc
}
thread_local napi_ref JsFontCollection::constructor_ = nullptr;
napi_value JsFontCollection::Constructor(napi_env env, napi_callback_info info)
{
size_t argCount = 0;
@ -63,6 +65,10 @@ napi_value JsFontCollection::Constructor(napi_env env, napi_callback_info info)
}
JsFontCollection* jsFontCollection = new(std::nothrow) JsFontCollection();
if (jsFontCollection == nullptr) {
TEXT_LOGE("Failed to new font collection");
return nullptr;
}
status = napi_wrap(env, jsThis, jsFontCollection,
JsFontCollection::Destructor, nullptr, nullptr);
if (status != napi_ok) {
@ -386,6 +392,10 @@ napi_value JsFontCollection::OnLoadFont(napi_env env, napi_callback_info info)
TEXT_LOGE("Failed to get argument, argc %{public}zu", argc);
return nullptr;
}
if (argv[0] == nullptr) {
TEXT_LOGE("Argv[0] is invalid");
return nullptr;
}
std::string familyName;
std::string familySrc;
if (!ConvertFromJsValue(env, argv[0], familyName)) {
@ -393,6 +403,10 @@ napi_value JsFontCollection::OnLoadFont(napi_env env, napi_callback_info info)
return nullptr;
}
napi_valuetype valueType = napi_undefined;
if (argv[1] == nullptr) {
TEXT_LOGE("JsFontCollection::OnLoadFont Argv[1] is invalid");
return nullptr;
}
napi_typeof(env, argv[1], &valueType);
if (valueType != napi_object) {
if (!ConvertFromJsValue(env, argv[1], familySrc)) {

View File

@ -12,8 +12,9 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "fontcollection_napi/js_fontcollection.h"
#include "js_paragraph_builder.h"
#include "fontcollection_napi/js_fontcollection.h"
#include "line_typeset_napi/js_line_typeset.h"
#include "napi_common.h"
#include "paragraph_napi/js_paragraph.h"
@ -21,9 +22,12 @@
#include "utils/text_log.h"
namespace OHOS::Rosen {
std::unique_ptr<Typography> drawingTypography;
thread_local napi_ref JsParagraphBuilder::constructor_ = nullptr;
namespace {
const std::string CLASS_NAME = "ParagraphBuilder";
}
thread_local napi_ref JsParagraphBuilder::constructor_ = nullptr;
napi_value JsParagraphBuilder::Constructor(napi_env env, napi_callback_info info)
{
size_t argCount = ARGC_TWO;

View File

@ -16,9 +16,10 @@
#ifndef OHOS_ROSEN_JS_PARAGRAPH_BUILDER_H
#define OHOS_ROSEN_JS_PARAGRAPH_BUILDER_H
#include "font_collection.h"
#include <native_engine/native_engine.h>
#include <native_engine/native_value.h>
#include "font_collection.h"
#include "text_style.h"
#include "typography_create.h"
@ -50,7 +51,6 @@ private:
napi_value OnAppendSymbol(napi_env env, napi_callback_info info);
static thread_local napi_ref constructor_;
std::unique_ptr<TypographyCreate> typographyCreate_ = nullptr;
std::shared_ptr<FontCollection> fontCollection_ = nullptr;
};
} // namespace OHOS::Rosen
#endif // OHOS_ROSEN_JS_PARAGRAPH_BUILDER_H

View File

@ -26,9 +26,12 @@
#include "utils/text_log.h"
namespace OHOS::Rosen {
std::unique_ptr<Typography> g_Typography = nullptr;
thread_local napi_ref JsParagraph::constructor_ = nullptr;
namespace {
const std::string CLASS_NAME = "Paragraph";
}
std::unique_ptr<Typography> JsParagraph::g_Typography = nullptr;
thread_local napi_ref JsParagraph::constructor_ = nullptr;
napi_value JsParagraph::Constructor(napi_env env, napi_callback_info info)
{
@ -41,7 +44,7 @@ napi_value JsParagraph::Constructor(napi_env env, napi_callback_info info)
}
if (!g_Typography) {
TEXT_LOGE("JsParagraph::Constructor g_Typography is nullptr");
TEXT_LOGE("Typography is null");
return nullptr;
}
@ -142,10 +145,11 @@ napi_value JsParagraph::OnLayout(napi_env env, napi_callback_info info)
return NapiThrowError(env, TextErrorCode::ERROR_INVALID_PARAM, "Invalid params.");
}
double width = 0.0;
if (!(ConvertFromJsValue(env, argv[0], width))) {
if (!(argv[0] != nullptr && ConvertFromJsValue(env, argv[0], width))) {
TEXT_LOGE("Failed to convert");
return NapiGetUndefined(env);
}
paragraph_->Layout(width);
return NapiGetUndefined(env);
}
@ -169,12 +173,17 @@ napi_value JsParagraph::OnPaint(napi_env env, napi_callback_info info)
TEXT_LOGE("Failed to get parameter, argc %{public}zu, ret %{public}d", argc, status);
return NapiThrowError(env, TextErrorCode::ERROR_INVALID_PARAM, "Invalid params.");
}
if (argv[0] == nullptr) {
TEXT_LOGE("Argv is invalid");
return NapiGetUndefined(env);
}
Drawing::JsCanvas* jsCanvas = nullptr;
double x = 0.0;
double y = 0.0;
napi_unwrap(env, argv[0], reinterpret_cast<void **>(&jsCanvas));
if (!jsCanvas || !jsCanvas->GetCanvas() ||
!(ConvertFromJsValue(env, argv[ARGC_ONE], x) && ConvertFromJsValue(env, argv[ARGC_TWO], y))) {
!(argv[ARGC_ONE] != nullptr && ConvertFromJsValue(env, argv[ARGC_ONE], x) &&
argv[ARGC_TWO] != nullptr && ConvertFromJsValue(env, argv[ARGC_TWO], y))) {
TEXT_LOGE("Failed to get paint parameter");
return NapiGetUndefined(env);
}
@ -376,11 +385,20 @@ napi_value JsParagraph::OnGetRectsForRange(napi_env env, napi_callback_info info
TextRectWidthStyle wstyle;
TextRectHeightStyle hstyle;
napi_get_named_property(env, argv[0], "start", &tempValue);
if (tempValue == nullptr) {
TEXT_LOGE("Get start property failed");
return NapiGetUndefined(env);
}
bool isStartOk = ConvertFromJsValue(env, tempValue, start);
tempValue = nullptr;
napi_get_named_property(env, argv[0], "end", &tempValue);
if (tempValue == nullptr) {
TEXT_LOGE("Get end property failed");
return NapiGetUndefined(env);
}
bool isEndOk = ConvertFromJsValue(env, tempValue, end);
if (!(isStartOk && isEndOk && ConvertFromJsValue(env, argv[ARGC_ONE], wstyle) &&
ConvertFromJsValue(env, argv[ARGC_TWO], hstyle))) {
if (!(isStartOk && isEndOk && argv[ARGC_ONE] != nullptr && ConvertFromJsValue(env, argv[ARGC_ONE], wstyle) &&
argv[ARGC_TWO] != nullptr && ConvertFromJsValue(env, argv[ARGC_TWO], hstyle))) {
TEXT_LOGE("Failed to convert, start ok:%{public}d, end ok:%{public}d", isStartOk, isEndOk);
return NapiGetUndefined(env);
}
@ -413,7 +431,9 @@ napi_value JsParagraph::OnGetRectsForPlaceholders(napi_env env, napi_callback_in
int num = static_cast<int>(rectsForPlaceholders.size());
for (int index = 0; index < num; ++index) {
napi_value tempValue = CreateTextRectJsValue(env, rectsForPlaceholders[index]);
NAPI_CALL(env, napi_set_element(env, returnPlaceholders, index, tempValue));
if (tempValue != nullptr) {
NAPI_CALL(env, napi_set_element(env, returnPlaceholders, index, tempValue));
}
}
return returnPlaceholders;
}
@ -439,7 +459,8 @@ napi_value JsParagraph::OnGetGlyphPositionAtCoordinate(napi_env env, napi_callba
}
double dx = 0.0;
double dy = 0.0;
if (!(ConvertFromJsValue(env, argv[0], dx) && ConvertFromJsValue(env, argv[1], dy))) {
if (!(argv[0] != nullptr && ConvertFromJsValue(env, argv[0], dx) && argv[1] != nullptr &&
ConvertFromJsValue(env, argv[1], dy))) {
TEXT_LOGE("Failed to convert");
return NapiGetUndefined(env);
}
@ -467,7 +488,7 @@ napi_value JsParagraph::OnGetWordBoundary(napi_env env, napi_callback_info info)
return NapiThrowError(env, TextErrorCode::ERROR_INVALID_PARAM, "Invalid params.");
}
size_t offset = 0;
if (!(ConvertFromJsValue(env, argv[0], offset))) {
if (!(argv[0] != nullptr && ConvertFromJsValue(env, argv[0], offset))) {
TEXT_LOGE("Failed to convert");
return NapiGetUndefined(env);
}
@ -511,7 +532,7 @@ napi_value JsParagraph::OnGetLineHeight(napi_env env, napi_callback_info info)
return NapiThrowError(env, TextErrorCode::ERROR_INVALID_PARAM, "Invalid params.");
}
int lineNumber = 0;
if (!(ConvertFromJsValue(env, argv[0], lineNumber))) {
if (!(argv[0] != nullptr && ConvertFromJsValue(env, argv[0], lineNumber))) {
TEXT_LOGE("Failed to convert");
return NapiGetUndefined(env);
}
@ -539,7 +560,7 @@ napi_value JsParagraph::OnGetLineWidth(napi_env env, napi_callback_info info)
return NapiThrowError(env, TextErrorCode::ERROR_INVALID_PARAM, "Invalid params.");
}
int lineNumber = 0;
if (!(ConvertFromJsValue(env, argv[0], lineNumber))) {
if (!(argv[0] != nullptr && ConvertFromJsValue(env, argv[0], lineNumber))) {
TEXT_LOGE("Failed to convert line number");
return NapiGetUndefined(env);
}
@ -668,7 +689,7 @@ napi_value JsParagraph::OnGetFontMetricsByTextStyle(napi_env env, napi_callback_
napi_value argv[ARGC_ONE] = {nullptr};
napi_status status = napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
if (status != napi_ok || argc < ARGC_ONE) {
TEXT_LOGE("Failed to get parameter, argc %{public}zu, ret %{public}d", argc, status);
TEXT_LOGE("Argc is invalid: %{public}zu", argc);
return NapiThrowError(env, TextErrorCode::ERROR_INVALID_PARAM, "Invalid params.");
}
TextStyle textStyle;
@ -697,12 +718,12 @@ napi_value JsParagraph::OnGetLineFontMetrics(napi_env env, napi_callback_info in
napi_value argv[ARGC_ONE] = {nullptr};
napi_status status = napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
if (status != napi_ok || argc < ARGC_ONE) {
TEXT_LOGE("Failed to get parameter, argc %{public}zu, ret %{public}d", argc, status);
TEXT_LOGE("Argc is invalid: %{public}zu", argc);
return NapiThrowError(env, TextErrorCode::ERROR_INVALID_PARAM, "Invalid params.");
}
int lineNumber = 0;
if (!(ConvertFromJsValue(env, argv[0], lineNumber))) {
TEXT_LOGE("Failed to convert line number");
TEXT_LOGE("Argv is invalid");
return NapiGetUndefined(env);
}
@ -711,7 +732,7 @@ napi_value JsParagraph::OnGetLineFontMetrics(napi_env env, napi_callback_info in
size_t fontMetricsSize = 0;
std::vector<Drawing::FontMetrics> grabFontMetrics;
if (!paragraph_->GetLineFontMetrics(lineNumber, fontMetricsSize, grabFontMetrics)) {
TEXT_LOGE("Failed to get line font metrics");
TEXT_LOGE("GetLineFontMetrics failed");
return returnFontMetrics;
}
@ -732,6 +753,12 @@ JsParagraph::~JsParagraph()
{
}
std::shared_ptr<Typography> JsParagraph::GetParagraph()
{
std::shared_ptr<Typography> typography = std::move(paragraph_);
return typography;
}
napi_value JsParagraph::CreateJsTypography(napi_env env, std::unique_ptr<Typography> typography)
{
napi_value constructor = nullptr;

View File

@ -61,7 +61,6 @@ public:
static napi_value IsStrutStyleEqual(napi_env env, napi_callback_info info);
private:
static thread_local napi_ref constructor_;
napi_value OnLayout(napi_env env, napi_callback_info info);
napi_value OnPaint(napi_env env, napi_callback_info info);
napi_value OnPaintOnPath(napi_env env, napi_callback_info info);
@ -85,9 +84,13 @@ private:
napi_value OnGetActualTextRange(napi_env env, napi_callback_info info);
napi_value OnGetLineMetrics(napi_env env, napi_callback_info info);
napi_value OnGetLineMetricsAt(napi_env env, napi_callback_info info);
napi_value OnGetFontMetricsByTextStyle(napi_env env, napi_callback_info info);
napi_value OnGetLineFontMetrics(napi_env env, napi_callback_info info);
napi_value OnLayoutAsync(napi_env env, napi_callback_info info);
static thread_local napi_ref constructor_;
static std::unique_ptr<Typography> g_Typography;
std::shared_ptr<Typography> paragraph_ = nullptr;
};
} // namespace OHOS::Rosen

View File

@ -293,12 +293,17 @@ napi_value JsRun::OnPaint(napi_env env, napi_callback_info info)
TEXT_LOGE("Failed to get parameter, argc %{public}zu, ret %{public}d", argc, status);
return NapiThrowError(env, TextErrorCode::ERROR_INVALID_PARAM, "Invalid params.");
}
if (argv[0] == nullptr) {
TEXT_LOGE("JsRun::OnPaint argv[0] is invalid");
return NapiGetUndefined(env);
}
Drawing::JsCanvas* jsCanvas = nullptr;
double x = 0.0;
double y = 0.0;
napi_unwrap(env, argv[0], reinterpret_cast<void **>(&jsCanvas));
if (!jsCanvas || !jsCanvas->GetCanvas() ||
!(ConvertFromJsValue(env, argv[ARGC_ONE], x) && ConvertFromJsValue(env, argv[ARGC_TWO], y))) {
!(argv[ARGC_ONE] != nullptr && ConvertFromJsValue(env, argv[ARGC_ONE], x) &&
argv[ARGC_TWO] != nullptr && ConvertFromJsValue(env, argv[ARGC_TWO], y))) {
TEXT_LOGE("Failed to get paint parameter");
return NapiGetUndefined(env);
}

View File

@ -20,8 +20,11 @@
#include "utils/text_log.h"
namespace OHOS::Rosen {
namespace {
const std ::string CLASS_NAME = "TextLine";
}
thread_local napi_ref JsTextLine::constructor_ = nullptr;
const std::string CLASS_NAME = "TextLine";
napi_value JsTextLine::Constructor(napi_env env, napi_callback_info info)
{
size_t argCount = 0;
@ -267,6 +270,10 @@ napi_value JsTextLine::OnPaint(napi_env env, napi_callback_info info)
TEXT_LOGE("Failed to get paramter, argc %{public}zu, ret %{public}d", argc, status);
return NapiThrowError(env, TextErrorCode::ERROR_INVALID_PARAM, "Invalid params.");
}
if (argv[0] == nullptr) {
TEXT_LOGE("JsTextLine::OnPaint argv[0] is invalid");
return NapiGetUndefined(env);
}
Drawing::JsCanvas* jsCanvas = nullptr;
napi_unwrap(env, argv[0], reinterpret_cast<void**>(&jsCanvas));
if (!jsCanvas || !jsCanvas->GetCanvas()) {
@ -275,7 +282,8 @@ napi_value JsTextLine::OnPaint(napi_env env, napi_callback_info info)
}
double x = 0.0;
double y = 0.0;
if (!(ConvertFromJsValue(env, argv[ARGC_ONE], x) && ConvertFromJsValue(env, argv[ARGC_TWO], y))) {
if (!(argv[ARGC_ONE] != nullptr && ConvertFromJsValue(env, argv[ARGC_ONE], x) &&
argv[ARGC_TWO] != nullptr && ConvertFromJsValue(env, argv[ARGC_TWO], y))) {
return NapiGetUndefined(env);
}
textLine_->Paint(jsCanvas->GetCanvas(), x, y);

View File

@ -12,9 +12,11 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "utils/log.h"
#include <native_engine/native_engine.h>
#include "js_text_init.h"
#include "utils/text_log.h"
static napi_module g_textModule = {
.nm_filename = "libtext_napi.so/text.js",

View File

@ -111,6 +111,7 @@ bool GetDecorationFromJS(napi_env env, napi_value argValue, const std::string& s
SetColorFromJS(env, tempValue, "color", textStyle.decorationColor);
tempValueChild = nullptr;
napi_get_named_property(env, tempValue, "decorationStyle", &tempValueChild);
uint32_t decorationStyle = 0;
if (tempValueChild != nullptr && napi_get_value_uint32(env, tempValueChild, &decorationStyle) == napi_ok) {
@ -120,57 +121,6 @@ bool GetDecorationFromJS(napi_env env, napi_value argValue, const std::string& s
return true;
}
void ParsePartTextStyle(napi_env env, napi_value argValue, TextStyle& textStyle)
{
napi_value tempValue = nullptr;
napi_get_named_property(env, argValue, "fontWeight", &tempValue);
uint32_t fontWeight = 0;
if (tempValue != nullptr && napi_get_value_uint32(env, tempValue, &fontWeight) == napi_ok) {
textStyle.fontWeight = FontWeight(fontWeight);
}
napi_get_named_property(env, argValue, "fontStyle", &tempValue);
uint32_t fontStyle = 0;
if (tempValue != nullptr && napi_get_value_uint32(env, tempValue, &fontStyle) == napi_ok) {
textStyle.fontStyle = FontStyle(fontStyle);
// Let OBLIQUE be equal to ITALIC, it's a temp modify.
if (textStyle.fontStyle == FontStyle::OBLIQUE) {
textStyle.fontStyle = FontStyle::ITALIC;
}
}
napi_get_named_property(env, argValue, "baseline", &tempValue);
uint32_t baseline = 0;
if (tempValue != nullptr && napi_get_value_uint32(env, tempValue, &baseline) == napi_ok) {
textStyle.baseline = TextBaseline(baseline);
}
SetDoubleValueFromJS(env, argValue, "fontSize", textStyle.fontSize);
std::vector<std::string> fontFamilies;
napi_get_named_property(env, argValue, "fontFamilies", &tempValue);
if (tempValue != nullptr && OnMakeFontFamilies(env, tempValue, fontFamilies)) {
textStyle.fontFamilies = fontFamilies;
}
GetDecorationFromJS(env, argValue, "decoration", textStyle);
SetTextStyleBaseType(env, argValue, textStyle);
ReceiveFontFeature(env, argValue, textStyle);
ReceiveFontVariation(env, argValue, textStyle);
napi_get_named_property(env, argValue, "ellipsis", &tempValue);
std::string text = "";
if (tempValue != nullptr && ConvertFromJsValue(env, tempValue, text)) {
textStyle.ellipsis = Str8ToStr16(text);
}
napi_get_named_property(env, argValue, "ellipsisMode", &tempValue);
uint32_t ellipsisModal = 0;
if (tempValue != nullptr && napi_get_value_uint32(env, tempValue, &ellipsisModal)== napi_ok) {
textStyle.ellipsisModal = EllipsisModal(ellipsisModal);
}
napi_get_named_property(env, argValue, "locale", &tempValue);
std::string textLocale = "";
if (tempValue != nullptr && ConvertFromJsValue(env, tempValue, textLocale)) {
textStyle.locale = textLocale;
}
}
bool GetNamePropertyFromJS(napi_env env, napi_value argValue, const std::string& str, napi_value& propertyValue)
{
bool result = false;
@ -317,6 +267,57 @@ void SetTextShadowProperty(napi_env env, napi_value argValue, TextStyle& textSty
return;
}
void ParsePartTextStyle(napi_env env, napi_value argValue, TextStyle& textStyle)
{
napi_value tempValue = nullptr;
napi_get_named_property(env, argValue, "fontWeight", &tempValue);
uint32_t fontWeight = 0;
if (tempValue != nullptr && napi_get_value_uint32(env, tempValue, &fontWeight) == napi_ok) {
textStyle.fontWeight = FontWeight(fontWeight);
}
napi_get_named_property(env, argValue, "fontStyle", &tempValue);
uint32_t fontStyle = 0;
if (tempValue != nullptr && napi_get_value_uint32(env, tempValue, &fontStyle) == napi_ok) {
textStyle.fontStyle = FontStyle(fontStyle);
// Let OBLIQUE be equal to ITALIC, it's a temp modify.
if (textStyle.fontStyle == FontStyle::OBLIQUE) {
textStyle.fontStyle = FontStyle::ITALIC;
}
}
napi_get_named_property(env, argValue, "baseline", &tempValue);
uint32_t baseline = 0;
if (tempValue != nullptr && napi_get_value_uint32(env, tempValue, &baseline) == napi_ok) {
textStyle.baseline = TextBaseline(baseline);
}
SetDoubleValueFromJS(env, argValue, "fontSize", textStyle.fontSize);
std::vector<std::string> fontFamilies;
napi_get_named_property(env, argValue, "fontFamilies", &tempValue);
if (tempValue != nullptr && OnMakeFontFamilies(env, tempValue, fontFamilies)) {
textStyle.fontFamilies = fontFamilies;
}
GetDecorationFromJS(env, argValue, "decoration", textStyle);
SetTextStyleBaseType(env, argValue, textStyle);
ReceiveFontFeature(env, argValue, textStyle);
ReceiveFontVariation(env, argValue, textStyle);
napi_get_named_property(env, argValue, "ellipsis", &tempValue);
std::string text = "";
if (tempValue != nullptr && ConvertFromJsValue(env, tempValue, text)) {
textStyle.ellipsis = Str8ToStr16(text);
}
napi_get_named_property(env, argValue, "ellipsisMode", &tempValue);
uint32_t ellipsisModal = 0;
if (tempValue != nullptr && napi_get_value_uint32(env, tempValue, &ellipsisModal)== napi_ok) {
textStyle.ellipsisModal = EllipsisModal(ellipsisModal);
}
napi_get_named_property(env, argValue, "locale", &tempValue);
std::string textLocale = "";
if (tempValue != nullptr && ConvertFromJsValue(env, tempValue, textLocale)) {
textStyle.locale = textLocale;
}
}
bool GetTextStyleFromJS(napi_env env, napi_value argValue, TextStyle& textStyle)
{
napi_valuetype valueType = napi_undefined;
@ -330,25 +331,17 @@ bool GetTextStyleFromJS(napi_env env, napi_value argValue, TextStyle& textStyle)
return true;
}
void SetParagraphStyleEllipsis(napi_env env, napi_value argValue, TypographyStyle& pographyStyle)
static void SetAlignValueForParagraphStyle(napi_env env, napi_value argValue, TypographyStyle& pographyStyle)
{
if (argValue == nullptr) {
return;
}
napi_value tempValue = nullptr;
if (napi_get_named_property(env, argValue, "ellipsis", &tempValue) != napi_ok) {
return;
napi_get_named_property(env, argValue, "ellipsis", &tempValue);
uint32_t align = 0;
if (tempValue != nullptr && napi_get_value_uint32(env, tempValue, &align) == napi_ok) {
pographyStyle.textAlign = TextAlign(align);
}
std::string text = "";
if (tempValue != nullptr && ConvertFromJsValue(env, tempValue, text)) {
pographyStyle.ellipsis = Str8ToStr16(text);
}
if (napi_get_named_property(env, argValue, "ellipsisMode", &tempValue) != napi_ok) {
return;
}
uint32_t ellipsisModal = 0;
if (tempValue != nullptr && napi_get_value_uint32(env, tempValue, &ellipsisModal) == napi_ok) {
pographyStyle.ellipsisModal = EllipsisModal(ellipsisModal);
}
return;
}
bool GetParagraphStyleFromJS(napi_env env, napi_value argValue, TypographyStyle& pographyStyle)
@ -362,31 +355,26 @@ bool GetParagraphStyleFromJS(napi_env env, napi_value argValue, TypographyStyle&
if (tempValue != nullptr && GetTextStyleFromJS(env, tempValue, textStyle)) {
pographyStyle.SetTextStyle(textStyle);
}
tempValue = nullptr;
napi_get_named_property(env, argValue, "textDirection", &tempValue);
uint32_t textDirection = 0;
if (tempValue != nullptr && napi_get_value_uint32(env, tempValue, &textDirection) == napi_ok) {
pographyStyle.textDirection = TextDirection(textDirection);
}
napi_get_named_property(env, argValue, "align", &tempValue);
uint32_t align = 0;
if (tempValue != nullptr && napi_get_value_uint32(env, tempValue, &align) == napi_ok) {
pographyStyle.textAlign = TextAlign(align);
}
SetAlignValueForParagraphStyle(env, argValue, pographyStyle);
tempValue = nullptr;
napi_get_named_property(env, argValue, "wordBreak", &tempValue);
uint32_t wordBreak = 0;
if (tempValue != nullptr && napi_get_value_uint32(env, tempValue, &wordBreak) == napi_ok) {
pographyStyle.wordBreakType = WordBreakType(wordBreak);
}
tempValue = nullptr;
napi_get_named_property(env, argValue, "maxLines", &tempValue);
int64_t maxLines = 0;
if (tempValue != nullptr && napi_get_value_int64(env, tempValue, &maxLines) == napi_ok) {
pographyStyle.maxLines = maxLines < 0 ? 0 : maxLines;
}
tempValue = nullptr;
napi_get_named_property(env, argValue, "breakStrategy", &tempValue);
uint32_t breakStrategy = 0;
if (tempValue != nullptr && napi_get_value_uint32(env, tempValue, &breakStrategy) == napi_ok) {
@ -398,13 +386,9 @@ bool GetParagraphStyleFromJS(napi_env env, napi_value argValue, TypographyStyle&
SetStrutStyleFromJS(env, strutStyleValue, pographyStyle);
}
if (!textStyle.ellipsis.empty()) {
pographyStyle.ellipsis = textStyle.ellipsis;
pographyStyle.ellipsisModal = textStyle.ellipsisModal;
} else {
SetParagraphStyleEllipsis(env, argValue, pographyStyle);
}
pographyStyle.ellipsis = textStyle.ellipsis;
pographyStyle.ellipsisModal = textStyle.ellipsisModal;
napi_get_named_property(env, argValue, "tab", &tempValue);
TextTab textTab;
if (tempValue != nullptr && GetTextTabFromJS(env, tempValue, textTab)) {
@ -429,7 +413,7 @@ bool GetPlaceholderSpanFromJS(napi_env env, napi_value argValue, PlaceholderSpan
} else {
return false;
}
tempValue = nullptr;
napi_get_named_property(env, argValue, "height", &tempValue);
double height = 0;
if (tempValue != nullptr && napi_get_value_double(env, tempValue, &height) == napi_ok) {
@ -437,7 +421,7 @@ bool GetPlaceholderSpanFromJS(napi_env env, napi_value argValue, PlaceholderSpan
} else {
return false;
}
tempValue = nullptr;
napi_get_named_property(env, argValue, "align", &tempValue);
uint32_t align = 0;
if (tempValue != nullptr && napi_get_value_uint32(env, tempValue, &align) == napi_ok) {
@ -445,7 +429,7 @@ bool GetPlaceholderSpanFromJS(napi_env env, napi_value argValue, PlaceholderSpan
} else {
return false;
}
tempValue = nullptr;
napi_get_named_property(env, argValue, "baseline", &tempValue);
uint32_t baseline = 0;
if (tempValue != nullptr && napi_get_value_uint32(env, tempValue, &baseline) == napi_ok) {
@ -453,7 +437,7 @@ bool GetPlaceholderSpanFromJS(napi_env env, napi_value argValue, PlaceholderSpan
} else {
return false;
}
tempValue = nullptr;
napi_get_named_property(env, argValue, "baselineOffset", &tempValue);
double baselineOffset = 0;
if (tempValue != nullptr && napi_get_value_double(env, tempValue, &baselineOffset) == napi_ok) {
@ -732,16 +716,6 @@ napi_value GetFontMetricsAndConvertToJsValue(napi_env env, Drawing::FontMetrics*
return objValue;
}
bool NapiValueTypeIsValid(napi_env env, napi_value argValue)
{
napi_valuetype valueType;
if (napi_typeof(env, argValue, &valueType) != napi_ok || valueType == napi_null || valueType == napi_undefined) {
TEXT_LOGE("Invalid value type %{public}d", static_cast<int32_t>(valueType));
return false;
}
return true;
}
bool GetTextTabFromJS(napi_env env, napi_value argValue, TextTab& tab)
{
if (argValue == nullptr) {
@ -792,6 +766,16 @@ bool GetTextTabFromJS(napi_env env, napi_value argValue, TextTab& tab)
return true;
}
bool NapiValueTypeIsValid(napi_env env, napi_value argValue)
{
napi_valuetype valueType;
if (napi_typeof(env, argValue, &valueType) != napi_ok || valueType == napi_null || valueType == napi_undefined) {
TEXT_LOGE("Invalid value type %{public}d", static_cast<int32_t>(valueType));
return false;
}
return true;
}
napi_value GetTypographicBoundsAndConvertToJsValue(napi_env env, float ascent,
float descent, float leading, float width)
{

View File

@ -29,7 +29,6 @@
#include "utils/point.h"
#include "utils/text_log.h"
namespace OHOS::Rosen {
constexpr size_t ARGC_ONE = 1;
constexpr size_t ARGC_TWO = 2;
@ -191,6 +190,8 @@ napi_value CreateJsValue(napi_env env, const T& value)
(value != nullptr) ? napi_create_string_utf8(env, value, strlen(value), &result) :
napi_get_undefined(env, &result);
return result;
} else {
return result;
}
}
@ -539,10 +540,10 @@ void SetRectStyleFromJS(napi_env env, napi_value argValue, RectStyle& rectStyle)
napi_value GetFontMetricsAndConvertToJsValue(napi_env env, Drawing::FontMetrics* metrics);
bool NapiValueTypeIsValid(napi_env env, napi_value argValue);
bool GetTextTabFromJS(napi_env env, napi_value argValue, TextTab& tab);
bool NapiValueTypeIsValid(napi_env env, napi_value argValue);
napi_value GetTypographicBoundsAndConvertToJsValue(napi_env env, float ascent,
float descent, float leading, float width);

View File

@ -45,6 +45,11 @@ FontCollection::FontCollection(std::shared_ptr<txt::FontCollection> fontCollecti
fontCollection_->SetDynamicFontManager(dfmanager_);
}
std::shared_ptr<txt::FontCollection> FontCollection::Get()
{
return fontCollection_;
}
FontCollection::~FontCollection()
{
if (Drawing::Typeface::GetTypefaceUnRegisterCallBack() == nullptr) {
@ -58,11 +63,6 @@ FontCollection::~FontCollection()
typefaces_.clear();
}
std::shared_ptr<txt::FontCollection> FontCollection::Get()
{
return fontCollection_;
}
void FontCollection::DisableFallback()
{
fontCollection_->DisableFontFallback();
@ -86,14 +86,14 @@ bool FontCollection::RegisterTypeface(std::shared_ptr<Drawing::Typeface> typefac
std::unique_lock<std::mutex> lock(mutex_);
if (typefaces_.find(typeface->GetUniqueID()) != typefaces_.end()) {
TEXT_LOGI("Find same typeface:familyname:%{public}s, uniqueid:%{public}u",
TEXT_LOGI("Find same typeface:family name:%{public}s, uniqueid:%{public}u",
typeface->GetFamilyName().c_str(), typeface->GetUniqueID());
return true;
}
if (!Drawing::Typeface::GetTypefaceRegisterCallBack()(typeface)) {
return false;
}
TEXT_LOGI("Reg fontcollection typeface:familyname:%{public}s, uniqueid:%{public}u",
TEXT_LOGI("Reg fontcollection typeface:family name:%{public}s, uniqueid:%{public}u",
typeface->GetFamilyName().c_str(), typeface->GetUniqueID());
typefaces_.emplace(typeface->GetUniqueID(), typeface);
return true;
@ -104,7 +104,7 @@ std::shared_ptr<Drawing::Typeface> FontCollection::LoadFont(
{
std::shared_ptr<Drawing::Typeface> typeface(dfmanager_->LoadDynamicFont(familyName, data, datalen));
if (!RegisterTypeface(typeface)) {
TEXT_LOGE("Register typeface failed.");
TEXT_LOGE("Failed to register typeface %{public}s", familyName.c_str());
return nullptr;
}
fontCollection_->ClearFontFamilyCache();
@ -128,7 +128,7 @@ std::shared_ptr<Drawing::Typeface> FontCollection::LoadThemeFont(
std::string name = face->GetFamilyName();
for (auto item : typefaces_) {
if (name == item.second->GetFamilyName()) {
TEXT_LOGI("Find same theme font:familyname:%{public}s, uniqueid:%{public}u",
TEXT_LOGI("Find same theme font:family name:%{public}s, uniqueid:%{public}u",
name.c_str(), item.second->GetUniqueID());
dfmanager_->LoadThemeFont(OHOS_THEME_FONT, item.second);
fontCollection_->ClearFontFamilyCache();
@ -139,7 +139,7 @@ std::shared_ptr<Drawing::Typeface> FontCollection::LoadThemeFont(
std::shared_ptr<Drawing::Typeface> typeface(dfmanager_->LoadThemeFont(familyName, OHOS_THEME_FONT, data, datalen));
if (!RegisterTypeface(typeface)) {
TEXT_LOGE("Register typeface failed.");
TEXT_LOGE("Failed to register typeface %{public}s", familyName.c_str());
}
fontCollection_->ClearFontFamilyCache();
return typeface;

View File

@ -14,8 +14,9 @@
*/
#include "run_impl.h"
#include "impl/run_impl.h"
#include "convert.h"
#include "impl/run_impl.h"
namespace OHOS {
namespace Rosen {
@ -93,7 +94,7 @@ void RunImpl::GetStringRange(uint64_t* location, uint64_t* length) const
if (location == nullptr || length == nullptr) {
return;
} else if (run_ == nullptr) {
*location = 0;
*location= 0;
*length = 0;
return;
}

View File

@ -16,13 +16,14 @@
#ifndef ROSEN_TEXT_ADAPTER_TXT_TEXT_LINE_BASE_H
#define ROSEN_TEXT_ADAPTER_TXT_TEXT_LINE_BASE_H
#include "rosen_text/text_line_base.h"
#include "txt/text_line_base.h"
#include "draw/canvas.h" // Drawing
#include "rosen_text/typography.h"
#include <memory>
#include <utility>
#include "draw/canvas.h" // Drawing
#include "rosen_text/text_line_base.h"
#include "rosen_text/typography.h"
#include "txt/text_line_base.h"
namespace OHOS {
namespace Rosen {
namespace AdapterTxt {

View File

@ -160,8 +160,8 @@ public:
size_t& charNumber, std::vector<Drawing::FontMetrics>& fontMetrics) = 0;
virtual std::vector<std::unique_ptr<TextLineBase>> GetTextLines() const = 0;
virtual std::unique_ptr<Typography> CloneSelf() = 0;
virtual void UpdateColor(size_t from, size_t to, const Drawing::Color& color) = 0;
virtual double GetLongestLineWithIndent() const = 0;
virtual void UpdateColor(size_t from, size_t to, const Drawing::Color& color) = 0;
virtual void* GetParagraph() = 0;
virtual Drawing::RectI GeneratePaintRegion(double x, double y) const = 0;
};

View File

@ -76,7 +76,8 @@ struct TypographyStyle {
EllipsisModal ellipsisModal = EllipsisModal::TAIL;
float textSplitRatio = 0.5f;
bool operator==(const TypographyStyle &rhs) const {
bool operator==(const TypographyStyle &rhs) const
{
return
this->ELLIPSIS == rhs.ELLIPSIS &&
this->fontWeight == rhs.fontWeight &&

View File

@ -96,8 +96,8 @@ skt::TextShadow MakeTextShadow(const TextShadow& txtShadow)
const char* DefaultLocale()
{
static const char* localeZh = "zh-Hans";
return localeZh;
static const char* LOCALE_ZH = "zh-Hans";
return LOCALE_ZH;
}
} // anonymous namespace
@ -178,6 +178,24 @@ skt::TextTabs ConvertToSkTextTab(const TextTab& tab)
};
}
void ParagraphBuilderImpl::TextStyleToSKStrutStyle(skt::StrutStyle& strutStyle, const ParagraphStyle& txt)
{
strutStyle.setFontStyle(MakeFontStyle(txt.strutFontWeight, txt.strutFontWidth, txt.strutFontStyle));
strutStyle.setFontSize(SkDoubleToScalar(txt.strutFontSize));
strutStyle.setHeight(SkDoubleToScalar(txt.strutHeight));
strutStyle.setHeightOverride(txt.strutHeightOverride);
std::vector<SkString> strutFonts;
std::transform(txt.strutFontFamilies.begin(), txt.strutFontFamilies.end(), std::back_inserter(strutFonts),
[](const std::string& f) { return SkString(f.c_str()); });
strutStyle.setFontFamilies(strutFonts);
strutStyle.setLeading(txt.strutLeading);
strutStyle.setForceStrutHeight(txt.forceStrutHeight);
strutStyle.setStrutEnabled(txt.strutEnabled);
strutStyle.setWordBreakType(static_cast<skt::WordBreakType>(txt.wordBreakType));
strutStyle.setLineBreakStrategy(static_cast<skt::LineBreakStrategy>(txt.breakStrategy));
}
skt::ParagraphStyle ParagraphBuilderImpl::TextStyleToSkStyle(const ParagraphStyle& txt)
{
skt::ParagraphStyle skStyle;
@ -200,20 +218,7 @@ skt::ParagraphStyle ParagraphBuilderImpl::TextStyleToSkStyle(const ParagraphStyl
skStyle.setTextStyle(textStyle);
skStyle.setTextOverflower(txt.textOverflower);
skt::StrutStyle strutStyle;
strutStyle.setFontStyle(MakeFontStyle(txt.strutFontWeight, txt.strutFontWidth, txt.strutFontStyle));
strutStyle.setFontSize(SkDoubleToScalar(txt.strutFontSize));
strutStyle.setHeight(SkDoubleToScalar(txt.strutHeight));
strutStyle.setHeightOverride(txt.strutHeightOverride);
std::vector<SkString> strutFonts;
std::transform(txt.strutFontFamilies.begin(), txt.strutFontFamilies.end(), std::back_inserter(strutFonts),
[](const std::string& f) { return SkString(f.c_str()); });
strutStyle.setFontFamilies(strutFonts);
strutStyle.setLeading(txt.strutLeading);
strutStyle.setForceStrutHeight(txt.forceStrutHeight);
strutStyle.setStrutEnabled(txt.strutEnabled);
strutStyle.setWordBreakType(static_cast<skt::WordBreakType>(txt.wordBreakType));
strutStyle.setLineBreakStrategy(static_cast<skt::LineBreakStrategy>(txt.breakStrategy));
TextStyleToSKStrutStyle(strutStyle, txt);
skStyle.setStrutStyle(strutStyle);
skStyle.setTextAlign(static_cast<skt::TextAlign>(txt.textAlign));

View File

@ -43,6 +43,7 @@ public:
private:
void RecordDifferentPthreadCall(const char* caller) const;
skia::textlayout::ParagraphPainter::PaintID AllocPaintID(const PaintRecord& paint);
void TextStyleToSKStrutStyle(skia::textlayout::StrutStyle& strutStyle, const ParagraphStyle& txt);
skia::textlayout::ParagraphStyle TextStyleToSkStyle(const ParagraphStyle& txt);
skia::textlayout::TextStyle TextStyleToSkStyle(const TextStyle& txt);
void CopyTextStylePaint(const TextStyle& txt, skia::textlayout::TextStyle& skStyle);

View File

@ -20,9 +20,9 @@
#include "include/core/SkMatrix.h"
#include "drawing_painter_impl.h"
#include "paragraph_builder_impl.h"
#include "skia_adapter/skia_convert_utils.h"
#include "text/font_metrics.h"
#include "paragraph_builder_impl.h"
#include "text_line_impl.h"
#include "utils/text_log.h"

View File

@ -16,13 +16,14 @@
#ifndef ROSEN_MODULES_SPTEXT_TEXTLINE_IMPL_H
#define ROSEN_MODULES_SPTEXT_TEXTLINE_IMPL_H
#include "modules/skparagraph/include/TextLineBase.h"
#include "txt/paint_record.h"
#include "txt/text_line_base.h"
#include <cstdint>
#include <memory>
#include <utility>
#include "modules/skparagraph/include/TextLineBase.h"
#include "txt/paint_record.h"
#include "txt/text_line_base.h"
namespace OHOS {
namespace Rosen {
namespace SPText {

View File

@ -79,7 +79,7 @@ void TypefaceFontAssetProvider::RegisterTypeface(sk_sp<SkTypeface> typeface, std
auto value = std::make_pair(canonicalName, sk_make_sp<TypefaceFontStyleSet>());
iter = registeredFamilies_.emplace(value).first;
}
TEXT_LOGI("Reg fontmgr typeface:canonicalName:%{public}s, uniqueid:%{public}u",
TEXT_LOGI("Reg fontmgr typeface: family Name:%{public}s, uniqueid:%{public}u",
canonicalName.c_str(), typeface->uniqueID());
iter->second->registerTypeface(std::move(typeface));
} else if (iter != registeredFamilies_.end()) {
@ -91,7 +91,7 @@ void TypefaceFontAssetProvider::RegisterTypeface(sk_sp<SkTypeface> typeface, std
break;
}
}
TEXT_LOGW("Unreg fontmgr typeface:canonicalName:%{public}s", canonicalName.c_str());
TEXT_LOGI("Unreg fontmgr typeface: family Name:%{public}s", canonicalName.c_str());
}
}

View File

@ -33,7 +33,6 @@ namespace TextEngine {
* including parameters for default TextStyle, multi-text, and line style.
*/
struct TypographyStyle {
const static inline std::u16string ELLIPSIS = u"\u2026";
// default TextStyle
FontWeight fontWeight = FontWeight::W400;
FontStyle fontStyle = FontStyle::NORMAL;

View File

@ -57,8 +57,7 @@ void FontDescriptorCache::ClearFontFileCache()
void FontDescriptorCache::ParserSystemFonts()
{
icu::Locale locale = icu::Locale::getDefault();
for (auto& item : parser_.GetSystemFonts(std::string(locale.getName()))) {
for (auto& item : parser_.GetSystemFonts()) {
FontDescriptorScatter(item);
}
Dump();
@ -89,7 +88,7 @@ void FontDescriptorCache::ParserInstallFonts()
for (const auto& path : fontPathList) {
if (!ProcessInstalledFontPath(path)) {
TEXT_LOGE("Failed to process font path, path: %{public}s", path.c_str());
TEXT_LOGE("Failed to process font path");
}
}
}
@ -218,8 +217,8 @@ bool FontDescriptorCache::ProcessSystemFontType(const int32_t& systemFontType, i
return true;
}
void FontDescriptorCache::GetSystemFontFullNamesByType(const int32_t& systemFontType,
std::unordered_set<std::string>& fontList)
void FontDescriptorCache::GetSystemFontFullNamesByType(
const int32_t &systemFontType, std::unordered_set<std::string> &fontList)
{
if (systemFontType < 0) {
TEXT_LOGE("SystemFontType is an invalid value");
@ -262,9 +261,11 @@ bool FontDescriptorCache::ParseInstallFontDescSharedPtrByName(const std::string&
break;
}
}
//Setting the locale to English is to ensure consistency with the fullName format obtained from Skia.
std::string locale = TextEngine::ENGLISH;
std::vector<FontDescSharedPtr> descriptors;
icu::Locale locale = icu::Locale::getDefault();
if (parser_.ParserFontDescriptorFromPath(path, fullName, descriptors, std::string(locale.getName()))) {
if (parser_.ParserFontDescriptorFromPath(path, fullName, descriptors, locale)) {
for (auto& item : descriptors) {
if (item->fullName == fullName) {
item->weight = WeightAlignment(item->weight);
@ -296,6 +297,11 @@ void FontDescriptorCache::GetFontDescSharedPtrByFullName(const std::string& full
result = nullptr;
return;
}
if (systemFontType < 0) {
TEXT_LOGE("SystemFontType is an invalid value");
result = nullptr;
return;
}
auto tryFindFontDescriptor = [&fullName, &result](const std::unordered_map<std::string,
std::set<FontDescSharedPtr>>& map) -> bool {
auto it = map.find(fullName);
@ -549,7 +555,7 @@ int32_t FontDescriptorCache::WeightAlignment(int32_t weight)
return weight;
}
static const std::vector<int> weightRange = {
static const std::vector<int> weightType = {
Drawing::FontStyle::THIN_WEIGHT,
Drawing::FontStyle::EXTRA_LIGHT_WEIGHT,
Drawing::FontStyle::LIGHT_WEIGHT,
@ -562,7 +568,7 @@ int32_t FontDescriptorCache::WeightAlignment(int32_t weight)
Drawing::FontStyle::EXTRA_BLACK_WEIGHT
};
// Obtain weight ranges for non-whole hundred values
auto it = std::lower_bound(weightRange.begin(), weightRange.end(), weight);
auto it = std::lower_bound(weightType.begin(), weightType.end(), weight);
std::vector<int> targetRange = { *(it - 1), *it };
/**

View File

@ -47,7 +47,7 @@ FontParser::FontParser()
{
data_ = nullptr;
length_ = 0;
fontSet_.clear();
fontSet_.clear();
FontConfig fontConfig(FONT_CONFIG_FILE);
auto fonts = fontConfig.GetFontSet();
fontSet_.insert(fontSet_.end(), fonts.begin(), fonts.end());
@ -109,11 +109,8 @@ void FontParser::SetNameString(FontParser::FontDescriptor& fontDescriptor, std::
{
bool willSet = field.empty();
if (!willSet) {
if (languageId == fontDescriptor.requestedLid) {
willSet = true;
} else if (fieldLid != fontDescriptor.requestedLid && languageId == LANGUAGE_DEFAULT) {
willSet = true;
}
willSet = languageId == fontDescriptor.requestedLid ||
(fieldLid != fontDescriptor.requestedLid && languageId == LANGUAGE_DEFAULT);
}
if (willSet) {
@ -291,7 +288,7 @@ int FontParser::ParseTable(std::shared_ptr<Drawing::Typeface> typeface, FontPars
int FontParser::SetFontDescriptor(const unsigned int languageId)
{
visibilityFonts_.clear();
visibilityFonts_.clear();
std::list<std::string> fontSetCache;
for (unsigned int i = 0; i < fontSet_.size(); ++i) {
FontParser::FontDescriptor fontDescriptor;
@ -310,7 +307,6 @@ int FontParser::SetFontDescriptor(const unsigned int languageId)
LOGSO_FUNC_LINE(ERROR) << "parse table failed";
return FAILED;
}
size_t idx = fontSet_[i].rfind('/');
std::string fontName = fontSet_[i].substr(idx + 1, fontSet_[i].size() - idx - 1);
if (std::find(fontSetCache.begin(), fontSetCache.end(), fontName) == fontSetCache.end()) {
@ -458,7 +454,7 @@ std::unique_ptr<FontParser::FontDescriptor> FontParser::ParseFontDescriptor(cons
return nullptr;
}
std::string path = SYSTEM_FONT_PATH + (*fontFileMap)[fontName];
auto typeface = Drawing::Typeface::MakeFromFile(path.c_str());
auto typeface = Drawing::Typeface::MakeFromFile(path.c_str());
if (typeface == nullptr) {
path = SYS_PROD_FONT_PATH + (*fontFileMap)[fontName];
typeface = Drawing::Typeface::MakeFromFile(path.c_str());
@ -467,13 +463,16 @@ std::unique_ptr<FontParser::FontDescriptor> FontParser::ParseFontDescriptor(cons
return nullptr;
}
}
FontParser::FontDescriptor fontDescriptor;
fontDescriptor.requestedLid = languageId;
fontDescriptor.path = path;
fontDescriptor.requestedFullname = fontName;
auto fontStyle = typeface->GetFontStyle();
fontDescriptor.weight = fontStyle.GetWeight();
fontDescriptor.width = fontStyle.GetWidth();
if (ParseTable(typeface, fontDescriptor) != SUCCESSED) {
LOGSO_FUNC_LINE(ERROR) << "parse table failed";
return nullptr;

View File

@ -29,13 +29,13 @@
namespace OHOS {
namespace Rosen {
namespace TextEngine {
#define SIMPLIFIED_CHINESE "zh-hans"
#define TRADITIONAL_CHINESE "zh-hant"
#define ENGLISH "en-latn"
#define LANGUAGE_SC 2052
#define LANGUAGE_TC 1028
#define LANGUAGE_EN 1033
#define LANGUAGE_DEFAULT LANGUAGE_SC
const std::string SIMPLIFIED_CHINESE = "zh-hans";
const std::string TRADITIONAL_CHINESE = "zh-hant";
const std::string ENGLISH = "en-latn";
const unsigned int LANGUAGE_SC = 2052;
const unsigned int LANGUAGE_TC = 1028;
const unsigned int LANGUAGE_EN = 1033;
const unsigned int LANGUAGE_DEFAULT = LANGUAGE_SC;
class FontParser {
public:

View File

@ -31,11 +31,11 @@ const std::string Tag::Get() const
// 5 is the size of open type table tags length
int size = 5;
char tagsWithZero[size];
tagsWithZero[0] = tags[0]; // numbers means array subscripts
tagsWithZero[1] = tags[1];
tagsWithZero[2] = tags[2];
tagsWithZero[3] = tags[3];
tagsWithZero[4] = 0;
tagsWithZero[0] = tags[0]; // 0 means array subscripts
tagsWithZero[1] = tags[1]; // 1 means array subscripts
tagsWithZero[2] = tags[2]; // 2 means array subscripts
tagsWithZero[3] = tags[3]; // 3 means array subscripts
tagsWithZero[4] = 0; // 4 means array subscripts
return tagsWithZero;
}
@ -61,10 +61,10 @@ uint16_t Uint16::Get() const
int32_t Int32::Get() const
{
if (G_ENDIAN.big != '\0') {
return ((static_cast<uint32_t>(data) & 0xff000000) >> 24) |
((static_cast<uint32_t>(data) & 0x00ff0000) >> 8) | // 8 & 24 means offset
((static_cast<uint32_t>(data) & 0x0000ff00) << 8) |
((static_cast<uint32_t>(data) & 0x000000ff) << 24);
return ((static_cast<uint32_t>(data) & 0xff000000) >> 24) | // 24 means offset
((static_cast<uint32_t>(data) & 0x00ff0000) >> 8) | // 8 means offset
((static_cast<uint32_t>(data) & 0x0000ff00) << 8) | // 8 means offset
((static_cast<uint32_t>(data) & 0x000000ff) << 24); // 24 means offset
} else {
return data;
}

View File

@ -48,16 +48,18 @@ int32_t Ranges::GetGlyphId(uint32_t codepoint) const
void Ranges::Dump() const
{
for (const auto &[start, end, gid] : ranges_) {
// 4 means output width0 means fill with 0
LOGSO_FUNC_LINE(INFO) << "0x" << std::uppercase << std::hex << std::setw(4) << std::setfill('0') << start
<< " ~ 0x" << std::uppercase << std::hex << std::setw(4) << std::setfill('0') << end
LOGSO_FUNC_LINE(INFO) << "0x" << std::uppercase << std::hex
<< std::setw(4) << std::setfill('0') << start //4 means output width
<< " ~ 0x" << std::uppercase << std::hex
<< std::setw(4) << std::setfill('0') << end //4 means output width
<< ": offset " << std::dec << end;
}
for (const auto &[codepoint, gid] : singles_) {
// 4 means output width1 means operand, 16 means offset, 1 << 16 means residual multiple
LOGSO_FUNC_LINE(INFO) << "0x" << std::uppercase << std::hex << std::setw(4) << std::setfill('0') << codepoint
<< ": glyphid " << std::dec << (codepoint + gid) % (1 << 16);
LOGSO_FUNC_LINE(INFO) << "0x" << std::uppercase << std::hex
<< std::setw(4) << std::setfill('0') << codepoint //4 means output width
<< ": glyphid " << std::dec
<< (codepoint + gid) % (1 << 16); //16 means offset; 1 << 16 means residual multiple
}
}
} // namespace TextEngine

View File

@ -50,7 +50,7 @@ std::shared_ptr<TexgineTypeface> TexgineFontManager::MatchFamilyStyleCharacter(c
RSTypeface* tf = fontMgr_->MatchFamilyStyleCharacter(familyName.c_str(),
*style.GetFontStyle(), bcp47, bcp47Count, character);
return std::make_shared<TexgineTypeface>(std::shared_ptr<RSTypeface>(tf));
return std::make_shared<TexgineTypeface>(tf);
}
std::shared_ptr<TexgineFontStyleSet> TexgineFontManager::MatchFamily(const std::string &familyName)

View File

@ -1,4 +1,4 @@
# Copyright (c) 2022 Huawei Device Co., Ltd.
# Copyright (c) 2024 Huawei Device Co., Ltd.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at

View File

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (c) 2022 Huawei Device Co., Ltd.
<!-- Copyright (c) 2024 Huawei Device Co., Ltd.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.

View File

@ -1,4 +1,4 @@
# Copyright (c) 2022 Huawei Device Co., Ltd.
# Copyright (c) 2024 Huawei Device Co., Ltd.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at

View File

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (c) 2022 Huawei Device Co., Ltd.
<!-- Copyright (c) 2024 Huawei Device Co., Ltd.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.