2012-07-08 32 views
5

Actualmente estoy implementando FreeType 2 en un proyecto mío y, por lo tanto, necesito los filepaths para Fonts en mi sistema. Necesito una solución que solo tome el nombre de la fuente y el estilo de fuente deseado (por ejemplo, negrita o cursiva) y luego devuelve la ruta de archivo de la fuente.Obtener una ruta de archivo de fuente desde el nombre y el estilo en C++/Windows

Ya probé la respuesta de este Stack Overflow question, pero no funciona en Windows 7 (y probablemente tampoco en Vista), pero obviamente necesito una solución que funcione en estos sistemas y sistemas futuros también.

+0

No existe tal API, al igual que la respuesta vinculada dice. –

+1

Si tiene el manejador HFONT de la fuente, podría probar mi [solución propuesta] (http://stackoverflow.com/questions/16769758/get-a-font-file-filename-based-on-the-font-handle- hfont). No quería agregar la respuesta aquí, porque la pregunta no está clara si tiene un control para la fuente o no. –

Respuesta

5

¿Puedo preguntar por qué necesita la ruta a un archivo físico?

7

Una vez escribí el código para la plataforma de Windows para encontrar un archivo de fuente basado en "Arial Bold" o tal nombre. El código se publica a continuación. Está escaneando el Registro e intentando encontrar una coincidencia para el nombre de la fuente a un archivo en el directorio de fuentes de Windows. Puede que no sea a prueba de balas, pero sí funcionó. Una vez que tenga el nombre del archivo, puede pasarlo a FreeType.

// Get system font file path 
std::string GetSystemFontFile(const std::string &faceName) { 

    static const LPWSTR fontRegistryPath = L"Software\\Microsoft\\Windows NT\\CurrentVersion\\Fonts"; 
    HKEY hKey; 
    LONG result; 
    std::wstring wsFaceName(faceName.begin(), faceName.end()); 

    // Open Windows font registry key 
    result = RegOpenKeyEx(HKEY_LOCAL_MACHINE, fontRegistryPath, 0, KEY_READ, &hKey); 
    if (result != ERROR_SUCCESS) { 
    return ""; 
    } 

    DWORD maxValueNameSize, maxValueDataSize; 
    result = RegQueryInfoKey(hKey, 0, 0, 0, 0, 0, 0, 0, &maxValueNameSize, &maxValueDataSize, 0, 0); 
    if (result != ERROR_SUCCESS) { 
    return ""; 
    } 

    DWORD valueIndex = 0; 
    LPWSTR valueName = new WCHAR[maxValueNameSize]; 
    LPBYTE valueData = new BYTE[maxValueDataSize]; 
    DWORD valueNameSize, valueDataSize, valueType; 
    std::wstring wsFontFile; 

    // Look for a matching font name 
    do { 

    wsFontFile.clear(); 
    valueDataSize = maxValueDataSize; 
    valueNameSize = maxValueNameSize; 

    result = RegEnumValue(hKey, valueIndex, valueName, &valueNameSize, 0, &valueType, valueData, &valueDataSize); 

    valueIndex++; 

    if (result != ERROR_SUCCESS || valueType != REG_SZ) { 
     continue; 
    } 

    std::wstring wsValueName(valueName, valueNameSize); 

    // Found a match 
    if (_wcsnicmp(wsFaceName.c_str(), wsValueName.c_str(), wsFaceName.length()) == 0) { 

     wsFontFile.assign((LPWSTR)valueData, valueDataSize); 
     break; 
    } 
    } 
    while (result != ERROR_NO_MORE_ITEMS); 

    delete[] valueName; 
    delete[] valueData; 

    RegCloseKey(hKey); 

    if (wsFontFile.empty()) { 
    return ""; 
    } 

    // Build full font file path 
    WCHAR winDir[MAX_PATH]; 
    GetWindowsDirectory(winDir, MAX_PATH); 

    std::wstringstream ss; 
    ss << winDir << "\\Fonts\\" << wsFontFile; 
    wsFontFile = ss.str(); 

    return std::string(wsFontFile.begin(), wsFontFile.end()); 
} 
Cuestiones relacionadas