From c667e026ece28f4f39e5824836ee81354a46fa47 Mon Sep 17 00:00:00 2001 From: hyzboy Date: Fri, 3 Jul 2020 21:11:47 +0800 Subject: [PATCH] add FontSourceManage,FontMultiSource,TileFont.cpp --- inc/hgl/graph/font/FontManage.h | 9 ++-- inc/hgl/graph/font/FontMultiSource.h | 26 ++++++++++ inc/hgl/graph/font/FontSource.h | 7 +++ inc/hgl/graph/font/TileFont.h | 12 +---- src/SceneGraph/font/FontSource.cpp | 16 ++++++ src/SceneGraph/font/FontSourceManage.cpp | 24 +++++++++ src/SceneGraph/font/FontSourceWin.cpp | 10 ++-- src/SceneGraph/font/TileFont.cpp | 65 ++++++++++++++++++++++++ 8 files changed, 148 insertions(+), 21 deletions(-) create mode 100644 inc/hgl/graph/font/FontMultiSource.h create mode 100644 src/SceneGraph/font/FontSourceManage.cpp create mode 100644 src/SceneGraph/font/TileFont.cpp diff --git a/inc/hgl/graph/font/FontManage.h b/inc/hgl/graph/font/FontManage.h index cfe326d2..5aa68519 100644 --- a/inc/hgl/graph/font/FontManage.h +++ b/inc/hgl/graph/font/FontManage.h @@ -1,16 +1,13 @@ #ifndef HGL_GRAPH_FONT_MANAGE_INCLUDE #define HGL_GRAPH_FONT_MANAGE_INCLUDE -#include +#include +#include #include namespace hgl { namespace graph { - struct FontConfig - { - };// - /** * 字体管理模块,包括所有的字体数据源,以及字符绘制器。
* 在可期的未来,我们会增加对字体、字符的使用情况统计信息 @@ -19,7 +16,7 @@ namespace hgl { MapObject sources; - TileFont * + MapObject tilefonts; };//class FontManage }//namespace graph }//namespace hgl diff --git a/inc/hgl/graph/font/FontMultiSource.h b/inc/hgl/graph/font/FontMultiSource.h new file mode 100644 index 00000000..fd920880 --- /dev/null +++ b/inc/hgl/graph/font/FontMultiSource.h @@ -0,0 +1,26 @@ +#ifndef HGL_GRAPH_FONT_MULTI_SOURCE_INCLUDE +#define HGL_GRAPH_FONT_MULTI_SOURCE_INCLUDE + +#include +#include +namespace hgl +{ + namespace graph + { + class FontMultiSource:public FontSource + { + using FontSourcePointer=FontSource *; + using FontSourceTable=FontSourcePointer[(size_t)UnicodeBlock::RANGE_SIZE]; + + FontSourceTable source_map; + + public: + + FontMultiSource(); + virtual ~FontMultiSource(); + + void Add(UnicodeBlock,FontSource *); + };//class FontMultiSource:public FontSource + }//namespace graph +}//namespace hgl +#endif//HGL_GRAPH_FONT_MULTI_SOURCE_INCLUDE diff --git a/inc/hgl/graph/font/FontSource.h b/inc/hgl/graph/font/FontSource.h index f9356090..18268288 100644 --- a/inc/hgl/graph/font/FontSource.h +++ b/inc/hgl/graph/font/FontSource.h @@ -3,6 +3,7 @@ #include #include +#include #include using namespace hgl; @@ -37,6 +38,8 @@ namespace hgl MapObject chars_bitmap; ///<字符位图 + Set ref_object; + protected: virtual bool MakeCharBitmap(FontBitmap *,u32char)=0; ///<产生字体数据 @@ -48,6 +51,10 @@ namespace hgl virtual ~FontSource()=default; FontBitmap *GetCharBitmap(const u32char &); ///<取得字符位图数据 + + void RefAcquire(void *); ///<引用请求 + void RefRelease(void *); ///<引用释放 + int RefCount()const{return ref_object.GetCount();} ///<获取引用对象数量 };//class FontSource }//namespace graph }//namespace hgl diff --git a/inc/hgl/graph/font/TileFont.h b/inc/hgl/graph/font/TileFont.h index f5d63bf2..887034d9 100644 --- a/inc/hgl/graph/font/TileFont.h +++ b/inc/hgl/graph/font/TileFont.h @@ -15,22 +15,14 @@ namespace hgl */ class TileFont { - using FontSourcePointer=FontSource *; - using FontSourceTable=FontSourcePointer[(size_t)UnicodeBlock::RANGE_SIZE]; - - FontSourceTable source_map; TileData *tile_data; public: - TileFont() - { - hgl_zero(source_map); - } + TileFont(TileData *td,FontSource *fs); + virtual ~TileFont(); };//class TileFont - - TileFont *CreateTileFont(const Font &,const int=-1); }//namespace graph }//namespace hgl #endif//HGL_GRAPH_TILE_FONT_INCLUDE diff --git a/src/SceneGraph/font/FontSource.cpp b/src/SceneGraph/font/FontSource.cpp index a7b654e4..e0c1021f 100644 --- a/src/SceneGraph/font/FontSource.cpp +++ b/src/SceneGraph/font/FontSource.cpp @@ -32,5 +32,21 @@ namespace hgl return bmp; } } + + void FontSource::RefAcquire(void *ptr) + { + if(!ptr)return; + + ref_object.Add(ptr); + + return; + } + + void FontSource::RefRelease(void *ptr) + { + if(!ptr)return; + + ref_object.Delete(ptr); + } }//namespace graph }//namespace hgl diff --git a/src/SceneGraph/font/FontSourceManage.cpp b/src/SceneGraph/font/FontSourceManage.cpp new file mode 100644 index 00000000..281fc7e6 --- /dev/null +++ b/src/SceneGraph/font/FontSourceManage.cpp @@ -0,0 +1,24 @@ +#include +namespace hgl +{ + namespace graph + { + FontSource *CreateFontSource(const Font &f); //各平台独立提供 + + static MapObject FontStorage; + + FontSource *AcquireFontSource(const Font &f) + { + FontSource *source; + + if(!FontStorage.Get(f,source)) + { + source=CreateFontSource(f); + + FontStorage.Add(f,source); + } + + return source; + } + }//namespace graph +}//namespace hgl \ No newline at end of file diff --git a/src/SceneGraph/font/FontSourceWin.cpp b/src/SceneGraph/font/FontSourceWin.cpp index 1d601fb5..2bc5fb66 100644 --- a/src/SceneGraph/font/FontSourceWin.cpp +++ b/src/SceneGraph/font/FontSourceWin.cpp @@ -57,11 +57,6 @@ namespace hgl } }//namespace - FontSource *CreateWinBitmapFont(const Font &f) - { - return(new WinBitmapFont(f)); - } - WinBitmapFont::WinBitmapFont(const Font &f):FontSource(f) { hdc=CreateCompatibleDC(0); @@ -146,5 +141,10 @@ namespace hgl return(true); } + + FontSource *CreateFontSource(const Font &f) + { + return(new WinBitmapFont(f)); + } }//namespace graph }//namespace hgl \ No newline at end of file diff --git a/src/SceneGraph/font/TileFont.cpp b/src/SceneGraph/font/TileFont.cpp new file mode 100644 index 00000000..0f40d1aa --- /dev/null +++ b/src/SceneGraph/font/TileFont.cpp @@ -0,0 +1,65 @@ +#include +#include +#include + +namespace hgl +{ + namespace graph + { + FontSource *AcquireFontSource(const Font &f); + + TileFont::TileFont(TileData *td,FontSource *fs) + { + hgl_zero(source_map); + tile_data=td; + + for(uint i=0;i<(uint)UnicodeBlock::RANGE_SIZE;i++) + source_map[i]=fs; + + fs->RefAcquire(this); + } + + TileFont::~TileFont() + { + for(uint i=0;i<(uint)UnicodeBlock::RANGE_SIZE;i++) + { + if(source_map[i]) + source_map[i]->RefRelease(this); + } + + SAFE_CLEAR(tile_data); + } + + /** + * 创建只使用一种字符的Tile字符管理对象 + * @param f 字体需求信息 + * @param limit_count 缓冲字符数量上限 + */ + TileFont *CreateTileFont(VK_NAMESPACE::Device *device,const Font &f,int limit_count=-1) + { + int height=(f.height+3)>>2; + + height<<=2; //保证可以被4整除 + height+=2; //上下左右各空一个象素 + + if(limit_count<=0) + { + const VkExtent2D &ext=device->GetSwapchainSize(); + + limit_count=(ext.width/height)*(ext.height/height); //按全屏幕放满不一样的字符为上限 + } + + FontSource *fs=AcquireFontSource(f); + + if(!fs) + return(nullptr); + + TileData *td=device->CreateTileData(UFMT_R8,height,height,limit_count); + + if(!td) + return nullptr; + + return(new TileFont(td,fs)); + } + }//namespace graph +}//namespace hgl