From 3119c1367d87c831b3af4486a5565bdac93b2607 Mon Sep 17 00:00:00 2001 From: hyzboy Date: Wed, 22 Jul 2020 20:50:13 +0800 Subject: [PATCH] update TextLayout --- inc/hgl/graph/font/FontSource.h | 31 ++++---- inc/hgl/graph/font/TextLayout.h | 3 +- src/SceneGraph/CMakeLists.txt | 3 +- src/SceneGraph/font/FontSourceSingle.cpp | 11 +++ src/SceneGraph/font/FontSourceWin.cpp | 18 ++--- src/SceneGraph/font/FontSourceWin.h | 1 - src/SceneGraph/font/TextLayout.cpp | 93 ++++++++++++++++++++++++ src/SceneGraph/font/TextLayoutPlane.cpp | 21 ------ 8 files changed, 133 insertions(+), 48 deletions(-) delete mode 100644 src/SceneGraph/font/TextLayoutPlane.cpp diff --git a/inc/hgl/graph/font/FontSource.h b/inc/hgl/graph/font/FontSource.h index d9481533..94568970 100644 --- a/inc/hgl/graph/font/FontSource.h +++ b/inc/hgl/graph/font/FontSource.h @@ -13,6 +13,14 @@ namespace hgl { namespace graph { + struct FontAdvInfo + { + int x,y; //图像显示偏移 + int w,h; //图像尺寸 + + int adv_x,adv_y;//字符尺寸 + };//struct FontAdvInfo + /** * 字体位图数据 */ @@ -20,10 +28,7 @@ namespace hgl { int count; //使用次数 - int x,y; //图像显示偏移 - int w,h; //图像尺寸 - - int adv_x,adv_y;//字符尺寸 + FontAdvInfo adv_info; uint8 *data; };//struct FontBitmap @@ -41,9 +46,9 @@ namespace hgl virtual ~FontSource()=default; - virtual FontBitmap *GetCharBitmap(const u32char &)=0; ///<取得字符位图数据 - virtual int GetCharAdvWidth(const u32char &)=0; ///<取得字符绘制宽度 - virtual int GetCharHeight()const=0; ///<取得字符高度 + virtual FontBitmap *GetCharBitmap (const u32char &)=0; ///<取得字符位图数据 + virtual const bool GetCharAdvInfo (FontAdvInfo &,const u32char &); ///<取得字符绘制信息 + virtual int GetCharHeight ()const=0; ///<取得字符高度 void RefAcquire(void *); ///<引用请求 void RefRelease(void *); ///<引用释放 @@ -70,9 +75,9 @@ namespace hgl FontSourceSingle(const Font &f){fnt=f;} virtual ~FontSourceSingle()=default; - FontBitmap *GetCharBitmap(const u32char &ch) override; ///<取得字符位图数据 - virtual int GetCharAdvWidth(const u32char &)=0; ///<取得字符绘制宽度 - virtual int GetCharHeight()const override{return fnt.height;} ///<取得字符高度 + FontBitmap *GetCharBitmap (const u32char &ch) override; ///<取得字符位图数据 + const bool GetCharAdvInfo (FontAdvInfo &,const u32char &); ///<取得字符绘制信息 + virtual int GetCharHeight ()const override{return fnt.height;} ///<取得字符高度 };//class FontSourceSingle:public FontSource /** @@ -105,9 +110,9 @@ namespace hgl void Remove(UnicodeBlock); void Remove(FontSource *); - FontBitmap *GetCharBitmap(const u32char &ch) override; - int GetCharAdvWidth(const u32char &) override; - int GetCharHeight()const override; ///<取得字符高度 + FontBitmap *GetCharBitmap (const u32char &ch) override; + const bool GetCharAdvInfo (FontAdvInfo &,const u32char &); ///<取得字符绘制信息 + int GetCharHeight ()const override; ///<取得字符高度 };//class FontSourceMulti:public FontSource }//namespace graph }//namespace hgl diff --git a/inc/hgl/graph/font/TextLayout.h b/inc/hgl/graph/font/TextLayout.h index 5d365b03..881674ee 100644 --- a/inc/hgl/graph/font/TextLayout.h +++ b/inc/hgl/graph/font/TextLayout.h @@ -32,7 +32,6 @@ namespace hgl { uint vertical:1; ///<是否竖排 uint right_to_left:1; ///<是否从右往左 - uint bottom_to_top:1; ///<是否从下到上 }; };//union TextDirection @@ -79,7 +78,7 @@ namespace hgl int TextLayout(RenderableCreater *,const TextLayoutAttributes *,const int max_chars,const UTF16String &); - int PlaneTextLayout(RenderableCreater *,FontSource *font_source,const int max_chars,const UTF16String &); + int PlaneTextLayout(RenderableCreater *,FontSource *font_source,const int max_chars,const UTF16String &,const uint8 text_direction=0); }//namespace graph }//namespace hgl #endif//HGL_GRAPH_TEXT_LAYOUT_INCLUDE diff --git a/src/SceneGraph/CMakeLists.txt b/src/SceneGraph/CMakeLists.txt index cb1961d2..8c905a62 100644 --- a/src/SceneGraph/CMakeLists.txt +++ b/src/SceneGraph/CMakeLists.txt @@ -62,8 +62,7 @@ SET(TILE_FONT_SOURCE ${ROOT_INCLUDE_PATH}/hgl/graph/font/TileFont.h font/TileFont.cpp) SET(FONT_LAYOUT_SOURCE ${ROOT_INCLUDE_PATH}/hgl/graph/font/TextLayout.h - font/TextLayout.cpp - font/TextLayoutPlane.cpp) + font/TextLayout.cpp) SOURCE_GROUP("Font" FILES ${FONT_MANAGE_SOURCE}) SOURCE_GROUP("Font\\Source" FILES ${FONT_SOURCE}) diff --git a/src/SceneGraph/font/FontSourceSingle.cpp b/src/SceneGraph/font/FontSourceSingle.cpp index 97b567be..e2066ac9 100644 --- a/src/SceneGraph/font/FontSourceSingle.cpp +++ b/src/SceneGraph/font/FontSourceSingle.cpp @@ -29,5 +29,16 @@ namespace hgl return bmp; } } + + const bool FontSourceSingle::GetCharAdvInfo(FontAdvInfo &adv_info,const u32char &ch) + { + FontBitmap *bmp=GetCharBitmap(ch); + + if(!bmp) + return false; + + adv_info=bmp->adv_info; + return(true); + } }//namespace graph }//namespace hgl diff --git a/src/SceneGraph/font/FontSourceWin.cpp b/src/SceneGraph/font/FontSourceWin.cpp index 26284358..a2445b83 100644 --- a/src/SceneGraph/font/FontSourceWin.cpp +++ b/src/SceneGraph/font/FontSourceWin.cpp @@ -120,21 +120,21 @@ namespace hgl GetGlyphOutlineW(hdc,ch,ggo,&gm,buffer_size,buffer,&mat); - bmp->w=gm.gmBlackBoxX; - bmp->h=gm.gmBlackBoxY; + bmp->adv_info.w =gm.gmBlackBoxX; + bmp->adv_info.h =gm.gmBlackBoxY; - bmp->x=gm.gmptGlyphOrigin.x; - bmp->y=gm.gmptGlyphOrigin.y; + bmp->adv_info.x =gm.gmptGlyphOrigin.x; + bmp->adv_info.y =gm.gmptGlyphOrigin.y; - bmp->adv_x=gm.gmCellIncX; - bmp->adv_y=gm.gmCellIncY; + bmp->adv_info.adv_x =gm.gmCellIncX; + bmp->adv_info.adv_y =gm.gmCellIncY; - bmp->data=new uint8[bmp->w*bmp->h]; + bmp->data=new uint8[bmp->adv_info.w*bmp->adv_info.h]; if(ggo==GGO_GRAY8_BITMAP) - Convert8BitGrey(bmp->data,buffer,gm.gmBlackBoxX,gm.gmBlackBoxY,size/bmp->h); + Convert8BitGrey(bmp->data,buffer,gm.gmBlackBoxX,gm.gmBlackBoxY,size/bmp->adv_info.h); else - ConvertBitmap(bmp->data,buffer,gm.gmBlackBoxX,gm.gmBlackBoxY,size/bmp->h); + ConvertBitmap(bmp->data,buffer,gm.gmBlackBoxX,gm.gmBlackBoxY,size/bmp->adv_info.h); return(true); } diff --git a/src/SceneGraph/font/FontSourceWin.h b/src/SceneGraph/font/FontSourceWin.h index 60ae2242..b27d54cf 100644 --- a/src/SceneGraph/font/FontSourceWin.h +++ b/src/SceneGraph/font/FontSourceWin.h @@ -27,7 +27,6 @@ namespace hgl ~WinBitmapFont(); bool MakeCharBitmap(FontBitmap *,u32char) override; ///<产生字体数据 - int GetCharAdvWidth(const u32char &) override; };//class WinBitmapFont }//namespace graph }//namespace hgl diff --git a/src/SceneGraph/font/TextLayout.cpp b/src/SceneGraph/font/TextLayout.cpp index a7435700..e6fe0038 100644 --- a/src/SceneGraph/font/TextLayout.cpp +++ b/src/SceneGraph/font/TextLayout.cpp @@ -37,6 +37,8 @@ namespace hgl bool begin_disable; ///<是否行首禁用符号 bool end_disable; ///<是否行尾禁用符号 bool vrotate; ///<竖排时是否需要旋转 + + FontAdvInfo adv_info; ///<字符绘制信息 };//struct CharLayoutAttributes /** @@ -79,8 +81,21 @@ namespace hgl TEXT_COORD_TYPE line_height; TEXT_COORD_TYPE paragraph_gap; + protected: + + AutoDelete vertex; + public: + TextLayoutEngine() + { + rc=nullptr; + tla=nullptr; + direction.text_direction=0; + max_chars=0; + vertex=nullptr; + } + virtual ~TextLayoutEngine()=default; bool Init(RenderableCreater *_rc,const TextLayoutAttributes *_tla) @@ -167,6 +182,9 @@ namespace hgl cla->is_emoji =isEmoji(*cp); cla->is_punctuation =isPunctuation(*cp); + + if(!tla->font_source->GetCharAdvInfo(cla->adv_info,*cp)) + hgl_zero(cla->adv_info); } ++cp; @@ -225,6 +243,81 @@ namespace hgl return(-4); } + return 0; + } + + protected: + + int pl_h_l2r() + { + const int count=chars_attributes.GetCount(); + const CLA *cla=chars_attributes.GetData(); + + int cur_size=0; + + for(int i=0;iWrite(x,y + } + } + + int pl_h_r2l() + { + return 0; + } + + int pl_v_r2l() + { + return 0; + } + + int pl_v_l2r() + { + return 0; + } + + public: + + /** + * 平面文本排版
+ * 不处理自动换行,仅支持\r\n换行。无任何特殊处理 + */ + int plane_layout(const int mc,const BaseString &str) + { + if(mc<=0 + ||!str + ||!(*str)) + return(-1); + + max_chars=hgl_min(mc,str.Length()); + origin_string=str; + + if(preprocess()<=0) + return(-3); + + if(!rc->Init(ch_count)) + return(-4); + + vertex=rc->CreateVADA(VAN::Vertex); + + if(!vertex) + return(-5); + + if(direction.vertical) + { + if(direction.right_to_left) + return pl_v_r2l(); + else + return pl_v_l2r(); + } + else + { + if(direction.right_to_left) + return pl_h_r2l(); + else + return pl_h_l2r(); + } + return 0; } };//template class TextLayoutEngine diff --git a/src/SceneGraph/font/TextLayoutPlane.cpp b/src/SceneGraph/font/TextLayoutPlane.cpp deleted file mode 100644 index 25b9f335..00000000 --- a/src/SceneGraph/font/TextLayoutPlane.cpp +++ /dev/null @@ -1,21 +0,0 @@ -#include - -namespace hgl -{ - namespace graph - { - /** - * 平面文本排版 - */ - int PlaneTextLayout(RenderableCreater *rc,FontSource *font_source,const int max_chars,const UTF16String &str) - { - if(!rc - ||!font_source - ||max_chars<=0 - ||str.IsEmpty()) - return 0; - - - } - }//namespace graph -}//namespace hgl \ No newline at end of file