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 "ps" => {fieldname => "Scripts", alias => ["printscripts"],
62 comment => "Print supported scripts"},
63 "pl" => {fieldname => "PrintLangs", alias => ["printlangs"],
64 comment => "Print supported languages"},
65 "pp" => {fieldname => "PrintProperties", alias => ["printproperties"],
66 comment => "Print properties from weight, slant and width"},
67 "pf" => {fieldname => "PrintFiles", alias => ["printfiles"],
68 comment => "Print font file names"},
69 "p" => {fieldname => "Property",
70 type => "=s", listsep => ',',
71 comment => "Select fonts with properties matching these (comma separated) regexes"},
72 "np" => {fieldname => "NProperty",
73 type => "=s", listsep => ',',
74 comment => "Select fonts with properties NOT matching these (comma separated) regexes"},
75 "pw" => {fieldname => "PrintWarnings",
76 comment => "Print warnings about discarded/overwritten fonts, conflicting styles"},
78 my %options = %{&handleOptions(\%optionsDef)};
80 $options{Lang} = "" if (! defined($options{Lang}));
82 #############################################################
84 my @langs = split(',', $options{Lang});
86 $lg = &convertlang($lg);
90 if (defined($langs[0])) {
91 $cmd .= " :lang=" . join(',', @langs);
94 my $format = "foundry=\"%{foundry}\" postscriptname=\"%{postscriptname}\" fn=\"%{fullname}\" fnl=\"%{fullnamelang}\" family=\"%{family}\" flang=\"%{familylang}\" style=\"%{style}\" stylelang=\"%{stylelang}\"";
96 if (exists($options{Scripts})) {
97 $format .= " script=\"%{capability}\"";
99 if (exists($options{PrintLangs}) || defined($langs[0])) {
100 $format .= " lang=\"%{lang}\"";
102 if (exists($options{PrintProperties}) || defined($options{Property})) {
103 $format .= " weight=%{weight} slant=%{slant} width=%{width} spacing=%{spacing}";
105 $format .= " file=\"%{file}\" abcd\\n";
106 $cmd .= " -f '$format'";
113 "default" => "Serif",
137 50 => "Ultracondensed",
138 63 => "Extracondensed",
140 87 => "Semicondensed",
142 113 => "Semiexpanded",
144 150 => "Extraexpanded",
145 200 => "Ultraexpanded",
153 "default" => "Proportional",
156 # Build reverse mappings, (not needed yet)
157 for my $txt (qw(ftypes weights slants widths spacings)) {
159 eval "\$map = \\%$txt";
160 for my $key (keys %{$map}) {
161 next if ($key !~ /^\d+$/);
162 my $value = lc($map->{$key});
163 $map->{"r"}->{$value} = $key;
170 my %collectedfonts = ();
172 otf => 0, # type 2, opentype CFF (Compact Font Format)
173 ttc => 1.1, # type 1 (True Type font Collection)
174 ttf => 1.2, # type 1 (True Type Font)
175 woff=> 1.3, # type 1 (Web Open Font Format)
176 t1 => 1.4, # type 1 (postscript)
177 pfb => 1.5, # type 1 (Printer Font Binary)
178 pfa => 1.6, # type 1 (Printer Font Ascii)
179 pcf => 5, # Bitmap (Packaged Collaboration Files)?
183 if (open(FI, "$cmd |")) {
184 NXTLINE: while (my $l = <FI>) {
186 while ($l !~ /abcd$/) {
192 if ($l =~ /file=\"([^\"]+)\"/) {
194 #next if ($file !~ /\.(otf|ttf|pfa|pfb|pcf|ttc)$/i);
195 if ($file !~ /\.([a-z0-9]{2,5})$/i) {
196 print "Unhandled extension for file $file\n";
200 if (! defined($fontpriority{$fonttype})) {
201 print "Added extension $fonttype for file $file\n";
202 $fontpriority{$fonttype} = $nexttype;
208 if ($l =~ / lang=\"([^\"]+)\"/) {
209 my @ll = split(/\|/, $1);
211 $usedlangs{&convertlang($lx)} = 1;
215 for my $lang (@langs) {
216 next NXTLINE if (! defined($usedlangs{$lang}));
219 my $style = &getVal($l, "style", "stylelang");
220 $style =~ s/^\\040//;
221 my $fullname = &getVal($l, "fn", "fnl");
222 my $postscriptname = "";
223 if ($l =~ /postscriptname=\"([^\"]+)\"/) {
224 $postscriptname = $1;
226 my $family = &getVal($l, "family", "flang");
227 $family =~ s/\\040/\-/;
229 if (length($family) < 3) {
230 if (length($postscriptname) < 2) {
231 $fontname = $fullname;
234 $fontname = $postscriptname;
238 $fontname = "$family $style";
240 if (defined($options{NFontName})) {
241 for my $fn (@{$options{NFontName}}) {
242 next NXTLINE if ($fontname =~ /$fn/i);
245 if (defined($options{FontName})) {
246 for my $fn (@{$options{FontName}}) {
247 next NXTLINE if ($fontname !~ /$fn/i);
252 if (exists($options{PrintProperties}) || defined($options{Property}) || defined($options{NProperty})) {
253 my $properties = getproperties($l, $family, $style, \@errors);
254 if (defined($options{Property})) {
255 for my $pn (@{$options{Property}}) {
256 next NXTLINE if ($properties !~ /$pn/i);
259 if (defined($options{NProperty})) {
260 for my $pn (@{$options{NProperty}}) {
261 next NXTLINE if ($properties =~ /$pn/i);
264 if (exists($options{PrintProperties})) {
265 $props .= "($properties)";
269 if (exists($options{PrintLangs})) {
270 $props .= '(' . join(',', sort keys %usedlangs) . ')';
272 if (exists($options{Scripts})) {
273 if ($l =~ / script=\"([^\"]+)\"/) {
274 my @scripts = split(/\s+/, $1);
275 for my $ent (@scripts) {
276 $ent =~ s/^\s*otlayout://;
279 $props .= '(' . join(',', @scripts) . ')';
283 if ($l =~ /foundry=\"([^\"]+)\"/) {
285 $foundry =~ s/^\s+//;
286 $foundry =~ s/\s+$//;
288 if (defined($collectedfonts{$fontname}->{$foundry}->{errors})) {
289 # Apparently not the first one, so add some info
290 my $oldfonttype = $collectedfonts{$fontname}->{$foundry}->{fonttype};
291 if (defined($errors[0])) {
292 push(@{$collectedfonts{$fontname}->{$foundry}->{errors}}, @errors);
294 if ($fontpriority{$oldfonttype} > $fontpriority{$fonttype}) {
295 push(@{$collectedfonts{$fontname}->{$foundry}->{errors}}, "Warning: overwriting old info for file: " . $collectedfonts{$fontname}->{$foundry}->{file});
298 push(@{$collectedfonts{$fontname}->{$foundry}->{errors}}, "Warning: discarding new info for file: $file");
303 $collectedfonts{$fontname}->{$foundry}->{errors} = \@errors;
305 $collectedfonts{$fontname}->{$foundry}->{props} = $props;
306 $collectedfonts{$fontname}->{$foundry}->{file} = $file;
307 $collectedfonts{$fontname}->{$foundry}->{fonttype} = $fonttype;
312 for my $fontname (sort keys %collectedfonts) {
313 my @foundries = sort keys %{$collectedfonts{$fontname}};
314 my $printfoundries = 0;
315 if (defined($foundries[1])) {
318 for my $foundry (@foundries) {
319 if (exists($options{PrintWarnings})) {
320 for my $err (@{$collectedfonts{$fontname}->{$foundry}->{errors}}) {
324 print "Font : $fontname";
325 if ($printfoundries && ($foundry ne "")) {
326 print " \[$foundry\]";
328 print $collectedfonts{$fontname}->{$foundry}->{props};
329 if (exists($options{PrintFiles})) {
330 print ": " . $collectedfonts{$fontname}->{$foundry}->{file} . "\n";
339 #################################################################################
343 if ($ilang =~ /^\s*([a-z]+)([\-_]([a-z]+))?\s*$/i) {
344 my ($x, $y) = ($1, $3);
346 $ilang = lc($x) . '-' . lc($y);
357 my ($l, $islang, $txt, $rres) = @_;
359 if ($l =~ /$txt=\"([^\"]+)\"/) {
360 @{res} = split(',', $1);
363 $lg = &convertlang($lg);
372 my ($lang, $rlangs) = @_;
373 for (my $i = 0; defined($rlangs->[$i]); $i++) {
374 return $i if ($rlangs->[$i] eq $lang);
381 my ($l, $txtval, $txtlang) = @_;
384 &extractlist($l, 0, $txtval, \@values);
385 return("") if (! defined($values[0]));
386 &extractlist($l, 1, $txtlang, \@langs);
387 my $i = &getIndex("en", \@langs);
388 return ($values[$i]) if ($i >= 0);
392 sub getsinglevalue($$$)
394 my ($l, $txt, $rMap, $rget) = @_;
396 if ($l =~ / $txt=(\d+)/) {
398 # Search for nearest value to $val
399 if (defined($rMap->{$val})) {
400 return($rMap->{$val});
404 for my $key (keys %{$rMap}) {
405 next if ($key !~ /^\d+$/);
406 my $diff = abs($key - $val);
411 elsif ($diff == $minv) {
417 if (! defined($rMap->{$maxv})) {
418 print "ERROR2: txt=$txt, val=$val\n";
422 return($rMap->{$maxv} . "+$minv");
425 return($rMap->{$maxv} . "-$minv");
435 my ($txt, $val) = @_;
436 return("$txt($val)");
441 my ($family, $style) = @_;
442 if ("$family" =~ /arial|helvet/i) {
443 return($ftypes{100}); # Sans Serif
445 elsif ($family =~ /(sans)[-_ ]?(serif)?/i) {
446 return($ftypes{100}); # Sans Serif
448 elsif ($family =~ /serif/i) {
449 return($ftypes{0}); # Serif
458 my ($family, $style) = @_;
460 for my $key (keys %weights) {
461 next if ($key !~ /^\d+$/);
462 my $val = $weights{$key};
463 for my $info ($style, $family) {
464 if ($info =~ /\b$val\b/i) {
465 if ($val eq "Regular") {
466 $result = $val; # It may refer to width
479 my ($family, $style) = @_;
481 for my $key (keys %widths) {
482 next if ($key !~ /^\d+$/);
483 for my $info ($style, $family) {
484 if ($info =~ /\b$widths{$key}\b/i) {
485 return($widths{$key});
487 if ($info =~ /\bRegular\b/) {
488 if (!defined($result)) {
489 $result = $widths{100};
499 my ($family, $style) = @_;
500 for my $key (keys %slants) {
501 next if ($key !~ /^\d+$/);
502 if ($style =~ /\b$slants{$key}\b/i) {
503 return($slants{$key});
511 my ($family, $style) = @_;
512 for my $key (keys %spacings) {
513 next if ($key !~ /^\d+$/);
514 if ($style =~ /\b$spacings{$key}\b/i) {
515 return($spacings{$key});
518 if ("$family $style" =~ /[-_ ](mono|typewriter|cursor)\b/i) {
519 return($spacings{100}); # Mono
526 sub getproperties($$$$)
528 my ($l, $family, $style, $rerrors) = @_;
529 my $newstyle = &correctstyle($style);
530 my $newfam = &correctstyle($family);
533 for my $txt (qw(ftype weight width slant spacing)) {
535 eval("\$map = " . '\%' . $txt . 's');
536 eval('$rget = \&' . "get$txt");
537 my $val2 = getsinglevalue($l, $txt, $map);
538 my $val1 = $rget->($newfam, $newstyle);
540 if (defined($val2) && defined($val1) && ($val2 ne $val1)) {
541 push(@{$rerrors}, "Family($family),Style($style): Values for $txt ($val1 != $val2) differ, selecting internal $txt($val2)");
544 elsif (! defined($val2)) {
551 push(@properties, &addTxt($txt,$val));
554 if (defined($map->{"default"})) {
555 push(@properties, &addTxt($txt,$map->{"default"}));
558 push(@{$rerrors}, "Undefined value for $txt");
562 return(join(' ', @properties));
568 $style =~ s/^\\040//;
569 $style =~ s/^\s*\d+\s*//;
570 $style =~ s/\s*\d+$//;
571 $style =~ s/italic/ Italic/i;
572 $style =~ s/oblique/ Oblique/i;
573 $style =~ s/[\-_]/ /g;
574 $style =~ s/\breg\b/Regular/i;
575 $style =~ s/\bregita(lic)?\b/Regular Italic/i;
576 $style =~ s/\bregobl(ique)?\b/Regular Oblique/i;
577 $style =~ s/medium/Medium /i;
578 $style =~ s/\bmedita(lic)?\b/Medium Italic/i;
579 $style =~ s/\bmedobl(ique)?\b/Medium Oblique/i;
580 $style =~ s/\bmed\b/Medium /i;
581 $style =~ s/\bdemi\b/SemiBold/i;
582 $style =~ s/\bex(pd|t)\b/Expanded/i;
583 $style =~ s/semi ?cond(ensed)?/SemiCondensed/i;
584 $style =~ s/[sd]emi ?(bold|bd|bol)/SemiBold/i;
585 $style =~ s/semi ?(expanded|extended|expd)/SemiExpanded/i;
586 $style =~ s/[sd]emi ?light/SemiLight/i;
587 $style =~ s/ultra ?(expanded|extended|expd)/UltraExpanded/i;
588 $style =~ s/light/Light /i;
589 $style =~ s/\blt\b/Light /i;
590 $style =~ s/(ultra|extra)(light|lt)/ExtraLight /i;
591 $style =~ s/\bheavy\b/Extrabold/i;
592 $style =~ s/\bhairline\b/Extralight/i;
593 $style =~ s/\bcond\b/Condensed/i;
594 $style =~ s/(roman)?slanted/ Italic/i;
595 $style =~ s/\bslant\b/Italic/i;
596 $style =~ s/\b(SC|Small(caps(alt)?)?)\b/SmallCaps/i;
597 $style =~ s/w3 mono/Dual/i;
598 $style =~ s/Regul[ea]r/Regular/i;