axmol/cocos/2d/CCFontFNT.h

194 lines
5.8 KiB
C
Raw Normal View History

2019-11-23 20:27:39 +08:00
/****************************************************************************
Copyright (c) 2013 Zynga Inc.
Copyright (c) 2013-2016 Chukong Technologies Inc.
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
2021-10-24 14:09:59 +08:00
Copyright (c) 2021 Bytedance Inc.
2021-12-25 10:04:45 +08:00
2021-10-24 14:09:59 +08:00
https://adxe.org
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
#ifndef _CCFontFNT_h_
#define _CCFontFNT_h_
/// @cond DO_NOT_SHOW
#include "2d/CCFont.h"
#include <set>
#include <unordered_map>
2019-11-23 20:27:39 +08:00
NS_CC_BEGIN
/**
@struct BMFontDef
BMFont definition
*/
2021-12-25 10:04:45 +08:00
typedef struct _BMFontDef
{
//! ID of the character
unsigned int charID;
//! origin and size of the font
Rect rect;
//! The X amount the image should be offset when drawing the image (in pixels)
short xOffset;
//! The Y amount the image should be offset when drawing the image (in pixels)
short yOffset;
//! The amount to move the current position after drawing the character (in pixels)
short xAdvance;
} BMFontDef;
/** @struct BMFontPadding
BMFont padding
@since v0.8.2
*/
2021-12-25 10:04:45 +08:00
typedef struct _BMFontPadding
{
/// padding left
int left;
/// padding top
int top;
/// padding right
int right;
/// padding bottom
int bottom;
} BMFontPadding;
/** @brief BMFontConfiguration has parsed configuration of the .fnt file
@since v0.8
*/
class CC_DLL BMFontConfiguration : public Ref
{
// FIXME: Creating a public interface so that the bitmapFontArray[] is accessible
2021-12-25 10:04:45 +08:00
public: //@public
// BMFont definitions
std::unordered_map<int /* key */, BMFontDef /* fontDef */> _fontDefDictionary;
//! FNTConfig: Common Height Should be signed (issue #1343)
int _commonHeight;
//! Padding
2021-12-25 10:04:45 +08:00
BMFontPadding _padding;
//! atlas name
std::string _atlasName;
//! values for kerning
std::unordered_map<uint64_t /* key */, int /* amount */> _kerningDictionary;
// Character Set defines the letters that actually exist in the font
std::set<unsigned int>* _characterSet;
2021-10-24 14:09:59 +08:00
//! Font size
int _fontSize;
2021-12-25 10:04:45 +08:00
public:
/**
* @js ctor
*/
BMFontConfiguration();
/**
* @js NA
* @lua NA
*/
virtual ~BMFontConfiguration();
/**
* @js NA
* @lua NA
*/
std::string description() const;
/** allocates a BMFontConfiguration with a FNT file */
2021-12-26 23:26:34 +08:00
static BMFontConfiguration* create(std::string_view FNTfile);
/** initializes a BitmapFontConfiguration with a FNT file */
2021-12-26 23:26:34 +08:00
bool initWithFNTfile(std::string_view FNTfile);
2021-12-26 23:26:34 +08:00
std::string_view getAtlasName() { return _atlasName; }
void setAtlasName(std::string_view atlasName) { _atlasName = atlasName; }
std::set<unsigned int>* getCharacterSet() const;
protected:
2021-12-26 23:26:34 +08:00
virtual std::set<unsigned int>* parseConfigFile(std::string_view controlFile);
2021-12-25 10:04:45 +08:00
virtual std::set<unsigned int>* parseBinaryConfigFile(unsigned char* pData,
uint32_t size,
2021-12-26 23:26:34 +08:00
std::string_view controlFile);
private:
unsigned int parseCharacterDefinition(const char* line);
void parseInfoArguments(const char* line);
void parseCommonArguments(const char* line);
2021-12-26 23:26:34 +08:00
void parseImageFileName(const char* line, std::string_view fntFile);
void parseKerningEntry(const char* line);
void purgeKerningDictionary();
void purgeFontDefDictionary();
};
2019-11-23 20:27:39 +08:00
class CC_DLL FontFNT : public Font
{
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
public:
2021-12-26 23:26:34 +08:00
static FontFNT* create(std::string_view fntFilePath, const Rect& imageRect, bool imageRotated);
static FontFNT* create(std::string_view fntFilePath, std::string_view subTextureKey);
static FontFNT* create(std::string_view fntFilePath);
2021-12-28 11:00:34 +08:00
CC_DEPRECATED_ATTRIBUTE static FontFNT* create(std::string_view fntFilePath, const Vec2& imageOffset = Vec2::ZERO);
2019-11-23 20:27:39 +08:00
/** Purges the cached data.
Removes from memory the cached configurations and the atlas name dictionary.
*/
static void purgeCachedData();
2021-12-25 10:04:45 +08:00
virtual int* getHorizontalKerningForTextUTF32(const std::u32string& text, int& outNumLetters) const override;
2021-11-11 17:20:21 +08:00
virtual FontAtlas* newFontAtlas() override;
2019-11-23 20:27:39 +08:00
void setFontSize(float fontSize);
float getFontSize() const { return _fontSize; }
2021-12-25 10:04:45 +08:00
int getOriginalFontSize() const;
2019-11-23 20:27:39 +08:00
2021-12-26 23:26:34 +08:00
static void reloadBMFontResource(std::string_view fntFilePath);
2019-11-23 20:27:39 +08:00
protected:
FontFNT(BMFontConfiguration* theContfig, const Rect& imageRect, bool imageRotated);
FontFNT(BMFontConfiguration* theContfig);
2019-11-23 20:27:39 +08:00
/**
* @js NA
* @lua NA
*/
virtual ~FontFNT();
BMFontConfiguration* _configuration;
2021-12-25 10:04:45 +08:00
int getHorizontalKerningForChars(char32_t firstChar, char32_t secondChar) const;
Rect _imageRectInPoints;
bool _imageRotated;
2021-12-25 10:04:45 +08:00
// User defined font size
float _fontSize;
2019-11-23 20:27:39 +08:00
};
/// @endcond
NS_CC_END
#endif /* defined(__cocos2d_libs__CCFontFNT__) */