首页 > 代码库 > cocos2dx windows phone平台下CCLabelTTF自动换行的实现(2)
cocos2dx windows phone平台下CCLabelTTF自动换行的实现(2)
前几天发过一篇文章说如何实现wp8下的CCLabelTTF如何自动换行,后来发现果如预料的那般,效果很不好,主要是非等宽字体的情况下看着很糟心,因此再修改了一版,效果要好很多了。
具体实现其实就是参考initGlyphs,但是会不断的检查是否超过宽度,如果超过则自动换行。
具体的直接看代码就明白了
/**************************************************************************** Copyright (c) 2010 cocos2d-x.org Copyright (c) Microsoft Open Technologies, 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. ****************************************************************************/ #ifndef __PLATFORM_WINRT_FREETYPE_H__ #define __PLATFORM_WINRT_FREETYPE_H__ #include "platform/CCCommon.h" #include "platform/CCImage.h" #include <string> #include <vector> #include <memory> #define generic GenericFromFreeTypeLibrary #define internal InternalFromFreeTypeLibrary #include <ft2build.h> #include <freetype/freetype.h> #include <freetype/ftglyph.h> #include <freetype/ftoutln.h> #include <freetype/fttrigon.h> #undef generic #undef internal NS_CC_BEGIN typedef struct TGlyph_ { FT_UInt index; // glyph index FT_Vector pos; // glyph origin on the baseline FT_Glyph image; // glyph image } TGlyph, *PGlyph; typedef struct FontBufferInfo { unsigned char* pBuffer; unsigned long size; } FontBufferInfo; typedef struct FTWordInfo { std::vector<TGlyph> glyphs; // glyphs for the word FT_BBox bbox; // bounding box containing all of the glyphs in the word } FTWordInfo; typedef struct FTLineInfo { std::vector<TGlyph> glyphs; // glyphs for the line text FT_BBox bbox; // bounding box containing all of the glyphs in the line unsigned int width; // width of the line FT_Vector pen; // current pen position } FTLineInfo; class CC_DLL CCFreeTypeFont { public: CCFreeTypeFont(); ~CCFreeTypeFont(); bool initWithString( const char* pText, const char* pFontName, int nSize, int width, int height ); unsigned char* getBitmap( CCImage::ETextAlign eAlignMask, int* outWidth, int* outHeight ); private: unsigned char* loadFont(const char *pFontName, unsigned long *size); unsigned char* CCFreeTypeFont::loadSystemFont(const char *pFontName, unsigned long *size); FT_Error CCFreeTypeFont::initGlyphs(const char* text); void compute_bbox(std::vector<TGlyph>& glyphs, FT_BBox *abbox); void drawText(FTLineInfo* pInfo, unsigned char* pBuffer, FT_Vector *pen); void draw_bitmap(unsigned char* pBuffer, FT_Bitmap* bitmap,FT_Int x,FT_Int y); void reset(); FT_Vector getPenForAlignment(FTLineInfo* pInfo, CCImage::ETextAlign eAlignMask, int lineNumber, int totalLines); FT_Error addLine(const std::string& line); void newLine(); void endLine(); const std::string m_space; std::string m_text; std::string m_fontName; FT_Face m_face; std::vector<FTLineInfo*> m_lines; int m_inWidth; // requested width of text box int m_inHeight; // requested height of text box int m_width; // final bitMap width int m_height; // final bitMap height int m_textWidth; // width of text text after word wrapping and line breaks int m_textHeight; // height of text text after word wrapping and line breaks int m_lineHeight; // height of a line for the font size int m_windowWidth; // the width of the window FTLineInfo* m_currentLine; // the current line object to add words to. }; NS_CC_END
/**************************************************************************** Copyright (c) 2013 cocos2d-x.org http://cocos2d-x.org Copyright (c) Microsoft Open Technologies, Inc. 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 "CCFreeTypeFont.h" #include "CCDirector.h" #include "platform/CCFileUtils.h" #if (CC_TARGET_PLATFORM != CC_PLATFORM_WP8) #include <dwrite.h> #endif #include <map> #include <string> #include <sstream> #include <vector> #include <memory> #include <algorithm> using namespace std; NS_CC_BEGIN static map<std::string, FontBufferInfo> s_fontsNames; static FT_Library s_FreeTypeLibrary = nullptr; CCFreeTypeFont::CCFreeTypeFont() :m_space(" ") , m_face(nullptr) { } CCFreeTypeFont::~CCFreeTypeFont() { reset(); } void CCFreeTypeFont::reset() { for(auto line:m_lines) { line->glyphs.clear(); delete line; } m_lines.clear(); if(m_face) { FT_Done_Face(m_face); m_face = nullptr; } } bool CCFreeTypeFont::initWithString( const char* pText, const char* pFontName, int nSize, int inWidth, int inHeight ) { FT_Error error = 0; unsigned long size = 0; unsigned char* pBuffer = nullptr; unsigned char* data = http://www.mamicode.com/nullptr;>
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。