]> git.lyx.org Git - features.git/blob - development/tools/listFontWithLang.pl
Tools(listFontWithLang.pl): Group related parameters in the command syntax
[features.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   ["n",
54    {fieldname => "FontName", listsep => ',',
55     type => "=s", alias => ["name"],
56     comment => "Select font-names matching these (comma separated) regexes"},],
57   ["nn",
58    {fieldname => "NFontName",
59     type => "=s", listsep => ',',
60     comment => "Select font-names NOT matching these (comma separated) regexes"},],
61   ["p",
62    {fieldname => "Property",
63     type => "=s", listsep => ',',
64     comment => "Select fonts with properties matching these (comma separated) regexes"},],
65   ["np",
66    {fieldname => "NProperty",
67     type => "=s", listsep => ',',
68     comment => "Select fonts with properties NOT matching these (comma separated) regexes"},],
69   ["s",
70    {fieldname => "Scripts",
71     type => "=s", listsep => ',',
72     comment => "Select fonts with scripts matching these (comma separated) regexes"},],
73   ["ns",
74    {fieldname => "NScripts",
75     type => "=s", listsep => ',',
76     comment => "Select fonts with scripts NOT matching these (comma separated) regexes"},],
77   ["math",
78    {fieldname => "Math",
79     comment => "Select fonts probably containing math glyphs"},],
80   ["l",
81    {fieldname => "Lang",
82     type => "=s", alias=>["lang"],
83     comment => "Comma separated list of desired languages"},],
84   ["pl",
85    {fieldname => "PrintLangs", alias => ["printlangs"],
86     comment => "Print supported languages"},],
87   ["pp",
88    {fieldname => "PrintProperties", alias => ["printproperties"],
89     comment => "Print properties from weight, slant and width"},],
90   ["ps",
91    {fieldname => "PrintScripts", alias => ["printscripts"],
92     comment => "Print supported scripts"},],
93   ["pf",
94    {fieldname => "PrintFiles", alias => ["printfiles"],
95     comment => "Print font file names"},],
96   ["pw",
97    {fieldname => "PrintWarnings",
98     comment => "Print warnings about discarded/overwritten fonts, conflicting styles"},],
99 );
100 my %options = %{&handleOptions(\@optionsDef)};
101
102 $options{Lang} = "" if (! defined($options{Lang}));
103
104 #############################################################
105
106 my @langs = split(',', $options{Lang});
107 for my $lg (@langs) {
108   $lg = &convertlang($lg);
109 }
110
111 my $cmd = "fc-list";
112 if (defined($langs[0])) {
113   $cmd .= " :lang=" . join(',', @langs);
114 }
115
116 my $format = "foundry=\"%{foundry}\"" .
117     " postscriptname=\"%{postscriptname}\"" .
118     " fn=\"%{fullname}\" fnl=\"%{fullnamelang}\"" .
119     " family=\"%{family}\" flang=\"%{familylang}\" " .
120     " style=\"%{style}\" stylelang=\"%{stylelang}\"";
121
122 if (exists($options{PrintScripts}) || defined($options{Scripts}) || defined($options{NSpripts}) || exists($options{Math})) {
123   $format .= " script=\"%{capability}\"";
124 }
125 if (exists($options{PrintLangs}) || defined($langs[0])) {
126   $format .= " lang=\"%{lang}\"";
127 }
128 if (exists($options{PrintProperties}) || defined($options{Property})) {
129   $format .= " weight=%{weight} slant=%{slant} width=%{width} spacing=%{spacing}";
130 }
131 $format .= " file=\"%{file}\" abcd\\n";
132 $cmd .= " -f '$format'";
133 #print "$cmd\n";
134
135 my %ftypes = (
136   # Dummy internal map
137   0 => "Serif",
138   100 => "Sans",
139   110 => "Script",
140   120 => "Fraktur",
141   130 => "Fancy",
142   140 => "Initials",
143   200 => "Symbol",
144   "default" => "Serif",
145 );
146
147 my %weights = (
148   0 => "Thin",
149   40 => "Extralight",
150   50 => "Light",
151   55 => "Semilight",
152   75 => "Book",
153   80 => "Regular",
154   100 => "Medium",
155   180 => "Semibold",
156   200 => "Bold",
157   205 => "Extrabold",
158   210 => "Black",
159 );
160
161 my %slants = (
162   0 => "Roman",
163   100 => "Italic",
164   110 => "Oblique",
165 );
166
167 my %widths = (
168   50 => "Ultracondensed",
169   63 => "Extracondensed",
170   75 => "Condensed",
171   87 => "Semicondensed",
172   100 => "Normal",
173   113 => "Semiexpanded",
174   125 => "Expanded",
175   150 => "Extraexpanded",
176   200 => "Ultraexpanded",
177 );
178
179 my %spacings = (
180   0 => "Proportional",
181   90 => "Dual",
182   100 => "Mono",
183   110 => "Charcell",
184   "default" => "Proportional",
185 );
186
187 # Build reverse mappings, (not needed yet)
188 for my $txt (qw(ftypes weights slants widths spacings)) {
189   my $map;
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;
195   }
196 }
197
198 # key:= fontname
199 #     subkey foundry
200 #            subfoundry
201 my %collectedfonts = ();
202 my %fontpriority = (
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)?
211 );
212 my $nexttype = 6;
213
214 # list of regexes for known sans serif fonts
215 my %sansFonts = (
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,
242 );
243 my %scriptFonts = (
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,
253 );
254
255 my %fraktFonts = (
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,
261 );
262
263 my %fancyFonts = (
264   "value" => 130,          # Fancy
265   "c" => qr/^(cretino)/i,
266   "g" => qr/^(gfs.?theo)/i,
267 );
268
269 my %initialFonts = (
270   "value" => 140,          # Initials
271   "e" => qr/^(eb.?garamond.?init)/i,
272   "l" => qr/^(libertinus|linux).*initials/i,
273   "y" => qr/^(yinit)/i,
274 );
275
276 my %symbolFonts = (
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,
282   "h" => qr/^(hots)/i,
283   "m" => qr/^(marvosym)/i,
284   "n" => qr/^(noto.*emoji)/i,
285   "q" => qr/^(qtdingbits)/i,
286 );
287
288 if (open(FI,  "$cmd |")) {
289  NXTLINE: while (my $l = <FI>) {
290     chomp($l);
291     while ($l !~ /abcd$/) {
292       $l .= <FI>;
293       chomp($l);
294     }
295     my $file = "";
296     my $fonttype;
297     if ($l =~ /file=\"([^\"]+)\"/) {
298       $file = $1;
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";
302         next;
303       }
304       $fonttype = lc($1);
305       if (! defined($fontpriority{$fonttype})) {
306         print "Added extension $fonttype for file $file\n";
307         $fontpriority{$fonttype} = $nexttype;
308         $nexttype++;
309       }
310     }
311     my $nfound = 0;
312     my %usedlangs = ();
313     if ($l =~ / lang=\"([^\"]+)\"/) {
314       my @ll = split(/\|/, $1);
315       for my $lx (@ll) {
316         $usedlangs{&convertlang($lx)} = 1;
317       }
318     }
319
320     for my $lang (@langs) {
321       next NXTLINE if (! defined($usedlangs{$lang}));
322     }
323     next if ($nfound);
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;
330     }
331     my $family = &getVal($l, "family", "flang");
332     $family =~ s/\\040/\-/;
333     my $fontname;
334     if (length($fullname) < 3) {
335       if (length($postscriptname) < 2) {
336         $fontname = "$family $style";
337       }
338       else {
339         $fontname = $postscriptname;
340       }
341     }
342     else {
343       $fontname = $fullname;
344     }
345     if (defined($options{NFontName})) {
346       for my $fn (@{$options{NFontName}}) {
347         next NXTLINE if ($fontname =~ /$fn/i);
348       }
349     }
350     if (defined($options{FontName})) {
351       for my $fn (@{$options{FontName}}) {
352         next NXTLINE if ($fontname !~ /$fn/i);
353       }
354     }
355     my $props = "";
356     my @errors = ();
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);
362         }
363       }
364       if (defined($options{NProperty})) {
365         for my $pn (@{$options{NProperty}}) {
366           next NXTLINE if ($properties =~ /$pn/i);
367         }
368       }
369       if (exists($options{PrintProperties})) {
370         $props .= " ($properties)";
371       }
372     }
373
374     if (exists($options{PrintLangs})) {
375       $props .= '(' . join(',', sort keys %usedlangs) . ')';
376     }
377     if (exists($options{PrintScripts}) || defined($options{Scripts}) || defined($options{NScripts}) || exists($options{Math})) {
378       my @scripts = ();
379       my $scripts = "";
380       if ($l =~ / script=\"([^\"]+)\"/) {
381         @scripts = split(/\s+/, $1);
382         for my $ent (@scripts) {
383           $ent =~ s/^\s*otlayout://;
384           $ent = lc($ent);
385         }
386         $scripts = join(',', @scripts);
387       }
388       if (exists($options{Math})) {
389         next NXTLINE if (! &ismathfont($fontname,\@scripts));
390       }
391       if (exists($options{PrintScripts})) {
392         $props .= "($scripts)";
393       }
394       if (!defined($scripts[0])) {
395         # No script defined in font, so check only $options{Scripts}
396         next NXTLINE if (defined($options{Scripts}));
397       }
398       else {
399         if (defined($options{Scripts})) {
400           for my $s (@{$options{Scripts}}) {
401             next NXTLINE if ($scripts !~ /$s/i);
402           }
403         }
404         if (defined($options{NScripts})) {
405           for my $s (@{$options{NScripts}}) {
406             next NXTLINE if ($scripts =~ /$s/i);
407           }
408         }
409       }
410     }
411     my $foundry = "";
412     if ($l =~ /foundry=\"([^\"]+)\"/) {
413       $foundry = $1;
414       $foundry =~ s/^\s+//;
415       $foundry =~ s/\s+$//;
416     }
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);
422       }
423       if ($fontpriority{$oldfonttype} > $fontpriority{$fonttype}) {
424         push(@{$collectedfonts{$fontname}->{$foundry}->{errors}}, "Warning: overwriting old info for file: " . $collectedfonts{$fontname}->{$foundry}->{file});
425       }
426       else {
427         push(@{$collectedfonts{$fontname}->{$foundry}->{errors}}, "Warning: discarding new info for file: $file");
428         next;
429       }
430     }
431     else {
432       $collectedfonts{$fontname}->{$foundry}->{errors} = \@errors;
433     }
434     $collectedfonts{$fontname}->{$foundry}->{props} = $props;
435     $collectedfonts{$fontname}->{$foundry}->{file} = $file;
436     $collectedfonts{$fontname}->{$foundry}->{fonttype} = $fonttype;
437   }
438   close(FI);
439 }
440
441 for my $fontname (sort keys %collectedfonts) {
442   my @foundries = sort keys %{$collectedfonts{$fontname}};
443   my $printfoundries = 0;
444   if (defined($foundries[1])) {
445     $printfoundries = 1;
446   }
447   for my $foundry (@foundries) {
448     if (exists($options{PrintWarnings})) {
449       for my $err (@{$collectedfonts{$fontname}->{$foundry}->{errors}}) {
450         print "$err\n";
451       }
452     }
453     my $fn = "Font : $fontname";
454     if ($printfoundries && ($foundry ne "")) {
455       $fn .= " \[$foundry\]";
456     }
457     print $fn;
458     print $collectedfonts{$fontname}->{$foundry}->{props};
459     if (exists($options{PrintFiles})) {
460       print ": " . $collectedfonts{$fontname}->{$foundry}->{file} . "\n";
461     }
462     else {
463       print "\n";
464     }
465   }
466 }
467
468 exit(0);
469 #################################################################################
470 sub convertlang($)
471 {
472   my ($ilang) = @_;
473   if ($ilang =~ /^\s*([a-z]+)([\-_]([a-z]+))?\s*$/i) {
474     my ($x, $y) = ($1, $3);
475     if (defined($y)) {
476       $ilang = lc($x) . '-' . lc($y);
477     }
478     else {
479       $ilang = lc($x);
480     }
481   }
482   return($ilang);
483 }
484
485 sub extractlist($$$)
486 {
487   my ($l, $islang, $txt, $rres) = @_;
488   my @res = ();
489   if ($l =~ /$txt=\"([^\"]+)\"/) {
490     @res = split(',', $1);
491     if ($islang) {
492       for my $lg (@res) {
493         $lg = &convertlang($lg);
494       }
495     }
496   }
497   @{$rres} = @res;
498 }
499
500 sub getIndexes($$)
501 {
502   my ($lang, $rlangs) = @_;
503   my @res = ();
504
505   for (my $i = 0; defined($rlangs->[$i]); $i++) {
506     if ($rlangs->[$i] eq $lang) {
507       push(@res, $i);
508     }
509   }
510   return(\@res);
511 }
512
513 sub getVal($$$)
514 {
515   my ($l, $txtval, $txtlang) = @_;
516   my @values = ();
517   my @langs = ();
518   &extractlist($l, 0, $txtval, \@values);
519   return("") if (! defined($values[0]));
520   &extractlist($l, 1, $txtlang, \@langs);
521   my $i = &getIndexes("en", \@langs);
522   my $res = "";
523   for my $k (@{$i}) {
524     if (defined($values[$k]) && (length($values[$k]) > length($res))) {
525       $res = $values[$k];
526     }
527   }
528   return($values[0]) if ($res eq "");
529   return($res);
530 }
531
532 sub getsinglevalue($$$)
533 {
534   my ($l, $txt, $rMap, $rget) = @_;
535   my $val;
536   if ($l =~ / $txt=(\d+)/) {
537     $val = $1;
538     # Search for nearest value to $val
539     if (defined($rMap->{$val})) {
540       return($rMap->{$val});
541     }
542     my $maxv = -1;
543     my $minv = 1000;
544     for my $key (keys %{$rMap}) {
545       next if ($key !~ /^\d+$/);
546       my $diff = abs($key - $val);
547       if ($diff < $minv) {
548         $maxv = $key;
549         $minv = $diff;
550       }
551       elsif ($diff == $minv) {
552         if ($key < $maxv) {
553           $maxv = $key;
554         }
555       }
556     }
557     if (! defined($rMap->{$maxv})) {
558       print "ERROR2: txt=$txt, val=$val\n";
559       exit(-2);
560     }
561     if ($val > $maxv) {
562       return($rMap->{$maxv} . "+$minv");
563     }
564     else {
565       return($rMap->{$maxv} . "-$minv");
566     }
567   }
568   else {
569     return(undef);
570   }
571 }
572
573 sub addTxt($$)
574 {
575   my ($txt, $val) = @_;
576   return("$txt($val)");
577 }
578
579 sub getftype($$)
580 {
581   my ($fontname, $style) = @_;
582   if ($fontname =~ /(sans)[-_ ]?(serif)?/i) {
583     return($ftypes{100}); # Sans Serif
584   }
585   elsif ($fontname =~ /gothic|dotum|gulim/i) {
586     if ($fontname =~ /bisrat gothic/i) {
587       return($ftypes{0});    # Serif
588     }
589     else {
590       return($ftypes{100}); # Sans Serif
591     }
592   }
593   elsif ($fontname =~ /serif|times|mincho|batang/i) {
594     if ($fontname =~ /good times/i) {
595       return($ftypes{100}); # Sans Serif
596     }
597     elsif ($fontname !~ /initials/i) {
598       return($ftypes{0});    # Serif
599     }
600   }
601   # Now check for fonts without a hint in font name
602   if ($fontname =~ /([a-z])/i) {
603     my $key = lc($1);
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"}});
608         }
609       }
610     }
611   }
612   if ("$fontname" =~ /^bpg/i) {
613     if ("$fontname" =~ /bpg (courier gpl|elite)/i) {
614       return($ftypes{0});    # Serif
615     }
616     else {
617       return($ftypes{100}); # Sans Serif
618     }
619   }
620   elsif ("$fontname" =~ /^dustismo/i) {
621     if ("$fontname" =~ /^dustismo roman/i) {
622       return($ftypes{0});    # Serif
623     }
624     else {
625       return($ftypes{100}); # Sans Serif
626     }
627   }
628   elsif ("$fontname" =~ /^go\b/i) {
629     if ("$fontname" =~ /^go mono/i) {
630       return($ftypes{0});    # Serif
631     }
632     else {
633       return($ftypes{100}); # Sans Serif
634     }
635   }
636   else {
637     return(undef);
638   }
639 }
640
641 sub getweight($$)
642 {
643   my ($fontname, $style) = @_;
644   my $result = undef;
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
652         }
653         else {
654           return($val);
655         }
656       }
657     }
658   }
659   return($result);
660 }
661
662 sub getwidth($$)
663 {
664   my ($fontname, $style) = @_;
665   my $result = undef;
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});
671       }
672       if ($info =~ /\bRegular\b/) {
673         if (!defined($result)) {
674           $result = $widths{100};
675         }
676       }
677     }
678   }
679   return($result);
680 }
681
682 sub getslant($$)
683 {
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});
689     }
690   }
691   return(undef);
692 }
693
694 sub getspacing($$)
695 {
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});
701     }
702   }
703   if ("$fontname $style" =~ /(mono|typewriter|cursor|fixed)\b/i) {
704     return($spacings{100}); # Mono
705   }
706   else {
707     return(undef);
708   }
709 }
710
711 sub ismathfont($$)
712 {
713   my ($fontname, $rCapability) = @_;
714
715   return 1 if ($fontname =~ /math/i);
716   for my $cap (@{$rCapability}) {
717     return 1 if ($cap eq "math");
718   }
719   return 0;
720 }
721
722 sub getproperties($$$$)
723 {
724   my ($l, $fontname, $style, $rerrors) = @_;
725   my $newstyle = &correctstyle($style);
726   my $newfam = &correctstyle($fontname);
727   my @properties = ();
728
729   for my $txt (qw(ftype weight width slant spacing)) {
730     my ($map, $rget);
731     eval("\$map = " . '\%' . $txt . 's');
732     eval('$rget = \&' . "get$txt");
733     my $val2 = getsinglevalue($l, $txt, $map);
734     my $val1 = $rget->($newfam, $newstyle);
735     my $val;
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)");
738       $val = $val2;
739     }
740     elsif (! defined($val2)) {
741       $val = $val1;
742     }
743     else {
744       $val = $val2;
745     }
746     if (defined($val)) {
747       push(@properties, &addTxt($txt,$val));
748     }
749     else {
750       if (defined($map->{"default"})) {
751         push(@properties, &addTxt($txt,$map->{"default"}));
752       }
753       else {
754         push(@{$rerrors}, "Undefined value for $txt");
755       }
756     }
757   }
758   return(join(' ', @properties));
759 }
760
761 sub correctstyle($)
762 {
763   my ($style) = @_;
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;
795   $style =~ s/  +/ /g;
796   return($style);
797 }