axmol/core/2d/FontAtlas.h

189 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-11-11 17:20:21 +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_FONTATLAS_H_
#define _AX_FONTATLAS_H_
2019-11-23 20:27:39 +08:00
/// @cond DO_NOT_SHOW
#include <string>
#include <unordered_map>
#include "platform/PlatformMacros.h"
#include "base/Object.h"
#include "platform/StdC.h" // ssize_t on windows
#include "renderer/Texture2D.h"
2019-11-23 20:27:39 +08:00
2024-04-08 01:55:41 +08:00
#include "base/Map.h"
#include "2d/FontFreeType.h"
namespace ax
{
2019-11-23 20:27:39 +08:00
class Font;
class EventCustom;
class EventListenerCustom;
class FontFreeType;
struct FontLetterDefinition
{
float U;
float V;
float width;
float height;
float offsetX;
float offsetY;
int textureID;
bool validDefinition;
int xAdvance;
bool rotated;
2019-11-23 20:27:39 +08:00
};
class AX_DLL FontAtlas : public Object
2019-11-23 20:27:39 +08:00
{
public:
static const int CacheTextureWidth;
static const int CacheTextureHeight;
static const char* CMD_PURGE_FONTATLAS;
static const char* CMD_RESET_FONTATLAS;
static void loadFontAtlas(std::string_view fontatlasFile, hlookup::string_map<FontAtlas*>& outAtlasMap);
2019-11-23 20:27:39 +08:00
/**
* @js ctor
*/
2021-11-11 17:20:21 +08:00
FontAtlas(Font* theFont);
FontAtlas(Font* theFont, int atlasWidth, int atlasHeight, float scaleFactor = 1.0f);
2019-11-23 20:27:39 +08:00
/**
* @js NA
* @lua NA
*/
virtual ~FontAtlas();
2021-12-25 10:04:45 +08:00
void addLetterDefinition(char32_t utf32Char, const FontLetterDefinition& letterDefinition);
bool getLetterDefinitionForChar(char32_t utf32Char, FontLetterDefinition& letterDefinition);
2019-11-23 20:27:39 +08:00
bool prepareLetterDefinitions(const std::u32string& utf16String);
const auto& getLetterDefinitions() const { return _letterDefinitions; }
const std::unordered_map<unsigned int, Texture2D*>& getTextures() const { return _atlasTextures; }
virtual void addNewPage();
void addNewPageWithData(const uint8_t* data, size_t size);
void setTexture(unsigned int slot, Texture2D* texture);
Texture2D* getTexture(int slot);
2019-11-23 20:27:39 +08:00
float getLineHeight() const { return _lineHeight; }
2021-12-25 10:04:45 +08:00
void setLineHeight(float newHeight);
std::string_view getFontName() const;
2019-11-23 20:27:39 +08:00
const Font* getFont() const { return _font; }
/** listen the event that renderer was recreated on Android/WP8
It only has effect on Android and WP8.
*/
2021-12-25 10:04:45 +08:00
void listenRendererRecreated(EventCustom* event);
2019-11-23 20:27:39 +08:00
/** Removes textures atlas.
It will purge the textures atlas and if multiple texture exist in the FontAtlas.
*/
void purgeTexturesAtlas();
/** sets font texture parameters:
- GL_TEXTURE_MIN_FILTER = GL_LINEAR
- GL_TEXTURE_MAG_FILTER = GL_LINEAR
*/
2021-12-25 10:04:45 +08:00
void setAntiAliasTexParameters();
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
/** sets font texture parameters:
- GL_TEXTURE_MIN_FILTER = GL_NEAREST
- GL_TEXTURE_MAG_FILTER = GL_NEAREST
*/
void setAliasTexParameters();
2019-11-23 20:27:39 +08:00
protected:
void initWithSettings(void* opaque /*simdjson::ondemand::document*/);
2019-11-23 20:27:39 +08:00
void reset();
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
void reinit();
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
void releaseTextures();
void findNewCharacters(const std::u32string& u32Text, std::unordered_set<char32_t>& charCodeSet);
2019-11-23 20:27:39 +08:00
/**
* Scale each font letter by scaleFactor.
*
* @param scaleFactor A float scale factor for scaling font letter info.
*/
void scaleFontLetterDefinition(float scaleFactor);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
void updateTextureContent(backend::PixelFormat format, int startY);
std::unordered_map<unsigned int, Texture2D*> _atlasTextures;
2019-11-23 20:27:39 +08:00
std::unordered_map<char32_t, FontLetterDefinition> _letterDefinitions;
2024-04-08 01:55:41 +08:00
StringMap<FontFreeType*> _missingFallbackFonts; // maybe style no needs?
std::unordered_map<char32_t, std::pair<FontFreeType*, unsigned int>> _missingGlyphFallbackFonts;
2021-12-25 10:04:45 +08:00
Font* _font = nullptr;
2019-11-23 20:27:39 +08:00
FontFreeType* _fontFreeType = nullptr;
int _width = 0; // atlas width
int _height = 0; // atlas height
float _scaleFactor = 1.0f;
float _lineHeight = 0.f;
2019-11-23 20:27:39 +08:00
// Dynamic GlyphCollection related stuff
int _currentPage = -1;
backend::PixelFormat _pixelFormat = backend::PixelFormat::NONE;
int _strideShift = 0;
uint8_t* _currentPageData = nullptr;
int _currentPageDataSize = 0;
float _currentPageOrigX = 0;
float _currentPageOrigY = 0;
int _letterPadding = 0;
int _letterEdgeExtend = 0;
2021-12-25 10:04:45 +08:00
int _fontAscender = 0;
2019-11-23 20:27:39 +08:00
EventListenerCustom* _rendererRecreatedListener = nullptr;
2021-12-25 10:04:45 +08:00
bool _antialiasEnabled = true;
int _currLineHeight = 0;
2019-11-23 20:27:39 +08:00
friend class Label;
};
}
2019-11-23 20:27:39 +08:00
/// @endcond
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_FONTATLAS_H_) */