Hi351x 通过freetype 显示OSD
2 E币
成为会员,免费下载资料
文件大小:1.22 KB
上传者:wahuqeir
时间:2017-05-06 16:21:16
下载量:100
1.依赖库 freetype-xxx.tar 从字库中获取字形bitmap
libiconv-xxx.tar.gz 转码,用于显示中文等字符
交叉编译到相应平台
2. freetype 操作详见 附件
3. main 示例
#include "loadfont.h"
#include
#include
#include
#include
#include
static char *font_library_path = "./simsun.ttc";
#define BUFFER_SIZE 4
void printf_font(struct Font_Bitmap *font_bitmap)
{
if(NULL == font_bitmap || NULL == font_bitmap->buffer) {
return;
}
int i = 0;
int j = 0;
int k = 0;
int counter = 0;
for (j = 0; j < (font_bitmap->fontsize * 26) / 32 - font_bitmap->top; j++){
for (i = 0; i < font_bitmap->fontsize; i++){
printf("_");
}
printf("\n");
}
for (; j < font_bitmap->rows + (font_bitmap->fontsize * 26) / 32 - font_bitmap->top; j++){
for (i = 1; i <= font_bitmap->left; i++) {
printf("_");
}
for (k = 0; k < font_bitmap->pitch; k++) {
int tmp = font_bitmap->buffer[font_bitmap->pitch*(j + font_bitmap->top - (font_bitmap->fontsize * 26) / 32) + k];
for (counter = 0; counter < 8; counter++) {
if (tmp & 0x80) {
printf("*");
}
else {
printf("_");
}
tmp <<= 1;
i++;
if (i > font_bitmap->fontsize) {
break;
}
}
}
for(; i <= font_bitmap->fontsize; ++ i){
printf("_");
}
printf("\n");
}
for (; j < font_bitmap->fontsize; j++){
for (i = 0; i < font_bitmap->fontsize; i++){
printf("_");
}
printf("\n");
}
}
void test_load_font(wchar_t testa, int fontsize)
{
struct Font_Bitmap font_bitmap;
int ret = SS_Font_Set_FontSize(fontsize);
if(0 != ret) {
printf("%s:%d-----SS_Font_Set_FontSize failed\n", __func__, __LINE__);
}
ret = SS_Load_FontBitmap(testa, &font_bitmap);
if(0 == ret){
//actually the memory bitmap should be drawed here
printf_font(&font_bitmap);
} else {
printf("%s:%d-----SS_Load_FontBitmap failed\n", __func__, __LINE__);
}
}
unsigned short Get_Unicode(char *utf8)
{ unsigned short unicode;
unicode = utf8[0];
//printf("unicode before:0x%x\n",unicode);
if (unicode >= 0xF0) {
unicode = (unsigned short) (utf8[0] & 0x07) << 18;
unicode |= (unsigned short) (utf8[1] & 0x3F) << 12;
unicode |= (unsigned short) (utf8[2] & 0x3F) << 6;
unicode |= (unsigned short) (utf8[3] & 0x3F);
} else if (unicode >= 0xE0) {
unicode = (unsigned short) (utf8[0] & 0x0F) << 12;
unicode |= (unsigned short) (utf8[1] & 0x3F) << 6;
unicode |= (unsigned short) (utf8[2] & 0x3F);
} else if (unicode >= 0xC0) {
unicode = (unsigned short) (utf8[0] & 0x1F) << 6;
unicode |= (unsigned short) (utf8[1] & 0x3F);
}
return unicode;
}
static unsigned short Get_Unicode_(char *cStr, int *unicode_char_len)
{
unsigned short unicode = 0x00;
if(NULL == cStr){
return unicode;
}
int str_len = strlen(cStr);
if(0 == str_len){
return unicode;
}
int char_len = 0;;
unicode = cStr[0];
if (unicode >= 0xF0 && str_len >=4) {
unicode = (unsigned short) (cStr[0] & 0x07) << 18;
unicode |= (unsigned short) (cStr[1] & 0x3F) << 12;
unicode |= (unsigned short) (cStr[2] & 0x3F) << 6;
unicode |= (unsigned short) (cStr[3] & 0x3F);
char_len = 4;
} else if (unicode >= 0xE0 && str_len >=3) {
unicode = (unsigned short) (cStr[0] & 0x0F) << 12;
unicode |= (unsigned short) (cStr[1] & 0x3F) << 6;
unicode |= (unsigned short) (cStr[2] & 0x3F);
char_len = 3;
} else if (unicode >= 0xC0 && str_len >=2) {
unicode = (unsigned short) (cStr[0] & 0x1F) << 6;
unicode |= (unsigned short) (cStr[1] & 0x3F);
char_len = 2;
} else {
char_len = 1;
}
if(NULL != unicode_char_len) {
*unicode_char_len = char_len;
}
return unicode;
}
int code_convert(char *from_charset, char *to_charset, char *inbuf, size_t inlen, char *outbuf, size_t outlen)
{
iconv_t cd;
char **pin = &inbuf;
char **pout = &outbuf;
cd = iconv_open(to_charset,from_charset);
if (cd == 0){
return -1;
}
if (iconv(cd, pin, &inlen, pout, &outlen) == -1){
iconv_close(cd);
return -1;
}
iconv_close(cd);
return 0;
}
int main()
{
int ret = 0;
ret = SS_Font_Init(font_library_path);
if(ret == 0) {
char *test_ch = "我";
char *utf8_ch = (char *)malloc(strlen(test_ch) * sizeof(char));
memset(utf8_ch, 0, strlen(test_ch) * sizeof(char));
code_convert("gb2312", "utf-8", test_ch, strlen(test_ch), utf8_ch, BUFFER_SIZE);
int str_len = strlen(utf8_ch);
printf("strlen:%d \n", str_len);
int index = 0;
int len = 0;
for(index = 0; index < str_len; index += len){
unsigned short ch_unicode = Get_Unicode_(&utf8_ch[index], &len);
test_load_font(ch_unicode , 64);
}
free(utf8_ch);
} else {
printf("%s:%d-----SS_Font_Init failed\n", __func__, __LINE__);
}
SS_Font_deInit();
return 0;
}
libiconv-xxx.tar.gz 转码,用于显示中文等字符
交叉编译到相应平台
2. freetype 操作详见 附件
3. main 示例
#include "loadfont.h"
#include
#include
#include
#include
#include
static char *font_library_path = "./simsun.ttc";
#define BUFFER_SIZE 4
void printf_font(struct Font_Bitmap *font_bitmap)
{
if(NULL == font_bitmap || NULL == font_bitmap->buffer) {
return;
}
int i = 0;
int j = 0;
int k = 0;
int counter = 0;
for (j = 0; j < (font_bitmap->fontsize * 26) / 32 - font_bitmap->top; j++){
for (i = 0; i < font_bitmap->fontsize; i++){
printf("_");
}
printf("\n");
}
for (; j < font_bitmap->rows + (font_bitmap->fontsize * 26) / 32 - font_bitmap->top; j++){
for (i = 1; i <= font_bitmap->left; i++) {
printf("_");
}
for (k = 0; k < font_bitmap->pitch; k++) {
int tmp = font_bitmap->buffer[font_bitmap->pitch*(j + font_bitmap->top - (font_bitmap->fontsize * 26) / 32) + k];
for (counter = 0; counter < 8; counter++) {
if (tmp & 0x80) {
printf("*");
}
else {
printf("_");
}
tmp <<= 1;
i++;
if (i > font_bitmap->fontsize) {
break;
}
}
}
for(; i <= font_bitmap->fontsize; ++ i){
printf("_");
}
printf("\n");
}
for (; j < font_bitmap->fontsize; j++){
for (i = 0; i < font_bitmap->fontsize; i++){
printf("_");
}
printf("\n");
}
}
void test_load_font(wchar_t testa, int fontsize)
{
struct Font_Bitmap font_bitmap;
int ret = SS_Font_Set_FontSize(fontsize);
if(0 != ret) {
printf("%s:%d-----SS_Font_Set_FontSize failed\n", __func__, __LINE__);
}
ret = SS_Load_FontBitmap(testa, &font_bitmap);
if(0 == ret){
//actually the memory bitmap should be drawed here
printf_font(&font_bitmap);
} else {
printf("%s:%d-----SS_Load_FontBitmap failed\n", __func__, __LINE__);
}
}
unsigned short Get_Unicode(char *utf8)
{ unsigned short unicode;
unicode = utf8[0];
//printf("unicode before:0x%x\n",unicode);
if (unicode >= 0xF0) {
unicode = (unsigned short) (utf8[0] & 0x07) << 18;
unicode |= (unsigned short) (utf8[1] & 0x3F) << 12;
unicode |= (unsigned short) (utf8[2] & 0x3F) << 6;
unicode |= (unsigned short) (utf8[3] & 0x3F);
} else if (unicode >= 0xE0) {
unicode = (unsigned short) (utf8[0] & 0x0F) << 12;
unicode |= (unsigned short) (utf8[1] & 0x3F) << 6;
unicode |= (unsigned short) (utf8[2] & 0x3F);
} else if (unicode >= 0xC0) {
unicode = (unsigned short) (utf8[0] & 0x1F) << 6;
unicode |= (unsigned short) (utf8[1] & 0x3F);
}
return unicode;
}
static unsigned short Get_Unicode_(char *cStr, int *unicode_char_len)
{
unsigned short unicode = 0x00;
if(NULL == cStr){
return unicode;
}
int str_len = strlen(cStr);
if(0 == str_len){
return unicode;
}
int char_len = 0;;
unicode = cStr[0];
if (unicode >= 0xF0 && str_len >=4) {
unicode = (unsigned short) (cStr[0] & 0x07) << 18;
unicode |= (unsigned short) (cStr[1] & 0x3F) << 12;
unicode |= (unsigned short) (cStr[2] & 0x3F) << 6;
unicode |= (unsigned short) (cStr[3] & 0x3F);
char_len = 4;
} else if (unicode >= 0xE0 && str_len >=3) {
unicode = (unsigned short) (cStr[0] & 0x0F) << 12;
unicode |= (unsigned short) (cStr[1] & 0x3F) << 6;
unicode |= (unsigned short) (cStr[2] & 0x3F);
char_len = 3;
} else if (unicode >= 0xC0 && str_len >=2) {
unicode = (unsigned short) (cStr[0] & 0x1F) << 6;
unicode |= (unsigned short) (cStr[1] & 0x3F);
char_len = 2;
} else {
char_len = 1;
}
if(NULL != unicode_char_len) {
*unicode_char_len = char_len;
}
return unicode;
}
int code_convert(char *from_charset, char *to_charset, char *inbuf, size_t inlen, char *outbuf, size_t outlen)
{
iconv_t cd;
char **pin = &inbuf;
char **pout = &outbuf;
cd = iconv_open(to_charset,from_charset);
if (cd == 0){
return -1;
}
if (iconv(cd, pin, &inlen, pout, &outlen) == -1){
iconv_close(cd);
return -1;
}
iconv_close(cd);
return 0;
}
int main()
{
int ret = 0;
ret = SS_Font_Init(font_library_path);
if(ret == 0) {
char *test_ch = "我";
char *utf8_ch = (char *)malloc(strlen(test_ch) * sizeof(char));
memset(utf8_ch, 0, strlen(test_ch) * sizeof(char));
code_convert("gb2312", "utf-8", test_ch, strlen(test_ch), utf8_ch, BUFFER_SIZE);
int str_len = strlen(utf8_ch);
printf("strlen:%d \n", str_len);
int index = 0;
int len = 0;
for(index = 0; index < str_len; index += len){
unsigned short ch_unicode = Get_Unicode_(&utf8_ch[index], &len);
test_load_font(ch_unicode , 64);
}
free(utf8_ch);
} else {
printf("%s:%d-----SS_Font_Init failed\n", __func__, __LINE__);
}
SS_Font_deInit();
return 0;
}
展开》
折叠》