Get font name in .bat

2

I'm using the script below to install fonts in Windows, but I wonder if there is a way to get the font name from the font. For example, the source file name is BebasNeue.otf , but the name of the face is Bebas Neue (OpenType) . How to get the face name for script to register correctly?

The script is recording the BebasNeue font name in the registry and would like it to save the face name in the Bebas Neue (OpenType) case.

@setlocal enableextensions enabledelayedexpansion
@echo off

echo instalando fontes....

for /f "delims=" %%f in ('dir /b \mulinfsv0005\Configs\fonts\') do (
    set variable=%%f
    set variable=!variable:~0,-4!
    echo !variable!
    xcopy \servidorOrigem\Configs\fonts\%%f c:\Windows\Fonts /y /q
    reg add "HKLM\Software\Microsoft\Windows NT\CurrentVersion\Fonts" /v !variable! /t REG_SZ /d %%f /f
) 
endlocal

echo As fontes foram instaladas!

pause
    
asked by anonymous 06.11.2014 / 10:38

1 answer

2

One solution, but that does not involve just batch is to use a program to extract the source name directly from the file. This can be done with various languages (C, Python and etc). I do not know if there is any way with pure batch that solves this.

Example:

You can use the freetype library to load the source and get the name. To do this, you can use the following code (in C):

#include <stdint.h>

#include <ft2build.h>
#include FT_FREETYPE_H

#include <freetype.h>
#include <ftglyph.h>
#include <ftsnames.h>

int main(int argc, char** argv)
{
    // Verifica se há parâmetros:
    if (argc < 2)
    {
        printf("Nenhum parametro informado.");
        return 0;
    }

    // O arquivo da fonte que se extrairá o nome.
    const char* face_target = argv[1];

    FT_Library  library;

    int error = FT_Init_FreeType(&library);
    if (error)
    {
        // Erro ao carregar biblioteca.
        return -1;
    }

    FT_Face face;
    error = FT_New_Face(library, face_target, 0, &face);

    if (error == FT_Err_Unknown_File_Format)
    {
        // Erro: Provavelmente o arquivo da fonte não é suportado.
        return -1;
    }
    else if (error)
    {
        // Erro: Não foi possível abrir o arquivo ou o arquivo está quebrado.
        return -1;
    }
    else
    {
        // A fonte foi lida com sucesso:

        FT_SfntName name;
        memset(&name, 0, sizeof(FT_SfntName));
        FT_Get_Sfnt_Name(face, 1, &name);

        printf("%.*s", name.string_len, name.string);
    }

    return 0;
}

And generate a program that prints the font name, given its file. Assuming the program is named "font2name" and is accessible, you can call it in Batch like this:

font2name BebasNeue.otf > name.txt
set /p FACE_NAME=< name.txt
echo %FACE_NAME%

As a result, FACE_NAME will store the font name the way you want "Neuba Bebas".

I'm not sure, but I believe the same can be done with python, using freetype for python . But, I do not know if, for what you're doing, you can replace the batch with python.

    
14.11.2014 / 16:24