TouchGFX之字体缓存
作者:小教学发布时间:2023-10-04分类:程序开发学习浏览:583
导读:使用二进制字体需要将整个字体加载到存储器。在某些情况下,如果字体很大,如大字号中文字体,则这样做可能不可取。字体缓存使应用能够从外部存储器只能加载显示字符串所需的字母。这意...
使用二进制字体需要将整个字体加载到存储器。 在某些情况下,如果字体很大,如大字号中文字体,则这样做可能不可取。
字体缓存使应用能够从外部存储器只能加载显示字符串所需的字母。 这意味着整个字体无需保存到在可寻址闪存或RAM上,而是只需保存在更大的文件系统上。
字体文件阅读器
FileDataReader.hpp
#ifndef FILEDATAREADER_HPP
#define FILEDATAREADER_HPP
#include <fonts/FontCache.hpp>
#include <stdio.h>
using namespace touchgfx;
class FileDataReader : public FontDataReader
{
public:
virtual ~FileDataReader() { }
virtual void open()
{
fp = fopen("generated/fonts/bin/Font_verdana_20_4bpp.bin", "rb");
}
virtual void close()
{
fclose(fp);
}
virtual void setPosition(uint32_t position)
{
fseek(fp, position, SEEK_SET);
}
virtual void readData(void* out, uint32_t numberOfBytes)
{
fread(out, numberOfBytes, 1, fp);
}
private:
FILE* fp;
};
#endif // FRONTENDAPPLICATION_HPP
创建FontCache、存储缓冲区和文件系统阅读器对象,然后安装CachedFont:
#include <gui/common/FrontendApplication.hpp>
#include <BitmapDatabase.hpp>
#include <texts/TextKeysAndLanguages.hpp>
#include <texts/TypedTextDatabase.hpp>
#include <gui/common/FileDataReader.hpp>
#include <fonts/CachedFont.hpp>
#include <fonts/FontCache.hpp>
uint8_t fontdata[5120];
FileDataReader reader;
FontCache fontCache;
CachedFont cachedFont; //Cached Font object
LOCATION_PRAGMA_NOLOAD("TouchGFX_Cache")
uint16_t Cache[1024 * 604] LOCATION_ATTRIBUTE_NOLOAD("TouchGFX_Cache");
FrontendApplication::FrontendApplication(Model& m, FrontendHeap& heap)
: FrontendApplicationBase(m, heap)
{
#ifdef SIMULATOR
const uint32_t cacheSize = 0x300000; //3 MB, as example
uint16_t* const cacheStartAddr = (uint16_t*)malloc(cacheSize);
Bitmap::setCache(cacheStartAddr, cacheSize, 4);
#else
Bitmap::setCache(Cache, sizeof(Cache));
#endif
//setup the font cache with buffer and size; and file reader object
fontCache.setMemory(fontdata, sizeof(fontdata));
fontCache.setReader(&reader);
TypedText text = TypedText(T___SINGLEUSE_2OJQ);
fontCache.initializeCachedFont(text, &cachedFont);
//replace the linked in font in TouchGFX with cachedFont
TypedTextDatabase::setFont(Typography::DEFAULT, &cachedFont);
Unicode::UnicodeChar* str = const_cast<Unicode::UnicodeChar*>(text.getText());
fontCache.cacheString(text, str);
}
运行模拟器

- 上一篇:[echarts]柱状图的点击事件
- 下一篇:好看的货架效果(含3D效果)
- 程序开发学习排行
- 最近发表
-
- WordPress 应用:如何修改模版头部文件中的 wp_head() - Web前端
- SEO 优化:YouTube SEO Plugin 提升影片流量 - Web前端
- 使用 React 服务器组件在 Next.js 13 中进行国际化 - Web前端
- 3 个意想不到的技术 SEO 问题示例(以及如何解决它们) - Web前端
- Vue 技术专题:Suspense、路由模式与单元测试实践 - Web前端
- 如何防止 WordPress SQL 注入攻击? - Web前端
- SEO 优化:如何围绕技术SEO框架以构建和制定策略? - Web前端
- SEO优化:YouTube SEO 关键词研究工具 - Web前端
- 2025 年帮助代理机构提升客户业绩的 14 款最佳 SEO 工具 - Web前端
- 客户端:服务器之间设置本地 DNS 服务器 - Web前端


