绘制多行文字代码(c语言实现)

1、头文件

#ifndef _MULTITEXT_H_
#define _MULTITEXT_H_

#include "vrecommon.h"

#define MULTITEXT_LINE_NUMBER (100)

#define MULTITEXT_ALIGN_LEFT (1<<0)
#define MULTITEXT_ALIGN_HCENTER (1<<1)
#define MULTITEXT_ALIGN_RIGHT (1<<2)
#define MULTITEXT_ALIGN_TOP (1<<3)
#define MULTITEXT_ALIGN_VCENTER (1<<4)
#define MULTITEXT_ALIGN_BOTTOM (1<<5)

typedef struct
{
    int x;
    int y;
    int width;
    int height;
    int color;
    int lineSpacing;
    int startLine;
    int paintLineNumber;
    WCHAR* text;
    int align;
    int indexArray[MULTITEXT_LINE_NUMBER];
    int lengthArray[MULTITEXT_LINE_NUMBER];
    int arraySize;
} MultiText;


MultiText* MultiText_create(int x, int y, int width, int height);
void MultiText_setText(MultiText* point, WCHAR * text);
void MultiText_setGbText(MultiText* point, char * text);
void MultiText_setLineSpacing(MultiText* point, int lineSpacing);
void MultiText_paint(MultiText* point);
void MultiText_destroy(MultiText* point);
int MultiText_isHavePrev(MultiText* point);
int MultiText_isHaveNext(MultiText* point);
void MultiText_prevPage(MultiText* point);
void MultiText_nextPage(MultiText* point);
int MultiText_isPointIn(MultiText* point, int x, int y);

 

 

#endif

 

 

 

2、源文件

#include "multitext.h"

extern unsigned char* scrbuf;

#define TEXT_LINE_LENGTH (100)


static int MultiText_getTop(MultiText* point);

MultiText* MultiText_create(int x, int y, int width, int height)
{
    int textLen = 0;
    MultiText* ret = 0;
   
    WCHAR text[2] = {0};

    text[0] = 0;
    text[1] = 0;
    textLen = vm_wstrlen(text);
   
    ret = vm_calloc(sizeof(MultiText));
    ret->x = 0;
    ret->y = 0;
    ret->text = vm_calloc(sizeof(WCHAR)*(textLen+1));/*lint !e737*/
   
    ret->color = 0xffffff;
    vm_wstrcpy((wchar_t *)ret->text,(wchar_t *)text);

   
    //
    ret->x = x;
    ret->y = y;
    ret->width = width;
    ret->height = height;
    ret->color = 0;
    ret->lineSpacing = 3;
    ret->startLine = 0;
    ret->paintLineNumber = height/(vm_graphic_get_character_height()+ret->lineSpacing);
    if (ret->paintLineNumber<=0) {
        ret->paintLineNumber = 1;
    }
    ret->align = MULTITEXT_ALIGN_LEFT | MULTITEXT_ALIGN_TOP;
    ret->arraySize = 0;

    return ret;
}
void MultiText_setLineSpacing(MultiText* point, int lineSpacing)
{
    point->lineSpacing = lineSpacing;
    point->paintLineNumber = point->height/(vm_graphic_get_character_height()+point->lineSpacing);
    if (point->paintLineNumber<=0) {
        point->paintLineNumber = 1;
    }
}

void MultiText_setText(MultiText* point, WCHAR* text)
{
    MultiText* ret = point;   
    int i = 0;
    size_t textLen = 0;
    WCHAR temp[TEXT_LINE_LENGTH] = {0};
    int tempIndex = 0;
    int computeIndex = 0;
    int computeLength = 0;

    if(point->text!=NULL)
    {
        vm_free(point->text);
    }
    ret->arraySize = 0;
    textLen = vm_wstrlen(text);   
    ret->text = (WCHAR*)vm_calloc((unsigned int)(sizeof(WCHAR)*(textLen+1)));
    vm_wstrcpy((wchar_t *)ret->text,(wchar_t *)text);

    while (1) {/*lint !e716*/
        if (text[i]==0) {
            if (i!=computeIndex) {
                computeLength = i - computeIndex;
                ret->indexArray[ret->arraySize] = computeIndex;
                ret->lengthArray[ret->arraySize] = computeLength;
                ret->arraySize++;
            }
            break;
        } else if (text[i]=='\n') {
            computeLength = i - computeIndex;
            ret->indexArray[ret->arraySize] = computeIndex;
            ret->lengthArray[ret->arraySize] = computeLength;
            ret->arraySize++;
            computeIndex = i + 1;
            tempIndex = 0;
            i = i+1;
            continue;
        } else {
            int lineWidth = 0;
            temp[tempIndex] = text[i];
            tempIndex++;
            temp[tempIndex] = 0;
            lineWidth = vm_graphic_get_string_width(temp);
            if (lineWidth>ret->width) {
                computeLength = i - computeIndex;
                ret->indexArray[ret->arraySize] = computeIndex;
                ret->lengthArray[ret->arraySize] = computeLength;
                ret->arraySize++;
                computeIndex = i;
                tempIndex = 0;
                continue;
            }
            i++;
        }
    }
}

