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'
53 "l" => {fieldname => "Lang",
54 type => "=s", alias=>["lang"],
55 comment => "Comma separated list of desired languages"},
56 "math" => {fieldname => "Math",
57 comment => "Select fonts probably containing math glyphs"},
58 "n" => {fieldname => "FontName", listsep => ',',
59 type => "=s", alias => ["name"],
60 comment => "Select font-names matching these (comma separated) regexes"},
61 "nn" => {fieldname => "NFontName",
62 type => "=s", listsep => ',',
63 comment => "Select font-names NOT matching these (comma separated) regexes"},
64 "pl" => {fieldname => "PrintLangs", alias => ["printlangs"],
65 comment => "Print supported languages"},
66 "pf" => {fieldname => "PrintFiles", alias => ["printfiles"],
67 comment => "Print font file names"},
68 "p" => {fieldname => "Property",
69 type => "=s", listsep => ',',
70 comment => "Select fonts with properties matching these (comma separated) regexes"},
71 "np" => {fieldname => "NProperty",
72 type => "=s", listsep => ',',
73 comment => "Select fonts with properties NOT matching these (comma separated) regexes"},
74 "pp" => {fieldname => "PrintProperties", alias => ["printproperties"],
75 comment => "Print properties from weight, slant and width"},
76 "s" => {fieldname => "Scripts",
77 type => "=s", listsep => ',',
78 comment => "Select fonts with scripts matching these (comma separated) regexes"},
79 "ns" => {fieldname => "NScripts",
80 type => "=s", listsep => ',',
81 comment => "Select fonts with scripts NOT matching these (comma separated) regexes"},
82 "ps" => {fieldname => "PrintScripts", alias => ["printscripts"],
83 comment => "Print supported scripts"},
84 "pw" => {fieldname => "PrintWarnings",
85 comment => "Print warnings about discarded/overwritten fonts, conflicting styles"},
87 my %options = %{&handleOptions(\%optionsDef)};
89 $options{Lang} = "" if (! defined($options{Lang}));
91 #############################################################
93 my @langs = split(',', $options{Lang});
95 $lg = &convertlang($lg);
99 if (defined($langs[0])) {
100 $cmd .= " :lang=" . join(',', @langs);
103 my $format = "foundry=\"%{foundry}\"" .
104 " postscriptname=\"%{postscriptname}\"" .
105 " fn=\"%{fullname}\" fnl=\"%{fullnamelang}\"" .
106 " family=\"%{family}\" flang=\"%{familylang}\" " .
107 " style=\"%{style}\" stylelang=\"%{stylelang}\"";
109 if (exists($options{PrintScripts}) || defined($options{Scripts}) || defined($options{NSpripts}) || exists($options{Math})) {
110 $format .= " script=\"%{capability}\"";
112 if (exists($options{PrintLangs}) || defined($langs[0])) {
113 $format .= " lang=\"%{lang}\"";
115 if (exists($options{PrintProperties}) || defined($options{Property})) {
116 $format .= " weight=%{weight} slant=%{slant} width=%{width} spacing=%{spacing}";
118 $format .= " file=\"%{file}\" abcd\\n";
119 $cmd .= " -f '$format'";
131 "default" => "Serif",
155 50 => "Ultracondensed",
156 63 => "Extracondensed",
158 87 => "Semicondensed",
160 113 => "Semiexpanded",
162 150 => "Extraexpanded",
163 200 => "Ultraexpanded",
171 "default" => "Proportional",
174 # Build reverse mappings, (not needed yet)
175 for my $txt (qw(ftypes weights slants widths spacings)) {
177 eval "\$map = \\%$txt";
178 for my $key (keys %{$map}) {
179 next if ($key !~ /^\d+$/);
180 my $value = lc($map->{$key});
181 $map->{"r"}->{$value} = $key;
188 my %collectedfonts = ();
190 otf => 0, # type 2, opentype CFF (Compact Font Format)
191 ttc => 1.1, # type 1 (True Type font Collection)
192 ttf => 1.2, # type 1 (True Type Font)
193 woff=> 1.3, # type 1 (Web Open Font Format)
194 t1 => 1.4, # type 1 (postscript)
195 pfb => 1.5, # type 1 (Printer Font Binary)
196 pfa => 1.6, # type 1 (Printer Font Ascii)
197 pcf => 5, # Bitmap (Packaged Collaboration Files)?
201 # list of regexes for known sans serif fonts
203 "value" => 100, # Sans serif
204 "a" => qr/^(arial|andika|angostura|anonymous|arab|aroania|arimo|asap)/i,
205 "b" => qr/^b(aekmuk|ebas|erenika|eteckna|euron|lue)/i,
206 "c" => qr/^c(abin|aliban|antarell|arbon|arlito|handas|hivo|mu bright|omfortaa|omic|oolvetica|ortoba|ousine|uprum|wtex(hei|yen)|yklop|ypro)/i,
207 "d" => qr/^(d2coding|dimnah|dosis|dyuthi)/i,
208 "e" => qr/^(electron|engebrechtre)/i,
209 "f" => qr/^(fandolhei|fetamont|fira|font awesome 5|forgotten)/i,
210 "g" => qr/^(gardiner|garuda|gfs ?neo|gillius|granada|graph|guanine|gunplay)/i,
211 "h" => qr/^(hack|hani|haramain|harano|harmattan|hor\b)/i,
212 "i" => qr/^(ibm plex|ikarius|inconsolata|induni.?h|iwona)/i,
213 "j" => qr/^(jara|jura)/i,
214 "k" => qr/^(kalimati|kanji|karla|kayrawan|kenyan|keraleeyam|khalid|khmer [or]|kiloji|klaudia|komatu|kurier)/i,
215 "l" => qr/^l(aksaman|arabie|ato|eague|exend|exigulim|ibel|iberation|ibre franklin|ibris|inux biolinum|obster|ogix|ohit|oma)/i,
216 "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,
217 "n" => qr/^(nachlieli|nada|nafees|nagham|nanum(barunpen|square)|nice)/i,
218 "o" => qr/^(ocr|okolaks|opendyslexic|ostorah|ouhud|over|oxygen)/i,
219 "p" => qr/^(padauk|padmaa|pagul|paktype|pakenham|palladio|petra|phetsarath|play\b|poiret|port\b|primer\b|prociono|pt\b|purisa)/i,
220 "q" => qr/^(qt(ancient|helvet|avanti|doghaus|eratype|eurotype|floraline|frank|fritz|future|greece|howard|letter|optimum)|quercus)/i,
221 "r" => qr/^(rachana|radio\b|raleway|ricty|roboto|rosario)/i,
222 "s" => qr/^(salem|samanata|sawasdee|shado|sharja|simple|sophia|soul|source|switzera)/i,
223 "t" => qr/^(tarablus|teen|texgyre(adventor|heros)|tiresias|trebuchet|tscu|tuffy)/i,
224 "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,
225 "v" => qr/^(veranda|vn ?urwclassico)/i,
226 "w" => qr/^(waree)/i,
227 "y" => qr/^(yanone)/i,
228 "z" => qr/^(zekton|zero)/i,
231 "value" => 110, # Script
232 "d" => qr/^(dancing)/i,
233 "e" => qr/^(elegante)/i,
234 "k" => qr/^(kaushan|karumbi)/i,
235 "m" => qr/^(mathjax_script|miama)/i,
236 "n" => qr/^(nanum (brush|pen) script)/i,
237 "q" => qr/^qt(arabian|boulevard|brushstroke|coronation|florencia|handwriting|linostroke|merry|pandora)/i,
238 "r" => qr/^(romande.*|ruf)script/i,
239 "u" => qr/^(un ?pilgi)/i,
243 "value" => 120, # Fraktur
244 "j" => qr/^(jsmath.?euf)/i,
245 "m" => qr/^(missaali)/i,
246 "o" => qr/^(oldania)/i,
247 "q" => qr/^qt(blackforest|cloisteredmonk|dublinirish|fraktur|heidelbergtype|(lino|london)scroll)/i,
251 "value" => 130, # Fancy
252 "c" => qr/^(cretino)/i,
253 "g" => qr/^(gfs.?theo)/i,
257 "value" => 140, # Initials
258 "e" => qr/^(eb.?garamond.?init)/i,
259 "l" => qr/^(libertinus|linux).*initials/i,
260 "y" => qr/^(yinit)/i,
264 "value" => 200, # Symbol
265 "a" => qr/^(academicons)/i,
266 "c" => qr/^(caladings|ccicons)/i,
267 "d" => qr/^(dingbats|drmsym)/i,
268 "f" => qr/^(fdsymbol|fourierorns)/i,
270 "m" => qr/^(marvosym)/i,
271 "n" => qr/^(noto.*emoji)/i,
272 "q" => qr/^(qtdingbits)/i,
275 if (open(FI, "$cmd |")) {
276 NXTLINE: while (my $l = <FI>) {
278 while ($l !~ /abcd$/) {
284 if ($l =~ /file=\"([^\"]+)\"/) {
286 #next if ($file !~ /\.(otf|ttf|pfa|pfb|pcf|ttc)$/i);
287 if ($file !~ /\.([a-z0-9]{2,5})$/i) {
288 print "Unhandled extension for file $file\n";
292 if (! defined($fontpriority{$fonttype})) {
293 print "Added extension $fonttype for file $file\n";
294 $fontpriority{$fonttype} = $nexttype;
300 if ($l =~ / lang=\"([^\"]+)\"/) {
301 my @ll = split(/\|/, $1);
303 $usedlangs{&convertlang($lx)} = 1;
307 for my $lang (@langs) {
308 next NXTLINE if (! defined($usedlangs{$lang}));
311 my $style = &getVal($l, "style", "stylelang");
312 $style =~ s/^\\040//;
313 my $fullname = &getVal($l, "fn", "fnl");
314 my $postscriptname = "";
315 if ($l =~ /postscriptname=\"([^\"]+)\"/) {
316 $postscriptname = $1;
318 my $family = &getVal($l, "family", "flang");
319 $family =~ s/\\040/\-/;
321 if (length($fullname) < 3) {
322 if (length($postscriptname) < 2) {
323 $fontname = "$family $style";
326 $fontname = $postscriptname;
330 $fontname = $fullname;
332 if (defined($options{NFontName})) {
333 for my $fn (@{$options{NFontName}}) {
334 next NXTLINE if ($fontname =~ /$fn/i);
337 if (defined($options{FontName})) {
338 for my $fn (@{$options{FontName}}) {
339 next NXTLINE if ($fontname !~ /$fn/i);
344 if (exists($options{PrintProperties}) || defined($options{Property}) || defined($options{NProperty})) {
345 my $properties = getproperties($l, $fontname, $style, \@errors);
346 if (defined($options{Property})) {
347 for my $pn (@{$options{Property}}) {
348 next NXTLINE if ($properties !~ /$pn/i);
351 if (defined($options{NProperty})) {
352 for my $pn (@{$options{NProperty}}) {
353 next NXTLINE if ($properties =~ /$pn/i);
356 if (exists($options{PrintProperties})) {
357 $props .= " ($properties)";
361 if (exists($options{PrintLangs})) {
362 $props .= '(' . join(',', sort keys %usedlangs) . ')';
364 if (exists($options{PrintScripts}) || defined($options{Scripts}) || defined($options{NScripts}) || exists($options{Math})) {
367 if ($l =~ / script=\"([^\"]+)\"/) {
368 @scripts = split(/\s+/, $1);
369 for my $ent (@scripts) {
370 $ent =~ s/^\s*otlayout://;
373 $scripts = join(',', @scripts);
375 if (exists($options{Math})) {
376 next NXTLINE if (! &ismathfont($fontname,\@scripts));
378 if (exists($options{PrintScripts})) {
379 $props .= "($scripts)";
381 if (!defined($scripts[0])) {
382 # No script defined in font, so check only $options{Scripts}
383 next NXTLINE if (defined($options{Scripts}));
386 if (defined($options{Scripts})) {
387 for my $s (@{$options{Scripts}}) {
388 next NXTLINE if ($scripts !~ /$s/i);
391 if (defined($options{NScripts})) {
392 for my $s (@{$options{NScripts}}) {
393 next NXTLINE if ($scripts =~ /$s/i);
399 if ($l =~ /foundry=\"([^\"]+)\"/) {
401 $foundry =~ s/^\s+//;
402 $foundry =~ s/\s+$//;
404 if (defined($collectedfonts{$fontname}->{$foundry}->{errors})) {
405 # Apparently not the first one, so add some info
406 my $oldfonttype = $collectedfonts{$fontname}->{$foundry}->{fonttype};
407 if (defined($errors[0])) {
408 push(@{$collectedfonts{$fontname}->{$foundry}->{errors}}, @errors);
410 if ($fontpriority{$oldfonttype} > $fontpriority{$fonttype}) {
411 push(@{$collectedfonts{$fontname}->{$foundry}->{errors}}, "Warning: overwriting old info for file: " . $collectedfonts{$fontname}->{$foundry}->{file});
414 push(@{$collectedfonts{$fontname}->{$foundry}->{errors}}, "Warning: discarding new info for file: $file");
419 $collectedfonts{$fontname}->{$foundry}->{errors} = \@errors;
421 $collectedfonts{$fontname}->{$foundry}->{props} = $props;
422 $collectedfonts{$fontname}->{$foundry}->{file} = $file;
423 $collectedfonts{$fontname}->{$foundry}->{fonttype} = $fonttype;
428 for my $fontname (sort keys %collectedfonts) {
429 my @foundries = sort keys %{$collectedfonts{$fontname}};
430 my $printfoundries = 0;
431 if (defined($foundries[1])) {
434 for my $foundry (@foundries) {
435 if (exists($options{PrintWarnings})) {
436 for my $err (@{$collectedfonts{$fontname}->{$foundry}->{errors}}) {
440 my $fn = "Font : $fontname";
441 if ($printfoundries && ($foundry ne "")) {
442 $fn .= " \[$foundry\]";
445 print $collectedfonts{$fontname}->{$foundry}->{props};
446 if (exists($options{PrintFiles})) {
447 print ": " . $collectedfonts{$fontname}->{$foundry}->{file} . "\n";
456 #################################################################################
460 if ($ilang =~ /^\s*([a-z]+)([\-_]([a-z]+))?\s*$/i) {
461 my ($x, $y) = ($1, $3);
463 $ilang = lc($x) . '-' . lc($y);
474 my ($l, $islang, $txt, $rres) = @_;
476 if ($l =~ /$txt=\"([^\"]+)\"/) {
477 @res = split(',', $1);
480 $lg = &convertlang($lg);
489 my ($lang, $rlangs) = @_;
492 for (my $i = 0; defined($rlangs->[$i]); $i++) {
493 if ($rlangs->[$i] eq $lang) {
502 my ($l, $txtval, $txtlang) = @_;
505 &extractlist($l, 0, $txtval, \@values);
506 return("") if (! defined($values[0]));
507 &extractlist($l, 1, $txtlang, \@langs);
508 my $i = &getIndexes("en", \@langs);
511 if (defined($values[$k]) && (length($values[$k]) > length($res))) {
515 return($values[0]) if ($res eq "");
519 sub getsinglevalue($$$)
521 my ($l, $txt, $rMap, $rget) = @_;
523 if ($l =~ / $txt=(\d+)/) {
525 # Search for nearest value to $val
526 if (defined($rMap->{$val})) {
527 return($rMap->{$val});
531 for my $key (keys %{$rMap}) {
532 next if ($key !~ /^\d+$/);
533 my $diff = abs($key - $val);
538 elsif ($diff == $minv) {
544 if (! defined($rMap->{$maxv})) {
545 print "ERROR2: txt=$txt, val=$val\n";
549 return($rMap->{$maxv} . "+$minv");
552 return($rMap->{$maxv} . "-$minv");
562 my ($txt, $val) = @_;
563 return("$txt($val)");
568 my ($fontname, $style) = @_;
569 if ($fontname =~ /(sans)[-_ ]?(serif)?/i) {
570 return($ftypes{100}); # Sans Serif
572 elsif ($fontname =~ /gothic|dotum|gulim/i) {
573 if ($fontname =~ /bisrat gothic/i) {
574 return($ftypes{0}); # Serif
577 return($ftypes{100}); # Sans Serif
580 elsif ($fontname =~ /serif|times|mincho|batang/i) {
581 if ($fontname =~ /good times/i) {
582 return($ftypes{100}); # Sans Serif
584 elsif ($fontname !~ /initials/i) {
585 return($ftypes{0}); # Serif
588 # Now check for fonts without a hint in font name
589 if ($fontname =~ /([a-z])/i) {
591 for my $rFonts (\%sansFonts, \%scriptFonts, \%fraktFonts, \%fancyFonts, \%initialFonts, \%symbolFonts) {
592 if (defined($rFonts->{$key})) {
593 if ($fontname =~ $rFonts->{$key}) {
594 return($ftypes{$rFonts->{"value"}});
599 if ("$fontname" =~ /^bpg/i) {
600 if ("$fontname" =~ /bpg (courier gpl|elite)/i) {
601 return($ftypes{0}); # Serif
604 return($ftypes{100}); # Sans Serif
607 elsif ("$fontname" =~ /^dustismo/i) {
608 if ("$fontname" =~ /^dustismo roman/i) {
609 return($ftypes{0}); # Serif
612 return($ftypes{100}); # Sans Serif
615 elsif ("$fontname" =~ /^go\b/i) {
616 if ("$fontname" =~ /^go mono/i) {
617 return($ftypes{0}); # Serif
620 return($ftypes{100}); # Sans Serif
630 my ($fontname, $style) = @_;
632 for my $key (keys %weights) {
633 next if ($key !~ /^\d+$/);
634 my $val = $weights{$key};
635 for my $info ($style, $fontname) {
636 if ($info =~ /\b$val\b/i) {
637 if ($val eq "Regular") {
638 $result = $val; # It may refer to width
651 my ($fontname, $style) = @_;
653 for my $key (keys %widths) {
654 next if ($key !~ /^\d+$/);
655 for my $info ($style, $fontname) {
656 if ($info =~ /\b$widths{$key}\b/i) {
657 return($widths{$key});
659 if ($info =~ /\bRegular\b/) {
660 if (!defined($result)) {
661 $result = $widths{100};
671 my ($fontname, $style) = @_;
672 for my $key (keys %slants) {
673 next if ($key !~ /^\d+$/);
674 if ($style =~ /\b$slants{$key}\b/i) {
675 return($slants{$key});
683 my ($fontname, $style) = @_;
684 for my $key (keys %spacings) {
685 next if ($key !~ /^\d+$/);
686 if ($style =~ /\b$spacings{$key}\b/i) {
687 return($spacings{$key});
690 if ("$fontname $style" =~ /(mono|typewriter|cursor|fixed)\b/i) {
691 return($spacings{100}); # Mono
700 my ($fontname, $rCapability) = @_;
702 return 1 if ($fontname =~ /math/i);
703 for my $cap (@{$rCapability}) {
704 return 1 if ($cap eq "math");
709 sub getproperties($$$$)
711 my ($l, $fontname, $style, $rerrors) = @_;
712 my $newstyle = &correctstyle($style);
713 my $newfam = &correctstyle($fontname);
716 for my $txt (qw(ftype weight width slant spacing)) {
718 eval("\$map = " . '\%' . $txt . 's');
719 eval('$rget = \&' . "get$txt");
720 my $val2 = getsinglevalue($l, $txt, $map);
721 my $val1 = $rget->($newfam, $newstyle);
723 if (defined($val2) && defined($val1) && ($val2 ne $val1)) {
724 push(@{$rerrors}, "Fontname($fontname),Style($style): Values for $txt ($val1 != $val2) differ, selecting internal $txt($val2)");
727 elsif (! defined($val2)) {
734 push(@properties, &addTxt($txt,$val));
737 if (defined($map->{"default"})) {
738 push(@properties, &addTxt($txt,$map->{"default"}));
741 push(@{$rerrors}, "Undefined value for $txt");
745 return(join(' ', @properties));
751 $style =~ s/^\\040//;
752 $style =~ s/^\s*\d+\s*//;
753 $style =~ s/\s*\d+$//;
754 $style =~ s/italic/ Italic/i;
755 $style =~ s/oblique/ Oblique/i;
756 $style =~ s/[\-_]/ /g;
757 $style =~ s/\breg\b/Regular/i;
758 $style =~ s/\bregita(lic)?\b/Regular Italic/i;
759 $style =~ s/\bregobl(ique)?\b/Regular Oblique/i;
760 $style =~ s/medium/Medium /i;
761 $style =~ s/\bmedita(lic)?\b/Medium Italic/i;
762 $style =~ s/\bmedobl(ique)?\b/Medium Oblique/i;
763 $style =~ s/\bmed\b/Medium /i;
764 $style =~ s/\bdemi\b/SemiBold/i;
765 $style =~ s/\bex(pd|t)\b/Expanded/i;
766 $style =~ s/semi ?cond(ensed)?/SemiCondensed/i;
767 $style =~ s/[sd]emi ?(bold|bd|bol)/SemiBold/i;
768 $style =~ s/semi ?(expanded|extended|expd)/SemiExpanded/i;
769 $style =~ s/[sd]emi ?light/SemiLight/i;
770 $style =~ s/ultra ?(expanded|extended|expd)/UltraExpanded/i;
771 $style =~ s/light/Light /i;
772 $style =~ s/\blt\b/Light /i;
773 $style =~ s/(ultra|extra)(light|lt)/ExtraLight /i;
774 $style =~ s/\bheavy\b/Extrabold/i;
775 $style =~ s/\bhairline\b/Extralight/i;
776 $style =~ s/\bcond\b/Condensed/i;
777 $style =~ s/(roman)?slanted/ Italic/i;
778 $style =~ s/\bslant\b/Italic/i;
779 $style =~ s/\b(SC|Small(caps(alt)?)?)\b/SmallCaps/i;
780 $style =~ s/w3 mono/Dual/i;
781 $style =~ s/Regul[ea]r/Regular/i;