Following the official example, I rewrote this code and have resolved the issue.
D2D DWrite encountered error 0x88985001 while loading a custom font. Repeated debugging attempts failed to pinpoint the issue. Please help me.
I am attempting to add a TTC font to an IDWriteFontCollection and have chosen to register a custom loader using RegisterFontCollectionLoader(collectionLoader);. The font file is located within a resource file (.rc).
The font file format is .ttf, but DWrite recognizes it as a TTC font (hence the design in my code to handle this), and this TTC font contains only one font face (meaning a single font).
I simplified and created a standalone test project containing extensive debugging output, but I still can't pinpoint the issue. When loaded directly from its actual local path, this font file loads correctly (indicating the font itself is not the problem).
I have placed my test sample in cloud storage. It contains the entire solution project (code, fonts, etc.), making it convenient for you to download and test.
Below is a portion of my code (as it's too lengthy), where I've included only an example of the loading process.
INT numFound = 0;
HRSRC hRes = FindResource(NULL, MAKEINTRESOURCE(101), L"TTF");
HGLOBAL hMem = LoadResource(NULL, hRes);
void* pLock = LockResource(hMem);
DWORD dwSize = SizeofResource(NULL, hRes);
BYTE* alignedData = static_cast<BYTE*>(_aligned_malloc(dwSize, 16));
if (!alignedData) cout << "alignedData malloc failed!" << endl;
memcpy(alignedData, pLock, dwSize);
auto fileLoader = new IdtFontFileLoader();
auto collectionLoader = new IdtFontCollectionLoader(D2DTextFactory, fileLoader);
D2DTextFactory->RegisterFontFileLoader(fileLoader);
D2DTextFactory->RegisterFontCollectionLoader(collectionLoader);
IDWriteFontCollection* tempFontCollection = nullptr;
HRESULT hr = D2DTextFactory->CreateCustomFontCollection(collectionLoader, alignedData, dwSize, &tempFontCollection);
Meanwhile, DWrite can correctly identify the font's properties but fails to load the font itself. My previous code successfully loaded TTF (single-font-face fonts), but I later made modifications to this TTC font (which suggests my code issue shouldn't be too significant).
IDWriteFontFile* fontFile = nullptr;
hr = D2DTextFactory->CreateCustomFontFileReference(
pLock, dwSize,
fileLoader,
&fontFile
);
BOOL supported = FALSE;
DWRITE_FONT_FILE_TYPE fileType;
DWRITE_FONT_FACE_TYPE faceType;
UINT32 faceCount = 0;
fontFile->Analyze(
&supported,
&fileType,
&faceType,
&faceCount
);
printf(
"HarmonyOS Sans Analyze: supported=%d fileType=%u faceType=%u faceCount=%u\n",
supported, fileType, faceType, faceCount
);
Code output obtained
IdtFontFileStream::GetFileSize CALLED
IdtFontFileStream::ReadFileFragment CALLED: Offset = 0, Size = 117
IdtFontFileStream::ReadFileFragment CALLED: Offset = 0, Size = 7
IdtFontFileStream::ReadFileFragment CALLED: Offset = 0, Size = 16
HarmonyOS Sans Analyze: supported=1 fileType=2 faceType=1 faceCount=1
I've been working on this problem for six hours now. Please help me out. I'd be extremely grateful!