2022-02-16 23:26:50 +08:00
|
|
|
|
#include<hgl/graph/font/TextRender.h>
|
2022-06-24 17:51:05 +08:00
|
|
|
|
#include<hgl/graph/font/TextPrimitive.h>
|
2022-02-16 23:26:50 +08:00
|
|
|
|
#include<hgl/graph/font/TileFont.h>
|
|
|
|
|
#include<hgl/graph/font/TextLayout.h>
|
|
|
|
|
#include<hgl/graph/VKDevice.h>
|
2023-02-19 19:28:47 +08:00
|
|
|
|
#include<hgl/color/Color.h>
|
2022-02-16 23:26:50 +08:00
|
|
|
|
|
|
|
|
|
namespace hgl
|
|
|
|
|
{
|
|
|
|
|
namespace graph
|
|
|
|
|
{
|
2022-02-17 10:48:12 +08:00
|
|
|
|
TextRender::TextRender(GPUDevice *dev,FontSource *fs)
|
2022-02-16 23:26:50 +08:00
|
|
|
|
{
|
|
|
|
|
device=dev;
|
|
|
|
|
db=new RenderResource(device);
|
|
|
|
|
tl_engine=new TextLayout();
|
2022-02-17 10:48:12 +08:00
|
|
|
|
font_source=fs;
|
2022-02-17 11:02:55 +08:00
|
|
|
|
|
|
|
|
|
material =nullptr;
|
|
|
|
|
material_instance =nullptr;
|
|
|
|
|
sampler =nullptr;
|
|
|
|
|
pipeline =nullptr;
|
|
|
|
|
tile_font =nullptr;
|
|
|
|
|
ubo_color =nullptr;
|
2022-02-16 23:26:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TextRender::~TextRender()
|
|
|
|
|
{
|
2022-06-24 17:51:05 +08:00
|
|
|
|
for(TextPrimitive *tr:tr_sets)
|
2022-02-21 17:13:12 +08:00
|
|
|
|
{
|
|
|
|
|
tile_font->Unregistry(tr->GetCharsSets().GetList());
|
|
|
|
|
delete tr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
tr_sets.Clear();
|
|
|
|
|
|
2022-02-16 23:26:50 +08:00
|
|
|
|
SAFE_CLEAR(tl_engine);
|
|
|
|
|
SAFE_CLEAR(tile_font);
|
|
|
|
|
SAFE_CLEAR(db);
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-13 10:57:05 +08:00
|
|
|
|
bool TextRender::InitTileFont(int limit)
|
2022-02-16 23:26:50 +08:00
|
|
|
|
{
|
2022-05-13 10:57:05 +08:00
|
|
|
|
tile_font=device->CreateTileFont(font_source,limit);
|
2022-02-16 23:26:50 +08:00
|
|
|
|
return(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool TextRender::InitTextLayoutEngine()
|
|
|
|
|
{
|
|
|
|
|
CharLayoutAttr cla;
|
2022-02-18 19:07:02 +08:00
|
|
|
|
TextLayoutAttribute tla;
|
2022-02-16 23:26:50 +08:00
|
|
|
|
|
2022-03-11 17:38:21 +08:00
|
|
|
|
cla.CharColor=GetColor4f(COLOR::White,1.0);
|
|
|
|
|
cla.BackgroundColor.Zero();
|
2022-02-16 23:26:50 +08:00
|
|
|
|
|
|
|
|
|
tla.char_layout_attr=&cla;
|
|
|
|
|
tla.line_gap=0.1f;
|
|
|
|
|
|
|
|
|
|
tl_engine->SetFont(tile_font->GetFontSource());
|
|
|
|
|
tl_engine->Set(&tla);
|
|
|
|
|
tl_engine->SetTextDirection(0);
|
|
|
|
|
tl_engine->SetAlign(TextAlign::Left);
|
|
|
|
|
tl_engine->SetMaxWidth(0);
|
|
|
|
|
tl_engine->SetMaxHeight(0);
|
|
|
|
|
|
|
|
|
|
return tl_engine->Init();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool TextRender::InitUBO()
|
|
|
|
|
{
|
|
|
|
|
color.One();
|
|
|
|
|
|
|
|
|
|
ubo_color=db->CreateUBO(sizeof(Color4f),&color);
|
|
|
|
|
|
|
|
|
|
if(!ubo_color)
|
|
|
|
|
return(false);
|
|
|
|
|
|
|
|
|
|
return(true);
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-14 17:52:35 +08:00
|
|
|
|
bool TextRender::InitMaterial(RenderPass *rp,DeviceBuffer *ubo_camera_info)
|
2022-02-16 23:26:50 +08:00
|
|
|
|
{
|
|
|
|
|
material=db->CreateMaterial(OS_TEXT("res/material/LumTextureRect2D"));
|
|
|
|
|
|
|
|
|
|
//文本渲染Position坐标全部是使用整数,这里强制要求Position输入流使用RGBA16I格式
|
|
|
|
|
{
|
2022-10-11 19:16:06 +08:00
|
|
|
|
VILConfig vil_config;
|
2022-02-16 23:26:50 +08:00
|
|
|
|
|
2023-02-21 18:36:42 +08:00
|
|
|
|
vil_config.Add("Position",VF_V4I16);
|
2022-02-16 23:26:50 +08:00
|
|
|
|
|
2022-10-11 19:16:06 +08:00
|
|
|
|
material_instance=db->CreateMaterialInstance(material,&vil_config);
|
2022-02-16 23:26:50 +08:00
|
|
|
|
if(!material_instance)return(false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pipeline=rp->CreatePipeline(material_instance,InlinePipeline::Solid2D,Prim::SolidRectangles);
|
|
|
|
|
if(!pipeline)return(false);
|
|
|
|
|
|
|
|
|
|
sampler=db->CreateSampler();
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
MaterialParameters *mp_global=material_instance->GetMP(DescriptorSetsType::Global);
|
|
|
|
|
|
|
|
|
|
if(!mp_global)
|
|
|
|
|
return(false);
|
|
|
|
|
|
|
|
|
|
if(!mp_global->BindUBO("g_camera",ubo_camera_info))return(false);
|
|
|
|
|
|
|
|
|
|
mp_global->Update();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
{
|
2023-02-22 21:50:18 +08:00
|
|
|
|
MaterialParameters *mp=material_instance->GetMP(DescriptorSetsType::PerMaterial);
|
2022-02-16 23:26:50 +08:00
|
|
|
|
|
|
|
|
|
if(!mp)
|
|
|
|
|
return(false);
|
|
|
|
|
|
2022-02-18 19:07:02 +08:00
|
|
|
|
if(!mp->BindSampler("lum_texture",tile_font->GetTexture(),sampler))return(false);
|
2022-02-16 23:26:50 +08:00
|
|
|
|
if(!mp->BindUBO("color_material",ubo_color))return(false);
|
|
|
|
|
|
|
|
|
|
mp->Update();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return(true);
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-14 17:52:35 +08:00
|
|
|
|
bool TextRender::Init(RenderPass *rp,DeviceBuffer *ubo_camera_info,int limit)
|
2022-02-16 23:26:50 +08:00
|
|
|
|
{
|
2022-05-13 10:57:05 +08:00
|
|
|
|
if(!InitTileFont(limit))
|
2022-02-16 23:26:50 +08:00
|
|
|
|
return(false);
|
|
|
|
|
|
|
|
|
|
if(!InitTextLayoutEngine())
|
|
|
|
|
return(false);
|
|
|
|
|
|
|
|
|
|
if(!InitUBO())
|
|
|
|
|
return(false);
|
|
|
|
|
|
|
|
|
|
if(!InitMaterial(rp,ubo_camera_info))
|
|
|
|
|
return(false);
|
|
|
|
|
|
|
|
|
|
return(true);
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-24 17:51:05 +08:00
|
|
|
|
TextPrimitive *TextRender::CreatePrimitive()
|
2022-02-21 17:13:12 +08:00
|
|
|
|
{
|
2022-06-24 17:51:05 +08:00
|
|
|
|
TextPrimitive *tr=new TextPrimitive(device,material);
|
2022-02-21 17:13:12 +08:00
|
|
|
|
|
|
|
|
|
tr_sets.Add(tr);
|
|
|
|
|
|
|
|
|
|
return tr;
|
2022-02-17 20:02:59 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-06-24 17:51:05 +08:00
|
|
|
|
TextPrimitive *TextRender::CreatePrimitive(const UTF16String &str)
|
2022-02-16 23:26:50 +08:00
|
|
|
|
{
|
2022-06-24 17:51:05 +08:00
|
|
|
|
TextPrimitive *tr=CreatePrimitive();
|
2022-02-16 23:26:50 +08:00
|
|
|
|
|
2022-02-17 11:02:55 +08:00
|
|
|
|
if(tl_engine->SimpleLayout(tr,tile_font,str)<=0)
|
2022-02-17 20:02:59 +08:00
|
|
|
|
return(tr);
|
2022-02-17 11:02:55 +08:00
|
|
|
|
|
|
|
|
|
return tr;
|
|
|
|
|
}
|
2022-02-16 23:26:50 +08:00
|
|
|
|
|
2022-06-24 17:51:05 +08:00
|
|
|
|
bool TextRender::Layout(TextPrimitive *tr,const UTF16String &str)
|
2022-02-17 20:02:59 +08:00
|
|
|
|
{
|
|
|
|
|
if(!tr)
|
|
|
|
|
return(false);
|
|
|
|
|
|
|
|
|
|
if(tl_engine->SimpleLayout(tr,tile_font,str)<=0)
|
|
|
|
|
return(false);
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-24 21:17:28 +08:00
|
|
|
|
Renderable *TextRender::CreateRenderable(TextPrimitive *text_primitive)
|
2022-02-17 11:02:55 +08:00
|
|
|
|
{
|
2022-06-24 21:17:28 +08:00
|
|
|
|
return db->CreateRenderable(text_primitive,material_instance,pipeline);
|
2022-02-16 23:26:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-06-24 17:51:05 +08:00
|
|
|
|
void TextRender::Release(TextPrimitive *tr)
|
2022-02-21 17:13:12 +08:00
|
|
|
|
{
|
|
|
|
|
if(!tr)return;
|
|
|
|
|
|
|
|
|
|
if(!tr_sets.Delete(tr))return;
|
|
|
|
|
|
|
|
|
|
tile_font->Unregistry(tr->GetCharsSets().GetList());
|
|
|
|
|
|
|
|
|
|
delete tr;
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-17 10:48:12 +08:00
|
|
|
|
FontSource *CreateCJKFontSource(const os_char *cf,const os_char *lf,const uint32_t size)
|
|
|
|
|
{
|
|
|
|
|
Font eng_fnt(lf,0,size);
|
|
|
|
|
Font chs_fnt(cf,0,size);
|
|
|
|
|
|
|
|
|
|
FontSource *eng_fs=AcquireFontSource(eng_fnt);
|
|
|
|
|
FontSource *chs_fs=AcquireFontSource(chs_fnt);
|
|
|
|
|
|
|
|
|
|
FontSourceMulti *font_source=new FontSourceMulti(eng_fs);
|
|
|
|
|
|
|
|
|
|
font_source->AddCJK(chs_fs);
|
|
|
|
|
|
|
|
|
|
return font_source;
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-17 20:02:59 +08:00
|
|
|
|
FontSource *AcquireFontSource(const os_char *name,const uint32_t size)
|
2022-02-17 10:48:12 +08:00
|
|
|
|
{
|
|
|
|
|
Font fnt(name,0,size);
|
|
|
|
|
|
|
|
|
|
return AcquireFontSource(fnt);
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-14 17:52:35 +08:00
|
|
|
|
TextRender *CreateTextRender(GPUDevice *dev,FontSource *fs,RenderPass *rp,DeviceBuffer *ubo_camera_info,int limit)
|
2022-02-16 23:26:50 +08:00
|
|
|
|
{
|
|
|
|
|
if(!dev||!rp||!ubo_camera_info)
|
|
|
|
|
return(nullptr);
|
|
|
|
|
|
2022-02-17 10:48:12 +08:00
|
|
|
|
TextRender *text_render=new TextRender(dev,fs);
|
2022-02-16 23:26:50 +08:00
|
|
|
|
|
2022-05-13 10:57:05 +08:00
|
|
|
|
if(!text_render->Init(rp,ubo_camera_info,limit))
|
2022-02-16 23:26:50 +08:00
|
|
|
|
{
|
|
|
|
|
delete text_render;
|
|
|
|
|
return(nullptr);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return text_render;
|
|
|
|
|
}
|
|
|
|
|
}//namespace graph
|
|
|
|
|
}//namespace hgl
|
|
|
|
|
|