Is there a unix command line tool that can analyze font files?
Solution 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".
Solution 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 FILENAME\n" 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";
Related videos on Youtube
kreek
I have extensive experience designing and building scalable distributed web services. I’m currently a Staff Software Engineer with Ad Hoc LLC and the Department of Veterans Affairs. For the past three years I’ve helped turn the VA’s web offerings from a fractured experience into a one-stop shop. Allowing veterans to efficiently receive the benefits they’ve earned. I was one of the founding developers on vets.gov. A United States Digital Service project functioning as a start-up within the VA. Veteran outcomes improved dramatically via vets.gov. So much so that within two years of launch the VA secretary decided it should become the new va.gov. How we built the site received as much praise as the end-product. Agile methodologies, automated tests, CI/CD, cloud services (AWS), active monitoring, and user centered design were all foreign to the VA before vets.gov. Those practices are being propagated throughout the VA via my current project. The Veteran Service Platform, or VSP. VSP helps development teams succeed, by building on top of our APIs, and running our digital services playbook.
Updated on September 18, 2022Comments
-
kreek over 1 year
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?
-
kreek over 12 yearsPerfect, thanks! For those interested I'm on OS X and installed it via brew with
brew install lcdf-typetools
-
Samir Daraf over 8 yearsAwesome, thanks for the tip (and script.. though I haven't tested the script yet). Do you know if there's a way to get license/copyright info as well? I tried %{license}, %{copyright} and no format, but none of those yielded anything, whereas fontforge is able to show it to me.
-
mivk over 8 yearsIndeed, fc-scan does't seem to show the copyright.
foundry
is the closest it gives you. Butotfinfo -i
, suggested by cjm, does display it. -
Samir Daraf over 8 yearsAh that's great, I installed
lcdf-typetools
and and ranotfinfo -i
as suggested and that did the trick, thanks! (And I gave @cjm a +1 as well). -
mpr about 6 yearsfc-scan is great for getting the font "fullname" that is used to reference the font in programs.
-
hans_meine about 5 yearsFWIW, the MacPorts package is also called
lcdf-typetools
(and is a dependency oftexlive-fontutils
, so it may already be installed for people using TeX). -
emk2203 about 4 yearsIf you want to avoid the language issue,
postscriptname
instead offullname
gives you the same information, language-independent, in a different format. Alternatively, in bash:for fontfile in /mnt/Fonts/*.{otf,ttf}; do fc-scan --format "%{fullname[$(( $(sed -E 's/^(.*)en.*/\1/;s/[^,]//g' <<< "$(fc-scan --format "%{fullnamelang}\n" ${fontfile})" | wc -c) -1 ))]}\n" "${fontfile}"; done > fontlist.txt
-
Digger over 3 years@emk2203 That is money!