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 
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值