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}\" postscriptname=\"%{postscriptname}\" fn=\"%{fullname}\" fnl=\"%{fullnamelang}\" family=\"%{family}\" flang=\"%{familylang}\" style=\"%{style}\" stylelang=\"%{stylelang}\"";
105 if (exists($options{PrintScripts}) || defined($options{Scripts}) || defined($options{NSpripts}) || exists($options{Math})) {
106 $format .= " script=\"%{capability}\"";
108 if (exists($options{PrintLangs}) || defined($langs[0])) {
109 $format .= " lang=\"%{lang}\"";
111 if (exists($options{PrintProperties}) || defined($options{Property})) {
112 $format .= " weight=%{weight} slant=%{slant} width=%{width} spacing=%{spacing}";
114 $format .= " file=\"%{file}\" abcd\\n";
115 $cmd .= " -f '$format'";
122 "default" => "Serif",
146 50 => "Ultracondensed",
147 63 => "Extracondensed",
149 87 => "Semicondensed",
151 113 => "Semiexpanded",
153 150 => "Extraexpanded",
154 200 => "Ultraexpanded",
162 "default" => "Proportional",
165 # Build reverse mappings, (not needed yet)
166 for my $txt (qw(ftypes weights slants widths spacings)) {
168 eval "\$map = \\%$txt";
169 for my $key (keys %{$map}) {
170 next if ($key !~ /^\d+$/);
171 my $value = lc($map->{$key});
172 $map->{"r"}->{$value} = $key;
179 my %collectedfonts = ();
181 otf => 0, # type 2, opentype CFF (Compact Font Format)
182 ttc => 1.1, # type 1 (True Type font Collection)
183 ttf => 1.2, # type 1 (True Type Font)
184 woff=> 1.3, # type 1 (Web Open Font Format)
185 t1 => 1.4, # type 1 (postscript)
186 pfb => 1.5, # type 1 (Printer Font Binary)
187 pfa => 1.6, # type 1 (Printer Font Ascii)
188 pcf => 5, # Bitmap (Packaged Collaboration Files)?
192 # list of regexes for known sans serif fonts
194 "a" => qr/^(arial|andika|angostura|anonymous|arab|aroania|arimo|asap)/i,
195 "b" => qr/^(baekmuk|bebas|berenika|beteckna|beuron|blue)/i,
196 "c" => qr/^(cabin|caliban|cantarell|carbon|carlito|chandas|chivo|cmu bright|comfortaa|comic|cortoba|cousine|cuprum|cwtex(hei|yen)|cyklop|cypro)/i,
197 "d" => qr/^(d2coding|dimnah|dosis|dyuthi)/i,
198 "e" => qr/^(electron|engebrechtre)/i,
199 "f" => qr/^(fandolhei|fetamont|fira|font awesome 5|forgotten)/i,
200 "g" => qr/^(gardiner|garuda|gfs ?neo|gillius|granada|graph|guanine|gunplay)/i,
201 "h" => qr/^(hack|hani|haramain|harano|harmattan|hor\b)/i,
202 "i" => qr/^(ibm plex|ikarius|inconsolata|induni.?h|iwona)/i,
203 "j" => qr/^(jara|jura)/i,
204 "k" => qr/^(kalimati|kanji|karla|kayrawan|kenyan|keraleeyam|khalid|khmer [or]|kiloji|klaudia|komatu|kurier)/i,
205 "l" => qr/^(laksaman|larabie|lato|league|lexend|lexigulim|libel|liberation|libre franklin|libris|linux biolinum|lobster|logix|lohit|loma)/i,
206 "m" => qr/^(m\+ |manchu|manjari|marcellus|mashq|meera|metal|migmix|migu|mikachan|mintspirit|mona|monlam|mono(fonto|id|isome|noki)|montserrat|motoyal|mukti|musica)/i,
207 "n" => qr/^(nachlieli|nada|nafees|nagham|nanum(barunpen|square)|nice)/i,
208 "o" => qr/^(ocr|okolaks|opendyslexic|ostorah|ouhud|over|oxygen)/i,
209 "p" => qr/^(padauk|padmaa|pagul|paktype|pakenham|palladio|petra|phetsarath|play\b|poiret|port\b|primer\b|prociono|pt\b|purisa)/i,
210 "q" => qr/^(qt(ancient|helvet|avanti|eratype|eurotype|floraline|florencia|frank|fritz|future|greece|howard|letter|optimum|pandora)|quercus)/i,
211 "r" => qr/^(rachana|radio\b|raleway|ricty|roboto|rosario)/i,
212 "s" => qr/^(salem|samanata|sawasdee|shado|sharja|simple|sophia|soul|source|switzera)/i,
213 "t" => qr/^(tarablus|teen|texgyre(adventor|heros)|tiresias|trebuchet|tscu|tuffy)/i,
214 "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,
215 "v" => qr/^(veranda|vn ?urwclassico)/i,
216 "w" => qr/^(waree)/i,
217 "y" => qr/^(yanone)/i,
218 "z" => qr/^(zekton|zero)/i,
220 if (open(FI, "$cmd |")) {
221 NXTLINE: while (my $l = <FI>) {
223 while ($l !~ /abcd$/) {
229 if ($l =~ /file=\"([^\"]+)\"/) {
231 #next if ($file !~ /\.(otf|ttf|pfa|pfb|pcf|ttc)$/i);
232 if ($file !~ /\.([a-z0-9]{2,5})$/i) {
233 print "Unhandled extension for file $file\n";
237 if (! defined($fontpriority{$fonttype})) {
238 print "Added extension $fonttype for file $file\n";
239 $fontpriority{$fonttype} = $nexttype;
245 if ($l =~ / lang=\"([^\"]+)\"/) {
246 my @ll = split(/\|/, $1);
248 $usedlangs{&convertlang($lx)} = 1;
252 for my $lang (@langs) {
253 next NXTLINE if (! defined($usedlangs{$lang}));
256 my $style = &getVal($l, "style", "stylelang");
257 $style =~ s/^\\040//;
258 my $fullname = &getVal($l, "fn", "fnl");
259 my $postscriptname = "";
260 if ($l =~ /postscriptname=\"([^\"]+)\"/) {
261 $postscriptname = $1;
263 my $family = &getVal($l, "family", "flang");
264 $family =~ s/\\040/\-/;
266 if (length($fullname) < 3) {
267 if (length($postscriptname) < 2) {
268 $fontname = "$family $style";
271 $fontname = $postscriptname;
275 $fontname = $fullname;
277 if (defined($options{NFontName})) {
278 for my $fn (@{$options{NFontName}}) {
279 next NXTLINE if ($fontname =~ /$fn/i);
282 if (defined($options{FontName})) {
283 for my $fn (@{$options{FontName}}) {
284 next NXTLINE if ($fontname !~ /$fn/i);
289 if (exists($options{PrintProperties}) || defined($options{Property}) || defined($options{NProperty})) {
290 my $properties = getproperties($l, $fontname, $style, \@errors);
291 if (defined($options{Property})) {
292 for my $pn (@{$options{Property}}) {
293 next NXTLINE if ($properties !~ /$pn/i);
296 if (defined($options{NProperty})) {
297 for my $pn (@{$options{NProperty}}) {
298 next NXTLINE if ($properties =~ /$pn/i);
301 if (exists($options{PrintProperties})) {
302 $props .= " ($properties)";
306 if (exists($options{PrintLangs})) {
307 $props .= '(' . join(',', sort keys %usedlangs) . ')';
309 if (exists($options{PrintScripts}) || defined($options{Scripts}) || defined($options{NScripts}) || exists($options{Math})) {
312 if ($l =~ / script=\"([^\"]+)\"/) {
313 @scripts = split(/\s+/, $1);
314 for my $ent (@scripts) {
315 $ent =~ s/^\s*otlayout://;
318 $scripts = join(',', @scripts);
320 if (exists($options{Math})) {
321 next NXTLINE if (! &ismathfont($fontname,\@scripts));
323 if (exists($options{PrintScripts})) {
324 $props .= "($scripts)";
326 if (!defined($scripts[0])) {
327 # No script defined in font, so check only $options{Scripts}
328 next NXTLINE if (defined($options{Scripts}));
331 if (defined($options{Scripts})) {
332 for my $s (@{$options{Scripts}}) {
333 next NXTLINE if ($scripts !~ /$s/i);
336 if (defined($options{NScripts})) {
337 for my $s (@{$options{NScripts}}) {
338 next NXTLINE if ($scripts =~ /$s/i);
344 if ($l =~ /foundry=\"([^\"]+)\"/) {
346 $foundry =~ s/^\s+//;
347 $foundry =~ s/\s+$//;
349 if (defined($collectedfonts{$fontname}->{$foundry}->{errors})) {
350 # Apparently not the first one, so add some info
351 my $oldfonttype = $collectedfonts{$fontname}->{$foundry}->{fonttype};
352 if (defined($errors[0])) {
353 push(@{$collectedfonts{$fontname}->{$foundry}->{errors}}, @errors);
355 if ($fontpriority{$oldfonttype} > $fontpriority{$fonttype}) {
356 push(@{$collectedfonts{$fontname}->{$foundry}->{errors}}, "Warning: overwriting old info for file: " . $collectedfonts{$fontname}->{$foundry}->{file});
359 push(@{$collectedfonts{$fontname}->{$foundry}->{errors}}, "Warning: discarding new info for file: $file");
364 $collectedfonts{$fontname}->{$foundry}->{errors} = \@errors;
366 $collectedfonts{$fontname}->{$foundry}->{props} = $props;
367 $collectedfonts{$fontname}->{$foundry}->{file} = $file;
368 $collectedfonts{$fontname}->{$foundry}->{fonttype} = $fonttype;
373 for my $fontname (sort keys %collectedfonts) {
374 my @foundries = sort keys %{$collectedfonts{$fontname}};
375 my $printfoundries = 0;
376 if (defined($foundries[1])) {
379 for my $foundry (@foundries) {
380 if (exists($options{PrintWarnings})) {
381 for my $err (@{$collectedfonts{$fontname}->{$foundry}->{errors}}) {
385 my $fn = "Font : $fontname";
386 if ($printfoundries && ($foundry ne "")) {
387 $fn .= " \[$foundry\]";
390 print $collectedfonts{$fontname}->{$foundry}->{props};
391 if (exists($options{PrintFiles})) {
392 print ": " . $collectedfonts{$fontname}->{$foundry}->{file} . "\n";
401 #################################################################################
405 if ($ilang =~ /^\s*([a-z]+)([\-_]([a-z]+))?\s*$/i) {
406 my ($x, $y) = ($1, $3);
408 $ilang = lc($x) . '-' . lc($y);
419 my ($l, $islang, $txt, $rres) = @_;
421 if ($l =~ /$txt=\"([^\"]+)\"/) {
422 @res = split(',', $1);
425 $lg = &convertlang($lg);
434 my ($lang, $rlangs) = @_;
437 for (my $i = 0; defined($rlangs->[$i]); $i++) {
438 if ($rlangs->[$i] eq $lang) {
447 my ($l, $txtval, $txtlang) = @_;
450 &extractlist($l, 0, $txtval, \@values);
451 return("") if (! defined($values[0]));
452 &extractlist($l, 1, $txtlang, \@langs);
453 my $i = &getIndexes("en", \@langs);
456 if (defined($values[$k]) && (length($values[$k]) > length($res))) {
460 return($values[0]) if ($res eq "");
464 sub getsinglevalue($$$)
466 my ($l, $txt, $rMap, $rget) = @_;
468 if ($l =~ / $txt=(\d+)/) {
470 # Search for nearest value to $val
471 if (defined($rMap->{$val})) {
472 return($rMap->{$val});
476 for my $key (keys %{$rMap}) {
477 next if ($key !~ /^\d+$/);
478 my $diff = abs($key - $val);
483 elsif ($diff == $minv) {
489 if (! defined($rMap->{$maxv})) {
490 print "ERROR2: txt=$txt, val=$val\n";
494 return($rMap->{$maxv} . "+$minv");
497 return($rMap->{$maxv} . "-$minv");
507 my ($txt, $val) = @_;
508 return("$txt($val)");
513 my ($fontname, $style) = @_;
514 if ($fontname =~ /(sans)[-_ ]?(serif)?/i) {
515 return($ftypes{100}); # Sans Serif
517 elsif ($fontname =~ /gothic|dotum|gulim/i) {
518 if ($fontname =~ /bisrat gothic/i) {
519 return($ftypes{0}); # Serif
522 return($ftypes{100}); # Sans Serif
525 elsif ($fontname =~ /serif|times|mincho|batang/i) {
526 if ($fontname =~ /good times/i) {
527 return($ftypes{100}); # Sans Serif
530 return($ftypes{0}); # Serif
533 # Now check for fonts without a hint in font name
534 if ($fontname =~ /([a-z])/i) {
536 if (defined($sansFonts{$key})) {
537 if ($fontname =~ $sansFonts{$key}) {
538 return($ftypes{100}); # Sans Serif
542 if ("$fontname" =~ /^bpg/i) {
543 if ("$fontname" =~ /bpg (courier gpl|elite)/i) {
544 return($ftypes{0}); # Serif
547 return($ftypes{100}); # Sans Serif
550 elsif ("$fontname" =~ /^dustismo/i) {
551 if ("$fontname" =~ /^dustismo roman/i) {
552 return($ftypes{0}); # Serif
555 return($ftypes{100}); # Sans Serif
558 elsif ("$fontname" =~ /^go\b/i) {
559 if ("$fontname" =~ /^go mono/i) {
560 return($ftypes{0}); # Serif
563 return($ftypes{100}); # Sans Serif
573 my ($fontname, $style) = @_;
575 for my $key (keys %weights) {
576 next if ($key !~ /^\d+$/);
577 my $val = $weights{$key};
578 for my $info ($style, $fontname) {
579 if ($info =~ /\b$val\b/i) {
580 if ($val eq "Regular") {
581 $result = $val; # It may refer to width
594 my ($fontname, $style) = @_;
596 for my $key (keys %widths) {
597 next if ($key !~ /^\d+$/);
598 for my $info ($style, $fontname) {
599 if ($info =~ /\b$widths{$key}\b/i) {
600 return($widths{$key});
602 if ($info =~ /\bRegular\b/) {
603 if (!defined($result)) {
604 $result = $widths{100};
614 my ($fontname, $style) = @_;
615 for my $key (keys %slants) {
616 next if ($key !~ /^\d+$/);
617 if ($style =~ /\b$slants{$key}\b/i) {
618 return($slants{$key});
626 my ($fontname, $style) = @_;
627 for my $key (keys %spacings) {
628 next if ($key !~ /^\d+$/);
629 if ($style =~ /\b$spacings{$key}\b/i) {
630 return($spacings{$key});
633 if ("$fontname $style" =~ /(mono|typewriter|cursor|fixed)\b/i) {
634 return($spacings{100}); # Mono
643 my ($fontname, $rCapability) = @_;
645 return 1 if ($fontname =~ /math/i);
646 for my $cap (@{$rCapability}) {
647 return 1 if ($cap eq "math");
652 sub getproperties($$$$)
654 my ($l, $fontname, $style, $rerrors) = @_;
655 my $newstyle = &correctstyle($style);
656 my $newfam = &correctstyle($fontname);
659 for my $txt (qw(ftype weight width slant spacing)) {
661 eval("\$map = " . '\%' . $txt . 's');
662 eval('$rget = \&' . "get$txt");
663 my $val2 = getsinglevalue($l, $txt, $map);
664 my $val1 = $rget->($newfam, $newstyle);
666 if (defined($val2) && defined($val1) && ($val2 ne $val1)) {
667 push(@{$rerrors}, "Fontname($fontname),Style($style): Values for $txt ($val1 != $val2) differ, selecting internal $txt($val2)");
670 elsif (! defined($val2)) {
677 push(@properties, &addTxt($txt,$val));
680 if (defined($map->{"default"})) {
681 push(@properties, &addTxt($txt,$map->{"default"}));
684 push(@{$rerrors}, "Undefined value for $txt");
688 return(join(' ', @properties));
694 $style =~ s/^\\040//;
695 $style =~ s/^\s*\d+\s*//;
696 $style =~ s/\s*\d+$//;
697 $style =~ s/italic/ Italic/i;
698 $style =~ s/oblique/ Oblique/i;
699 $style =~ s/[\-_]/ /g;
700 $style =~ s/\breg\b/Regular/i;
701 $style =~ s/\bregita(lic)?\b/Regular Italic/i;
702 $style =~ s/\bregobl(ique)?\b/Regular Oblique/i;
703 $style =~ s/medium/Medium /i;
704 $style =~ s/\bmedita(lic)?\b/Medium Italic/i;
705 $style =~ s/\bmedobl(ique)?\b/Medium Oblique/i;
706 $style =~ s/\bmed\b/Medium /i;
707 $style =~ s/\bdemi\b/SemiBold/i;
708 $style =~ s/\bex(pd|t)\b/Expanded/i;
709 $style =~ s/semi ?cond(ensed)?/SemiCondensed/i;
710 $style =~ s/[sd]emi ?(bold|bd|bol)/SemiBold/i;
711 $style =~ s/semi ?(expanded|extended|expd)/SemiExpanded/i;
712 $style =~ s/[sd]emi ?light/SemiLight/i;
713 $style =~ s/ultra ?(expanded|extended|expd)/UltraExpanded/i;
714 $style =~ s/light/Light /i;
715 $style =~ s/\blt\b/Light /i;
716 $style =~ s/(ultra|extra)(light|lt)/ExtraLight /i;
717 $style =~ s/\bheavy\b/Extrabold/i;
718 $style =~ s/\bhairline\b/Extralight/i;
719 $style =~ s/\bcond\b/Condensed/i;
720 $style =~ s/(roman)?slanted/ Italic/i;
721 $style =~ s/\bslant\b/Italic/i;
722 $style =~ s/\b(SC|Small(caps(alt)?)?)\b/SmallCaps/i;
723 $style =~ s/w3 mono/Dual/i;
724 $style =~ s/Regul[ea]r/Regular/i;