axmol/core/2d/FontFNT.h

195 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.
Copyright (c) 2019-present Axmol Engine contributors (see AUTHORS.md).
2021-12-25 10:04:45 +08:00
https://axmol.dev/
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.
****************************************************************************/
Release 2.1.5 (#2076) * Fix unexpected libpng used * Fix string format incorrect for tests * Fix #1751, use coroutine control AutoTest flow * Update CHANGELOG.md * Added OpenType font (.otf) to the noCompress list. (#2077) * Update 1k & copyright notice in some sources * Move doctest to axmol 3rdparty * Fix ci * Update 1kdist to v90 * Update 1kiss.ps1 * DrawNodeV2 0.95.1 (#2079) * Rename remaining legacy engine related spells and improve code style * Update 3rdparty README.md * Fix checkReallySupportsASTC does not work on ios device reported by @BIGCATDOG in https://github.com/axmolengine/axmol/issues/2078 * Fix ci * FastRNG: add missing include for AXASSERT (#2081) * Delete unused files * Improve FileUtils - Rename FileUtils::createDirectory to FileUtils::createDirectories - Use splitpath_cb to optimize FileUtils::createDirectories - Rename FileUtils::getFileShortName to FileUtils::getPathBaseName - Rename FileUtils::getFileExtension to FileUtils::getPathExtension - Add FileUtils::getPathDirName - Add FileUtils::getPathBaseNameNoExtension - Mark all renamed FileUtils stubs old name deprecated - Mark all FileUtils offthread APIs deprecated * Update box2d to v2.4.2 * Disable /sdl checks explicitly for winuwp For axmol deprecated policy, we need disable /sdl checks explicitly to avoid compiler traits invoking deprecated functions as error * Update cppwinrt to 2.0.240405.15 * Update simdjson to 3.10.0 * Fix box2d testbed compile error * Improve file path to url * Fix FileUtils::createDirectories unix logic * axmol-cmdline: remove arch suffix for host build output directory * Update CHANGELOG.md * Update lua bindings --------- Co-authored-by: Dani Alias <danielgutierrezalias@gmail.com> Co-authored-by: aismann <icesoft@freenet.de> Co-authored-by: smilediver <smilediver@outlook.com>
2024-08-11 21:11:35 +08:00
#ifndef _AX_FONTFNT_H_
#define _AX_FONTFNT_H_
2019-11-23 20:27:39 +08:00
/// @cond DO_NOT_SHOW
#include "2d/Font.h"
#include <set>
#include <unordered_map>
2019-11-23 20:27:39 +08:00
namespace ax
{
2019-11-23 20:27:39 +08:00
/**
@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 AX_DLL BMFontConfiguration : public Object
{
// 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 */
static BMFontConfiguration* create(std::string_view FNTfile);
/** initializes a BitmapFontConfiguration with a FNT file */
bool initWithFNTfile(std::string_view FNTfile);
std::string_view getAtlasName() { return _atlasName; }
void setAtlasName(std::string_view atlasName) { _atlasName = atlasName; }
std::set<unsigned int>* getCharacterSet() const;
protected:
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,
std::string_view controlFile);
private:
unsigned int parseCharacterDefinition(const char* line);
void parseInfoArguments(const char* line);
void parseCommonArguments(const char* line);
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
2022-07-16 10:43:05 +08:00
class AX_DLL FontFNT : public Font
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
public:
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);
#ifndef AX_CORE_PROFILE
AX_DEPRECATED(2.1) static FontFNT* create(std::string_view fntFilePath, const Vec2& imageOffset = Vec2::ZERO);
#endif
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
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
}
2019-11-23 20:27:39 +08:00
Release 2.1.5 (#2076) * Fix unexpected libpng used * Fix string format incorrect for tests * Fix #1751, use coroutine control AutoTest flow * Update CHANGELOG.md * Added OpenType font (.otf) to the noCompress list. (#2077) * Update 1k & copyright notice in some sources * Move doctest to axmol 3rdparty * Fix ci * Update 1kdist to v90 * Update 1kiss.ps1 * DrawNodeV2 0.95.1 (#2079) * Rename remaining legacy engine related spells and improve code style * Update 3rdparty README.md * Fix checkReallySupportsASTC does not work on ios device reported by @BIGCATDOG in https://github.com/axmolengine/axmol/issues/2078 * Fix ci * FastRNG: add missing include for AXASSERT (#2081) * Delete unused files * Improve FileUtils - Rename FileUtils::createDirectory to FileUtils::createDirectories - Use splitpath_cb to optimize FileUtils::createDirectories - Rename FileUtils::getFileShortName to FileUtils::getPathBaseName - Rename FileUtils::getFileExtension to FileUtils::getPathExtension - Add FileUtils::getPathDirName - Add FileUtils::getPathBaseNameNoExtension - Mark all renamed FileUtils stubs old name deprecated - Mark all FileUtils offthread APIs deprecated * Update box2d to v2.4.2 * Disable /sdl checks explicitly for winuwp For axmol deprecated policy, we need disable /sdl checks explicitly to avoid compiler traits invoking deprecated functions as error * Update cppwinrt to 2.0.240405.15 * Update simdjson to 3.10.0 * Fix box2d testbed compile error * Improve file path to url * Fix FileUtils::createDirectories unix logic * axmol-cmdline: remove arch suffix for host build output directory * Update CHANGELOG.md * Update lua bindings --------- Co-authored-by: Dani Alias <danielgutierrezalias@gmail.com> Co-authored-by: aismann <icesoft@freenet.de> Co-authored-by: smilediver <smilediver@outlook.com>
2024-08-11 21:11:35 +08:00
#endif /* defined(_AX_FONTFNT_H_) */