axmol/cocos/2d/CCFontFreeType.cpp

387 lines
9.5 KiB
C++
Raw Normal View History

/****************************************************************************
Copyright (c) 2013 Zynga Inc.
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.
****************************************************************************/
#include <stdio.h>
#include <algorithm>
2013-08-07 05:37:19 +08:00
#include "ccUTF8.h"
#include "CCFontFreeType.h"
#include "CCTextImage.h"
2013-08-07 05:37:19 +08:00
#include "CCFont.h"
#include "CCFontDefinition.h"
NS_CC_BEGIN
2013-07-17 06:18:39 +08:00
FT_Library FontFreeType::_FTlibrary;
bool FontFreeType::_FTInitialized = false;
2013-08-07 04:43:29 +08:00
FontFreeType * FontFreeType::create(const std::string &fontName, int fontSize, GlyphCollection glyphs, const char *customGlyphs)
2013-08-06 06:56:18 +08:00
{
bool dynamicGlyphCollection = false;
if(glyphs == GlyphCollection::DYNAMIC)
dynamicGlyphCollection = true;
2013-08-07 04:43:29 +08:00
FontFreeType *tempFont = new FontFreeType(dynamicGlyphCollection);
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-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);
_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;
}
2013-11-13 11:22:34 +08:00
FontFreeType::FontFreeType(bool dynamicGlyphCollection)
: _fontRef(nullptr),
_letterPadding(5),
_ttfData(nullptr),
_dynamicGlyphCollection(dynamicGlyphCollection)
2013-07-17 06:18:39 +08:00
{
}
bool FontFreeType::createFontObject(const std::string &fontName, int fontSize)
{
FT_Face face;
2013-12-05 17:19:01 +08:00
ssize_t len = 0;
_ttfData = FileUtils::getInstance()->getFileData(fontName.c_str(), "rb", &len);
if (!_ttfData)
2013-07-17 06:18:39 +08:00
return false;
2013-07-17 06:18:39 +08:00
// create the face from the data
if (FT_New_Memory_Face(getFTLibrary(), _ttfData, len, 0, &face ))
2013-07-17 06:18:39 +08:00
return false;
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))
return false;
2013-07-17 06:18:39 +08:00
// set the requested font size
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-17 06:18:39 +08:00
// store the face globally
_fontRef = face;
// save font name locally
_fontName = fontName;
2013-07-17 06:18:39 +08:00
// done and good
return true;
}
FontFreeType::~FontFreeType()
{
if (_fontRef)
{
FT_Done_Face(_fontRef);
}
if (_ttfData)
{
free(_ttfData);
_ttfData = nullptr;
}
2013-07-17 06:18:39 +08:00
}
FontAtlas * FontFreeType::createFontAtlas()
{
if (_dynamicGlyphCollection)
{
FontAtlas *atlas = new FontAtlas(*this);
this->release();
return atlas;
}
else
{
FontDefinitionTTF *def = FontDefinitionTTF::create(this);
if (!def)
return nullptr;
FontAtlas *atlas = def->createFontAtlas();
return atlas;
}
}
2013-09-13 11:46:46 +08:00
bool FontFreeType::getBBOXFotChar(unsigned short theChar, Rect &outRect) const
2013-07-17 06:18:39 +08:00
{
if (!_fontRef)
return false;
2013-07-17 06:18:39 +08:00
// get the ID to the char we need
int glyph_index = FT_Get_Char_Index(_fontRef, theChar);
2013-07-17 06:18:39 +08:00
if (!glyph_index)
return false;
2013-07-17 06:18:39 +08:00
// load glyph infos
if (FT_Load_Glyph(_fontRef, glyph_index, FT_LOAD_DEFAULT))
return false;
2013-07-17 06:18:39 +08:00
// store result in the passed rectangle
outRect.origin.x = 0;
outRect.origin.y = - (_fontRef->glyph->metrics.horiBearingY >> 6);
outRect.size.width = (_fontRef->glyph->metrics.width >> 6);
outRect.size.height = (_fontRef->glyph->metrics.height >> 6);
2013-07-17 06:18:39 +08:00
return true;
}
2013-09-13 11:46:46 +08:00
GlyphDef * FontFreeType::getGlyphDefintionsForText(const char *text, int &outNumGlyphs, bool UTF16text) const
{
unsigned short* utf16String = 0;
if (UTF16text)
{
2013-09-13 11:46:46 +08:00
utf16String = (unsigned short*) text;
}
else
{
2013-09-13 11:46:46 +08:00
utf16String = cc_utf8_to_utf16(text);
}
2013-07-17 06:18:39 +08:00
//
if (!utf16String)
return 0;
2013-07-17 06:18:39 +08:00
int numChar = cc_wcslen(utf16String);
if (!numChar)
return 0;
// allocate the needed Glyphs
2013-09-13 11:46:46 +08:00
GlyphDef *glyphs = new GlyphDef[numChar];
assert(glyphs != nullptr);
if (!glyphs)
return 0;
// sore result as CCRect
2013-09-13 11:46:46 +08:00
for (int c = 0; c < numChar; ++c)
{
Rect tempRect;
2013-09-13 11:46:46 +08:00
if (!getBBOXFotChar(utf16String[c], tempRect))
2013-07-17 06:18:39 +08:00
{
2013-08-01 03:11:56 +08:00
log("Warning: Cannot find definition for glyph: %c in font:%s", utf16String[c], _fontName.c_str());
tempRect.origin.x = 0;
tempRect.origin.y = 0;
tempRect.size.width = 0;
tempRect.size.height = 0;
2013-09-13 11:46:46 +08:00
glyphs[c].setRect(tempRect);
glyphs[c].setUTF16Letter(utf16String[c]);
glyphs[c].setValid(false);
glyphs[c].setPadding(_letterPadding);
}
else
{
2013-09-13 11:46:46 +08:00
glyphs[c].setRect(tempRect);
glyphs[c].setUTF16Letter(utf16String[c]);
glyphs[c].setPadding(_letterPadding);
glyphs[c].setValid(true);
2013-07-17 06:18:39 +08:00
}
}
2013-07-17 06:18:39 +08:00
outNumGlyphs = numChar;
2013-07-17 06:18:39 +08:00
// free memory
if (!UTF16text)
delete [] utf16String;
2013-07-17 06:18:39 +08:00
// done
2013-09-13 11:46:46 +08:00
return glyphs;
}
2013-09-13 11:46:46 +08:00
Size * FontFreeType::getAdvancesForTextUTF16(unsigned short *text, int &outNumLetters) const
{
2013-09-13 11:46:46 +08:00
if (!text)
return 0;
2013-09-13 11:46:46 +08:00
outNumLetters = cc_wcslen(text);
if (!outNumLetters)
return 0;
2013-09-13 11:46:46 +08:00
Size *sizes = new Size[outNumLetters];
if (!sizes)
2013-07-17 06:18:39 +08:00
return 0;
2013-09-13 11:46:46 +08:00
for (int c = 0; c < outNumLetters; ++c)
{
2013-07-17 06:18:39 +08:00
int advance = 0;
int kerning = 0;
2013-09-13 11:46:46 +08:00
advance = getAdvanceForChar(text[c]) - getBearingXForChar(text[c]);
2013-07-17 06:18:39 +08:00
2013-09-13 11:46:46 +08:00
if (c < (outNumLetters-1))
kerning = getHorizontalKerningForChars(text[c], text[c+1]);
2013-07-17 06:18:39 +08:00
2013-09-13 11:46:46 +08:00
sizes[c].width = (advance + kerning);
}
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::getAdvanceForChar(unsigned short theChar) const
2013-07-17 06:18:39 +08:00
{
if (!_fontRef)
return 0;
2013-07-17 06:18:39 +08:00
// get the ID to the char we need
int glyph_index = FT_Get_Char_Index(_fontRef, theChar);
2013-07-17 06:18:39 +08:00
if (!glyph_index)
return 0;
2013-07-17 06:18:39 +08:00
// load glyph infos
if (FT_Load_Glyph(_fontRef, glyph_index, FT_LOAD_DEFAULT))
return 0;
2013-07-17 06:18:39 +08:00
// get to the advance for this glyph
2013-12-06 16:32:06 +08:00
return (static_cast<int>(_fontRef->glyph->advance.x >> 6));
}
2013-09-13 11:46:46 +08:00
int FontFreeType::getBearingXForChar(unsigned short theChar) const
{
if (!_fontRef)
return 0;
// get the ID to the char we need
2013-09-13 11:46:46 +08:00
int glyphIndex = FT_Get_Char_Index(_fontRef, theChar);
2013-09-13 11:46:46 +08:00
if (!glyphIndex)
return 0;
// load glyph infos
2013-09-13 11:46:46 +08:00
if (FT_Load_Glyph(_fontRef, glyphIndex, FT_LOAD_DEFAULT))
return 0;
2013-12-06 16:32:06 +08:00
return (static_cast<int>(_fontRef->glyph->metrics.horiBearingX >>6));
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
{
if (!_fontRef)
return 0;
bool hasKerning = FT_HAS_KERNING( _fontRef ) != 0;
if (!hasKerning)
return 0;
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-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
2013-09-13 11:46:46 +08:00
int glyphIndex2 = FT_Get_Char_Index(_fontRef, 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));
}
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
}
2013-09-13 11:46:46 +08:00
unsigned char * FontFreeType::getGlyphBitmap(unsigned short theChar, int &outWidth, int &outHeight) const
{
2013-07-17 06:18:39 +08:00
if (!_fontRef)
return 0;
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 glyphIndex = FT_Get_Char_Index(_fontRef, theChar);
2013-09-13 11:46:46 +08:00
if (!glyphIndex)
return 0;
2013-07-17 06:18:39 +08:00
// load glyph infos
2013-09-13 11:46:46 +08:00
if (FT_Load_Glyph(_fontRef, glyphIndex, FT_LOAD_DEFAULT))
2013-07-17 06:18:39 +08:00
return 0;
2013-07-17 06:18:39 +08:00
if (FT_Render_Glyph( _fontRef->glyph, FT_RENDER_MODE_NORMAL ))
return 0;
2013-07-17 06:18:39 +08:00
outWidth = _fontRef->glyph->bitmap.width;
outHeight = _fontRef->glyph->bitmap.rows;
2013-07-17 06:18:39 +08:00
// return the pointer to the bitmap
return _fontRef->glyph->bitmap.buffer;
}
2013-09-13 11:46:46 +08:00
int FontFreeType::getLetterPadding() const
{
return _letterPadding;
}
NS_CC_END