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($$$$);
45 # Following fields for a parameter can be defined:
46 # fieldname: Name of entry in %options
47 # type: [:=][sif], ':' = optional, '=' = required, 's' = string, 'i' = integer, 'f' = float
48 # alias: reference to a list of aliases e.g. ["alias1", "alias2", ... ]
49 # listsep: Separator for multiple data
50 # comment: Parameter description
52 # help + verbose already handled in 'GetOptions'
54 {fieldname => "FontName", listsep => ',',
55 type => "=s", alias => ["name"],
56 comment => "Select font-names matching these (comma separated) regexes"},],
58 {fieldname => "NFontName",
59 type => "=s", listsep => ',',
60 comment => "Select font-names NOT matching these (comma separated) regexes"},],
62 {fieldname => "Property",
63 type => "=s", listsep => ',',
64 comment => "Select fonts with properties matching these (comma separated) regexes"},],
66 {fieldname => "NProperty",
67 type => "=s", listsep => ',',
68 comment => "Select fonts with properties NOT matching these (comma separated) regexes"},],
70 {fieldname => "Scripts",
71 type => "=s", listsep => ',',
72 comment => "Select fonts with scripts matching these (comma separated) regexes"},],
74 {fieldname => "NScripts",
75 type => "=s", listsep => ',',
76 comment => "Select fonts with scripts NOT matching these (comma separated) regexes"},],
79 comment => "Select fonts probably containing math glyphs"},],
82 type => "=s", alias=>["lang"],
83 comment => "Comma separated list of desired languages"},],
85 {fieldname => "PrintLangs", alias => ["printlangs"],
86 comment => "Print supported languages"},],
88 {fieldname => "PrintProperties", alias => ["printproperties"],
89 comment => "Print properties from weight, slant and width"},],
91 {fieldname => "PrintScripts", alias => ["printscripts"],
92 comment => "Print supported scripts"},],
94 {fieldname => "PrintFiles", alias => ["printfiles"],
95 comment => "Print font file names"},],
97 {fieldname => "PrintWarnings",
98 comment => "Print warnings about discarded/overwritten fonts, conflicting styles"},],
100 my %options = %{&handleOptions(\@optionsDef)};
102 $options{Lang} = "" if (! defined($options{Lang}));
104 #############################################################
106 my @langs = split(',', $options{Lang});
107 for my $lg (@langs) {
108 $lg = &convertlang($lg);
112 if (defined($langs[0])) {
113 $cmd .= " :lang=" . join(',', @langs);
116 my $format = "foundry=\"%{foundry}\"" .
117 " postscriptname=\"%{postscriptname}\"" .
118 " fn=\"%{fullname}\" fnl=\"%{fullnamelang}\"" .
119 " family=\"%{family}\" flang=\"%{familylang}\" " .
120 " style=\"%{style}\" stylelang=\"%{stylelang}\"";
122 if (exists($options{PrintScripts}) || defined($options{Scripts}) || defined($options{NSpripts}) || exists($options{Math})) {
123 $format .= " script=\"%{capability}\"";
125 if (exists($options{PrintLangs}) || defined($langs[0])) {
126 $format .= " lang=\"%{lang}\"";
128 if (exists($options{PrintProperties}) || defined($options{Property})) {
129 $format .= " weight=%{weight} slant=%{slant} width=%{width} spacing=%{spacing}";
131 $format .= " file=\"%{file}\" abcd\\n";
132 $cmd .= " -f '$format'";
144 "default" => "Serif",
168 50 => "Ultracondensed",
169 63 => "Extracondensed",
171 87 => "Semicondensed",
173 113 => "Semiexpanded",
175 150 => "Extraexpanded",
176 200 => "Ultraexpanded",
184 "default" => "Proportional",
187 # Build reverse mappings, (not needed yet)
188 for my $txt (qw(ftypes weights slants widths spacings)) {
190 eval "\$map = \\%$txt";
191 for my $key (keys %{$map}) {
192 next if ($key !~ /^\d+$/);
193 my $value = lc($map->{$key});
194 $map->{"r"}->{$value} = $key;
201 my %collectedfonts = ();
203 otf => 0, # type 2, opentype CFF (Compact Font Format)
204 ttc => 1.1, # type 1 (True Type font Collection)
205 ttf => 1.2, # type 1 (True Type Font)
206 woff=> 1.3, # type 1 (Web Open Font Format)
207 t1 => 1.4, # type 1 (postscript)
208 pfb => 1.5, # type 1 (Printer Font Binary)
209 pfa => 1.6, # type 1 (Printer Font Ascii)
210 pcf => 5, # Bitmap (Packaged Collaboration Files)?
214 # list of regexes for known sans serif fonts
216 "value" => 100, # Sans serif
217 "a" => qr/^(arial|andika|angostura|anonymous|arab|aroania|arimo|asap)/i,
218 "b" => qr/^b(aekmuk|ebas|erenika|eteckna|euron|lue)/i,
219 "c" => qr/^c(abin|aliban|antarell|arbon|arlito|handas|hivo|mu bright|omfortaa|omic|oolvetica|ortoba|ousine|uprum|wtex(hei|yen)|yklop|ypro)/i,
220 "d" => qr/^(d2coding|dimnah|dosis|dyuthi)/i,
221 "e" => qr/^(electron|engebrechtre)/i,
222 "f" => qr/^(fandolhei|fetamont|fira|font awesome 5|forgotten)/i,
223 "g" => qr/^(gardiner|garuda|gfs ?neo|gillius|granada|graph|guanine|gunplay)/i,
224 "h" => qr/^(hack|hani|haramain|harano|harmattan|hor\b)/i,
225 "i" => qr/^(ibm plex|ikarius|inconsolata|induni.?h|iwona)/i,
226 "j" => qr/^(jara|jura)/i,
227 "k" => qr/^(kalimati|kanji|karla|kayrawan|kenyan|keraleeyam|khalid|khmer [or]|kiloji|klaudia|komatu|kurier)/i,
228 "l" => qr/^l(aksaman|arabie|ato|eague|exend|exigulim|ibel|iberation|ibre franklin|ibris|inux biolinum|obster|ogix|ohit|oma)/i,
229 "m" => qr/^m(\+ |anchu|anjari|arcellus|ashq|eera|etal|igmix|igu|ikachan|intspirit|ona|onlam|ono(fonto|id|isome|noki)|ontserrat|otoyal|ukti|usica)/i,
230 "n" => qr/^(nachlieli|nada|nafees|nagham|nanum(barunpen|square)|nice)/i,
231 "o" => qr/^(ocr|okolaks|opendyslexic|ostorah|ouhud|over|oxygen)/i,
232 "p" => qr/^(padauk|padmaa|pagul|paktype|pakenham|palladio|petra|phetsarath|play\b|poiret|port\b|primer\b|prociono|pt\b|purisa)/i,
233 "q" => qr/^(qt(ancient|helvet|avanti|doghaus|eratype|eurotype|floraline|frank|fritz|future|greece|howard|letter|optimum)|quercus)/i,
234 "r" => qr/^(rachana|radio\b|raleway|ricty|roboto|rosario)/i,
235 "s" => qr/^(salem|samanata|sawasdee|shado|sharja|simple|sophia|soul|source|switzera)/i,
236 "t" => qr/^(tarablus|teen|texgyre(adventor|heros)|tiresias|trebuchet|tscu|tuffy)/i,
237 "u" => qr/^(ubuntu|ukij (bom|chechek|cjk|diwani|ekran|elipbe|inchike|jelliy|kufi|qara|qolyazma|teng|title|tor)|umpush|un ?(dinaru|jamo|graphic|taza|vada|yetgul)|uni(kurd|space|versalis)|uroob|urw ?classico)/i,
238 "v" => qr/^(veranda|vn ?urwclassico)/i,
239 "w" => qr/^(waree)/i,
240 "y" => qr/^(yanone)/i,
241 "z" => qr/^(zekton|zero)/i,
244 "value" => 110, # Script
245 "c" => qr/^(chancery)/i,
246 "d" => qr/^(dancing)/i,
247 "e" => qr/^(elegante)/i,
248 "k" => qr/^(kaushan|karumbi)/i,
249 "m" => qr/^(mathjax_script|miama)/i,
250 "n" => qr/^(nanum (brush|pen) script)/i,
251 "q" => qr/^qt(arabian|boulevard|brushstroke|chancery|coronation|florencia|handwriting|linostroke|merry|pandora|slogan)/i,
252 "r" => qr/^(romande.*|ruf)script/i,
253 "u" => qr/^(un ?pilgi|urw ?chancery)/i,
257 "value" => 120, # Fraktur
258 "j" => qr/^(jsmath.?euf)/i,
259 "m" => qr/^(missaali)/i,
260 "o" => qr/^(oldania)/i,
261 "q" => qr/^qt(blackforest|cloisteredmonk|dublinirish|fraktur|heidelbergtype|(lino|london)scroll)/i,
265 "value" => 130, # Fancy
266 "c" => qr/^(cretino)/i,
267 "g" => qr/^(gfs.?theo)/i,
271 "value" => 140, # Initials
272 "e" => qr/^(eb.?garamond.?init)/i,
273 "l" => qr/^(libertinus|linux).*initials/i,
274 "y" => qr/^(yinit)/i,
278 "value" => 200, # Symbol
279 "a" => qr/^(academicons)/i,
280 "c" => qr/^(caladings|ccicons|chess)/i,
281 "d" => qr/^(dingbats|drmsym)/i,
282 "e" => qr/^(elusiveicons|emoji)/i,
283 "f" => qr/^(fdsymbol|fourierorns)/i,
285 "m" => qr/^(marvosym|material)/i,
286 "n" => qr/^(noto.*emoji)/i,
287 "o" => qr/^(octicons)/i,
288 "q" => qr/^(qtdingbits)/i,
289 "t" => qr/^(typicons|twemoji)/i,
292 if (open(FI, "$cmd |")) {
293 NXTLINE: while (my $l = <FI>) {
295 while ($l !~ /abcd$/) {
301 if ($l =~ /file=\"([^\"]+)\"/) {
303 #next if ($file !~ /\.(otf|ttf|pfa|pfb|pcf|ttc)$/i);
304 if ($file !~ /\.([a-z0-9]{2,5})$/i) {
305 print "Unhandled extension for file $file\n";
309 if (! defined($fontpriority{$fonttype})) {
310 print "Added extension $fonttype for file $file\n";
311 $fontpriority{$fonttype} = $nexttype;
317 if ($l =~ / lang=\"([^\"]+)\"/) {
318 my @ll = split(/\|/, $1);
320 $usedlangs{&convertlang($lx)} = 1;
324 for my $lang (@langs) {
325 next NXTLINE if (! defined($usedlangs{$lang}));
328 my $style = &getVal($l, "style", "stylelang");
329 $style =~ s/^\\040//;
330 my $fullname = &getVal($l, "fn", "fnl");
331 my $postscriptname = "";
332 if ($l =~ /postscriptname=\"([^\"]+)\"/) {
333 $postscriptname = $1;
335 my $family = &getVal($l, "family", "flang");
336 $family =~ s/\\040/\-/;
338 if (length($fullname) < 3) {
339 if (length($postscriptname) < 2) {
340 $fontname = "$family $style";
343 $fontname = $postscriptname;
347 $fontname = $fullname;
349 if (defined($options{NFontName})) {
350 for my $fn (@{$options{NFontName}}) {
351 next NXTLINE if ($fontname =~ /$fn/i);
354 if (defined($options{FontName})) {
355 for my $fn (@{$options{FontName}}) {
356 next NXTLINE if ($fontname !~ /$fn/i);
361 if (exists($options{PrintProperties}) || defined($options{Property}) || defined($options{NProperty})) {
362 my $properties = getproperties($l, $fontname, $style, \@errors);
363 if (defined($options{Property})) {
364 for my $pn (@{$options{Property}}) {
365 next NXTLINE if ($properties !~ /$pn/i);
368 if (defined($options{NProperty})) {
369 for my $pn (@{$options{NProperty}}) {
370 next NXTLINE if ($properties =~ /$pn/i);
373 if (exists($options{PrintProperties})) {
374 $props .= " ($properties)";
378 if (exists($options{PrintLangs})) {
379 $props .= '(' . join(',', sort keys %usedlangs) . ')';
381 if (exists($options{PrintScripts}) || defined($options{Scripts}) || defined($options{NScripts}) || exists($options{Math})) {
384 if ($l =~ / script=\"([^\"]+)\"/) {
385 @scripts = split(/\s+/, $1);
386 for my $ent (@scripts) {
387 $ent =~ s/^\s*otlayout://;
390 $scripts = join(',', @scripts);
392 if (exists($options{Math})) {
393 next NXTLINE if (! &ismathfont($fontname,\@scripts));
395 if (exists($options{PrintScripts})) {
396 $props .= "($scripts)";
398 if (!defined($scripts[0])) {
399 # No script defined in font, so check only $options{Scripts}
400 next NXTLINE if (defined($options{Scripts}));
403 if (defined($options{Scripts})) {
404 for my $s (@{$options{Scripts}}) {
405 next NXTLINE if ($scripts !~ /$s/i);
408 if (defined($options{NScripts})) {
409 for my $s (@{$options{NScripts}}) {
410 next NXTLINE if ($scripts =~ /$s/i);
416 if ($l =~ /foundry=\"([^\"]+)\"/) {
418 $foundry =~ s/^\s+//;
419 $foundry =~ s/\s+$//;
421 if (defined($collectedfonts{$fontname}->{$foundry}->{errors})) {
422 # Apparently not the first one, so add some info
423 my $oldfonttype = $collectedfonts{$fontname}->{$foundry}->{fonttype};
424 if (defined($errors[0])) {
425 push(@{$collectedfonts{$fontname}->{$foundry}->{errors}}, @errors);
427 if ($fontpriority{$oldfonttype} > $fontpriority{$fonttype}) {
428 push(@{$collectedfonts{$fontname}->{$foundry}->{errors}}, "Warning: overwriting old info for file: " . $collectedfonts{$fontname}->{$foundry}->{file});
431 push(@{$collectedfonts{$fontname}->{$foundry}->{errors}}, "Warning: discarding new info for file: $file");
436 $collectedfonts{$fontname}->{$foundry}->{errors} = \@errors;
438 $collectedfonts{$fontname}->{$foundry}->{props} = $props;
439 $collectedfonts{$fontname}->{$foundry}->{file} = $file;
440 $collectedfonts{$fontname}->{$foundry}->{fonttype} = $fonttype;
445 for my $fontname (sort keys %collectedfonts) {
446 my @foundries = sort keys %{$collectedfonts{$fontname}};
447 my $printfoundries = 0;
448 if (defined($foundries[1])) {
451 for my $foundry (@foundries) {
452 if (exists($options{PrintWarnings})) {
453 for my $err (@{$collectedfonts{$fontname}->{$foundry}->{errors}}) {
457 my $fn = "Font : $fontname";
458 if ($printfoundries && ($foundry ne "")) {
459 $fn .= " \[$foundry\]";
462 print $collectedfonts{$fontname}->{$foundry}->{props};
463 if (exists($options{PrintFiles})) {
464 print ": " . $collectedfonts{$fontname}->{$foundry}->{file} . "\n";
473 #################################################################################
477 if ($ilang =~ /^\s*([a-z]+)([\-_]([a-z]+))?\s*$/i) {
478 my ($x, $y) = ($1, $3);
480 $ilang = lc($x) . '-' . lc($y);
491 my ($l, $islang, $txt, $rres) = @_;
493 if ($l =~ /$txt=\"([^\"]+)\"/) {
494 @res = split(',', $1);
497 $lg = &convertlang($lg);
506 my ($lang, $rlangs) = @_;
509 for (my $i = 0; defined($rlangs->[$i]); $i++) {
510 if ($rlangs->[$i] eq $lang) {
519 my ($l, $txtval, $txtlang) = @_;
522 &extractlist($l, 0, $txtval, \@values);
523 return("") if (! defined($values[0]));
524 &extractlist($l, 1, $txtlang, \@langs);
525 my $i = &getIndexes("en", \@langs);
528 if (defined($values[$k]) && (length($values[$k]) > length($res))) {
532 return($values[0]) if ($res eq "");
536 sub getsinglevalue($$$)
538 my ($l, $txt, $rMap, $rget) = @_;
540 if ($l =~ / $txt=(\d+)/) {
542 # Search for nearest value to $val
543 if (defined($rMap->{$val})) {
544 return($rMap->{$val});
548 for my $key (keys %{$rMap}) {
549 next if ($key !~ /^\d+$/);
550 my $diff = abs($key - $val);
555 elsif ($diff == $minv) {
561 if (! defined($rMap->{$maxv})) {
562 print "ERROR2: txt=$txt, val=$val\n";
566 return($rMap->{$maxv} . "+$minv");
569 return($rMap->{$maxv} . "-$minv");
579 my ($txt, $val) = @_;
580 return("$txt($val)");
585 my ($fontname, $style) = @_;
586 if ($fontname =~ /(sans)[-_ ]?(serif)?/i) {
587 return($ftypes{100}); # Sans Serif
589 elsif ($fontname =~ /gothic|dotum|gulim/i) {
590 if ($fontname =~ /bisrat gothic/i) {
591 return($ftypes{0}); # Serif
594 return($ftypes{100}); # Sans Serif
597 elsif ($fontname =~ /serif|times|mincho|batang/i) {
598 if ($fontname =~ /good times/i) {
599 return($ftypes{100}); # Sans Serif
601 elsif ($fontname !~ /initials/i) {
602 return($ftypes{0}); # Serif
605 # Now check for fonts without a hint in font name
606 if ($fontname =~ /([a-z])/i) {
608 for my $rFonts (\%sansFonts, \%scriptFonts, \%fraktFonts, \%fancyFonts, \%initialFonts, \%symbolFonts) {
609 if (defined($rFonts->{$key})) {
610 if ($fontname =~ $rFonts->{$key}) {
611 return($ftypes{$rFonts->{"value"}});
616 if ("$fontname" =~ /^bpg/i) {
617 if ("$fontname" =~ /bpg (courier gpl|elite)/i) {
618 return($ftypes{0}); # Serif
621 return($ftypes{100}); # Sans Serif
624 elsif ("$fontname" =~ /^dustismo/i) {
625 if ("$fontname" =~ /^dustismo roman/i) {
626 return($ftypes{0}); # Serif
629 return($ftypes{100}); # Sans Serif
632 elsif ("$fontname" =~ /^go\b/i) {
633 if ("$fontname" =~ /^go mono/i) {
634 return($ftypes{0}); # Serif
637 return($ftypes{100}); # Sans Serif
647 my ($fontname, $style) = @_;
649 for my $key (keys %weights) {
650 next if ($key !~ /^\d+$/);
651 my $val = $weights{$key};
652 for my $info ($style, $fontname) {
653 if ($info =~ /\b$val\b/i) {
654 if ($val eq "Regular") {
655 $result = $val; # It may refer to width
668 my ($fontname, $style) = @_;
670 for my $key (keys %widths) {
671 next if ($key !~ /^\d+$/);
672 for my $info ($style, $fontname) {
673 if ($info =~ /\b$widths{$key}\b/i) {
674 return($widths{$key});
676 if ($info =~ /\bRegular\b/) {
677 if (!defined($result)) {
678 $result = $widths{100};
688 my ($fontname, $style) = @_;
689 for my $key (keys %slants) {
690 next if ($key !~ /^\d+$/);
691 if ($style =~ /\b$slants{$key}\b/i) {
692 return($slants{$key});
700 my ($fontname, $style) = @_;
701 for my $key (keys %spacings) {
702 next if ($key !~ /^\d+$/);
703 if ($style =~ /\b$spacings{$key}\b/i) {
704 return($spacings{$key});
707 if ("$fontname $style" =~ /(mono|typewriter|cursor|fixed)\b/i) {
708 return($spacings{100}); # Mono
717 my ($fontname, $rCapability) = @_;
719 return 1 if ($fontname =~ /math/i);
720 for my $cap (@{$rCapability}) {
721 return 1 if ($cap eq "math");
726 sub getproperties($$$$)
728 my ($l, $fontname, $style, $rerrors) = @_;
729 my $newstyle = &correctstyle($style);
730 my $newfam = &correctstyle($fontname);
733 for my $txt (qw(ftype weight width slant spacing)) {
735 eval("\$map = " . '\%' . $txt . 's');
736 eval('$rget = \&' . "get$txt");
737 my $val2 = getsinglevalue($l, $txt, $map);
738 my $val1 = $rget->($newfam, $newstyle);
740 if (defined($val2) && defined($val1) && ($val2 ne $val1)) {
741 push(@{$rerrors}, "Fontname($fontname),Style($style): Values for $txt ($val1 != $val2) differ, selecting internal $txt($val2)");
744 elsif (! defined($val2)) {
751 push(@properties, &addTxt($txt,$val));
754 if (defined($map->{"default"})) {
755 push(@properties, &addTxt($txt,$map->{"default"}));
758 push(@{$rerrors}, "Undefined value for $txt");
762 return(join(' ', @properties));
768 $style =~ s/^\\040//;
769 $style =~ s/^\s*\d+\s*//;
770 $style =~ s/\s*\d+$//;
771 $style =~ s/italic/ Italic/i;
772 $style =~ s/oblique/ Oblique/i;
773 $style =~ s/[\-_]/ /g;
774 $style =~ s/\breg\b/Regular/i;
775 $style =~ s/\bregita(lic)?\b/Regular Italic/i;
776 $style =~ s/\bregobl(ique)?\b/Regular Oblique/i;
777 $style =~ s/medium/Medium /i;
778 $style =~ s/\bmedita(lic)?\b/Medium Italic/i;
779 $style =~ s/\bmedobl(ique)?\b/Medium Oblique/i;
780 $style =~ s/\bmed\b/Medium /i;
781 $style =~ s/\bdemi\b/SemiBold/i;
782 $style =~ s/\bex(pd|t)\b/Expanded/i;
783 $style =~ s/semi ?cond(ensed)?/SemiCondensed/i;
784 $style =~ s/[sd]emi ?(bold|bd|bol)/SemiBold/i;
785 $style =~ s/semi ?(expanded|extended|expd)/SemiExpanded/i;
786 $style =~ s/[sd]emi ?light/SemiLight/i;
787 $style =~ s/ultra ?(expanded|extended|expd)/UltraExpanded/i;
788 $style =~ s/light/Light /i;
789 $style =~ s/\blt\b/Light /i;
790 $style =~ s/(ultra|extra)(light|lt)/ExtraLight /i;
791 $style =~ s/\bheavy\b/Extrabold/i;
792 $style =~ s/\bhairline\b/Extralight/i;
793 $style =~ s/\bcond\b/Condensed/i;
794 $style =~ s/(roman)?slanted/ Italic/i;
795 $style =~ s/\bslant\b/Italic/i;
796 $style =~ s/\b(SC|Small(caps(alt)?)?)\b/SmallCaps/i;
797 $style =~ s/w3 mono/Dual/i;
798 $style =~ s/Regul[ea]r/Regular/i;