* Copyright (c) 2025-2026 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <native_drawing/drawing_bitmap.h>
#include <native_drawing/drawing_brush.h>
#include <native_drawing/drawing_canvas.h>
#include <native_drawing/drawing_color.h>
#include <native_drawing/drawing_color_filter.h>
#include <native_drawing/drawing_filter.h>
#include <native_drawing/drawing_font.h>
#include <native_drawing/drawing_font_collection.h>
#include <native_drawing/drawing_path.h>
#include <native_drawing/drawing_pen.h>
#include <native_drawing/drawing_pixel_map.h>
#include <native_drawing/drawing_rect.h>
#include <native_drawing/drawing_register_font.h>
#include <native_drawing/drawing_sampling_options.h>
#include <native_drawing/drawing_text_blob.h>
#include <native_drawing/drawing_text_declaration.h>
#include <native_drawing/drawing_text_font_descriptor.h>
#include <native_drawing/drawing_text_typography.h>
#include <native_drawing/drawing_typeface.h>
#include <native_drawing/drawing_types.h>
#include <native_drawing/drawing_text_line.h>
#include <native_drawing/drawing_text_run.h>
#include <native_drawing/drawing_point.h>
#include <native_drawing/drawing_text_lineTypography.h>
#include <native_drawing/drawing_text_typography.h>
#include <native_drawing/drawing_font_mgr.h>
#include "napi/native_api.h"
#include "native_common.h"
#include "common.h"
#include "nativeFontNdk.h"
namespace {
constexpr double DEFAULT_FONT_SIZE = 50;
constexpr double MAX_WIDTH = 800.0;
}
napi_value OHDrawingTextStyleGetFontMetrics001(napi_env env, napi_callback_info info)
{
napi_value result = nullptr;
OH_Drawing_TypographyStyle *typoStyle = OH_Drawing_CreateTypographyStyle();
OH_Drawing_TextStyle *txtStyle = OH_Drawing_CreateTextStyle();
OH_Drawing_TypographyCreate *handler =
OH_Drawing_CreateTypographyHandler(typoStyle, OH_Drawing_CreateFontCollection());
const char *text = "OpenHarmony";
OH_Drawing_TypographyHandlerPushTextStyle(handler, txtStyle);
OH_Drawing_TypographyHandlerAddText(handler, text);
OH_Drawing_Typography *typography = OH_Drawing_CreateTypography(handler);
OH_Drawing_TextStyle *style = OH_Drawing_CreateTextStyle();
OH_Drawing_Font_Metrics fontmetrics = {0};
NAPI_ASSERT(env, typography != nullptr, "Typography create failed");
NAPI_ASSERT(env, style != nullptr, "TextStyle create failed");
bool ret = OH_Drawing_TextStyleGetFontMetrics(typography, style, &fontmetrics);
NAPI_ASSERT(env, ret == true, "GetFontMetrics001 failed: ret != true");
NAPI_ASSERT_NO_MES(fontmetrics.ascent < 0);
NAPI_ASSERT_NO_MES(fontmetrics.descent > 0);
NAPI_ASSERT_NO_MES(fontmetrics.leading >= 0);
OH_Drawing_DestroyTypography(typography);
OH_Drawing_DestroyTypographyHandler(handler);
OH_Drawing_DestroyTypographyStyle(typoStyle);
OH_Drawing_DestroyTextStyle(txtStyle);
OH_Drawing_DestroyTextStyle(style);
napi_create_int32(env, SUCCESS, &result);
return result;
}
napi_value OHDrawingTextStyleGetFontMetrics002(napi_env env, napi_callback_info info)
{
napi_value result = nullptr;
OH_Drawing_TextStyle *style = OH_Drawing_CreateTextStyle();
NAPI_ASSERT_NO_MES(style != nullptr);
OH_Drawing_Font_Metrics fontmetrics = {0};
OH_Drawing_Typography *typography = nullptr;
bool ret = OH_Drawing_TextStyleGetFontMetrics(typography, style, &fontmetrics);
NAPI_ASSERT(env, ret == false, "GetFontMetrics002 failed: ret != false");
OH_Drawing_DestroyTextStyle(style);
napi_create_int32(env, SUCCESS, &result);
return result;
}
napi_value OHDrawingTextStyleGetFontMetrics003(napi_env env, napi_callback_info info)
{
napi_value result = nullptr;
OH_Drawing_TypographyStyle *typoStyle = OH_Drawing_CreateTypographyStyle();
OH_Drawing_FontCollection *fontCollection = OH_Drawing_CreateFontCollection();
OH_Drawing_TextStyle *txtStyle = OH_Drawing_CreateTextStyle();
OH_Drawing_TypographyCreate *handler = OH_Drawing_CreateTypographyHandler(typoStyle, fontCollection);
const char *text = "OpenHarmony";
OH_Drawing_TypographyHandlerPushTextStyle(handler, txtStyle);
OH_Drawing_TypographyHandlerAddText(handler, text);
OH_Drawing_Typography *typography = OH_Drawing_CreateTypography(handler);
OH_Drawing_TextStyle *style = nullptr;
OH_Drawing_Font_Metrics fontmetrics = {0};
NAPI_ASSERT(env, typography != nullptr, "Typography create failed");
bool ret = OH_Drawing_TextStyleGetFontMetrics(typography, style, &fontmetrics);
NAPI_ASSERT(env, ret == false, "GetFontMetrics003 failed: ret != false");
OH_Drawing_DestroyTypography(typography);
OH_Drawing_DestroyTypographyHandler(handler);
OH_Drawing_DestroyTypographyStyle(typoStyle);
OH_Drawing_DestroyFontCollection(fontCollection);
OH_Drawing_DestroyTextStyle(txtStyle);
napi_create_int32(env, SUCCESS, &result);
return result;
}
napi_value OHDrawingTextStyleGetFontMetrics004(napi_env env, napi_callback_info info)
{
napi_value result = nullptr;
OH_Drawing_TypographyStyle *typoStyle = OH_Drawing_CreateTypographyStyle();
OH_Drawing_FontCollection *fontCollection = OH_Drawing_CreateFontCollection();
OH_Drawing_TextStyle *txtStyle = OH_Drawing_CreateTextStyle();
OH_Drawing_TypographyCreate *handler = OH_Drawing_CreateTypographyHandler(typoStyle, fontCollection);
const char *text = "OpenHarmony";
OH_Drawing_TypographyHandlerPushTextStyle(handler, txtStyle);
OH_Drawing_TypographyHandlerAddText(handler, text);
OH_Drawing_Typography *typography = OH_Drawing_CreateTypography(handler);
OH_Drawing_TextStyle *style = OH_Drawing_CreateTextStyle();
OH_Drawing_Font_Metrics *fontmetrics = nullptr;
NAPI_ASSERT(env, typography != nullptr, "Typography create failed");
NAPI_ASSERT(env, style != nullptr, "TextStyle create failed");
bool ret = OH_Drawing_TextStyleGetFontMetrics(typography, style, fontmetrics);
NAPI_ASSERT(env, ret == false, "GetFontMetrics004 failed: ret != false");
OH_Drawing_DestroyTypography(typography);
OH_Drawing_DestroyTypographyHandler(handler);
OH_Drawing_DestroyTypographyStyle(typoStyle);
OH_Drawing_DestroyFontCollection(fontCollection);
OH_Drawing_DestroyTextStyle(txtStyle);
OH_Drawing_DestroyTextStyle(style);
napi_create_int32(env, SUCCESS, &result);
return result;
}
* @brief OHDrawingTextStyleGetFontMetrics005:反向用例-多参数为空
* @details 验证typography+style同时为空时,接口仍返回false(参数校验鲁棒性)
*/
napi_value OHDrawingTextStyleGetFontMetrics005(napi_env env, napi_callback_info info)
{
napi_value result = nullptr;
OH_Drawing_Typography *typography = nullptr;
OH_Drawing_TextStyle *style = nullptr;
OH_Drawing_Font_Metrics fontmetrics = {0};
bool ret = OH_Drawing_TextStyleGetFontMetrics(typography, style, &fontmetrics);
NAPI_ASSERT(env, ret == false, "GetFontMetrics005 failed: ret != false");
napi_create_int32(env, SUCCESS, &result);
return result;
}
* @brief OHDrawingTypographyGetLineInfo001:正向用例-全合法参数
* @details 验证有效typography+合法行号+有效LineMetrics时,接口返回true
*/
napi_value OHDrawingTypographyGetLineInfo001(napi_env env, napi_callback_info info)
{
napi_value result = nullptr;
OH_Drawing_TypographyStyle *typoStyle = OH_Drawing_CreateTypographyStyle();
OH_Drawing_FontCollection *fontCollection = OH_Drawing_CreateFontCollection();
OH_Drawing_TextStyle *txtStyle = OH_Drawing_CreateTextStyle();
OH_Drawing_TypographyCreate *handler = OH_Drawing_CreateTypographyHandler(typoStyle, fontCollection);
const char *text = "OpenHarmony\nTypographyLineInfo";
OH_Drawing_TypographyHandlerPushTextStyle(handler, txtStyle);
OH_Drawing_TypographyHandlerAddText(handler, text);
OH_Drawing_Typography *typography = OH_Drawing_CreateTypography(handler);
OH_Drawing_TypographyLayout(typography, MAX_WIDTH);
int lineNumber = 0;
bool oneLine = true;
bool includeWhitespace = true;
OH_Drawing_LineMetrics lineMetrics = {0};
NAPI_ASSERT(env, typography != nullptr, "Typography create failed");
bool ret = OH_Drawing_TypographyGetLineInfo(typography, lineNumber, oneLine, includeWhitespace, &lineMetrics);
NAPI_ASSERT(env, ret == true, "GetLineInfo001 failed: ret != true");
OH_Drawing_DestroyTypography(typography);
OH_Drawing_DestroyTypographyHandler(handler);
OH_Drawing_DestroyTypographyStyle(typoStyle);
OH_Drawing_DestroyFontCollection(fontCollection);
OH_Drawing_DestroyTextStyle(txtStyle);
napi_create_int32(env, SUCCESS, &result);
return result;
}
* @brief OHDrawingTypographyGetLineInfo002:反向用例-空Typography对象
* @details 验证typography为空时,接口返回false
*/
napi_value OHDrawingTypographyGetLineInfo002(napi_env env, napi_callback_info info)
{
napi_value result = nullptr;
OH_Drawing_Typography *typography = nullptr;
int lineNumber = 0;
bool oneLine = true;
bool includeWhitespace = true;
OH_Drawing_LineMetrics lineMetrics = {0};
bool ret = OH_Drawing_TypographyGetLineInfo(typography, lineNumber, oneLine, includeWhitespace, &lineMetrics);
NAPI_ASSERT(env, ret == false, "GetLineInfo002 failed: ret != false");
napi_create_int32(env, SUCCESS, &result);
return result;
}
* @brief OHDrawingTypographyGetLineInfo003:反向用例-空LineMetrics指针
* @details 验证drawingLineMetrics为空时,接口返回false
*/
napi_value OHDrawingTypographyGetLineInfo003(napi_env env, napi_callback_info info)
{
napi_value result = nullptr;
OH_Drawing_TypographyStyle *typoStyle = OH_Drawing_CreateTypographyStyle();
OH_Drawing_FontCollection *fontCollection = OH_Drawing_CreateFontCollection();
OH_Drawing_TextStyle *txtStyle = OH_Drawing_CreateTextStyle();
OH_Drawing_TypographyCreate *handler = OH_Drawing_CreateTypographyHandler(typoStyle, fontCollection);
const char *text = "OpenHarmony";
OH_Drawing_TypographyHandlerPushTextStyle(handler, txtStyle);
OH_Drawing_TypographyHandlerAddText(handler, text);
OH_Drawing_Typography *typography = OH_Drawing_CreateTypography(handler);
OH_Drawing_TypographyLayout(typography, MAX_WIDTH);
int lineNumber = 0;
bool oneLine = true;
bool includeWhitespace = true;
OH_Drawing_LineMetrics *lineMetrics = nullptr;
NAPI_ASSERT(env, typography != nullptr, "Typography create failed");
bool ret = OH_Drawing_TypographyGetLineInfo(typography, lineNumber, oneLine, includeWhitespace, lineMetrics);
NAPI_ASSERT(env, ret == false, "GetLineInfo003 failed: ret != false");
OH_Drawing_DestroyTypography(typography);
OH_Drawing_DestroyTypographyHandler(handler);
OH_Drawing_DestroyTypographyStyle(typoStyle);
OH_Drawing_DestroyFontCollection(fontCollection);
OH_Drawing_DestroyTextStyle(txtStyle);
napi_create_int32(env, SUCCESS, &result);
return result;
}
* @brief OHDrawingTypographyGetLineInfo004:反向用例-非法行号
* @details 验证行号超出文本行数时,接口返回false(原生GetLineInfo失败)
*/
napi_value OHDrawingTypographyGetLineInfo004(napi_env env, napi_callback_info info)
{
napi_value result = nullptr;
OH_Drawing_TypographyStyle *typoStyle = OH_Drawing_CreateTypographyStyle();
OH_Drawing_FontCollection *fontCollection = OH_Drawing_CreateFontCollection();
OH_Drawing_TextStyle *txtStyle = OH_Drawing_CreateTextStyle();
OH_Drawing_TypographyCreate *handler = OH_Drawing_CreateTypographyHandler(typoStyle, fontCollection);
const char *text = "SingleLineText";
OH_Drawing_TypographyHandlerPushTextStyle(handler, txtStyle);
OH_Drawing_TypographyHandlerAddText(handler, text);
OH_Drawing_Typography *typography = OH_Drawing_CreateTypography(handler);
OH_Drawing_TypographyLayout(typography, MAX_WIDTH);
int lineNumber = 1;
bool oneLine = true;
bool includeWhitespace = true;
OH_Drawing_LineMetrics lineMetrics = {0};
NAPI_ASSERT(env, typography != nullptr, "Typography create failed");
bool ret = OH_Drawing_TypographyGetLineInfo(typography, lineNumber, oneLine, includeWhitespace, &lineMetrics);
NAPI_ASSERT(env, ret == false, "GetLineInfo004 failed: ret != false");
OH_Drawing_DestroyTypography(typography);
OH_Drawing_DestroyTypographyHandler(handler);
OH_Drawing_DestroyTypographyStyle(typoStyle);
OH_Drawing_DestroyFontCollection(fontCollection);
OH_Drawing_DestroyTextStyle(txtStyle);
napi_create_int32(env, SUCCESS, &result);
return result;
}
* @brief OHDrawingTypographyGetLineInfo005:反向用例-未布局的Typography
* @details 验证Typography未执行Layout时,原生GetLineInfo失败,接口返回false
*/
napi_value OHDrawingTypographyGetLineInfo005(napi_env env, napi_callback_info info)
{
napi_value result = nullptr;
OH_Drawing_TypographyStyle *typoStyle = OH_Drawing_CreateTypographyStyle();
OH_Drawing_FontCollection *fontCollection = OH_Drawing_CreateFontCollection();
OH_Drawing_TextStyle *txtStyle = OH_Drawing_CreateTextStyle();
OH_Drawing_TypographyCreate *handler = OH_Drawing_CreateTypographyHandler(typoStyle, fontCollection);
const char *text = "UnlayoutText";
OH_Drawing_TypographyHandlerPushTextStyle(handler, txtStyle);
OH_Drawing_TypographyHandlerAddText(handler, text);
OH_Drawing_Typography *typography = OH_Drawing_CreateTypography(handler);
int lineNumber = 0;
bool oneLine = true;
bool includeWhitespace = true;
OH_Drawing_LineMetrics lineMetrics = {0};
NAPI_ASSERT(env, typography != nullptr, "Typography create failed");
bool ret = OH_Drawing_TypographyGetLineInfo(typography, lineNumber, oneLine, includeWhitespace, &lineMetrics);
NAPI_ASSERT(env, ret == false, "GetLineInfo005 failed: ret != false");
OH_Drawing_DestroyTypography(typography);
OH_Drawing_DestroyTypographyHandler(handler);
OH_Drawing_DestroyTypographyStyle(typoStyle);
OH_Drawing_DestroyFontCollection(fontCollection);
OH_Drawing_DestroyTextStyle(txtStyle);
napi_create_int32(env, SUCCESS, &result);
return result;
}
* @brief OHDrawingTextStyleGetShadowWithIndex001:正向用例-全合法参数
* @details 验证有效TextStyle+合法索引(存在阴影)时,接口返回非空TextShadow指针
*/
napi_value OHDrawingTextStyleGetShadowWithIndex001(napi_env env, napi_callback_info info)
{
napi_value result = nullptr;
OH_Drawing_TextShadow *shadow = OH_Drawing_CreateTextShadow();
NAPI_ASSERT(env, shadow != nullptr, "TextShadow create failed");
OH_Drawing_Point *offset = OH_Drawing_PointCreate(10, 10);
uint32_t color = OH_Drawing_ColorSetArgb(0xFF, 0x00, 0xFF, 0x00);
double blurRadius = 20.0;
OH_Drawing_SetTextShadow(shadow, color, offset, blurRadius);
OH_Drawing_TextStyle *textStyle = OH_Drawing_CreateTextStyle();
NAPI_ASSERT(env, textStyle != nullptr, "TextStyle create failed");
OH_Drawing_TextStyleAddShadow(textStyle, shadow);
int index = 0;
OH_Drawing_TextShadow *getShadow = OH_Drawing_TextStyleGetShadowWithIndex(textStyle, 0);
NAPI_ASSERT(env, getShadow != nullptr, "Valid index should return non-null shadow");
OH_Drawing_PointDestroy(offset);
OH_Drawing_DestroyTextShadow(shadow);
OH_Drawing_DestroyTextStyle(textStyle);
napi_create_int32(env, SUCCESS, &result);
return result;
}
* @brief OHDrawingTextStyleGetShadowWithIndex002:反向用例-空TextStyle对象
* @details 验证style为空时,接口返回nullptr
*/
napi_value OHDrawingTextStyleGetShadowWithIndex002(napi_env env, napi_callback_info info)
{
napi_value result = nullptr;
OH_Drawing_TextStyle *style = nullptr;
int index = 0;
OH_Drawing_TextShadow *shadow = OH_Drawing_TextStyleGetShadowWithIndex(style, index);
NAPI_ASSERT(env, shadow == nullptr, "GetShadowWithIndex002 failed: shadow is not nullptr");
napi_create_int32(env, SUCCESS, &result);
return result;
}
* @brief OHDrawingTextStyleGetShadowWithIndex003:反向用例-负索引
* @details 验证index<0时,接口返回nullptr
*/
napi_value OHDrawingTextStyleGetShadowWithIndex003(napi_env env, napi_callback_info info)
{
napi_value result = nullptr;
OH_Drawing_TextStyle *style = OH_Drawing_CreateTextStyle();
NAPI_ASSERT(env, style != nullptr, "TextStyle create failed");
int index = -1;
OH_Drawing_TextStyleGetShadowWithIndex(style, index);
OH_Drawing_TextShadow *shadow = OH_Drawing_TextStyleGetShadowWithIndex(style, index);
NAPI_ASSERT(env, shadow == nullptr, "GetShadowWithIndex003 failed: shadow is not nullptr");
OH_Drawing_DestroyTextStyle(style);
napi_create_int32(env, SUCCESS, &result);
return result;
}
* @brief OHDrawingTextStyleGetShadowWithIndex004:反向用例-索引超出范围
* @details 验证index>=阴影列表长度时,接口返回nullptr
*/
napi_value OHDrawingTextStyleGetShadowWithIndex004(napi_env env, napi_callback_info info)
{
napi_value result = nullptr;
OH_Drawing_TextStyle *style = OH_Drawing_CreateTextStyle();
NAPI_ASSERT(env, style != nullptr, "TextStyle create failed");
int index = 0;
OH_Drawing_TextShadow *shadow = OH_Drawing_TextStyleGetShadowWithIndex(style, index);
NAPI_ASSERT(env, shadow == nullptr, "GetShadowWithIndex004 failed: shadow is not nullptr");
OH_Drawing_DestroyTextStyle(style);
napi_create_int32(env, SUCCESS, &result);
return result;
}
* @brief OHDrawingTypographyTextGetHeightBehavior001:正向用例-有效TypographyStyle
* @details 验证有效style时,返回正确的TextHeightBehavior值
*/
napi_value OHDrawingTypographyTextGetHeightBehavior001(napi_env env, napi_callback_info info)
{
napi_value result = nullptr;
OH_Drawing_TypographyStyle *typoStyle = OH_Drawing_CreateTypographyStyle();
OH_Drawing_TypographyTextSetHeightBehavior(typoStyle, TEXT_HEIGHT_ALL);
OH_Drawing_TextHeightBehavior behavior = OH_Drawing_TypographyTextGetHeightBehavior(typoStyle);
NAPI_ASSERT(env, behavior == TEXT_HEIGHT_ALL, "Valid style should return TEXT_HEIGHT_ALL");
OH_Drawing_TypographyTextSetHeightBehavior(typoStyle, TEXT_HEIGHT_DISABLE_FIRST_ASCENT);
behavior = OH_Drawing_TypographyTextGetHeightBehavior(typoStyle);
NAPI_ASSERT(env, behavior == TEXT_HEIGHT_DISABLE_FIRST_ASCENT,
"Valid style should return TEXT_HEIGHT_DISABLE_FIRST_ASCENT");
OH_Drawing_TypographyTextSetHeightBehavior(typoStyle, TEXT_HEIGHT_DISABLE_LAST_ASCENT);
behavior = OH_Drawing_TypographyTextGetHeightBehavior(typoStyle);
NAPI_ASSERT(env, behavior == TEXT_HEIGHT_DISABLE_LAST_ASCENT,
"Valid style should return TEXT_HEIGHT_DISABLE_LAST_ASCENT");
OH_Drawing_TypographyTextSetHeightBehavior(typoStyle, TEXT_HEIGHT_DISABLE_ALL);
behavior = OH_Drawing_TypographyTextGetHeightBehavior(typoStyle);
NAPI_ASSERT(env, behavior == TEXT_HEIGHT_DISABLE_ALL, "Valid style should return TEXT_HEIGHT_DISABLE_ALL");
OH_Drawing_DestroyTypographyStyle(typoStyle);
napi_create_int32(env, SUCCESS, &result);
return result;
}
* @brief OHDrawingTypographyTextGetHeightBehavior002:反向用例-空TypographyStyle
* @details 验证style为空时,返回默认值TEXT_HEIGHT_ALL
*/
napi_value OHDrawingTypographyTextGetHeightBehavior002(napi_env env, napi_callback_info info)
{
napi_value result = nullptr;
OH_Drawing_TypographyStyle *style = nullptr;
OH_Drawing_TextHeightBehavior behavior = OH_Drawing_TypographyTextGetHeightBehavior(style);
NAPI_ASSERT(env, behavior == TEXT_HEIGHT_ALL, "Null style should return default TEXT_HEIGHT_ALL");
napi_create_int32(env, SUCCESS, &result);
return result;
}
* @brief OHDrawingTextStyleGetDecorationStyle001:反向用例-覆盖所有异常分支
* @details 验证style为空/原生对象为空时,均返回默认值TEXT_DECORATION_STYLE_SOLID
*/
napi_value OHDrawingTextStyleGetDecorationStyle001(napi_env env, napi_callback_info info)
{
napi_value result = nullptr;
OH_Drawing_TextDecorationStyle res1 = OH_Drawing_TextStyleGetDecorationStyle(nullptr);
NAPI_ASSERT(env, res1 == TEXT_DECORATION_STYLE_SOLID, "Null style should return TEXT_DECORATION_STYLE_SOLID");
napi_create_int32(env, SUCCESS, &result);
return result;
}
* @brief OHDrawingTextStyleGetDecorationStyle002:正向用例-有效参数分支
* @details 验证style和原生对象均有效时,返回设置的decorationStyle值
*/
napi_value OHDrawingTextStyleGetDecorationStyle002(napi_env env, napi_callback_info info)
{
napi_value result = nullptr;
OH_Drawing_TextStyle *txtStyle = OH_Drawing_CreateTextStyle();
NAPI_ASSERT(env, txtStyle != nullptr, "Failed to create valid TextStyle");
OH_Drawing_SetTextStyleColor(txtStyle, 1);
NAPI_ASSERT(env, OH_Drawing_TextStyleGetColor(txtStyle) == 1, "Text color should be 1");
OH_Drawing_SetTextStyleDecorationStyle(txtStyle, TEXT_DECORATION_STYLE_SOLID);
OH_Drawing_TextDecorationStyle decoStyle = OH_Drawing_TextStyleGetDecorationStyle(txtStyle);
NAPI_ASSERT(env, decoStyle == TEXT_DECORATION_STYLE_SOLID,
"Decoration style should be TEXT_DECORATION_STYLE_SOLID");
OH_Drawing_SetTextStyleDecorationStyle(txtStyle, TEXT_DECORATION_STYLE_DOUBLE);
decoStyle = OH_Drawing_TextStyleGetDecorationStyle(txtStyle);
NAPI_ASSERT(env, decoStyle == TEXT_DECORATION_STYLE_DOUBLE,
"Decoration style should be TEXT_DECORATION_STYLE_DOUBLE");
OH_Drawing_SetTextStyleDecorationStyle(txtStyle, TEXT_DECORATION_STYLE_DOTTED);
decoStyle = OH_Drawing_TextStyleGetDecorationStyle(txtStyle);
NAPI_ASSERT(env, decoStyle == TEXT_DECORATION_STYLE_DOTTED,
"Decoration style should be TEXT_DECORATION_STYLE_DOTTED");
OH_Drawing_SetTextStyleDecorationStyle(txtStyle, TEXT_DECORATION_STYLE_DASHED);
decoStyle = OH_Drawing_TextStyleGetDecorationStyle(txtStyle);
NAPI_ASSERT(env, decoStyle == TEXT_DECORATION_STYLE_DASHED,
"Decoration style should be TEXT_DECORATION_STYLE_DASHED");
OH_Drawing_SetTextStyleDecorationStyle(txtStyle, TEXT_DECORATION_STYLE_WAVY);
decoStyle = OH_Drawing_TextStyleGetDecorationStyle(txtStyle);
NAPI_ASSERT(env, decoStyle == TEXT_DECORATION_STYLE_WAVY, "Decoration style should be TEXT_DECORATION_STYLE_WAVY");
NAPI_ASSERT(env, OH_Drawing_TextStyleGetDecorationStyle(nullptr) == TEXT_DECORATION_STYLE_SOLID,
"Null param should return TEXT_DECORATION_STYLE_SOLID");
OH_Drawing_DestroyTextStyle(txtStyle);
napi_create_int32(env, SUCCESS, &result);
return result;
}
* @brief OHDrawingTextStyleGetFontWeight001:反向用例-覆盖所有异常分支
* @details 验证style为空/原生对象为空时,均返回默认值FONT_WEIGHT_400
*/
napi_value OHDrawingTextStyleGetFontWeight001(napi_env env, napi_callback_info info)
{
napi_value result = nullptr;
OH_Drawing_FontWeight res1 = OH_Drawing_TextStyleGetFontWeight(nullptr);
NAPI_ASSERT(env, res1 == FONT_WEIGHT_400, "Null style should return FONT_WEIGHT_400");
napi_create_int32(env, 0, &result);
return result;
}
* @brief OHDrawingTextStyleGetFontWeight002:正向用例-有效参数分支
* @details 验证style和原生对象均有效时,返回设置的fontWeight值
*/
napi_value OHDrawingTextStyleGetFontWeight002(napi_env env, napi_callback_info info)
{
napi_value result = nullptr;
OH_Drawing_TextStyle *style = OH_Drawing_CreateTextStyle();
NAPI_ASSERT(env, style != nullptr, "TextStyle create failed");
OH_Drawing_SetTextStyleFontWeight(style, FONT_WEIGHT_100);
OH_Drawing_FontWeight res = OH_Drawing_TextStyleGetFontWeight(style);
NAPI_ASSERT(env, res == FONT_WEIGHT_100, "Valid style should return set FONT_WEIGHT_100");
OH_Drawing_SetTextStyleFontWeight(style, FONT_WEIGHT_200);
res = OH_Drawing_TextStyleGetFontWeight(style);
NAPI_ASSERT(env, res == FONT_WEIGHT_200, "Valid style should return set FONT_WEIGHT_200");
OH_Drawing_SetTextStyleFontWeight(style, FONT_WEIGHT_300);
res = OH_Drawing_TextStyleGetFontWeight(style);
NAPI_ASSERT(env, res == FONT_WEIGHT_300, "Valid style should return set FONT_WEIGHT_300");
OH_Drawing_SetTextStyleFontWeight(style, FONT_WEIGHT_400);
res = OH_Drawing_TextStyleGetFontWeight(style);
NAPI_ASSERT(env, res == FONT_WEIGHT_400, "Valid style should return set FONT_WEIGHT_400");
OH_Drawing_SetTextStyleFontWeight(style, FONT_WEIGHT_500);
res = OH_Drawing_TextStyleGetFontWeight(style);
NAPI_ASSERT(env, res == FONT_WEIGHT_500, "Valid style should return set FONT_WEIGHT_500");
OH_Drawing_SetTextStyleFontWeight(style, FONT_WEIGHT_600);
res = OH_Drawing_TextStyleGetFontWeight(style);
NAPI_ASSERT(env, res == FONT_WEIGHT_600, "Valid style should return set FONT_WEIGHT_600");
OH_Drawing_SetTextStyleFontWeight(style, FONT_WEIGHT_700);
res = OH_Drawing_TextStyleGetFontWeight(style);
NAPI_ASSERT(env, res == FONT_WEIGHT_700, "Valid style should return set FONT_WEIGHT_700");
OH_Drawing_SetTextStyleFontWeight(style, FONT_WEIGHT_800);
res = OH_Drawing_TextStyleGetFontWeight(style);
NAPI_ASSERT(env, res == FONT_WEIGHT_800, "Valid style should return set FONT_WEIGHT_800");
OH_Drawing_SetTextStyleFontWeight(style, FONT_WEIGHT_900);
res = OH_Drawing_TextStyleGetFontWeight(style);
NAPI_ASSERT(env, res == FONT_WEIGHT_900, "Valid style should return set FONT_WEIGHT_900");
OH_Drawing_DestroyTextStyle(style);
napi_create_int32(env, 0, &result);
return result;
}
* @brief OHDrawingTextStyleGetFontStyle001:反向用例-覆盖所有异常分支
* @details 验证style为空/原生对象为空时,均返回默认值FONT_STYLE_NORMAL
*/
napi_value OHDrawingTextStyleGetFontStyle001(napi_env env, napi_callback_info info)
{
napi_value result = nullptr;
OH_Drawing_FontStyle res1 = OH_Drawing_TextStyleGetFontStyle(nullptr);
NAPI_ASSERT(env, res1 == FONT_STYLE_NORMAL, "Null style should return FONT_STYLE_NORMAL");
napi_create_int32(env, SUCCESS, &result);
return result;
}
* @brief OHDrawingTextStyleGetFontStyle002:正向用例-有效参数分支
* @details 验证style和原生对象均有效时,返回设置的fontStyle值
*/
napi_value OHDrawingTextStyleGetFontStyle002(napi_env env, napi_callback_info info)
{
napi_value result = nullptr;
OH_Drawing_TextStyle *style = OH_Drawing_CreateTextStyle();
NAPI_ASSERT(env, style != nullptr, "TextStyle create failed");
OH_Drawing_SetTextStyleFontStyle(style, FONT_STYLE_NORMAL);
OH_Drawing_FontStyle res = OH_Drawing_TextStyleGetFontStyle(style);
NAPI_ASSERT(env, res == FONT_STYLE_NORMAL, "Valid style should return set FONT_STYLE_NORMAL");
OH_Drawing_SetTextStyleFontStyle(style, FONT_STYLE_ITALIC);
res = OH_Drawing_TextStyleGetFontStyle(style);
NAPI_ASSERT(env, res == FONT_STYLE_ITALIC, "Valid style should return set FONT_STYLE_ITALIC");
OH_Drawing_SetTextStyleFontStyle(style, FONT_STYLE_OBLIQUE);
res = OH_Drawing_TextStyleGetFontStyle(style);
NAPI_ASSERT(env, res == FONT_STYLE_ITALIC, "Valid style FONT_STYLE_OBLIQUE should return set FONT_STYLE_ITALIC");
OH_Drawing_DestroyTextStyle(style);
napi_create_int32(env, SUCCESS, &result);
return result;
}
* @brief OHDrawingTextStyleGetBaseline001:反向用例-覆盖所有异常分支
* @details 验证style为空/原生对象为空时,均返回默认值TEXT_BASELINE_ALPHABETIC
*/
napi_value OHDrawingTextStyleGetBaseline001(napi_env env, napi_callback_info info)
{
napi_value result = nullptr;
OH_Drawing_TextBaseline res1 = OH_Drawing_TextStyleGetBaseline(nullptr);
NAPI_ASSERT(env, res1 == TEXT_BASELINE_ALPHABETIC, "Null style should return TEXT_BASELINE_ALPHABETIC");
napi_create_int32(env, SUCCESS, &result);
return result;
}
* @brief OHDrawingTextStyleGetBaseline002:正向用例-有效参数分支
* @details 验证style和原生对象均有效时,返回设置的baseline值
*/
napi_value OHDrawingTextStyleGetBaseline002(napi_env env, napi_callback_info info)
{
napi_value result = nullptr;
OH_Drawing_TextStyle *style = OH_Drawing_CreateTextStyle();
NAPI_ASSERT(env, style != nullptr, "TextStyle create failed");
OH_Drawing_SetTextStyleBaseLine(style, TEXT_BASELINE_IDEOGRAPHIC);
OH_Drawing_TextBaseline res = OH_Drawing_TextStyleGetBaseline(style);
NAPI_ASSERT(env, res == TEXT_BASELINE_IDEOGRAPHIC, "Valid style should return set TEXT_BASELINE_MIDDLE");
OH_Drawing_SetTextStyleBaseLine(style, TEXT_BASELINE_ALPHABETIC);
res = OH_Drawing_TextStyleGetBaseline(style);
NAPI_ASSERT(env, res == TEXT_BASELINE_ALPHABETIC, "Valid style should return set TEXT_BASELINE_ALPHABETIC");
OH_Drawing_DestroyTextStyle(style);
napi_create_int32(env, SUCCESS, &result);
return result;
}
* @brief OHDrawingTextStyleGetHalfLeading001:反向用例-空参数场景
* @details 验证style为nullptr时,返回默认值false
*/
napi_value OHDrawingTextStyleGetHalfLeading001(napi_env env, napi_callback_info info)
{
napi_value result = nullptr;
bool res = OH_Drawing_TextStyleGetHalfLeading(nullptr);
NAPI_ASSERT(env, res == false, "Null style should return false");
napi_create_int32(env, SUCCESS, &result);
return result;
}
* @brief OHDrawingTextStyleGetHalfLeading002:正向用例-有效参数场景
* @details 验证有效style时,返回设置的halfLeading值
*/
napi_value OHDrawingTextStyleGetHalfLeading002(napi_env env, napi_callback_info info)
{
napi_value result = nullptr;
OH_Drawing_TextStyle *style = OH_Drawing_CreateTextStyle();
NAPI_ASSERT(env, style != nullptr, "TextStyle create failed");
bool targetHalfLeading = true;
OH_Drawing_SetTextStyleHalfLeading(style, targetHalfLeading);
bool res = OH_Drawing_TextStyleGetHalfLeading(style);
NAPI_ASSERT(env, res == targetHalfLeading, "Valid style should return set true");
OH_Drawing_DestroyTextStyle(style);
napi_create_int32(env, SUCCESS, &result);
return result;
}
* @brief OHDrawingTextStyleIsPlaceholder001:反向用例-空参数场景
* @details 验证style为nullptr时,返回默认值false(参考代码隐含空参数逻辑)
*/
napi_value OHDrawingTextStyleIsPlaceholder001(napi_env env, napi_callback_info info)
{
napi_value result = nullptr;
bool res = OH_Drawing_TextStyleIsPlaceholder(nullptr);
NAPI_ASSERT(env, res == false, "Null style should return false");
napi_create_int32(env, SUCCESS, &result);
return result;
}
* @brief OHDrawingTextStyleIsPlaceholder002:正向用例-匹配参考代码完整逻辑
* @details 复刻参考代码:创建Style->验证默认值false->调用SetPlaceholder->验证值true
*/
napi_value OHDrawingTextStyleIsPlaceholder002(napi_env env, napi_callback_info info)
{
napi_value result = nullptr;
OH_Drawing_TextStyle *txtStyle = OH_Drawing_CreateTextStyle();
NAPI_ASSERT(env, txtStyle != nullptr, "TextStyle create failed");
bool initRes = OH_Drawing_TextStyleIsPlaceholder(txtStyle);
NAPI_ASSERT(env, initRes == false, "New TextStyle should return false for IsPlaceholder");
OH_Drawing_TextStyleSetPlaceholder(nullptr);
OH_Drawing_TextStyleSetPlaceholder(txtStyle);
bool setRes = OH_Drawing_TextStyleIsPlaceholder(txtStyle);
NAPI_ASSERT(env, setRes == true, "TextStyle should return true after SetPlaceholder");
OH_Drawing_DestroyTextStyle(txtStyle);
napi_create_int32(env, SUCCESS, &result);
return result;
}
* @brief OHDrawingTypographyStyleGetEffectiveAlignment001:反向用例-空参数场景
* @details 验证style为空/原生对象为空时,返回默认值TEXT_ALIGN_START
*/
napi_value OHDrawingTypographyStyleGetEffectiveAlignment001(napi_env env, napi_callback_info info)
{
napi_value result = nullptr;
OH_Drawing_TextAlign res = OH_Drawing_TypographyStyleGetEffectiveAlignment(nullptr);
NAPI_ASSERT(env, res == TEXT_ALIGN_START, "Null style should return TEXT_ALIGN_START");
napi_create_int32(env, SUCCESS, &result);
return result;
}
* @brief OHDrawingTypographyStyleGetEffectiveAlignment002:正向用例-有效参数场景
* @details 验证有效style时,返回设置的EffectiveAlignment值
*/
napi_value OHDrawingTypographyStyleGetEffectiveAlignment002(napi_env env, napi_callback_info info)
{
napi_value result = nullptr;
OH_Drawing_TypographyStyle *fTypoStyle = OH_Drawing_CreateTypographyStyle();
NAPI_ASSERT(env, fTypoStyle != nullptr, "TypographyStyle create failed");
OH_Drawing_SetTypographyTextAlign(fTypoStyle, TEXT_ALIGN_START);
OH_Drawing_SetTypographyTextDirection(fTypoStyle, TEXT_DIRECTION_LTR);
OH_Drawing_TextAlign align = OH_Drawing_TypographyStyleGetEffectiveAlignment(fTypoStyle);
NAPI_ASSERT(env, align == TEXT_ALIGN_LEFT, "TEXT_ALIGN_START + LTR should return TEXT_ALIGN_LEFT");
OH_Drawing_SetTypographyTextDirection(fTypoStyle, TEXT_DIRECTION_RTL);
align = OH_Drawing_TypographyStyleGetEffectiveAlignment(fTypoStyle);
NAPI_ASSERT(env, align == TEXT_ALIGN_RIGHT, "TEXT_ALIGN_START + RTL should return TEXT_ALIGN_RIGHT");
OH_Drawing_SetTypographyTextAlign(fTypoStyle, TEXT_ALIGN_END);
OH_Drawing_SetTypographyTextDirection(fTypoStyle, TEXT_DIRECTION_LTR);
align = OH_Drawing_TypographyStyleGetEffectiveAlignment(fTypoStyle);
NAPI_ASSERT(env, align == TEXT_ALIGN_RIGHT, "TEXT_ALIGN_END + LTR should return TEXT_ALIGN_RIGHT");
OH_Drawing_SetTypographyTextDirection(fTypoStyle, TEXT_DIRECTION_RTL);
align = OH_Drawing_TypographyStyleGetEffectiveAlignment(fTypoStyle);
NAPI_ASSERT(env, align == TEXT_ALIGN_LEFT, "TEXT_ALIGN_END + RTL should return TEXT_ALIGN_LEFT");
OH_Drawing_SetTypographyTextAlign(fTypoStyle, TEXT_ALIGN_CENTER);
align = OH_Drawing_TypographyStyleGetEffectiveAlignment(fTypoStyle);
NAPI_ASSERT(env, align == TEXT_ALIGN_CENTER, "TEXT_ALIGN_CENTER should return TEXT_ALIGN_CENTER");
OH_Drawing_SetTypographyTextAlign(fTypoStyle, TEXT_ALIGN_JUSTIFY);
align = OH_Drawing_TypographyStyleGetEffectiveAlignment(fTypoStyle);
NAPI_ASSERT(env, align == TEXT_ALIGN_JUSTIFY, "TEXT_ALIGN_JUSTIFY should return TEXT_ALIGN_JUSTIFY");
OH_Drawing_DestroyTypographyStyle(fTypoStyle);
napi_create_int32(env, SUCCESS, &result);
return result;
}
* @brief OHDrawingTypographyDidExceedMaxLines001:反向用例-空参数场景
* @details 验证typography为nullptr时,返回默认值false
*/
napi_value OHDrawingTypographyDidExceedMaxLines001(napi_env env, napi_callback_info info)
{
napi_value result = nullptr;
bool ret = OH_Drawing_TypographyDidExceedMaxLines(nullptr);
if (ret != false) {
napi_create_int32(env, FAIL, &result);
return result;
}
napi_create_int32(env, SUCCESS, &result);
return result;
}
* @brief OHDrawingTypographyDidExceedMaxLines002:正向用例-未超出最大行数
* @details 验证有效typography且文本行数<=最大行数时,返回false
*/
napi_value OHDrawingTypographyDidExceedMaxLines002(napi_env env, napi_callback_info info)
{
napi_value result = nullptr;
OH_Drawing_TypographyStyle *typoStyle = OH_Drawing_CreateTypographyStyle();
OH_Drawing_TextStyle *txtStyle = OH_Drawing_CreateTextStyle();
OH_Drawing_TypographyCreate *handler =
OH_Drawing_CreateTypographyHandler(typoStyle, OH_Drawing_CreateFontCollection());
NAPI_ASSERT(env, handler != nullptr, "TypographyHandler create failed");
OH_Drawing_TypographyHandlerPushTextStyle(handler, txtStyle);
const char *text = "OpenHarmony\n";
OH_Drawing_TypographyHandlerAddText(handler, text);
OH_Drawing_PlaceholderSpan placeholderSpan = {20, 40, ALIGNMENT_OFFSET_AT_BASELINE, TEXT_BASELINE_ALPHABETIC, 10};
OH_Drawing_TypographyHandlerAddPlaceholder(handler, &placeholderSpan);
OH_Drawing_TypographyHandlerPopTextStyle(handler);
OH_Drawing_Typography *typography = OH_Drawing_CreateTypography(handler);
double maxWidth = 2160.0;
OH_Drawing_TypographyLayout(typography, maxWidth);
double position[2] = {10.0, 15.0};
OH_Drawing_Bitmap *cBitmap = OH_Drawing_BitmapCreate();
OH_Drawing_BitmapFormat cFormat{COLOR_FORMAT_RGBA_8888, ALPHA_FORMAT_OPAQUE};
uint32_t width = 20;
uint32_t height = 40;
OH_Drawing_BitmapBuild(cBitmap, width, height, &cFormat);
OH_Drawing_Canvas *cCanvas = OH_Drawing_CanvasCreate();
OH_Drawing_CanvasBind(cCanvas, cBitmap);
OH_Drawing_CanvasClear(cCanvas, OH_Drawing_ColorSetArgb(0xFF, 0xFF, 0xFF, 0xFF));
NAPI_ASSERT(env, OH_Drawing_TypographyDidExceedMaxLines(typography) != true,
"Typography should not exceed max lines");
OH_Drawing_RectHeightStyle heightStyle = RECT_HEIGHT_STYLE_TIGHT;
OH_Drawing_RectWidthStyle widthStyle = RECT_WIDTH_STYLE_TIGHT;
size_t rangeStart = 1;
size_t rangeEnd = 2;
NAPI_ASSERT_NO_MES(
OH_Drawing_TypographyGetRectsForRange(typography, rangeStart, rangeEnd, heightStyle, widthStyle) != nullptr);
NAPI_ASSERT_NO_MES(OH_Drawing_TypographyGetRectsForPlaceholders(typography) != nullptr);
NAPI_ASSERT_NO_MES(OH_Drawing_TypographyGetGlyphPositionAtCoordinate(typography, 1, 0) != nullptr);
NAPI_ASSERT_NO_MES(OH_Drawing_TypographyGetGlyphPositionAtCoordinateWithCluster(typography, 1, 0) != nullptr);
NAPI_ASSERT_NO_MES(OH_Drawing_TypographyGetWordBoundary(typography, 1) != nullptr);
NAPI_ASSERT_NO_MES(OH_Drawing_TypographyGetLineTextRange(typography, 1, true) != nullptr);
NAPI_ASSERT_NO_MES(OH_Drawing_TypographyGetLineCount(typography) != 0);
OH_Drawing_TypographyPaint(typography, cCanvas, position[0], position[1]);
OH_Drawing_CanvasDestroy(cCanvas);
OH_Drawing_DestroyTypography(typography);
OH_Drawing_DestroyTypographyHandler(handler);
OH_Drawing_DestroyTextStyle(txtStyle);
OH_Drawing_DestroyTypographyStyle(typoStyle);
napi_create_int32(env, 0, &result);
return result;
}
* @brief OHDrawingTypographyDidExceedMaxLines003:正向用例-超出最大行数
* @details 验证有效typography且文本行数>最大行数时,返回true
*/
napi_value OHDrawingTypographyDidExceedMaxLines003(napi_env env, napi_callback_info info)
{
napi_value result = nullptr;
OH_Drawing_TypographyStyle *typoStyle = OH_Drawing_CreateTypographyStyle();
OH_Drawing_TextStyle *txtStyle = OH_Drawing_CreateTextStyle();
OH_Drawing_TypographyCreate *handler =
OH_Drawing_CreateTypographyHandler(typoStyle, OH_Drawing_CreateFontCollection());
NAPI_ASSERT(env, handler != nullptr, "Failed to create TypographyHandler");
OH_Drawing_TypographyHandlerPushTextStyle(handler, txtStyle);
const char *text = "OpenHarmony\n";
OH_Drawing_TypographyHandlerAddText(handler, text);
OH_Drawing_TypographyHandlerPopTextStyle(handler);
OH_Drawing_Typography *typography = OH_Drawing_CreateTypography(handler);
double maxWidth = 800.0;
OH_Drawing_TypographyLayout(typography, maxWidth);
double position[2] = {10.0, 15.0};
OH_Drawing_Bitmap *cBitmap = OH_Drawing_BitmapCreate();
OH_Drawing_BitmapFormat cFormat{COLOR_FORMAT_RGBA_8888, ALPHA_FORMAT_OPAQUE};
uint32_t width = 20;
uint32_t height = 40;
OH_Drawing_BitmapBuild(cBitmap, width, height, &cFormat);
OH_Drawing_Canvas *cCanvas = OH_Drawing_CanvasCreate();
OH_Drawing_CanvasBind(cCanvas, cBitmap);
OH_Drawing_CanvasClear(cCanvas, OH_Drawing_ColorSetArgb(0xFF, 0xFF, 0xFF, 0xFF));
NAPI_ASSERT(env, OH_Drawing_TypographyDidExceedMaxLines(typography) != true,
"Typography should not exceed max lines");
OH_Drawing_RectHeightStyle heightStyle = RECT_HEIGHT_STYLE_TIGHT;
OH_Drawing_RectWidthStyle widthStyle = RECT_WIDTH_STYLE_TIGHT;
auto box = OH_Drawing_TypographyGetRectsForRange(typography, 1, 2, heightStyle, widthStyle);
NAPI_ASSERT(env, box != nullptr, "TypographyGetRectsForRange return null");
OH_Drawing_GetRightFromTextBox(box, 0);
NAPI_ASSERT(env, OH_Drawing_TypographyGetRectsForPlaceholders(typography) != nullptr,
"TypographyGetRectsForPlaceholders return null");
NAPI_ASSERT(env, OH_Drawing_TypographyGetGlyphPositionAtCoordinate(typography, 1, 0) != nullptr,
"TypographyGetGlyphPositionAtCoordinate return null");
NAPI_ASSERT(env, OH_Drawing_TypographyGetGlyphPositionAtCoordinateWithCluster(typography, 1, 0) != nullptr,
"TypographyGetGlyphPositionAtCoordinateWithCluster return null");
NAPI_ASSERT(env, OH_Drawing_TypographyGetWordBoundary(typography, 1) != nullptr,
"TypographyGetWordBoundary return null");
NAPI_ASSERT(env, OH_Drawing_TypographyGetLineTextRange(typography, 1, true) != nullptr,
"TypographyGetLineTextRange return null");
NAPI_ASSERT(env, OH_Drawing_TypographyGetLineCount(typography) != 0, "TypographyGetLineCount return 0");
OH_Drawing_TypographyPaint(typography, cCanvas, position[0], position[1]);
OH_Drawing_CanvasDestroy(cCanvas);
OH_Drawing_DestroyTypography(typography);
OH_Drawing_DestroyTypographyHandler(handler);
OH_Drawing_DestroyTypographyStyle(typoStyle);
OH_Drawing_DestroyTextStyle(txtStyle);
napi_create_int32(env, SUCCESS, &result);
return result;
}
* @brief OHDrawingTypographyGetRectsForRange002:反向用例-空Typography调用
* @details 验证传入nullptr的Typography对象时,函数返回nullptr且无崩溃
*/
napi_value OHDrawingTypographyGetRectsForRange001(napi_env env, napi_callback_info info)
{
napi_value result = nullptr;
size_t start = 0;
size_t end = 5;
OH_Drawing_RectHeightStyle heightStyle = RECT_HEIGHT_STYLE_INCLUDELINESPACEMIDDLE;
OH_Drawing_RectWidthStyle widthStyle = RECT_WIDTH_STYLE_MAX;
OH_Drawing_TextBox *textBox = OH_Drawing_TypographyGetRectsForRange(nullptr, start, end, heightStyle, widthStyle);
NAPI_ASSERT(env, textBox == nullptr, "Null Typography should return nullptr");
napi_create_int32(env, SUCCESS, &result);
return result;
}
* @brief OHDrawingTypographyGetGlyphPositionAtCoordinate001:正向用例-有效参数获取PositionAndAffinity
* @details 验证有效Typography对象+合法坐标调用后返回非空PositionAndAffinity,且资源可正常释放
*/
napi_value OHDrawingTypographyGetGlyphPositionAtCoordinate001(napi_env env, napi_callback_info info)
{
napi_value result = nullptr;
OH_Drawing_TypographyStyle *fTypoStyle = OH_Drawing_CreateTypographyStyle();
NAPI_ASSERT(env, fTypoStyle != nullptr, "CreateTypographyStyle failed");
OH_Drawing_TypographyCreate *fHandler =
OH_Drawing_CreateTypographyHandler(fTypoStyle, OH_Drawing_CreateFontCollection());
NAPI_ASSERT(env, fHandler != nullptr, "OH_Drawing_CreateTypographyHandler failed");
OH_Drawing_Typography *fTypography = OH_Drawing_CreateTypography(fHandler);
NAPI_ASSERT(env, fTypography != nullptr, "Create Typography failed");
double dx = 50.5;
double dy = 10.5;
OH_Drawing_PositionAndAffinity *positionAndAffinity =
OH_Drawing_TypographyGetGlyphPositionAtCoordinate(fTypography, dx, dy);
NAPI_ASSERT(env, positionAndAffinity != nullptr, "Valid Typography should return non-null PositionAndAffinity");
int32_t position = OH_Drawing_GetPositionFromPositionAndAffinity(positionAndAffinity);
NAPI_ASSERT(env, position == 0, "GetPosition from valid PositionAndAffinity should return 0");
OH_Drawing_DestroyTypographyHandler(fHandler);
OH_Drawing_DestroyTypography(fTypography);
OH_Drawing_DestroyTypographyStyle(fTypoStyle);
napi_create_int32(env, SUCCESS, &result);
return result;
}
* @brief OHDrawingTypographyGetGlyphPositionAtCoordinate002:反向用例-空Typography调用(核心覆盖空指针)
* @details 验证传入nullptr的Typography对象时,函数返回nullptr且无崩溃
*/
napi_value OHDrawingTypographyGetGlyphPositionAtCoordinate002(napi_env env, napi_callback_info info)
{
napi_value result = nullptr;
double dx = 10.0;
double dy = 20.0;
OH_Drawing_PositionAndAffinity *posAffinity = OH_Drawing_TypographyGetGlyphPositionAtCoordinate(nullptr, dx, dy);
NAPI_ASSERT(env, posAffinity == nullptr, "Null Typography should return nullptr");
napi_create_int32(env, SUCCESS, &result);
return result;
}
* @brief OHDrawingTypographyGetGlyphPositionAtCoordinateWithCluster001:反向用例-空Typography调用
* @details 验证空Typography对象调用函数返回nullptr,且无崩溃
*/
napi_value OHDrawingTypographyGetGlyphPositionAtCoordinateWithCluster001(napi_env env, napi_callback_info info)
{
napi_value result = nullptr;
double dx = 50.5;
double dy = 10.5;
OH_Drawing_PositionAndAffinity *positionAndAffinity =
OH_Drawing_TypographyGetGlyphPositionAtCoordinateWithCluster(nullptr, dx, dy);
NAPI_ASSERT(env, positionAndAffinity == nullptr, "Null Typography should return nullptr");
napi_create_int32(env, SUCCESS, &result);
return result;
}
* @brief OHDrawingTypographyGetWordBoundary001:反向用例-空Typography调用(仅覆盖空指针分支)
* @details 验证传入nullptr的Typography对象时,OH_Drawing_TypographyGetWordBoundary返回nullptr且无崩溃
*/
napi_value OHDrawingTypographyGetWordBoundary001(napi_env env, napi_callback_info info)
{
napi_value result = nullptr;
size_t offset = 5;
OH_Drawing_Range *range = OH_Drawing_TypographyGetWordBoundary(nullptr, offset);
NAPI_ASSERT(env, range == nullptr, "Null Typography should return nullptr");
napi_create_int32(env, SUCCESS, &result);
return result;
}
* @brief OHDrawingTypographyGetLineMetricsAt001:反向用例-空Typography对象调用
* @details 验证typography=nullptr时,函数返回false(覆盖第一个返回false分支)
*/
napi_value OHDrawingTypographyGetLineMetricsAt001(napi_env env, napi_callback_info info)
{
napi_value result = nullptr;
OH_Drawing_LineMetrics lineMetric = {0};
bool ret = OH_Drawing_TypographyGetLineMetricsAt(nullptr, 0, &lineMetric);
bool isRetValid = (ret == false);
NAPI_ASSERT(env, isRetValid, "Null Typography should return false");
napi_create_int32(env, SUCCESS, &result);
return result;
}
* @brief OHDrawingTypographyGetLineMetricsAt002:反向用例-空LineMetrics对象调用
* @details 验证lineMetric=nullptr时,函数返回false(覆盖第二个返回false分支)
*/
napi_value OHDrawingTypographyGetLineMetricsAt002(napi_env env, napi_callback_info info)
{
napi_value result = nullptr;
OH_Drawing_TypographyStyle *typoStyle = OH_Drawing_CreateTypographyStyle();
NAPI_ASSERT(env, typoStyle != nullptr, "CreateTypographyStyle failed");
OH_Drawing_TypographyCreate *handler =
OH_Drawing_CreateTypographyHandler(typoStyle, OH_Drawing_CreateFontCollection());
NAPI_ASSERT(env, handler != nullptr, "CreateTypographyHandler failed");
OH_Drawing_Typography *typography = OH_Drawing_CreateTypography(handler);
NAPI_ASSERT(env, typography != nullptr, "CreateTypography failed");
int lineNumber = 0;
bool ret = OH_Drawing_TypographyGetLineMetricsAt(typography, lineNumber, nullptr);
bool isRetValid = (ret == false);
NAPI_ASSERT(env, isRetValid, "Null LineMetrics should return false");
OH_Drawing_DestroyTypography(typography);
OH_Drawing_DestroyTypographyHandler(handler);
OH_Drawing_DestroyTypographyStyle(typoStyle);
napi_create_int32(env, SUCCESS, &result);
return result;
}
* @brief OHDrawingTypographyGetLineMetricsAt003:正向用例-有效参数+合法行号
* @details 验证有效参数+合法行号时,函数返回true(覆盖返回true分支)
*/
napi_value OHDrawingTypographyGetLineMetricsAt003(napi_env env, napi_callback_info info)
{
napi_value result = nullptr;
OH_Drawing_TypographyStyle *typoStyle = nullptr;
OH_Drawing_TextStyle *txtStyle = nullptr;
OH_Drawing_TypographyCreate *handler = nullptr;
OH_Drawing_Typography *typography = nullptr;
OH_Drawing_LineMetrics lineMetric = {0};
do {
typoStyle = OH_Drawing_CreateTypographyStyle();
NAPI_ASSERT(env, typoStyle != nullptr, "CreateTypographyStyle failed");
txtStyle = OH_Drawing_CreateTextStyle();
NAPI_ASSERT(env, txtStyle != nullptr, "CreateTextStyle failed");
OH_Drawing_SetTextStyleFontSize(txtStyle, DEFAULT_FONT_SIZE);
handler = OH_Drawing_CreateTypographyHandler(typoStyle, OH_Drawing_CreateFontCollection());
NAPI_ASSERT(env, handler != nullptr, "CreateTypographyHandler failed");
OH_Drawing_TypographyHandlerPushTextStyle(handler, txtStyle);
OH_Drawing_TypographyHandlerAddText(handler, "测试行度量获取的文本内容");
typography = OH_Drawing_CreateTypography(handler);
NAPI_ASSERT(env, typography != nullptr, "CreateTypography failed");
OH_Drawing_TypographyLayout(typography, MAX_WIDTH);
bool ret = OH_Drawing_TypographyGetLineMetricsAt(typography, 0, &lineMetric);
bool isRetValid = (ret == true);
NAPI_ASSERT(env, isRetValid, "Valid params should return true");
} while (0);
OH_Drawing_DestroyTextStyle(txtStyle);
OH_Drawing_DestroyTypography(typography);
OH_Drawing_DestroyTypographyHandler(handler);
OH_Drawing_DestroyTypographyStyle(typoStyle);
napi_create_int32(env, SUCCESS, &result);
return result;
}
* @brief OHDrawingTypographyGetLineMetricsAt004:反向用例-有效参数+非法行号
* @details 验证有效参数+非法行号时,函数返回false(覆盖第三个返回false分支)
*/
napi_value OHDrawingTypographyGetLineMetricsAt004(napi_env env, napi_callback_info info)
{
napi_value result = nullptr;
OH_Drawing_TypographyStyle *typoStyle = nullptr;
OH_Drawing_TextStyle *txtStyle = nullptr;
OH_Drawing_TypographyCreate *handler = nullptr;
OH_Drawing_Typography *typography = nullptr;
OH_Drawing_LineMetrics lineMetric = {0};
do {
typoStyle = OH_Drawing_CreateTypographyStyle();
NAPI_ASSERT(env, typoStyle != nullptr, "CreateTypographyStyle failed");
txtStyle = OH_Drawing_CreateTextStyle();
NAPI_ASSERT(env, txtStyle != nullptr, "CreateTextStyle failed");
OH_Drawing_SetTextStyleFontSize(txtStyle, DEFAULT_FONT_SIZE);
handler = OH_Drawing_CreateTypographyHandler(typoStyle, OH_Drawing_CreateFontCollection());
NAPI_ASSERT(env, handler != nullptr, "CreateTypographyHandler failed");
OH_Drawing_TypographyHandlerPushTextStyle(handler, txtStyle);
OH_Drawing_TypographyHandlerAddText(handler, "测试行度量获取的文本内容");
typography = OH_Drawing_CreateTypography(handler);
NAPI_ASSERT(env, typography != nullptr, "CreateTypography failed");
OH_Drawing_TypographyLayout(typography, MAX_WIDTH);
int lineCount = static_cast<int>(OH_Drawing_TypographyGetLineCount(typography));
bool ret = OH_Drawing_TypographyGetLineMetricsAt(typography, lineCount + 1, &lineMetric);
bool isRetValid = (ret == false);
NAPI_ASSERT(env, isRetValid, "Invalid line number should return false");
} while (0);
OH_Drawing_DestroyTextStyle(txtStyle);
OH_Drawing_DestroyTypography(typography);
OH_Drawing_DestroyTypographyHandler(handler);
OH_Drawing_DestroyTypographyStyle(typoStyle);
napi_create_int32(env, SUCCESS, &result);
return result;
}