From 18f68df9bea5e2cc493b700ff527ea80d915a7c5 Mon Sep 17 00:00:00 2001 From: hyzboy Date: Sun, 28 Jun 2020 22:16:07 +0800 Subject: [PATCH] add Font/FontBitmapCache --- CMCore | 2 +- example/Vulkan/CMakeLists.txt | 5 ++- example/Vulkan/DrawText.cpp | 47 +++++++++++++++++++++ inc/hgl/graph/font/Font.h | 35 ++++++++++++++++ inc/hgl/graph/font/FontBitmapCache.h | 54 +++++++++++++++++++++++++ src/SceneGraph/CMakeLists.txt | 10 ++++- src/SceneGraph/font/Font.cpp | 27 +++++++++++++ src/SceneGraph/font/FontBitmapCache.cpp | 36 +++++++++++++++++ 8 files changed, 213 insertions(+), 3 deletions(-) create mode 100644 example/Vulkan/DrawText.cpp create mode 100644 inc/hgl/graph/font/Font.h create mode 100644 inc/hgl/graph/font/FontBitmapCache.h create mode 100644 src/SceneGraph/font/Font.cpp create mode 100644 src/SceneGraph/font/FontBitmapCache.cpp diff --git a/CMCore b/CMCore index b078d6cb..bb66ce1f 160000 --- a/CMCore +++ b/CMCore @@ -1 +1 @@ -Subproject commit b078d6cbdc95da29bdb068e5079e77bfd3745a89 +Subproject commit bb66ce1f5a4b1f9dcbe61758813ffdd34ae7c1e3 diff --git a/example/Vulkan/CMakeLists.txt b/example/Vulkan/CMakeLists.txt index 5bcf5357..d30cf8ad 100644 --- a/example/Vulkan/CMakeLists.txt +++ b/example/Vulkan/CMakeLists.txt @@ -21,7 +21,10 @@ CreateProject(08.SceneTree SceneTree.cpp) CreateProject(09.LoadStaticMesh LoadStaticMesh.cpp LoadScene.cpp) CreateProject(10.InlineGeometryScene InlineGeometryScene.cpp) CreateProject(11.Atomsphere Atomsphere.cpp) +CreateProject(12.DrawText DrawText.cpp) + #CreateProject(12.PBRBasic PBRBasic.cpp) #CreateProject(12.Deferred Deferred.cpp) -CreateProject(13.DeferredModel DeferredModel.cpp) +#CreateProject(13.DeferredModel DeferredModel.cpp) #CreateProject(14.AutoMaterial auto_material.cpp) + diff --git a/example/Vulkan/DrawText.cpp b/example/Vulkan/DrawText.cpp new file mode 100644 index 00000000..73dc9a81 --- /dev/null +++ b/example/Vulkan/DrawText.cpp @@ -0,0 +1,47 @@ +#include +#include +#include + +using namespace hgl; + +namespace hgl +{ + namespace graph + { + /** + * 文本位图缓冲 + */ + class FontBitmapCache + { + /** + * 字体位图数据 + */ + struct Bitmap + { + int count; //使用次数 + + int x,y; //图像显示偏移 + int w,h; //图像尺寸 + + int adv_x,adv_y;//字符尺寸 + + uint8 *data; + };//struct Bitmap + + protected: + + MapObject chars_bitmap; ///<字符位图 + + protected: + + virtual bool MakeCharBitmap(u32char)=0; ///<产生字体数据 + virtual int GetLineHeight()const=0; ///<取得行高 + + public: + + FontBitmapCache(const Font &); + + FontBitmapCache::Bitmap *GetCharBitmap(const u32char); ///<取得字符位图数据 + };//class FontBitmapCache + }//namespace graph +}//namespace hgl \ No newline at end of file diff --git a/inc/hgl/graph/font/Font.h b/inc/hgl/graph/font/Font.h new file mode 100644 index 00000000..b5ed4ed2 --- /dev/null +++ b/inc/hgl/graph/font/Font.h @@ -0,0 +1,35 @@ +#ifndef HGL_GRAPH_FONT_INCLUDE +#define HGL_GRAPH_FONT_INCLUDE + +#include +#include + +namespace hgl +{ + namespace graph + { + /** + * 字体信息 + */ + struct Font + { + char name[128]; ///<字体名称(utf8) + + int width; ///<宽度 + int height; ///<高度 + + bool bold; ///<加粗 + bool italic; ///<右斜 + + bool anti; ///<反矩齿 + + public: + + Font(); + Font(const char *,int,int,bool,bool,bool=true); + + CompOperatorMemcmp(const Font &); ///<比较操作符重载 + };//struct Font + }//namespace graph +}//namespace hgl +#endif//HGL_GRAPH_FONT_INCLUDE \ No newline at end of file diff --git a/inc/hgl/graph/font/FontBitmapCache.h b/inc/hgl/graph/font/FontBitmapCache.h new file mode 100644 index 00000000..c2e6d5ed --- /dev/null +++ b/inc/hgl/graph/font/FontBitmapCache.h @@ -0,0 +1,54 @@ +#ifndef HGL_GRAPH_FONT_BITMAP_CACHE_INCLUDE +#define HGL_GRAPH_FONT_BITMAP_CACHE_INCLUDE + +#include +#include +#include + +using namespace hgl; + +namespace hgl +{ + namespace graph + { + /** + * 文本位图缓冲 + */ + class FontBitmapCache + { + /** + * 字体位图数据 + */ + struct Bitmap + { + int count; //使用次数 + + int x,y; //图像显示偏移 + int w,h; //图像尺寸 + + int adv_x,adv_y;//字符尺寸 + + uint8 *data; + };//struct Bitmap + + protected: + + Font fnt; + + MapObject chars_bitmap; ///<字符位图 + + protected: + + virtual bool MakeCharBitmap(FontBitmapCache::Bitmap *,u32char)=0; ///<产生字体数据 + virtual int GetLineHeight()const=0; ///<取得行高 + + public: + + FontBitmapCache(const Font &f){fnt=f;} + virtual ~FontBitmapCache()=default; + + FontBitmapCache::Bitmap *GetCharBitmap(const u32char &); ///<取得字符位图数据 + };//class FontBitmapCache + }//namespace graph +}//namespace hgl +#endif//HGL_GRAPH_FONT_BITMAP_CACHE_INCLUDE \ No newline at end of file diff --git a/src/SceneGraph/CMakeLists.txt b/src/SceneGraph/CMakeLists.txt index d217f637..3b19928c 100644 --- a/src/SceneGraph/CMakeLists.txt +++ b/src/SceneGraph/CMakeLists.txt @@ -42,6 +42,11 @@ SET(SCENE_GRAPH_SOURCE RenderList.cpp #SceneFile.cpp ) +file(GLOB FONT_HEADER ${ROOT_INCLUDE_PATH}/hgl/graph/font/*.*) +file(GLOB FONT_SOURCE font/*.*) + +SOURCE_GROUP("Font" FILES ${FONT_HEADER} ${FONT_SOURCE}) + SOURCE_GROUP("Header Files" FILES ${SCENE_GRAPH_HEADER}) SOURCE_GROUP("Source Files" FILES ${SCENE_GRAPH_SOURCE}) @@ -53,4 +58,7 @@ add_cm_library(ULRE.SceneGraph "ULRE" ${SCENE_GRAPH_HEADER} ${SG_MATERIAL_HEADER} ${SG_MATERIAL_SOURCE} - ${SG_VERTEX_SOURCE}) + ${SG_VERTEX_SOURCE} + + ${FONT_HEADER} + ${FONT_SOURCE}) diff --git a/src/SceneGraph/font/Font.cpp b/src/SceneGraph/font/Font.cpp new file mode 100644 index 00000000..a9b32409 --- /dev/null +++ b/src/SceneGraph/font/Font.cpp @@ -0,0 +1,27 @@ +#include + +namespace hgl +{ + namespace graph + { + Font::Font() + { + memset(this,0,sizeof(Font)); + } + + Font::Font(const char *n,int w,int h,bool b,bool i,bool aa) + { + memset(this,0,sizeof(Font)); + + strcpy(name,n); + + width=w; + height=h; + + bold=b; + italic=i; + + anti=aa; + } + }//namespace graph +}//namespace hgl diff --git a/src/SceneGraph/font/FontBitmapCache.cpp b/src/SceneGraph/font/FontBitmapCache.cpp new file mode 100644 index 00000000..b2ace63f --- /dev/null +++ b/src/SceneGraph/font/FontBitmapCache.cpp @@ -0,0 +1,36 @@ +#include + +namespace hgl +{ + namespace graph + { + FontBitmapCache::Bitmap *FontBitmapCache::GetCharBitmap(const u32char &ch) + { + if(!this) + return(nullptr); + + if(hgl::isspace(ch))return(nullptr); //不能显示的数据或是空格 + + FontBitmapCache::Bitmap *bmp; + + if(chars_bitmap.Get(ch,bmp)) + return bmp; + + bmp=new FontBitmapCache::Bitmap; + + memset(bmp,0,sizeof(FontBitmapCache::Bitmap)); + + 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