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-2016 Chukong Technologies Inc.
|
||||
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
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
|
@ -26,7 +27,7 @@ THE SOFTWARE.
|
|||
|
||||
#include "2d/CCFontFreeType.h"
|
||||
|
||||
#include <freetype/src/truetype/ttobjs.h>
|
||||
#include <freetype/ftfntfmt.h>
|
||||
|
||||
#include FT_BBOX_H
|
||||
#include "edtaa3func.h"
|
||||
|
@ -43,6 +44,7 @@ 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{|}~¡¢£¤¥¦§¨©ª«¬®¯°±²³´µ¶·¸¹º»¼½¾¿ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖ×ØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö÷øùúûüýþ ";
|
||||
|
@ -126,12 +128,15 @@ FT_Library FontFreeType::getFTLibrary()
|
|||
return _FTlibrary;
|
||||
}
|
||||
|
||||
// clang-format off
|
||||
FontFreeType::FontFreeType(bool distanceFieldEnabled /* = false */, float outline /* = 0 */)
|
||||
: _fontRef(nullptr)
|
||||
, _stroker(nullptr)
|
||||
, _encoding(FT_ENCODING_UNICODE)
|
||||
, _distanceFieldEnabled(distanceFieldEnabled)
|
||||
, _outlineSize(0.0f)
|
||||
, _ascender(0)
|
||||
, _descender(0)
|
||||
, _lineHeight(0)
|
||||
, _fontAtlas(nullptr)
|
||||
, _usedGlyphs(GlyphCollection::ASCII)
|
||||
|
@ -147,6 +152,7 @@ FontFreeType::FontFreeType(bool distanceFieldEnabled /* = false */, float outlin
|
|||
0);
|
||||
}
|
||||
}
|
||||
// clang-format on
|
||||
|
||||
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
|
||||
_fontRef = face;
|
||||
auto* ttSize = (TT_Size)(_fontRef->size);
|
||||
// Notes:
|
||||
// a. ttSize->metrics->height: (ttSize->metrics->ascender - ttSize->metrics->descender)
|
||||
// b. ftSize->metrics.height == ttSize->metrics->height
|
||||
// c. the TT spec always asks for ROUND, not FLOOR or CEIL, see also freetype: ttobjs.c
|
||||
_lineHeight = static_cast<int>((ttSize->metrics->ascender - ttSize->metrics->descender) >> 6);
|
||||
|
||||
// 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
|
||||
// ** 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
|
||||
return true;
|
||||
|
@ -338,7 +357,7 @@ int FontFreeType::getHorizontalKerningForChars(uint64_t firstChar, uint64_t sec
|
|||
|
||||
int FontFreeType::getFontAscender() const
|
||||
{
|
||||
return (static_cast<int>(((TT_Size)_fontRef->size)->metrics->ascender >> 6));
|
||||
return _ascender >> 6;
|
||||
}
|
||||
|
||||
const char* FontFreeType::getFontFamily() const
|
||||
|
|
|
@ -2,19 +2,20 @@
|
|||
Copyright (c) 2013 Zynga Inc.
|
||||
Copyright (c) 2013-2016 Chukong Technologies Inc.
|
||||
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
||||
|
||||
http://www.cocos2d-x.org
|
||||
|
||||
Copyright (c) 2021 Bytedance Inc.
|
||||
|
||||
https://adxe.org
|
||||
|
||||
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
|
||||
|
@ -31,8 +32,8 @@
|
|||
|
||||
#include "2d/CCFont.h"
|
||||
|
||||
#include <string>
|
||||
#include <ft2build.h>
|
||||
#include <string>
|
||||
|
||||
#include FT_FREETYPE_H
|
||||
#include FT_STROKER_H
|
||||
|
@ -44,32 +45,56 @@ class CC_DLL FontFreeType : public Font
|
|||
public:
|
||||
static const int DistanceMapSpread;
|
||||
|
||||
static FontFreeType* create(const std::string &fontName, float fontSize, GlyphCollection glyphs,
|
||||
const char *customGlyphs,bool distanceFieldEnabled = false, float outline = 0);
|
||||
static FontFreeType* create(const std::string& fontName,
|
||||
float fontSize,
|
||||
GlyphCollection glyphs,
|
||||
const char* customGlyphs,
|
||||
bool distanceFieldEnabled = false,
|
||||
float outline = 0);
|
||||
|
||||
static void shutdownFreeType();
|
||||
|
||||
/*
|
||||
* @remark: if you want enable stream parsing, you need do one of follow steps
|
||||
* a. disable .ttf compress on .apk, see:
|
||||
* https://simdsoft.com/notes/#build-apk-config-nocompress-file-type-at-appbuildgradle
|
||||
* b. uncomporess .ttf to disk by yourself.
|
||||
*/
|
||||
* @remark: if you want enable stream parsing, you need do one of follow steps
|
||||
* a. disable .ttf compress on .apk, see:
|
||||
* https://simdsoft.com/notes/#build-apk-config-nocompress-file-type-at-appbuildgradle
|
||||
* b. uncomporess .ttf to disk by yourself.
|
||||
*/
|
||||
static void setStreamParsingEnabled(bool bEnabled) { _streamParsingEnabled = bEnabled; }
|
||||
static bool isStreamParsingEnabled() { return _streamParsingEnabled; }
|
||||
|
||||
bool isDistanceFieldEnabled() const { return _distanceFieldEnabled;}
|
||||
/*
|
||||
**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; }
|
||||
|
||||
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; }
|
||||
|
||||
int* getHorizontalKerningForTextUTF32(const std::u32string& text, int &outNumLetters) const override;
|
||||
|
||||
unsigned char* getGlyphBitmap(uint64_t theChar, long &outWidth, long &outHeight, Rect &outRect,int &xAdvance);
|
||||
|
||||
int* getHorizontalKerningForTextUTF32(const std::u32string& text, int& outNumLetters) const override;
|
||||
|
||||
unsigned char* getGlyphBitmap(uint64_t theChar, long& outWidth, long& outHeight, Rect& outRect, int& xAdvance);
|
||||
|
||||
int getFontAscender() const;
|
||||
const char* getFontFamily() const;
|
||||
std::string getFontName() const { return _fontName; }
|
||||
|
@ -77,7 +102,7 @@ public:
|
|||
virtual FontAtlas* createFontAtlas() override;
|
||||
virtual int getFontMaxHeight() const override { return _lineHeight; }
|
||||
|
||||
static void releaseFont(const std::string &fontName);
|
||||
static void releaseFont(const std::string& fontName);
|
||||
|
||||
private:
|
||||
static const char* _glyphASCII;
|
||||
|
@ -85,21 +110,22 @@ private:
|
|||
static FT_Library _FTlibrary;
|
||||
static bool _FTInitialized;
|
||||
static bool _streamParsingEnabled;
|
||||
static bool _doNativeBytecodeHinting;
|
||||
|
||||
FontFreeType(bool distanceFieldEnabled = false, float outline = 0);
|
||||
virtual ~FontFreeType();
|
||||
|
||||
bool createFontObject(const std::string &fontName, float fontSize);
|
||||
bool createFontObject(const std::string& fontName, float fontSize);
|
||||
|
||||
bool initFreeType();
|
||||
FT_Library getFTLibrary();
|
||||
|
||||
|
||||
int getHorizontalKerningForChars(uint64_t firstChar, uint64_t secondChar) const;
|
||||
unsigned char* getGlyphBitmapWithOutline(uint64_t code, FT_BBox &bbox);
|
||||
unsigned char* getGlyphBitmapWithOutline(uint64_t code, FT_BBox& bbox);
|
||||
|
||||
void setGlyphCollection(GlyphCollection glyphs, const char* customGlyphs = nullptr);
|
||||
const char* getGlyphCollection() const;
|
||||
|
||||
|
||||
FT_Face _fontRef;
|
||||
std::unique_ptr<FT_StreamRec> _fontStream;
|
||||
FT_Stroker _stroker;
|
||||
|
@ -108,6 +134,8 @@ private:
|
|||
std::string _fontName;
|
||||
bool _distanceFieldEnabled;
|
||||
float _outlineSize;
|
||||
int _ascender;
|
||||
int _descender;
|
||||
int _lineHeight;
|
||||
FontAtlas* _fontAtlas;
|
||||
|
||||
|
|
Loading…
Reference in New Issue