TouchGFX之字体缓存

使用二进制字体需要将整个字体加载到存储器。 在某些情况下,如果字体很大,如大字号中文字体,则这样做可能不可取。

字体缓存使应用能够从外部存储器只能加载显示字符串所需的字母。 这意味着整个字体无需保存到在可寻址闪存或RAM上,而是只需保存在更大的文件系统上。

字体文件阅读器

cpp 复制代码
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:

cpp 复制代码
#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);
}

运行模拟器

相关推荐
szxinmai主板定制专家7 小时前
RK3568 + CODESYS+实时系统运动控制器PLC,支持 AI 视觉目标检测,预测性维护,混合多系统部署,多路模拟量采集
arm开发·人工智能·嵌入式硬件·fpga开发
XMAIPC_Robot12 小时前
深度无人机自动驾驶仪,中小型无人机硬件在环仿真飞行
运维·arm开发·人工智能·fpga开发·无人机·边缘计算
番茄灭世神14 小时前
Vscode开发/调试ARM单片机最新教程
c语言·arm开发·vscode·stm32·嵌入式·gd32
猫猫的小茶馆1 天前
【Python】函数与模块化编程
linux·开发语言·arm开发·驱动开发·python·stm32
霞姐聊IT1 天前
x86程序移植到arm平台的四种场景及解决方案简介
arm开发
楼兰公子1 天前
《深入理解Linux网络技术内幕》配套学习大纲 + 源码Demo + 进阶实战实例
linux·arm开发·学习
lljss20202 天前
Arm GNU 工具链 命名规则
服务器·arm开发·gnu
底层开发智库2 天前
无需硬件开发板,从零构建并运行ARM aemfvp-a-rme软件栈
arm开发·arm
HMS工业网络3 天前
主从结合,安全互联:Anybus工业通信解决方案全栈升级
arm开发
XINVRY-FPGA3 天前
XC7Z010-2CLG400I Xilinx Zynq-7000 FPGA
arm开发·嵌入式硬件·算法·fpga开发·硬件工程·dsp开发·fpga