现在的位置: 首页 > 综合 > 正文

OGRE资源–Font

2018年01月16日 ⁄ 综合 ⁄ 共 2405字 ⁄ 字号 评论关闭

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

Ogre中的Resource共用如下几种:

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

Resource种类

本文的主角是Font

相关的头文件有:

OgreFont.h

OgreFontManager.h

(1)Font

Ogre之Font

首先来看下Font的成员变量

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

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

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

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

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

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

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

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

Real则是长宽比例。

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

  1. /// A range of code points, inclusive on both ends
  2. typedef std::pair<CodePoint, CodePoint> CodePointRange;
  3. typedef vector<CodePointRange>::type CodePointRangeList;
  4. /// Range of code points to generate glyphs for (truetype only)
  5. CodePointRangeList mCodePointRangeList;
  6. /// Map from unicode code point to texture coordinates
  7. typedef map<CodePoint, GlyphInfo>::type CodePointMap;
  8. 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

fontManager

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

FontManager中最主要的两个函数为

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

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

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

【上篇】
【下篇】

抱歉!评论已关闭.