CCFontAtlasCache,CCFontCharMap,CCFontFNT,CCFontFreeType解析

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/c_boy_lu/article/details/48168157

CCFontAtlasCache:


代码:

class FontAtlas;
// 字体图集缓存
class CC_DLL FontAtlasCache
{  
public:
// 得到字体图集
    static FontAtlas * getFontAtlasTTF(const TTFConfig & config);
    static FontAtlas * getFontAtlasFNT(const std::string& fontFileName, const Vec2& imageOffset = Vec2::ZERO);

    static FontAtlas * getFontAtlasCharMap(const std::string& charMapFile, int itemWidth, int itemHeight, int startCharMap);
    static FontAtlas * getFontAtlasCharMap(Texture2D* texture, int itemWidth, int itemHeight, int startCharMap);
    static FontAtlas * getFontAtlasCharMap(const std::string& plistFile);
    
    static bool releaseFontAtlas(FontAtlas *atlas);

    /** Removes cached data.
     It will purge the textures atlas and if multiple texture exist in one FontAtlas.
     */
	 // 清除缓存数据
    static void purgeCachedData();
    
private: 
// 生成字体名字
    static std::string generateFontName(const std::string& fontFileName, int size, GlyphCollection theGlyphs, bool useDistanceField);
    static std::unordered_map<std::string, FontAtlas *> _atlasMap;
};

NS_CC_END


CCFontCharMap:

代码:

NS_CC_BEGIN
// 字体字符图
class FontCharMap : public Font
{  
public:
// 创建一个字体字符图
    static FontCharMap * create(const std::string& charMapFile, int itemWidth, int itemHeight, int startCharMap);
    static FontCharMap * create(Texture2D* texture, int itemWidth, int itemHeight, int startCharMap);
    static FontCharMap * create(const std::string& plistFile);
    // 得到水平字间距
    virtual int* getHorizontalKerningForTextUTF16(const std::u16string& text, int &outNumLetters) const override;
    // 创建一个字体图集
	virtual FontAtlas *createFontAtlas() override;
    
protected:    
// 构造函数
    FontCharMap(Texture2D* texture,int itemWidth, int itemHeight, int startCharMap)
        :_texture(texture)
        ,_mapStartChar(startCharMap)
        ,_itemWidth(itemWidth)
        ,_itemHeight(itemHeight)
    {}
    /**
     * @js NA
     * @lua NA
     */
    virtual ~FontCharMap();
    
private:
    Texture2D* _texture;
    int _mapStartChar;
    int _itemWidth;
    int _itemHeight;

};

CCFontFNT:

代码:

// FNT字体
class CC_DLL FontFNT : public Font
{
    
public:
    // 创建一个FNT字体
    static FontFNT * create(const std::string& fntFilePath, const Vec2& imageOffset = Vec2::ZERO);
    /** Purges the cached data.
    Removes from memory the cached configurations and the atlas name dictionary.
    */
	// 清除缓存的数据
    static void purgeCachedData();
	// 得到水平字间距
    virtual int* getHorizontalKerningForTextUTF16(const std::u16string& text, int &outNumLetters) const override;
	// 创建字体图集
    virtual FontAtlas *createFontAtlas() override;
    
protected:
    // 构造函数
    FontFNT(BMFontConfiguration *theContfig, const Vec2& imageOffset = Vec2::ZERO);
    /**
     * @js NA
     * @lua NA
     */
    virtual ~FontFNT();
    
private:
    // 得到水平字间距
    int  getHorizontalKerningForChars(unsigned short firstChar, unsigned short secondChar) const;
    
    BMFontConfiguration * _configuration;
    Vec2                   _imageOffset;
    
};



CCFontFreeType:


代码:

// 自由字体类型
class CC_DLL FontFreeType : public Font
{
public:
    static const int DistanceMapSpread;
// 创建一个自由字体类型
    static FontFreeType * create(const std::string &fontName, int fontSize, GlyphCollection glyphs, const char *customGlyphs,bool distanceFieldEnabled = false,int outline = 0);
// 关闭自由字体类型
    static void shutdownFreeType();

    bool     isDistanceFieldEnabled() const { return _distanceFieldEnabled;}
	// 得到描边的尺寸
    float    getOutlineSize() const { return _outlineSize; }
	// 渲染字符
    void     renderCharAt(unsigned char *dest,int posX, int posY, unsigned char* bitmap,long bitmapWidth,long bitmapHeight); 
// 创建字体图集
    virtual FontAtlas   * createFontAtlas() override;
	// 得到UTF-16水平的字间距
    virtual int         * getHorizontalKerningForTextUTF16(const std::u16string& text, int &outNumLetters) const override;
    // 得到标志符的位图资源
    unsigned char       * getGlyphBitmap(unsigned short theChar, long &outWidth, long &outHeight, Rect &outRect,int &xAdvance);
    // 得到字体的最大高度
    virtual int           getFontMaxHeight() const override;  
	// 得到小写字母超出部分
    virtual int           getFontAscender() const;

protected:
    // 构造函数
    FontFreeType(bool distanceFieldEnabled = false,int outline = 0);
    virtual ~FontFreeType();
	// 创建一个字体对象
    bool   createFontObject(const std::string &fontName, int fontSize);
    
private:
// 初始化自由类型
    bool initFreeType();
	// 得到FreeFont库
    FT_Library getFTLibrary();
    // 得到水平字间距
    int  getHorizontalKerningForChars(unsigned short firstChar, unsigned short secondChar) const;
	// 得到描边尺寸
    unsigned char       * getGlyphBitmapWithOutline(unsigned short theChar, FT_BBox &bbox);
    
    static FT_Library _FTlibrary;
    static bool       _FTInitialized;
    FT_Face           _fontRef;
    FT_Stroker        _stroker;
    std::string       _fontName;
    bool              _distanceFieldEnabled;
    float             _outlineSize;
};


展开阅读全文

没有更多推荐了,返回首页