2013-07-26 08:58:13 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2013 Zynga Inc.
|
2014-01-07 11:25:07 +08:00
|
|
|
Copyright (c) 2013-2014 Chukong Technologies Inc.
|
2013-07-26 08:58:13 +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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include "CCFont.h"
|
2013-10-14 14:01:00 +08:00
|
|
|
#include "ccUTF8.h"
|
2013-07-26 08:58:13 +08:00
|
|
|
|
2013-08-02 05:36:34 +08:00
|
|
|
#include "CCFontFNT.h"
|
2013-08-15 20:59:05 +08:00
|
|
|
#include "CCFontFreeType.h"
|
2013-12-13 12:42:15 +08:00
|
|
|
#include "edtaa3func.h"
|
2013-08-02 05:36:34 +08:00
|
|
|
|
2013-07-26 08:58:13 +08:00
|
|
|
NS_CC_BEGIN
|
|
|
|
|
2013-12-13 12:42:15 +08:00
|
|
|
const int Font::DistanceMapSpread = 3;
|
|
|
|
|
2013-08-06 06:11:07 +08:00
|
|
|
const char * Font::_glyphASCII = "\"!#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~¡¢£¤¥¦§¨©ª«¬®¯°±²³´µ¶·¸¹º»¼½¾¿ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖ×ØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö÷øùúûüýþ ";
|
2013-08-02 05:36:34 +08:00
|
|
|
|
2013-08-06 06:11:07 +08:00
|
|
|
const char * Font::_glyphNEHE = "!\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~ ";
|
2013-08-02 05:36:34 +08:00
|
|
|
|
|
|
|
|
2013-12-13 12:42:15 +08:00
|
|
|
Font::Font() :
|
|
|
|
_usedGlyphs(GlyphCollection::ASCII)
|
|
|
|
, _customGlyphs(nullptr)
|
|
|
|
,_distanceFieldEnabled(false)
|
2013-08-06 06:11:07 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-09-13 11:46:46 +08:00
|
|
|
const char * Font::getGlyphCollection(GlyphCollection glyphs) const
|
2013-08-02 05:36:34 +08:00
|
|
|
{
|
|
|
|
switch (glyphs)
|
|
|
|
{
|
|
|
|
case GlyphCollection::NEHE:
|
2013-08-06 06:11:07 +08:00
|
|
|
return _glyphNEHE;
|
2013-08-02 05:36:34 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case GlyphCollection::ASCII:
|
2013-08-06 06:11:07 +08:00
|
|
|
return _glyphASCII;
|
2013-08-02 05:36:34 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-06 06:11:07 +08:00
|
|
|
void Font::setCurrentGlyphCollection(GlyphCollection glyphs, const char *customGlyphs)
|
2013-08-02 05:36:34 +08:00
|
|
|
{
|
2013-08-06 06:11:07 +08:00
|
|
|
if (_customGlyphs)
|
|
|
|
delete [] _customGlyphs;
|
2013-08-02 05:36:34 +08:00
|
|
|
|
2013-08-06 06:11:07 +08:00
|
|
|
switch (glyphs)
|
2013-08-02 05:36:34 +08:00
|
|
|
{
|
2013-08-06 06:11:07 +08:00
|
|
|
case GlyphCollection::NEHE:
|
|
|
|
_customGlyphs = 0;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GlyphCollection::ASCII:
|
|
|
|
_customGlyphs = 0;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2013-10-29 20:25:03 +08:00
|
|
|
if (customGlyphs)
|
|
|
|
{
|
2013-11-05 08:31:36 +08:00
|
|
|
size_t lenght = strlen(customGlyphs);
|
2013-10-29 20:25:03 +08:00
|
|
|
_customGlyphs = new char [lenght + 2];
|
|
|
|
memcpy(_customGlyphs, customGlyphs, lenght);
|
|
|
|
|
|
|
|
_customGlyphs[lenght] = 0;
|
|
|
|
_customGlyphs[lenght+1] = 0;
|
|
|
|
}
|
2013-08-06 06:11:07 +08:00
|
|
|
|
|
|
|
break;
|
2013-08-02 05:36:34 +08:00
|
|
|
}
|
2013-08-06 06:11:07 +08:00
|
|
|
}
|
|
|
|
|
2013-09-13 11:46:46 +08:00
|
|
|
const char * Font::getCurrentGlyphCollection() const
|
2013-08-06 06:11:07 +08:00
|
|
|
{
|
|
|
|
if (_customGlyphs)
|
2013-08-15 03:37:59 +08:00
|
|
|
{
|
2013-08-06 06:11:07 +08:00
|
|
|
return _customGlyphs;
|
2013-08-15 03:37:59 +08:00
|
|
|
}
|
2013-08-02 05:36:34 +08:00
|
|
|
else
|
|
|
|
{
|
2013-08-06 06:11:07 +08:00
|
|
|
return getGlyphCollection(_usedGlyphs);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-07 09:05:13 +08:00
|
|
|
Font* Font::createWithTTF(const std::string& fntName, int fontSize, GlyphCollection glyphs, const char *customGlyphs)
|
2013-08-06 06:11:07 +08:00
|
|
|
{
|
2013-08-07 04:43:29 +08:00
|
|
|
return FontFreeType::create(fntName, fontSize, glyphs, customGlyphs);
|
2013-08-02 05:36:34 +08:00
|
|
|
}
|
|
|
|
|
2013-11-07 09:05:13 +08:00
|
|
|
Font* Font::createWithFNT(const std::string& fntFilePath)
|
2013-08-02 05:36:34 +08:00
|
|
|
{
|
2013-08-07 04:43:29 +08:00
|
|
|
return FontFNT::create(fntFilePath);
|
2013-08-02 05:36:34 +08:00
|
|
|
}
|
|
|
|
|
2013-12-13 12:42:15 +08:00
|
|
|
unsigned char * Font::makeDistanceMap( unsigned char *img, unsigned int width, unsigned int height)
|
|
|
|
{
|
|
|
|
unsigned int pixelAmount = (width + 2 * DistanceMapSpread) * (height + 2 * 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) );
|
|
|
|
unsigned int i,j;
|
|
|
|
|
|
|
|
// Convert img into double (data) rescale image levels between 0 and 1
|
|
|
|
unsigned int outWidth = width + 2 * DistanceMapSpread;
|
|
|
|
for (i = 0; i < width; ++i)
|
|
|
|
{
|
|
|
|
for (j = 0; j < height; ++j)
|
|
|
|
{
|
|
|
|
data[j * outWidth + DistanceMapSpread + i] = img[j * width + i] / 255.0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
width += 2 * DistanceMapSpread;
|
|
|
|
height += 2 * DistanceMapSpread;
|
|
|
|
|
|
|
|
// Transform background (outside contour, in areas of 0's)
|
|
|
|
computegradient( data, width, height, gx, gy);
|
|
|
|
edtaa3(data, gx, gy, width, 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];
|
|
|
|
computegradient( data, width, height, gx, gy);
|
|
|
|
edtaa3(data, gx, gy, width, 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Font::setDistanceFieldEnabled(bool distanceFieldEnabled)
|
|
|
|
{
|
|
|
|
_distanceFieldEnabled = distanceFieldEnabled;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Font::renderCharAt(unsigned short int charToRender, int posX, int posY, unsigned char *destMemory, int destSize)
|
|
|
|
{
|
|
|
|
unsigned char *sourceBitmap = 0;
|
|
|
|
int sourceWidth = 0;
|
|
|
|
int sourceHeight = 0;
|
|
|
|
|
|
|
|
sourceBitmap = getGlyphBitmap(charToRender, sourceWidth, sourceHeight);
|
|
|
|
|
|
|
|
if (!sourceBitmap)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (_distanceFieldEnabled)
|
|
|
|
{
|
|
|
|
unsigned char * out = makeDistanceMap(sourceBitmap,sourceWidth,sourceHeight);
|
|
|
|
|
|
|
|
int iX = posX;
|
|
|
|
int iY = posY;
|
|
|
|
|
|
|
|
sourceWidth += 2 * DistanceMapSpread;
|
|
|
|
sourceHeight += 2 * DistanceMapSpread;
|
|
|
|
|
|
|
|
for (int y = 0; y < sourceHeight; ++y)
|
|
|
|
{
|
|
|
|
int bitmap_y = y * sourceWidth;
|
|
|
|
|
|
|
|
for (int x = 0; x < sourceWidth; ++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;
|
|
|
|
destMemory[index] = out[index2];
|
|
|
|
destMemory[index + 1] = out[index2 + 1];
|
|
|
|
destMemory[index + 2] = out[index2 + 2];*/
|
|
|
|
|
|
|
|
//Single channel 8-bit output
|
|
|
|
destMemory[iX + ( iY * destSize )] = out[bitmap_y + x];
|
|
|
|
|
|
|
|
iX += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
iX = posX;
|
|
|
|
iY += 1;
|
|
|
|
}
|
|
|
|
free(out);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
int iX = posX;
|
|
|
|
int iY = posY;
|
|
|
|
|
|
|
|
for (int y = 0; y < sourceHeight; ++y)
|
|
|
|
{
|
|
|
|
int bitmap_y = y * sourceWidth;
|
|
|
|
|
|
|
|
for (int x = 0; x < sourceWidth; ++x)
|
|
|
|
{
|
|
|
|
unsigned char cTemp = sourceBitmap[bitmap_y + x];
|
|
|
|
|
|
|
|
// the final pixel
|
|
|
|
destMemory[(iX + ( iY * destSize ) )] = cTemp;
|
|
|
|
|
|
|
|
iX += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
iX = posX;
|
|
|
|
iY += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
//everything good
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-09-13 11:46:46 +08:00
|
|
|
unsigned short int * Font::getUTF16Text(const char *text, int &outNumLetters) const
|
2013-07-26 08:58:13 +08:00
|
|
|
{
|
2013-09-13 11:46:46 +08:00
|
|
|
unsigned short* utf16String = cc_utf8_to_utf16(text);
|
2013-07-26 08:58:13 +08:00
|
|
|
|
|
|
|
if(!utf16String)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
outNumLetters = cc_wcslen(utf16String);
|
|
|
|
return utf16String;
|
|
|
|
}
|
|
|
|
|
2013-09-13 11:46:46 +08:00
|
|
|
int Font::getUTF16TextLenght(unsigned short int *text) const
|
2013-07-26 08:58:13 +08:00
|
|
|
{
|
2013-09-13 11:46:46 +08:00
|
|
|
return cc_wcslen(text);
|
2013-07-26 08:58:13 +08:00
|
|
|
}
|
|
|
|
|
2013-09-13 11:46:46 +08:00
|
|
|
unsigned short int * Font::trimUTF16Text(unsigned short int *text, int newBegin, int newEnd) const
|
2013-07-26 08:58:13 +08:00
|
|
|
{
|
2013-09-13 11:46:46 +08:00
|
|
|
if ( newBegin < 0 || newEnd <= 0 )
|
2013-07-26 08:58:13 +08:00
|
|
|
return 0;
|
|
|
|
|
2013-09-13 11:46:46 +08:00
|
|
|
if ( newBegin >= newEnd )
|
2013-07-26 08:58:13 +08:00
|
|
|
return 0;
|
|
|
|
|
2013-09-13 11:46:46 +08:00
|
|
|
if (newEnd >= cc_wcslen(text))
|
2013-07-26 08:58:13 +08:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
int newLenght = newEnd - newBegin + 2;
|
|
|
|
unsigned short* trimmedString = new unsigned short[newLenght];
|
|
|
|
|
2013-09-13 11:46:46 +08:00
|
|
|
for(int c = 0; c < (newLenght - 1); ++c)
|
2013-07-26 08:58:13 +08:00
|
|
|
{
|
2013-09-13 11:46:46 +08:00
|
|
|
trimmedString[c] = text[newBegin + c];
|
2013-07-26 08:58:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// last char
|
|
|
|
trimmedString[newLenght-1] = 0x0000;
|
|
|
|
|
|
|
|
// done
|
|
|
|
return trimmedString;
|
|
|
|
}
|
|
|
|
|
2013-09-13 11:46:46 +08:00
|
|
|
Rect Font::getRectForChar(unsigned short theChar) const
|
2013-07-26 08:58:13 +08:00
|
|
|
{
|
2013-09-13 11:46:46 +08:00
|
|
|
return Rect::ZERO;
|
2013-07-26 08:58:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_CC_END
|
|
|
|
|
|
|
|
|