mirror of https://github.com/axmolengine/axmol.git
Fix crash problem when load a non-truetype .ttf file.
This commit is contained in:
parent
c0f7b7ba12
commit
56067f5acc
|
@ -2,8 +2,9 @@
|
||||||
Copyright (c) 2013 Zynga Inc.
|
Copyright (c) 2013 Zynga Inc.
|
||||||
Copyright (c) 2013-2016 Chukong Technologies Inc.
|
Copyright (c) 2013-2016 Chukong Technologies Inc.
|
||||||
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
||||||
|
Copyright (c) 2021 Bytedance Inc.
|
||||||
|
|
||||||
http://www.cocos2d-x.org
|
https://adxe.org
|
||||||
|
|
||||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
of this software and associated documentation files (the "Software"), to deal
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
@ -26,7 +27,7 @@ THE SOFTWARE.
|
||||||
|
|
||||||
#include "2d/CCFontFreeType.h"
|
#include "2d/CCFontFreeType.h"
|
||||||
|
|
||||||
#include <freetype/src/truetype/ttobjs.h>
|
#include <freetype/ftfntfmt.h>
|
||||||
|
|
||||||
#include FT_BBOX_H
|
#include FT_BBOX_H
|
||||||
#include "edtaa3func.h"
|
#include "edtaa3func.h"
|
||||||
|
@ -43,6 +44,7 @@ NS_CC_BEGIN
|
||||||
FT_Library FontFreeType::_FTlibrary;
|
FT_Library FontFreeType::_FTlibrary;
|
||||||
bool FontFreeType::_FTInitialized = false;
|
bool FontFreeType::_FTInitialized = false;
|
||||||
bool FontFreeType::_streamParsingEnabled = false;
|
bool FontFreeType::_streamParsingEnabled = false;
|
||||||
|
bool FontFreeType::_doNativeBytecodeHinting = true;
|
||||||
const int FontFreeType::DistanceMapSpread = 3;
|
const int FontFreeType::DistanceMapSpread = 3;
|
||||||
|
|
||||||
const char* FontFreeType::_glyphASCII = "\"!#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~¡¢£¤¥¦§¨©ª«¬®¯°±²³´µ¶·¸¹º»¼½¾¿ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖ×ØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö÷øùúûüýþ ";
|
const char* FontFreeType::_glyphASCII = "\"!#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~¡¢£¤¥¦§¨©ª«¬®¯°±²³´µ¶·¸¹º»¼½¾¿ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖ×ØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö÷øùúûüýþ ";
|
||||||
|
@ -126,12 +128,15 @@ FT_Library FontFreeType::getFTLibrary()
|
||||||
return _FTlibrary;
|
return _FTlibrary;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// clang-format off
|
||||||
FontFreeType::FontFreeType(bool distanceFieldEnabled /* = false */, float outline /* = 0 */)
|
FontFreeType::FontFreeType(bool distanceFieldEnabled /* = false */, float outline /* = 0 */)
|
||||||
: _fontRef(nullptr)
|
: _fontRef(nullptr)
|
||||||
, _stroker(nullptr)
|
, _stroker(nullptr)
|
||||||
, _encoding(FT_ENCODING_UNICODE)
|
, _encoding(FT_ENCODING_UNICODE)
|
||||||
, _distanceFieldEnabled(distanceFieldEnabled)
|
, _distanceFieldEnabled(distanceFieldEnabled)
|
||||||
, _outlineSize(0.0f)
|
, _outlineSize(0.0f)
|
||||||
|
, _ascender(0)
|
||||||
|
, _descender(0)
|
||||||
, _lineHeight(0)
|
, _lineHeight(0)
|
||||||
, _fontAtlas(nullptr)
|
, _fontAtlas(nullptr)
|
||||||
, _usedGlyphs(GlyphCollection::ASCII)
|
, _usedGlyphs(GlyphCollection::ASCII)
|
||||||
|
@ -147,6 +152,7 @@ FontFreeType::FontFreeType(bool distanceFieldEnabled /* = false */, float outlin
|
||||||
0);
|
0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// clang-format on
|
||||||
|
|
||||||
bool FontFreeType::createFontObject(const std::string &fontName, float fontSize)
|
bool FontFreeType::createFontObject(const std::string &fontName, float fontSize)
|
||||||
{
|
{
|
||||||
|
@ -232,12 +238,25 @@ bool FontFreeType::createFontObject(const std::string &fontName, float fontSize)
|
||||||
|
|
||||||
// store the face globally
|
// store the face globally
|
||||||
_fontRef = face;
|
_fontRef = face;
|
||||||
auto* ttSize = (TT_Size)(_fontRef->size);
|
|
||||||
// Notes:
|
// Notes:
|
||||||
// a. ttSize->metrics->height: (ttSize->metrics->ascender - ttSize->metrics->descender)
|
// 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. ftSize->metrics.height == ttSize->metrics->height
|
// b. The TT spec always asks for ROUND, not FLOOR or CEIL, see also the function 'tt_size_reset' in
|
||||||
// c. the TT spec always asks for ROUND, not FLOOR or CEIL, see also freetype: ttobjs.c
|
// truetype/ttobjs.c
|
||||||
_lineHeight = static_cast<int>((ttSize->metrics->ascender - ttSize->metrics->descender) >> 6);
|
// ** 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"))
|
||||||
|
{
|
||||||
|
_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;
|
||||||
|
}
|
||||||
|
|
||||||
|
_lineHeight = (_ascender - _descender) >> 6;
|
||||||
|
|
||||||
// done and good
|
// done and good
|
||||||
return true;
|
return true;
|
||||||
|
@ -338,7 +357,7 @@ int FontFreeType::getHorizontalKerningForChars(uint64_t firstChar, uint64_t sec
|
||||||
|
|
||||||
int FontFreeType::getFontAscender() const
|
int FontFreeType::getFontAscender() const
|
||||||
{
|
{
|
||||||
return (static_cast<int>(((TT_Size)_fontRef->size)->metrics->ascender >> 6));
|
return _ascender >> 6;
|
||||||
}
|
}
|
||||||
|
|
||||||
const char* FontFreeType::getFontFamily() const
|
const char* FontFreeType::getFontFamily() const
|
||||||
|
|
|
@ -2,8 +2,9 @@
|
||||||
Copyright (c) 2013 Zynga Inc.
|
Copyright (c) 2013 Zynga Inc.
|
||||||
Copyright (c) 2013-2016 Chukong Technologies Inc.
|
Copyright (c) 2013-2016 Chukong Technologies Inc.
|
||||||
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
||||||
|
Copyright (c) 2021 Bytedance Inc.
|
||||||
|
|
||||||
http://www.cocos2d-x.org
|
https://adxe.org
|
||||||
|
|
||||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
of this software and associated documentation files (the "Software"), to deal
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
@ -31,8 +32,8 @@
|
||||||
|
|
||||||
#include "2d/CCFont.h"
|
#include "2d/CCFont.h"
|
||||||
|
|
||||||
#include <string>
|
|
||||||
#include <ft2build.h>
|
#include <ft2build.h>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
#include FT_FREETYPE_H
|
#include FT_FREETYPE_H
|
||||||
#include FT_STROKER_H
|
#include FT_STROKER_H
|
||||||
|
@ -44,8 +45,12 @@ class CC_DLL FontFreeType : public Font
|
||||||
public:
|
public:
|
||||||
static const int DistanceMapSpread;
|
static const int DistanceMapSpread;
|
||||||
|
|
||||||
static FontFreeType* create(const std::string &fontName, float fontSize, GlyphCollection glyphs,
|
static FontFreeType* create(const std::string& fontName,
|
||||||
const char *customGlyphs,bool distanceFieldEnabled = false, float outline = 0);
|
float fontSize,
|
||||||
|
GlyphCollection glyphs,
|
||||||
|
const char* customGlyphs,
|
||||||
|
bool distanceFieldEnabled = false,
|
||||||
|
float outline = 0);
|
||||||
|
|
||||||
static void shutdownFreeType();
|
static void shutdownFreeType();
|
||||||
|
|
||||||
|
@ -58,11 +63,31 @@ public:
|
||||||
static void setStreamParsingEnabled(bool bEnabled) { _streamParsingEnabled = bEnabled; }
|
static void setStreamParsingEnabled(bool bEnabled) { _streamParsingEnabled = bEnabled; }
|
||||||
static bool isStreamParsingEnabled() { return _streamParsingEnabled; }
|
static bool isStreamParsingEnabled() { return _streamParsingEnabled; }
|
||||||
|
|
||||||
|
/*
|
||||||
|
**TrueType fonts with native bytecode hinting**
|
||||||
|
*
|
||||||
|
* All applications that handle TrueType fonts with native hinting must
|
||||||
|
* be aware that TTFs expect different rounding of vertical font
|
||||||
|
* 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
|
||||||
|
* - By default it's enabled for compatible with cocos2d-x-4.0 or older with freetype-2.5.5
|
||||||
|
* - Please see freetype.h
|
||||||
|
* */
|
||||||
|
static void setNativeBytecodeHintingEnabled(bool bEnabled) { _doNativeBytecodeHinting = bEnabled; }
|
||||||
|
static bool isNativeBytecodeHintingEnabled() { return _doNativeBytecodeHinting; }
|
||||||
|
|
||||||
bool isDistanceFieldEnabled() const { return _distanceFieldEnabled; }
|
bool isDistanceFieldEnabled() const { return _distanceFieldEnabled; }
|
||||||
|
|
||||||
float getOutlineSize() const { return _outlineSize; }
|
float getOutlineSize() const { return _outlineSize; }
|
||||||
|
|
||||||
void renderCharAt(unsigned char *dest,int posX, int posY, unsigned char* bitmap,long bitmapWidth,long bitmapHeight);
|
void renderCharAt(unsigned char* dest,
|
||||||
|
int posX,
|
||||||
|
int posY,
|
||||||
|
unsigned char* bitmap,
|
||||||
|
long bitmapWidth,
|
||||||
|
long bitmapHeight);
|
||||||
|
|
||||||
FT_Encoding getEncoding() const { return _encoding; }
|
FT_Encoding getEncoding() const { return _encoding; }
|
||||||
|
|
||||||
|
@ -85,6 +110,7 @@ private:
|
||||||
static FT_Library _FTlibrary;
|
static FT_Library _FTlibrary;
|
||||||
static bool _FTInitialized;
|
static bool _FTInitialized;
|
||||||
static bool _streamParsingEnabled;
|
static bool _streamParsingEnabled;
|
||||||
|
static bool _doNativeBytecodeHinting;
|
||||||
|
|
||||||
FontFreeType(bool distanceFieldEnabled = false, float outline = 0);
|
FontFreeType(bool distanceFieldEnabled = false, float outline = 0);
|
||||||
virtual ~FontFreeType();
|
virtual ~FontFreeType();
|
||||||
|
@ -108,6 +134,8 @@ private:
|
||||||
std::string _fontName;
|
std::string _fontName;
|
||||||
bool _distanceFieldEnabled;
|
bool _distanceFieldEnabled;
|
||||||
float _outlineSize;
|
float _outlineSize;
|
||||||
|
int _ascender;
|
||||||
|
int _descender;
|
||||||
int _lineHeight;
|
int _lineHeight;
|
||||||
FontAtlas* _fontAtlas;
|
FontAtlas* _fontAtlas;
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue