axmol/cocos/2d/CCFontAtlas.h

169 lines
5.3 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-11-11 17:20:21 +08:00
Copyright (c) 2021 Bytedance Inc.
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 _CCFontAtlas_h_
#define _CCFontAtlas_h_
/// @cond DO_NOT_SHOW
#include <string>
#include <unordered_map>
#include "platform/CCPlatformMacros.h"
#include "base/CCRef.h"
2021-12-25 10:04:45 +08:00
#include "platform/CCStdC.h" // ssize_t on windows
2019-11-23 20:27:39 +08:00
#include "renderer/CCTexture2D.h"
NS_CC_BEGIN
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 CC_DLL FontAtlas : public Ref
{
public:
static const int CacheTextureWidth;
static const int CacheTextureHeight;
static const char* CMD_PURGE_FONTATLAS;
static const char* CMD_RESET_FONTATLAS;
/**
* @js ctor
*/
2021-11-11 17:20:21 +08:00
FontAtlas(Font* theFont);
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 std::unordered_map<ssize_t, Texture2D*>& getTextures() const { return _atlasTextures; }
2021-12-25 10:04:45 +08:00
void addTexture(Texture2D* texture, 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);
2021-12-26 23:26:34 +08:00
std::string_view getFontName() const;
2019-11-23 20:27:39 +08:00
Texture2D* getTexture(int slot);
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 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_map<unsigned int, unsigned int>& charCodeMap);
2021-12-25 10:04:45 +08:00
void conversionU32TOGB2312(const std::u32string& u32Text,
std::unordered_map<unsigned int, unsigned int>& charCodeMap);
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
void initTextureWithZeros(Texture2D* texture);
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<ssize_t, Texture2D*> _atlasTextures;
std::unordered_map<char32_t, FontLetterDefinition> _letterDefinitions;
2021-12-25 10:04:45 +08:00
float _lineHeight = 0.f;
Font* _font = nullptr;
2019-11-23 20:27:39 +08:00
FontFreeType* _fontFreeType = nullptr;
2021-12-25 10:04:45 +08:00
void* _iconv = nullptr;
2019-11-23 20:27:39 +08:00
// Dynamic GlyphCollection related stuff
2021-12-25 10:04:45 +08:00
int _currentPage = 0;
unsigned char* _currentPageData = nullptr;
unsigned char* _currentPageDataRGBA = nullptr;
int _currentPageDataSize = 0;
int _currentPageDataSizeRGBA = 0;
float _currentPageOrigX = 0;
float _currentPageOrigY = 0;
int _letterPadding = 0;
int _letterEdgeExtend = 0;
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;
};
NS_CC_END
/// @endcond
#endif /* defined(__cocos2d_libs__CCFontAtlas__) */