4 # file listFontWithLang.pl
5 # This file is part of LyX, the document processor.
6 # Licence details can be found in the file COPYING
7 # or at http://www.lyx.org/about/licence.php
10 # Full author contact details are available in the file CREDITS
11 # or at https://www.lyx.org/Credits
13 # Usage: listFontWithLang.pl <options>
14 # Displays installed system font names selected by <options>
15 # Option-strings with more that 1 char need be prefixed by '--'
17 # Option to get list of options: -h
19 # Some equivalencies for instance with option -n
24 # Options and option-parameter are case insensitive
28 my $p = File::Spec->rel2abs( __FILE__ );
29 $p =~ s/[\/\\]?[^\/\\]+$//;
38 sub extractlist($$$); # my ($l, $islang, $txt, $rres) = @_;
40 sub getVal($$$); # my ($l, $txtval, $txtlang) = @_;
41 sub getproperties($$$$);
44 # Following fields for a parameter can be defined:
45 # fieldname: Name of entry in %options
46 # type: [:=][sif], ':' = optional, '=' = required, 's' = string, 'i' = integer, 'f' = float
47 # alias: reference to a list of aliases e.g. ["alias1", "alias2", ... ]
48 # listsep: Separator for multiple data
49 # comment: Parameter description
51 # help + verbose already handled in 'GetOptions'
52 "l" => {fieldname => "Lang",
53 type => "=s", alias=>["lang"],
54 comment => "Comma separated list of desired languages"},
55 "n" => {fieldname => "FontName", listsep => ',',
56 type => "=s", alias => ["name"],
57 comment => "Select font-names matching these (comma separated) regexes"},
58 "nn" => {fieldname => "NFontName",
59 type => "=s", listsep => ',',
60 comment => "Select font-names NOT matching these (comma separated) regexes"},
61 "pl" => {fieldname => "PrintLangs", alias => ["printlangs"],
62 comment => "Print supported languages"},
63 "pf" => {fieldname => "PrintFiles", alias => ["printfiles"],
64 comment => "Print font file names"},
65 "p" => {fieldname => "Property",
66 type => "=s", listsep => ',',
67 comment => "Select fonts with properties matching these (comma separated) regexes"},
68 "np" => {fieldname => "NProperty",
69 type => "=s", listsep => ',',
70 comment => "Select fonts with properties NOT matching these (comma separated) regexes"},
71 "pp" => {fieldname => "PrintProperties", alias => ["printproperties"],
72 comment => "Print properties from weight, slant and width"},
73 "s" => {fieldname => "Scripts",
74 type => "=s", listsep => ',',
75 comment => "Select fonts with scripts matching these (comma separated) regexes"},
76 "ns" => {fieldname => "NScripts",
77 type => "=s", listsep => ',',
78 comment => "Select fonts with scripts NOT matching these (comma separated) regexes"},
79 "ps" => {fieldname => "PrintScripts", alias => ["printscripts"],
80 comment => "Print supported scripts"},
81 "pw" => {fieldname => "PrintWarnings",
82 comment => "Print warnings about discarded/overwritten fonts, conflicting styles"},
84 my %options = %{&handleOptions(\%optionsDef)};
86 $options{Lang} = "" if (! defined($options{Lang}));
88 #############################################################
90 my @langs = split(',', $options{Lang});
92 $lg = &convertlang($lg);
96 if (defined($langs[0])) {
97 $cmd .= " :lang=" . join(',', @langs);
100 my $format = "foundry=\"%{foundry}\" postscriptname=\"%{postscriptname}\" fn=\"%{fullname}\" fnl=\"%{fullnamelang}\" family=\"%{family}\" flang=\"%{familylang}\" style=\"%{style}\" stylelang=\"%{stylelang}\"";
102 if (exists($options{PrintScripts}) || defined($options{Scripts}) || defined($options{NSpripts})) {
103 $format .= " script=\"%{capability}\"";
105 if (exists($options{PrintLangs}) || defined($langs[0])) {
106 $format .= " lang=\"%{lang}\"";
108 if (exists($options{PrintProperties}) || defined($options{Property})) {
109 $format .= " weight=%{weight} slant=%{slant} width=%{width} spacing=%{spacing}";
111 $format .= " file=\"%{file}\" abcd\\n";
112 $cmd .= " -f '$format'";
119 "default" => "Serif",
143 50 => "Ultracondensed",
144 63 => "Extracondensed",
146 87 => "Semicondensed",
148 113 => "Semiexpanded",
150 150 => "Extraexpanded",
151 200 => "Ultraexpanded",
159 "default" => "Proportional",
162 # Build reverse mappings, (not needed yet)
163 for my $txt (qw(ftypes weights slants widths spacings)) {
165 eval "\$map = \\%$txt";
166 for my $key (keys %{$map}) {
167 next if ($key !~ /^\d+$/);
168 my $value = lc($map->{$key});
169 $map->{"r"}->{$value} = $key;
176 my %collectedfonts = ();
178 otf => 0, # type 2, opentype CFF (Compact Font Format)
179 ttc => 1.1, # type 1 (True Type font Collection)
180 ttf => 1.2, # type 1 (True Type Font)
181 woff=> 1.3, # type 1 (Web Open Font Format)
182 t1 => 1.4, # type 1 (postscript)
183 pfb => 1.5, # type 1 (Printer Font Binary)
184 pfa => 1.6, # type 1 (Printer Font Ascii)
185 pcf => 5, # Bitmap (Packaged Collaboration Files)?
189 if (open(FI, "$cmd |")) {
190 NXTLINE: while (my $l = <FI>) {
192 while ($l !~ /abcd$/) {
198 if ($l =~ /file=\"([^\"]+)\"/) {
200 #next if ($file !~ /\.(otf|ttf|pfa|pfb|pcf|ttc)$/i);
201 if ($file !~ /\.([a-z0-9]{2,5})$/i) {
202 print "Unhandled extension for file $file\n";
206 if (! defined($fontpriority{$fonttype})) {
207 print "Added extension $fonttype for file $file\n";
208 $fontpriority{$fonttype} = $nexttype;
214 if ($l =~ / lang=\"([^\"]+)\"/) {
215 my @ll = split(/\|/, $1);
217 $usedlangs{&convertlang($lx)} = 1;
221 for my $lang (@langs) {
222 next NXTLINE if (! defined($usedlangs{$lang}));
225 my $style = &getVal($l, "style", "stylelang");
226 $style =~ s/^\\040//;
227 my $fullname = &getVal($l, "fn", "fnl");
228 my $postscriptname = "";
229 if ($l =~ /postscriptname=\"([^\"]+)\"/) {
230 $postscriptname = $1;
232 my $family = &getVal($l, "family", "flang");
233 $family =~ s/\\040/\-/;
235 if (length($family) < 3) {
236 if (length($postscriptname) < 2) {
237 $fontname = $fullname;
240 $fontname = $postscriptname;
244 $fontname = "$family $style";
246 if (defined($options{NFontName})) {
247 for my $fn (@{$options{NFontName}}) {
248 next NXTLINE if ($fontname =~ /$fn/i);
251 if (defined($options{FontName})) {
252 for my $fn (@{$options{FontName}}) {
253 next NXTLINE if ($fontname !~ /$fn/i);
258 if (exists($options{PrintProperties}) || defined($options{Property}) || defined($options{NProperty})) {
259 my $properties = getproperties($l, $family, $style, \@errors);
260 if (defined($options{Property})) {
261 for my $pn (@{$options{Property}}) {
262 next NXTLINE if ($properties !~ /$pn/i);
265 if (defined($options{NProperty})) {
266 for my $pn (@{$options{NProperty}}) {
267 next NXTLINE if ($properties =~ /$pn/i);
270 if (exists($options{PrintProperties})) {
271 $props .= " ($properties)";
275 if (exists($options{PrintLangs})) {
276 $props .= '(' . join(',', sort keys %usedlangs) . ')';
278 if (exists($options{PrintScripts}) || defined($options{Scripts}) || defined($options{NScripts})) {
281 if ($l =~ / script=\"([^\"]+)\"/) {
282 @scripts = split(/\s+/, $1);
283 for my $ent (@scripts) {
284 $ent =~ s/^\s*otlayout://;
287 $scripts = join(',', @scripts);
289 if (exists($options{PrintScripts})) {
290 $props .= "($scripts)";
292 if (!defined($scripts[0])) {
293 # No script defined in font, so check only $options{Scripts}
294 next NXTLINE if (defined($options{Scripts}));
297 if (defined($options{Scripts})) {
298 for my $s (@{$options{Scripts}}) {
299 next NXTLINE if ($scripts !~ /$s/i);
302 if (defined($options{NScripts})) {
303 for my $s (@{$options{NScripts}}) {
304 next NXTLINE if ($scripts =~ /$s/i);
310 if ($l =~ /foundry=\"([^\"]+)\"/) {
312 $foundry =~ s/^\s+//;
313 $foundry =~ s/\s+$//;
315 if (defined($collectedfonts{$fontname}->{$foundry}->{errors})) {
316 # Apparently not the first one, so add some info
317 my $oldfonttype = $collectedfonts{$fontname}->{$foundry}->{fonttype};
318 if (defined($errors[0])) {
319 push(@{$collectedfonts{$fontname}->{$foundry}->{errors}}, @errors);
321 if ($fontpriority{$oldfonttype} > $fontpriority{$fonttype}) {
322 push(@{$collectedfonts{$fontname}->{$foundry}->{errors}}, "Warning: overwriting old info for file: " . $collectedfonts{$fontname}->{$foundry}->{file});
325 push(@{$collectedfonts{$fontname}->{$foundry}->{errors}}, "Warning: discarding new info for file: $file");
330 $collectedfonts{$fontname}->{$foundry}->{errors} = \@errors;
332 $collectedfonts{$fontname}->{$foundry}->{props} = $props;
333 $collectedfonts{$fontname}->{$foundry}->{file} = $file;
334 $collectedfonts{$fontname}->{$foundry}->{fonttype} = $fonttype;
339 for my $fontname (sort keys %collectedfonts) {
340 my @foundries = sort keys %{$collectedfonts{$fontname}};
341 my $printfoundries = 0;
342 if (defined($foundries[1])) {
345 for my $foundry (@foundries) {
346 if (exists($options{PrintWarnings})) {
347 for my $err (@{$collectedfonts{$fontname}->{$foundry}->{errors}}) {
351 my $fn = "Font : $fontname";
352 if ($printfoundries && ($foundry ne "")) {
353 $fn .= " \[$foundry\]";
356 print $collectedfonts{$fontname}->{$foundry}->{props};
357 if (exists($options{PrintFiles})) {
358 print ": " . $collectedfonts{$fontname}->{$foundry}->{file} . "\n";
367 #################################################################################
371 if ($ilang =~ /^\s*([a-z]+)([\-_]([a-z]+))?\s*$/i) {
372 my ($x, $y) = ($1, $3);
374 $ilang = lc($x) . '-' . lc($y);
385 my ($l, $islang, $txt, $rres) = @_;
387 if ($l =~ /$txt=\"([^\"]+)\"/) {
388 @{res} = split(',', $1);
391 $lg = &convertlang($lg);
400 my ($lang, $rlangs) = @_;
401 for (my $i = 0; defined($rlangs->[$i]); $i++) {
402 return $i if ($rlangs->[$i] eq $lang);
409 my ($l, $txtval, $txtlang) = @_;
412 &extractlist($l, 0, $txtval, \@values);
413 return("") if (! defined($values[0]));
414 &extractlist($l, 1, $txtlang, \@langs);
415 my $i = &getIndex("en", \@langs);
416 return ($values[$i]) if ($i >= 0);
420 sub getsinglevalue($$$)
422 my ($l, $txt, $rMap, $rget) = @_;
424 if ($l =~ / $txt=(\d+)/) {
426 # Search for nearest value to $val
427 if (defined($rMap->{$val})) {
428 return($rMap->{$val});
432 for my $key (keys %{$rMap}) {
433 next if ($key !~ /^\d+$/);
434 my $diff = abs($key - $val);
439 elsif ($diff == $minv) {
445 if (! defined($rMap->{$maxv})) {
446 print "ERROR2: txt=$txt, val=$val\n";
450 return($rMap->{$maxv} . "+$minv");
453 return($rMap->{$maxv} . "-$minv");
463 my ($txt, $val) = @_;
464 return("$txt($val)");
469 my ($family, $style) = @_;
470 if ("$family" =~ /arial|helvet|trebuchet/i) {
471 return($ftypes{100}); # Sans Serif
473 elsif ($family =~ /(sans)[-_ ]?(serif)?/i) {
474 return($ftypes{100}); # Sans Serif
476 elsif ($family =~ /serif/i) {
477 return($ftypes{0}); # Serif
486 my ($family, $style) = @_;
488 for my $key (keys %weights) {
489 next if ($key !~ /^\d+$/);
490 my $val = $weights{$key};
491 for my $info ($style, $family) {
492 if ($info =~ /\b$val\b/i) {
493 if ($val eq "Regular") {
494 $result = $val; # It may refer to width
507 my ($family, $style) = @_;
509 for my $key (keys %widths) {
510 next if ($key !~ /^\d+$/);
511 for my $info ($style, $family) {
512 if ($info =~ /\b$widths{$key}\b/i) {
513 return($widths{$key});
515 if ($info =~ /\bRegular\b/) {
516 if (!defined($result)) {
517 $result = $widths{100};
527 my ($family, $style) = @_;
528 for my $key (keys %slants) {
529 next if ($key !~ /^\d+$/);
530 if ($style =~ /\b$slants{$key}\b/i) {
531 return($slants{$key});
539 my ($family, $style) = @_;
540 for my $key (keys %spacings) {
541 next if ($key !~ /^\d+$/);
542 if ($style =~ /\b$spacings{$key}\b/i) {
543 return($spacings{$key});
546 if ("$family $style" =~ /(mono|typewriter|cursor|fixed)\b/i) {
547 return($spacings{100}); # Mono
554 sub getproperties($$$$)
556 my ($l, $family, $style, $rerrors) = @_;
557 my $newstyle = &correctstyle($style);
558 my $newfam = &correctstyle($family);
561 for my $txt (qw(ftype weight width slant spacing)) {
563 eval("\$map = " . '\%' . $txt . 's');
564 eval('$rget = \&' . "get$txt");
565 my $val2 = getsinglevalue($l, $txt, $map);
566 my $val1 = $rget->($newfam, $newstyle);
568 if (defined($val2) && defined($val1) && ($val2 ne $val1)) {
569 push(@{$rerrors}, "Family($family),Style($style): Values for $txt ($val1 != $val2) differ, selecting internal $txt($val2)");
572 elsif (! defined($val2)) {
579 push(@properties, &addTxt($txt,$val));
582 if (defined($map->{"default"})) {
583 push(@properties, &addTxt($txt,$map->{"default"}));
586 push(@{$rerrors}, "Undefined value for $txt");
590 return(join(' ', @properties));
596 $style =~ s/^\\040//;
597 $style =~ s/^\s*\d+\s*//;
598 $style =~ s/\s*\d+$//;
599 $style =~ s/italic/ Italic/i;
600 $style =~ s/oblique/ Oblique/i;
601 $style =~ s/[\-_]/ /g;
602 $style =~ s/\breg\b/Regular/i;
603 $style =~ s/\bregita(lic)?\b/Regular Italic/i;
604 $style =~ s/\bregobl(ique)?\b/Regular Oblique/i;
605 $style =~ s/medium/Medium /i;
606 $style =~ s/\bmedita(lic)?\b/Medium Italic/i;
607 $style =~ s/\bmedobl(ique)?\b/Medium Oblique/i;
608 $style =~ s/\bmed\b/Medium /i;
609 $style =~ s/\bdemi\b/SemiBold/i;
610 $style =~ s/\bex(pd|t)\b/Expanded/i;
611 $style =~ s/semi ?cond(ensed)?/SemiCondensed/i;
612 $style =~ s/[sd]emi ?(bold|bd|bol)/SemiBold/i;
613 $style =~ s/semi ?(expanded|extended|expd)/SemiExpanded/i;
614 $style =~ s/[sd]emi ?light/SemiLight/i;
615 $style =~ s/ultra ?(expanded|extended|expd)/UltraExpanded/i;
616 $style =~ s/light/Light /i;
617 $style =~ s/\blt\b/Light /i;
618 $style =~ s/(ultra|extra)(light|lt)/ExtraLight /i;
619 $style =~ s/\bheavy\b/Extrabold/i;
620 $style =~ s/\bhairline\b/Extralight/i;
621 $style =~ s/\bcond\b/Condensed/i;
622 $style =~ s/(roman)?slanted/ Italic/i;
623 $style =~ s/\bslant\b/Italic/i;
624 $style =~ s/\b(SC|Small(caps(alt)?)?)\b/SmallCaps/i;
625 $style =~ s/w3 mono/Dual/i;
626 $style =~ s/Regul[ea]r/Regular/i;