From 57a0326ee85c30e5a91b3c21d69852189f0e21ba Mon Sep 17 00:00:00 2001 From: hyzboy Date: Sat, 4 Jul 2020 14:44:07 +0800 Subject: [PATCH] add FontSourceSingle/Multi.cpp --- CMCore | 2 +- inc/hgl/graph/font/FontMultiSource.h | 26 -------- inc/hgl/graph/font/FontSource.h | 60 +++++++++++++++---- src/SceneGraph/CMakeLists.txt | 17 +++++- src/SceneGraph/font/FontSource.cpp | 31 +--------- src/SceneGraph/font/FontSourceMulti.cpp | 75 ++++++++++++++++++++++++ src/SceneGraph/font/FontSourceSingle.cpp | 33 +++++++++++ src/SceneGraph/font/FontSourceWin.cpp | 10 ++-- src/SceneGraph/font/FontSourceWin.h | 4 +- 9 files changed, 183 insertions(+), 75 deletions(-) delete mode 100644 inc/hgl/graph/font/FontMultiSource.h create mode 100644 src/SceneGraph/font/FontSourceMulti.cpp create mode 100644 src/SceneGraph/font/FontSourceSingle.cpp diff --git a/CMCore b/CMCore index 2e076881..02cb4bb8 160000 --- a/CMCore +++ b/CMCore @@ -1 +1 @@ -Subproject commit 2e076881513560dd8111d3f101222dc2fd0b333e +Subproject commit 02cb4bb8b3f086321c8dd5773cc6355e72a5c8cb diff --git a/inc/hgl/graph/font/FontMultiSource.h b/inc/hgl/graph/font/FontMultiSource.h deleted file mode 100644 index fd920880..00000000 --- a/inc/hgl/graph/font/FontMultiSource.h +++ /dev/null @@ -1,26 +0,0 @@ -#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 18268288..8587e1b6 100644 --- a/inc/hgl/graph/font/FontSource.h +++ b/inc/hgl/graph/font/FontSource.h @@ -5,6 +5,7 @@ #include #include #include +#include using namespace hgl; @@ -32,14 +33,32 @@ namespace hgl */ class FontSource { + protected: + + Set ref_object; + + public: + + virtual ~FontSource()=default; + + virtual FontBitmap *GetCharBitmap(const u32char &)=0; ///<取得字符位图数据 + + void RefAcquire(void *); ///<引用请求 + void RefRelease(void *); ///<引用释放 + int RefCount()const{return ref_object.GetCount();} ///<获取引用对象数量 + };//class FontSource + + /** + * 文字位图单一数据源 + */ + class FontSourceSingle:public FontSource + { protected: Font fnt; MapObject chars_bitmap; ///<字符位图 - Set ref_object; - protected: virtual bool MakeCharBitmap(FontBitmap *,u32char)=0; ///<产生字体数据 @@ -47,15 +66,36 @@ namespace hgl public: - FontSource(const Font &f){fnt=f;} - virtual ~FontSource()=default; + FontSourceSingle(const Font &f){fnt=f;} + virtual ~FontSourceSingle()=default; - FontBitmap *GetCharBitmap(const u32char &); ///<取得字符位图数据 + FontBitmap *GetCharBitmap(const u32char &ch) override; + };//class FontSourceSingle:public FontSource - void RefAcquire(void *); ///<引用请求 - void RefRelease(void *); ///<引用释放 - int RefCount()const{return ref_object.GetCount();} ///<获取引用对象数量 - };//class FontSource + /** + * 文字位图多重数据源 + */ + class FontSourceMulti:public FontSource + { + using FontSourcePointer=FontSource *; + + FontSource *default_source; + Map source_map; + + public: + + /** + * @param dfs 缺省字符数据源 + */ + FontSourceMulti(FontSource *dfs); + virtual ~FontSourceMulti(); + + void Add(UnicodeBlock,FontSource *); + void Remove(UnicodeBlock); + void Remove(FontSource *); + + FontBitmap *GetCharBitmap(const u32char &ch) override; + };//class FontSourceMulti:public FontSource }//namespace graph }//namespace hgl -#endif//HGL_GRAPH_FONT_SOURCE_INCLUDE \ No newline at end of file +#endif//HGL_GRAPH_FONT_SOURCE_INCLUDE diff --git a/src/SceneGraph/CMakeLists.txt b/src/SceneGraph/CMakeLists.txt index 3b19928c..9664eb96 100644 --- a/src/SceneGraph/CMakeLists.txt +++ b/src/SceneGraph/CMakeLists.txt @@ -43,10 +43,25 @@ SET(SCENE_GRAPH_SOURCE RenderList.cpp ) file(GLOB FONT_HEADER ${ROOT_INCLUDE_PATH}/hgl/graph/font/*.*) -file(GLOB FONT_SOURCE font/*.*) + +SET(FONT_SOURCE font/Font.cpp + font/FontSource.cpp + font/FontSourceSingle.cpp + font/FontSourceMulti.cpp + font/FontSourceManage.cpp + font/TileFont.cpp) SOURCE_GROUP("Font" FILES ${FONT_HEADER} ${FONT_SOURCE}) +IF(WIN32) + SET(FONT_SOURCE_OS font/FontSourceWin.cpp + font/FontSourceWin.h) + + SOURCE_GROUP("Font\\Windows" FILES ${FONT_SOURCE_OS}) + + SET(FONT_SOURCE ${FONT_SOURCE} ${FONT_SOURCE_OS}) +ENDIF(WIN32) + SOURCE_GROUP("Header Files" FILES ${SCENE_GRAPH_HEADER}) SOURCE_GROUP("Source Files" FILES ${SCENE_GRAPH_SOURCE}) diff --git a/src/SceneGraph/font/FontSource.cpp b/src/SceneGraph/font/FontSource.cpp index e0c1021f..dfdc618d 100644 --- a/src/SceneGraph/font/FontSource.cpp +++ b/src/SceneGraph/font/FontSource.cpp @@ -3,36 +3,7 @@ namespace hgl { namespace graph - { - FontBitmap *FontSource::GetCharBitmap(const u32char &ch) - { - if(!this) - return(nullptr); - - if(hgl::isspace(ch))return(nullptr); //不能显示的数据或是空格 - - FontBitmap *bmp; - - if(chars_bitmap.Get(ch,bmp)) - return bmp; - - bmp=new FontBitmap; - - memset(bmp,0,sizeof(FontBitmap)); - - if(!MakeCharBitmap(bmp,ch)) - { - delete bmp; - chars_bitmap.Add(ch,nullptr); - return(nullptr); - } - else - { - chars_bitmap.Add(ch,bmp); - return bmp; - } - } - + { void FontSource::RefAcquire(void *ptr) { if(!ptr)return; diff --git a/src/SceneGraph/font/FontSourceMulti.cpp b/src/SceneGraph/font/FontSourceMulti.cpp new file mode 100644 index 00000000..32cec1d8 --- /dev/null +++ b/src/SceneGraph/font/FontSourceMulti.cpp @@ -0,0 +1,75 @@ +#include + +namespace hgl +{ + namespace graph + { + FontSourceMulti::FontSourceMulti(FontSource *fs) + { + default_source=fs; + + if(fs) + fs->RefAcquire(this); + } + + FontSourceMulti::~FontSourceMulti() + { + if(default_source) + default_source->RefRelease(this); + } + + void FontSourceMulti::Add(UnicodeBlock ub,FontSource *fs) + { + if(ubUnicodeBlock::END_RANGE + ||!fs + ||fs==default_source)return; + + source_map.Update(ub,fs); + } + + void FontSourceMulti::Remove(UnicodeBlock ub) + { + FontSourcePointer fsp; + + if(source_map.Get(ub,fsp)) + { + fsp->RefRelease(this); + source_map.DeleteByKey(ub); + } + } + + void FontSourceMulti::Remove(FontSource *fs) + { + if(!fs)return; + if(fs==default_source)return; + + if(source_map.ValueExist(fs)) + { + fs->RefRelease(this); + source_map.DeleteByValue(fs); + } + } + + FontBitmap *FontSourceMulti::GetCharBitmap(const u32char &ch) + { + if(hgl::isspace(ch))return(nullptr); //不能显示的数据或是空格 + + const auto count=source_map.GetCount(); + auto **fsp=source_map.GetDataList(); + + for(int i=0;ileft,ch)) + return (*fsp)->right->GetCharBitmap(ch); + + ++fsp; + } + + if(default_source) + return default_source->GetCharBitmap(ch); + + return nullptr; + } + }//namespace graph +}//namespace hgl diff --git a/src/SceneGraph/font/FontSourceSingle.cpp b/src/SceneGraph/font/FontSourceSingle.cpp new file mode 100644 index 00000000..97b567be --- /dev/null +++ b/src/SceneGraph/font/FontSourceSingle.cpp @@ -0,0 +1,33 @@ +#include + +namespace hgl +{ + namespace graph + { + FontBitmap *FontSourceSingle::GetCharBitmap(const u32char &ch) + { + if(hgl::isspace(ch))return(nullptr); //不能显示的数据或是空格 + + FontBitmap *bmp; + + if(chars_bitmap.Get(ch,bmp)) + return bmp; + + bmp=new FontBitmap; + + memset(bmp,0,sizeof(FontBitmap)); + + if(!MakeCharBitmap(bmp,ch)) + { + delete bmp; + chars_bitmap.Add(ch,nullptr); + return(nullptr); + } + else + { + chars_bitmap.Add(ch,bmp); + return bmp; + } + } + }//namespace graph +}//namespace hgl diff --git a/src/SceneGraph/font/FontSourceWin.cpp b/src/SceneGraph/font/FontSourceWin.cpp index 2bc5fb66..90b48560 100644 --- a/src/SceneGraph/font/FontSourceWin.cpp +++ b/src/SceneGraph/font/FontSourceWin.cpp @@ -57,7 +57,7 @@ namespace hgl } }//namespace - WinBitmapFont::WinBitmapFont(const Font &f):FontSource(f) + WinBitmapFont::WinBitmapFont(const Font &f):FontSourceSingle(f) { hdc=CreateCompatibleDC(0); @@ -103,13 +103,13 @@ namespace hgl if(ch>0xFFFF) return(false); - memset(&gm,0,sizeof(GLYPHMETRICS)); - memset(&mat,0,sizeof(MAT2)); + hgl_zero(gm); + hgl_zero(mat); mat.eM11.value = 1; mat.eM22.value = 1; - const int size=GetGlyphOutline(hdc,ch,ggo,&gm,0,0,&mat); + const int size=GetGlyphOutlineW(hdc,ch,ggo,&gm,0,0,&mat); if(size<=0)return(false); @@ -121,7 +121,7 @@ namespace hgl buffer=new uint8[buffer_size]; } - GetGlyphOutline(hdc,ch,ggo,&gm,buffer_size,buffer,&mat); + GetGlyphOutlineW(hdc,ch,ggo,&gm,buffer_size,buffer,&mat); bmp->w=gm.gmBlackBoxX; bmp->h=gm.gmBlackBoxY; diff --git a/src/SceneGraph/font/FontSourceWin.h b/src/SceneGraph/font/FontSourceWin.h index 86d5bfff..2c511526 100644 --- a/src/SceneGraph/font/FontSourceWin.h +++ b/src/SceneGraph/font/FontSourceWin.h @@ -8,7 +8,7 @@ namespace hgl { namespace graph { - class WinBitmapFont:public FontSource + class WinBitmapFont:public FontSourceSingle { HDC hdc; HFONT hfont; @@ -18,7 +18,7 @@ namespace hgl uint ggo; - unsigned char *buffer; + uint8 *buffer; int buffer_size; protected: