Ogre引擎源码——资源之Font

http://blog.****.net/hunter8777/article/details/6231284

Resource部分源码已经解读过了,对Ogre如何管理Resource的流程有了一定的了解。下面就来具体看下每种资源的实现。

Ogre中的Resource共用如下几种:

Texture、Compositor、Font、GpuProgram、Material、Mesh、Skeleton、BspLevel

Ogre引擎源码——资源之Font

本文的主角是Font

相关的头文件有:

OgreFont.h

OgreFontManager.h

(1)Font

Ogre引擎源码——资源之Font

首先来看下Font的成员变量

  1. ///Thetypeoffont
  2. FontTypemType;
  3. ///Sourceofthefont(eitheranimagenameoratruetypefont)
  4. StringmSource;
  5. ///Sizeofthetruetypefont,inpoints
  6. RealmTtfSize;
  7. ///Resolution(dpi)oftruetypefont
  8. uintmTtfResolution;
  9. ///Maxdistancetobaselineofthis(truetype)font
  10. intmTtfMaxBearingY;
  1. ///Thetypeoffont
  2. FontTypemType;
  3. ///Sourceofthefont(eitheranimagenameoratruetypefont)
  4. StringmSource;
  5. ///Sizeofthetruetypefont,inpoints
  6. RealmTtfSize;
  7. ///Resolution(dpi)oftruetypefont
  8. uintmTtfResolution;
  9. ///Maxdistancetobaselineofthis(truetype)font
  10. intmTtfMaxBearingY;
/// The type of font FontType mType; /// Source of the font (either an image name or a truetype font) String mSource; /// Size of the truetype font, in points Real mTtfSize; /// Resolution (dpi) of truetype font uint mTtfResolution; /// Max distance to baseline of this (truetype) font int mTtfMaxBearingY;

第一个变量类FontType是一个枚举,标示当前字体类型。Ogre中Font只有两种类型:truetype和image。

  1. enumFontType
  2. {
  3. ///Generatedfromatruetype(.ttf)font
  4. FT_TRUETYPE=1,
  5. ///Loadedfromanimagecreatedbyanartist
  6. FT_IMAGE=2
  7. };
  1. enumFontType
  2. {
  3. ///Generatedfromatruetype(.ttf)font
  4. FT_TRUETYPE=1,
  5. ///Loadedfromanimagecreatedbyanartist
  6. FT_IMAGE=2
  7. };
enum FontType { /// Generated from a truetype (.ttf) font FT_TRUETYPE = 1, /// Loaded from an image created by an artist FT_IMAGE = 2 };

其余四个变量顾名思义。其中后三个和truetype类型字体有关。

Font中还有一个重要的结构体定义GlyphInfo,即字体轮廓信息。

  1. structGlyphInfo
  2. {
  3. CodePointcodePoint;
  4. UVRectuvRect;
  5. RealaspectRatio;
  6. GlyphInfo(CodePointid,constUVRect&rect,Realaspect)
  7. :codePoint(id),uvRect(rect),aspectRatio(aspect)
  8. {
  9. }
  10. };
  1. structGlyphInfo
  2. {
  3. CodePointcodePoint;
  4. UVRectuvRect;
  5. RealaspectRatio;
  6. GlyphInfo(CodePointid,constUVRect&rect,Realaspect)
  7. :codePoint(id),uvRect(rect),aspectRatio(aspect)
  8. {
  9. }
  10. };
struct GlyphInfo { CodePoint codePoint; UVRect uvRect; Real aspectRatio; GlyphInfo(CodePoint id, const UVRect& rect, Real aspect) : codePoint(id), uvRect(rect), aspectRatio(aspect) { } };

CodePoint是一个无符号32位整型,用来表示一个unicode。

UVRect是一个模板参数类型为浮点数的矩形TRect<float>,用来表示字体轮廓大小。

Real则是长宽比例。

由于一个Font类通常表示一段编码范围的字体,所以该类中主要存储的数据类型定义如下

