axmol/cocos/2d/CCFontFreeType.cpp

737 lines
23 KiB
C++
Raw Normal View History

/****************************************************************************
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
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.
****************************************************************************/
Squashed commit of the following: commit a794d107ad85667e3d754f0b6251fc864dfbf288 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 14:33:49 2014 -0700 Yeah... everything compiles on win32 and wp8 commit 4740be6e4a0d16f742c27996e7ab2c100adc76af Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 13:58:38 2014 -0700 CCIME moved to base and compiles on Android commit ff3e1bf1eb27a01019f4e1b56d1aebbe2d385f72 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 13:02:57 2014 -0700 compiles Ok for Windows Phone 8 commit 8160a4eb2ecdc61b5bd1cf56b90d2da6f11e3ebd Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 12:25:31 2014 -0700 fixes for Windows Phone 8 commit 418197649efc93032aee0adc205e502101cdb53d Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 11:15:13 2014 -0700 Compiles on Win32 commit 08813ed7cf8ac1079ffadeb1ce78ea9e833e1a33 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 10:08:31 2014 -0700 Compiles on linux! commit 118896521e5b335a5257090b6863f1fb2a2002fe Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 09:30:42 2014 -0700 moves cocos/2d/platform -> cocos/platform commit 4fe9319d7717b0c1bccb2db0156eeb86255a89e0 Merge: bd68ec2 511295e Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 08:24:41 2014 -0700 Merge remote-tracking branch 'cocos2d/v3' into files commit bd68ec2f0e3a826d8b2f4b60564ba65ce766bc56 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Thu May 15 19:36:23 2014 -0700 files in the correct directory
2014-05-17 05:36:00 +08:00
#include "2d/CCFontFreeType.h"
#include <freetype/src/truetype/ttobjs.h>
#include FT_BBOX_H
#include "edtaa3func.h"
#include "2d/CCFontAtlas.h"
#include "base/CCDirector.h"
Squashed commit of the following: commit a794d107ad85667e3d754f0b6251fc864dfbf288 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 14:33:49 2014 -0700 Yeah... everything compiles on win32 and wp8 commit 4740be6e4a0d16f742c27996e7ab2c100adc76af Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 13:58:38 2014 -0700 CCIME moved to base and compiles on Android commit ff3e1bf1eb27a01019f4e1b56d1aebbe2d385f72 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 13:02:57 2014 -0700 compiles Ok for Windows Phone 8 commit 8160a4eb2ecdc61b5bd1cf56b90d2da6f11e3ebd Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 12:25:31 2014 -0700 fixes for Windows Phone 8 commit 418197649efc93032aee0adc205e502101cdb53d Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 11:15:13 2014 -0700 Compiles on Win32 commit 08813ed7cf8ac1079ffadeb1ce78ea9e833e1a33 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 10:08:31 2014 -0700 Compiles on linux! commit 118896521e5b335a5257090b6863f1fb2a2002fe Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 09:30:42 2014 -0700 moves cocos/2d/platform -> cocos/platform commit 4fe9319d7717b0c1bccb2db0156eeb86255a89e0 Merge: bd68ec2 511295e Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 08:24:41 2014 -0700 Merge remote-tracking branch 'cocos2d/v3' into files commit bd68ec2f0e3a826d8b2f4b60564ba65ce766bc56 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Thu May 15 19:36:23 2014 -0700 files in the correct directory
2014-05-17 05:36:00 +08:00
#include "base/ccUTF8.h"
#include "freetype/internal/tttypes.h"
Squashed commit of the following: commit a794d107ad85667e3d754f0b6251fc864dfbf288 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 14:33:49 2014 -0700 Yeah... everything compiles on win32 and wp8 commit 4740be6e4a0d16f742c27996e7ab2c100adc76af Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 13:58:38 2014 -0700 CCIME moved to base and compiles on Android commit ff3e1bf1eb27a01019f4e1b56d1aebbe2d385f72 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 13:02:57 2014 -0700 compiles Ok for Windows Phone 8 commit 8160a4eb2ecdc61b5bd1cf56b90d2da6f11e3ebd Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 12:25:31 2014 -0700 fixes for Windows Phone 8 commit 418197649efc93032aee0adc205e502101cdb53d Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 11:15:13 2014 -0700 Compiles on Win32 commit 08813ed7cf8ac1079ffadeb1ce78ea9e833e1a33 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 10:08:31 2014 -0700 Compiles on linux! commit 118896521e5b335a5257090b6863f1fb2a2002fe Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 09:30:42 2014 -0700 moves cocos/2d/platform -> cocos/platform commit 4fe9319d7717b0c1bccb2db0156eeb86255a89e0 Merge: bd68ec2 511295e Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 08:24:41 2014 -0700 Merge remote-tracking branch 'cocos2d/v3' into files commit bd68ec2f0e3a826d8b2f4b60564ba65ce766bc56 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Thu May 15 19:36:23 2014 -0700 files in the correct directory
2014-05-17 05:36:00 +08:00
#include "platform/CCFileUtils.h"
#include "platform/CCFileStream.h"
NS_CC_BEGIN
2013-07-17 06:18:39 +08:00
FT_Library FontFreeType::_FTlibrary;
bool FontFreeType::_FTInitialized = false;
2020-08-27 11:40:44 +08:00
bool FontFreeType::_streamParsingEnabled = false;
const int FontFreeType::DistanceMapSpread = 3;
2013-07-17 06:18:39 +08:00
2015-07-30 11:33:18 +08:00
const char* FontFreeType::_glyphASCII = "\"!#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~¡¢£¤¥¦§¨©ª«¬­®¯°±²³´µ¶·¸¹º»¼½¾¿ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖ×ØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö÷øùúûüýþ ";
const char* FontFreeType::_glyphNEHE = "!\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~ ";
typedef struct _DataRef
{
Data data;
unsigned int referenceCount;
}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) {
auto fd = (FileStream*)stream->descriptor.pointer;
if (!fd) return 1;
if (!size && offset >= stream->size) return 1;
if (stream->pos != offset)
fd->seek(offset, SEEK_SET);
if (buf)
return fd->read(buf, size);
return 0;
}
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;
}
2017-01-13 14:31:12 +08:00
FontFreeType * FontFreeType::create(const std::string &fontName, float fontSize, GlyphCollection glyphs, const char *customGlyphs,bool distanceFieldEnabled /* = false */,float outline /* = 0 */)
2013-08-06 06:56:18 +08:00
{
FontFreeType *tempFont = new (std::nothrow) FontFreeType(distanceFieldEnabled,outline);
2013-08-07 04:43:29 +08:00
if (!tempFont)
return nullptr;
2015-07-30 11:33:18 +08:00
tempFont->setGlyphCollection(glyphs, customGlyphs);
2013-08-07 04:43:29 +08:00
2013-09-13 11:46:46 +08:00
if (!tempFont->createFontObject(fontName, fontSize))
2013-08-07 04:43:29 +08:00
{
delete tempFont;
return nullptr;
}
tempFont->autorelease();
2013-08-07 04:43:29 +08:00
return tempFont;
2013-08-06 06:56:18 +08:00
}
2013-07-17 06:18:39 +08:00
bool FontFreeType::initFreeType()
{
2013-07-17 06:18:39 +08:00
if (_FTInitialized == false)
{
// begin freetype
if (FT_Init_FreeType( &_FTlibrary ))
return false;
_FTInitialized = true;
}
2013-07-17 06:18:39 +08:00
return _FTInitialized;
}
2013-07-17 06:18:39 +08:00
void FontFreeType::shutdownFreeType()
{
2013-07-17 06:18:39 +08:00
if (_FTInitialized == true)
{
FT_Done_FreeType(_FTlibrary);
2016-03-24 23:05:22 +08:00
s_cacheFontData.clear();
_FTInitialized = false;
2013-07-17 06:18:39 +08:00
}
}
2013-07-17 06:18:39 +08:00
FT_Library FontFreeType::getFTLibrary()
{
2013-07-17 06:18:39 +08:00
initFreeType();
return _FTlibrary;
}
2017-01-13 14:31:12 +08:00
FontFreeType::FontFreeType(bool distanceFieldEnabled /* = false */, float outline /* = 0 */)
: _fontRef(nullptr)
, _stroker(nullptr)
2018-05-14 10:34:56 +08:00
, _encoding(FT_ENCODING_UNICODE)
, _distanceFieldEnabled(distanceFieldEnabled)
, _outlineSize(0.0f)
, _lineHeight(0)
, _fontAtlas(nullptr)
2015-07-30 11:33:18 +08:00
, _usedGlyphs(GlyphCollection::ASCII)
2013-07-17 06:18:39 +08:00
{
2017-01-13 14:31:12 +08:00
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);
}
2013-07-17 06:18:39 +08:00
}
2015-09-28 21:15:51 +08:00
bool FontFreeType::createFontObject(const std::string &fontName, float fontSize)
2013-07-17 06:18:39 +08:00
{
FT_Face face;
// save font name locally
_fontName = fontName;
2020-08-27 11:40:44 +08:00
if (!_streamParsingEnabled) {
auto it = s_cacheFontData.find(fontName);
if (it != s_cacheFontData.end())
{
(*it).second.referenceCount += 1;
}
else
{
s_cacheFontData[fontName].referenceCount = 1;
s_cacheFontData[fontName].data = FileUtils::getInstance()->getDataFromFile(fontName);
if (s_cacheFontData[fontName].data.isNull())
{
return false;
}
}
if (FT_New_Memory_Face(getFTLibrary(), s_cacheFontData[fontName].data.getBytes(), s_cacheFontData[fontName].data.getSize(), 0, &face))
return false;
}
else {
auto fullPath = FileUtils::getInstance()->fullPathForFilename(fontName);
if (fullPath.empty()) return false;
auto fs = FileUtils::getInstance()->openFileStream(fullPath, FileStream::Mode::READ).release();
if (!fs)
{
return false;
}
std::unique_ptr<FT_StreamRec> fts(new FT_StreamRec());
fts->read = ft_stream_read_callback;
fts->close = ft_stream_close_callback;
fts->size = fs->size();
fts->descriptor.pointer = fs;
FT_Open_Args args = { 0 };
args.flags = FT_OPEN_STREAM;
args.stream = fts.get();
if (FT_Open_Face(getFTLibrary(), &args, 0, &face))
return false;
_fontStream = std::move(fts);
}
Adds xml support in UIRichText Supported tags: - <small>: makes the font 20% smaller - <big>: makes the font 25% bigger - <img src="" />: to add an image - <font face="path to font" size="size" color="#00ffaa">: font attributes - <i>: italics - <b>: bold - <u>: underline - <del> strike-through - </br>: new line Also adds support for strikethrough, italics, bold and underlie to `Label` Contains tests for UIRichText and Label Squashed commit of the following: commit a0d6ae0513d40dbf414930210ab032a49d8984a0 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Tue Jan 19 20:39:21 2016 -0800 cleanup LabelTestNew commit 0cf423af85d88a42fc1317207feeb3d05da7f8ab Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Tue Jan 19 20:34:53 2016 -0800 adds maaaany tests ...and fixes width and height in <img src=""> commit e8ba6acd5d79bbc766d7aa02ef166e166c801d01 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri Jan 15 19:41:37 2016 -0800 Label + URL working Ok commit 36689e29ee3d77310e92264a09084cc06e30664c Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri Jan 15 18:24:44 2016 -0800 adding this code just in case... ... should use a listener component instead commit af03708950e74483b875d0baad593aa6ed242a04 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Thu Jan 14 20:23:36 2016 -0800 URL node plus <de><u> and <a img commit e3a4930b012c0b3756752dac6ffb5ad43d24ae99 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Jan 13 18:42:18 2016 -0800 strikethrough and underline implemented they support multiline and horizontal alignment as well includes test however vertical alignment might not work commit 640ccf39f56e153db46785a61be7000e898174c7 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Tue Jan 12 18:14:57 2016 -0800 bug fixes commit 5e41fb76e91c571639585a609a255eb41797a302 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Tue Jan 12 16:47:20 2016 -0800 adds italics and bold "disable test" commit 202c5a45bb9c8ea160b9f6880ef858874e07814b Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Tue Jan 12 14:40:13 2016 -0800 adds italics test commit d1a8b421445053cc36860fc376f52692a3672dfe Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Mon Jan 11 20:53:23 2016 -0800 italics is working ok commit fdd02087fce920c27c2409301ec88685fe68085b Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri Jan 8 17:18:56 2016 -0800 color, size and face working commit c01bdef6b1d49f8805b69d4c162b74cd00c8f5b3 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Thu Jan 7 19:02:16 2016 -0800 initial commit
2016-01-20 12:41:31 +08:00
2013-09-13 11:46:46 +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;
}
}
2013-07-17 06:18:39 +08:00
// set the requested font size
int dpi = 72;
int fontSizePoints = (int)(64.f * fontSize * CC_CONTENT_SCALE_FACTOR());
if (FT_Set_Char_Size(face, fontSizePoints, fontSizePoints, dpi, dpi))
2013-07-17 06:18:39 +08:00
return false;
2013-07-17 06:18:39 +08:00
// store the face globally
_fontRef = face;
auto* ttSize = (TT_Size)(_fontRef->size);
2021-06-17 21:56:38 +08:00
// 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);
2021-06-17 21:56:38 +08:00
2013-07-17 06:18:39 +08:00
// done and good
return true;
}
FontFreeType::~FontFreeType()
{
if (_FTInitialized)
{
if (_stroker)
{
FT_Stroker_Done(_stroker);
}
if (_fontRef)
{
FT_Done_Face(_fontRef);
}
}
auto iter = s_cacheFontData.find(_fontName);
if (iter != s_cacheFontData.end())
{
iter->second.referenceCount -= 1;
if (iter->second.referenceCount == 0)
{
s_cacheFontData.erase(iter);
}
}
2013-07-17 06:18:39 +08:00
}
FontAtlas * FontFreeType::createFontAtlas()
{
if (_fontAtlas == nullptr)
{
_fontAtlas = new (std::nothrow) FontAtlas(*this);
if (_fontAtlas && _usedGlyphs != GlyphCollection::DYNAMIC)
{
std::u32string utf32;
if (StringUtils::UTF8ToUTF32(getGlyphCollection(), utf32))
{
_fontAtlas->prepareLetterDefinitions(utf32);
}
}
// this->autorelease();
}
return _fontAtlas;
}
int * FontFreeType::getHorizontalKerningForTextUTF32(const std::u32string& text, int &outNumLetters) const
{
if (!_fontRef)
2014-03-07 12:03:45 +08:00
return nullptr;
outNumLetters = static_cast<int>(text.length());
if (!outNumLetters)
2014-03-07 12:03:45 +08:00
return nullptr;
int *sizes = new (std::nothrow) int[outNumLetters];
2013-09-13 11:46:46 +08:00
if (!sizes)
2014-03-07 12:03:45 +08:00
return nullptr;
memset(sizes,0,outNumLetters * sizeof(int));
bool hasKerning = FT_HAS_KERNING( _fontRef ) != 0;
if (hasKerning)
{
2014-03-07 12:03:45 +08:00
for (int c = 1; c < outNumLetters; ++c)
{
sizes[c] = getHorizontalKerningForChars(text[c-1], text[c]);
}
}
2013-09-13 11:46:46 +08:00
return sizes;
2013-07-17 06:18:39 +08:00
}
int FontFreeType::getHorizontalKerningForChars(uint64_t firstChar, uint64_t secondChar) const
2013-07-17 06:18:39 +08:00
{
// get the ID to the char we need
2019-10-09 17:50:32 +08:00
int glyphIndex1 = FT_Get_Char_Index(_fontRef, static_cast<FT_ULong>(firstChar));
2013-09-13 11:46:46 +08:00
if (!glyphIndex1)
return 0;
2013-07-17 06:18:39 +08:00
// get the ID to the char we need
2019-10-09 17:50:32 +08:00
int glyphIndex2 = FT_Get_Char_Index(_fontRef, static_cast<FT_ULong>(secondChar));
2013-09-13 11:46:46 +08:00
if (!glyphIndex2)
return 0;
2013-07-17 06:18:39 +08:00
FT_Vector kerning;
2013-09-13 11:46:46 +08:00
if (FT_Get_Kerning( _fontRef, glyphIndex1, glyphIndex2, FT_KERNING_DEFAULT, &kerning))
return 0;
2013-12-06 16:32:06 +08:00
return (static_cast<int>(kerning.x >> 6));
}
int FontFreeType::getFontAscender() const
{
return (static_cast<int>(((TT_Size)_fontRef->size)->metrics->ascender >> 6));
}
2016-01-20 02:43:35 +08:00
const char* FontFreeType::getFontFamily() const
{
if (!_fontRef)
return nullptr;
return _fontRef->family_name;
}
unsigned char* FontFreeType::getGlyphBitmap(uint64_t theChar, long &outWidth, long &outHeight, Rect &outRect,int &xAdvance)
{
bool invalidChar = true;
unsigned char* ret = nullptr;
do
{
if (_fontRef == nullptr)
break;
// @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)))
break;
#if defined(_DEBUG)
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());
}
#endif
auto& metrics = _fontRef->glyph->metrics;
2019-10-09 17:50:32 +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));
outRect.size.height = static_cast<float>((metrics.height >> 6));
xAdvance = (static_cast<int>(_fontRef->glyph->metrics.horiAdvance >> 6));
outWidth = _fontRef->glyph->bitmap.width;
outHeight = _fontRef->glyph->bitmap.rows;
ret = _fontRef->glyph->bitmap.buffer;
if (_outlineSize > 0 && outWidth > 0 && outHeight > 0)
{
auto copyBitmap = new (std::nothrow) unsigned char[outWidth * outHeight];
memcpy(copyBitmap,ret,outWidth * outHeight * sizeof(unsigned char));
FT_BBox bbox;
auto outlineBitmap = getGlyphBitmapWithOutline(theChar,bbox);
if(outlineBitmap == nullptr)
{
ret = nullptr;
delete [] copyBitmap;
break;
}
2019-10-09 17:50:32 +08:00
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;
2019-10-25 16:40:30 +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;
auto outlineHeight = outlineMaxY - outlineMinY;
auto blendImageMinX = MIN(outlineMinX, glyphMinX);
auto blendImageMaxY = MAX(outlineMaxY, glyphMaxY);
auto blendWidth = MAX(outlineMaxX, glyphMaxX) - blendImageMinX;
auto blendHeight = blendImageMaxY - MIN(outlineMinY, glyphMinY);
2019-10-09 17:50:32 +08:00
outRect.origin.x = (float)blendImageMinX;
outRect.origin.y = -blendImageMaxY + _outlineSize;
2017-02-06 15:17:18 +08:00
unsigned char *blendImage = nullptr;
if (blendWidth > 0 && blendHeight > 0)
{
2019-10-25 16:40:30 +08:00
FT_Pos index, index2;
auto imageSize = blendWidth * blendHeight * 2;
2019-10-09 17:50:32 +08:00
blendImage = new (std::nothrow) unsigned char[imageSize];
memset(blendImage, 0, imageSize);
2017-02-06 15:17:18 +08:00
auto px = outlineMinX - blendImageMinX;
auto py = blendImageMaxY - outlineMaxY;
for (int x = 0; x < outlineWidth; ++x)
{
2017-02-06 15:17:18 +08:00
for (int y = 0; y < outlineHeight; ++y)
{
index = px + x + ((py + y) * blendWidth);
index2 = x + (y * outlineWidth);
blendImage[2 * index] = outlineBitmap[index2];
}
}
2017-02-06 15:17:18 +08:00
px = glyphMinX - blendImageMinX;
py = blendImageMaxY - glyphMaxY;
for (int x = 0; x < outWidth; ++x)
{
2017-02-06 15:17:18 +08:00
for (int y = 0; y < outHeight; ++y)
{
index = px + x + ((y + py) * blendWidth);
index2 = x + (y * outWidth);
blendImage[2 * index + 1] = copyBitmap[index2];
}
}
}
2019-10-09 17:50:32 +08:00
outRect.size.width = (float)blendWidth;
outRect.size.height = (float)blendHeight;
outWidth = blendWidth;
outHeight = blendHeight;
delete [] outlineBitmap;
delete [] copyBitmap;
ret = blendImage;
}
invalidChar = false;
} while (0);
if (invalidChar)
{
outRect.size.width = 0;
outRect.size.height = 0;
xAdvance = 0;
return nullptr;
}
else
{
return ret;
}
}
unsigned char * FontFreeType::getGlyphBitmapWithOutline(uint64_t theChar, FT_BBox &bbox)
{
unsigned char* ret = nullptr;
2019-10-09 17:50:32 +08:00
if (FT_Load_Char(_fontRef, static_cast<FT_ULong>(theChar), FT_LOAD_NO_BITMAP) == 0)
{
if (_fontRef->glyph->format == FT_GLYPH_FORMAT_OUTLINE)
{
FT_Glyph glyph;
if (FT_Get_Glyph(_fontRef->glyph, &glyph) == 0)
{
FT_Glyph_StrokeBorder(&glyph, _stroker, 0, 1);
if (glyph->format == FT_GLYPH_FORMAT_OUTLINE)
{
FT_Outline *outline = &reinterpret_cast<FT_OutlineGlyph>(glyph)->outline;
FT_Glyph_Get_CBox(glyph,FT_GLYPH_BBOX_GRIDFIT,&bbox);
2014-03-20 11:58:36 +08:00
long width = (bbox.xMax - bbox.xMin)>>6;
long rows = (bbox.yMax - bbox.yMin)>>6;
FT_Bitmap bmp;
bmp.buffer = new (std::nothrow) unsigned char[width * rows];
memset(bmp.buffer, 0, width * rows);
2014-03-20 11:58:36 +08:00
bmp.width = (int)width;
bmp.rows = (int)rows;
bmp.pitch = (int)width;
bmp.pixel_mode = FT_PIXEL_MODE_GRAY;
bmp.num_grays = 256;
FT_Raster_Params params;
memset(&params, 0, sizeof (params));
params.source = outline;
params.target = &bmp;
params.flags = FT_RASTER_FLAG_AA;
FT_Outline_Translate(outline,-bbox.xMin,-bbox.yMin);
FT_Outline_Render(_FTlibrary, outline, &params);
ret = bmp.buffer;
}
FT_Done_Glyph(glyph);
}
}
}
return ret;
}
2014-03-20 11:58:36 +08:00
unsigned char * makeDistanceMap( unsigned char *img, long width, long height)
{
2014-03-20 11:58:36 +08:00
long pixelAmount = (width + 2 * FontFreeType::DistanceMapSpread) * (height + 2 * FontFreeType::DistanceMapSpread);
short * xdist = (short *) malloc( pixelAmount * sizeof(short) );
short * ydist = (short *) malloc( pixelAmount * sizeof(short) );
double * gx = (double *) calloc( pixelAmount, sizeof(double) );
double * gy = (double *) calloc( pixelAmount, sizeof(double) );
double * data = (double *) calloc( pixelAmount, sizeof(double) );
double * outside = (double *) calloc( pixelAmount, sizeof(double) );
double * inside = (double *) calloc( pixelAmount, sizeof(double) );
2014-03-24 10:12:40 +08:00
long i,j;
// Convert img into double (data) rescale image levels between 0 and 1
2014-03-20 11:58:36 +08:00
long outWidth = width + 2 * FontFreeType::DistanceMapSpread;
for (i = 0; i < width; ++i)
{
for (j = 0; j < height; ++j)
{
data[j * outWidth + FontFreeType::DistanceMapSpread + i] = img[j * width + i] / 255.0;
}
}
width += 2 * FontFreeType::DistanceMapSpread;
height += 2 * FontFreeType::DistanceMapSpread;
// Transform background (outside contour, in areas of 0's)
2014-03-20 11:58:36 +08:00
computegradient( data, (int)width, (int)height, gx, gy);
edtaa3(data, gx, gy, (int)width, (int)height, xdist, ydist, outside);
for( i=0; i< pixelAmount; i++)
if( outside[i] < 0.0 )
outside[i] = 0.0;
// Transform foreground (inside contour, in areas of 1's)
for( i=0; i< pixelAmount; i++)
data[i] = 1 - data[i];
2014-03-20 11:58:36 +08:00
computegradient( data, (int)width, (int)height, gx, gy);
edtaa3(data, gx, gy, (int)width, (int)height, xdist, ydist, inside);
for( i=0; i< pixelAmount; i++)
if( inside[i] < 0.0 )
inside[i] = 0.0;
// The bipolar distance field is now outside-inside
double dist;
/* Single channel 8-bit output (bad precision and range, but simple) */
unsigned char *out = (unsigned char *) malloc( pixelAmount * sizeof(unsigned char) );
for( i=0; i < pixelAmount; i++)
{
dist = outside[i] - inside[i];
dist = 128.0 - dist*16;
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) */
/*unsigned char *out = (unsigned char *) malloc( pixelAmount * 3 * sizeof(unsigned char) );
for( i=0; i< pixelAmount; i++)
{
dist = outside[i] - inside[i];
dist = 128.0 - dist*16;
if( dist < 0.0 ) dist = 0.0;
if( dist >= 256.0 ) dist = 255.999;
// R channel is a copy of the original grayscale image
out[3*i] = img[i];
// G channel is fraction
out[3*i + 1] = (unsigned char) ( 256 - (dist - floor(dist)* 256.0 ));
// B channel is truncated integer part
out[3*i + 2] = (unsigned char)dist;
}*/
free( xdist );
free( ydist );
free( gx );
free( gy );
free( data );
free( outside );
free( inside );
return out;
}
2014-03-20 11:58:36 +08:00
void FontFreeType::renderCharAt(unsigned char *dest,int posX, int posY, unsigned char* bitmap,long bitmapWidth,long bitmapHeight)
{
int iX = posX;
int iY = posY;
if (_distanceFieldEnabled)
{
auto distanceMap = makeDistanceMap(bitmap,bitmapWidth,bitmapHeight);
bitmapWidth += 2 * DistanceMapSpread;
bitmapHeight += 2 * DistanceMapSpread;
2014-03-20 11:58:36 +08:00
for (long y = 0; y < bitmapHeight; ++y)
{
2014-03-20 11:58:36 +08:00
long bitmap_y = y * bitmapWidth;
2014-04-09 20:41:09 +08:00
for (long x = 0; x < bitmapWidth; ++x)
{
/* Dual channel 16-bit output (more complicated, but good precision and range) */
/*int index = (iX + ( iY * destSize )) * 3;
int index2 = (bitmap_y + x)*3;
dest[index] = out[index2];
dest[index + 1] = out[index2 + 1];
dest[index + 2] = out[index2 + 2];*/
//Single channel 8-bit output
dest[iX + ( iY * FontAtlas::CacheTextureWidth )] = distanceMap[bitmap_y + x];
iX += 1;
}
iX = posX;
iY += 1;
}
free(distanceMap);
}
else if(_outlineSize > 0)
{
unsigned char tempChar;
2014-03-20 11:58:36 +08:00
for (long y = 0; y < bitmapHeight; ++y)
{
2014-03-20 11:58:36 +08:00
long bitmap_y = y * bitmapWidth;
for (int x = 0; x < bitmapWidth; ++x)
{
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;
iX += 1;
}
iX = posX;
iY += 1;
}
delete [] bitmap;
}
else
{
2014-03-20 11:58:36 +08:00
for (long y = 0; y < bitmapHeight; ++y)
{
2014-03-20 11:58:36 +08:00
long bitmap_y = y * bitmapWidth;
for (int x = 0; x < bitmapWidth; ++x)
{
unsigned char cTemp = bitmap[bitmap_y + x];
// the final pixel
dest[(iX + ( iY * FontAtlas::CacheTextureWidth ) )] = cTemp;
iX += 1;
}
iX = posX;
iY += 1;
}
}
}
2015-07-30 11:33:18 +08:00
void FontFreeType::setGlyphCollection(GlyphCollection glyphs, const char* customGlyphs /* = nullptr */)
{
_usedGlyphs = glyphs;
if (glyphs == GlyphCollection::CUSTOM)
{
_customGlyphs = customGlyphs;
}
}
const char* FontFreeType::getGlyphCollection() const
{
const char* glyphCollection = nullptr;
switch (_usedGlyphs)
{
case cocos2d::GlyphCollection::DYNAMIC:
break;
case cocos2d::GlyphCollection::NEHE:
glyphCollection = _glyphNEHE;
break;
case cocos2d::GlyphCollection::ASCII:
glyphCollection = _glyphASCII;
break;
case cocos2d::GlyphCollection::CUSTOM:
glyphCollection = _customGlyphs.c_str();
break;
default:
break;
}
return glyphCollection;
}
void FontFreeType::releaseFont(const std::string &fontName)
{
auto item = s_cacheFontData.begin();
while (s_cacheFontData.end() != item)
{
2016-01-20 02:47:18 +08:00
if (item->first.find(fontName) != std::string::npos)
item = s_cacheFontData.erase(item);
else
item++;
}
}
Squashed commit of the following: commit a794d107ad85667e3d754f0b6251fc864dfbf288 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 14:33:49 2014 -0700 Yeah... everything compiles on win32 and wp8 commit 4740be6e4a0d16f742c27996e7ab2c100adc76af Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 13:58:38 2014 -0700 CCIME moved to base and compiles on Android commit ff3e1bf1eb27a01019f4e1b56d1aebbe2d385f72 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 13:02:57 2014 -0700 compiles Ok for Windows Phone 8 commit 8160a4eb2ecdc61b5bd1cf56b90d2da6f11e3ebd Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 12:25:31 2014 -0700 fixes for Windows Phone 8 commit 418197649efc93032aee0adc205e502101cdb53d Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 11:15:13 2014 -0700 Compiles on Win32 commit 08813ed7cf8ac1079ffadeb1ce78ea9e833e1a33 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 10:08:31 2014 -0700 Compiles on linux! commit 118896521e5b335a5257090b6863f1fb2a2002fe Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 09:30:42 2014 -0700 moves cocos/2d/platform -> cocos/platform commit 4fe9319d7717b0c1bccb2db0156eeb86255a89e0 Merge: bd68ec2 511295e Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 08:24:41 2014 -0700 Merge remote-tracking branch 'cocos2d/v3' into files commit bd68ec2f0e3a826d8b2f4b60564ba65ce766bc56 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Thu May 15 19:36:23 2014 -0700 files in the correct directory
2014-05-17 05:36:00 +08:00
NS_CC_END