Given a directory of font files (TTF and OTF) I’d like to inspect each font and determine what style (regular, italic, bold, bold-italic) it is. Is there a command line tool for unix flavored operating systems that can do this? Or does anyone know how to extract the metadata from a TTF or OTF font file?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
I think you’re looking for otfinfo. There doesn’t seem to be an option to get at the Subfamily directly, but you could do:
otfinfo --info *.ttf | grep Subfamily
Note that a number of the fonts I looked at use “Oblique” instead of “Italic”.
Method 2
In Linux, if you have .ttf fonts, you most probably also have fontconfig, which comes with the fc-scan utility. You can parse the output for the information you want, or use the badly documented --format option.
For example:
fc-scan --format "%{foundry} : %{family}n" /usr/share/fonts/truetype/msttcorefonts/arialbd.ttf
The font properties you can print this way are shown here: http://www.freedesktop.org/software/fontconfig/fontconfig-user.html#AEN21
Some properties are listed in multiple languages. For example, %{fullname} may be a list. In that case, %{fullnamelang} will list the languages. If that shows you your language in fourth position in the list, you can use %{fullname[3]} as the format string to print the full name in only that language.
This language stuff being quite inconvenient, I ended up writing a full Perl script to list the info I wanted in only one language:
#!/usr/bin/perl
use strict;
my $VERSION=0.1;
my $debug=1;
my @wanted = qw(foundry family fullname style weight slant width spacing file);
my @lang_dependent = qw(family fullname style);
my $lang = "en";
my $separator = ", ";
use File::Basename;
use Data::Dumper; $Data::Dumper::Sortkeys = 1;
my $me = basename $0;
die "Usage: $me FILENAMEn" unless @ARGV;
my $fontfile = shift;
unless (-f $fontfile) {
die "Bad argument: '$fontfile' is not a file !n";
}
my $fc_format = join( "\n", map { "%{$_}" } @wanted );
my @info = `fc-scan --format "$fc_format" "$fontfile"`;
chomp @info;
my %fontinfo;
@fontinfo{@wanted} = @info;
if ( grep /,/, @fontinfo{ @lang_dependent } ) {
my $format = join( "\n", map { "%{${_}lang}" } @lang_dependent );
my @langs = `fc-scan --format "$format" "$fontfile"`;
for my $i (0..$#lang_dependent) {
my @lang_list = split /,/, $langs[$i];
my ($pos) = grep { $lang_list[$_] ~~ $lang } 0 .. $#lang_list;
my @vals = split /,/, $fontinfo{$lang_dependent[$i]};
$fontinfo{$lang_dependent[$i]} = $vals[$pos];
}
}
warn Dumper(%fontinfo), "n" if $debug;
$fontinfo{'fullname'} ||= $fontinfo{'family'}; # some old fonts don't have a fullname? (WINNT/Fonts/marlett.ttf)
print join($separator, @fontinfo{@wanted}), "n";
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0