  1. ///Arangeofcodepoints,inclusiveonbothends
  2. typedefstd::pair<CodePoint,CodePoint>CodePointRange;
  3. typedefvector<CodePointRange>::typeCodePointRangeList;
  4. ///Rangeofcodepointstogenerateglyphsfor(truetypeonly)
  5. CodePointRangeListmCodePointRangeList;
  6. ///Mapfromunicodecodepointtotexturecoordinates
  7. typedefmap<CodePoint,GlyphInfo>::typeCodePointMap;
  8. CodePointMapmCodePointMap;
  1. ///Arangeofcodepoints,inclusiveonbothends
  2. typedefstd::pair<CodePoint,CodePoint>CodePointRange;
  3. typedefvector<CodePointRange>::typeCodePointRangeList;
  4. ///Rangeofcodepointstogenerateglyphsfor(truetypeonly)
  5. CodePointRangeListmCodePointRangeList;
  6. ///Mapfromunicodecodepointtotexturecoordinates
  7. typedefmap<CodePoint,GlyphInfo>::typeCodePointMap;
  8. CodePointMapmCodePointMap;
/// A range of code points, inclusive on both ends typedef std::pair<CodePoint, CodePoint> CodePointRange; typedef vector<CodePointRange>::type CodePointRangeList; /// Range of code points to generate glyphs for (truetype only) CodePointRangeList mCodePointRangeList; /// Map from unicode code point to texture coordinates typedef map<CodePoint, GlyphInfo>::type CodePointMap; CodePointMap mCodePointMap;

RangeList是以CodePoint的范围成对存储;map则是将每个CodePoint与相应的字体轮廓信息对应起来。

剩下的成员变量是Material和Texture,即保存字体在内存中以便显示的形式。

Material和Texture也都是Resource的一种,留以后解读源码。

再来关注下Font的成员函数

从UML图中可以看出,Font类不仅继承了Resource,还继承了ManualResourceLoader。继承自后者表示Font类支持自定义load方法,以手动方式导入资源。

所以,除去标准的getter/setter函数,Font主要实现了从Resource接口继承来的loadImpl和unloadImpl方法以及ManualResourceLoader类的loadResource方法。

首先是loadResource函数。

这个函数作用是导入字体文件,它封装了FreeType库load字体文件的操作,主要完成以下流程:

  • 通过FT库导入fft文件
  • 获取所有字体中最大宽度和高度
  • 计算所需要的纹理大小总数
  • 分别计算每个字体的轮廓信息

当导入了字体文件后,装载资源的流程就主要表现为设置material和texture,这就需要在loadImpl和unloadImpl函数中设置。

(2)FontManager

Ogre引擎源码——资源之Font

如同在资源管理一文中写到的意义,FontManager负责创建Font(ResourceManager负责创建Resource)。除此之外,在构造函数中,FontManager还要将自己注册入ResourceManager,方便后者进行管理。

FontManager中最主要的两个函数为

  1. voidparseScript(DataStreamPtr&stream,constString&groupName);
  2. voidparseAttribute(constString&line,FontPtr&pFont);
  1. voidparseScript(DataStreamPtr&stream,constString&groupName);
  2. voidparseAttribute(constString&line,FontPtr&pFont);
void parseScript(DataStreamPtr& stream, const String& groupName); void parseAttribute(const String& line, FontPtr& pFont);

parseScript函数负责解析后缀为fontdef的脚本。

parseAttribute则将parseScript解析出来的相应属性,对Font类进行设置。设置方法是通过StringInterface中的Cmd模式。

Resource部分源码已经解读过了,对Ogre如何管理Resource的流程有了一定的了解。下面就来具体看下每种资源的实现。

Ogre中的Resource共用如下几种:

Texture、Compositor、Font、GpuProgram、Material、Mesh、Skeleton、BspLevel

Ogre引擎源码——资源之Font

本文的主角是Font

相关的头文件有:

OgreFont.h

OgreFontManager.h

(1)Font

Ogre引擎源码——资源之Font

首先来看下Font的成员变量

  1. ///Thetypeoffont
  2. FontTypemType;
  3. ///Sourceofthefont(eitheranimagenameoratruetypefont)
  4. StringmSource;
  5. ///Sizeofthetruetypefont,inpoints
  6. RealmTtfSize;
  7. ///Resolution(dpi)oftruetypefont
  8. uintmTtfResolution;
  9. ///Maxdistancetobaselineofthis(truetype)font
  10. intmTtfMaxBearingY;
  1. ///Thetypeoffont
  2. FontTypemType;
  3. ///Sourceofthefont(eitheranimagenameoratruetypefont)
  4. StringmSource;
  5. ///Sizeofthetruetypefont,inpoints
  6. RealmTtfSize;
  7. ///Resolution(dpi)oftruetypefont
  8. uintmTtfResolution;
  9. ///Maxdistancetobaselineofthis(truetype)font
  10. intmTtfMaxBearingY;
/// The type of font FontType mType; /// Source of the font (either an image name or a truetype font) String mSource; /// Size of the truetype font, in points Real mTtfSize; /// Resolution (dpi) of truetype font uint mTtfResolution; /// Max distance to baseline of this (truetype) font int mTtfMaxBearingY;

第一个变量类FontType是一个枚举,标示当前字体类型。Ogre中Font只有两种类型:truetype和image。

  1. enumFontType
  2. {
  3. ///Generatedfromatruetype(.ttf)font
  4. FT_TRUETYPE=1,
  5. ///Loadedfromanimagecreatedbyanartist
  6. FT_IMAGE=2
  7. };
  1. enumFontType
  2. {
  3. ///Generatedfromatruetype(.ttf)font
  4. FT_TRUETYPE=1,
  5. ///Loadedfromanimagecreatedbyanartist
  6. FT_IMAGE=2
  7. };
enum FontType { /// Generated from a truetype (.ttf) font FT_TRUETYPE = 1, /// Loaded from an image created by an artist FT_IMAGE = 2 };

其余四个变量顾名思义。其中后三个和truetype类型字体有关。

Font中还有一个重要的结构体定义GlyphInfo,即字体轮廓信息。

