发布时间:2025-12-10 19:36:24 浏览次数:6
touchGFX综合学习六、touchGFX使用FontCache字体缓存功能实现直接从外部存储介质显示字符串[亲测有效]touchGFX使用FontCache字体缓存功能实现直接从外部存储介质显示字符串
参考这一篇文件
将生成的文件拷贝到sd卡
FontReader.hpp和FontReader.cpp
FontReader.hpp
/* * Copyright (c) 2006-2021, RT-Thread Development Team * * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes * 2022-10-17 01417804 the first version */#ifndef _FONTREADER_HPP_#define _FONTREADER_HPP_#include <fonts/FontCache.hpp>#include <unistd.h>#include <fcntl.h>#include "ff.h"#include "stdio.h"using namespace touchgfx;class FontReader :public FontDataReader{ public: FontReader(); ~FontReader(); virtual void open(); virtual void close(); virtual void setPosition(uint32_t position); virtual void readData(void* out, uint32_t numberOfBytes);private: FILE* fp;};#endif /* LIBRARIES_TOUCHGFX_LIB_TOUCHGFX_GUI_INCLUDE_GUI_COMMON_FONTREADER_HPP_ */ 是否还在为Ide开发工具频繁失效而烦恼,来吧关注以下公众号获取最新激活方式。亲测可用!
【正版授权,激活自己账号】:Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】:官方授权 正版激活 自己使用,支持Jetbrains家族下所有IDE…
FontReader.cpp
/* * Copyright (c) 2006-2021, RT-Thread Development Team * * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes * 2022-10-17 01417804 the first version */#include <gui/common/FontReader.hpp>int fd;extern "C" void open_file(){ fd = open("/sdcard/touchgfx/font.bin", O_RDONLY); if(fd<0) rt_kprintf("open failed!\r\n"); else { rt_kprintf("open success!\r\n"); }}extern "C" void close_file(){ close(fd);}FontReader::FontReader(){ }FontReader::~FontReader(){ }void FontReader::open(){ open_file();}void FontReader::close(){ close_file();}void FontReader::setPosition(uint32_t position){ rt_kprintf("lseek:%ld!\r\n",lseek(fd, position, SEEK_SET));}void FontReader::readData(void* out, uint32_t numberOfBytes){ read(fd, out,numberOfBytes); rt_kprintf("read:%d!\r\n",numberOfBytes);} 这两个函数是将open和close封装了一下
extern "C" void open_file()extern "C" void close_file() 在FontDataReader类中下面四个函数为纯虚函数,所以子函数必须要实现
virtual void open() = 0; virtual void close() = 0; virtual void setPosition(uint32_t position) = 0; virtual void readData(void* out, uint32_t numberOfBytes) = 0; #ifndef SCREEN1VIEW_HPP#define SCREEN1VIEW_HPP#include <gui_generated/screen1_screen/Screen1ViewBase.hpp>#include <gui/screen1_screen/Screen1Presenter.hpp>#include <touchgfx/Font.hpp>#include <fonts/GeneratedFont.hpp>#include <touchgfx/Font.hpp>#include <fonts/ApplicationFontProvider.hpp>#include <texts/TypedTextDatabase.hpp>#include <fonts/CachedFont.hpp>#include <texts/TextKeysAndLanguages.hpp>#include <gui/common/FontReader.hpp>using namespace touchgfx;class Screen1View : public Screen1ViewBase{ public: Screen1View(); virtual ~Screen1View() { } virtual void setupScreen(); virtual void tearDownScreen(); void cache_font();protected: uint8_t fontData[5120]; //字符缓存区,缓存12个字符 FontCache fontCache; CachedFont cacheFont; FontReader fontReader;};#endif // SCREEN1VIEW_HPP 添加相关头文件和定义相关变量
#include <gui/screen1_screen/Screen1View.hpp>//0x4e00-0x9fa5,0x0000-0x007fScreen1View::Screen1View(){ }void Screen1View::setupScreen(){ Screen1ViewBase::setupScreen(); this->cache_font(); //显示字符串 Unicode::snprintf(this->textArea1Buffer,this->TEXTAREA1_SIZE,"%s","e"); this->textArea1.resizeToCurrentText(); this->textArea1.invalidate();}void Screen1View::tearDownScreen(){ Screen1ViewBase::tearDownScreen();}void Screen1View::cache_font(){ fontCache.setMemory(this->fontData,sizeof(this->fontData)); fontCache.setReader(&this->fontReader); TypedText text = TypedText(T___SINGLEUSE_W0UD); this->fontCache.initializeCachedFont(text, &this->cacheFont); TypedTextDatabase::setFont(Typography::TEST,&this->cacheFont); //缓存字符数据 //第一种方法 //this->fontCache.cacheString(text, (Unicode::UnicodeChar*)"0123456789"); //第二种方法 Unicode::UnicodeChar* str = (Unicode::UnicodeChar*)this->textArea2.getTypedText().getText(); fontCache.cacheString(text,str);} cache_font()函数加载字符,将用到的字符加载到fontData数组中。
关于fontData[8192]大小的计算8192/20*40 = 10.24大概20个中文字符
//显示字符串 Unicode::snprintf(this->textArea1Buffer,this->TEXTAREA1_SIZE,"%s","e"); this->textArea1.resizeToCurrentText(); this->textArea1.invalidate(); 此处显示的字符只能为cache_font()中加载的字符。