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-08-28 18:24:17 +08:00
|
|
|
Copyright (c) 2021 Bytedance Inc.
|
2021-08-28 20:22:01 +08:00
|
|
|
|
2021-08-28 18:24:17 +08:00
|
|
|
https://adxe.org
|
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:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include "2d/CCFontFreeType.h"
|
2021-06-17 21:15:50 +08:00
|
|
|
|
2021-09-20 18:24:11 +08:00
|
|
|
#include "freetype/ftfntfmt.h"
|
2021-06-17 21:15:50 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
#include FT_BBOX_H
|
|
|
|
#include "2d/CCFontAtlas.h"
|
|
|
|
#include "base/CCDirector.h"
|
|
|
|
#include "base/ccUTF8.h"
|
2021-06-17 21:15:50 +08:00
|
|
|
#include "freetype/internal/tttypes.h"
|
2019-11-23 20:27:39 +08:00
|
|
|
#include "platform/CCFileUtils.h"
|
2020-08-27 11:35:55 +08:00
|
|
|
#include "platform/CCFileStream.h"
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
|
|
|
FT_Library FontFreeType::_FTlibrary;
|
2021-08-28 20:22:01 +08:00
|
|
|
bool FontFreeType::_FTInitialized = false;
|
2021-11-16 18:41:18 +08:00
|
|
|
#if !defined(__ANDROID__)
|
|
|
|
bool FontFreeType::_streamParsingEnabled = true;
|
|
|
|
#else
|
2021-08-28 20:22:01 +08:00
|
|
|
bool FontFreeType::_streamParsingEnabled = false;
|
2021-11-16 18:41:18 +08:00
|
|
|
#endif
|
2021-08-28 20:22:01 +08:00
|
|
|
bool FontFreeType::_doNativeBytecodeHinting = true;
|
2021-09-26 13:03:40 +08:00
|
|
|
const int FontFreeType::DistanceMapSpread = 6;
|
2021-08-28 20:22:01 +08:00
|
|
|
|
2021-11-27 13:51:21 +08:00
|
|
|
using namespace std::string_view_literals;
|
|
|
|
constexpr std::string_view _glyphASCII =
|
2021-08-28 20:22:01 +08:00
|
|
|
"\"!#$%&'()*+,-./"
|
|
|
|
"0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~"
|
2021-11-27 11:06:51 +08:00
|
|
|
"¡¢£¤¥¦§¨©ª«¬®¯°±²³´µ¶·¸¹º»¼½¾¿ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖ×ØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö÷øùúûüýþ "sv;
|
2021-11-27 13:51:21 +08:00
|
|
|
constexpr std::string_view _glyphNEHE =
|
2021-11-27 11:06:51 +08:00
|
|
|
"!\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~ "sv;
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
typedef struct _DataRef
|
|
|
|
{
|
|
|
|
Data data;
|
2021-11-11 16:15:23 +08:00
|
|
|
unsigned int referenceCount = 0;
|
2021-08-28 20:22:01 +08:00
|
|
|
} DataRef;
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
static std::unordered_map<std::string, DataRef> s_cacheFontData;
|
|
|
|
|
2020-08-27 11:35:55 +08:00
|
|
|
// ------ freetype2 stream parsing support ---
|
2021-09-02 13:55:57 +08:00
|
|
|
static unsigned long ft_stream_read_callback(FT_Stream stream,
|
2021-09-20 18:24:11 +08:00
|
|
|
unsigned long offset,
|
|
|
|
unsigned char* buf,
|
|
|
|
unsigned long size)
|
2021-08-28 20:22:01 +08:00
|
|
|
{
|
2020-08-27 11:35:55 +08:00
|
|
|
auto fd = (FileStream*)stream->descriptor.pointer;
|
2021-08-28 20:22:01 +08:00
|
|
|
if (!fd)
|
|
|
|
return 1;
|
|
|
|
if (!size && offset >= stream->size)
|
|
|
|
return 1;
|
2020-08-27 11:35:55 +08:00
|
|
|
|
|
|
|
if (stream->pos != offset)
|
|
|
|
fd->seek(offset, SEEK_SET);
|
|
|
|
|
|
|
|
if (buf)
|
|
|
|
return fd->read(buf, size);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-08-28 20:22:01 +08:00
|
|
|
static void ft_stream_close_callback(FT_Stream stream)
|
|
|
|
{
|
2021-04-24 07:22:27 +08:00
|
|
|
const auto* fd = (FileStream*)stream->descriptor.pointer;
|
|
|
|
delete fd;
|
2021-08-28 20:22:01 +08:00
|
|
|
stream->size = 0;
|
2020-08-27 11:35:55 +08:00
|
|
|
stream->descriptor.pointer = nullptr;
|
|
|
|
}
|
|
|
|
|
2021-11-27 11:06:51 +08:00
|
|
|
FontFreeType* FontFreeType::create(std::string_view fontName,
|
2021-08-28 20:22:01 +08:00
|
|
|
float fontSize,
|
|
|
|
GlyphCollection glyphs,
|
2021-11-27 11:06:51 +08:00
|
|
|
std::string_view customGlyphs,
|
2021-08-28 20:22:01 +08:00
|
|
|
bool distanceFieldEnabled /* = false */,
|
|
|
|
float outline /* = 0 */)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-08 00:11:53 +08:00
|
|
|
FontFreeType* tempFont = new FontFreeType(distanceFieldEnabled, outline);
|
2021-08-28 20:22:01 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
tempFont->setGlyphCollection(glyphs, customGlyphs);
|
2021-08-28 20:22:01 +08:00
|
|
|
|
2021-11-17 14:07:47 +08:00
|
|
|
if (tempFont->loadFontFace(fontName, fontSize))
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-11-17 14:07:47 +08:00
|
|
|
tempFont->autorelease();
|
|
|
|
return tempFont;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2021-11-17 14:07:47 +08:00
|
|
|
|
|
|
|
delete tempFont;
|
|
|
|
return nullptr;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool FontFreeType::initFreeType()
|
|
|
|
{
|
2021-11-17 14:07:47 +08:00
|
|
|
if (!_FTInitialized)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
// begin freetype
|
2021-08-28 20:22:01 +08:00
|
|
|
if (FT_Init_FreeType(&_FTlibrary))
|
2019-11-23 20:27:39 +08:00
|
|
|
return false;
|
2021-08-28 20:22:01 +08:00
|
|
|
|
2021-09-20 18:24:11 +08:00
|
|
|
const FT_Int spread = DistanceMapSpread;
|
|
|
|
FT_Property_Set(_FTlibrary, "sdf", "spread", &spread);
|
|
|
|
FT_Property_Set(_FTlibrary, "bsdf", "spread", &spread);
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
_FTInitialized = true;
|
|
|
|
}
|
2021-08-28 20:22:01 +08:00
|
|
|
|
|
|
|
return _FTInitialized;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void FontFreeType::shutdownFreeType()
|
|
|
|
{
|
2021-11-17 14:07:47 +08:00
|
|
|
if (_FTInitialized)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
FT_Done_FreeType(_FTlibrary);
|
|
|
|
s_cacheFontData.clear();
|
|
|
|
_FTInitialized = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
FT_Library FontFreeType::getFTLibrary()
|
|
|
|
{
|
|
|
|
initFreeType();
|
|
|
|
return _FTlibrary;
|
|
|
|
}
|
|
|
|
|
2021-08-28 18:24:17 +08:00
|
|
|
// clang-format off
|
2019-11-23 20:27:39 +08:00
|
|
|
FontFreeType::FontFreeType(bool distanceFieldEnabled /* = false */, float outline /* = 0 */)
|
2021-11-11 15:56:07 +08:00
|
|
|
: _fontFace(nullptr)
|
2019-11-23 20:27:39 +08:00
|
|
|
, _stroker(nullptr)
|
|
|
|
, _encoding(FT_ENCODING_UNICODE)
|
|
|
|
, _distanceFieldEnabled(distanceFieldEnabled)
|
|
|
|
, _outlineSize(0.0f)
|
2021-08-28 18:24:17 +08:00
|
|
|
, _ascender(0)
|
|
|
|
, _descender(0)
|
2019-11-23 20:27:39 +08:00
|
|
|
, _lineHeight(0)
|
|
|
|
, _usedGlyphs(GlyphCollection::ASCII)
|
|
|
|
{
|
|
|
|
if (outline > 0.0f)
|
|
|
|
{
|
|
|
|
_outlineSize = outline * CC_CONTENT_SCALE_FACTOR();
|
|
|
|
FT_Stroker_New(FontFreeType::getFTLibrary(), &_stroker);
|
|
|
|
FT_Stroker_Set(_stroker,
|
|
|
|
(int)(_outlineSize * 64),
|
|
|
|
FT_STROKER_LINECAP_ROUND,
|
|
|
|
FT_STROKER_LINEJOIN_ROUND,
|
|
|
|
0);
|
|
|
|
}
|
|
|
|
}
|
2021-08-28 18:24:17 +08:00
|
|
|
// clang-format on
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-11-27 11:06:51 +08:00
|
|
|
bool FontFreeType::loadFontFace(std::string_view fontName, float fontSize)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
FT_Face face;
|
|
|
|
// save font name locally
|
|
|
|
_fontName = fontName;
|
2021-11-16 18:41:18 +08:00
|
|
|
_fontSize = fontSize;
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-09-20 18:24:11 +08:00
|
|
|
if (_streamParsingEnabled)
|
2021-08-28 20:22:01 +08:00
|
|
|
{
|
2021-11-27 11:06:51 +08:00
|
|
|
auto fullPath = FileUtils::getInstance()->fullPathForFilename(_fontName);
|
2021-08-28 20:22:01 +08:00
|
|
|
if (fullPath.empty())
|
|
|
|
return false;
|
2020-08-27 11:35:55 +08:00
|
|
|
|
2021-11-16 18:41:18 +08:00
|
|
|
auto fs = FileUtils::getInstance()->openFileStream(fullPath, FileStream::Mode::READ);
|
2021-04-24 07:22:27 +08:00
|
|
|
if (!fs)
|
2021-04-22 19:49:43 +08:00
|
|
|
{
|
2020-08-27 11:35:55 +08:00
|
|
|
return false;
|
2021-04-22 19:49:43 +08:00
|
|
|
}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2020-08-27 11:35:55 +08:00
|
|
|
std::unique_ptr<FT_StreamRec> fts(new FT_StreamRec());
|
2021-08-28 20:22:01 +08:00
|
|
|
fts->read = ft_stream_read_callback;
|
|
|
|
fts->close = ft_stream_close_callback;
|
2021-11-11 16:15:23 +08:00
|
|
|
fts->size = static_cast<unsigned long>(fs->size());
|
2021-11-16 18:41:18 +08:00
|
|
|
fts->descriptor.pointer = fs.release(); // transfer ownership to FT_Open_Face
|
2020-08-27 11:35:55 +08:00
|
|
|
|
2021-08-28 20:22:01 +08:00
|
|
|
FT_Open_Args args = {0};
|
|
|
|
args.flags = FT_OPEN_STREAM;
|
|
|
|
args.stream = fts.get();
|
2020-08-27 11:35:55 +08:00
|
|
|
|
|
|
|
if (FT_Open_Face(getFTLibrary(), &args, 0, &face))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
_fontStream = std::move(fts);
|
|
|
|
}
|
2021-09-20 18:24:11 +08:00
|
|
|
else
|
|
|
|
{
|
2021-11-11 16:15:23 +08:00
|
|
|
DataRef* sharableData;
|
2021-11-27 11:06:51 +08:00
|
|
|
auto it = s_cacheFontData.find(_fontName);
|
2021-09-20 18:24:11 +08:00
|
|
|
if (it != s_cacheFontData.end())
|
|
|
|
{
|
2021-11-11 16:15:23 +08:00
|
|
|
sharableData = &it->second;
|
2021-09-20 18:24:11 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-11-27 11:06:51 +08:00
|
|
|
sharableData = &s_cacheFontData[_fontName];
|
|
|
|
sharableData->data = FileUtils::getInstance()->getDataFromFile(_fontName);
|
2021-09-20 18:24:11 +08:00
|
|
|
}
|
|
|
|
|
2021-11-11 16:15:23 +08:00
|
|
|
++sharableData->referenceCount;
|
|
|
|
auto& data = sharableData->data;
|
|
|
|
if (data.isNull() || FT_New_Memory_Face(getFTLibrary(), data.getBytes(),
|
2021-11-11 17:20:21 +08:00
|
|
|
static_cast<FT_Long>(data.getSize()), 0, &face))
|
2021-09-20 18:24:11 +08:00
|
|
|
return false;
|
|
|
|
}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
if (FT_Select_Charmap(face, FT_ENCODING_UNICODE))
|
|
|
|
{
|
|
|
|
int foundIndex = -1;
|
|
|
|
for (int charmapIndex = 0; charmapIndex < face->num_charmaps; charmapIndex++)
|
|
|
|
{
|
|
|
|
if (face->charmaps[charmapIndex]->encoding != FT_ENCODING_NONE)
|
|
|
|
{
|
|
|
|
foundIndex = charmapIndex;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (foundIndex == -1)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
_encoding = face->charmaps[foundIndex]->encoding;
|
|
|
|
if (FT_Select_Charmap(face, _encoding))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// set the requested font size
|
2021-08-28 20:22:01 +08:00
|
|
|
int dpi = 72;
|
2019-11-23 20:27:39 +08:00
|
|
|
int fontSizePoints = (int)(64.f * fontSize * CC_CONTENT_SCALE_FACTOR());
|
|
|
|
if (FT_Set_Char_Size(face, fontSizePoints, fontSizePoints, dpi, dpi))
|
|
|
|
return false;
|
2021-08-28 20:22:01 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
// store the face globally
|
2021-11-11 15:56:07 +08:00
|
|
|
_fontFace = face;
|
2021-08-28 18:24:17 +08:00
|
|
|
|
|
|
|
// Notes:
|
2021-08-28 20:22:01 +08:00
|
|
|
// a. Since freetype 2.8.1 the TT matrics isn't sync to size_matrics, see the function 'tt_size_request' in
|
|
|
|
// truetype/ttdriver.c b. The TT spec always asks for ROUND, not FLOOR or CEIL, see also the function
|
|
|
|
// 'tt_size_reset' in truetype/ttobjs.c
|
2021-08-28 18:24:17 +08:00
|
|
|
// ** Please see description of FT_Size_Metrics_ in freetype.h about this solution
|
2021-11-11 15:56:07 +08:00
|
|
|
auto& size_metrics = _fontFace->size->metrics;
|
2021-08-28 18:24:17 +08:00
|
|
|
if (_doNativeBytecodeHinting && !strcmp(FT_Get_Font_Format(face), "TrueType"))
|
|
|
|
{
|
|
|
|
_ascender = FT_PIX_ROUND(FT_MulFix(face->ascender, size_metrics.y_scale));
|
|
|
|
_descender = FT_PIX_ROUND(FT_MulFix(face->descender, size_metrics.y_scale));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_ascender = size_metrics.ascender;
|
|
|
|
_descender = size_metrics.descender;
|
|
|
|
}
|
|
|
|
|
2021-08-28 20:22:01 +08:00
|
|
|
_lineHeight = (_ascender - _descender) >> 6;
|
2021-06-17 21:56:38 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
// done and good
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
FontFreeType::~FontFreeType()
|
|
|
|
{
|
|
|
|
if (_FTInitialized)
|
|
|
|
{
|
|
|
|
if (_stroker)
|
|
|
|
{
|
|
|
|
FT_Stroker_Done(_stroker);
|
|
|
|
}
|
2021-11-11 15:56:07 +08:00
|
|
|
if (_fontFace)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-11-11 15:56:07 +08:00
|
|
|
FT_Done_Face(_fontFace);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
auto iter = s_cacheFontData.find(_fontName);
|
|
|
|
if (iter != s_cacheFontData.end())
|
|
|
|
{
|
|
|
|
iter->second.referenceCount -= 1;
|
|
|
|
if (iter->second.referenceCount == 0)
|
|
|
|
{
|
|
|
|
s_cacheFontData.erase(iter);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-11 17:20:21 +08:00
|
|
|
FontAtlas* FontFreeType::newFontAtlas()
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-08 00:11:53 +08:00
|
|
|
auto fontAtlas = new FontAtlas(this);
|
|
|
|
if (_usedGlyphs != GlyphCollection::DYNAMIC)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-11-11 17:20:21 +08:00
|
|
|
std::u32string utf32;
|
|
|
|
if (StringUtils::UTF8ToUTF32(getGlyphCollection(), utf32))
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-11-11 17:20:21 +08:00
|
|
|
fontAtlas->prepareLetterDefinitions(utf32);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
}
|
2021-11-11 17:20:21 +08:00
|
|
|
return fontAtlas;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2021-08-28 20:22:01 +08:00
|
|
|
int* FontFreeType::getHorizontalKerningForTextUTF32(const std::u32string& text, int& outNumLetters) const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-11-11 15:56:07 +08:00
|
|
|
if (!_fontFace)
|
2019-11-23 20:27:39 +08:00
|
|
|
return nullptr;
|
2021-08-28 20:22:01 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
outNumLetters = static_cast<int>(text.length());
|
|
|
|
|
|
|
|
if (!outNumLetters)
|
|
|
|
return nullptr;
|
2021-08-28 20:22:01 +08:00
|
|
|
|
2021-12-08 00:11:53 +08:00
|
|
|
int* sizes = new int[outNumLetters];
|
2021-08-28 20:22:01 +08:00
|
|
|
memset(sizes, 0, outNumLetters * sizeof(int));
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-11-11 15:56:07 +08:00
|
|
|
bool hasKerning = FT_HAS_KERNING(_fontFace) != 0;
|
2019-11-23 20:27:39 +08:00
|
|
|
if (hasKerning)
|
|
|
|
{
|
|
|
|
for (int c = 1; c < outNumLetters; ++c)
|
|
|
|
{
|
2021-08-28 20:22:01 +08:00
|
|
|
sizes[c] = getHorizontalKerningForChars(text[c - 1], text[c]);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
}
|
2021-08-28 20:22:01 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
return sizes;
|
|
|
|
}
|
|
|
|
|
2021-08-28 20:22:01 +08:00
|
|
|
int FontFreeType::getHorizontalKerningForChars(uint64_t firstChar, uint64_t secondChar) const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
// get the ID to the char we need
|
2021-11-11 15:56:07 +08:00
|
|
|
int glyphIndex1 = FT_Get_Char_Index(_fontFace, static_cast<FT_ULong>(firstChar));
|
2021-08-28 20:22:01 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
if (!glyphIndex1)
|
|
|
|
return 0;
|
2021-08-28 20:22:01 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
// get the ID to the char we need
|
2021-11-11 15:56:07 +08:00
|
|
|
int glyphIndex2 = FT_Get_Char_Index(_fontFace, static_cast<FT_ULong>(secondChar));
|
2021-08-28 20:22:01 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
if (!glyphIndex2)
|
|
|
|
return 0;
|
2021-08-28 20:22:01 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
FT_Vector kerning;
|
2021-08-28 20:22:01 +08:00
|
|
|
|
2021-11-11 15:56:07 +08:00
|
|
|
if (FT_Get_Kerning(_fontFace, glyphIndex1, glyphIndex2, FT_KERNING_DEFAULT, &kerning))
|
2019-11-23 20:27:39 +08:00
|
|
|
return 0;
|
2021-08-28 20:22:01 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
return (static_cast<int>(kerning.x >> 6));
|
|
|
|
}
|
|
|
|
|
|
|
|
int FontFreeType::getFontAscender() const
|
|
|
|
{
|
2021-08-28 18:24:17 +08:00
|
|
|
return _ascender >> 6;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const char* FontFreeType::getFontFamily() const
|
|
|
|
{
|
2021-11-11 15:56:07 +08:00
|
|
|
if (!_fontFace)
|
2019-11-23 20:27:39 +08:00
|
|
|
return nullptr;
|
|
|
|
|
2021-11-11 15:56:07 +08:00
|
|
|
return _fontFace->family_name;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2021-11-11 16:15:23 +08:00
|
|
|
unsigned char* FontFreeType::getGlyphBitmap(uint32_t theChar,
|
2021-09-05 20:45:32 +08:00
|
|
|
int32_t& outWidth,
|
|
|
|
int32_t& outHeight,
|
2021-08-28 20:22:01 +08:00
|
|
|
Rect& outRect,
|
|
|
|
int& xAdvance)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
unsigned char* ret = nullptr;
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
2021-11-11 15:56:07 +08:00
|
|
|
if (_fontFace == nullptr)
|
2019-11-23 20:27:39 +08:00
|
|
|
break;
|
|
|
|
|
2021-11-11 15:56:07 +08:00
|
|
|
// @remark: glyphIndex=0 means charactor is mssing on current font face
|
|
|
|
auto glyphIndex = FT_Get_Char_Index(_fontFace, static_cast<FT_ULong>(theChar));
|
2021-09-20 18:24:11 +08:00
|
|
|
#if defined(COCOS2D_DEBUG) && COCOS2D_DEBUG > 0
|
2021-11-11 15:56:07 +08:00
|
|
|
if (glyphIndex == 0)
|
2021-08-28 20:22:01 +08:00
|
|
|
{
|
2021-11-27 11:06:51 +08:00
|
|
|
char32_t ntcs[2] = {theChar, (char32_t)0};
|
|
|
|
std::u32string_view charUTF32(ntcs, 1);
|
2020-08-12 14:24:06 +08:00
|
|
|
std::string charUTF8;
|
|
|
|
cocos2d::StringUtils::UTF32ToUTF8(charUTF32, charUTF8);
|
|
|
|
|
2021-08-28 20:22:01 +08:00
|
|
|
if (charUTF8 == "\n")
|
|
|
|
charUTF8 = "\\n";
|
2021-11-11 15:56:07 +08:00
|
|
|
cocos2d::log("The font face: %s doesn't contains char: <%s>", _fontFace->charmap->face->family_name,
|
2021-08-28 20:22:01 +08:00
|
|
|
charUTF8.c_str());
|
2021-11-16 18:41:18 +08:00
|
|
|
return nullptr;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2020-08-12 14:24:06 +08:00
|
|
|
#endif
|
2021-11-16 18:41:18 +08:00
|
|
|
if (FT_Load_Glyph(_fontFace, glyphIndex, FT_LOAD_RENDER | FT_LOAD_NO_AUTOHINT))
|
|
|
|
break;
|
2021-11-11 16:24:18 +08:00
|
|
|
if (_distanceFieldEnabled && _fontFace->glyph->bitmap.buffer)
|
|
|
|
{
|
2021-09-20 18:24:11 +08:00
|
|
|
// Require freetype version > 2.11.0, because freetype 2.11.0 sdf has memory access bug, see: https://gitlab.freedesktop.org/freetype/freetype/-/issues/1077
|
2021-11-11 15:56:07 +08:00
|
|
|
FT_Render_Glyph(_fontFace->glyph, FT_Render_Mode::FT_RENDER_MODE_SDF);
|
2021-09-20 18:24:11 +08:00
|
|
|
}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-11-11 15:56:07 +08:00
|
|
|
auto& metrics = _fontFace->glyph->metrics;
|
2021-08-28 20:22:01 +08:00
|
|
|
outRect.origin.x = static_cast<float>(metrics.horiBearingX >> 6);
|
|
|
|
outRect.origin.y = static_cast<float>(-(metrics.horiBearingY >> 6));
|
|
|
|
outRect.size.width = static_cast<float>((metrics.width >> 6));
|
2019-11-23 20:27:39 +08:00
|
|
|
outRect.size.height = static_cast<float>((metrics.height >> 6));
|
|
|
|
|
2021-11-11 15:56:07 +08:00
|
|
|
xAdvance = (static_cast<int>(_fontFace->glyph->metrics.horiAdvance >> 6));
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-11-11 15:56:07 +08:00
|
|
|
outWidth = _fontFace->glyph->bitmap.width;
|
|
|
|
outHeight = _fontFace->glyph->bitmap.rows;
|
|
|
|
ret = _fontFace->glyph->bitmap.buffer;
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
if (_outlineSize > 0 && outWidth > 0 && outHeight > 0)
|
|
|
|
{
|
2021-12-08 00:11:53 +08:00
|
|
|
auto copyBitmap = new unsigned char[outWidth * outHeight];
|
2021-08-28 20:22:01 +08:00
|
|
|
memcpy(copyBitmap, ret, outWidth * outHeight * sizeof(unsigned char));
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
FT_BBox bbox;
|
2021-11-11 15:56:07 +08:00
|
|
|
auto outlineBitmap = getGlyphBitmapWithOutline(glyphIndex, bbox);
|
2021-08-28 20:22:01 +08:00
|
|
|
if (outlineBitmap == nullptr)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
ret = nullptr;
|
2021-08-28 20:22:01 +08:00
|
|
|
delete[] copyBitmap;
|
2019-11-23 20:27:39 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
int glyphMinX = (int)outRect.origin.x;
|
|
|
|
int glyphMaxX = (int)(outRect.origin.x + outWidth);
|
|
|
|
int glyphMinY = (int)(-outHeight - outRect.origin.y);
|
|
|
|
int glyphMaxY = (int)-outRect.origin.y;
|
|
|
|
|
2021-08-28 20:22:01 +08:00
|
|
|
auto outlineMinX = bbox.xMin >> 6;
|
|
|
|
auto outlineMaxX = bbox.xMax >> 6;
|
|
|
|
auto outlineMinY = bbox.yMin >> 6;
|
|
|
|
auto outlineMaxY = bbox.yMax >> 6;
|
|
|
|
auto outlineWidth = outlineMaxX - outlineMinX;
|
2019-11-23 20:27:39 +08:00
|
|
|
auto outlineHeight = outlineMaxY - outlineMinY;
|
|
|
|
|
|
|
|
auto blendImageMinX = MIN(outlineMinX, glyphMinX);
|
|
|
|
auto blendImageMaxY = MAX(outlineMaxY, glyphMaxY);
|
2021-08-28 20:22:01 +08:00
|
|
|
auto blendWidth = MAX(outlineMaxX, glyphMaxX) - blendImageMinX;
|
|
|
|
auto blendHeight = blendImageMaxY - MIN(outlineMinY, glyphMinY);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
outRect.origin.x = (float)blendImageMinX;
|
|
|
|
outRect.origin.y = -blendImageMaxY + _outlineSize;
|
|
|
|
|
2021-08-28 20:22:01 +08:00
|
|
|
unsigned char* blendImage = nullptr;
|
2019-11-23 20:27:39 +08:00
|
|
|
if (blendWidth > 0 && blendHeight > 0)
|
|
|
|
{
|
|
|
|
FT_Pos index, index2;
|
|
|
|
auto imageSize = blendWidth * blendHeight * 2;
|
2021-12-08 00:11:53 +08:00
|
|
|
blendImage = new unsigned char[imageSize];
|
2019-11-23 20:27:39 +08:00
|
|
|
memset(blendImage, 0, imageSize);
|
|
|
|
|
|
|
|
auto px = outlineMinX - blendImageMinX;
|
|
|
|
auto py = blendImageMaxY - outlineMaxY;
|
|
|
|
for (int x = 0; x < outlineWidth; ++x)
|
|
|
|
{
|
|
|
|
for (int y = 0; y < outlineHeight; ++y)
|
|
|
|
{
|
2021-08-28 20:22:01 +08:00
|
|
|
index = px + x + ((py + y) * blendWidth);
|
|
|
|
index2 = x + (y * outlineWidth);
|
2019-11-23 20:27:39 +08:00
|
|
|
blendImage[2 * index] = outlineBitmap[index2];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
px = glyphMinX - blendImageMinX;
|
|
|
|
py = blendImageMaxY - glyphMaxY;
|
|
|
|
for (int x = 0; x < outWidth; ++x)
|
|
|
|
{
|
|
|
|
for (int y = 0; y < outHeight; ++y)
|
|
|
|
{
|
2021-08-28 20:22:01 +08:00
|
|
|
index = px + x + ((y + py) * blendWidth);
|
|
|
|
index2 = x + (y * outWidth);
|
2019-11-23 20:27:39 +08:00
|
|
|
blendImage[2 * index + 1] = copyBitmap[index2];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
outRect.size.width = (float)blendWidth;
|
|
|
|
outRect.size.height = (float)blendHeight;
|
2021-08-28 20:22:01 +08:00
|
|
|
outWidth = blendWidth;
|
|
|
|
outHeight = blendHeight;
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-08-28 20:22:01 +08:00
|
|
|
delete[] outlineBitmap;
|
|
|
|
delete[] copyBitmap;
|
2019-11-23 20:27:39 +08:00
|
|
|
ret = blendImage;
|
|
|
|
}
|
|
|
|
|
2021-11-11 17:30:47 +08:00
|
|
|
return ret;
|
2019-11-23 20:27:39 +08:00
|
|
|
} while (0);
|
|
|
|
|
2021-11-11 17:30:47 +08:00
|
|
|
outRect.size.width = 0;
|
|
|
|
outRect.size.height = 0;
|
|
|
|
xAdvance = 0;
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-11-11 17:30:47 +08:00
|
|
|
return nullptr;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2021-11-11 15:56:07 +08:00
|
|
|
unsigned char* FontFreeType::getGlyphBitmapWithOutline(unsigned int glyphIndex, FT_BBox& bbox)
|
2021-08-28 20:22:01 +08:00
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
unsigned char* ret = nullptr;
|
2021-11-11 15:56:07 +08:00
|
|
|
if (FT_Load_Glyph(_fontFace, glyphIndex, FT_LOAD_NO_BITMAP) == 0)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-11-11 15:56:07 +08:00
|
|
|
if (_fontFace->glyph->format == FT_GLYPH_FORMAT_OUTLINE)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
FT_Glyph glyph;
|
2021-11-11 15:56:07 +08:00
|
|
|
if (FT_Get_Glyph(_fontFace->glyph, &glyph) == 0)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
FT_Glyph_StrokeBorder(&glyph, _stroker, 0, 1);
|
|
|
|
if (glyph->format == FT_GLYPH_FORMAT_OUTLINE)
|
|
|
|
{
|
2021-08-28 20:22:01 +08:00
|
|
|
FT_Outline* outline = &reinterpret_cast<FT_OutlineGlyph>(glyph)->outline;
|
|
|
|
FT_Glyph_Get_CBox(glyph, FT_GLYPH_BBOX_GRIDFIT, &bbox);
|
2021-09-05 20:45:32 +08:00
|
|
|
int32_t width = (bbox.xMax - bbox.xMin) >> 6;
|
|
|
|
int32_t rows = (bbox.yMax - bbox.yMin) >> 6;
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
FT_Bitmap bmp;
|
2021-12-08 00:11:53 +08:00
|
|
|
bmp.buffer = new unsigned char[width * rows];
|
2019-11-23 20:27:39 +08:00
|
|
|
memset(bmp.buffer, 0, width * rows);
|
2021-08-28 20:22:01 +08:00
|
|
|
bmp.width = (int)width;
|
|
|
|
bmp.rows = (int)rows;
|
|
|
|
bmp.pitch = (int)width;
|
2019-11-23 20:27:39 +08:00
|
|
|
bmp.pixel_mode = FT_PIXEL_MODE_GRAY;
|
2021-08-28 20:22:01 +08:00
|
|
|
bmp.num_grays = 256;
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
FT_Raster_Params params;
|
2021-08-28 20:22:01 +08:00
|
|
|
memset(¶ms, 0, sizeof(params));
|
2019-11-23 20:27:39 +08:00
|
|
|
params.source = outline;
|
|
|
|
params.target = &bmp;
|
2021-08-28 20:22:01 +08:00
|
|
|
params.flags = FT_RASTER_FLAG_AA;
|
|
|
|
FT_Outline_Translate(outline, -bbox.xMin, -bbox.yMin);
|
2019-11-23 20:27:39 +08:00
|
|
|
FT_Outline_Render(_FTlibrary, outline, ¶ms);
|
|
|
|
|
|
|
|
ret = bmp.buffer;
|
|
|
|
}
|
|
|
|
FT_Done_Glyph(glyph);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2021-08-28 20:22:01 +08:00
|
|
|
void FontFreeType::renderCharAt(unsigned char* dest,
|
|
|
|
int posX,
|
|
|
|
int posY,
|
|
|
|
unsigned char* bitmap,
|
2021-09-05 20:45:32 +08:00
|
|
|
int32_t bitmapWidth,
|
|
|
|
int32_t bitmapHeight)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
int iX = posX;
|
|
|
|
int iY = posY;
|
|
|
|
|
2021-09-20 18:24:11 +08:00
|
|
|
if (_outlineSize > 0)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
unsigned char tempChar;
|
2021-09-05 20:45:32 +08:00
|
|
|
for (int32_t y = 0; y < bitmapHeight; ++y)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-09-05 20:45:32 +08:00
|
|
|
int32_t bitmap_y = y * bitmapWidth;
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
for (int x = 0; x < bitmapWidth; ++x)
|
|
|
|
{
|
2021-08-28 20:22:01 +08:00
|
|
|
tempChar = bitmap[(bitmap_y + x) * 2];
|
|
|
|
dest[(iX + (iY * FontAtlas::CacheTextureWidth)) * 2] = tempChar;
|
|
|
|
tempChar = bitmap[(bitmap_y + x) * 2 + 1];
|
|
|
|
dest[(iX + (iY * FontAtlas::CacheTextureWidth)) * 2 + 1] = tempChar;
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
iX += 1;
|
|
|
|
}
|
|
|
|
|
2021-08-28 20:22:01 +08:00
|
|
|
iX = posX;
|
2019-11-23 20:27:39 +08:00
|
|
|
iY += 1;
|
|
|
|
}
|
2021-08-28 20:22:01 +08:00
|
|
|
delete[] bitmap;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-09-05 20:45:32 +08:00
|
|
|
for (int32_t y = 0; y < bitmapHeight; ++y)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-09-05 20:45:32 +08:00
|
|
|
int32_t bitmap_y = y * bitmapWidth;
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
for (int x = 0; x < bitmapWidth; ++x)
|
|
|
|
{
|
|
|
|
unsigned char cTemp = bitmap[bitmap_y + x];
|
|
|
|
|
|
|
|
// the final pixel
|
2021-08-28 20:22:01 +08:00
|
|
|
dest[(iX + (iY * FontAtlas::CacheTextureWidth))] = cTemp;
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
iX += 1;
|
|
|
|
}
|
|
|
|
|
2021-08-28 20:22:01 +08:00
|
|
|
iX = posX;
|
2019-11-23 20:27:39 +08:00
|
|
|
iY += 1;
|
|
|
|
}
|
2021-08-28 20:22:01 +08:00
|
|
|
}
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2021-11-27 11:06:51 +08:00
|
|
|
void FontFreeType::setGlyphCollection(GlyphCollection glyphs, std::string_view customGlyphs)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
_usedGlyphs = glyphs;
|
|
|
|
if (glyphs == GlyphCollection::CUSTOM)
|
|
|
|
{
|
2021-11-27 11:06:51 +08:00
|
|
|
_customGlyphs = customGlyphs;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-27 11:06:51 +08:00
|
|
|
std::string_view FontFreeType::getGlyphCollection() const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-11-27 11:06:51 +08:00
|
|
|
std::string_view glyphCollection;
|
2019-11-23 20:27:39 +08:00
|
|
|
switch (_usedGlyphs)
|
|
|
|
{
|
2021-08-28 20:22:01 +08:00
|
|
|
case cocos2d::GlyphCollection::DYNAMIC:
|
|
|
|
break;
|
|
|
|
case cocos2d::GlyphCollection::NEHE:
|
|
|
|
glyphCollection = _glyphNEHE;
|
|
|
|
break;
|
|
|
|
case cocos2d::GlyphCollection::ASCII:
|
|
|
|
glyphCollection = _glyphASCII;
|
|
|
|
break;
|
|
|
|
case cocos2d::GlyphCollection::CUSTOM:
|
2021-11-27 11:06:51 +08:00
|
|
|
glyphCollection = _customGlyphs;
|
2021-08-28 20:22:01 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return glyphCollection;
|
|
|
|
}
|
|
|
|
|
2021-08-28 20:22:01 +08:00
|
|
|
void FontFreeType::releaseFont(const std::string& fontName)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
auto item = s_cacheFontData.begin();
|
|
|
|
while (s_cacheFontData.end() != item)
|
|
|
|
{
|
|
|
|
if (item->first.find(fontName) != std::string::npos)
|
|
|
|
item = s_cacheFontData.erase(item);
|
|
|
|
else
|
|
|
|
item++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_CC_END
|