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