[Font] Getting font folder on all platforms

Flamaros flamaros.xavier at gmail.com
Tue Oct 15 16:10:30 PDT 2013


On Friday, 6 September 2013 at 20:54:53 UTC, Flamaros wrote:
> On Friday, 6 September 2013 at 16:05:43 UTC, Tourist wrote:
>> On Thursday, 5 September 2013 at 19:48:07 UTC, Flamaros wrote:
>>> I am searching the right way to find fonts folder for each 
>>> platforms (Windows, linux, macOS X)
>>>
>>> On Windows it's generally "C:\Windows\Fonts" but a direct 
>>> access seems brutal, it's certainly expected to retrieve this 
>>> path by using some register keys?
>>>
>>> Is someone know how it works for linux and/or macOS X?
>>>
>>> I need to be able to retrieve fastest as possible the right 
>>> file from the font and family name.
>>
>> Windows: call SHGetKnownFolderPath with FOLDERID_Fonts as rfid.
>>
>> http://msdn.microsoft.com/en-us/library/windows/desktop/bb762188%28v=vs.85%29.aspx
>
> Nice, thx.
>
> Do you know if there is a table of fonts and there family, or 
> need open all font file my self?

I need to do some more tests, but scanning the registry seems 
working under Windows.
Here is my test code :

string	fontPathFromName(in string name, in Font.Family family = 
Font.Family.Regular)
{
     version(Windows)
     {
         import std.windows.registry;

         string	fontPath = "C:/Windows/Fonts/";
         string	fontFileName;
         Key		fontKey;

         fontKey = 
Registry.localMachine().getKey("Software\\Microsoft\\Windows 
NT\\CurrentVersion\\Fonts");

         if (family == Font.Family.Regular)
             fontFileName = fontKey.getValue(name ~ " 
(TrueType)").value_EXPAND_SZ();
         else if (family == Font.Family.Bold)
             fontFileName = fontKey.getValue(name ~ " Bold 
(TrueType)").value_EXPAND_SZ();
         else if (family == Font.Family.Italic)
             fontFileName = fontKey.getValue(name ~ " Italic 
(TrueType)").value_EXPAND_SZ();
         else if (family == (Font.Family.Bold | 
Font.Family.Italic))
             fontFileName = fontKey.getValue(name ~ " Bold Italic 
(TrueType)").value_EXPAND_SZ();
         return fontPath ~ fontFileName;
     }
}

unittest
{
     assert(fontPathFromName("Arial") == 
"C:/Windows/Fonts/arial.ttf");
     assert(fontPathFromName("arial") == 
"C:/Windows/Fonts/arial.ttf");	// Test with wrong case
     assert(fontPathFromName("Arial", Font.Family.Bold | 
Font.Family.Italic) == "C:/Windows/Fonts/arialbi.ttf");
}


More information about the Digitalmars-d-learn mailing list