]> git.lyx.org Git - lyx.git/blob - development/tools/listFontWithLang.pl
3589ba0e16cb743387c6e5a5c9c77b0e1dc3c4e4
[lyx.git] / development / tools / listFontWithLang.pl
1 #! /usr/bin/env perl
2 # -*- mode: perl; -*-
3
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
8 #
9 # author Kornel Benko
10 # Full author contact details are available in the file CREDITS
11 # or at https://www.lyx.org/Credits
12 #
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 '--'
16 #
17 # Option to get list of options: -h
18 #
19 # Some equivalencies for instance with option -n
20 #       -n arial
21 #       -N=arial
22 #       --nAme=Arial
23 #       --name arial
24 # Options and option-parameter are case insensitive
25
26 BEGIN {
27     use File::Spec;
28     my $p = File::Spec->rel2abs( __FILE__ );
29     $p =~ s/[\/\\]?[^\/\\]+$//;
30     unshift(@INC, $p);
31 }
32
33 use strict;
34 use warnings;
35 use GetOptions;
36
37 sub convertlang($);
38 sub extractlist($$$);   # my ($l, $islang, $txt, $rres) = @_;
39 sub getIndexes($$);
40 sub getVal($$$);        # my ($l, $txtval, $txtlang) = @_;
41 sub getproperties($$$$);
42 sub ismathfont($$);
43 sub correctstyle($);
44
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
51 my %optionsDef = (
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"},
86 );
87 my %options = %{&handleOptions(\%optionsDef)};
88
89 $options{Lang} = "" if (! defined($options{Lang}));
90
91 #############################################################
92
93 my @langs = split(',', $options{Lang});
94 for my $lg (@langs) {
95   $lg = &convertlang($lg);
96 }
97
98 my $cmd = "fc-list";
99 if (defined($langs[0])) {
100   $cmd .= " :lang=" . join(',', @langs);
101 }
102
103 my $format = "foundry=\"%{foundry}\"" .
104     " postscriptname=\"%{postscriptname}\"" .
105     " fn=\"%{fullname}\" fnl=\"%{fullnamelang}\"" .
106     " family=\"%{family}\" flang=\"%{familylang}\" " .
107     " style=\"%{style}\" stylelang=\"%{stylelang}\"";
108
109 if (exists($options{PrintScripts}) || defined($options{Scripts}) || defined($options{NSpripts}) || exists($options{Math})) {
110   $format .= " script=\"%{capability}\"";
111 }
112 if (exists($options{PrintLangs}) || defined($langs[0])) {
113   $format .= " lang=\"%{lang}\"";
114 }
115 if (exists($options{PrintProperties}) || defined($options{Property})) {
116   $format .= " weight=%{weight} slant=%{slant} width=%{width} spacing=%{spacing}";
117 }
118 $format .= " file=\"%{file}\" abcd\\n";
119 $cmd .= " -f '$format'";
120 #print "$cmd\n";
121
122 my %ftypes = (
123   # Dummy internal map
124   0 => "Serif",
125   100 => "Sans",
126   110 => "Script",
127   120 => "Fraktur",
128   130 => "Fancy",
129   140 => "Initials",
130   200 => "Symbol",
131   "default" => "Serif",
132 );
133
134 my %weights = (
135   0 => "Thin",
136   40 => "Extralight",
137   50 => "Light",
138   55 => "Semilight",
139   75 => "Book",
140   80 => "Regular",
141   100 => "Medium",
142   180 => "Semibold",
143   200 => "Bold",
144   205 => "Extrabold",
145   210 => "Black",
146 );
147
148 my %slants = (
149   0 => "Roman",
150   100 => "Italic",
151   110 => "Oblique",
152 );
153
154 my %widths = (
155   50 => "Ultracondensed",
156   63 => "Extracondensed",
157   75 => "Condensed",
158   87 => "Semicondensed",
159   100 => "Normal",
160   113 => "Semiexpanded",
161   125 => "Expanded",
162   150 => "Extraexpanded",
163   200 => "Ultraexpanded",
164 );
165
166 my %spacings = (
167   0 => "Proportional",
168   90 => "Dual",
169   100 => "Mono",
170   110 => "Charcell",
171   "default" => "Proportional",
172 );
173
174 # Build reverse mappings, (not needed yet)
175 for my $txt (qw(ftypes weights slants widths spacings)) {
176   my $map;
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;
182   }
183 }
184
185 # key:= fontname
186 #     subkey foundry
187 #            subfoundry
188 my %collectedfonts = ();
189 my %fontpriority = (
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)?
198 );
199 my $nexttype = 6;
200
201 # list of regexes for known sans serif fonts
202 my %sansFonts = (
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,
229 );
230 my %scriptFonts = (
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,
240 );
241
242 my %fraktFonts = (
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,
248 );
249
250 my %fancyFonts = (
251   "value" => 130,          # Fancy
252   "c" => qr/^(cretino)/i,
253   "g" => qr/^(gfs.?theo)/i,
254 );
255
256 my %initialFonts = (
257   "value" => 140,          # Initials
258   "e" => qr/^(eb.?garamond.?init)/i,
259   "l" => qr/^(libertinus|linux).*initials/i,
260   "y" => qr/^(yinit)/i,
261 );
262
263 my %symbolFonts = (
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,
269   "h" => qr/^(hots)/i,
270   "m" => qr/^(marvosym)/i,
271   "n" => qr/^(noto.*emoji)/i,
272   "q" => qr/^(qtdingbits)/i,
273 );
274
275 if (open(FI,  "$cmd |")) {
276  NXTLINE: while (my $l = <FI>) {
277     chomp($l);
278     while ($l !~ /abcd$/) {
279       $l .= <FI>;
280       chomp($l);
281     }
282     my $file = "";
283     my $fonttype;
284     if ($l =~ /file=\"([^\"]+)\"/) {
285       $file = $1;
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";
289         next;
290       }
291       $fonttype = lc($1);
292       if (! defined($fontpriority{$fonttype})) {
293         print "Added extension $fonttype for file $file\n";
294         $fontpriority{$fonttype} = $nexttype;
295         $nexttype++;
296       }
297     }
298     my $nfound = 0;
299     my %usedlangs = ();
300     if ($l =~ / lang=\"([^\"]+)\"/) {
301       my @ll = split(/\|/, $1);
302       for my $lx (@ll) {
303         $usedlangs{&convertlang($lx)} = 1;
304       }
305     }
306
307     for my $lang (@langs) {
308       next NXTLINE if (! defined($usedlangs{$lang}));
309     }
310     next if ($nfound);
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;
317     }
318     my $family = &getVal($l, "family", "flang");
319     $family =~ s/\\040/\-/;
320     my $fontname;
321     if (length($fullname) < 3) {
322       if (length($postscriptname) < 2) {
323         $fontname = "$family $style";
324       }
325       else {
326         $fontname = $postscriptname;
327       }
328     }
329     else {
330       $fontname = $fullname;
331     }
332     if (defined($options{NFontName})) {
333       for my $fn (@{$options{NFontName}}) {
334         next NXTLINE if ($fontname =~ /$fn/i);
335       }
336     }
337     if (defined($options{FontName})) {
338       for my $fn (@{$options{FontName}}) {
339         next NXTLINE if ($fontname !~ /$fn/i);
340       }
341     }
342     my $props = "";
343     my @errors = ();
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);
349         }
350       }
351       if (defined($options{NProperty})) {
352         for my $pn (@{$options{NProperty}}) {
353           next NXTLINE if ($properties =~ /$pn/i);
354         }
355       }
356       if (exists($options{PrintProperties})) {
357         $props .= " ($properties)";
358       }
359     }
360
361     if (exists($options{PrintLangs})) {
362       $props .= '(' . join(',', sort keys %usedlangs) . ')';
363     }
364     if (exists($options{PrintScripts}) || defined($options{Scripts}) || defined($options{NScripts}) || exists($options{Math})) {
365       my @scripts = ();
366       my $scripts = "";
367       if ($l =~ / script=\"([^\"]+)\"/) {
368         @scripts = split(/\s+/, $1);
369         for my $ent (@scripts) {
370           $ent =~ s/^\s*otlayout://;
371           $ent = lc($ent);
372         }
373         $scripts = join(',', @scripts);
374       }
375       if (exists($options{Math})) {
376         next NXTLINE if (! &ismathfont($fontname,\@scripts));
377       }
378       if (exists($options{PrintScripts})) {
379         $props .= "($scripts)";
380       }
381       if (!defined($scripts[0])) {
382         # No script defined in font, so check only $options{Scripts}
383         next NXTLINE if (defined($options{Scripts}));
384       }
385       else {
386         if (defined($options{Scripts})) {
387           for my $s (@{$options{Scripts}}) {
388             next NXTLINE if ($scripts !~ /$s/i);
389           }
390         }
391         if (defined($options{NScripts})) {
392           for my $s (@{$options{NScripts}}) {
393             next NXTLINE if ($scripts =~ /$s/i);
394           }
395         }
396       }
397     }
398     my $foundry = "";
399     if ($l =~ /foundry=\"([^\"]+)\"/) {
400       $foundry = $1;
401       $foundry =~ s/^\s+//;
402       $foundry =~ s/\s+$//;
403     }
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);
409       }
410       if ($fontpriority{$oldfonttype} > $fontpriority{$fonttype}) {
411         push(@{$collectedfonts{$fontname}->{$foundry}->{errors}}, "Warning: overwriting old info for file: " . $collectedfonts{$fontname}->{$foundry}->{file});
412       }
413       else {
414         push(@{$collectedfonts{$fontname}->{$foundry}->{errors}}, "Warning: discarding new info for file: $file");
415         next;
416       }
417     }
418     else {
419       $collectedfonts{$fontname}->{$foundry}->{errors} = \@errors;
420     }
421     $collectedfonts{$fontname}->{$foundry}->{props} = $props;
422     $collectedfonts{$fontname}->{$foundry}->{file} = $file;
423     $collectedfonts{$fontname}->{$foundry}->{fonttype} = $fonttype;
424   }
425   close(FI);
426 }
427
428 for my $fontname (sort keys %collectedfonts) {
429   my @foundries = sort keys %{$collectedfonts{$fontname}};
430   my $printfoundries = 0;
431   if (defined($foundries[1])) {
432     $printfoundries = 1;
433   }
434   for my $foundry (@foundries) {
435     if (exists($options{PrintWarnings})) {
436       for my $err (@{$collectedfonts{$fontname}->{$foundry}->{errors}}) {
437         print "$err\n";
438       }
439     }
440     my $fn = "Font : $fontname";
441     if ($printfoundries && ($foundry ne "")) {
442       $fn .= " \[$foundry\]";
443     }
444     print $fn;
445     print $collectedfonts{$fontname}->{$foundry}->{props};
446     if (exists($options{PrintFiles})) {
447       print ": " . $collectedfonts{$fontname}->{$foundry}->{file} . "\n";
448     }
449     else {
450       print "\n";
451     }
452   }
453 }
454
455 exit(0);
456 #################################################################################
457 sub convertlang($)
458 {
459   my ($ilang) = @_;
460   if ($ilang =~ /^\s*([a-z]+)([\-_]([a-z]+))?\s*$/i) {
461     my ($x, $y) = ($1, $3);
462     if (defined($y)) {
463       $ilang = lc($x) . '-' . lc($y);
464     }
465     else {
466       $ilang = lc($x);
467     }
468   }
469   return($ilang);
470 }
471
472 sub extractlist($$$)
473 {
474   my ($l, $islang, $txt, $rres) = @_;
475   my @res = ();
476   if ($l =~ /$txt=\"([^\"]+)\"/) {
477     @res = split(',', $1);
478     if ($islang) {
479       for my $lg (@res) {
480         $lg = &convertlang($lg);
481       }
482     }
483   }
484   @{$rres} = @res;
485 }
486
487 sub getIndexes($$)
488 {
489   my ($lang, $rlangs) = @_;
490   my @res = ();
491
492   for (my $i = 0; defined($rlangs->[$i]); $i++) {
493     if ($rlangs->[$i] eq $lang) {
494       push(@res, $i);
495     }
496   }
497   return(\@res);
498 }
499
500 sub getVal($$$)
501 {
502   my ($l, $txtval, $txtlang) = @_;
503   my @values = ();
504   my @langs = ();
505   &extractlist($l, 0, $txtval, \@values);
506   return("") if (! defined($values[0]));
507   &extractlist($l, 1, $txtlang, \@langs);
508   my $i = &getIndexes("en", \@langs);
509   my $res = "";
510   for my $k (@{$i}) {
511     if (defined($values[$k]) && (length($values[$k]) > length($res))) {
512       $res = $values[$k];
513     }
514   }
515   return($values[0]) if ($res eq "");
516   return($res);
517 }
518
519 sub getsinglevalue($$$)
520 {
521   my ($l, $txt, $rMap, $rget) = @_;
522   my $val;
523   if ($l =~ / $txt=(\d+)/) {
524     $val = $1;
525     # Search for nearest value to $val
526     if (defined($rMap->{$val})) {
527       return($rMap->{$val});
528     }
529     my $maxv = -1;
530     my $minv = 1000;
531     for my $key (keys %{$rMap}) {
532       next if ($key !~ /^\d+$/);
533       my $diff = abs($key - $val);
534       if ($diff < $minv) {
535         $maxv = $key;
536         $minv = $diff;
537       }
538       elsif ($diff == $minv) {
539         if ($key < $maxv) {
540           $maxv = $key;
541         }
542       }
543     }
544     if (! defined($rMap->{$maxv})) {
545       print "ERROR2: txt=$txt, val=$val\n";
546       exit(-2);
547     }
548     if ($val > $maxv) {
549       return($rMap->{$maxv} . "+$minv");
550     }
551     else {
552       return($rMap->{$maxv} . "-$minv");
553     }
554   }
555   else {
556     return(undef);
557   }
558 }
559
560 sub addTxt($$)
561 {
562   my ($txt, $val) = @_;
563   return("$txt($val)");
564 }
565
566 sub getftype($$)
567 {
568   my ($fontname, $style) = @_;
569   if ($fontname =~ /(sans)[-_ ]?(serif)?/i) {
570     return($ftypes{100}); # Sans Serif
571   }
572   elsif ($fontname =~ /gothic|dotum|gulim/i) {
573     if ($fontname =~ /bisrat gothic/i) {
574       return($ftypes{0});    # Serif
575     }
576     else {
577       return($ftypes{100}); # Sans Serif
578     }
579   }
580   elsif ($fontname =~ /serif|times|mincho|batang/i) {
581     if ($fontname =~ /good times/i) {
582       return($ftypes{100}); # Sans Serif
583     }
584     elsif ($fontname !~ /initials/i) {
585       return($ftypes{0});    # Serif
586     }
587   }
588   # Now check for fonts without a hint in font name
589   if ($fontname =~ /([a-z])/i) {
590     my $key = lc($1);
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"}});
595         }
596       }
597     }
598   }
599   if ("$fontname" =~ /^bpg/i) {
600     if ("$fontname" =~ /bpg (courier gpl|elite)/i) {
601       return($ftypes{0});    # Serif
602     }
603     else {
604       return($ftypes{100}); # Sans Serif
605     }
606   }
607   elsif ("$fontname" =~ /^dustismo/i) {
608     if ("$fontname" =~ /^dustismo roman/i) {
609       return($ftypes{0});    # Serif
610     }
611     else {
612       return($ftypes{100}); # Sans Serif
613     }
614   }
615   elsif ("$fontname" =~ /^go\b/i) {
616     if ("$fontname" =~ /^go mono/i) {
617       return($ftypes{0});    # Serif
618     }
619     else {
620       return($ftypes{100}); # Sans Serif
621     }
622   }
623   else {
624     return(undef);
625   }
626 }
627
628 sub getweight($$)
629 {
630   my ($fontname, $style) = @_;
631   my $result = undef;
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
639         }
640         else {
641           return($val);
642         }
643       }
644     }
645   }
646   return($result);
647 }
648
649 sub getwidth($$)
650 {
651   my ($fontname, $style) = @_;
652   my $result = undef;
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});
658       }
659       if ($info =~ /\bRegular\b/) {
660         if (!defined($result)) {
661           $result = $widths{100};
662         }
663       }
664     }
665   }
666   return($result);
667 }
668
669 sub getslant($$)
670 {
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});
676     }
677   }
678   return(undef);
679 }
680
681 sub getspacing($$)
682 {
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});
688     }
689   }
690   if ("$fontname $style" =~ /(mono|typewriter|cursor|fixed)\b/i) {
691     return($spacings{100}); # Mono
692   }
693   else {
694     return(undef);
695   }
696 }
697
698 sub ismathfont($$)
699 {
700   my ($fontname, $rCapability) = @_;
701
702   return 1 if ($fontname =~ /math/i);
703   for my $cap (@{$rCapability}) {
704     return 1 if ($cap eq "math");
705   }
706   return 0;
707 }
708
709 sub getproperties($$$$)
710 {
711   my ($l, $fontname, $style, $rerrors) = @_;
712   my $newstyle = &correctstyle($style);
713   my $newfam = &correctstyle($fontname);
714   my @properties = ();
715
716   for my $txt (qw(ftype weight width slant spacing)) {
717     my ($map, $rget);
718     eval("\$map = " . '\%' . $txt . 's');
719     eval('$rget = \&' . "get$txt");
720     my $val2 = getsinglevalue($l, $txt, $map);
721     my $val1 = $rget->($newfam, $newstyle);
722     my $val;
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)");
725       $val = $val2;
726     }
727     elsif (! defined($val2)) {
728       $val = $val1;
729     }
730     else {
731       $val = $val2;
732     }
733     if (defined($val)) {
734       push(@properties, &addTxt($txt,$val));
735     }
736     else {
737       if (defined($map->{"default"})) {
738         push(@properties, &addTxt($txt,$map->{"default"}));
739       }
740       else {
741         push(@{$rerrors}, "Undefined value for $txt");
742       }
743     }
744   }
745   return(join(' ', @properties));
746 }
747
748 sub correctstyle($)
749 {
750   my ($style) = @_;
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;
782   $style =~ s/  +/ /g;
783   return($style);
784 }