Fix comment typo and do clang-format [skip ci]

This commit is contained in:
halx99 2021-08-28 20:22:01 +08:00
parent 0e2ade1350
commit beb593a22a
3 changed files with 211 additions and 176 deletions

View File

@ -13,6 +13,8 @@ IndentWidth: 4
# Keep lines under 120 columns long.
ColumnLimit: 120
SortIncludes: false
# Always break before braces
BreakBeforeBraces: Custom
BraceWrapping:

View File

@ -40,15 +40,18 @@ THE SOFTWARE.
NS_CC_BEGIN
FT_Library FontFreeType::_FTlibrary;
bool FontFreeType::_FTInitialized = false;
bool FontFreeType::_streamParsingEnabled = false;
bool FontFreeType::_doNativeBytecodeHinting = true;
const int FontFreeType::DistanceMapSpread = 3;
const char* FontFreeType::_glyphASCII = "\"!#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~¡¢£¤¥¦§¨©ª«¬­®¯°±²³´µ¶·¸¹º»¼½¾¿ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖ×ØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö÷øùúûüýþ ";
const char* FontFreeType::_glyphNEHE = "!\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~ ";
const char* FontFreeType::_glyphASCII =
"\"!#$%&'()*+,-./"
"0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~"
"¡¢£¤¥¦§¨©ª«¬­®¯°±²³´µ¶·¸¹º»¼½¾¿ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖ×ØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö÷øùúûüýþ ";
const char* FontFreeType::_glyphNEHE =
"!\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~ ";
typedef struct _DataRef
{
@ -59,10 +62,16 @@ typedef struct _DataRef
static std::unordered_map<std::string, DataRef> s_cacheFontData;
// ------ freetype2 stream parsing support ---
static unsigned long ft_stream_read_callback(FT_Stream stream, unsigned long offset, unsigned char* buf, unsigned long size) {
static unsigned long ft_stream_read_callback(FT_Stream stream,
unsigned long offset,
unsigned char* buf,
unsigned long size)
{
auto fd = (FileStream*)stream->descriptor.pointer;
if (!fd) return 1;
if (!size && offset >= stream->size) return 1;
if (!fd)
return 1;
if (!size && offset >= stream->size)
return 1;
if (stream->pos != offset)
fd->seek(offset, SEEK_SET);
@ -73,14 +82,20 @@ static unsigned long ft_stream_read_callback(FT_Stream stream, unsigned long off
return 0;
}
static void ft_stream_close_callback(FT_Stream stream) {
static void ft_stream_close_callback(FT_Stream stream)
{
const auto* fd = (FileStream*)stream->descriptor.pointer;
delete fd;
stream->size = 0;
stream->descriptor.pointer = nullptr;
}
FontFreeType * FontFreeType::create(const std::string &fontName, float fontSize, GlyphCollection glyphs, const char *customGlyphs,bool distanceFieldEnabled /* = false */,float outline /* = 0 */)
FontFreeType* FontFreeType::create(const std::string& fontName,
float fontSize,
GlyphCollection glyphs,
const char* customGlyphs,
bool distanceFieldEnabled /* = false */,
float outline /* = 0 */)
{
FontFreeType* tempFont = new (std::nothrow) FontFreeType(distanceFieldEnabled, outline);
@ -160,7 +175,8 @@ bool FontFreeType::createFontObject(const std::string &fontName, float fontSize)
// save font name locally
_fontName = fontName;
if (!_streamParsingEnabled) {
if (!_streamParsingEnabled)
{
auto it = s_cacheFontData.find(fontName);
if (it != s_cacheFontData.end())
{
@ -177,12 +193,15 @@ bool FontFreeType::createFontObject(const std::string &fontName, float fontSize)
}
}
if (FT_New_Memory_Face(getFTLibrary(), s_cacheFontData[fontName].data.getBytes(), s_cacheFontData[fontName].data.getSize(), 0, &face))
if (FT_New_Memory_Face(getFTLibrary(), s_cacheFontData[fontName].data.getBytes(),
s_cacheFontData[fontName].data.getSize(), 0, &face))
return false;
}
else {
else
{
auto fullPath = FileUtils::getInstance()->fullPathForFilename(fontName);
if (fullPath.empty()) return false;
if (fullPath.empty())
return false;
auto fs = FileUtils::getInstance()->openFileStream(fullPath, FileStream::Mode::READ).release();
if (!fs)
@ -240,9 +259,9 @@ bool FontFreeType::createFontObject(const std::string &fontName, float fontSize)
_fontRef = face;
// Notes:
// a. Since freetype 2.8.1 the tt matrics not 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
// 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
// ** Please see description of FT_Size_Metrics_ in freetype.h about this solution
auto& size_metrics = _fontRef->size->metrics;
if (_doNativeBytecodeHinting && !strcmp(FT_Get_Font_Format(face), "TrueType"))
@ -368,7 +387,11 @@ const char* FontFreeType::getFontFamily() const
return _fontRef->family_name;
}
unsigned char* FontFreeType::getGlyphBitmap(uint64_t theChar, long &outWidth, long &outHeight, Rect &outRect,int &xAdvance)
unsigned char* FontFreeType::getGlyphBitmap(uint64_t theChar,
long& outWidth,
long& outHeight,
Rect& outRect,
int& xAdvance)
{
bool invalidChar = true;
unsigned char* ret = nullptr;
@ -380,19 +403,22 @@ unsigned char* FontFreeType::getGlyphBitmap(uint64_t theChar, long &outWidth, lo
// @remark: glyph_index=0 means
auto glyph_index = FT_Get_Char_Index(_fontRef, static_cast<FT_ULong>(theChar));
if (FT_Load_Glyph(_fontRef, glyph_index, _distanceFieldEnabled ?
(FT_LOAD_RENDER | FT_LOAD_NO_HINTING | FT_LOAD_NO_AUTOHINT) :
(FT_LOAD_RENDER | FT_LOAD_NO_AUTOHINT)))
if (FT_Load_Glyph(_fontRef, glyph_index,
_distanceFieldEnabled ? (FT_LOAD_RENDER | FT_LOAD_NO_HINTING | FT_LOAD_NO_AUTOHINT)
: (FT_LOAD_RENDER | FT_LOAD_NO_AUTOHINT)))
break;
#if defined(_DEBUG)
if (glyph_index == 0) {
if (glyph_index == 0)
{
std::u32string charUTF32(1, theChar);
std::string charUTF8;
cocos2d::StringUtils::UTF32ToUTF8(charUTF32, charUTF8);
if (charUTF8 == "\n") charUTF8 = "\\n";
cocos2d::log("The font face: %s doesn't contains char: <%s>", _fontRef->charmap->face->family_name, charUTF8.c_str());
if (charUTF8 == "\n")
charUTF8 = "\\n";
cocos2d::log("The font face: %s doesn't contains char: <%s>", _fontRef->charmap->face->family_name,
charUTF8.c_str());
}
#endif
@ -597,8 +623,10 @@ unsigned char * makeDistanceMap( unsigned char *img, long width, long height)
{
dist = outside[i] - inside[i];
dist = 128.0 - dist * 16;
if( dist < 0 ) dist = 0;
if( dist > 255 ) dist = 255;
if (dist < 0)
dist = 0;
if (dist > 255)
dist = 255;
out[i] = (unsigned char)dist;
}
/* Dual channel 16-bit output (more complicated, but good precision and range) */
@ -628,7 +656,12 @@ unsigned char * makeDistanceMap( unsigned char *img, long width, long height)
return out;
}
void FontFreeType::renderCharAt(unsigned char *dest,int posX, int posY, unsigned char* bitmap,long bitmapWidth,long bitmapHeight)
void FontFreeType::renderCharAt(unsigned char* dest,
int posX,
int posY,
unsigned char* bitmap,
long bitmapWidth,
long bitmapHeight)
{
int iX = posX;
int iY = posY;

View File

@ -71,7 +71,7 @@ public:
* dimensions. The application has to cater for this, especially if it
* wants to rely on a TTF's vertical data (for example, to properly align
* box characters vertically).
* - since freetype-2.8.1 TureType acender,decender not sync to size_matrics
* - Since freetype-2.8.1 TureType matrics isn't sync to size_matrics
* - By default it's enabled for compatible with cocos2d-x-4.0 or older with freetype-2.5.5
* - Please see freetype.h
* */