void MultiText_setGbText(MultiText* point, char * text)
{
    int textLen ;
    unsigned int bufSize;
    WCHAR* wBuf;
    if(point==NULL)
        return;
    textLen= strlen(text);
    bufSize = textLen+1;
    wBuf = vm_calloc(sizeof(WCHAR)*bufSize);
    vm_gb2312_to_ucs2(wBuf, sizeof(WCHAR)*bufSize, text);
    MultiText_setText(point, wBuf);
    vm_free(wBuf);wBuf=NULL;
}

void MultiText_paint(MultiText* point)
{
    int i = 0;
    int j = 0;
    int yPos = 0;
    int xPos = 0;
    WCHAR temp[TEXT_LINE_LENGTH];
    int lineHeight = 0;

    lineHeight = point->lineSpacing + vm_graphic_get_character_height();
    yPos = MultiText_getTop(point);
    for(i=point->startLine, j=0; i<point->arraySize && j<point->paintLineNumber; i++, j++) {
        vm_wstrncpy(temp, &point->text[point->indexArray[i]], point->lengthArray[i]);
        if (point->align & MULTITEXT_ALIGN_LEFT) {
            xPos = point->x;
        } else if (point->align & MULTITEXT_ALIGN_HCENTER) {
            xPos = point->x + (point->width - vm_graphic_get_string_width(temp))/2;
        } else if (point->align & MULTITEXT_ALIGN_RIGHT) {
            xPos = point->x + point->width - vm_graphic_get_string_width(temp);
        } else {
            xPos = point->x;
        }
        //vm_graphic_drawtext(xPos, yPos, temp, point->lengthArray[i], point->color);
        vm_graphic_textout(scrbuf,xPos, yPos, temp,point->lengthArray[i],(unsigned short)(VM_COLOR_INT_TO_565(point->color)));
        yPos += lineHeight;       
    }
}

void MultiText_destroy(MultiText* point)
{
    if (point==NULL) {
        return;
    }
    vm_free(point->text);point->text = NULL;
    vm_free(point);
}

int MultiText_isHavePrev(MultiText* point)
{
    return point->startLine>0;
}

int MultiText_isHaveNext(MultiText* point)
{
    return (point->startLine+point->paintLineNumber)<point->arraySize;
}

void MultiText_prevPage(MultiText* point)
{
    int pageSize = 0;
    int newStart = 0;
   
    pageSize = point->paintLineNumber;
    newStart = point->startLine - pageSize;
    point->startLine = newStart>=0?newStart:point->startLine;
}

void MultiText_nextPage(MultiText* point)
{
    int pageSize = 0;
    int newStart = 0;
   
    pageSize = point->paintLineNumber;
    newStart = point->startLine + pageSize;
    point->startLine = newStart < point->arraySize ? newStart:point->startLine ;//(point->arraySize-1);
}

static int MultiText_getTop(MultiText* point)
{
    int ret = 0;
    int charHeight = 0;
    charHeight = vm_graphic_get_character_height();

    if (point->align&MULTITEXT_ALIGN_TOP) {
        ret = point->y;
    } else if (point->align&MULTITEXT_ALIGN_VCENTER) {
        ret = point->y + point->height/2
            - (point->arraySize-1)*point->lineSpacing/2
            - (point->arraySize)*charHeight/2;
    } else if (point->align&MULTITEXT_ALIGN_BOTTOM) {
        ret = point->y + point->height
            - (point->arraySize-1)*point->lineSpacing
            - (point->arraySize)*charHeight;
    } else {
        ret = point->y;
    }
    return ret;
}

int MultiText_isPointIn(MultiText* point, int x, int y)
{
    if (x>=point->x && x<point->x+point->width
        && y>=point->y && y<point->y+point->height) {
        return 1;
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值