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);
}

运行模拟器

相关推荐
ARM+FPGA+AI工业主板定制专家1 天前
基于ARM+FPGA+AI的船舶状态智能监测系统(二)软硬件设计,模拟量,温度等采集与分析
arm开发·人工智能·目标检测·fpga开发
梅尔文.古1 天前
ADCU-Ethernet-以太网在AUTOSAR与Linux架构下对比
arm开发·单片机·汽车
szxinmai主板定制专家1 天前
基于ZYNQ MPSOC船舶数据采集仪器设计(一)总体设计方案,包括振动、压力、温度、流量等参数
arm开发·人工智能·嵌入式硬件·fpga开发
坤坤藤椒牛肉面1 天前
ARM基础内容
arm开发
szxinmai主板定制专家1 天前
基于ZYNQ MPSOC船舶数据采集仪器设计(三)振动,流量,功耗,EMC,可靠性测试
arm开发·人工智能·嵌入式硬件·fpga开发
忆和熙1 天前
ARMv8异常级别与执行状态的转换机制(ARMv8异常机制——异常处理中,执行状态与异常级别的转换机制)
arm开发·arm异常
ARM+FPGA+AI工业主板定制专家2 天前
基于ARM+FPGA+AI的船舶状态智能监测系统(一)总体设计
网络·arm开发·人工智能·机器学习·fpga开发·自动驾驶
梦..2 天前
Allegro学习记录(一)
arm开发·单片机·嵌入式硬件·学习·硬件架构·硬件工程·pcb工艺
EnglishJun2 天前
ARM嵌入式学习(七)--- 汇编基础(数据指令、函数调用原理、中断原理)
arm开发·学习
Nan_Feng_ya2 天前
基于STM32的智能手表复刻成功(完全开源)
arm开发·stm32·pcb工艺·智能手表