本文主要是介绍MFC/QT利用COM组件接口从字体名称、粗体、斜体获取到字体文件路径的方法,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
MFC/QT利用COM组件接口从字体名称、粗体、斜体获取到字体文件路径的方法
这个是目前唯一能准确从字体名称+加粗+斜体三个属性定位到ttf/ttc字体路径的方法,其他的方式是没有办法区分后两个属性而且会出现不准确的问题
之前无意间在MSDN找到的资料:
IDWriteLocalFontFileLoader::GetFilePathFromKey (dwrite.h) - Win32 apps | Microsoft Learn
#include <iostream>
#include <dwrite.h>
#include <list>
#pragma comment(lib, "dwrite.lib")using namespace std;list<WCHAR*> get_fonts_path(WCHAR* family_name, BOOL is_bold, BOOL is_italic)
{list<WCHAR*> fonts_filename_list;HRESULT hr;IDWriteFactory* dwrite_factory;hr = DWriteCreateFactory(DWRITE_FACTORY_TYPE_ISOLATED, __uuidof(IDWriteFactory), reinterpret_cast<IUnknown**>(&dwrite_factory));if (FAILED(hr)){return fonts_filename_list;}IDWriteGdiInterop* gdi_interop;hr = dwrite_factory->GetGdiInterop(&gdi_interop);if (FAILED(hr)){dwrite_factory->Release();return fonts_filename_list;}LOGFONT lf;memset(&lf, 0, sizeof(lf));wcscpy_s(lf.lfFaceName, LF_FACESIZE, family_name);lf.lfWeight = is_bold ? FW_BOLD : FW_REGULAR;lf.lfItalic = is_italic;lf.lfCharSet = DEFAULT_CHARSET;lf.lfOutPrecision = OUT_TT_PRECIS;lf.lfClipPrecision = CLIP_DEFAULT_PRECIS;lf.lfQuality = ANTIALIASED_QUALITY;lf.lfPitchAndFamily = DEFAULT_PITCH | FF_DONTCARE;IDWriteFont* matching_font;hr = gdi_interop->CreateFontFromLOGFONT(&lf, &matching_font);if (FAILED(hr)){gdi_interop->Release();dwrite_factory->Release();return fonts_filename_list;}IDWriteFontFace* font_face;hr = matching_font->CreateFontFace(&font_face);if (FAILED(hr)){matching_font->Release();gdi_interop->Release();dwrite_factory->Release();return fonts_filename_list;}UINT file_count;hr = font_face->GetFiles(&file_count, NULL);if (FAILED(hr)){font_face->Release();matching_font->Release();gdi_interop->Release();dwrite_factory->Release();return fonts_filename_list;}IDWriteFontFile** font_files = new IDWriteFontFile * [file_count];hr = font_face->GetFiles(&file_count, font_files);if (FAILED(hr)){font_face->Release();matching_font->Release();gdi_interop->Release();dwrite_factory->Release();return fonts_filename_list;}for (int i = 0; i < file_count; i++){LPCVOID font_file_reference_key;UINT font_file_reference_key_size;hr = font_files[i]->GetReferenceKey(&font_file_reference_key, &font_file_reference_key_size);if (FAILED(hr)){font_files[i]->Release();continue;}IDWriteFontFileLoader* loader;hr = font_files[i]->GetLoader(&loader);if (FAILED(hr)){font_files[i]->Release();continue;}IDWriteLocalFontFileLoader* local_loader;hr = loader->QueryInterface(__uuidof(IDWriteLocalFontFileLoader), (void**)&local_loader);if (FAILED(hr)){loader->Release();font_files[i]->Release();continue;}UINT32 path_length;hr = local_loader->GetFilePathLengthFromKey(font_file_reference_key, font_file_reference_key_size, &path_length);if (FAILED(hr)){local_loader->Release();loader->Release();font_files[i]->Release();continue;}WCHAR* path = new WCHAR[path_length + 1];hr = local_loader->GetFilePathFromKey(font_file_reference_key, font_file_reference_key_size, path, path_length + 1);if (FAILED(hr)){local_loader->Release();loader->Release();font_files[i]->Release();continue;}fonts_filename_list.push_back(path);local_loader->Release();loader->Release();font_files[i]->Release();}font_face->Release();matching_font->Release();gdi_interop->Release();dwrite_factory->Release();return fonts_filename_list;
}int main() {list<WCHAR*> fonts_filename_list = get_fonts_path(const_cast<WCHAR*>(L"Arial"), false, true);for (WCHAR* font_filename : fonts_filename_list){wcout << L"Font filename: " << font_filename << endl;}return 0;
}
这篇关于MFC/QT利用COM组件接口从字体名称、粗体、斜体获取到字体文件路径的方法的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!