Toggle navigation
首页
问答
文章
积分商城
专家
专区
更多专区...
文档中心
返回主站
搜索
提问
会员
中心
登录
注册
RT-Thread一般讨论
ASC字库文件驱动分享
发布于 2013-01-21 16:41:16 浏览:4697
订阅该版
如果系统中使用了多套较大的asc字库,放在内部flash中明显不是一种很好的方法,所以还是仿照汉字字库的方式,可以读取SD字库,也算是GUI字库的一种完善吧。 新建并添加font_asc_file.c: ``` /* * Cached asc font engine */ #include
#include
#include
#include
#define RTGUI_USING_ASC_FILE #ifdef RTGUI_USING_ASC_FILE #ifdef _WIN32_NATIVE #include
#include
#include
#include
#define open _open #define close _close #define read _read #define write _write #define unlink _unlink #else #include
#endif #define ASC_CACHE_MAX 2 static int _font_cache_compare(struct asc_cache *node1, struct asc_cache *node2); static void rtgui_asc_file_font_load(struct rtgui_font *font); static void rtgui_asc_file_font_draw_text(struct rtgui_font *font, struct rtgui_dc *dc, const char *text, rt_ubase_t len, struct rtgui_rect *rect); static void rtgui_asc_file_font_get_metrics(struct rtgui_font *font, const char *text, rtgui_rect_t *rect); const struct rtgui_font_engine rtgui_asc_file_font_engine = { RT_NULL, rtgui_asc_file_font_load, rtgui_asc_file_font_draw_text, rtgui_asc_file_font_get_metrics }; SPLAY_PROTOTYPE(asc_cache_tree, asc_cache, asc_node, _font_cache_compare); SPLAY_GENERATE(asc_cache_tree, asc_cache, asc_node, _font_cache_compare); static int _font_cache_compare(struct asc_cache *cache_1, struct asc_cache *cache_2) { if (cache_1->asc_id > cache_2->asc_id) return 1; if (cache_1->asc_id < cache_2->asc_id) return -1; return 0; } static rt_uint8_t *_font_cache_get(struct rtgui_asc_file_font *font, rt_uint8_t asc_id) { rt_uint32_t seek; struct asc_cache *cache, search; search.asc_id = asc_id; /* enter critical */ rtgui_enter_critical(); cache = SPLAY_FIND(asc_cache_tree, &(font->cache_root), &search); if (cache != RT_NULL) { /* exit critical */ rtgui_exit_critical(); /* found it */ return (rt_uint8_t *)(cache + 1); } /* exit critical */ rtgui_exit_critical(); /* can not find it, load to cache */ cache = (struct asc_cache *) rtgui_malloc(sizeof(struct asc_cache) + font->font_data_size); if (cache == RT_NULL) return RT_NULL; /* no memory yet */ cache->asc_id = asc_id; seek =asc_id; seek *= font->font_data_size; /* read asc font data */ if ((lseek(font->fd, seek, SEEK_SET) < 0) || read(font->fd, (char *)(cache + 1), font->font_data_size) != font->font_data_size) { rtgui_free(cache); return RT_NULL; } /* enter critical */ rtgui_enter_critical(); if (font->cache_size >= ASC_CACHE_MAX) { /* remove a cache */ struct asc_cache *left; left = font->cache_root.sph_root; while (SPLAY_LEFT(left, asc_node) != RT_NULL) left = SPLAY_LEFT(left, asc_node); /* remove the left node */ SPLAY_REMOVE(asc_cache_tree, &(font->cache_root), left); rtgui_free(left); font->cache_size --; } /* insert to cache */ SPLAY_INSERT(asc_cache_tree, &(font->cache_root), cache); font->cache_size ++; /* exit critical */ rtgui_exit_critical(); return (rt_uint8_t *)(cache + 1); } static void rtgui_asc_file_font_load(struct rtgui_font *font) { struct rtgui_asc_file_font *asc_file_font = (struct rtgui_asc_file_font *)font->data; RT_ASSERT(asc_file_font != RT_NULL); asc_file_font->fd = open(asc_file_font->font_fn, O_RDONLY, 0); } static void _rtgui_asc_file_font_draw_text(struct rtgui_asc_file_font *asc_file_font, struct rtgui_dc *dc, const char *text, rt_ubase_t len, struct rtgui_rect *rect) { rt_uint8_t *str; rtgui_color_t bc; rt_uint16_t style; register rt_base_t h, word_bytes; /* get text style */ style = rtgui_dc_get_gc(dc)->textstyle; bc = rtgui_dc_get_gc(dc)->background; /* drawing height */ h = (asc_file_font->font_size + rect->y1 > rect->y2) ? rect->y2 - rect->y1 : asc_file_font->font_size; word_bytes = (asc_file_font->font_size + 15) / 16; str = (rt_uint8_t *)text; while (len > 0 && rect->x1 < rect->x2) { const rt_uint8_t *font_ptr; register rt_base_t i, j, k; /* get font pixel data */ font_ptr = _font_cache_get(asc_file_font, *str); /* draw word */ for (i = 0; i < h; i ++) { for (j = 0; j < word_bytes; j++) for (k = 0; k < 8; k++) { if (((font_ptr[i * word_bytes + j] >> (7 - k)) & 0x01) != 0 && (rect->x1 + 8 * j + k < rect->x2)) { rtgui_dc_draw_point(dc, rect->x1 + 8 * j + k, rect->y1 + i); } else if (style & RTGUI_TEXTSTYLE_DRAW_BACKGROUND) { rtgui_dc_draw_color_point(dc, rect->x1 + 8 * j + k, rect->y1 + i, bc); } } } /* move x to next character */ rect->x1 += asc_file_font->font_size /2; str += 1; len -= 1; } } static void rtgui_asc_file_font_draw_text(struct rtgui_font *font, struct rtgui_dc *dc, const char *text, rt_ubase_t length, struct rtgui_rect *rect) { rt_uint32_t len; struct rtgui_font *cfont; struct rtgui_asc_file_font *asc_file_font = (struct rtgui_asc_file_font *)font->data; RT_ASSERT(dc != RT_NULL); RT_ASSERT(asc_file_font != RT_NULL); cfont = rtgui_font_refer("asc", font->height); if (cfont == RT_NULL) cfont = rtgui_font_default(); /* use system default font */ while (length > 0) { len = 0; while (((rt_uint8_t) * (text + len)) >= 0x80 && len < length) len ++; /* draw text with Chinese font */ if (len > 0) { rtgui_font_draw(cfont, dc, text, len, rect); text += len; length -= len; } len = 0; while (((rt_uint8_t) * (text + len)) < 0x80 && *(text + len) && len < length) len ++; if (len > 0) { _rtgui_asc_file_font_draw_text(asc_file_font, dc, text, len, rect); text += len; length -= len; } } rtgui_font_derefer(cfont); } static void rtgui_asc_file_font_get_metrics(struct rtgui_font *font, const char *text, rtgui_rect_t *rect) { struct rtgui_asc_file_font *asc_file_font = (struct rtgui_asc_file_font *)font->data; RT_ASSERT(asc_file_font != RT_NULL); /* set metrics rect */ rect->x1 = rect->y1 = 0; rect->x2 = (rt_int16_t)(asc_file_font->font_size / 2 * rt_strlen((const char *)text)); rect->y2 = asc_file_font->font_size; } #endif ``` 在font.h里添加定义: ``` struct asc_cache { SPLAY_ENTRY(asc_cache) asc_node; rt_uint8_t asc_id; }; struct rtgui_asc_file_font { struct asc_cache_tree cache_root; rt_uint16_t cache_size; /* font size */ rt_uint16_t font_size; rt_uint16_t font_data_size; /* file descriptor */ int fd; /* font file name */ const char *font_fn; }; extern const struct rtgui_font_engine rtgui_asc_file_font_engine; ``` 这样系统就支持SD卡的asc字库了。 使用时仿照汉字字库的使用方法哈 例如 高度为24号的asc字符定义: ``` struct rtgui_asc_file_font ascii24 = { {RT_NULL}, /* cache root */ 0, /* cache size */ 24, /* font size */ 48, /* font data size */ -1, /* fd */ "/resource/ascii24.fnt" /* font_fn */ }; struct rtgui_font rtgui_font_ascii24 = { "asc", /* family */ 24, /* height */ 1, /* refer count */ &rtgui_asc_file_font_engine, /* font engine */ (void *) &ascii24, /* font private data */ }; ``` 添加字体字库 ``` rtgui_font_system_add_font(&rtgui_font_ascii24); ``` 这样只需要是24高度的ASC,就会用它显示了。 关于字库的制作参照 [http://www.rt-thread.org/phpBB3/viewtopic.php?f=3&t=1095](http://www.rt-thread.org/phpBB3/viewtopic.php?f=3&t=1095) 在最后生成的时候选择生成二进制字库文件即可,后缀是没有任何影响的。
查看更多
7
个回答
默认排序
按发布时间排序
bernard
2013-01-21
这家伙很懒,什么也没写!
这个很好啊,又多了一种选择方式。
xlog
2013-03-01
这家伙很懒,什么也没写!
font.h少了一行 ``` SPLAY_HEAD(asc_cache_tree, asc_cache); ``` 这个贴的代码建议加入到RTGUI中。
xlog
2013-03-01
这家伙很懒,什么也没写!
经测试 190 cfont = rtgui_font_refer("asc", font->height); 应该修改为cfont = rtgui_font_refer("hz", font->height);
xiao苦
2013-03-13
这家伙很懒,什么也没写!
其实可以将16的字体放大成32或者48,不就有大字体了吗?
hejiang177
2013-04-03
这家伙很懒,什么也没写!
好东西,强烈建议加入到GUI的字体引擎去,这样开发人员想同时用几种字体都行,只要有字库。
nongxiaoming
2013-04-14
rt-thread大师兄
这个不错~~
撰写答案
登录
注册新账号
关注者
0
被浏览
4.7k
关于作者
郁海难填
这家伙很懒,什么也没写!
提问
4
回答
4
被采纳
0
关注TA
发私信
相关问题
1
有关动态模块加载的一篇论文
2
最近的调程序总结
3
晕掉了,这么久都不见layer2的踪影啊
4
继续K9ii的历程
5
[GUI相关] FreeType 2
6
[GUI相关]嵌入式系统中文输入法的设计
7
20081101 RT-Thread开发者聚会总结
8
嵌入式系统基础
9
linux2.4.19在at91rm9200 上的寄存器设置
10
[转]基于嵌入式Linux的通用触摸屏校准程序
推荐文章
1
RT-Thread应用项目汇总
2
玩转RT-Thread系列教程
3
国产MCU移植系列教程汇总,欢迎查看!
4
机器人操作系统 (ROS2) 和 RT-Thread 通信
5
五分钟玩转RT-Thread新社区
6
【技术三千问】之《玩转ART-Pi》,看这篇就够了!干货汇总
7
关于STM32H7开发板上使用SDIO接口驱动SD卡挂载文件系统的问题总结
8
STM32的“GPU”——DMA2D实例详解
9
RT-Thread隐藏的宝藏之completion
10
【ART-PI】RT-Thread 开启RTC 与 Alarm组件
最新文章
1
基于RT-Thread的STM32F4开发第三讲——DAC
2
Wireshark抓包EtherCAT报文
3
RISC-V hardfault分析工具,RTTHREAD-RVBACKTRACE 原理讲解
4
基于RT-Thread的STM32G4开发第二讲第二篇——ADC
5
基于RT-Thread的STM32F4开发第二讲第一篇——ADC
热门标签
RT-Thread Studio
串口
Env
LWIP
SPI
AT
Bootloader
Hardfault
CAN总线
FinSH
ART-Pi
DMA
USB
文件系统
RT-Thread
SCons
RT-Thread Nano
线程
MQTT
STM32
FAL
RTC
rt-smart
I2C_IIC
cubemx
UART
ESP8266
WIZnet_W5500
BSP
ota在线升级
PWM
flash
packages_软件包
freemodbus
潘多拉开发板_Pandora
ADC
GD32
定时器
编译报错
flashDB
keil_MDK
socket
中断
rt_mq_消息队列_msg_queue
Debug
ulog
SFUD
msh
C++_cpp
at_device
本月问答贡献
出出啊
1524
个答案
343
次被采纳
小小李sunny
1444
个答案
290
次被采纳
张世争
818
个答案
179
次被采纳
crystal266
555
个答案
162
次被采纳
whj467467222
1222
个答案
149
次被采纳
本月文章贡献
出出啊
1
篇文章
1
次点赞
小小李sunny
1
篇文章
1
次点赞
张世争
1
篇文章
2
次点赞
crystal266
2
篇文章
1
次点赞
whj467467222
2
篇文章
1
次点赞
回到
顶部
发布
问题
分享
好友
手机
浏览
扫码手机浏览
投诉
建议
回到
底部