2013-07-19 04:13:41 +08:00
|
|
|
/****************************************************************************
|
2013-10-29 20:25:03 +08:00
|
|
|
Copyright (c) 2013 Zynga Inc.
|
2014-01-07 11:25:07 +08:00
|
|
|
Copyright (c) 2013-2014 Chukong Technologies Inc.
|
|
|
|
|
2013-10-29 20:25:03 +08:00
|
|
|
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.
|
|
|
|
****************************************************************************/
|
2013-07-12 06:43:45 +08:00
|
|
|
|
|
|
|
#include <stdio.h>
|
2013-10-29 20:25:03 +08:00
|
|
|
#include <algorithm>
|
2013-08-07 05:37:19 +08:00
|
|
|
|
2013-10-14 14:01:00 +08:00
|
|
|
#include "ccUTF8.h"
|
2014-05-01 10:09:13 +08:00
|
|
|
#include "2d/CCFontFreeType.h"
|
2014-04-27 01:11:22 +08:00
|
|
|
#include "2d/platform/CCFileUtils.h"
|
2014-01-21 10:36:32 +08:00
|
|
|
#include "edtaa3func.h"
|
2014-03-05 15:54:40 +08:00
|
|
|
#include FT_BBOX_H
|
2013-07-12 06:43:45 +08:00
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
2013-07-17 06:18:39 +08:00
|
|
|
|
|
|
|
FT_Library FontFreeType::_FTlibrary;
|
|
|
|
bool FontFreeType::_FTInitialized = false;
|
2014-01-21 10:36:32 +08:00
|
|
|
const int FontFreeType::DistanceMapSpread = 3;
|
2013-07-17 06:18:39 +08:00
|
|
|
|
2014-03-28 09:44:16 +08:00
|
|
|
typedef struct _DataRef
|
|
|
|
{
|
|
|
|
Data data;
|
|
|
|
unsigned int referenceCount;
|
|
|
|
}DataRef;
|
|
|
|
|
|
|
|
static std::unordered_map<std::string, DataRef> s_cacheFontData;
|
2014-03-27 17:04:05 +08:00
|
|
|
|
2014-03-05 15:54:40 +08:00
|
|
|
FontFreeType * FontFreeType::create(const std::string &fontName, int fontSize, GlyphCollection glyphs, const char *customGlyphs,bool distanceFieldEnabled /* = false */,int outline /* = 0 */)
|
2013-08-06 06:56:18 +08:00
|
|
|
{
|
2014-03-05 15:54:40 +08:00
|
|
|
FontFreeType *tempFont = new FontFreeType(distanceFieldEnabled,outline);
|
2013-10-29 20:25:03 +08:00
|
|
|
|
2013-08-07 04:43:29 +08:00
|
|
|
if (!tempFont)
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
tempFont->setCurrentGlyphCollection(glyphs, customGlyphs);
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
return tempFont;
|
2013-08-06 06:56:18 +08:00
|
|
|
}
|
2013-07-17 06:18:39 +08:00
|
|
|
|
|
|
|
bool FontFreeType::initFreeType()
|
2013-07-12 06:43:45 +08:00
|
|
|
{
|
2013-07-17 06:18:39 +08:00
|
|
|
if (_FTInitialized == false)
|
|
|
|
{
|
|
|
|
// begin freetype
|
|
|
|
if (FT_Init_FreeType( &_FTlibrary ))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
_FTInitialized = true;
|
|
|
|
}
|
2013-07-12 06:43:45 +08:00
|
|
|
|
2013-07-17 06:18:39 +08:00
|
|
|
return _FTInitialized;
|
2013-07-12 06:43:45 +08:00
|
|
|
}
|
|
|
|
|
2013-07-17 06:18:39 +08:00
|
|
|
void FontFreeType::shutdownFreeType()
|
2013-07-12 06:43:45 +08:00
|
|
|
{
|
2013-07-17 06:18:39 +08:00
|
|
|
if (_FTInitialized == true)
|
|
|
|
{
|
|
|
|
FT_Done_FreeType(_FTlibrary);
|
2013-10-29 20:25:03 +08:00
|
|
|
_FTInitialized = false;
|
2013-07-17 06:18:39 +08:00
|
|
|
}
|
2013-07-12 06:43:45 +08:00
|
|
|
}
|
|
|
|
|
2013-07-17 06:18:39 +08:00
|
|
|
FT_Library FontFreeType::getFTLibrary()
|
2013-07-12 06:43:45 +08:00
|
|
|
{
|
2013-07-17 06:18:39 +08:00
|
|
|
initFreeType();
|
|
|
|
return _FTlibrary;
|
|
|
|
}
|
|
|
|
|
2014-03-05 15:54:40 +08:00
|
|
|
FontFreeType::FontFreeType(bool distanceFieldEnabled /* = false */,int outline /* = 0 */)
|
2014-01-22 14:57:11 +08:00
|
|
|
: _fontRef(nullptr)
|
2014-03-05 15:54:40 +08:00
|
|
|
,_distanceFieldEnabled(distanceFieldEnabled)
|
|
|
|
,_outlineSize(outline)
|
|
|
|
,_stroker(nullptr)
|
2013-07-17 06:18:39 +08:00
|
|
|
{
|
2014-03-05 15:54:40 +08:00
|
|
|
if (_outlineSize > 0)
|
|
|
|
{
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
bool FontFreeType::createFontObject(const std::string &fontName, int fontSize)
|
|
|
|
{
|
2013-10-29 20:25:03 +08:00
|
|
|
FT_Face face;
|
2014-03-28 09:44:16 +08:00
|
|
|
// save font name locally
|
|
|
|
_fontName = fontName;
|
2013-10-29 20:25:03 +08:00
|
|
|
|
2014-03-28 09:44:16 +08:00
|
|
|
auto it = s_cacheFontData.find(fontName);
|
|
|
|
if (it != s_cacheFontData.end())
|
2014-03-27 17:04:05 +08:00
|
|
|
{
|
2014-03-28 09:44:16 +08:00
|
|
|
(*it).second.referenceCount += 1;
|
2014-03-27 17:04:05 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-03-28 09:44:16 +08:00
|
|
|
s_cacheFontData[fontName].referenceCount = 1;
|
|
|
|
s_cacheFontData[fontName].data = FileUtils::getInstance()->getDataFromFile(fontName);
|
|
|
|
|
|
|
|
if (s_cacheFontData[fontName].data.isNull())
|
2014-03-27 17:04:05 +08:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2013-10-29 20:25:03 +08:00
|
|
|
|
2014-03-28 09:44:16 +08:00
|
|
|
if (FT_New_Memory_Face(getFTLibrary(), s_cacheFontData[fontName].data.getBytes(), s_cacheFontData[fontName].data.getSize(), 0, &face ))
|
2013-07-17 06:18:39 +08:00
|
|
|
return false;
|
2014-03-27 17:04:05 +08:00
|
|
|
|
2013-07-17 06:18:39 +08:00
|
|
|
//we want to use unicode
|
2013-09-13 11:46:46 +08:00
|
|
|
if (FT_Select_Charmap(face, FT_ENCODING_UNICODE))
|
2013-07-12 06:43:45 +08:00
|
|
|
return false;
|
2013-10-29 20:25:03 +08:00
|
|
|
|
2013-07-17 06:18:39 +08:00
|
|
|
// set the requested font size
|
2013-10-29 20:25:03 +08:00
|
|
|
int dpi = 72;
|
|
|
|
int fontSizePoints = (int)(64.f * fontSize);
|
|
|
|
if (FT_Set_Char_Size(face, fontSizePoints, fontSizePoints, dpi, dpi))
|
2013-07-17 06:18:39 +08:00
|
|
|
return false;
|
2013-07-12 06:43:45 +08:00
|
|
|
|
2013-07-17 06:18:39 +08:00
|
|
|
// store the face globally
|
|
|
|
_fontRef = face;
|
2013-07-12 06:43:45 +08:00
|
|
|
|
2013-07-17 06:18:39 +08:00
|
|
|
// done and good
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
FontFreeType::~FontFreeType()
|
|
|
|
{
|
2014-03-05 15:54:40 +08:00
|
|
|
if (_stroker)
|
|
|
|
{
|
|
|
|
FT_Stroker_Done(_stroker);
|
|
|
|
}
|
2013-10-29 20:25:03 +08:00
|
|
|
if (_fontRef)
|
|
|
|
{
|
|
|
|
FT_Done_Face(_fontRef);
|
|
|
|
}
|
2014-03-27 17:04:05 +08:00
|
|
|
|
2014-03-28 09:44:16 +08:00
|
|
|
s_cacheFontData[_fontName].referenceCount -= 1;
|
|
|
|
if (s_cacheFontData[_fontName].referenceCount == 0)
|
2014-03-27 17:04:05 +08:00
|
|
|
{
|
2014-03-27 17:48:16 +08:00
|
|
|
s_cacheFontData.erase(_fontName);
|
2014-03-27 17:04:05 +08:00
|
|
|
}
|
2013-07-17 06:18:39 +08:00
|
|
|
}
|
|
|
|
|
2013-08-06 06:11:07 +08:00
|
|
|
FontAtlas * FontFreeType::createFontAtlas()
|
|
|
|
{
|
2014-01-20 10:32:12 +08:00
|
|
|
FontAtlas *atlas = new FontAtlas(*this);
|
|
|
|
if (_usedGlyphs != GlyphCollection::DYNAMIC)
|
2013-10-29 20:25:03 +08:00
|
|
|
{
|
2014-05-08 11:12:59 +08:00
|
|
|
std::u16string utf16;
|
|
|
|
if (StringUtils::UTF8ToUTF16(getCurrentGlyphCollection(), utf16))
|
|
|
|
{
|
|
|
|
atlas->prepareLetterDefinitions(utf16);
|
|
|
|
}
|
2014-01-20 10:32:12 +08:00
|
|
|
}
|
|
|
|
this->release();
|
|
|
|
return atlas;
|
2013-08-06 06:11:07 +08:00
|
|
|
}
|
|
|
|
|
2014-05-08 11:12:59 +08:00
|
|
|
int * FontFreeType::getHorizontalKerningForTextUTF16(const std::u16string& text, int &outNumLetters) const
|
2013-07-12 06:43:45 +08:00
|
|
|
{
|
2014-05-08 11:12:59 +08:00
|
|
|
if (!_fontRef)
|
2014-03-07 12:03:45 +08:00
|
|
|
return nullptr;
|
2013-07-12 06:43:45 +08:00
|
|
|
|
2014-05-08 11:12:59 +08:00
|
|
|
outNumLetters = static_cast<int>(text.length());
|
2013-07-12 06:43:45 +08:00
|
|
|
|
|
|
|
if (!outNumLetters)
|
2014-03-07 12:03:45 +08:00
|
|
|
return nullptr;
|
2013-07-12 06:43:45 +08:00
|
|
|
|
2014-01-21 17:55:49 +08:00
|
|
|
int *sizes = new 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)
|
2013-07-12 06:43:45 +08:00
|
|
|
{
|
2014-03-07 12:03:45 +08:00
|
|
|
for (int c = 1; c < outNumLetters; ++c)
|
|
|
|
{
|
|
|
|
sizes[c] = getHorizontalKerningForChars(text[c-1], text[c]);
|
|
|
|
}
|
2013-07-12 06:43:45 +08:00
|
|
|
}
|
|
|
|
|
2013-09-13 11:46:46 +08:00
|
|
|
return sizes;
|
2013-07-17 06:18:39 +08:00
|
|
|
}
|
|
|
|
|
2013-09-13 11:46:46 +08:00
|
|
|
int FontFreeType::getHorizontalKerningForChars(unsigned short firstChar, unsigned short secondChar) const
|
2013-07-17 06:18:39 +08:00
|
|
|
{
|
|
|
|
// get the ID to the char we need
|
2013-09-13 11:46:46 +08:00
|
|
|
int glyphIndex1 = FT_Get_Char_Index(_fontRef, firstChar);
|
2013-07-12 06:43:45 +08:00
|
|
|
|
2013-09-13 11:46:46 +08:00
|
|
|
if (!glyphIndex1)
|
2013-07-18 08:31:28 +08:00
|
|
|
return 0;
|
2013-07-12 06:43:45 +08:00
|
|
|
|
2013-07-17 06:18:39 +08:00
|
|
|
// get the ID to the char we need
|
2013-09-13 11:46:46 +08:00
|
|
|
int glyphIndex2 = FT_Get_Char_Index(_fontRef, secondChar);
|
2013-07-12 06:43:45 +08:00
|
|
|
|
2013-09-13 11:46:46 +08:00
|
|
|
if (!glyphIndex2)
|
2013-07-18 08:31:28 +08:00
|
|
|
return 0;
|
2013-07-12 06:43:45 +08:00
|
|
|
|
2013-07-17 06:18:39 +08:00
|
|
|
FT_Vector kerning;
|
2013-07-12 06:43:45 +08:00
|
|
|
|
2013-09-13 11:46:46 +08:00
|
|
|
if (FT_Get_Kerning( _fontRef, glyphIndex1, glyphIndex2, FT_KERNING_DEFAULT, &kerning))
|
2013-07-18 08:31:28 +08:00
|
|
|
return 0;
|
2013-07-12 06:43:45 +08:00
|
|
|
|
2013-12-06 16:32:06 +08:00
|
|
|
return (static_cast<int>(kerning.x >> 6));
|
2013-07-12 06:43:45 +08:00
|
|
|
}
|
|
|
|
|
2013-09-13 11:46:46 +08:00
|
|
|
int FontFreeType::getFontMaxHeight() const
|
2013-07-20 01:33:26 +08:00
|
|
|
{
|
2013-12-06 16:32:06 +08:00
|
|
|
return (static_cast<int>(_fontRef->size->metrics.height >> 6));
|
2013-07-20 01:33:26 +08:00
|
|
|
}
|
|
|
|
|
2014-03-07 11:42:13 +08:00
|
|
|
int FontFreeType::getFontAscender() const
|
|
|
|
{
|
|
|
|
return (static_cast<int>(_fontRef->size->metrics.ascender >> 6));
|
|
|
|
}
|
|
|
|
|
2014-03-20 11:58:36 +08:00
|
|
|
unsigned char* FontFreeType::getGlyphBitmap(unsigned short theChar, long &outWidth, long &outHeight, Rect &outRect,int &xAdvance)
|
2013-07-12 06:43:45 +08:00
|
|
|
{
|
2014-03-05 15:54:40 +08:00
|
|
|
bool invalidChar = true;
|
|
|
|
unsigned char * ret = nullptr;
|
|
|
|
|
|
|
|
do
|
2014-02-21 18:09:52 +08:00
|
|
|
{
|
2014-03-05 15:54:40 +08:00
|
|
|
if (!_fontRef)
|
|
|
|
break;
|
|
|
|
|
|
|
|
auto glyphIndex = FT_Get_Char_Index(_fontRef, theChar);
|
|
|
|
if(!glyphIndex)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (_distanceFieldEnabled)
|
|
|
|
{
|
|
|
|
if (FT_Load_Glyph(_fontRef,glyphIndex,FT_LOAD_RENDER | FT_LOAD_NO_HINTING | FT_LOAD_NO_AUTOHINT))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (FT_Load_Glyph(_fontRef,glyphIndex,FT_LOAD_RENDER))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
outRect.origin.x = _fontRef->glyph->metrics.horiBearingX >> 6;
|
|
|
|
outRect.origin.y = - (_fontRef->glyph->metrics.horiBearingY >> 6);
|
|
|
|
outRect.size.width = (_fontRef->glyph->metrics.width >> 6);
|
|
|
|
outRect.size.height = (_fontRef->glyph->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)
|
|
|
|
{
|
|
|
|
auto copyBitmap = new unsigned char[outWidth * outHeight];
|
|
|
|
memcpy(copyBitmap,ret,outWidth * outHeight * sizeof(unsigned char));
|
|
|
|
|
2014-03-20 11:58:36 +08:00
|
|
|
long bitmapWidth;
|
|
|
|
long bitmapHeight;
|
2014-03-05 15:54:40 +08:00
|
|
|
FT_BBox bbox;
|
|
|
|
auto outlineBitmap = getGlyphBitmapWithOutline(theChar,bbox);
|
|
|
|
if(outlineBitmap == nullptr)
|
|
|
|
{
|
|
|
|
ret = nullptr;
|
|
|
|
delete [] copyBitmap;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
bitmapWidth = (bbox.xMax - bbox.xMin)>>6;
|
|
|
|
bitmapHeight = (bbox.yMax - bbox.yMin)>>6;
|
|
|
|
|
2014-03-20 11:58:36 +08:00
|
|
|
long index;
|
2014-03-05 15:54:40 +08:00
|
|
|
auto blendImage = new unsigned char[bitmapWidth * bitmapHeight * 2];
|
|
|
|
memset(blendImage, 0, bitmapWidth * bitmapHeight * 2);
|
|
|
|
for (int x = 0; x < bitmapWidth; ++x)
|
|
|
|
{
|
|
|
|
for (int y = 0; y < bitmapHeight; ++y)
|
|
|
|
{
|
|
|
|
index = x + ( y * bitmapWidth );
|
|
|
|
blendImage[2 * index] = outlineBitmap[index];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-20 11:58:36 +08:00
|
|
|
long maxX = outWidth + _outlineSize;
|
|
|
|
long maxY = outHeight + _outlineSize;
|
2014-03-05 15:54:40 +08:00
|
|
|
for (int x = _outlineSize; x < maxX; ++x)
|
|
|
|
{
|
|
|
|
for (int y = _outlineSize; y < maxY; ++y)
|
|
|
|
{
|
|
|
|
index = x + ( y * bitmapWidth );
|
|
|
|
|
|
|
|
blendImage[2 * index + 1] = copyBitmap[outWidth * (y - _outlineSize) + x - _outlineSize];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
outRect.origin.x = bbox.xMin >> 6;
|
|
|
|
outRect.origin.y = - (bbox.yMax >> 6);
|
|
|
|
|
|
|
|
xAdvance += bitmapWidth - outRect.size.width;
|
|
|
|
|
|
|
|
outRect.size.width = bitmapWidth;
|
|
|
|
outRect.size.height = bitmapHeight;
|
|
|
|
outWidth = bitmapWidth;
|
|
|
|
outHeight = bitmapHeight;
|
|
|
|
|
|
|
|
delete [] outlineBitmap;
|
|
|
|
delete [] copyBitmap;
|
|
|
|
ret = blendImage;
|
|
|
|
}
|
|
|
|
|
|
|
|
invalidChar = false;
|
|
|
|
} while (0);
|
|
|
|
|
|
|
|
if (invalidChar)
|
|
|
|
{
|
|
|
|
outRect.size.width = 0;
|
|
|
|
outRect.size.height = 0;
|
|
|
|
xAdvance = 0;
|
|
|
|
|
|
|
|
return nullptr;
|
2013-12-13 12:42:15 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-03-05 15:54:40 +08:00
|
|
|
return ret;
|
2013-12-13 12:42:15 +08:00
|
|
|
}
|
2014-03-05 15:54:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
unsigned char * FontFreeType::getGlyphBitmapWithOutline(unsigned short theChar, FT_BBox &bbox)
|
|
|
|
{
|
|
|
|
unsigned char* ret = nullptr;
|
|
|
|
|
|
|
|
FT_UInt gindex = FT_Get_Char_Index(_fontRef, theChar);
|
|
|
|
if (FT_Load_Glyph(_fontRef, gindex, 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;
|
2014-03-05 15:54:40 +08:00
|
|
|
|
|
|
|
FT_Bitmap bmp;
|
|
|
|
bmp.buffer = new 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;
|
2014-03-05 15:54:40 +08:00
|
|
|
bmp.pixel_mode = FT_PIXEL_MODE_GRAY;
|
|
|
|
bmp.num_grays = 256;
|
|
|
|
|
|
|
|
FT_Raster_Params params;
|
|
|
|
memset(¶ms, 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, ¶ms);
|
|
|
|
|
|
|
|
ret = bmp.buffer;
|
|
|
|
}
|
|
|
|
FT_Done_Glyph(glyph);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
2013-07-12 06:43:45 +08:00
|
|
|
}
|
|
|
|
|
2014-03-20 11:58:36 +08:00
|
|
|
unsigned char * makeDistanceMap( unsigned char *img, long width, long height)
|
2014-01-21 10:36:32 +08:00
|
|
|
{
|
2014-03-20 11:58:36 +08:00
|
|
|
long pixelAmount = (width + 2 * FontFreeType::DistanceMapSpread) * (height + 2 * FontFreeType::DistanceMapSpread);
|
2014-01-21 10:36:32 +08:00
|
|
|
|
|
|
|
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;
|
2014-01-21 10:36:32 +08:00
|
|
|
|
|
|
|
// 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;
|
2014-01-21 10:36:32 +08:00
|
|
|
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);
|
2014-01-21 10:36:32 +08:00
|
|
|
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);
|
2014-01-21 10:36:32 +08:00
|
|
|
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)
|
2014-01-21 10:36:32 +08:00
|
|
|
{
|
2014-03-05 15:54:40 +08:00
|
|
|
int iX = posX;
|
|
|
|
int iY = posY;
|
2014-01-21 10:36:32 +08:00
|
|
|
|
|
|
|
if (_distanceFieldEnabled)
|
|
|
|
{
|
2014-03-05 15:54:40 +08:00
|
|
|
auto distanceMap = makeDistanceMap(bitmap,bitmapWidth,bitmapHeight);
|
2014-01-21 10:36:32 +08:00
|
|
|
|
2014-03-05 15:54:40 +08:00
|
|
|
bitmapWidth += 2 * DistanceMapSpread;
|
|
|
|
bitmapHeight += 2 * DistanceMapSpread;
|
2014-01-21 10:36:32 +08:00
|
|
|
|
2014-03-20 11:58:36 +08:00
|
|
|
for (long y = 0; y < bitmapHeight; ++y)
|
2014-01-21 10:36:32 +08:00
|
|
|
{
|
2014-03-20 11:58:36 +08:00
|
|
|
long bitmap_y = y * bitmapWidth;
|
2014-01-21 10:36:32 +08:00
|
|
|
|
2014-04-09 20:41:09 +08:00
|
|
|
for (long x = 0; x < bitmapWidth; ++x)
|
2014-01-21 10:36:32 +08:00
|
|
|
{
|
|
|
|
/* Dual channel 16-bit output (more complicated, but good precision and range) */
|
|
|
|
/*int index = (iX + ( iY * destSize )) * 3;
|
|
|
|
int index2 = (bitmap_y + x)*3;
|
2014-03-05 15:54:40 +08:00
|
|
|
dest[index] = out[index2];
|
|
|
|
dest[index + 1] = out[index2 + 1];
|
|
|
|
dest[index + 2] = out[index2 + 2];*/
|
2014-01-21 10:36:32 +08:00
|
|
|
|
|
|
|
//Single channel 8-bit output
|
2014-03-05 15:54:40 +08:00
|
|
|
dest[iX + ( iY * FontAtlas::CacheTextureWidth )] = distanceMap[bitmap_y + x];
|
2014-01-21 10:36:32 +08:00
|
|
|
|
|
|
|
iX += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
iX = posX;
|
|
|
|
iY += 1;
|
|
|
|
}
|
2014-03-05 15:54:40 +08:00
|
|
|
free(distanceMap);
|
2014-01-21 10:36:32 +08:00
|
|
|
}
|
2014-03-05 15:54:40 +08:00
|
|
|
else if(_outlineSize > 0)
|
|
|
|
{
|
|
|
|
unsigned char tempChar;
|
2014-03-20 11:58:36 +08:00
|
|
|
for (long y = 0; y < bitmapHeight; ++y)
|
2014-03-05 15:54:40 +08:00
|
|
|
{
|
2014-03-20 11:58:36 +08:00
|
|
|
long bitmap_y = y * bitmapWidth;
|
2014-01-21 10:36:32 +08:00
|
|
|
|
2014-03-05 15:54:40 +08:00
|
|
|
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;
|
2014-01-21 10:36:32 +08:00
|
|
|
|
2014-03-05 15:54:40 +08:00
|
|
|
iX += 1;
|
|
|
|
}
|
2014-01-21 10:36:32 +08:00
|
|
|
|
2014-03-05 15:54:40 +08:00
|
|
|
iX = posX;
|
|
|
|
iY += 1;
|
|
|
|
}
|
|
|
|
delete [] bitmap;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-03-20 11:58:36 +08:00
|
|
|
for (long y = 0; y < bitmapHeight; ++y)
|
2014-01-21 10:36:32 +08:00
|
|
|
{
|
2014-03-20 11:58:36 +08:00
|
|
|
long bitmap_y = y * bitmapWidth;
|
2014-01-21 10:36:32 +08:00
|
|
|
|
2014-03-05 15:54:40 +08:00
|
|
|
for (int x = 0; x < bitmapWidth; ++x)
|
|
|
|
{
|
|
|
|
unsigned char cTemp = bitmap[bitmap_y + x];
|
2014-01-21 10:36:32 +08:00
|
|
|
|
2014-03-05 15:54:40 +08:00
|
|
|
// the final pixel
|
|
|
|
dest[(iX + ( iY * FontAtlas::CacheTextureWidth ) )] = cTemp;
|
2014-01-21 10:36:32 +08:00
|
|
|
|
2014-03-05 15:54:40 +08:00
|
|
|
iX += 1;
|
|
|
|
}
|
2014-01-21 10:36:32 +08:00
|
|
|
|
2014-03-05 15:54:40 +08:00
|
|
|
iX = posX;
|
|
|
|
iY += 1;
|
|
|
|
}
|
|
|
|
}
|
2014-01-21 10:36:32 +08:00
|
|
|
}
|
|
|
|
|
2013-07-12 06:43:45 +08:00
|
|
|
NS_CC_END
|