增加字体测试功能

This commit is contained in:
JackLee 2026-03-29 22:26:35 +08:00
parent 23cf94de71
commit c20ff10e02
7 changed files with 74 additions and 15 deletions

View File

@ -85,8 +85,12 @@ bool Axis::InitAxisCube(Handle(AIS_InteractiveContext) & context,Handle(Prs3d_Te
axiViewCube->SetTransparency(0.0);
axiViewCube->SetFixedAnimationLoop(true);
axiViewCube->SetTextColor(Quantity_Color(Quantity_NOC_YELLOW2));
axiViewCube->SetFontHeight(35);
axiViewCube->SetFontHeight(30);
axiViewCube->SetFont("HarmonyOS Sans");
axiViewCube->SetMaterial(Graphic3d_MaterialAspect(Graphic3d_NOM_ALUMINIUM));
axiViewCube->BoxEdgeStyle()->Aspect()->SetTextFont(_textStyle->Aspect()->TextFont());
axiViewCube->BoxCornerStyle()->Aspect()->SetTextFont(_textStyle->Aspect()->TextFont());
axiViewCube->BoxSideStyle()->Aspect()->SetTextFont(_textStyle->Aspect()->TextFont());
axiViewCube->SetTransformPersistence(
new Graphic3d_TransformPers(Graphic3d_TMF_TriedronPers, Aspect_TOTP_RIGHT_UPPER, NCollection_Vec2<int>(125, 125)));
context->Display(axiViewCube, true);

View File

@ -18,8 +18,8 @@ bool Context::InitContext(Handle(V3d_Viewer)& viewer,Handle(Prs3d_TextAspect) _t
try {
context = new AIS_InteractiveContext(viewer);
context->SetDisplayMode(AIS_Shaded, true); // 默认使用着色模式
Handle(Prs3d_Drawer) theDrawer=context->DefaultDrawer();
theDrawer->SetTextAspect(_textAspect);
//Handle(Prs3d_Drawer) theDrawer=context->DefaultDrawer();
//theDrawer->SetTextAspect(_textAspect);
HILOG_INFO(NATIVE_TAG, "InitCtx Done");
return true;
} catch (std::exception &e) {

View File

@ -11,7 +11,7 @@ static std::mutex renderMutex;
namespace NativeOpenCAX {
NativeRender::NativeRender(int w, int h)
: width(0), height(0), mAxis(new Axis), nAxis(new Axis), cr(new Camera), ctx(new Context),
: width(0), height(0), mAxis(new Axis),nAxis(new Axis), cr(new Camera), ctx(new Context),
ogd(new OpenGlGraphicDriver), ts(new TextStyle), vw(new View), vr(new Viewer), win(new Window) {
width = w;
height = h;
@ -64,6 +64,7 @@ bool NativeRender::init(EGLCore &_eglCore) {
HILOG_ERROR(NATIVE_TAG, "Init AXIS Cuba Fail!");
return false;
}
//InitDevText();
vw->ResetView();
return true;
}
@ -188,7 +189,19 @@ void NativeRender::setZoomLevel(float zoom) {
}
void NativeRender::setClearColor(float r, float g, float b, float a) { vw->SetClearColor(r, g, b, a); }
void NativeRender::resetView() { vw->ResetView(); }
void NativeRender::SwitchView(std::string view) {
vw->SwitchView(view);
void NativeRender::SwitchView(std::string str) {
vw->SwitchView(str);
}
void NativeRender::InitDevText(){
Handle(AIS_TextLabel) aTextLabel = new AIS_TextLabel();
const char16_t chinese_array[] = u"新时代社会主义中国接班人";
aTextLabel->SetText(chinese_array);
aTextLabel->Attributes()->SetTextAspect(ts->GetFontStyle(STYLE_AXIS));
gp_Pnt position(0-(width/3), 0-(height/3), 0.0); // 例如,在原点
aTextLabel->SetPosition(position);
ctx->context->Display(aTextLabel, Standard_True);
HILOG_ERROR(NATIVE_TAG, "aTextLabel字体名字:%{public}s", aTextLabel->FontName().ToCString());
HILOG_ERROR(NATIVE_TAG, "aTextLabel字体名字:%{public}s", aTextLabel->Attributes()->TextAspect()->Aspect()->Font().ToCString());
}
} // namespace NativeOpenCAX

View File

@ -51,6 +51,7 @@ public:
void setCameraRotationMode(bool state);
//视图
void SwitchView(std::string);
void InitDevText();
private:
int width;
int height;

View File

@ -56,7 +56,41 @@ void NativeRenderThread::renderLoop() {
if (!eglCore_->init(nativeWindow_)) {
OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "EGLCoreInit","Failed to initialize EGL");
}
ftMgr=Font_FontMgr::GetInstance();
ftMgr=Font_FontMgr::GetInstance();
ftMgr->InitFontDataBase();
ftMgr->SetPrintErrors(true);
ftMgr->SetTraceAliases(true);
Handle(Font_SystemFont) sysFont=new Font_SystemFont("HarmonyOS_Sans_Medium");
sysFont->SetFontPath(Font_FontAspect_Regular, "/data/storage/el2/base/haps/entry/files/fonts/");
// 注册字体。第二个参数如果是 Standard_True表示强制注册即使同名也覆盖。
bool isRegistered = ftMgr->RegisterFont(sysFont, Standard_True);
if (!isRegistered) {
OH_LOG_Print(LOG_APP, LOG_INFO, LOG_DOMAIN, "OCCT","Error: Cannot register font: %{public}s",sysFont->FontName().ToCString());
return; // 或者进行错误处理
} else {
OH_LOG_Print(LOG_APP, LOG_INFO, LOG_DOMAIN, "OCCT","Successfully registered font: %{public}s",sysFont->FontName().ToCString());
}
Handle(Font_SystemFont) fsf;
fsf=ftMgr->GetFont("HarmonyOS_Sans_Medium");
OH_LOG_Print(LOG_APP, LOG_INFO, LOG_DOMAIN, "OCCT","OCCT Path: %{public}s", fsf->FontPath(Font_FontAspect_Regular).ToCString());
OH_LOG_Print(LOG_APP, LOG_INFO, LOG_DOMAIN, "OCCT","OCCT GetFont: %{public}s", fsf->FontName().ToCString());
//输出所有字体
NCollection_Sequence<occ::handle<TCollection_HAsciiString>> theAliases;
ftMgr->GetAllAliases(theAliases);
for(auto ft:theAliases){
OH_LOG_Print(LOG_APP, LOG_INFO, LOG_DOMAIN, "OCCT","OCCT Aliases: %{public}s", ft->ToCString());
}
//checkfont
NCollection_List<occ::handle<Font_SystemFont>> fsfs;
fsfs=ftMgr->GetAvailableFonts();
for(auto fta:fsfs){
OH_LOG_Print(LOG_APP, LOG_INFO, LOG_DOMAIN, "OCCT","OCCT Font: %{public}s", fta->FontName().ToCString());
OH_LOG_Print(LOG_APP, LOG_INFO, LOG_DOMAIN, "OCCT","OCCT Font Path: %{public}s", fta->FontPath(Font_FontAspect_Regular).ToCString());
}
// 初始化OCCT渲染器
if (!renderer_->init(*eglCore_)) {
OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "NativeRenderInit","Failed to initialize OCCT renderer");

View File

@ -38,13 +38,13 @@ void TextStyle::InitAxisText(){
FONT_STYLE_TYPE styleType=FONT_STYLE_TYPE::STYLE_AXIS;
Handle(Prs3d_TextAspect) axisText= new Prs3d_TextAspect();
axisText = new Prs3d_TextAspect();
axisText->SetHeight(70);
axisText->SetHeight(50);
axisText->SetFont("HarmonyOS Sans");
axisText->Aspect()->SetFont("HarmonyOS Sans");
axisText->Aspect()->SetColor(Quantity_NOC_GRAY95);
axisText->Aspect()->SetColorSubTitle(Quantity_NOC_BLACK);
axisText->Aspect()->SetDisplayType(Aspect_TODT_SHADOW);
axisText->Aspect()->SetTextFontAspect(Font_FA_Regular);
axisText->Aspect()->SetTextFontAspect(Font_FA_Bold);
axisText->Aspect()->SetTextZoomable(false);
axisText->SetHorizontalJustification(Graphic3d_HTA_LEFT);
axisText->SetVerticalJustification(Graphic3d_VTA_BOTTOM);

View File

@ -34,19 +34,27 @@ void View::SetViewOption(Handle(Prs3d_TextAspect) _textStyle) {
view->ChangeRenderingParams().Method = Graphic3d_RM_RASTERIZATION;
view->ChangeRenderingParams().IsShadowEnabled = Standard_False;
view->ChangeRenderingParams().IsReflectionEnabled = Standard_False;
//抗锯齿开启后字体无法显示
// view->ChangeRenderingParams().IsAntialiasingEnabled = Standard_True;
// view->ChangeRenderingParams().Resolution = 2;
// 设置背景渐变
view->SetBgGradientColors(Quantity_Color(Quantity_NOC_GRAY), Quantity_Color(Quantity_NOC_BLACK),
Aspect_GFM_VER, // 垂直渐变
false);
// 设置默认相机位置
view->SetProj(V3d_XposYnegZpos);
view->Camera()->SetProjectionType(Graphic3d_Camera::Projection_Orthographic);
view->FitAll(0.05, false);
// 可选:显示坐标轴
view->ZBufferTriedronSetup();
// 调整相机视角
view->Camera()->SetProjectionType(Graphic3d_Camera::Projection_Perspective);
view->SetBackgroundColor(Quantity_NOC_GRAY90);
//左下角默认坐标系
view->TriedronDisplay(Aspect_TOTP_LEFT_LOWER, Quantity_NOC_ANTIQUEWHITE, 0.3, V3d_ZBUFFER);
view->Trihedron(false)->LabelAspect(V3d_Y)->SetAspect(_textStyle->Aspect());
view->Trihedron(false)->LabelAspect(V3d_X)->SetAspect(_textStyle->Aspect());
view->Trihedron(false)->LabelAspect(V3d_Z)->SetAspect(_textStyle->Aspect());
// view->Trihedron(false)->LabelAspect(V3d_Y)->SetAspect(_textStyle->Aspect());
// view->Trihedron(false)->LabelAspect(V3d_X)->SetAspect(_textStyle->Aspect());
// view->Trihedron(false)->LabelAspect(V3d_Z)->SetAspect(_textStyle->Aspect());
view->SetBackgroundColor(Quantity_NOC_GRAY90);
}
@ -71,8 +79,7 @@ void View::Redraw() {
void View::ResetView() {
if (!view.IsNull()) {
view->SetProj(V3d_XposYnegZpos);
//view->FitAll(0.05, false);
view->AutoZFit();
view->FitAll(0.05, false);
}
}