  1. structGlyphInfo
  2. {
  3. CodePointcodePoint;
  4. UVRectuvRect;
  5. RealaspectRatio;
  6. GlyphInfo(CodePointid,constUVRect&rect,Realaspect)
  7. :codePoint(id),uvRect(rect),aspectRatio(aspect)
  8. {
  9. }
  10. };
  1. structGlyphInfo
  2. {
  3. CodePointcodePoint;
  4. UVRectuvRect;
  5. RealaspectRatio;
  6. GlyphInfo(CodePointid,constUVRect&rect,Realaspect)
  7. :codePoint(id),uvRect(rect),aspectRatio(aspect)
  8. {
  9. }
  10. };
struct GlyphInfo { CodePoint codePoint; UVRect uvRect; Real aspectRatio; GlyphInfo(CodePoint id, const UVRect& rect, Real aspect) : codePoint(id), uvRect(rect), aspectRatio(aspect) { } };

CodePoint是一个无符号32位整型,用来表示一个unicode。

UVRect是一个模板参数类型为浮点数的矩形TRect<float>,用来表示字体轮廓大小。

Real则是长宽比例。

由于一个Font类通常表示一段编码范围的字体,所以该类中主要存储的数据类型定义如下

  1. ///Arangeofcodepoints,inclusiveonbothends
  2. typedefstd::pair<CodePoint,CodePoint>CodePointRange;
  3. typedefvector<CodePointRange>::typeCodePointRangeList;
  4. ///Rangeofcodepointstogenerateglyphsfor(truetypeonly)
  5. CodePointRangeListmCodePointRangeList;
  6. ///Mapfromunicodecodepointtotexturecoordinates
  7. typedefmap<CodePoint,GlyphInfo>::typeCodePointMap;
  8. CodePointMapmCodePointMap;
  1. ///Arangeofcodepoints,inclusiveonbothends
  2. typedefstd::pair<CodePoint,CodePoint>CodePointRange;
  3. typedefvector<CodePointRange>::typeCodePointRangeList;
  4. ///Rangeofcodepointstogenerateglyphsfor(truetypeonly)
  5. CodePointRangeListmCodePointRangeList;
  6. ///Mapfromunicodecodepointtotexturecoordinates
  7. typedefmap<CodePoint,GlyphInfo>::typeCodePointMap;
  8. CodePointMapmCodePointMap;
/// A range of code points, inclusive on both ends typedef std::pair<CodePoint, CodePoint> CodePointRange; typedef vector<CodePointRange>::type CodePointRangeList; /// Range of code points to generate glyphs for (truetype only) CodePointRangeList mCodePointRangeList; /// Map from unicode code point to texture coordinates typedef map<CodePoint, GlyphInfo>::type CodePointMap; CodePointMap mCodePointMap;

RangeList是以CodePoint的范围成对存储;map则是将每个CodePoint与相应的字体轮廓信息对应起来。

剩下的成员变量是Material和Texture,即保存字体在内存中以便显示的形式。

Material和Texture也都是Resource的一种,留以后解读源码。

再来关注下Font的成员函数

从UML图中可以看出,Font类不仅继承了Resource,还继承了ManualResourceLoader。继承自后者表示Font类支持自定义load方法,以手动方式导入资源。

所以,除去标准的getter/setter函数,Font主要实现了从Resource接口继承来的loadImpl和unloadImpl方法以及ManualResourceLoader类的loadResource方法。

首先是loadResource函数。

这个函数作用是导入字体文件,它封装了FreeType库load字体文件的操作,主要完成以下流程:

  • 通过FT库导入fft文件
  • 获取所有字体中最大宽度和高度
  • 计算所需要的纹理大小总数
  • 分别计算每个字体的轮廓信息

当导入了字体文件后,装载资源的流程就主要表现为设置material和texture,这就需要在loadImpl和unloadImpl函数中设置。

(2)FontManager

Ogre引擎源码——资源之Font

如同在资源管理一文中写到的意义,FontManager负责创建Font(ResourceManager负责创建Resource)。除此之外,在构造函数中,FontManager还要将自己注册入ResourceManager,方便后者进行管理。

FontManager中最主要的两个函数为

  1. voidparseScript(DataStreamPtr&stream,constString&groupName);
  2. voidparseAttribute(constString&line,FontPtr&pFont);
  1. voidparseScript(DataStreamPtr&stream,constString&groupName);
  2. voidparseAttribute(constString&line,FontPtr&pFont);
void parseScript(DataStreamPtr& stream, const String& groupName); void parseAttribute(const String& line, FontPtr& pFont);

parseScript函数负责解析后缀为fontdef的脚本。

parseAttribute则将parseScript解析出来的相应属性,对Font类进行设置。设置方法是通过StringInterface中的Cmd模式。