]> git.lyx.org Git - lyx.git/blob - development/tools/listFontWithLang.pl
4cd9f0843f3f2e3368f624550cb004a8a658361a
[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 Encode;
36 use GetOptions;
37
38 sub convertlang($);
39 sub extractlist($$$);   # my ($l, $islang, $txt, $rres) = @_;
40 sub getIndexes($$);
41 sub getVal($$$);        # my ($l, $txtval, $txtlang) = @_;
42 sub getproperties($$$$);
43 sub ismathfont($$);
44 sub correctstyle($);
45 sub decimalUnicode($);
46 sub contains($$);
47
48 # Following fields for a parameter can be defined:
49 # fieldname:         Name of entry in %options
50 # type:              [:=][sif], ':' = optional, '=' = required, 's' = string, 'i' = integer, 'f' = float
51 # alias:             reference to a list of aliases e.g. ["alias1", "alias2", ... ]
52 # listsep:           Separator for multiple data
53 # comment:           Parameter description
54 my @optionsDef = (
55   # help + verbose already handled in 'GetOptions'
56   ["n",
57    {fieldname => "FontName", listsep => ',',
58     type => "=s", alias => ["name"],
59     comment => "Select font-names matching these (comma separated) regexes"},],
60   ["nn",
61    {fieldname => "NFontName",
62     type => "=s", listsep => ',',
63     comment => "Select font-names NOT matching these (comma separated) regexes"},],
64   ["p",
65    {fieldname => "Property",
66     type => "=s", listsep => ',',
67     comment => "Select fonts with properties matching these (comma separated) regexes"},],
68   ["np",
69    {fieldname => "NProperty",
70     type => "=s", listsep => ',',
71     comment => "Select fonts with properties NOT matching these (comma separated) regexes"},],
72   ["s",
73    {fieldname => "Scripts",
74     type => "=s", listsep => ',',
75     comment => "Select fonts with scripts matching these (comma separated) regexes"},],
76   ["ns",
77    {fieldname => "NScripts",
78     type => "=s", listsep => ',',
79     comment => "Select fonts with scripts NOT matching these (comma separated) regexes"},],
80   ["math",
81    {fieldname => "Math",
82     comment => "Select fonts probably containing math glyphs"},],
83   ["c",
84    {fieldname => "Contains",
85     type => "=s", listsep => ',',
86     comment => "Select fonts containing all comma separated glyphs",}],
87   ["l",
88    {fieldname => "Lang",
89     type => "=s", alias=>["lang"],
90     comment => "Comma separated list of desired languages"},],
91   ["pl",
92    {fieldname => "PrintLangs", alias => ["printlangs"],
93     comment => "Print supported languages"},],
94   ["pp",
95    {fieldname => "PrintProperties", alias => ["printproperties"],
96     comment => "Print properties from weight, slant and width"},],
97   ["ps",
98    {fieldname => "PrintScripts", alias => ["printscripts"],
99     comment => "Print supported scripts"},],
100   ["pf",
101    {fieldname => "PrintFiles", alias => ["printfiles"],
102     comment => "Print font file names"},],
103   ["pw",
104    {fieldname => "PrintWarnings",
105     comment => "Print warnings about discarded/overwritten fonts, conflicting styles"},],
106 );
107 my %options = %{&handleOptions(\@optionsDef)};
108
109 $options{Lang} = "" if (! defined($options{Lang}));
110
111 #############################################################
112
113 my @langs = split(',', $options{Lang});
114 for my $lg (@langs) {
115   $lg = &convertlang($lg);
116 }
117
118 my @glyphs = ();
119 if (defined($options{Contains})) {
120   for my $a (@{$options{Contains}}) {
121     push(@glyphs, decimalUnicode($a));
122   }
123 }
124
125 my $cmd = "fc-list";
126 if (defined($langs[0])) {
127   $cmd .= " :lang=" . join(',', @langs);
128 }
129
130 my $format = "foundry=\"%{foundry}\"" .
131     " postscriptname=\"%{postscriptname}\"" .
132     " fn=\"%{fullname}\" fnl=\"%{fullnamelang}\"" .
133     " family=\"%{family}\" flang=\"%{familylang}\" " .
134     " style=\"%{style}\" stylelang=\"%{stylelang}\"";
135
136 if (exists($options{PrintScripts}) || defined($options{Scripts}) || defined($options{NSpripts}) || exists($options{Math})) {
137   $format .= " script=\"%{capability}\"";
138 }
139 if (exists($options{PrintLangs}) || defined($langs[0])) {
140   $format .= " lang=\"%{lang}\"";
141 }
142 if (exists($options{PrintProperties}) || defined($options{Property})) {
143   $format .= " weight=%{weight} slant=%{slant} width=%{width} spacing=%{spacing}";
144 }
145 if (defined($options{Contains})) {
146   $format .= " charset=\"%{charset}\"";
147 }
148 $format .= " file=\"%{file}\" abcd\\n";
149 $cmd .= " -f '$format'";
150 #print "$cmd\n";
151
152 my %ftypes = (
153   # Dummy internal map
154   0 => "Serif",
155   100 => "Sans",
156   110 => "Script",
157   120 => "Fraktur",
158   130 => "Fancy",
159   140 => "Initials",
160   200 => "Symbol",
161   "default" => "Serif",
162 );
163
164 my %weights = (
165   0 => "Thin",
166   40 => "Extralight",
167   50 => "Light",
168   55 => "Semilight",
169   75 => "Book",
170   80 => "Regular",
171   100 => "Medium",
172   180 => "Semibold",
173   200 => "Bold",
174   205 => "Extrabold",
175   210 => "Black",
176 );
177
178 my %slants = (
179   0 => "Roman",
180   100 => "Italic",
181   110 => "Oblique",
182 );
183
184 my %widths = (
185   50 => "Ultracondensed",
186   63 => "Extracondensed",
187   75 => "Condensed",
188   87 => "Semicondensed",
189   100 => "Normal",
190   113 => "Semiexpanded",
191   125 => "Expanded",
192   150 => "Extraexpanded",
193   200 => "Ultraexpanded",
194 );
195
196 my %spacings = (
197   0 => "Proportional",
198   90 => "Dual",
199   100 => "Mono",
200   110 => "Charcell",
201   "default" => "Proportional",
202 );
203
204 # Build reverse mappings, (not needed yet)
205 for my $txt (qw(ftypes weights slants widths spacings)) {
206   my $map;
207   eval "\$map = \\%$txt";
208   for my $key (keys %{$map}) {
209     next if ($key !~ /^\d+$/);
210     my $value = lc($map->{$key});
211     $map->{"r"}->{$value} = $key;
212   }
213 }
214
215 # key:= fontname
216 #     subkey foundry
217 #            subfoundry
218 my %collectedfonts = ();
219 my %fontpriority = (
220   otf => 0,                # type 2, opentype CFF (Compact Font Format)
221   ttc => 1.1,              # type 1 (True Type font Collection)
222   ttf => 1.2,              # type 1 (True Type Font)
223   woff=> 1.3,              # type 1 (Web Open Font Format)
224   t1  => 1.4,              # type 1 (postscript)
225   pfb => 1.5,              # type 1 (Printer Font Binary)
226   pfa => 1.6,              # type 1 (Printer Font Ascii)
227   pcf => 5,                # Bitmap (Packaged Collaboration Files)?
228 );
229 my $nexttype = 6;
230
231 # list of regexes for known sans serif fonts
232 my %sansFonts = (
233   "value" => 100,          # Sans serif
234   "a" => qr/^(arial|andika|angostura|anonymous|arab|aroania|arimo|asap)/i,
235   "b" => qr/^b(aekmuk|ebas|erenika|eteckna|euron|lue)/i,
236   "c" => qr/^c(abin|aliban|antarell|arbon|arlito|handas|hivo|mu bright|omfortaa|omic|oolvetica|ortoba|ousine|uprum|wtex(hei|yen)|yklop|ypro)/i,
237   "d" => qr/^(d2coding|dimnah|dosis|dyuthi)/i,
238   "e" => qr/^(electron|engebrechtre)/i,
239   "f" => qr/^(fandolhei|fetamont|fira|font awesome 5|forgotten)/i,
240   "g" => qr/^(gardiner|garuda|gfs ?neo|gillius|granada|graph|guanine|gunplay)/i,
241   "h" => qr/^(hack|hani|haramain|harano|harmattan|hor\b)/i,
242   "i" => qr/^(ibm plex|ikarius|inconsolata|induni.?h|iwona)/i,
243   "j" => qr/^(jara|jura)/i,
244   "k" => qr/^(kalimati|kanji|karla|kayrawan|kenyan|keraleeyam|khalid|khmer [or]|kiloji|klaudia|komatu|kurier)/i,
245   "l" => qr/^l(aksaman|arabie|ato|eague|exend|exigulim|ibel|iberation|ibre franklin|ibris|inux biolinum|obster|ogix|ohit|oma)/i,
246   "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,
247   "n" => qr/^(nachlieli|nada|nafees|nagham|nanum(barunpen|square)|nice)/i,
248   "o" => qr/^(ocr|okolaks|opendyslexic|ostorah|ouhud|over|oxygen)/i,
249   "p" => qr/^(padauk|padmaa|pagul|paktype|pakenham|palladio|petra|phetsarath|play\b|poiret|port\b|primer\b|prociono|pt\b|purisa)/i,
250   "q" => qr/^(qt(ancient|helvet|avanti|doghaus|eratype|eurotype|floraline|frank|fritz|future|greece|howard|letter|optimum)|quercus)/i,
251   "r" => qr/^(rachana|radio\b|raleway|ricty|roboto|rosario)/i,
252   "s" => qr/^(salem|samanata|sawasdee|shado|sharja|simple|sophia|soul|source|switzera)/i,
253   "t" => qr/^(tarablus|teen|texgyre(adventor|heros)|tiresias|trebuchet|tscu|tuffy)/i,
254   "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,
255   "v" => qr/^(veranda|vn ?urwclassico)/i,
256   "w" => qr/^(waree)/i,
257   "y" => qr/^(yanone)/i,
258   "z" => qr/^(zekton|zero)/i,
259 );
260 my %scriptFonts = (
261   "value" => 110,          # Script
262   "c" => qr/^(chancery)/i,
263   "d" => qr/^(dancing)/i,
264   "e" => qr/^(elegante)/i,
265   "k" => qr/^(kaushan|karumbi)/i,
266   "m" => qr/^(mathjax_script|miama)/i,
267   "n" => qr/^(nanum (brush|pen) script)/i,
268   "q" => qr/^qt(arabian|boulevard|brushstroke|chancery|coronation|florencia|handwriting|linostroke|merry|pandora|slogan)/i,
269   "r" => qr/^(romande.*|ruf)script/i,
270   "u" => qr/^(un ?pilgi|urw ?chancery)/i,
271 );
272
273 my %fraktFonts = (
274   "value" => 120,          # Fraktur
275   "j" => qr/^(jsmath.?euf)/i,
276   "m" => qr/^(missaali)/i,
277   "o" => qr/^(oldania)/i,
278   "q" => qr/^qt(blackforest|cloisteredmonk|dublinirish|fraktur|heidelbergtype|(lino|london)scroll)/i,
279 );
280
281 my %fancyFonts = (
282   "value" => 130,          # Fancy
283   "c" => qr/^(cretino)/i,
284   "g" => qr/^(gfs.?theo)/i,
285 );
286
287 my %initialFonts = (
288   "value" => 140,          # Initials
289   "e" => qr/^(eb.?garamond.?init)/i,
290   "l" => qr/^(libertinus|linux).*initials/i,
291   "y" => qr/^(yinit)/i,
292 );
293
294 my %symbolFonts = (
295   "value" => 200,          # Symbol
296   "a" => qr/^(academicons)/i,
297   "c" => qr/^(caladings|ccicons|chess)/i,
298   "d" => qr/^(dingbats|drmsym)/i,
299   "e" => qr/^(elusiveicons|emoji)/i,
300   "f" => qr/^(fdsymbol|fourierorns)/i,
301   "h" => qr/^(hots)/i,
302   "m" => qr/^(marvosym|material)/i,
303   "n" => qr/^(noto.*emoji)/i,
304   "o" => qr/^(octicons)/i,
305   "q" => qr/^(qtdingbits)/i,
306   "t" => qr/^(typicons|twemoji)/i,
307   "w" => qr/^(webdings)/i,
308 );
309
310 if (open(FI,  "$cmd |")) {
311  NXTLINE: while (my $l = <FI>) {
312     chomp($l);
313     while ($l !~ /abcd$/) {
314       $l .= <FI>;
315       chomp($l);
316     }
317     my $file = "";
318     my $fonttype;
319     if ($l =~ /file=\"([^\"]+)\"/) {
320       $file = $1;
321       #next if ($file !~ /\.(otf|ttf|pfa|pfb|pcf|ttc)$/i);
322       if ($file !~ /\.([a-z0-9]{2,5})$/i) {
323         print "Unhandled extension for file $file\n";
324         next;
325       }
326       $fonttype = lc($1);
327       if (! defined($fontpriority{$fonttype})) {
328         print "Added extension $fonttype for file $file\n";
329         $fontpriority{$fonttype} = $nexttype;
330         $nexttype++;
331       }
332     }
333     my %usedlangs = ();
334     if ($l =~ / lang=\"([^\"]+)\"/) {
335       my @ll = split(/\|/, $1);
336       for my $lx (@ll) {
337         $usedlangs{&convertlang($lx)} = 1;
338       }
339     }
340
341     for my $lang (@langs) {
342       next NXTLINE if (! defined($usedlangs{$lang}));
343     }
344     if (defined($options{Contains})) {
345       my @charlist = ();
346       if ($l =~ / charset=\"([^\"]+)\"/) {
347         my @list = split(/\s+/, $1);
348         for my $e (@list) {
349           my ($l, $h) = split('-', $e);
350           $h = $l if (! defined($h));
351           push(@charlist, [hex($l), hex($h)]);
352         }
353       }
354       for my $g (@glyphs) {
355         next NXTLINE if (! contains($g, \@charlist));
356       }
357     }
358     my $style = &getVal($l, "style", "stylelang");
359     $style =~ s/^\\040//;
360     my $fullname = &getVal($l, "fn", "fnl");
361     my $postscriptname = "";
362     if ($l =~ /postscriptname=\"([^\"]+)\"/) {
363       $postscriptname = $1;
364     }
365     my $family = &getVal($l, "family", "flang");
366     $family =~ s/\\040/\-/;
367     my $fontname;
368     if (length($fullname) < 3) {
369       if (length($postscriptname) < 2) {
370         $fontname = "$family $style";
371       }
372       else {
373         $fontname = $postscriptname;
374       }
375     }
376     else {
377       $fontname = $fullname;
378     }
379     if (defined($options{NFontName})) {
380       for my $fn (@{$options{NFontName}}) {
381         next NXTLINE if ($fontname =~ /$fn/i);
382       }
383     }
384     if (defined($options{FontName})) {
385       for my $fn (@{$options{FontName}}) {
386         next NXTLINE if ($fontname !~ /$fn/i);
387       }
388     }
389     my $props = "";
390     my @errors = ();
391     if (exists($options{PrintProperties}) || defined($options{Property}) || defined($options{NProperty})) {
392       my $properties = getproperties($l, $fontname, $style, \@errors);
393       if (defined($options{Property})) {
394         for my $pn (@{$options{Property}}) {
395           next NXTLINE if ($properties !~ /$pn/i);
396         }
397       }
398       if (defined($options{NProperty})) {
399         for my $pn (@{$options{NProperty}}) {
400           next NXTLINE if ($properties =~ /$pn/i);
401         }
402       }
403       if (exists($options{PrintProperties})) {
404         $props .= " ($properties)";
405       }
406     }
407
408     if (exists($options{PrintLangs})) {
409       $props .= '(' . join(',', sort keys %usedlangs) . ')';
410     }
411     if (exists($options{PrintScripts}) || defined($options{Scripts}) || defined($options{NScripts}) || exists($options{Math})) {
412       my @scripts = ();
413       my $scripts = "";
414       if ($l =~ / script=\"([^\"]+)\"/) {
415         @scripts = split(/\s+/, $1);
416         for my $ent (@scripts) {
417           $ent =~ s/^\s*otlayout://;
418           $ent = lc($ent);
419         }
420         $scripts = join(',', @scripts);
421       }
422       if (exists($options{Math})) {
423         next NXTLINE if (! &ismathfont($fontname,\@scripts));
424       }
425       if (exists($options{PrintScripts})) {
426         $props .= "($scripts)";
427       }
428       if (!defined($scripts[0])) {
429         # No script defined in font, so check only $options{Scripts}
430         next NXTLINE if (defined($options{Scripts}));
431       }
432       else {
433         if (defined($options{Scripts})) {
434           for my $s (@{$options{Scripts}}) {
435             next NXTLINE if ($scripts !~ /$s/i);
436           }
437         }
438         if (defined($options{NScripts})) {
439           for my $s (@{$options{NScripts}}) {
440             next NXTLINE if ($scripts =~ /$s/i);
441           }
442         }
443       }
444     }
445     my $foundry = "";
446     if ($l =~ /foundry=\"([^\"]+)\"/) {
447       $foundry = $1;
448       $foundry =~ s/^\s+//;
449       $foundry =~ s/\s+$//;
450     }
451     if (defined($collectedfonts{$fontname}->{$foundry}->{errors})) {
452       # Apparently not the first one, so add some info
453       my $oldfonttype = $collectedfonts{$fontname}->{$foundry}->{fonttype};
454       if (defined($errors[0])) {
455         push(@{$collectedfonts{$fontname}->{$foundry}->{errors}}, @errors);
456       }
457       if ($fontpriority{$oldfonttype} > $fontpriority{$fonttype}) {
458         push(@{$collectedfonts{$fontname}->{$foundry}->{errors}}, "Warning: overwriting old info for file: " . $collectedfonts{$fontname}->{$foundry}->{file});
459       }
460       else {
461         push(@{$collectedfonts{$fontname}->{$foundry}->{errors}}, "Warning: discarding new info for file: $file");
462         next;
463       }
464     }
465     else {
466       $collectedfonts{$fontname}->{$foundry}->{errors} = \@errors;
467     }
468     $collectedfonts{$fontname}->{$foundry}->{props} = $props;
469     $collectedfonts{$fontname}->{$foundry}->{file} = $file;
470     $collectedfonts{$fontname}->{$foundry}->{fonttype} = $fonttype;
471   }
472   close(FI);
473 }
474
475 for my $fontname (sort keys %collectedfonts) {
476   my @foundries = sort keys %{$collectedfonts{$fontname}};
477   my $printfoundries = 0;
478   if (defined($foundries[1])) {
479     $printfoundries = 1;
480   }
481   for my $foundry (@foundries) {
482     if (exists($options{PrintWarnings})) {
483       for my $err (@{$collectedfonts{$fontname}->{$foundry}->{errors}}) {
484         print "$err\n";
485       }
486     }
487     my $fn = "Font : $fontname";
488     if ($printfoundries && ($foundry ne "")) {
489       $fn .= " \[$foundry\]";
490     }
491     print $fn;
492     print $collectedfonts{$fontname}->{$foundry}->{props};
493     if (exists($options{PrintFiles})) {
494       print ": " . $collectedfonts{$fontname}->{$foundry}->{file} . "\n";
495     }
496     else {
497       print "\n";
498     }
499   }
500 }
501
502 exit(0);
503 #################################################################################
504 sub convertlang($)
505 {
506   my ($ilang) = @_;
507   if ($ilang =~ /^\s*([a-z]+)([\-_]([a-z]+))?\s*$/i) {
508     my ($x, $y) = ($1, $3);
509     if (defined($y)) {
510       $ilang = lc($x) . '-' . lc($y);
511     }
512     else {
513       $ilang = lc($x);
514     }
515   }
516   return($ilang);
517 }
518
519 sub extractlist($$$)
520 {
521   my ($l, $islang, $txt, $rres) = @_;
522   my @res = ();
523   if ($l =~ /$txt=\"([^\"]+)\"/) {
524     @res = split(',', $1);
525     if ($islang) {
526       for my $lg (@res) {
527         $lg = &convertlang($lg);
528       }
529     }
530   }
531   @{$rres} = @res;
532 }
533
534 sub getIndexes($$)
535 {
536   my ($lang, $rlangs) = @_;
537   my @res = ();
538
539   for (my $i = 0; defined($rlangs->[$i]); $i++) {
540     if ($rlangs->[$i] eq $lang) {
541       push(@res, $i);
542     }
543   }
544   return(\@res);
545 }
546
547 sub getVal($$$)
548 {
549   my ($l, $txtval, $txtlang) = @_;
550   my @values = ();
551   my @langs = ();
552   &extractlist($l, 0, $txtval, \@values);
553   return("") if (! defined($values[0]));
554   &extractlist($l, 1, $txtlang, \@langs);
555   my $i = &getIndexes("en", \@langs);
556   my $res = "";
557   for my $k (@{$i}) {
558     if (defined($values[$k]) && (length($values[$k]) > length($res))) {
559       $res = $values[$k];
560     }
561   }
562   return($values[0]) if ($res eq "");
563   return($res);
564 }
565
566 sub getsinglevalue($$$)
567 {
568   my ($l, $txt, $rMap, $rget) = @_;
569   my $val;
570   if ($l =~ / $txt=(\d+)/) {
571     $val = $1;
572     # Search for nearest value to $val
573     if (defined($rMap->{$val})) {
574       return($rMap->{$val});
575     }
576     my $maxv = -1;
577     my $minv = 1000;
578     for my $key (keys %{$rMap}) {
579       next if ($key !~ /^\d+$/);
580       my $diff = abs($key - $val);
581       if ($diff < $minv) {
582         $maxv = $key;
583         $minv = $diff;
584       }
585       elsif ($diff == $minv) {
586         if ($key < $maxv) {
587           $maxv = $key;
588         }
589       }
590     }
591     if (! defined($rMap->{$maxv})) {
592       print "ERROR2: txt=$txt, val=$val\n";
593       exit(-2);
594     }
595     if ($val > $maxv) {
596       return($rMap->{$maxv} . "+$minv");
597     }
598     else {
599       return($rMap->{$maxv} . "-$minv");
600     }
601   }
602   else {
603     return(undef);
604   }
605 }
606
607 sub addTxt($$)
608 {
609   my ($txt, $val) = @_;
610   return("$txt($val)");
611 }
612
613 sub getftype($$)
614 {
615   my ($fontname, $style) = @_;
616   if ($fontname =~ /(sans)[-_ ]?(serif)?/i) {
617     return($ftypes{100}); # Sans Serif
618   }
619   elsif ($fontname =~ /gothic|dotum|gulim/i) {
620     if ($fontname =~ /bisrat gothic/i) {
621       return($ftypes{0});    # Serif
622     }
623     else {
624       return($ftypes{100}); # Sans Serif
625     }
626   }
627   elsif ($fontname =~ /serif|times|mincho|batang/i) {
628     if ($fontname =~ /good times/i) {
629       return($ftypes{100}); # Sans Serif
630     }
631     elsif ($fontname !~ /initials/i) {
632       return($ftypes{0});    # Serif
633     }
634   }
635   # Now check for fonts without a hint in font name
636   if ($fontname =~ /([a-z])/i) {
637     my $key = lc($1);
638     for my $rFonts (\%sansFonts, \%scriptFonts, \%fraktFonts, \%fancyFonts, \%initialFonts, \%symbolFonts) {
639       if (defined($rFonts->{$key})) {
640         if ($fontname =~ $rFonts->{$key}) {
641           return($ftypes{$rFonts->{"value"}});
642         }
643       }
644     }
645   }
646   if ("$fontname" =~ /^bpg/i) {
647     if ("$fontname" =~ /bpg (courier gpl|elite)/i) {
648       return($ftypes{0});    # Serif
649     }
650     else {
651       return($ftypes{100}); # Sans Serif
652     }
653   }
654   elsif ("$fontname" =~ /^dustismo/i) {
655     if ("$fontname" =~ /^dustismo roman/i) {
656       return($ftypes{0});    # Serif
657     }
658     else {
659       return($ftypes{100}); # Sans Serif
660     }
661   }
662   elsif ("$fontname" =~ /^go\b/i) {
663     if ("$fontname" =~ /^go mono/i) {
664       return($ftypes{0});    # Serif
665     }
666     else {
667       return($ftypes{100}); # Sans Serif
668     }
669   }
670   else {
671     return(undef);
672   }
673 }
674
675 sub getweight($$)
676 {
677   my ($fontname, $style) = @_;
678   my $result = undef;
679   for my $key (keys %weights) {
680     next if ($key !~ /^\d+$/);
681     my $val = $weights{$key};
682     for my $info ($style, $fontname) {
683       if ($info =~ /\b$val\b/i) {
684         if ($val eq "Regular") {
685           $result = $val;    # It may refer to width
686         }
687         else {
688           return($val);
689         }
690       }
691     }
692   }
693   return($result);
694 }
695
696 sub getwidth($$)
697 {
698   my ($fontname, $style) = @_;
699   my $result = undef;
700   for my $key (keys %widths) {
701     next if ($key !~ /^\d+$/);
702     for my $info ($style, $fontname) {
703       if ($info =~ /\b$widths{$key}\b/i) {
704         return($widths{$key});
705       }
706       if ($info =~ /\bRegular\b/) {
707         if (!defined($result)) {
708           $result = $widths{100};
709         }
710       }
711     }
712   }
713   return($result);
714 }
715
716 sub getslant($$)
717 {
718   my ($fontname, $style) = @_;
719   for my $key (keys %slants) {
720     next if ($key !~ /^\d+$/);
721     if ($style =~ /\b$slants{$key}\b/i) {
722       return($slants{$key});
723     }
724   }
725   return(undef);
726 }
727
728 sub getspacing($$)
729 {
730   my ($fontname, $style) = @_;
731   for my $key (keys %spacings) {
732     next if ($key !~ /^\d+$/);
733     if ($style =~ /\b$spacings{$key}\b/i) {
734       return($spacings{$key});
735     }
736   }
737   if ("$fontname $style" =~ /(mono|typewriter|cursor|fixed)\b/i) {
738     return($spacings{100}); # Mono
739   }
740   else {
741     return(undef);
742   }
743 }
744
745 sub ismathfont($$)
746 {
747   my ($fontname, $rCapability) = @_;
748
749   return 1 if ($fontname =~ /math/i);
750   for my $cap (@{$rCapability}) {
751     return 1 if ($cap eq "math");
752   }
753   return 0;
754 }
755
756 sub getproperties($$$$)
757 {
758   my ($l, $fontname, $style, $rerrors) = @_;
759   my $newstyle = &correctstyle($style);
760   my $newfam = &correctstyle($fontname);
761   my @properties = ();
762
763   for my $txt (qw(ftype weight width slant spacing)) {
764     my ($map, $rget);
765     eval("\$map = " . '\%' . $txt . 's');
766     eval('$rget = \&' . "get$txt");
767     my $val2 = getsinglevalue($l, $txt, $map);
768     my $val1 = $rget->($newfam, $newstyle);
769     my $val;
770     if (defined($val2) && defined($val1) && ($val2 ne $val1)) {
771       push(@{$rerrors}, "Fontname($fontname),Style($style): Values for $txt ($val1 != $val2) differ, selecting internal $txt($val2)");
772       $val = $val2;
773     }
774     elsif (! defined($val2)) {
775       $val = $val1;
776     }
777     else {
778       $val = $val2;
779     }
780     if (defined($val)) {
781       push(@properties, &addTxt($txt,$val));
782     }
783     else {
784       if (defined($map->{"default"})) {
785         push(@properties, &addTxt($txt,$map->{"default"}));
786       }
787       else {
788         push(@{$rerrors}, "Undefined value for $txt");
789       }
790     }
791   }
792   return(join(' ', @properties));
793 }
794
795 sub correctstyle($)
796 {
797   my ($style) = @_;
798   $style =~ s/^\\040//;
799   $style =~ s/^\s*\d+\s*//;
800   $style =~ s/\s*\d+$//;
801   $style =~ s/italic/ Italic/i;
802   $style =~ s/oblique/ Oblique/i;
803   $style =~ s/[\-_]/ /g;
804   $style =~ s/\breg\b/Regular/i;
805   $style =~ s/\bregita(lic)?\b/Regular Italic/i;
806   $style =~ s/\bregobl(ique)?\b/Regular Oblique/i;
807   $style =~ s/medium/Medium /i;
808   $style =~ s/\bmedita(lic)?\b/Medium Italic/i;
809   $style =~ s/\bmedobl(ique)?\b/Medium Oblique/i;
810   $style =~ s/\bmed\b/Medium /i;
811   $style =~ s/\bdemi\b/SemiBold/i;
812   $style =~ s/\bex(pd|t)\b/Expanded/i;
813   $style =~ s/semi ?cond(ensed)?/SemiCondensed/i;
814   $style =~ s/[sd]emi ?(bold|bd|bol)/SemiBold/i;
815   $style =~ s/semi ?(expanded|extended|expd)/SemiExpanded/i;
816   $style =~ s/[sd]emi ?light/SemiLight/i;
817   $style =~ s/ultra ?(expanded|extended|expd)/UltraExpanded/i;
818   $style =~ s/light/Light /i;
819   $style =~ s/\blt\b/Light /i;
820   $style =~ s/(ultra|extra)(light|lt)/ExtraLight /i;
821   $style =~ s/\bheavy\b/Extrabold/i;
822   $style =~ s/\bhairline\b/Extralight/i;
823   $style =~ s/\bcond\b/Condensed/i;
824   $style =~ s/(roman)?slanted/ Italic/i;
825   $style =~ s/\bslant\b/Italic/i;
826   $style =~ s/\b(SC|Small(caps(alt)?)?)\b/SmallCaps/i;
827   $style =~ s/w3 mono/Dual/i;
828   $style =~ s/Regul[ea]r/Regular/i;
829   $style =~ s/  +/ /g;
830   return($style);
831 }
832
833 sub decimalUnicode($)
834 {
835   my ($a) = @_;
836   if ($a =~ /^u\+(.+)$/i) {
837     $a = $1;
838     if ($a =~ /^0?x(.+)$/) {
839       $a = hex($1);
840     }
841     return($a);
842   }
843   return(ord(decode('utf-8', $a)));
844 }
845
846 sub contains($$)
847 {
848   my ($d, $rList) = @_;
849   for my $re (@{$rList}) {
850     return 0 if ($re->[0] >  $d);
851     return 1 if ($re->[1] >= $d);
852   }
853   return 0;
854 }