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 "d" => qr/^(dancing)/i,
246 "e" => qr/^(elegante)/i,
247 "k" => qr/^(kaushan|karumbi)/i,
248 "m" => qr/^(mathjax_script|miama)/i,
249 "n" => qr/^(nanum (brush|pen) script)/i,
250 "q" => qr/^qt(arabian|boulevard|brushstroke|coronation|florencia|handwriting|linostroke|merry|pandora)/i,
251 "r" => qr/^(romande.*|ruf)script/i,
252 "u" => qr/^(un ?pilgi)/i,
256 "value" => 120, # Fraktur
257 "j" => qr/^(jsmath.?euf)/i,
258 "m" => qr/^(missaali)/i,
259 "o" => qr/^(oldania)/i,
260 "q" => qr/^qt(blackforest|cloisteredmonk|dublinirish|fraktur|heidelbergtype|(lino|london)scroll)/i,
264 "value" => 130, # Fancy
265 "c" => qr/^(cretino)/i,
266 "g" => qr/^(gfs.?theo)/i,
270 "value" => 140, # Initials
271 "e" => qr/^(eb.?garamond.?init)/i,
272 "l" => qr/^(libertinus|linux).*initials/i,
273 "y" => qr/^(yinit)/i,
277 "value" => 200, # Symbol
278 "a" => qr/^(academicons)/i,
279 "c" => qr/^(caladings|ccicons)/i,
280 "d" => qr/^(dingbats|drmsym)/i,
281 "f" => qr/^(fdsymbol|fourierorns)/i,
283 "m" => qr/^(marvosym)/i,
284 "n" => qr/^(noto.*emoji)/i,
285 "q" => qr/^(qtdingbits)/i,
288 if (open(FI, "$cmd |")) {
289 NXTLINE: while (my $l = <FI>) {
291 while ($l !~ /abcd$/) {
297 if ($l =~ /file=\"([^\"]+)\"/) {
299 #next if ($file !~ /\.(otf|ttf|pfa|pfb|pcf|ttc)$/i);
300 if ($file !~ /\.([a-z0-9]{2,5})$/i) {
301 print "Unhandled extension for file $file\n";
305 if (! defined($fontpriority{$fonttype})) {
306 print "Added extension $fonttype for file $file\n";
307 $fontpriority{$fonttype} = $nexttype;
313 if ($l =~ / lang=\"([^\"]+)\"/) {
314 my @ll = split(/\|/, $1);
316 $usedlangs{&convertlang($lx)} = 1;
320 for my $lang (@langs) {
321 next NXTLINE if (! defined($usedlangs{$lang}));
324 my $style = &getVal($l, "style", "stylelang");
325 $style =~ s/^\\040//;
326 my $fullname = &getVal($l, "fn", "fnl");
327 my $postscriptname = "";
328 if ($l =~ /postscriptname=\"([^\"]+)\"/) {
329 $postscriptname = $1;
331 my $family = &getVal($l, "family", "flang");
332 $family =~ s/\\040/\-/;
334 if (length($fullname) < 3) {
335 if (length($postscriptname) < 2) {
336 $fontname = "$family $style";
339 $fontname = $postscriptname;
343 $fontname = $fullname;
345 if (defined($options{NFontName})) {
346 for my $fn (@{$options{NFontName}}) {
347 next NXTLINE if ($fontname =~ /$fn/i);
350 if (defined($options{FontName})) {
351 for my $fn (@{$options{FontName}}) {
352 next NXTLINE if ($fontname !~ /$fn/i);
357 if (exists($options{PrintProperties}) || defined($options{Property}) || defined($options{NProperty})) {
358 my $properties = getproperties($l, $fontname, $style, \@errors);
359 if (defined($options{Property})) {
360 for my $pn (@{$options{Property}}) {
361 next NXTLINE if ($properties !~ /$pn/i);
364 if (defined($options{NProperty})) {
365 for my $pn (@{$options{NProperty}}) {
366 next NXTLINE if ($properties =~ /$pn/i);
369 if (exists($options{PrintProperties})) {
370 $props .= " ($properties)";
374 if (exists($options{PrintLangs})) {
375 $props .= '(' . join(',', sort keys %usedlangs) . ')';
377 if (exists($options{PrintScripts}) || defined($options{Scripts}) || defined($options{NScripts}) || exists($options{Math})) {
380 if ($l =~ / script=\"([^\"]+)\"/) {
381 @scripts = split(/\s+/, $1);
382 for my $ent (@scripts) {
383 $ent =~ s/^\s*otlayout://;
386 $scripts = join(',', @scripts);
388 if (exists($options{Math})) {
389 next NXTLINE if (! &ismathfont($fontname,\@scripts));
391 if (exists($options{PrintScripts})) {
392 $props .= "($scripts)";
394 if (!defined($scripts[0])) {
395 # No script defined in font, so check only $options{Scripts}
396 next NXTLINE if (defined($options{Scripts}));
399 if (defined($options{Scripts})) {
400 for my $s (@{$options{Scripts}}) {
401 next NXTLINE if ($scripts !~ /$s/i);
404 if (defined($options{NScripts})) {
405 for my $s (@{$options{NScripts}}) {
406 next NXTLINE if ($scripts =~ /$s/i);
412 if ($l =~ /foundry=\"([^\"]+)\"/) {
414 $foundry =~ s/^\s+//;
415 $foundry =~ s/\s+$//;
417 if (defined($collectedfonts{$fontname}->{$foundry}->{errors})) {
418 # Apparently not the first one, so add some info
419 my $oldfonttype = $collectedfonts{$fontname}->{$foundry}->{fonttype};
420 if (defined($errors[0])) {
421 push(@{$collectedfonts{$fontname}->{$foundry}->{errors}}, @errors);
423 if ($fontpriority{$oldfonttype} > $fontpriority{$fonttype}) {
424 push(@{$collectedfonts{$fontname}->{$foundry}->{errors}}, "Warning: overwriting old info for file: " . $collectedfonts{$fontname}->{$foundry}->{file});
427 push(@{$collectedfonts{$fontname}->{$foundry}->{errors}}, "Warning: discarding new info for file: $file");
432 $collectedfonts{$fontname}->{$foundry}->{errors} = \@errors;
434 $collectedfonts{$fontname}->{$foundry}->{props} = $props;
435 $collectedfonts{$fontname}->{$foundry}->{file} = $file;
436 $collectedfonts{$fontname}->{$foundry}->{fonttype} = $fonttype;
441 for my $fontname (sort keys %collectedfonts) {
442 my @foundries = sort keys %{$collectedfonts{$fontname}};
443 my $printfoundries = 0;
444 if (defined($foundries[1])) {
447 for my $foundry (@foundries) {
448 if (exists($options{PrintWarnings})) {
449 for my $err (@{$collectedfonts{$fontname}->{$foundry}->{errors}}) {
453 my $fn = "Font : $fontname";
454 if ($printfoundries && ($foundry ne "")) {
455 $fn .= " \[$foundry\]";
458 print $collectedfonts{$fontname}->{$foundry}->{props};
459 if (exists($options{PrintFiles})) {
460 print ": " . $collectedfonts{$fontname}->{$foundry}->{file} . "\n";
469 #################################################################################
473 if ($ilang =~ /^\s*([a-z]+)([\-_]([a-z]+))?\s*$/i) {
474 my ($x, $y) = ($1, $3);
476 $ilang = lc($x) . '-' . lc($y);
487 my ($l, $islang, $txt, $rres) = @_;
489 if ($l =~ /$txt=\"([^\"]+)\"/) {
490 @res = split(',', $1);
493 $lg = &convertlang($lg);
502 my ($lang, $rlangs) = @_;
505 for (my $i = 0; defined($rlangs->[$i]); $i++) {
506 if ($rlangs->[$i] eq $lang) {
515 my ($l, $txtval, $txtlang) = @_;
518 &extractlist($l, 0, $txtval, \@values);
519 return("") if (! defined($values[0]));
520 &extractlist($l, 1, $txtlang, \@langs);
521 my $i = &getIndexes("en", \@langs);
524 if (defined($values[$k]) && (length($values[$k]) > length($res))) {
528 return($values[0]) if ($res eq "");
532 sub getsinglevalue($$$)
534 my ($l, $txt, $rMap, $rget) = @_;
536 if ($l =~ / $txt=(\d+)/) {
538 # Search for nearest value to $val
539 if (defined($rMap->{$val})) {
540 return($rMap->{$val});
544 for my $key (keys %{$rMap}) {
545 next if ($key !~ /^\d+$/);
546 my $diff = abs($key - $val);
551 elsif ($diff == $minv) {
557 if (! defined($rMap->{$maxv})) {
558 print "ERROR2: txt=$txt, val=$val\n";
562 return($rMap->{$maxv} . "+$minv");
565 return($rMap->{$maxv} . "-$minv");
575 my ($txt, $val) = @_;
576 return("$txt($val)");
581 my ($fontname, $style) = @_;
582 if ($fontname =~ /(sans)[-_ ]?(serif)?/i) {
583 return($ftypes{100}); # Sans Serif
585 elsif ($fontname =~ /gothic|dotum|gulim/i) {
586 if ($fontname =~ /bisrat gothic/i) {
587 return($ftypes{0}); # Serif
590 return($ftypes{100}); # Sans Serif
593 elsif ($fontname =~ /serif|times|mincho|batang/i) {
594 if ($fontname =~ /good times/i) {
595 return($ftypes{100}); # Sans Serif
597 elsif ($fontname !~ /initials/i) {
598 return($ftypes{0}); # Serif
601 # Now check for fonts without a hint in font name
602 if ($fontname =~ /([a-z])/i) {
604 for my $rFonts (\%sansFonts, \%scriptFonts, \%fraktFonts, \%fancyFonts, \%initialFonts, \%symbolFonts) {
605 if (defined($rFonts->{$key})) {
606 if ($fontname =~ $rFonts->{$key}) {
607 return($ftypes{$rFonts->{"value"}});
612 if ("$fontname" =~ /^bpg/i) {
613 if ("$fontname" =~ /bpg (courier gpl|elite)/i) {
614 return($ftypes{0}); # Serif
617 return($ftypes{100}); # Sans Serif
620 elsif ("$fontname" =~ /^dustismo/i) {
621 if ("$fontname" =~ /^dustismo roman/i) {
622 return($ftypes{0}); # Serif
625 return($ftypes{100}); # Sans Serif
628 elsif ("$fontname" =~ /^go\b/i) {
629 if ("$fontname" =~ /^go mono/i) {
630 return($ftypes{0}); # Serif
633 return($ftypes{100}); # Sans Serif
643 my ($fontname, $style) = @_;
645 for my $key (keys %weights) {
646 next if ($key !~ /^\d+$/);
647 my $val = $weights{$key};
648 for my $info ($style, $fontname) {
649 if ($info =~ /\b$val\b/i) {
650 if ($val eq "Regular") {
651 $result = $val; # It may refer to width
664 my ($fontname, $style) = @_;
666 for my $key (keys %widths) {
667 next if ($key !~ /^\d+$/);
668 for my $info ($style, $fontname) {
669 if ($info =~ /\b$widths{$key}\b/i) {
670 return($widths{$key});
672 if ($info =~ /\bRegular\b/) {
673 if (!defined($result)) {
674 $result = $widths{100};
684 my ($fontname, $style) = @_;
685 for my $key (keys %slants) {
686 next if ($key !~ /^\d+$/);
687 if ($style =~ /\b$slants{$key}\b/i) {
688 return($slants{$key});
696 my ($fontname, $style) = @_;
697 for my $key (keys %spacings) {
698 next if ($key !~ /^\d+$/);
699 if ($style =~ /\b$spacings{$key}\b/i) {
700 return($spacings{$key});
703 if ("$fontname $style" =~ /(mono|typewriter|cursor|fixed)\b/i) {
704 return($spacings{100}); # Mono
713 my ($fontname, $rCapability) = @_;
715 return 1 if ($fontname =~ /math/i);
716 for my $cap (@{$rCapability}) {
717 return 1 if ($cap eq "math");
722 sub getproperties($$$$)
724 my ($l, $fontname, $style, $rerrors) = @_;
725 my $newstyle = &correctstyle($style);
726 my $newfam = &correctstyle($fontname);
729 for my $txt (qw(ftype weight width slant spacing)) {
731 eval("\$map = " . '\%' . $txt . 's');
732 eval('$rget = \&' . "get$txt");
733 my $val2 = getsinglevalue($l, $txt, $map);
734 my $val1 = $rget->($newfam, $newstyle);
736 if (defined($val2) && defined($val1) && ($val2 ne $val1)) {
737 push(@{$rerrors}, "Fontname($fontname),Style($style): Values for $txt ($val1 != $val2) differ, selecting internal $txt($val2)");
740 elsif (! defined($val2)) {
747 push(@properties, &addTxt($txt,$val));
750 if (defined($map->{"default"})) {
751 push(@properties, &addTxt($txt,$map->{"default"}));
754 push(@{$rerrors}, "Undefined value for $txt");
758 return(join(' ', @properties));
764 $style =~ s/^\\040//;
765 $style =~ s/^\s*\d+\s*//;
766 $style =~ s/\s*\d+$//;
767 $style =~ s/italic/ Italic/i;
768 $style =~ s/oblique/ Oblique/i;
769 $style =~ s/[\-_]/ /g;
770 $style =~ s/\breg\b/Regular/i;
771 $style =~ s/\bregita(lic)?\b/Regular Italic/i;
772 $style =~ s/\bregobl(ique)?\b/Regular Oblique/i;
773 $style =~ s/medium/Medium /i;
774 $style =~ s/\bmedita(lic)?\b/Medium Italic/i;
775 $style =~ s/\bmedobl(ique)?\b/Medium Oblique/i;
776 $style =~ s/\bmed\b/Medium /i;
777 $style =~ s/\bdemi\b/SemiBold/i;
778 $style =~ s/\bex(pd|t)\b/Expanded/i;
779 $style =~ s/semi ?cond(ensed)?/SemiCondensed/i;
780 $style =~ s/[sd]emi ?(bold|bd|bol)/SemiBold/i;
781 $style =~ s/semi ?(expanded|extended|expd)/SemiExpanded/i;
782 $style =~ s/[sd]emi ?light/SemiLight/i;
783 $style =~ s/ultra ?(expanded|extended|expd)/UltraExpanded/i;
784 $style =~ s/light/Light /i;
785 $style =~ s/\blt\b/Light /i;
786 $style =~ s/(ultra|extra)(light|lt)/ExtraLight /i;
787 $style =~ s/\bheavy\b/Extrabold/i;
788 $style =~ s/\bhairline\b/Extralight/i;
789 $style =~ s/\bcond\b/Condensed/i;
790 $style =~ s/(roman)?slanted/ Italic/i;
791 $style =~ s/\bslant\b/Italic/i;
792 $style =~ s/\b(SC|Small(caps(alt)?)?)\b/SmallCaps/i;
793 $style =~ s/w3 mono/Dual/i;
794 $style =~ s/Regul[ea]r/Regular/i;