axmol/cocos2dx/platform/Linux/CCImage_Linux.cpp

345 lines
8.0 KiB
C++
Raw Normal View History

#include <string.h>
#include <vector>
#include <string>
#include <sstream>
#include "CCFileUtils.h"
#include "CCPlatformMacros.h"
#include "CCImage.h"
#include "ft2build.h"
#include "CCStdC.h"
#include FT_FREETYPE_H
#define szFont_kenning 2
#define SHIFT6(num) (num>>6)
using namespace std;
struct TextLine {
string sLineStr;
int iLineWidth;
};
NS_CC_BEGIN;
class BitmapDC
{
public:
BitmapDC() {
libError = FT_Init_FreeType( &library );
iInterval = szFont_kenning;
m_pData = NULL;
reset();
}
~BitmapDC() {
FT_Done_FreeType(library);
//data will be deleted by CCImage
// if (m_pData) {
// delete m_pData;
// }
}
void reset() {
iMaxLineWidth = 0;
iMaxLineHeight = 0;
vLines.clear();
}
void buildLine(stringstream& ss, FT_Face face, int iCurXCursor, char cLastChar) {
TextLine oTempLine;
ss << '\0';
oTempLine.sLineStr = ss.str();
//get last glyph
FT_Load_Glyph(face, FT_Get_Char_Index(face, cLastChar),
FT_LOAD_DEFAULT);
oTempLine.iLineWidth = iCurXCursor - SHIFT6(face->glyph->metrics.horiAdvance+face->glyph->metrics.horiBearingX-face->glyph->metrics.width)/*-iInterval*/;//TODO interval
iMaxLineWidth = MAX(iMaxLineWidth, oTempLine.iLineWidth);
ss.clear();
ss.str("");
vLines.push_back(oTempLine);
}
bool divideString(FT_Face face, const char* sText, int iMaxWidth, int iMaxHeight) {
const char* pText = sText;
int iError = 0;
int iCurXCursor;
iError = FT_Load_Glyph(face, FT_Get_Char_Index(face, *pText),
FT_LOAD_DEFAULT);
if (iError) {
return false;
}
iCurXCursor = -SHIFT6(face->glyph->metrics.horiBearingX);
//init stringstream
stringstream ss;
int cLastCh = 0;
while (*pText != '\0') {
if (*pText == '\n') {
buildLine(ss, face, iCurXCursor, cLastCh);
pText++;
iError = FT_Load_Glyph(face, FT_Get_Char_Index(face, *pText),
FT_LOAD_DEFAULT);
if (iError) {
return false;
}
iCurXCursor = -SHIFT6(face->glyph->metrics.horiBearingX);
continue;
}
iError = FT_Load_Glyph(face, FT_Get_Char_Index(face, *pText),
FT_LOAD_DEFAULT);
if (iError) {
return false;
//break;
}
//check its width
//divide it when exceeding
if ((iMaxWidth > 0
&& iCurXCursor + SHIFT6(face->glyph->metrics.width)
> iMaxWidth)) {
buildLine(ss, face , iCurXCursor, cLastCh);
iCurXCursor = -SHIFT6(face->glyph->metrics.horiBearingX);
}
cLastCh = *pText;
ss << *pText;
iCurXCursor += SHIFT6(face->glyph->metrics.horiAdvance) + iInterval;
pText++;
}
if (iError) {
return false;
}
buildLine(ss,face, iCurXCursor, cLastCh);
return true;
}
/**
* compute the start pos of every line
*
* return >0 represent the start x pos of the line
* while -1 means fail
*
*/
int computeLineStart(FT_Face face, CCImage::ETextAlign eAlignMask, char cText,
int iLineIndex) {
int iRet;
int iError = FT_Load_Glyph(face, FT_Get_Char_Index(face, cText),
FT_LOAD_DEFAULT);
if (iError) {
return -1;
}
if (eAlignMask == CCImage::kAlignCenter) {
iRet = (iMaxLineWidth - vLines[iLineIndex].iLineWidth) / 2
- SHIFT6(face->glyph->metrics.horiBearingX );
} else if (eAlignMask == CCImage::kAlignRight) {
iRet = (iMaxLineWidth - vLines[iLineIndex].iLineWidth)
- SHIFT6(face->glyph->metrics.horiBearingX );
} else {
// left or other situation
iRet = -SHIFT6(face->glyph->metrics.horiBearingX );
}
return iRet;
}
bool getBitmap(const char *text, int nWidth, int nHeight, CCImage::ETextAlign eAlignMask, const char * pFontName, float fontSize) {
FT_Face face;
FT_Error iError;
const char* pText = text;
//data will be deleted by CCImage
// if (m_pData) {
// delete m_pData;
// }
int iCurXCursor, iCurYCursor;
bool bRet = false;
if (libError) {
return false;
}
do {
iError = FT_New_Face( library, pFontName, 0, &face );
if (iError) {
//no valid font found use default
modified: cocos2dx/.cproject modified: cocos2dx/CCConfiguration.cpp modified: cocos2dx/CCConfiguration.h modified: cocos2dx/actions/CCActionGrid3D.cpp modified: cocos2dx/platform/Linux/CCAplication_linux.cpp modified: cocos2dx/platform/Linux/CCAplication_linux.h modified: cocos2dx/platform/Linux/CCImage_Linux.cpp modified: mycreate-android-project.sh new file: post.sh modified: template/android/mycopy_files.sh modified: tests/AppDelegate.cpp new file: tests/test.linux/Res deleted: tests/test.linux/Res/Hello.png deleted: tests/test.linux/Res/Images/BoilingFoam.plist deleted: tests/test.linux/Res/Images/BurstPipe.plist deleted: tests/test.linux/Res/Images/Comet.plist deleted: tests/test.linux/Res/Images/Comet.png deleted: tests/test.linux/Res/Images/ExplodingRing.plist deleted: tests/test.linux/Res/Images/Flower.plist deleted: tests/test.linux/Res/Images/Fog.png deleted: tests/test.linux/Res/Images/Galaxy.plist deleted: tests/test.linux/Res/Images/HelloWorld.png deleted: tests/test.linux/Res/Images/Icon.png deleted: tests/test.linux/Res/Images/LavaFlow.plist deleted: tests/test.linux/Res/Images/Pea.png deleted: tests/test.linux/Res/Images/Phoenix.plist deleted: tests/test.linux/Res/Images/PlanetCute-1024x1024.png deleted: tests/test.linux/Res/Images/SendScoreButton.png deleted: tests/test.linux/Res/Images/SendScoreButtonPressed.png deleted: tests/test.linux/Res/Images/SpinningPeas.plist deleted: tests/test.linux/Res/Images/SpinningPeas.png deleted: tests/test.linux/Res/Images/Spiral.plist deleted: tests/test.linux/Res/Images/SpookyPeas.plist deleted: tests/test.linux/Res/Images/SpookyPeas.png deleted: tests/test.linux/Res/Images/Upsidedown.plist deleted: tests/test.linux/Res/Images/atlastest.png deleted: tests/test.linux/Res/Images/b1-hd.png deleted: tests/test.linux/Res/Images/b1.png deleted: tests/test.linux/Res/Images/b2-hd.png deleted: tests/test.linux/Res/Images/b2.png deleted: tests/test.linux/Res/Images/background.png deleted: tests/test.linux/Res/Images/background1-hd.png deleted: tests/test.linux/Res/Images/background1.png deleted: tests/test.linux/Res/Images/background2-hd.png deleted: tests/test.linux/Res/Images/background2.png deleted: tests/test.linux/Res/Images/background3-hd.png deleted: tests/test.linux/Res/Images/background3.png deleted: tests/test.linux/Res/Images/ball-hd.png deleted: tests/test.linux/Res/Images/ball.png deleted: tests/test.linux/Res/Images/bitmapFontTest3.fnt deleted: tests/test.linux/Res/Images/bitmapFontTest3.png deleted: tests/test.linux/Res/Images/blocks-hd.png deleted: tests/test.linux/Res/Images/blocks.png deleted: tests/test.linux/Res/Images/btn-about-normal.png deleted: tests/test.linux/Res/Images/btn-about-selected.png deleted: tests/test.linux/Res/Images/btn-highscores-normal.png deleted: tests/test.linux/Res/Images/btn-highscores-selected.png deleted: tests/test.linux/Res/Images/btn-play-normal.png deleted: tests/test.linux/Res/Images/btn-play-selected.png deleted: tests/test.linux/Res/Images/bugs/RetinaDisplay.jpg deleted: tests/test.linux/Res/Images/bugs/bug886.jpg deleted: tests/test.linux/Res/Images/bugs/bug886.png deleted: tests/test.linux/Res/Images/bugs/circle-hd.plist deleted: tests/test.linux/Res/Images/bugs/circle-hd.png deleted: tests/test.linux/Res/Images/bugs/circle.plist deleted: tests/test.linux/Res/Images/bugs/circle.png deleted: tests/test.linux/Res/Images/bugs/corner.png deleted: tests/test.linux/Res/Images/bugs/edge.png deleted: tests/test.linux/Res/Images/bugs/fill.png deleted: tests/test.linux/Res/Images/bugs/picture-hd.png deleted: tests/test.linux/Res/Images/bugs/picture.png deleted: tests/test.linux/Res/Images/close-hd.png deleted: tests/test.linux/Res/Images/close.png deleted: tests/test.linux/Res/Images/debian.plist deleted: tests/test.linux/Res/Images/f1-hd.png deleted: tests/test.linux/Res/Images/f1.png deleted: tests/test.linux/Res/Images/f2-hd.png deleted: tests/test.linux/Res/Images/f2.png deleted: tests/test.linux/Res/Images/fire-grayscale.png deleted: tests/test.linux/Res/Images/fire.png deleted: tests/test.linux/Res/Images/fps_images.png deleted: tests/test.linux/Res/Images/grossini-hd.png deleted: tests/test.linux/Res/Images/grossini.png deleted: tests/test.linux/Res/Images/grossini_128x256_mipmap.pvr deleted: tests/test.linux/Res/Images/grossini_dance_01.png deleted: tests/test.linux/Res/Images/grossini_dance_02.png deleted: tests/test.linux/Res/Images/grossini_dance_03.png deleted: tests/test.linux/Res/Images/grossini_dance_04.png deleted: tests/test.linux/Res/Images/grossini_dance_05.png deleted: tests/test.linux/Res/Images/grossini_dance_06.png deleted: tests/test.linux/Res/Images/grossini_dance_07.png deleted: tests/test.linux/Res/Images/grossini_dance_08.png deleted: tests/test.linux/Res/Images/grossini_dance_09.png deleted: tests/test.linux/Res/Images/grossini_dance_10.png deleted: tests/test.linux/Res/Images/grossini_dance_11.png deleted: tests/test.linux/Res/Images/grossini_dance_12.png deleted: tests/test.linux/Res/Images/grossini_dance_13.png deleted: tests/test.linux/Res/Images/grossini_dance_14.png deleted: tests/test.linux/Res/Images/grossini_dance_atlas-hd.png deleted: tests/test.linux/Res/Images/grossini_dance_atlas-mono.png deleted: tests/test.linux/Res/Images/grossini_dance_atlas.png deleted: tests/test.linux/Res/Images/grossini_dance_atlas_nomipmap.png deleted: tests/test.linux/Res/Images/grossini_pvr_rgba4444.pvr deleted: tests/test.linux/Res/Images/grossini_pvr_rgba8888.pvr deleted: tests/test.linux/Res/Images/grossinis_sister1-hd.png deleted: tests/test.linux/Res/Images/grossinis_sister1-testalpha.png deleted: tests/test.linux/Res/Images/grossinis_sister1-testalpha.ppng deleted: tests/test.linux/Res/Images/grossinis_sister1.png deleted: tests/test.linux/Res/Images/grossinis_sister2-hd.png deleted: tests/test.linux/Res/Images/grossinis_sister2.png deleted: tests/test.linux/Res/Images/landscape-1024x1024.png deleted: tests/test.linux/Res/Images/logo-mipmap.pvr deleted: tests/test.linux/Res/Images/logo-nomipmap.pvr deleted: tests/test.linux/Res/Images/menuitemsprite.png deleted: tests/test.linux/Res/Images/paddle-hd.png deleted: tests/test.linux/Res/Images/paddle.png deleted: tests/test.linux/Res/Images/particles-hd.png deleted: tests/test.linux/Res/Images/particles.png deleted: tests/test.linux/Res/Images/pattern1.png deleted: tests/test.linux/Res/Images/piece.png deleted: tests/test.linux/Res/Images/powered.png deleted: tests/test.linux/Res/Images/r1-hd.png deleted: tests/test.linux/Res/Images/r1.png deleted: tests/test.linux/Res/Images/r2-hd.png deleted: tests/test.linux/Res/Images/r2.png deleted: tests/test.linux/Res/Images/snow.png deleted: tests/test.linux/Res/Images/sprites_test/sprite-0-0.png deleted: tests/test.linux/Res/Images/sprites_test/sprite-0-1.png deleted: tests/test.linux/Res/Images/sprites_test/sprite-0-2.png deleted: tests/test.linux/Res/Images/sprites_test/sprite-0-3.png deleted: tests/test.linux/Res/Images/sprites_test/sprite-0-4.png deleted: tests/test.linux/Res/Images/sprites_test/sprite-0-5.png deleted: tests/test.linux/Res/Images/sprites_test/sprite-0-6.png deleted: tests/test.linux/Res/Images/sprites_test/sprite-0-7.png deleted: tests/test.linux/Res/Images/sprites_test/sprite-1-0.png deleted: tests/test.linux/Res/Images/sprites_test/sprite-1-1.png deleted: tests/test.linux/Res/Images/sprites_test/sprite-1-2.png deleted: tests/test.linux/Res/Images/sprites_test/sprite-1-3.png deleted: tests/test.linux/Res/Images/sprites_test/sprite-1-4.png deleted: tests/test.linux/Res/Images/sprites_test/sprite-1-5.png deleted: tests/test.linux/Res/Images/sprites_test/sprite-1-6.png deleted: tests/test.linux/Res/Images/sprites_test/sprite-1-7.png deleted: tests/test.linux/Res/Images/sprites_test/sprite-2-0.png deleted: tests/test.linux/Res/Images/sprites_test/sprite-2-1.png deleted: tests/test.linux/Res/Images/sprites_test/sprite-2-2.png deleted: tests/test.linux/Res/Images/sprites_test/sprite-2-3.png deleted: tests/test.linux/Res/Images/sprites_test/sprite-2-4.png deleted: tests/test.linux/Res/Images/sprites_test/sprite-2-5.png deleted: tests/test.linux/Res/Images/sprites_test/sprite-2-6.png deleted: tests/test.linux/Res/Images/sprites_test/sprite-2-7.png deleted: tests/test.linux/Res/Images/sprites_test/sprite-3-0.png deleted: tests/test.linux/Res/Images/sprites_test/sprite-3-1.png deleted: tests/test.linux/Res/Images/sprites_test/sprite-3-2.png deleted: tests/test.linux/Res/Images/sprites_test/sprite-3-3.png deleted: tests/test.linux/Res/Images/sprites_test/sprite-3-4.png deleted: tests/test.linux/Res/Images/sprites_test/sprite-3-5.png deleted: tests/test.linux/Res/Images/sprites_test/sprite-3-6.png deleted: tests/test.linux/Res/Images/sprites_test/sprite-3-7.png deleted: tests/test.linux/Res/Images/sprites_test/sprite-4-0.png deleted: tests/test.linux/Res/Images/sprites_test/sprite-4-1.png deleted: tests/test.linux/Res/Images/sprites_test/sprite-4-2.png deleted: tests/test.linux/Res/Images/sprites_test/sprite-4-3.png deleted: tests/test.linux/Res/Images/sprites_test/sprite-4-4.png deleted: tests/test.linux/Res/Images/sprites_test/sprite-4-5.png deleted: tests/test.linux/Res/Images/sprites_test/sprite-4-6.png deleted: tests/test.linux/Res/Images/sprites_test/sprite-4-7.png deleted: tests/test.linux/Res/Images/sprites_test/sprite-5-0.png deleted: tests/test.linux/Res/Images/sprites_test/sprite-5-1.png deleted: tests/test.linux/Res/Images/sprites_test/sprite-5-2.png deleted: tests/test.linux/Res/Images/sprites_test/sprite-5-3.png deleted: tests/test.linux/Res/Images/sprites_test/sprite-5-4.png deleted: tests/test.linux/Res/Images/sprites_test/sprite-5-5.png deleted: tests/test.linux/Res/Images/sprites_test/sprite-5-6.png deleted: tests/test.linux/Res/Images/sprites_test/sprite-5-7.png deleted: tests/test.linux/Res/Images/sprites_test/sprite-6-0.png deleted: tests/test.linux/Res/Images/sprites_test/sprite-6-1.png deleted: tests/test.linux/Res/Images/sprites_test/sprite-6-2.png deleted: tests/test.linux/Res/Images/sprites_test/sprite-6-3.png deleted: tests/test.linux/Res/Images/sprites_test/sprite-6-4.png deleted: tests/test.linux/Res/Images/sprites_test/sprite-6-5.png deleted: tests/test.linux/Res/Images/sprites_test/sprite-6-6.png deleted: tests/test.linux/Res/Images/sprites_test/sprite-6-7.png deleted: tests/test.linux/Res/Images/sprites_test/sprite-7-0.png deleted: tests/test.linux/Res/Images/sprites_test/sprite-7-1.png deleted: tests/test.linux/Res/Images/sprites_test/sprite-7-2.png deleted: tests/test.linux/Res/Images/sprites_test/sprite-7-3.png deleted: tests/test.linux/Res/Images/sprites_test/sprite-7-4.png deleted: tests/test.linux/Res/Images/sprites_test/sprite-7-5.png deleted: tests/test.linux/Res/Images/sprites_test/sprite-7-6.png deleted: tests/test.linux/Res/Images/sprites_test/sprite-7-7.png deleted: tests/test.linux/Res/Images/spritesheet1.png deleted: tests/test.linux/Res/Images/stars-grayscale.png deleted: tests/test.linux/Res/Images/stars.png deleted: tests/test.linux/Res/Images/stars2-grayscale.png deleted: tests/test.linux/Res/Images/stars2.png deleted: tests/test.linux/Res/Images/streak.png deleted: tests/test.linux/Res/Images/test-rgba1.png deleted: tests/test.linux/Res/Images/test_blend.png deleted: tests/test.linux/Res/Images/test_image-bad_encoding.pvr deleted: tests/test.linux/Res/Images/test_image.jpeg deleted: tests/test.linux/Res/Images/test_image.png deleted: tests/test.linux/Res/Images/test_image.pvr deleted: tests/test.linux/Res/Images/test_image.pvrraw deleted: tests/test.linux/Res/Images/test_image_a8.pvr deleted: tests/test.linux/Res/Images/test_image_ai88.pvr deleted: tests/test.linux/Res/Images/test_image_bgra8888.pvr deleted: tests/test.linux/Res/Images/test_image_i8.pvr deleted: tests/test.linux/Res/Images/test_image_pvrtc2bpp.pvr deleted: tests/test.linux/Res/Images/test_image_pvrtc4bpp.pvr deleted: tests/test.linux/Res/Images/test_image_rgb565.pvr deleted: tests/test.linux/Res/Images/test_image_rgba4444.pvr deleted: tests/test.linux/Res/Images/test_image_rgba4444.pvr.ccz deleted: tests/test.linux/Res/Images/test_image_rgba4444.pvr.gz deleted: tests/test.linux/Res/Images/test_image_rgba4444_mipmap.pvr deleted: tests/test.linux/Res/Images/test_image_rgba5551.pvr deleted: tests/test.linux/Res/Images/test_image_rgba8888.pvr deleted: tests/test.linux/Res/Images/texture1024x1024.png deleted: tests/test.linux/Res/Images/texture2048x2048.png deleted: tests/test.linux/Res/Images/texture512x512.png deleted: tests/test.linux/Res/TileMaps/fixed-ortho-test2.png deleted: tests/test.linux/Res/TileMaps/hexa-test.tmx deleted: tests/test.linux/Res/TileMaps/hexa-tiles.png deleted: tests/test.linux/Res/TileMaps/iso-test-bug787.tmx deleted: tests/test.linux/Res/TileMaps/iso-test-movelayer.tmx deleted: tests/test.linux/Res/TileMaps/iso-test-objectgroup.tmx deleted: tests/test.linux/Res/TileMaps/iso-test-vertexz.tmx deleted: tests/test.linux/Res/TileMaps/iso-test-zorder.tmx deleted: tests/test.linux/Res/TileMaps/iso-test.png deleted: tests/test.linux/Res/TileMaps/iso-test.tmx deleted: tests/test.linux/Res/TileMaps/iso-test1.tmx deleted: tests/test.linux/Res/TileMaps/iso-test2-uncompressed.tmx deleted: tests/test.linux/Res/TileMaps/iso-test2.png deleted: tests/test.linux/Res/TileMaps/iso-test2.tmx deleted: tests/test.linux/Res/TileMaps/iso.png deleted: tests/test.linux/Res/TileMaps/levelmap.tga deleted: tests/test.linux/Res/TileMaps/ortho-objects.tmx deleted: tests/test.linux/Res/TileMaps/ortho-test1.png deleted: tests/test.linux/Res/TileMaps/ortho-test1_bw.png deleted: tests/test.linux/Res/TileMaps/ortho-test2.png deleted: tests/test.linux/Res/TileMaps/orthogonal-test-movelayer.tmx deleted: tests/test.linux/Res/TileMaps/orthogonal-test-vertexz.tmx deleted: tests/test.linux/Res/TileMaps/orthogonal-test-zorder.tmx deleted: tests/test.linux/Res/TileMaps/orthogonal-test1.tmx deleted: tests/test.linux/Res/TileMaps/orthogonal-test1.tsx deleted: tests/test.linux/Res/TileMaps/orthogonal-test2.tmx deleted: tests/test.linux/Res/TileMaps/orthogonal-test3.tmx deleted: tests/test.linux/Res/TileMaps/orthogonal-test4-hd.tmx deleted: tests/test.linux/Res/TileMaps/orthogonal-test4.tmx deleted: tests/test.linux/Res/TileMaps/orthogonal-test5.tmx deleted: tests/test.linux/Res/TileMaps/orthogonal-test6-hd.tmx deleted: tests/test.linux/Res/TileMaps/orthogonal-test6.tmx deleted: tests/test.linux/Res/TileMaps/test-object-layer.tmx deleted: tests/test.linux/Res/TileMaps/tiles-hd.png deleted: tests/test.linux/Res/TileMaps/tiles.png deleted: tests/test.linux/Res/TileMaps/tmw_desert_spacing-hd.png deleted: tests/test.linux/Res/TileMaps/tmw_desert_spacing.png deleted: tests/test.linux/Res/animations/dragon_animation-hd.png deleted: tests/test.linux/Res/animations/dragon_animation.png deleted: tests/test.linux/Res/animations/ghosts.plist deleted: tests/test.linux/Res/animations/ghosts.png deleted: tests/test.linux/Res/animations/grossini-aliases.plist deleted: tests/test.linux/Res/animations/grossini-aliases.png deleted: tests/test.linux/Res/animations/grossini.plist deleted: tests/test.linux/Res/animations/grossini.plist.xml deleted: tests/test.linux/Res/animations/grossini.png deleted: tests/test.linux/Res/animations/grossini.ztp deleted: tests/test.linux/Res/animations/grossini_blue.plist deleted: tests/test.linux/Res/animations/grossini_blue.png deleted: tests/test.linux/Res/animations/grossini_family.plist deleted: tests/test.linux/Res/animations/grossini_family.png deleted: tests/test.linux/Res/animations/grossini_gray.plist deleted: tests/test.linux/Res/animations/grossini_gray.png deleted: tests/test.linux/Res/app.config.txt deleted: tests/test.linux/Res/background.mp3 deleted: tests/test.linux/Res/effect1.wav deleted: tests/test.linux/Res/effect2.ogg deleted: tests/test.linux/Res/fonts/A Damn Mess.ttf deleted: tests/test.linux/Res/fonts/Abberancy.ttf deleted: tests/test.linux/Res/fonts/Abduction.ttf deleted: tests/test.linux/Res/fonts/Paint Boy.ttf deleted: tests/test.linux/Res/fonts/Schwarzwald Regular.ttf deleted: tests/test.linux/Res/fonts/Scissor Cuts.ttf deleted: tests/test.linux/Res/fonts/arial16.fnt deleted: tests/test.linux/Res/fonts/arial16.png deleted: tests/test.linux/Res/fonts/bitmapFontTest.fnt deleted: tests/test.linux/Res/fonts/bitmapFontTest.png deleted: tests/test.linux/Res/fonts/bitmapFontTest2.fnt deleted: tests/test.linux/Res/fonts/bitmapFontTest2.png deleted: tests/test.linux/Res/fonts/bitmapFontTest3.fnt deleted: tests/test.linux/Res/fonts/bitmapFontTest3.png deleted: tests/test.linux/Res/fonts/bitmapFontTest4.fnt deleted: tests/test.linux/Res/fonts/bitmapFontTest4.png deleted: tests/test.linux/Res/fonts/bitmapFontTest5.fnt deleted: tests/test.linux/Res/fonts/bitmapFontTest5.png deleted: tests/test.linux/Res/fonts/fps_images-hd.png deleted: tests/test.linux/Res/fonts/fps_images.png deleted: tests/test.linux/Res/fonts/futura-48.fnt deleted: tests/test.linux/Res/fonts/futura-48.png deleted: tests/test.linux/Res/fonts/konqa32-hd.fnt deleted: tests/test.linux/Res/fonts/konqa32-hd.png deleted: tests/test.linux/Res/fonts/konqa32.fnt deleted: tests/test.linux/Res/fonts/konqa32.png deleted: tests/test.linux/Res/fonts/larabie-16-hd.png deleted: tests/test.linux/Res/fonts/larabie-16.png deleted: tests/test.linux/Res/fonts/tuffy_bold_italic-charmap-hd.png deleted: tests/test.linux/Res/fonts/tuffy_bold_italic-charmap.png deleted: tests/test.linux/Res/music.mid deleted: tests/test.linux/Res/zwoptex/grossini-generic.plist deleted: tests/test.linux/Res/zwoptex/grossini-generic.png deleted: tests/test.linux/Res/zwoptex/grossini.plist deleted: tests/test.linux/Res/zwoptex/grossini.png CocosDenshion/AndroidDebug/ CocosDenshion/AndroidRelease/ HelloCocos2dx/AndroidDebug/2 HelloCocos2dx/android/assets HelloCocos2dx/postCompiled.sh installLinuxLib.sh
2011-08-23 11:28:04 +08:00
// CCLog("no valid font, use default %s\n", pFontName);
iError = FT_New_Face( library, "/usr/share/fonts/truetype/freefont/FreeSans.ttf", 0, &face );
}
CC_BREAK_IF(iError);
//select utf8 charmap
iError = FT_Select_Charmap(face,FT_ENCODING_UNICODE);
CC_BREAK_IF(iError);
iError = FT_Set_Pixel_Sizes(face, fontSize,fontSize);
CC_BREAK_IF(iError);
iError = divideString(face, text, nWidth, nHeight)?0:1;
//compute the final line width
iMaxLineWidth = MAX(iMaxLineWidth, nWidth);
iMaxLineHeight = (face->size->metrics.ascender >> 6)
- (face->size->metrics.descender >> 6);
iMaxLineHeight *= vLines.size();
//compute the final line height
iMaxLineHeight = MAX(iMaxLineHeight, nHeight);
m_pData = new unsigned char[iMaxLineWidth * iMaxLineHeight*4];
iCurYCursor = SHIFT6(face->size->metrics.ascender);
memset(m_pData,0, iMaxLineWidth * iMaxLineHeight*4);
for (int i = 0; i < vLines.size(); i++) {
pText = vLines[i].sLineStr.c_str();
//initialize the origin cursor
iCurXCursor = computeLineStart(face, eAlignMask, *pText, i);
while (*pText != 0) {
int iError = FT_Load_Glyph(face, FT_Get_Char_Index(face, *pText),
FT_LOAD_RENDER);
if (iError) {
break;
}
// convert glyph to bitmap with 256 gray
// and get the bitmap
FT_Bitmap & bitmap = face->glyph->bitmap;
for (int i = 0; i < bitmap.rows; ++i) {
for (int j = 0; j < bitmap.width; ++j) {
// if it has gray>0 we set show it as 1, o otherwise
int iY = iCurYCursor + i
- (face->glyph->metrics.horiBearingY
>> 6);
int iX = iCurXCursor
+ (face->glyph->metrics.horiBearingX
>> 6) + j;
if (iY>=iMaxLineHeight) {
//exceed the height truncate
continue;
}
// m_pData[(iY * iMaxLineWidth + iX) * 4 + 3] =
// bitmap.buffer[i * bitmap.width + j] ?
// 0xff : 0;//alpha
// m_pData[(iY * iMaxLineWidth + iX) * 4 + 1] =
// bitmap.buffer[i * bitmap.width + j];//R
// m_pData[(iY * iMaxLineWidth + iX) * 4 + 2] =
// bitmap.buffer[i * bitmap.width + j];//G
// m_pData[(iY * iMaxLineWidth + iX) * 4 + 0] =
// bitmap.buffer[i * bitmap.width + j];//B
int iTemp = 0;
unsigned char cTemp = bitmap.buffer[i
* bitmap.width + j];
iTemp |= (cTemp ? 0xff : 0)<<24;
iTemp |= cTemp << 16 | cTemp << 8 | cTemp;
*(int*) &m_pData[(iY * iMaxLineWidth + iX)
* 4 + 0] = iTemp;
}
}
//step to next glyph
iCurXCursor += (face->glyph->metrics.horiAdvance >> 6)
+ iInterval;
pText++;
}
iCurYCursor += (face->size->metrics.ascender >> 6)
- (face->size->metrics.descender >> 6);
}
//print all image bitmap
// for (int i = 0; i < iMaxLineHeight; i++) {
// for (int j = 0; j < iMaxLineWidth; j++) {
// printf("%d",
// m_pData[(i * iMaxLineWidth + j) * 4] ? 1 : 0);
// }
// printf("\n");
// }
// free face
FT_Done_Face(face);
face = NULL;
//clear all lines
vLines.clear();
//success;
if (iError) {
bRet = false;
} else
bRet = true;
}while(0);
return bRet;
}
public:
FT_Library library;
unsigned char *m_pData;
int libError;
vector<TextLine> vLines;
int iInterval;
int iMaxLineWidth;
int iMaxLineHeight;
};
static BitmapDC& sharedBitmapDC()
{
static BitmapDC s_BmpDC;
return s_BmpDC;
}
bool CCImage::initWithString(
const char * pText,
int nWidth/* = 0*/,
int nHeight/* = 0*/,
ETextAlign eAlignMask/* = kAlignCenter*/,
const char * pFontName/* = nil*/,
int nSize/* = 0*/)
{
bool bRet = false;
do
{
CC_BREAK_IF(! pText);
BitmapDC &dc = sharedBitmapDC();
const char* pFullFontName = CCFileUtils::fullPathFromRelativePath(pFontName);
CC_BREAK_IF(! dc.getBitmap(pText, nWidth, nHeight, eAlignMask, pFullFontName, nSize));
// assign the dc.m_pData to m_pData in order to save time
m_pData = dc.m_pData;
CC_BREAK_IF(! m_pData);
m_nWidth = (short)dc.iMaxLineWidth;
m_nHeight = (short)dc.iMaxLineHeight;
m_bHasAlpha = true;
m_bPreMulti = true;
m_nBitsPerComponent = 8;
bRet = true;
dc.reset();
}while (0);
//do nothing
return bRet;
}
NS_CC_END;