📄 glib.c
字号:
#include "CompileSwitch.h"
#include <stdio.h>
#include <string.h>
#include "Glib.h"
#include "FontLib.h" //该文件内部包含了字体的位图定义,因此我们仅在这里包含一次
char ASSERT_STR[100];
//////////////////////////////////////////////////////////////////
//
// Text related
//
//
const NANA_FONT_LIB NANA_FONT16x=
{
(unsigned char *)YYx_GB_charlib[0][0],
16,
(unsigned char *)ARN18_ASCII_charlib,
18,
(int *) ARN18_ASCII_charwidth,
(int *) ARN18_ASCII_charindex,
};
const NANA_FONT_LIB NANA_FONT12=
{
(unsigned char *)HW12_GB_charlib[0][0],
12,
(unsigned char *)HW12_ASCII_charlib,
12,
(int *) HW12_ASCII_charwidth,
(int *) HW12_ASCII_charindex,
};
const NANA_FONT_LIB NANA_FONT16=
{
(unsigned char *)YY_GB_charlib[0][0],
16,
(unsigned char *)ARN24_ASCII_charlib,
24,
(int *) ARN24_ASCII_charwidth,
(int *) ARN24_ASCII_charindex,
};
const NANA_FONT_LIB NANA_FONT32=
{
(unsigned char *)YYx_GB_charlib[0][0],//the HZ part is not usable
32,
(unsigned char *)IMP32_ASCII_charlib,
32,
(int *) IMP32_ASCII_charwidth,
(int *) IMP32_ASCII_charindex,
};
//=========计算字符串的宽度===============================================
// 输入参数 :cText 以0结尾的输入字符串
// nFont 字体编号
// nWordNum 统计的字数 -1则统计所有字符
// 返回值 :字符串的宽度
//
int GetTextWidth(const char * strText,const NANA_FONT_LIB * pfontLib,int symbol_limit)
{
int byte_count,text_index,fontLib_char_index;
int text_width,symbol_count;
if(strText == NULL) return 0;
byte_count = strlen(strText);
text_width = 0;
symbol_count = 0;
text_index = 0;
while(byte_count)
{
if(symbol_limit>=0)
{
symbol_count++;
if (symbol_count>symbol_limit)
{
break;
}
}
if(strText[text_index] & 0x80)
{
text_width += pfontLib->GBFontSize;
text_index += 2;
byte_count -= 2;
}else
{
// ASCII字符 每次取出一个字节显示
fontLib_char_index = (unsigned char)(strText[text_index]) - 0x20;
text_width += pfontLib->pASCIIFontWidth[fontLib_char_index];
text_index ++;
byte_count --;
}
}
return text_width;
}
//=======根据该汉字国标码取得汉字在字库中位置====================
// 输入参数 :GBCode 一个两个字节的数组,指定汉字国标编码
// 返回值 :汉字在字库缓冲区中的偏移量
unsigned int Get_GBIndex(const char * strGBCode)
{
const unsigned char * GBCode = (const unsigned char * )strGBCode;
unsigned int BlockNumber,PosNumber;
BlockNumber = (((GBCode[0])-0xB0) & 0xff);
PosNumber = (GBCode[1])- 0xA0;
return BlockNumber* 0x60 + PosNumber;
}
//========显示字符串================================================
void TextPrintEx(const char * strText,int x,int y,const NANA_FONT_LIB * pfontLib,int ByteLimit,int bNeg)
{
int byte_count,text_index,fontLib_char_index;
int GB_fontchar_bytes;
NANA_BITMAP pic;
if(strText == NULL) return;
pic.biBitCount = 1;
pic.biColorMask = 0;
pic.biReverseColor = bNeg;
//由于我们使用1bit位图保存图片,因此需要按照字节对齐方式保存一副位图
//因此需要下面的校正,GHAL的BMP格式内部缺省就是按照这个方式识别位图的
if(pfontLib->GBFontSize % 8 == 0)
{
GB_fontchar_bytes = (pfontLib->GBFontSize / 8) * pfontLib->GBFontSize;
}else
{
GB_fontchar_bytes = ((pfontLib->GBFontSize / 8) + 1) * pfontLib->GBFontSize;
}
byte_count = strlen(strText);
byte_count = ByteLimit>byte_count?byte_count:ByteLimit;
text_index = 0;
while(text_index<byte_count)
{
if(x >= SCREEN_WIDTH) break;
if(strText[text_index] & 0x80)
{
// GB字符 每次取出两个字节显示
fontLib_char_index = Get_GBIndex(&(strText[text_index]));
pic.biHeight = pfontLib->GBFontSize;
pic.biWidth = pfontLib->GBFontSize;
pic.pData = pfontLib->GBFontLib +
fontLib_char_index * GB_fontchar_bytes;
GHAL_put_bmp(x,y,&pic);
x += pic.biWidth;
text_index += 2;
}else
{
// ASCII字符 每次取出一个字节显示
fontLib_char_index = (unsigned char)(strText[text_index]) - 0x20;
pic.biHeight = pfontLib->ASCIIFontHeight;
pic.biWidth = pfontLib->pASCIIFontWidth[fontLib_char_index];
pic.pData = pfontLib->ASCIIFontlib + pfontLib->pASCIIFontIndex[fontLib_char_index];
GHAL_put_bmp(x,y,&pic);
x += pic.biWidth;
text_index ++;
}
}
}
void TextBlockPrint(const char * strMultiLineText,int x,int y,
int *pwidth,int *pheight,
const NANA_FONT_LIB * pfontLib,int bNeg,
int align_mode,int LineSpace,int bCalculateRect)
{
const char * pchar;
const char * pNewLineStart;
int line_width;
//real position and size of the multiLine block of text
int real_height;
int real_width;
int real_x;
int real_y;
line_width = 0;
real_height = 0;
real_width = 0;
pchar = strMultiLineText;
while(*pchar)
{
if( ( *pchar == '\\' ) && (*(pchar +1) == 'n') )
{
//return mark
if(real_width<line_width)
real_width = line_width;
real_height += pfontLib->GBFontSize + LineSpace;
pchar += 2;
line_width = 0;
}else if(*pchar == '\n')
{
//return mark
if(real_width<line_width)
real_width = line_width;
real_height += pfontLib->GBFontSize + LineSpace;
pchar += 1;
line_width = 0;
}else
if( (*pchar) & 0x80)
{
//GB char
line_width += pfontLib->GBFontSize;
pchar +=2;
}else{
line_width += pfontLib->pASCIIFontWidth[(*pchar)-0x20];
pchar ++;
}
}
if(real_width<line_width)
real_width = line_width;
real_height += pfontLib->GBFontSize;
if(bCalculateRect || (*pwidth < real_width) || (*pheight < real_height))
{
*pwidth = real_width;
*pheight = real_height;
return;
}
//draw it at the center of whole specified area
real_x = x + (*pwidth - real_width)/2;
real_y = y + (*pheight - real_height)/2;
line_width = 0;
pNewLineStart = pchar = strMultiLineText;
while(*pchar)
{
if( *pchar == '\\' && *(pchar + 1) == 'n' || *pchar == '\n')
{
//draw this line
switch(align_mode)
{
case ALIGN_LEFT:
x = real_x;//line_width;
break;
case ALIGN_RIGHT:
x = real_x + real_width - line_width;
break;
case ALIGN_MIDDLE:
x = real_x + (real_width - line_width)/2;
break;
}
TextPrintEx(pNewLineStart,x,real_y,pfontLib,(pchar-pNewLineStart),bNeg);
real_y += pfontLib->GBFontSize + LineSpace;
//return mark
if(*pchar == '\n')
{
pchar += 1;
}else
{
pchar += 2;
}
pNewLineStart = pchar;
line_width = 0;
}else
if( (*pchar) & 0x80)
{
//GB char
line_width += pfontLib->GBFontSize;
pchar +=2;
}else{
line_width += pfontLib->pASCIIFontWidth[(*pchar)-0x20];
pchar ++;
}
}
//draw final line
switch(align_mode)
{
case ALIGN_LEFT:
x = real_x;//line_width;
break;
case ALIGN_RIGHT:
x = real_x + real_width - line_width;
break;
case ALIGN_MIDDLE:
x = real_x + (real_width - line_width)/2;
break;
}
TextPrintEx(pNewLineStart,x,real_y,pfontLib,(pchar-pNewLineStart),bNeg);
}
///////////////////////////////////////////////////////////////////
//
// simple shape drawing
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -