在新版本的SOUI里render-skia导出了一个新的函数用于字体回退功能。Render_Skia_SetFontFallback
函数原型如下:
EXTERN_C void SOUI_COM_API Render_Skia_SetFontFallback(FontFallback fontFallback);
因为我的工程是使用动态库,这里可以直接获取到这个导出函数来实现字体回退功能。
这里使用一个粗制实现显示藏文的功能
#include <interface/SRender-i.h>
#include <usp10.h>
#include <map>
#pragma comment(lib, "usp10.lib")typedef void (*Render_Skia_SetFontFallback)(FontFallback);class SFontFallback {public:static std::map<wchar_t, std::string> m_FontFallbackList;static HDC fDDC;public:static BOOL FontFallback(LPCSTR u8FontName, wchar_t text, char u8FontNameFallback[100], int *charset){auto ite = m_FontFallbackList.find(text);if (ite != m_FontFallbackList.end()){strcpy_s(u8FontNameFallback, 100, ite->second.c_str());}if (!fDDC){return FALSE;}// Use uniscribe to detemine glyph index for non-BMP characters.static const int numWCHAR = 2;static const int maxItems = 20;// MSDN states that this can be NULL, but some things don't work then.SCRIPT_CONTROL sc = { 0 };// Add extra item to SCRIPT_ITEM to work around a bug (now documented).// https://bugzilla.mozilla.org/show_bug.cgi?id=366643SCRIPT_ITEM si[maxItems + 1];int numItems;if (S_OK != ScriptItemize(&text, numWCHAR, maxItems, &sc, NULL, si, &numItems)){return FALSE;}// Sometimes ScriptShape cannot find a glyph for a non-BMP and returns 2 space glyphs.static const int maxGlyphs = 20;SCRIPT_VISATTR vsa[maxGlyphs];WORD outGlyphs[maxGlyphs];WORD logClust[maxGlyphs];int numGlyphs=0;HFONT fFont = ::CreateFont(0, 0, 0, 0, 0, 0, 0, 0, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH, L"Microsoft Himalaya");if (!fFont){return FALSE;}SelectObject(fDDC, fFont);SCRIPT_CACHE fSC=NULL;if (0 == ScriptShape(fDDC, &fSC, &text, numWCHAR, maxGlyphs, &si[0].a, outGlyphs, logClust, vsa, &numGlyphs)){if (fSC){::ScriptFreeCache(&fSC);}DeleteObject(fFont);m_FontFallbackList[text] = "Microsoft Himalaya";strcpy_s(u8FontNameFallback, 100, "Microsoft Himalaya");return TRUE;}if (fSC){::ScriptFreeCache(&fSC);}DeleteObject(fFont);return FALSE;}SFontFallback(){ HMODULE _hMod=NULL;
#ifdef _DEBUG_hMod = GetModuleHandle(L"render-skiad.dll");
#else_hMod = GetModuleHandle(L"render-skia.dll");
#endif // _DEBUGif (!_hMod)return;Render_Skia_SetFontFallback pRender_Skia_SetFontFallback = (Render_Skia_SetFontFallback)GetProcAddress(_hMod, "Render_Skia_SetFontFallback");if (!pRender_Skia_SetFontFallback){return;}pRender_Skia_SetFontFallback(FontFallback); fDDC = ::CreateCompatibleDC(NULL); }~SFontFallback(){::DeleteDC(fDDC); }
};HDC SFontFallback::fDDC = NULL;
std::map<wchar_t, std::string> SFontFallback::m_FontFallbackList;
然后在合适的地方定义一个SFontFallback nonameFontFallback;即可实现显示藏文功能。使用效果如下图
使用前后对比图(CSDN上传自动加了水印,但是仔细看还是可以看到效果的,不使用回退将显示方框,使用后正确显示)