Can one get a list of fonts available for text annotations in DM scripting?

允我心安 提交于 2021-02-11 07:25:28

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/41106639/can-one-get-a-list-of-fonts-available-for-text-annotations-in-dm-scripting

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!