]> git.lyx.org Git - lyx.git/blob - development/tools/listFontWithLang.pl
88fed6bddae2e34f53dc67e866f080c48c102bf1
[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   ["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   "c" => qr/^(chancery)/i,
246   "d" => qr/^(dancing)/i,
247   "e" => qr/^(elegante)/i,
248   "k" => qr/^(kaushan|karumbi)/i,
249   "m" => qr/^(mathjax_script|miama)/i,
250   "n" => qr/^(nanum (brush|pen) script)/i,
251   "q" => qr/^qt(arabian|boulevard|brushstroke|chancery|coronation|florencia|handwriting|linostroke|merry|pandora|slogan)/i,
252   "r" => qr/^(romande.*|ruf)script/i,
253   "u" => qr/^(un ?pilgi|urw ?chancery)/i,
254 );
255
256 my %fraktFonts = (
257   "value" => 120,          # Fraktur
258   "j" => qr/^(jsmath.?euf)/i,
259   "m" => qr/^(missaali)/i,
260   "o" => qr/^(oldania)/i,
261   "q" => qr/^qt(blackforest|cloisteredmonk|dublinirish|fraktur|heidelbergtype|(lino|london)scroll)/i,
262 );
263
264 my %fancyFonts = (
265   "value" => 130,          # Fancy
266   "c" => qr/^(cretino)/i,
267   "g" => qr/^(gfs.?theo)/i,
268 );
269
270 my %initialFonts = (
271   "value" => 140,          # Initials
272   "e" => qr/^(eb.?garamond.?init)/i,
273   "l" => qr/^(libertinus|linux).*initials/i,
274   "y" => qr/^(yinit)/i,
275 );
276
277 my %symbolFonts = (
278   "value" => 200,          # Symbol
279   "a" => qr/^(academicons)/i,
280   "c" => qr/^(caladings|ccicons|chess)/i,
281   "d" => qr/^(dingbats|drmsym)/i,
282   "e" => qr/^(elusiveicons|emoji)/i,
283   "f" => qr/^(fdsymbol|fourierorns)/i,
284   "h" => qr/^(hots)/i,
285   "m" => qr/^(marvosym|material)/i,
286   "n" => qr/^(noto.*emoji)/i,
287   "o" => qr/^(octicons)/i,
288   "q" => qr/^(qtdingbits)/i,
289   "t" => qr/^(typicons|twemoji)/i,
290 );
291
292 if (open(FI,  "$cmd |")) {
293  NXTLINE: while (my $l = <FI>) {
294     chomp($l);
295     while ($l !~ /abcd$/) {
296       $l .= <FI>;
297       chomp($l);
298     }
299     my $file = "";
300     my $fonttype;
301     if ($l =~ /file=\"([^\"]+)\"/) {
302       $file = $1;
303       #next if ($file !~ /\.(otf|ttf|pfa|pfb|pcf|ttc)$/i);
304       if ($file !~ /\.([a-z0-9]{2,5})$/i) {
305         print "Unhandled extension for file $file\n";
306         next;
307       }
308       $fonttype = lc($1);
309       if (! defined($fontpriority{$fonttype})) {
310         print "Added extension $fonttype for file $file\n";
311         $fontpriority{$fonttype} = $nexttype;
312         $nexttype++;
313       }
314     }
315     my $nfound = 0;
316     my %usedlangs = ();
317     if ($l =~ / lang=\"([^\"]+)\"/) {
318       my @ll = split(/\|/, $1);
319       for my $lx (@ll) {
320         $usedlangs{&convertlang($lx)} = 1;
321       }
322     }
323
324     for my $lang (@langs) {
325       next NXTLINE if (! defined($usedlangs{$lang}));
326     }
327     next if ($nfound);
328     my $style = &getVal($l, "style", "stylelang");
329     $style =~ s/^\\040//;
330     my $fullname = &getVal($l, "fn", "fnl");
331     my $postscriptname = "";
332     if ($l =~ /postscriptname=\"([^\"]+)\"/) {
333       $postscriptname = $1;
334     }
335     my $family = &getVal($l, "family", "flang");
336     $family =~ s/\\040/\-/;
337     my $fontname;
338     if (length($fullname) < 3) {
339       if (length($postscriptname) < 2) {
340         $fontname = "$family $style";
341       }
342       else {
343         $fontname = $postscriptname;
344       }
345     }
346     else {
347       $fontname = $fullname;
348     }
349     if (defined($options{NFontName})) {
350       for my $fn (@{$options{NFontName}}) {
351         next NXTLINE if ($fontname =~ /$fn/i);
352       }
353     }
354     if (defined($options{FontName})) {
355       for my $fn (@{$options{FontName}}) {
356         next NXTLINE if ($fontname !~ /$fn/i);
357       }
358     }
359     my $props = "";
360     my @errors = ();
361     if (exists($options{PrintProperties}) || defined($options{Property}) || defined($options{NProperty})) {
362       my $properties = getproperties($l, $fontname, $style, \@errors);
363       if (defined($options{Property})) {
364         for my $pn (@{$options{Property}}) {
365           next NXTLINE if ($properties !~ /$pn/i);
366         }
367       }
368       if (defined($options{NProperty})) {
369         for my $pn (@{$options{NProperty}}) {
370           next NXTLINE if ($properties =~ /$pn/i);
371         }
372       }
373       if (exists($options{PrintProperties})) {
374         $props .= " ($properties)";
375       }
376     }
377
378     if (exists($options{PrintLangs})) {
379       $props .= '(' . join(',', sort keys %usedlangs) . ')';
380     }
381     if (exists($options{PrintScripts}) || defined($options{Scripts}) || defined($options{NScripts}) || exists($options{Math})) {
382       my @scripts = ();
383       my $scripts = "";
384       if ($l =~ / script=\"([^\"]+)\"/) {
385         @scripts = split(/\s+/, $1);
386         for my $ent (@scripts) {
387           $ent =~ s/^\s*otlayout://;
388           $ent = lc($ent);
389         }
390         $scripts = join(',', @scripts);
391       }
392       if (exists($options{Math})) {
393         next NXTLINE if (! &ismathfont($fontname,\@scripts));
394       }
395       if (exists($options{PrintScripts})) {
396         $props .= "($scripts)";
397       }
398       if (!defined($scripts[0])) {
399         # No script defined in font, so check only $options{Scripts}
400         next NXTLINE if (defined($options{Scripts}));
401       }
402       else {
403         if (defined($options{Scripts})) {
404           for my $s (@{$options{Scripts}}) {
405             next NXTLINE if ($scripts !~ /$s/i);
406           }
407         }
408         if (defined($options{NScripts})) {
409           for my $s (@{$options{NScripts}}) {
410             next NXTLINE if ($scripts =~ /$s/i);
411           }
412         }
413       }
414     }
415     my $foundry = "";
416     if ($l =~ /foundry=\"([^\"]+)\"/) {
417       $foundry = $1;
418       $foundry =~ s/^\s+//;
419       $foundry =~ s/\s+$//;
420     }
421     if (defined($collectedfonts{$fontname}->{$foundry}->{errors})) {
422       # Apparently not the first one, so add some info
423       my $oldfonttype = $collectedfonts{$fontname}->{$foundry}->{fonttype};
424       if (defined($errors[0])) {
425         push(@{$collectedfonts{$fontname}->{$foundry}->{errors}}, @errors);
426       }
427       if ($fontpriority{$oldfonttype} > $fontpriority{$fonttype}) {
428         push(@{$collectedfonts{$fontname}->{$foundry}->{errors}}, "Warning: overwriting old info for file: " . $collectedfonts{$fontname}->{$foundry}->{file});
429       }
430       else {
431         push(@{$collectedfonts{$fontname}->{$foundry}->{errors}}, "Warning: discarding new info for file: $file");
432         next;
433       }
434     }
435     else {
436       $collectedfonts{$fontname}->{$foundry}->{errors} = \@errors;
437     }
438     $collectedfonts{$fontname}->{$foundry}->{props} = $props;
439     $collectedfonts{$fontname}->{$foundry}->{file} = $file;
440     $collectedfonts{$fontname}->{$foundry}->{fonttype} = $fonttype;
441   }
442   close(FI);
443 }
444
445 for my $fontname (sort keys %collectedfonts) {
446   my @foundries = sort keys %{$collectedfonts{$fontname}};
447   my $printfoundries = 0;
448   if (defined($foundries[1])) {
449     $printfoundries = 1;
450   }
451   for my $foundry (@foundries) {
452     if (exists($options{PrintWarnings})) {
453       for my $err (@{$collectedfonts{$fontname}->{$foundry}->{errors}}) {
454         print "$err\n";
455       }
456     }
457     my $fn = "Font : $fontname";
458     if ($printfoundries && ($foundry ne "")) {
459       $fn .= " \[$foundry\]";
460     }
461     print $fn;
462     print $collectedfonts{$fontname}->{$foundry}->{props};
463     if (exists($options{PrintFiles})) {
464       print ": " . $collectedfonts{$fontname}->{$foundry}->{file} . "\n";
465     }
466     else {
467       print "\n";
468     }
469   }
470 }
471
472 exit(0);
473 #################################################################################
474 sub convertlang($)
475 {
476   my ($ilang) = @_;
477   if ($ilang =~ /^\s*([a-z]+)([\-_]([a-z]+))?\s*$/i) {
478     my ($x, $y) = ($1, $3);
479     if (defined($y)) {
480       $ilang = lc($x) . '-' . lc($y);
481     }
482     else {
483       $ilang = lc($x);
484     }
485   }
486   return($ilang);
487 }
488
489 sub extractlist($$$)
490 {
491   my ($l, $islang, $txt, $rres) = @_;
492   my @res = ();
493   if ($l =~ /$txt=\"([^\"]+)\"/) {
494     @res = split(',', $1);
495     if ($islang) {
496       for my $lg (@res) {
497         $lg = &convertlang($lg);
498       }
499     }
500   }
501   @{$rres} = @res;
502 }
503
504 sub getIndexes($$)
505 {
506   my ($lang, $rlangs) = @_;
507   my @res = ();
508
509   for (my $i = 0; defined($rlangs->[$i]); $i++) {
510     if ($rlangs->[$i] eq $lang) {
511       push(@res, $i);
512     }
513   }
514   return(\@res);
515 }
516
517 sub getVal($$$)
518 {
519   my ($l, $txtval, $txtlang) = @_;
520   my @values = ();
521   my @langs = ();
522   &extractlist($l, 0, $txtval, \@values);
523   return("") if (! defined($values[0]));
524   &extractlist($l, 1, $txtlang, \@langs);
525   my $i = &getIndexes("en", \@langs);
526   my $res = "";
527   for my $k (@{$i}) {
528     if (defined($values[$k]) && (length($values[$k]) > length($res))) {
529       $res = $values[$k];
530     }
531   }
532   return($values[0]) if ($res eq "");
533   return($res);
534 }
535
536 sub getsinglevalue($$$)
537 {
538   my ($l, $txt, $rMap, $rget) = @_;
539   my $val;
540   if ($l =~ / $txt=(\d+)/) {
541     $val = $1;
542     # Search for nearest value to $val
543     if (defined($rMap->{$val})) {
544       return($rMap->{$val});
545     }
546     my $maxv = -1;
547     my $minv = 1000;
548     for my $key (keys %{$rMap}) {
549       next if ($key !~ /^\d+$/);
550       my $diff = abs($key - $val);
551       if ($diff < $minv) {
552         $maxv = $key;
553         $minv = $diff;
554       }
555       elsif ($diff == $minv) {
556         if ($key < $maxv) {
557           $maxv = $key;
558         }
559       }
560     }
561     if (! defined($rMap->{$maxv})) {
562       print "ERROR2: txt=$txt, val=$val\n";
563       exit(-2);
564     }
565     if ($val > $maxv) {
566       return($rMap->{$maxv} . "+$minv");
567     }
568     else {
569       return($rMap->{$maxv} . "-$minv");
570     }
571   }
572   else {
573     return(undef);
574   }
575 }
576
577 sub addTxt($$)
578 {
579   my ($txt, $val) = @_;
580   return("$txt($val)");
581 }
582
583 sub getftype($$)
584 {
585   my ($fontname, $style) = @_;
586   if ($fontname =~ /(sans)[-_ ]?(serif)?/i) {
587     return($ftypes{100}); # Sans Serif
588   }
589   elsif ($fontname =~ /gothic|dotum|gulim/i) {
590     if ($fontname =~ /bisrat gothic/i) {
591       return($ftypes{0});    # Serif
592     }
593     else {
594       return($ftypes{100}); # Sans Serif
595     }
596   }
597   elsif ($fontname =~ /serif|times|mincho|batang/i) {
598     if ($fontname =~ /good times/i) {
599       return($ftypes{100}); # Sans Serif
600     }
601     elsif ($fontname !~ /initials/i) {
602       return($ftypes{0});    # Serif
603     }
604   }
605   # Now check for fonts without a hint in font name
606   if ($fontname =~ /([a-z])/i) {
607     my $key = lc($1);
608     for my $rFonts (\%sansFonts, \%scriptFonts, \%fraktFonts, \%fancyFonts, \%initialFonts, \%symbolFonts) {
609       if (defined($rFonts->{$key})) {
610         if ($fontname =~ $rFonts->{$key}) {
611           return($ftypes{$rFonts->{"value"}});
612         }
613       }
614     }
615   }
616   if ("$fontname" =~ /^bpg/i) {
617     if ("$fontname" =~ /bpg (courier gpl|elite)/i) {
618       return($ftypes{0});    # Serif
619     }
620     else {
621       return($ftypes{100}); # Sans Serif
622     }
623   }
624   elsif ("$fontname" =~ /^dustismo/i) {
625     if ("$fontname" =~ /^dustismo roman/i) {
626       return($ftypes{0});    # Serif
627     }
628     else {
629       return($ftypes{100}); # Sans Serif
630     }
631   }
632   elsif ("$fontname" =~ /^go\b/i) {
633     if ("$fontname" =~ /^go mono/i) {
634       return($ftypes{0});    # Serif
635     }
636     else {
637       return($ftypes{100}); # Sans Serif
638     }
639   }
640   else {
641     return(undef);
642   }
643 }
644
645 sub getweight($$)
646 {
647   my ($fontname, $style) = @_;
648   my $result = undef;
649   for my $key (keys %weights) {
650     next if ($key !~ /^\d+$/);
651     my $val = $weights{$key};
652     for my $info ($style, $fontname) {
653       if ($info =~ /\b$val\b/i) {
654         if ($val eq "Regular") {
655           $result = $val;    # It may refer to width
656         }
657         else {
658           return($val);
659         }
660       }
661     }
662   }
663   return($result);
664 }
665
666 sub getwidth($$)
667 {
668   my ($fontname, $style) = @_;
669   my $result = undef;
670   for my $key (keys %widths) {
671     next if ($key !~ /^\d+$/);
672     for my $info ($style, $fontname) {
673       if ($info =~ /\b$widths{$key}\b/i) {
674         return($widths{$key});
675       }
676       if ($info =~ /\bRegular\b/) {
677         if (!defined($result)) {
678           $result = $widths{100};
679         }
680       }
681     }
682   }
683   return($result);
684 }
685
686 sub getslant($$)
687 {
688   my ($fontname, $style) = @_;
689   for my $key (keys %slants) {
690     next if ($key !~ /^\d+$/);
691     if ($style =~ /\b$slants{$key}\b/i) {
692       return($slants{$key});
693     }
694   }
695   return(undef);
696 }
697
698 sub getspacing($$)
699 {
700   my ($fontname, $style) = @_;
701   for my $key (keys %spacings) {
702     next if ($key !~ /^\d+$/);
703     if ($style =~ /\b$spacings{$key}\b/i) {
704       return($spacings{$key});
705     }
706   }
707   if ("$fontname $style" =~ /(mono|typewriter|cursor|fixed)\b/i) {
708     return($spacings{100}); # Mono
709   }
710   else {
711     return(undef);
712   }
713 }
714
715 sub ismathfont($$)
716 {
717   my ($fontname, $rCapability) = @_;
718
719   return 1 if ($fontname =~ /math/i);
720   for my $cap (@{$rCapability}) {
721     return 1 if ($cap eq "math");
722   }
723   return 0;
724 }
725
726 sub getproperties($$$$)
727 {
728   my ($l, $fontname, $style, $rerrors) = @_;
729   my $newstyle = &correctstyle($style);
730   my $newfam = &correctstyle($fontname);
731   my @properties = ();
732
733   for my $txt (qw(ftype weight width slant spacing)) {
734     my ($map, $rget);
735     eval("\$map = " . '\%' . $txt . 's');
736     eval('$rget = \&' . "get$txt");
737     my $val2 = getsinglevalue($l, $txt, $map);
738     my $val1 = $rget->($newfam, $newstyle);
739     my $val;
740     if (defined($val2) && defined($val1) && ($val2 ne $val1)) {
741       push(@{$rerrors}, "Fontname($fontname),Style($style): Values for $txt ($val1 != $val2) differ, selecting internal $txt($val2)");
742       $val = $val2;
743     }
744     elsif (! defined($val2)) {
745       $val = $val1;
746     }
747     else {
748       $val = $val2;
749     }
750     if (defined($val)) {
751       push(@properties, &addTxt($txt,$val));
752     }
753     else {
754       if (defined($map->{"default"})) {
755         push(@properties, &addTxt($txt,$map->{"default"}));
756       }
757       else {
758         push(@{$rerrors}, "Undefined value for $txt");
759       }
760     }
761   }
762   return(join(' ', @properties));
763 }
764
765 sub correctstyle($)
766 {
767   my ($style) = @_;
768   $style =~ s/^\\040//;
769   $style =~ s/^\s*\d+\s*//;
770   $style =~ s/\s*\d+$//;
771   $style =~ s/italic/ Italic/i;
772   $style =~ s/oblique/ Oblique/i;
773   $style =~ s/[\-_]/ /g;
774   $style =~ s/\breg\b/Regular/i;
775   $style =~ s/\bregita(lic)?\b/Regular Italic/i;
776   $style =~ s/\bregobl(ique)?\b/Regular Oblique/i;
777   $style =~ s/medium/Medium /i;
778   $style =~ s/\bmedita(lic)?\b/Medium Italic/i;
779   $style =~ s/\bmedobl(ique)?\b/Medium Oblique/i;
780   $style =~ s/\bmed\b/Medium /i;
781   $style =~ s/\bdemi\b/SemiBold/i;
782   $style =~ s/\bex(pd|t)\b/Expanded/i;
783   $style =~ s/semi ?cond(ensed)?/SemiCondensed/i;
784   $style =~ s/[sd]emi ?(bold|bd|bol)/SemiBold/i;
785   $style =~ s/semi ?(expanded|extended|expd)/SemiExpanded/i;
786   $style =~ s/[sd]emi ?light/SemiLight/i;
787   $style =~ s/ultra ?(expanded|extended|expd)/UltraExpanded/i;
788   $style =~ s/light/Light /i;
789   $style =~ s/\blt\b/Light /i;
790   $style =~ s/(ultra|extra)(light|lt)/ExtraLight /i;
791   $style =~ s/\bheavy\b/Extrabold/i;
792   $style =~ s/\bhairline\b/Extralight/i;
793   $style =~ s/\bcond\b/Condensed/i;
794   $style =~ s/(roman)?slanted/ Italic/i;
795   $style =~ s/\bslant\b/Italic/i;
796   $style =~ s/\b(SC|Small(caps(alt)?)?)\b/SmallCaps/i;
797   $style =~ s/w3 mono/Dual/i;
798   $style =~ s/Regul[ea]r/Regular/i;
799   $style =~ s/  +/ /g;
800   return($style);
801 }