]> git.lyx.org Git - lyx.git/blob - development/tools/listFontWithLang.pl
e365dc49f0b1113e7f78ca382f25bddae747238c
[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 use constant {
38   SERIF => 1,
39   SANS => 2,
40   SCRIPT => 4,
41   FRAKTUR => 8,
42   DOUBLESTROKE => 16,
43   FANCY => 32,
44   INITIALS => 64,
45   SYMBOL => 128,
46 };
47
48 sub convertlang($);
49 sub extractlist($$$);   # my ($l, $islang, $txt, $rres) = @_;
50 sub getIndexes($$);
51 sub getVal($$$$);       # my ($l, $txtval, $txtlang, $combine) = @_;
52 sub getproperties($$$$);
53 sub ismathfont($$);
54 sub correctstyle($);
55 sub decimalUnicode($);
56 sub contains($$);
57 sub sprintIntervalls($);
58 sub buildFontName($$$$);
59
60 # Following fields for a parameter can be defined:
61 # fieldname:         Name of entry in %options
62 # type:              [:=][sif], ':' = optional, '=' = required, 's' = string, 'i' = integer, 'f' = float
63 # alias:             reference to a list of aliases e.g. ["alias1", "alias2", ... ]
64 # listsep:           Separator for multiple data
65 # comment:           Parameter description
66 my @optionsDef = (
67   # help + verbose already handled in 'GetOptions'
68   ["n",
69    {fieldname => "FontName", listsep => ',',
70     type => "=s", alias => ["name"],
71     comment => "Select font-names matching these (comma separated) regexes"},],
72   ["nn",
73    {fieldname => "NFontName",
74     type => "=s", listsep => ',',
75     comment => "Select font-names NOT matching these (comma separated) regexes"},],
76   ["p",
77    {fieldname => "Property",
78     type => "=s", listsep => ',',
79     comment => "Select fonts with properties matching these (comma separated) regexes"},],
80   ["np",
81    {fieldname => "NProperty",
82     type => "=s", listsep => ',',
83     comment => "Select fonts with properties NOT matching these (comma separated) regexes"},],
84   ["s",
85    {fieldname => "Scripts",
86     type => "=s", listsep => ',',
87     comment => "Select fonts with scripts matching these (comma separated) regexes"},],
88   ["ns",
89    {fieldname => "NScripts",
90     type => "=s", listsep => ',',
91     comment => "Select fonts with scripts NOT matching these (comma separated) regexes"},],
92   ["math",
93    {fieldname => "Math",
94     comment => "Select fonts probably containing math glyphs"},],
95   ["c",
96    {fieldname => "Contains",  alias => ["contains"],
97     type => "=s", listsep => ',',
98     comment => "Select fonts containing all these (possibly comma separated) glyphs",
99     comment2 => "____example: -c=\"0-9,u+32-u+x7f\"",}],
100   ["nc",
101    {fieldname => "NContains",
102     type => "=s", listsep => ',',
103     comment => "Select fonts NOT containing any of these (possibly comma separated) glyphs",
104     comment2 => "____example: --nc=\"0-9,u+32-u+x7f\"",}],
105   ["l",
106    {fieldname => "Lang",
107     type => "=s", alias=>["lang"],
108     comment => "Comma separated list of desired languages"},],
109   ["pc",
110    {fieldname => "PrintCharset", alias => ["printcharset"],
111     comment => "Print intervals of supported unicode character values"},],
112   ["pl",
113    {fieldname => "PrintLangs", alias => ["printlangs"],
114     comment => "Print supported languages"},],
115   ["pp",
116    {fieldname => "PrintProperties", alias => ["printproperties"],
117     comment => "Print properties from weight, slant and width"},],
118   ["ps",
119    {fieldname => "PrintScripts", alias => ["printscripts"],
120     comment => "Print supported scripts"},],
121   ["pf",
122    {fieldname => "PrintFiles", alias => ["printfiles"],
123     comment => "Print font file names"},],
124   ["pw",
125    {fieldname => "PrintWarnings",
126     comment => "Print warnings about discarded/overwritten fonts, conflicting styles"},],
127 );
128 my %options = %{&handleOptions(\@optionsDef)};
129
130 $options{Lang} = "" if (! defined($options{Lang}));
131
132 #############################################################
133
134 my @langs = split(',', $options{Lang});
135 for my $lg (@langs) {
136   $lg = &convertlang($lg);
137 }
138
139 for my $charFld ("Contains", "NContains") {
140   if (defined($options{$charFld})) {
141     my %glyphs = ();         # To ignore duplicates
142     for my $a1 (@{$options{$charFld}}) {
143       for my $e (decimalUnicode($a1)) {
144         $glyphs{$e} = 1;
145       }
146     }
147     # create intervalls
148     my @glyphs = sort {$a <=> $b;} keys %glyphs;
149
150     # $options{$charFld} no longer needed, so use it for unicode-point intervalls
151     $options{$charFld} = [];
152     my ($first, $last) = (undef, undef);
153     for my $i (@glyphs) {
154       if (! defined($last)) {
155         $first = $i;
156         $last = $i;
157         next;
158       }
159       if ($i == $last+1) {
160         $last = $i;
161         next;
162       }
163       push(@{$options{$charFld}}, [$first, $last]);
164       $first = $i;
165       $last = $i;
166     }
167     if (defined($last)) {
168       push(@{$options{$charFld}}, [$first, $last]);
169     }
170     if (exists($options{verbose})) {
171       if ($charFld eq "Contains") {
172         print "Checking for unicode-points: " . &sprintIntervalls($options{$charFld}) . "\n";
173       }
174       else {
175         print "Ignore if matching unicode-points: " . &sprintIntervalls($options{$charFld}) . "\n";
176       }
177     }
178   }
179 }
180
181 my $cmd = "fc-list";
182 if (defined($langs[0])) {
183   $cmd .= " :lang=" . join(',', @langs);
184 }
185
186 my $format = "foundry=\"%{foundry}\"" .
187     " postscriptname=\"%{postscriptname}\"" .
188     " fn=\"%{fullname}\" fnl=\"%{fullnamelang}\"" .
189     " family=\"%{family}\" flang=\"%{familylang}\" " .
190     " style=\"%{style}\" stylelang=\"%{stylelang}\"";
191
192 if (exists($options{PrintScripts}) || defined($options{Scripts}) || defined($options{NScripts}) || exists($options{Math})) {
193   $format .= " script=\"%{capability}\"";
194 }
195 if (exists($options{PrintLangs}) || defined($langs[0])) {
196   $format .= " lang=\"%{lang}\"";
197 }
198 if (exists($options{PrintProperties}) || defined($options{Property}) || defined($options{NProperty})) {
199   $format .= " weight=%{weight} slant=%{slant} width=%{width} spacing=%{spacing}";
200 }
201 if (defined($options{Contains}) || defined($options{NContains}) || exists($options{PrintCharset})) {
202   $format .= " charset=\"%{charset}\"";
203 }
204 $format .= " file=\"%{file}\" abcd\\n";
205 $cmd .= " -f '$format'";
206 #print "$cmd\n";
207
208
209 my %ftypes = (
210   # Dummy internal map
211   # using '()' to prevent the initializer to take
212   #    the key as a string. (Constants in perl _are_ functions without argument)
213   SERIF() => "Serif",
214   SANS() => "Sans",
215   SCRIPT() => "Script",
216   FRAKTUR() => "Fraktur",
217   DOUBLESTROKE() => "DoubleStroke",
218   FANCY() => "Fancy",
219   INITIALS() => "Initials",
220   SYMBOL() => "Symbol",
221   "default" => 1,
222 );
223
224 my %weights = (
225   0 => "Thin",
226   40 => "Extralight",
227   50 => "Light",
228   55 => "Semilight",
229   75 => "Book",
230   80 => "Regular",
231   100 => "Medium",
232   180 => "Semibold",
233   200 => "Bold",
234   205 => "Extrabold",
235   210 => "Black",
236   215 => "ExtraBlack",
237 );
238
239 my %slants = (
240   0 => "Roman",
241   100 => "Italic",
242   110 => "Oblique",
243 );
244
245 my %widths = (
246   50 => "Ultracondensed",
247   63 => "Extracondensed",
248   75 => "Condensed",
249   87 => "Semicondensed",
250   100 => "Normal",
251   113 => "Semiexpanded",
252   125 => "Expanded",
253   150 => "Extraexpanded",
254   200 => "Ultraexpanded",
255 );
256
257 my %spacings = (
258   0 => "Proportional",
259   90 => "Dual",
260   100 => "Mono",
261   110 => "Charcell",
262   "default" => "Proportional",
263 );
264
265 # Build reverse mappings, (not needed yet)
266 for my $txt (qw(ftypes weights slants widths spacings)) {
267   my $map;
268   eval "\$map = \\%$txt";
269   for my $key (keys %{$map}) {
270     next if ($key !~ /^\d+$/);
271     my $value = lc($map->{$key});
272     $map->{"r"}->{$value} = $key;
273   }
274 }
275
276 # key:= fontname
277 #     subkey foundry
278 #            subfoundry
279 my %collectedfonts = ();
280 my %fontpriority = (
281   otf => 0,                # type 2, opentype CFF (Compact Font Format)
282   ttc => 1.1,              # type 1 (True Type font Collection)
283   ttf => 1.2,              # type 1 (True Type Font)
284   woff=> 1.3,              # type 1 (Web Open Font Format)
285   t1  => 1.4,              # type 1 (postscript)
286   pfb => 1.5,              # type 1 (Printer Font Binary)
287   pfa => 1.6,              # type 1 (Printer Font Ascii)
288   pfm => 2,                # requires associated .pfb file
289   pcf => 5,                # Bitmap (Packaged Collaboration Files)?
290 );
291 my $nexttype = 6;
292
293 my %serifFonts = (
294   # some extra serif fonts
295   "value" => SERIF,
296   "g" => qr/^gfs ?(didot)/i,
297 );
298 # list of regexes for known sans serif fonts
299 my %sansFonts = (
300   "value" => SANS,          # Sans serif
301   "a" => qr/^a(030|bydos|haroni|e?rial|ndika|ngostura|nonymous|rab|roania|rimo|sap|e almothnna|egean|egyptus|l (arabiya|battar|hor|manzomah|yarmook)|lmonte|natolian|ndale|nglepoise|njali|xaxa)/i,
302   "b" => qr/^b(abel ?stone ?modern|aekmuk|alker|altar|andal|angwool|arbatrick|aveuse|dxsfm|ebas|erenika|eteckna|euron|iometric|iting|lue|m ?hanna)/i,
303   "c" => qr/^c(abin|aliban|antarell|arbon|arlito|handas|hivo|mu bright|omfortaa|omi[cx]|oolvetica|ortoba|ousine|uprum|wtex(hei|yen)|yklop|ypro)/i,
304   "d" => qr/^(d2coding|dimnah|dosis|dyuthi)/i,
305   "e" => qr/^(electron|engebrechtre)/i,
306   "f" => qr/^(fandolhei|fetamont|fira|font awesome 5|forgotten)/i,
307   "g" => qr/^(gardiner|garuda|gfs ?neo|gillius|granada|graph|guanine|gunplay)/i,
308   "h" => qr/^(hack|hani|haramain|harano|harmattan|hor\b)/i,
309   "i" => qr/^(ibm ?(plex ?mono|3270)|ikarius|inconsolata|induni.?h|iwona)/i,
310   "j" => qr/^(jara|jura)/i,
311   "k" => qr/^(kalimati|kanji|karla|karma|kayrawan|kenyan|keraleeyam|khalid|khmer [or]|kiloji|klaudia|ko[mn]atu|kurier|kustom)/i,
312   "l" => qr/^l(aksaman|arabie|ato|eague|exend|exigulim|ibel|iberation|ibre franklin|ibris|inux biolinum|obster|ogix|ohit|oma)/i,
313   "m" => qr/^m(\+ |anchu|anjari|arcellus|ashq|eera|etal|igmix|igu|ikachan|intspirit|iriam ?clm|ona|onlam|ono(fonto|id|isome|noki)|ontserrat|otoyal|ukti|usica)/i,
314   "n" => qr/^(nachlieli|nada|nafees|nagham|nanum(barunpen|square)|nice)/i,
315   "o" => qr/^(ocr|okolaks|opendyslexic|ostorah|ouhud|over|oxygen)/i,
316   "p" => qr/^(padauk|pagul|paktype|pakenham|palladio|petra|phetsarath|play\b|poiret|port\b|primer\b|prociono|pt\b|purisa)/i,
317   "q" => qr/^(qt(ancient|helvet|avanti|doghaus|eratype|eurotype|floraline|frank|fritz|future|greece|howard|letter|optimum)|quercus)/i,
318   "r" => qr/^(rachana|radio\b|raleway|ricty|roboto|rosario)/i,
319   "s" => qr/^(salem|samanata|sawasdee|shado|sharja|simple|sophia|soul|source|switzera)/i,
320   "t" => qr/^(tarablus|teen|texgyre(adventor|heros)|tiresias|trebuchet|tscu|tuffy)/i,
321   "u" => qr/^u(buntu|kij (bom|chechek|cjk|diwani|ekran|elipbe|inchike|jelliy|kufi|mejnuntal|qara|qolyazma|teng|title|tor|tuz ?(neqish|tom))|mpush|n ?(dinaru|jamo|graphic|taza|vada|yetgul)|uni(kurd|space|versalis)|roob|rw ?classico)/i,
322   "v" => qr/^(veranda|vn ?urwclassico)/i,
323   "w" => qr/^(waree)/i,
324   "y" => qr/^(yanone)/i,
325   "z" => qr/^(zekton|zero)/i,
326 );
327 my %scriptFonts = (
328   "value" => SCRIPT,          # Script
329   "a" => qr/^a(becedario|ir ?cut|ugie|uriocus ?kalligraph)/i,
330   "b" => qr/^b(reip|rush ?script)/i,
331   "c" => qr/^(chancery)/i,
332   "d" => qr/^(dancing)/i,
333   "e" => qr/^(elegante)/i,
334   "f" => qr/^femkeklaver/i,
335   "j" => qr/^jsmath.?(rsfs)/i,
336   "k" => qr/^(kaushan|karumbi|kristi)/i,
337   "m" => qr/^(mathjax_script|miama)/i,
338   "n" => qr/^(nanum (brush|pen) script)/i,
339   "p" => qr/^pecita/i,
340   "q" => qr/^qt(arabian|boulevard|brush ?stroke|chancery|coronation|florencia|handwriting|linostroke|merry|pandora|slogan)/i,
341   "r" => qr/^((romande.*|ruf)script|rsfs)/i,
342   "t" => qr/^typo ?script/i,
343   "u" => qr/^u(n ?pilgi|rw ?chancery|kij ?(jelliy|moy|qolyazma ?(tez|yantu)))/i,
344 );
345
346 my %fraktFonts = (
347   "value" => FRAKTUR,          # Fraktur
348   "b" => qr/^boondox ?fraktur/i,
349   "e" => qr/^eufm/i,
350   "j" => qr/^(jsmath.?euf)/i,
351   "m" => qr/^(missaali)/i,
352   "o" => qr/^(oldania)/i,
353   "q" => qr/^qt(blackforest|cloisteredmonk|dublinirish|fraktur|heidelbergtype|(lino|london)scroll)/i,
354   "u" => qr/^ukij ?(kufi ?tar|mejnun ?reg)/i,
355 );
356
357 my %fancyFonts = (
358   "value" => FANCY,          # Fancy
359   "a" => qr/^a(bandoned|bberancy|driator|irmole|lmonte (snow|woodgrain)|nalecta|ni|nklepants|nn ?stone|oyagi|rt ?nouveau ?caps|stron|xaxa)/i,
360   "b" => qr/^b(aileys|alcony|altar|andal|arbatrick|aveuse|eat ?my|etsy|iometric|iting|lankenburg|oondox ?callig|org|oron|raeside|ramalea|udmo|urnstown|utterbelly)/i,
361   "c" => qr/^c(retino|msy)/i,
362   "d" => qr/^dseg/i,
363   "e" => qr/^electorate/i,
364   "f" => qr/^frederika/i,
365   "g" => qr/^(gfs.?theo)/i,
366   "j" => qr/^jsmath.cmsy/i,
367   "k" => qr/^keter|kicking|kredit|kouzan|kerkis calligraphic/i,
368   "l" => qr/^lcmsy/i,
369   "u" => qr/^u(kij ?(saet|tiken)|nion ?city)/i,
370   "v" => qr/^vectroid/i,
371 );
372
373 my %initialFonts = (
374   "value" => INITIALS,          # Initials
375   "e" => qr/^(eb.?garamond.?init)/i,
376   "t" => qr/^typographer/i,
377   "y" => qr/^(yinit)/i,
378 );
379
380 my %symbolFonts = (
381   "value" => SYMBOL,          # Symbol
382   "a" => qr/^a(cademicons|lblant|lianna|mar|nka|rb?\d|rchaic|rrow|rs|rt[mt]|ssy(rb\d+)?\b|miri ?quran|mit\b)/i,
383   "b" => qr/^b(aby ?jeepers|bding|euron|guq|lex|lsy|oondox ?upr|ullets|urma)/i,
384   "c" => qr/^(caladings|ccicons|chess|cmsy|cmex)/i,
385   "d" => qr/^(dingbats|drmsym|d05)/i,
386   "e" => qr/^e(lusiveicons|mmentaler|moji|sint|uterpe)/i,
387   "f" => qr/^(fandol.?brail|fdsymbol|fourierorns|font(awesome|ello|.?mfizz))/i,
388   "g" => qr/^(gan.?clm|gfs.?(baskerville|gazis|olga|porson|solomos|(bodoni|didot).?classic|complutum))/i,
389   "h" => qr/^(hots)/i,
390   "j" => qr/^jsmath.?(msam|cmsy|masm|msbm|wasy|cmex|stmary)/i,
391   "l" => qr/^l(cmsy|msam)/i,
392   "m" => qr/^(marvosym|material|msam|msbm)/i,
393   "n" => qr/^(noto.*(emoji|brahmi))/i,
394   "o" => qr/^(octicons)/i,
395   "p" => qr/^patch/i,
396   "q" => qr/^(qtding ?bits)/i,
397   "s" => qr/^s(kak|tmary|s?msam)/i,
398   "t" => qr/^(typicons|twemoji)/i,
399   "u" => qr/^ukij ?(imaret|orxun|tughra)/i,
400   "w" => qr/^w(ebdings|asy|elfare ?brat)/i,
401 );
402
403 if (open(FI,  "$cmd |")) {
404  NXTLINE: while (my $l = <FI>) {
405     chomp($l);
406     while ($l !~ /abcd$/) {
407       $l .= <FI>;
408       chomp($l);
409     }
410     my $file = "";
411     my $fonttype;
412     if ($l =~ /file=\"([^\"]+)\"/) {
413       $file = $1;
414       #next if ($file !~ /\.(otf|ttf|pfa|pfb|pcf|ttc)$/i);
415       if ($file !~ /\.([a-z0-9]{2,5})$/i) {
416         print "Unhandled extension for file $file\n";
417         next;
418       }
419       $fonttype = lc($1);
420       if (! defined($fontpriority{$fonttype})) {
421         print "Added extension $fonttype for file $file\n";
422         $fontpriority{$fonttype} = $nexttype;
423         $nexttype++;
424       }
425     }
426     my %usedlangs = ();
427     if ($l =~ / lang=\"([^\"]+)\"/) {
428       my @ll = split(/\|/, $1);
429       for my $lx (@ll) {
430         $usedlangs{&convertlang($lx)} = 1;
431       }
432     }
433
434     for my $lang (@langs) {
435       next NXTLINE if (! defined($usedlangs{$lang}));
436     }
437     my $style = &getVal($l, "style", "stylelang", 1);
438     $style =~ s/^\\040//;
439     my $fullname = &getVal($l, "fn", "fnl");
440     my $postscriptname = "";
441     if ($l =~ /postscriptname=\"([^\"]+)\"/) {
442       $postscriptname = $1;
443     }
444     my $family = &getVal($l, "family", "flang", 0);
445     my $fontname = &buildFontName($family, $style, $fullname, $postscriptname);
446
447     if (defined($options{NFontName})) {
448       for my $fn (@{$options{NFontName}}) {
449         next NXTLINE if ($fontname =~ /$fn/i);
450       }
451     }
452     if (defined($options{FontName})) {
453       for my $fn (@{$options{FontName}}) {
454         next NXTLINE if ($fontname !~ /$fn/i);
455       }
456     }
457     my @charlist = ();
458     if (defined($options{Contains}) || defined($options{NContains}) || exists($options{PrintCharset})) {
459       if ($l =~ / charset=\"([^\"]+)\"/) {
460         my @list = split(/\s+/, $1);
461         for my $e (@list) {
462           my ($l, $h) = split('-', $e);
463           $h = $l if (! defined($h));
464           push(@charlist, [hex($l), hex($h)]);
465         }
466       }
467       if (defined($options{Contains})) {
468         for my $g (@{$options{Contains}}) {
469           next NXTLINE if (! contains($g, \@charlist));
470         }
471       }
472       if (defined($options{NContains})) {
473         for my $g (@{$options{NContains}}) {
474           # Ignore if ANY char exist in @charlist
475           for (my $i = $g->[0]; $i <= $g->[1]; $i++) {
476             next NXTLINE if (contains([$i,$i], \@charlist));
477           }
478         }
479       }
480     }
481     my $props = "";
482     my @errors = ();
483     if (exists($options{PrintProperties}) || defined($options{Property}) || defined($options{NProperty})) {
484       my $properties = getproperties($l, $fontname, $style, \@errors);
485       if (defined($options{Property})) {
486         for my $pn (@{$options{Property}}) {
487           next NXTLINE if ($properties !~ /$pn/i);
488         }
489       }
490       if (defined($options{NProperty})) {
491         for my $pn (@{$options{NProperty}}) {
492           next NXTLINE if ($properties =~ /$pn/i);
493         }
494       }
495       if (exists($options{PrintProperties})) {
496         $props .= " ($properties)";
497       }
498     }
499
500     if (exists($options{PrintLangs})) {
501       $props .= '(' . join(',', sort keys %usedlangs) . ')';
502     }
503     if (exists($options{PrintCharset})) {
504       $props .= '(' . &sprintIntervalls(\@charlist) . ')';
505     }
506     if (exists($options{PrintScripts}) || defined($options{Scripts}) || defined($options{NScripts}) || exists($options{Math})) {
507       my @scripts = ();
508       my $scripts = "";
509       if ($l =~ / script=\"([^\"]+)\"/) {
510         @scripts = split(/\s+/, $1);
511         for my $ent (@scripts) {
512           $ent =~ s/^\s*otlayout://;
513           $ent = lc($ent);
514         }
515         $scripts = join(',', @scripts);
516       }
517       if (exists($options{Math})) {
518         next NXTLINE if (! &ismathfont($fontname,\@scripts));
519       }
520       if (exists($options{PrintScripts})) {
521         $props .= "($scripts)";
522       }
523       if (!defined($scripts[0])) {
524         # No script defined in font, so check only $options{Scripts}
525         next NXTLINE if (defined($options{Scripts}));
526       }
527       else {
528         if (defined($options{Scripts})) {
529           for my $s (@{$options{Scripts}}) {
530             next NXTLINE if ($scripts !~ /$s/i);
531           }
532         }
533         if (defined($options{NScripts})) {
534           for my $s (@{$options{NScripts}}) {
535             next NXTLINE if ($scripts =~ /$s/i);
536           }
537         }
538       }
539     }
540     my $foundry = "";
541     if ($l =~ /foundry=\"([^\"]+)\"/) {
542       $foundry = $1;
543       $foundry =~ s/^\s+//;
544       $foundry =~ s/\s+$//;
545     }
546     if (defined($collectedfonts{$fontname}->{$foundry}->{errors})) {
547       # Apparently not the first one, so add some info
548       my $oldfonttype = $collectedfonts{$fontname}->{$foundry}->{fonttype};
549       if (defined($errors[0])) {
550         push(@{$collectedfonts{$fontname}->{$foundry}->{errors}}, @errors);
551       }
552       if ($fontpriority{$oldfonttype} > $fontpriority{$fonttype}) {
553         push(@{$collectedfonts{$fontname}->{$foundry}->{errors}}, "Warning: overwriting old info for file: " . $collectedfonts{$fontname}->{$foundry}->{file});
554       }
555       else {
556         push(@{$collectedfonts{$fontname}->{$foundry}->{errors}}, "Warning: discarding new info for file: $file");
557         next;
558       }
559     }
560     else {
561       $collectedfonts{$fontname}->{$foundry}->{errors} = \@errors;
562     }
563     $collectedfonts{$fontname}->{$foundry}->{props} = $props;
564     $collectedfonts{$fontname}->{$foundry}->{file} = $file;
565     $collectedfonts{$fontname}->{$foundry}->{fonttype} = $fonttype;
566   }
567   close(FI);
568 }
569
570 for my $fontname (sort keys %collectedfonts) {
571   my @foundries = sort keys %{$collectedfonts{$fontname}};
572   my $printfoundries = 0;
573   if (defined($foundries[1])) {
574     $printfoundries = 1;
575   }
576   for my $foundry (@foundries) {
577     if (exists($options{PrintWarnings})) {
578       for my $err (@{$collectedfonts{$fontname}->{$foundry}->{errors}}) {
579         print "$err\n";
580       }
581     }
582     my $fn = "Font : $fontname";
583     if ($printfoundries && ($foundry ne "")) {
584       $fn .= " \[$foundry\]";
585     }
586     print $fn;
587     print $collectedfonts{$fontname}->{$foundry}->{props};
588     if (exists($options{PrintFiles})) {
589       print ": " . $collectedfonts{$fontname}->{$foundry}->{file} . "\n";
590     }
591     else {
592       print "\n";
593     }
594   }
595 }
596
597 exit(0);
598 #################################################################################
599 sub convertlang($)
600 {
601   my ($ilang) = @_;
602   if ($ilang =~ /^\s*([a-z]+)([\-_]([a-z]+))?\s*$/i) {
603     my ($x, $y) = ($1, $3);
604     if (defined($y)) {
605       $ilang = lc($x) . '-' . lc($y);
606     }
607     else {
608       $ilang = lc($x);
609     }
610   }
611   return($ilang);
612 }
613
614 sub extractlist($$$)
615 {
616   my ($l, $islang, $txt, $rres) = @_;
617   my @res = ();
618   if ($l =~ /$txt=\"([^\"]+)\"/) {
619     @res = split(',', $1);
620     if ($islang) {
621       for my $lg (@res) {
622         $lg = &convertlang($lg);
623       }
624     }
625   }
626   @{$rres} = @res;
627 }
628
629 sub getIndexes($$)
630 {
631   my ($lang, $rlangs) = @_;
632   my @res = ();
633
634   for (my $i = 0; defined($rlangs->[$i]); $i++) {
635     if ($rlangs->[$i] eq $lang) {
636       push(@res, $i);
637     }
638   }
639   return(\@res);
640 }
641
642 sub getVal($$$$)
643 {
644   my ($l, $txtval, $txtlang, $combine) = @_;
645   my @values = ();
646   my @langs = ();
647   &extractlist($l, 0, $txtval, \@values);
648   return("") if (! defined($values[0]));
649   &extractlist($l, 1, $txtlang, \@langs);
650   my $i = &getIndexes("en", \@langs);
651   my $res = "";
652   for my $k (@{$i}) {
653     if (defined($values[$k])) {
654       if ($combine) {
655         if ($res ne "") {
656           $res .= " $values[$k]";
657         }
658         else {
659           $res = $values[$k];
660         }
661       }
662       else {
663         if (length($values[$k]) > length($res)) {
664           $res = $values[$k];
665         }
666       }
667     }
668   }
669   return($values[0]) if ($res eq "");
670   return($res);
671 }
672
673 sub getsinglevalue($$$)
674 {
675   my ($l, $txt, $rMap, $rget) = @_;
676   my $val;
677   if ($l =~ / $txt=(\d+)/) {
678     $val = $1;
679     # Search for nearest value to $val
680     if (defined($rMap->{$val})) {
681       return($rMap->{$val});
682     }
683     my $maxv = -1;
684     my $minv = 1000;
685     for my $key (keys %{$rMap}) {
686       next if ($key !~ /^\d+$/);
687       my $diff = abs($key - $val);
688       if ($diff < $minv) {
689         $maxv = $key;
690         $minv = $diff;
691       }
692       elsif ($diff == $minv) {
693         if ($key < $maxv) {
694           $maxv = $key;
695         }
696       }
697     }
698     if (! defined($rMap->{$maxv})) {
699       print "ERROR2: txt=$txt, val=$val\n";
700       exit(-2);
701     }
702     if ($val > $maxv) {
703       return($rMap->{$maxv} . "+$minv");
704     }
705     else {
706       return($rMap->{$maxv} . "-$minv");
707     }
708   }
709   else {
710     return(undef);
711   }
712 }
713
714 sub addTxt($$)
715 {
716   my ($txt, $val) = @_;
717   return("$txt($val)");
718 }
719
720 sub getftype($$)
721 {
722   my ($fontname, $style) = @_;
723   my $resftype = 0;
724   if ($fontname =~ /(sans)[-_ ]?(serif)?/i) {
725     $resftype |= SANS;
726   }
727   elsif ($fontname =~ /gothic|dotum|gulim/i) {
728     if ($fontname =~ /bisrat gothic/i) {
729       $resftype |= SERIF;
730     }
731     else {
732       $resftype |= SANS;
733     }
734   }
735   elsif ($fontname =~ /^(jsmath.?)?bbold|msbm|^(ds(rom|serif|ss))|DoubleStruck/i) {
736     $resftype |= DOUBLESTROKE;  # Double stroke (math font)
737   }
738   if ($fontname =~ /serif|times|mincho|batang/i) {
739     if ($fontname =~ /good times/i) {
740       $resftype |= SERIF; # Serif
741     }
742   }
743   if ($fontname =~ /initial(s|en)/i) {
744     $resftype |= INITIALS;
745   }
746   if ($fontname =~ /participants/i) {
747     $resftype |= SANS|FANCY;
748   }
749   if ($fontname =~ /symbol/i) {
750     if ($fontname !~ /^symbola/i) {
751       $resftype |= SYMBOL;
752     }
753   }
754   # Now check for fonts without a hint in font name
755   if ($fontname =~ /^([a-z])/i) {
756     my $key = lc($1);
757     # check the mutual exclusive first
758     for my $rFonts (\%fraktFonts, \%scriptFonts, \%sansFonts, \%serifFonts) {
759       if (defined($rFonts->{$key})) {
760         if ($fontname =~ $rFonts->{$key}) {
761           $resftype |= $rFonts->{"value"};
762           last;
763         }
764       }
765     }
766     for my $rFonts (\%fancyFonts, \%initialFonts, \%symbolFonts) {
767       if (defined($rFonts->{$key})) {
768         if ($fontname =~ $rFonts->{$key}) {
769           $resftype |= $rFonts->{"value"};
770         }
771       }
772     }
773   }
774   if ("$fontname" =~ /^bpg/i) {
775     if ("$fontname" =~ /bpg (courier gpl|elite|serif)/i) {
776       $resftype |= SERIF;    # Serif
777     }
778     else {
779       $resftype |= SANS; # Sans Serif
780     }
781   }
782   elsif ("$fontname" =~ /^dustismo/i) {
783     if ("$fontname" =~ /^dustismo roman/i) {
784       $resftype |= SERIF;    # Serif
785     }
786     else {
787       $resftype |= SANS; # Sans Serif
788     }
789   }
790   elsif ("$fontname" =~ /^go\b/i) {
791     if ("$fontname" =~ /^go mono/i) {
792       $resftype |= SERIF;    # Serif
793     }
794     else {
795       $resftype |= SANS; # Sans Serif
796     }
797   }
798   # Create the string
799   my @ft = ();
800   if ($resftype == 0) {
801     $resftype = $ftypes{default};
802   }
803   else {
804     # fonts SANS, SERIF, SCRIPT and FRAKTUR are mutualy exclusive
805     if ($resftype & FRAKTUR) {
806       $resftype &= ~(SANS|SERIF|SCRIPT);
807     }
808     elsif ($resftype & SCRIPT) {
809       $resftype &= ~(SANS|SERIF);
810     }
811     elsif ($resftype & SANS) {
812       $resftype &= ~SERIF;
813     }
814   }
815   for (my $i = 1; $i < 513; $i *= 2) {
816     if ($resftype & $i) {
817       push(@ft, $ftypes{$i});
818     }
819   }
820   return(join(',', @ft));
821 }
822
823 sub getweight($$)
824 {
825   my ($fontname, $style) = @_;
826   my $result = undef;
827   for my $info ($style, $fontname) {
828     for my $key (keys %weights) {
829       next if ($key !~ /^\d+$/);
830       my $val = $weights{$key};
831       if ($info =~ /\b$val\b/i) {
832         return($val);
833       }
834     }
835   }
836   return($result);
837 }
838
839 sub getwidth($$)
840 {
841   my ($fontname, $style) = @_;
842   my $result = undef;
843   for my $key (keys %widths) {
844     next if ($key !~ /^\d+$/);
845     for my $info ($style, $fontname) {
846       if ($info =~ /\b$widths{$key}\b/i) {
847         return($widths{$key});
848       }
849       if ($info =~ /\bRegular\b/) {
850         if (!defined($result)) {
851           $result = $widths{100};
852         }
853       }
854     }
855   }
856   return($result);
857 }
858
859 sub getslant($$)
860 {
861   my ($fontname, $style) = @_;
862   for my $key (keys %slants) {
863     next if ($key !~ /^\d+$/);
864     if ($style =~ /\b$slants{$key}\b/i) {
865       return($slants{$key});
866     }
867   }
868   return(undef);
869 }
870
871 sub getspacing($$)
872 {
873   my ($fontname, $style) = @_;
874   for my $key (keys %spacings) {
875     next if ($key !~ /^\d+$/);
876     if ($style =~ /\b$spacings{$key}\b/i) {
877       return($spacings{$key});
878     }
879   }
880   if ("$fontname $style" =~ /(\bmono\b|luximono|typewriter|cursor|fixed)\b/i) {
881     return($spacings{100}); # Mono
882   }
883   else {
884     return(undef);
885   }
886 }
887
888 sub ismathfont($$)
889 {
890   my ($fontname, $rCapability) = @_;
891
892   return 1 if ($fontname =~ /math/i);
893   for my $cap (@{$rCapability}) {
894     return 1 if ($cap eq "math");
895   }
896   return 0;
897 }
898
899 sub getproperties($$$$)
900 {
901   my ($l, $fontname, $style, $rerrors) = @_;
902   my $newstyle = &correctstyle($style);
903   my $newfam = &correctstyle($fontname);
904   my @properties = ();
905
906   for my $txt (qw(ftype weight width slant spacing)) {
907     my ($map, $rget);
908     eval("\$map = " . '\%' . $txt . 's');
909     eval('$rget = \&' . "get$txt");
910     my $val2 = getsinglevalue($l, $txt, $map);
911     my $val1 = $rget->($newfam, $newstyle);
912     my $val;
913     if (defined($val2) && defined($val1) && ($val2 ne $val1)) {
914       if (($txt =~/^(weight|slant)$/) && ($newstyle =~ /$val1/)){
915         # style overrides weight and slant
916         push(@{$rerrors}, "Fontname($fontname),Style($style): Values for $txt ($val1 != $val2) differ, pick $val1 from style");
917         $val = $val1;
918       }
919       elsif ($newfam =~ /$val1/) {
920         push(@{$rerrors}, "Fontname($fontname),Style($style): Values for $txt ($val1 != $val2) differ, pick $val1 from fontname");
921         $val = $val1;
922       }
923       else {
924         push(@{$rerrors}, "Fontname($fontname),Style($style): Values for $txt ($val1 != $val2) differ, pick $val2 from $txt-property");
925         $val = $val2;
926       }
927     }
928     elsif (! defined($val2)) {
929       $val = $val1;
930     }
931     else {
932       $val = $val2;
933     }
934     if (defined($val)) {
935       push(@properties, &addTxt($txt,$val));
936     }
937     else {
938       if (defined($map->{"default"})) {
939         push(@properties, &addTxt($txt,$map->{"default"}));
940       }
941       else {
942         push(@{$rerrors}, "Undefined value for $txt");
943       }
944     }
945   }
946   return(join(' ', @properties));
947 }
948
949 sub correctstyle($)
950 {
951   my ($style) = @_;
952   $style =~ s/^\\040//;
953   $style =~ s/^\s*\d+\s*//;
954   $style =~ s/\s*\d+$//;
955   $style =~ s/italic/ Italic/i;
956   $style =~ s/oblique/ Oblique/i;
957   $style =~ s/[\-_]/ /g;
958   $style =~ s/\breg\b/Regular/i;
959   $style =~ s/\bregita(lic)?\b/Regular Italic/i;
960   $style =~ s/\bregobl(ique)?\b/Regular Oblique/i;
961   $style =~ s/medium/Medium /i;
962   $style =~ s/\bmedita(lic)?\b/Medium Italic/i;
963   $style =~ s/\bmedobl(ique)?\b/Medium Oblique/i;
964   $style =~ s/\bmed\b/Medium /i;
965   $style =~ s/\bdemi\b/SemiBold/i;
966   $style =~ s/\bex(pd|t)\b/Expanded/i;
967   $style =~ s/semi ?cond(ensed)?/SemiCondensed/i;
968   $style =~ s/[sd]emi ?(bold|bd|bol)/SemiBold/i;
969   $style =~ s/semi ?(expanded|extended|expd)/SemiExpanded/i;
970   $style =~ s/[sd]emi ?light/SemiLight/i;
971   $style =~ s/ultra ?(expanded|extended|expd)/UltraExpanded/i;
972   $style =~ s/light/Light /i;
973   $style =~ s/\blt\b/Light /i;
974   $style =~ s/(ultra|extra)(light|lt)/ExtraLight /i;
975   $style =~ s/\bheavy\b/Extrabold/i;
976   $style =~ s/\bhairline\b/Extralight/i;
977   $style =~ s/\bcond\b/Condensed/i;
978   $style =~ s/(roman)?slanted/ Italic/i;
979   $style =~ s/\bslant\b/Italic/i;
980   $style =~ s/\b(SC|Small(caps(alt)?)?)\b/SmallCaps/i;
981   $style =~ s/w3 mono/Dual/i;
982   $style =~ s/Regul[ea]r/Regular/i;
983   $style =~ s/Megablack/ExtraBlack/i;
984   $style =~ s/  +/ /g;
985   return($style);
986 }
987
988 # return list of unicode values of the input string
989 #Allow input of intervals (e.g. 'a-z')
990 sub decimalUnicode($)
991 {
992   my ($a) = @_;
993   my @res = ();
994   # Convert to unicode chars first
995   while ($a =~ /^(.*)u\+(0?x[\da-f]+|\d+)(.*)$/i) {
996     my ($prev, $d, $post) = ($1, $2, $3);
997     if ($d =~ /^0?x(.+)$/) {
998       $d = hex($1);
999     }
1000     my $chr = encode('utf-8', chr($d));
1001     $a = $prev . $chr . $post;
1002   }
1003   # $a is now a string of unicode chars
1004   my $u = decode('utf-8', $a);
1005   my @a = split(//, $u);
1006   my $interval = 0;
1007   my $start = undef;
1008   for my $x (@a) {
1009     if ($x eq '-') {    # Interval
1010       $interval = 1;
1011       next;
1012     }
1013     if ($interval && defined($start)) {
1014       if (ord($x) < $start) {
1015         for (my $i = $start - 1; $i >= ord($x); $i--) {
1016           push(@res, $i);
1017         }
1018       }
1019       else {
1020         for (my $i = $start + 1; $i <= ord($x); $i++) {
1021           push(@res, $i);
1022         }
1023       }
1024       $start = undef;
1025     }
1026     else {
1027       $start = ord($x);
1028       push(@res, $start);
1029     }
1030     $interval = 0;
1031   }
1032   return(@res);
1033 }
1034
1035
1036 # check if the glyph-values in interval @{$ri} are contained
1037 # in one of the (sorted) intervals
1038 sub contains($$)
1039 {
1040   # ok if
1041   # ...re0..........re1...
1042   # ......start..end......
1043   my ($ri, $rList) = @_;
1044   my $start = $ri->[0];
1045   my $end = $ri->[1];
1046
1047   for my $re (@{$rList}) {
1048     next if ($re->[1] < $start);
1049     # now we found a possible matching interval
1050     return 1 if (($start >= $re->[0]) && ($end <= $re->[1]));
1051     return 0;
1052   }
1053   return 0;
1054 }
1055
1056 sub sprintIntervalls($)
1057 {
1058   my ($rList) = @_;
1059   my @out = ();
1060   for my $rE (@{$rList}) {
1061     if ($rE->[0] != $rE->[1]) {
1062       push(@out, $rE->[0] . '-' . $rE->[1]);
1063     }
1064     else {
1065       push(@out, $rE->[0]);
1066     }
1067   }
1068   return join(',', @out);
1069 }
1070
1071 sub buildFontName($$$$)
1072 {
1073   my ($family, $style, $fullname, $postscriptname) = @_;
1074
1075   my $result = "";
1076   $style =~ s/\\040//;
1077   $family =~ s/\\040/\-/;
1078   $family =~ s/\bcond\b/Condensed/i;
1079   $family =~ s/\bblk\b/Black/i;
1080   $family =~ s/\bsembd\b/SemiBold/i;
1081   $family =~ s/\bsemcond\b/SemiCondensed/i;
1082   $family =~ s/\bextcond\b/ExtraCondensed/i;
1083   $family =~ s/\bextbd\b/ExtraBold/i;
1084   $family =~ s/\bextlt\b/ExtraLight/i;
1085   $style =~ s/\bextra\-light\b/ExtraLight/i;
1086   $style =~ s/\bbol\b/Bold/i;
1087   $family =~ s/\bmed\b/Medium/i;
1088   $family =~ s/^([A-Z]+[a-z]+)([A-Z][a-z]+)\b/$1 $2/;
1089   my @style = split(' ', $style);
1090   for my $st (@style) {
1091     $st = ucfirst($st);
1092     if ($family !~ s/$st/$st/i) {
1093       $family .= " $st";
1094     }
1095     else {
1096       # check if $st in $family starts with ' '
1097       $family =~ s/(\w)$st/$1 $st/i;
1098     }
1099   }
1100   $postscriptname =~ s/[- ]?Regular$//;
1101   if ($fullname =~ /^(font)?\d+/) {
1102     $fullname = "";
1103   }
1104   if (length($fullname) <= length($family)) {
1105     $result = $family;
1106   }
1107   else {
1108     $result = $fullname;
1109   }
1110   return($result);
1111 }