1

One can create text annotations on image displays via the function NewTextAnnotation and then change the font via the method ComponentSetFontFaceName. However to do so successfully, one must provide the full name of the desired font as a string and one must already know that it is available on the current system. If one specifies an unavailable font, some default font seems to be chosen and no exception or error message is posted.

Is there any way to get a list of available fonts within a DM script or to determine whether a specific named font is actually available?

Mike Kundmann
  • 705
  • 5
  • 12
  • Good question! I'm not aware of any scripting command doing what you need, but as far as I know, the list of "available" fonts is the same as if you right-click a text-annotation and check the fonts drop-down. The list is populated by the OS. – BmyGuest Dec 12 '16 at 19:08
  • 1
    If you need to get to the list systematically, you might be able to contruct yourself an auxiliary method using the LaunchExternal() command together with f.e. powershell. This might be a [useful read](http://superuser.com/a/760632/544823) – BmyGuest Dec 12 '16 at 20:55
  • Many thanks for the great tip. I will look into this. – Mike Kundmann Dec 12 '16 at 21:02
  • Another maybe obvious thing: You can easily combine `ComponentSetFontFaceName` and `ComponentGetFontFaceName` to verify that setting a font was successful and act accordingly. But I'm sure you thought of that yourself ;c) – BmyGuest Dec 12 '16 at 22:06
  • Yes, I tried this, but it seems that it is possible to change the nominal font face of a text annotation even if that font is not available. DM simply renders any such text annotation in a default font, which maybe the first one in the system list or always Arial. – Mike Kundmann Dec 12 '16 at 23:05

1 Answers1

2

Interesting task!

There is no actual script command to do this, and the list of installed font-names is populated by the OS.

However, using Powershell and the script command LaunchExternal() one can construct a workaround.

After some trial and error, I think I got it working by the following script:

void WriteFontListToDisk( string fileName )
{
    String PSscript
    PSscript += "[System.Reflection.Assembly]::LoadWithPartialName('System.Drawing');"
    PSscript += "(New-Object System.Drawing.Text.InstalledFontCollection).Families "
    PSscript += " | out-file -encoding ASCII " + fileName           // Need to specify ASCII here!

    String callString
    callString += "powershell"
    //callString += " -NoExit"          // Keep Powershell open
    callString += " -Command &{ "
    callString += PSscript
    callString += " }"
    LaunchExternalProcess( callString, 5 )
}

TagGroup ReadFontListFromFile( string fileName )
{
    TagGroup tg = NewTagList()
    if ( !DoesFileExist( fileName ) ) Throw( "Font list file not found:\n" + fileName )
    number fileID = OpenFileForReading( fileName )
    object fileStream = NewStreamFromFileReference( fileID, 1 )
    result("\n SIZE:" + fileStream.StreamGetSize()  )
    // Output format is
    //  #1:(empty)
    //  #2: Name                                                                           
    //  #3: ----
    //  #4+: FontNames
    string line
    for( number i=0;i<3;i++) fileStream.StreamReadTextLine( 0, line )

    number inc = 0
    while( fileStream.StreamGetPos() != fileStream.StreamGetSize() )
    {
        if ( !fileStream.StreamReadTextLine( 0, line ) ) break;
        tg.TagGroupInsertTagAsString( Infinity(), line )
        if ( ShiftDown() ) exit(0)
    }

    return tg
}

TagGroup GetFontList()
{
    TagGroup tg = NewTagGroup()
    string fileName = "C:\\FontNamesList.txt"
    if ( DoesFileExist( fileName ) ) DeleteFile( fileName )
    WriteFontListToDisk( fileName )
    tg = ReadFontListFromFile( fileName )
    if ( DoesFileExist( fileName ) ) DeleteFile( fileName )
    return tg
}

GetFontList().TagGroupOpenBrowserWindow( "Fonts" , 0 )

It is worthwhile to note that PowerShell by default streams text output as UNICODE and that does not work well with text-import in DM. However, this question was helpful, and the script above sets the output to ASCII. One issue though is, that in doing so some characters might be lost and it might be needed to carefully check the output font-list.

Community
  • 1
  • 1
BmyGuest
  • 6,331
  • 1
  • 21
  • 35