]> git.lyx.org Git - lyx.git/blob - development/tools/listFontWithLang.pl
271a5ba428fd6a477afeb0cfdf6db5d427fb189c
[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 # list of regexes for known sans serif fonts
294 my %sansFonts = (
295   "value" => SANS,          # Sans serif
296   "a" => qr/^a(030|becedario|bydos|haroni|rial|ndika|ngostura|nonymous|rab|roania|rimo|sap)/i,
297   "b" => qr/^b(aekmuk|ebas|erenika|eteckna|euron|lue)/i,
298   "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,
299   "d" => qr/^(d2coding|dimnah|dosis|dyuthi)/i,
300   "e" => qr/^(electron|engebrechtre)/i,
301   "f" => qr/^(fandolhei|fetamont|fira|font awesome 5|forgotten)/i,
302   "g" => qr/^(gardiner|garuda|gfs ?neo|gillius|granada|graph|guanine|gunplay)/i,
303   "h" => qr/^(hack|hani|haramain|harano|harmattan|hor\b)/i,
304   "i" => qr/^(ibm ?(plex ?mono|3270)|ikarius|inconsolata|induni.?h|iwona)/i,
305   "j" => qr/^(jara|jura)/i,
306   "k" => qr/^(kalimati|kanji|karla|karma|kayrawan|kenyan|keraleeyam|khalid|khmer [or]|kiloji|klaudia|ko[mn]atu|kurier|kustom)/i,
307   "l" => qr/^l(aksaman|arabie|ato|eague|exend|exigulim|ibel|iberation|ibre franklin|ibris|inux biolinum|obster|ogix|ohit|oma)/i,
308   "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,
309   "n" => qr/^(nachlieli|nada|nafees|nagham|nanum(barunpen|square)|nice)/i,
310   "o" => qr/^(ocr|okolaks|opendyslexic|ostorah|ouhud|over|oxygen)/i,
311   "p" => qr/^(padauk|pagul|paktype|pakenham|palladio|petra|phetsarath|play\b|poiret|port\b|primer\b|prociono|pt\b|purisa)/i,
312   "q" => qr/^(qt(ancient|helvet|avanti|doghaus|eratype|eurotype|floraline|frank|fritz|future|greece|howard|letter|optimum)|quercus)/i,
313   "r" => qr/^(rachana|radio\b|raleway|ricty|roboto|rosario)/i,
314   "s" => qr/^(salem|samanata|sawasdee|shado|sharja|simple|sophia|soul|source|switzera)/i,
315   "t" => qr/^(tarablus|teen|texgyre(adventor|heros)|tiresias|trebuchet|tscu|tuffy)/i,
316   "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,
317   "v" => qr/^(veranda|vn ?urwclassico)/i,
318   "w" => qr/^(waree)/i,
319   "y" => qr/^(yanone)/i,
320   "z" => qr/^(zekton|zero)/i,
321 );
322 my %scriptFonts = (
323   "value" => SCRIPT,          # Script
324   "c" => qr/^(chancery)/i,
325   "d" => qr/^(dancing)/i,
326   "e" => qr/^(elegante)/i,
327   "j" => qr/^jsmath.?(rsfs)/i,
328   "k" => qr/^(kaushan|karumbi|kristi)/i,
329   "m" => qr/^(mathjax_script|miama)/i,
330   "n" => qr/^(nanum (brush|pen) script)/i,
331   "q" => qr/^qt(arabian|boulevard|brushstroke|chancery|coronation|florencia|handwriting|linostroke|merry|pandora|slogan)/i,
332   "r" => qr/^((romande.*|ruf)script|rsfs)/i,
333   "t" => qr/^typo ?script/i,
334   "u" => qr/^u(n ?pilgi|rw ?chancery|kij ?(jelliy|moy|qolyazma ?(tez|yantu)))/i,
335 );
336
337 my %fraktFonts = (
338   "value" => FRAKTUR,          # Fraktur
339   "e" => qr/^eufm/i,
340   "j" => qr/^(jsmath.?euf)/i,
341   "m" => qr/^(missaali)/i,
342   "o" => qr/^(oldania)/i,
343   "q" => qr/^qt(blackforest|cloisteredmonk|dublinirish|fraktur|heidelbergtype|(lino|london)scroll)/i,
344   "u" => qr/^ukij ?(kufi ?tar|mejnun ?reg)/i,
345 );
346
347 my %fancyFonts = (
348   "value" => FANCY,          # Fancy
349   "a" => qr/^a(bandoned|bberancy)/i,
350   "c" => qr/^(cretino)/i,
351   "d" => qr/^dseg/i,
352   "f" => qr/^frederika/i,
353   "g" => qr/^(gfs.?theo)/i,
354   "k" => qr/^keter|kicking|kredit|kouzan|kerkis calligraphic/i,
355   "u" => qr/^ukij ?(saet|tiken)/i,
356 );
357
358 my %initialFonts = (
359   "value" => INITIALS,          # Initials
360   "e" => qr/^(eb.?garamond.?init)/i,
361   "t" => qr/^typographer/i,
362   "y" => qr/^(yinit)/i,
363 );
364
365 my %symbolFonts = (
366   "value" => SYMBOL,          # Symbol
367   "a" => qr/^(academicons)/i,
368   "c" => qr/^(caladings|ccicons|chess|cmsy|cmex)/i,
369   "d" => qr/^(dingbats|drmsym|d05)/i,
370   "e" => qr/^(elusiveicons|emoji|esint|euterpe)/i,
371   "f" => qr/^(fandol.?brail|fdsymbol|fourierorns|font(awesome|ello|.?mfizz))/i,
372   "g" => qr/^(gan.?clm|gfs.?(baskerville|gazis|olga|porson|solomos|(bodoni|didot|complutum).?classic))/i,
373   "h" => qr/^(hots)/i,
374   "j" => qr/^jsmath.?(msam|cmsy|masm|msbm|wasy|cmex|stmary)/i,
375   "m" => qr/^(marvosym|material|msam|msbm)/i,
376   "n" => qr/^(noto.*emoji)/i,
377   "o" => qr/^(octicons)/i,
378   "p" => qr/^patch/i,
379   "q" => qr/^(qtdingbits)/i,
380   "s" => qr/^stmary/i,
381   "t" => qr/^(typicons|twemoji)/i,
382   "u" => qr/^ukij ?(imaret|orxun|tughra)/i,
383   "w" => qr/^(webdings|wasy)/i,
384 );
385
386 if (open(FI,  "$cmd |")) {
387  NXTLINE: while (my $l = <FI>) {
388     chomp($l);
389     while ($l !~ /abcd$/) {
390       $l .= <FI>;
391       chomp($l);
392     }
393     my $file = "";
394     my $fonttype;
395     if ($l =~ /file=\"([^\"]+)\"/) {
396       $file = $1;
397       #next if ($file !~ /\.(otf|ttf|pfa|pfb|pcf|ttc)$/i);
398       if ($file !~ /\.([a-z0-9]{2,5})$/i) {
399         print "Unhandled extension for file $file\n";
400         next;
401       }
402       $fonttype = lc($1);
403       if (! defined($fontpriority{$fonttype})) {
404         print "Added extension $fonttype for file $file\n";
405         $fontpriority{$fonttype} = $nexttype;
406         $nexttype++;
407       }
408     }
409     my %usedlangs = ();
410     if ($l =~ / lang=\"([^\"]+)\"/) {
411       my @ll = split(/\|/, $1);
412       for my $lx (@ll) {
413         $usedlangs{&convertlang($lx)} = 1;
414       }
415     }
416
417     for my $lang (@langs) {
418       next NXTLINE if (! defined($usedlangs{$lang}));
419     }
420     my $style = &getVal($l, "style", "stylelang", 1);
421     $style =~ s/^\\040//;
422     my $fullname = &getVal($l, "fn", "fnl");
423     my $postscriptname = "";
424     if ($l =~ /postscriptname=\"([^\"]+)\"/) {
425       $postscriptname = $1;
426     }
427     my $family = &getVal($l, "family", "flang", 0);
428     my $fontname = &buildFontName($family, $style, $fullname, $postscriptname);
429
430     if (defined($options{NFontName})) {
431       for my $fn (@{$options{NFontName}}) {
432         next NXTLINE if ($fontname =~ /$fn/i);
433       }
434     }
435     if (defined($options{FontName})) {
436       for my $fn (@{$options{FontName}}) {
437         next NXTLINE if ($fontname !~ /$fn/i);
438       }
439     }
440     my @charlist = ();
441     if (defined($options{Contains}) || defined($options{NContains}) || exists($options{PrintCharset})) {
442       if ($l =~ / charset=\"([^\"]+)\"/) {
443         my @list = split(/\s+/, $1);
444         for my $e (@list) {
445           my ($l, $h) = split('-', $e);
446           $h = $l if (! defined($h));
447           push(@charlist, [hex($l), hex($h)]);
448         }
449       }
450       if (defined($options{Contains})) {
451         for my $g (@{$options{Contains}}) {
452           next NXTLINE if (! contains($g, \@charlist));
453         }
454       }
455       if (defined($options{NContains})) {
456         for my $g (@{$options{NContains}}) {
457           # Ignore if ANY char exist in @charlist
458           for (my $i = $g->[0]; $i <= $g->[1]; $i++) {
459             next NXTLINE if (contains([$i,$i], \@charlist));
460           }
461         }
462       }
463     }
464     my $props = "";
465     my @errors = ();
466     if (exists($options{PrintProperties}) || defined($options{Property}) || defined($options{NProperty})) {
467       my $properties = getproperties($l, $fontname, $style, \@errors);
468       if (defined($options{Property})) {
469         for my $pn (@{$options{Property}}) {
470           next NXTLINE if ($properties !~ /$pn/i);
471         }
472       }
473       if (defined($options{NProperty})) {
474         for my $pn (@{$options{NProperty}}) {
475           next NXTLINE if ($properties =~ /$pn/i);
476         }
477       }
478       if (exists($options{PrintProperties})) {
479         $props .= " ($properties)";
480       }
481     }
482
483     if (exists($options{PrintLangs})) {
484       $props .= '(' . join(',', sort keys %usedlangs) . ')';
485     }
486     if (exists($options{PrintCharset})) {
487       $props .= '(' . &sprintIntervalls(\@charlist) . ')';
488     }
489     if (exists($options{PrintScripts}) || defined($options{Scripts}) || defined($options{NScripts}) || exists($options{Math})) {
490       my @scripts = ();
491       my $scripts = "";
492       if ($l =~ / script=\"([^\"]+)\"/) {
493         @scripts = split(/\s+/, $1);
494         for my $ent (@scripts) {
495           $ent =~ s/^\s*otlayout://;
496           $ent = lc($ent);
497         }
498         $scripts = join(',', @scripts);
499       }
500       if (exists($options{Math})) {
501         next NXTLINE if (! &ismathfont($fontname,\@scripts));
502       }
503       if (exists($options{PrintScripts})) {
504         $props .= "($scripts)";
505       }
506       if (!defined($scripts[0])) {
507         # No script defined in font, so check only $options{Scripts}
508         next NXTLINE if (defined($options{Scripts}));
509       }
510       else {
511         if (defined($options{Scripts})) {
512           for my $s (@{$options{Scripts}}) {
513             next NXTLINE if ($scripts !~ /$s/i);
514           }
515         }
516         if (defined($options{NScripts})) {
517           for my $s (@{$options{NScripts}}) {
518             next NXTLINE if ($scripts =~ /$s/i);
519           }
520         }
521       }
522     }
523     my $foundry = "";
524     if ($l =~ /foundry=\"([^\"]+)\"/) {
525       $foundry = $1;
526       $foundry =~ s/^\s+//;
527       $foundry =~ s/\s+$//;
528     }
529     if (defined($collectedfonts{$fontname}->{$foundry}->{errors})) {
530       # Apparently not the first one, so add some info
531       my $oldfonttype = $collectedfonts{$fontname}->{$foundry}->{fonttype};
532       if (defined($errors[0])) {
533         push(@{$collectedfonts{$fontname}->{$foundry}->{errors}}, @errors);
534       }
535       if ($fontpriority{$oldfonttype} > $fontpriority{$fonttype}) {
536         push(@{$collectedfonts{$fontname}->{$foundry}->{errors}}, "Warning: overwriting old info for file: " . $collectedfonts{$fontname}->{$foundry}->{file});
537       }
538       else {
539         push(@{$collectedfonts{$fontname}->{$foundry}->{errors}}, "Warning: discarding new info for file: $file");
540         next;
541       }
542     }
543     else {
544       $collectedfonts{$fontname}->{$foundry}->{errors} = \@errors;
545     }
546     $collectedfonts{$fontname}->{$foundry}->{props} = $props;
547     $collectedfonts{$fontname}->{$foundry}->{file} = $file;
548     $collectedfonts{$fontname}->{$foundry}->{fonttype} = $fonttype;
549   }
550   close(FI);
551 }
552
553 for my $fontname (sort keys %collectedfonts) {
554   my @foundries = sort keys %{$collectedfonts{$fontname}};
555   my $printfoundries = 0;
556   if (defined($foundries[1])) {
557     $printfoundries = 1;
558   }
559   for my $foundry (@foundries) {
560     if (exists($options{PrintWarnings})) {
561       for my $err (@{$collectedfonts{$fontname}->{$foundry}->{errors}}) {
562         print "$err\n";
563       }
564     }
565     my $fn = "Font : $fontname";
566     if ($printfoundries && ($foundry ne "")) {
567       $fn .= " \[$foundry\]";
568     }
569     print $fn;
570     print $collectedfonts{$fontname}->{$foundry}->{props};
571     if (exists($options{PrintFiles})) {
572       print ": " . $collectedfonts{$fontname}->{$foundry}->{file} . "\n";
573     }
574     else {
575       print "\n";
576     }
577   }
578 }
579
580 exit(0);
581 #################################################################################
582 sub convertlang($)
583 {
584   my ($ilang) = @_;
585   if ($ilang =~ /^\s*([a-z]+)([\-_]([a-z]+))?\s*$/i) {
586     my ($x, $y) = ($1, $3);
587     if (defined($y)) {
588       $ilang = lc($x) . '-' . lc($y);
589     }
590     else {
591       $ilang = lc($x);
592     }
593   }
594   return($ilang);
595 }
596
597 sub extractlist($$$)
598 {
599   my ($l, $islang, $txt, $rres) = @_;
600   my @res = ();
601   if ($l =~ /$txt=\"([^\"]+)\"/) {
602     @res = split(',', $1);
603     if ($islang) {
604       for my $lg (@res) {
605         $lg = &convertlang($lg);
606       }
607     }
608   }
609   @{$rres} = @res;
610 }
611
612 sub getIndexes($$)
613 {
614   my ($lang, $rlangs) = @_;
615   my @res = ();
616
617   for (my $i = 0; defined($rlangs->[$i]); $i++) {
618     if ($rlangs->[$i] eq $lang) {
619       push(@res, $i);
620     }
621   }
622   return(\@res);
623 }
624
625 sub getVal($$$$)
626 {
627   my ($l, $txtval, $txtlang, $combine) = @_;
628   my @values = ();
629   my @langs = ();
630   &extractlist($l, 0, $txtval, \@values);
631   return("") if (! defined($values[0]));
632   &extractlist($l, 1, $txtlang, \@langs);
633   my $i = &getIndexes("en", \@langs);
634   my $res = "";
635   for my $k (@{$i}) {
636     if (defined($values[$k])) {
637       if ($combine) {
638         if ($res ne "") {
639           $res .= " $values[$k]";
640         }
641         else {
642           $res = $values[$k];
643         }
644       }
645       else {
646         if (length($values[$k]) > length($res)) {
647           $res = $values[$k];
648         }
649       }
650     }
651   }
652   return($values[0]) if ($res eq "");
653   return($res);
654 }
655
656 sub getsinglevalue($$$)
657 {
658   my ($l, $txt, $rMap, $rget) = @_;
659   my $val;
660   if ($l =~ / $txt=(\d+)/) {
661     $val = $1;
662     # Search for nearest value to $val
663     if (defined($rMap->{$val})) {
664       return($rMap->{$val});
665     }
666     my $maxv = -1;
667     my $minv = 1000;
668     for my $key (keys %{$rMap}) {
669       next if ($key !~ /^\d+$/);
670       my $diff = abs($key - $val);
671       if ($diff < $minv) {
672         $maxv = $key;
673         $minv = $diff;
674       }
675       elsif ($diff == $minv) {
676         if ($key < $maxv) {
677           $maxv = $key;
678         }
679       }
680     }
681     if (! defined($rMap->{$maxv})) {
682       print "ERROR2: txt=$txt, val=$val\n";
683       exit(-2);
684     }
685     if ($val > $maxv) {
686       return($rMap->{$maxv} . "+$minv");
687     }
688     else {
689       return($rMap->{$maxv} . "-$minv");
690     }
691   }
692   else {
693     return(undef);
694   }
695 }
696
697 sub addTxt($$)
698 {
699   my ($txt, $val) = @_;
700   return("$txt($val)");
701 }
702
703 sub getftype($$)
704 {
705   my ($fontname, $style) = @_;
706   my $resftype = 0;
707   if ($fontname =~ /(sans)[-_ ]?(serif)?/i) {
708     $resftype |= SANS;
709   }
710   elsif ($fontname =~ /gothic|dotum|gulim/i) {
711     if ($fontname =~ /bisrat gothic/i) {
712       $resftype |= SERIF;
713     }
714     else {
715       $resftype |= SANS;
716     }
717   }
718   elsif ($fontname =~ /^(jsmath.?)?bbold|msbm|^(ds(rom|serif|ss))/i) {
719     $resftype |= DOUBLESTROKE;  # Double stroke (math font)
720   }
721   if ($fontname =~ /serif|times|mincho|batang/i) {
722     if ($fontname =~ /good times/i) {
723       $resftype |= SANS; # Sans Serif
724     }
725   }
726   if ($fontname =~ /initial(s|en)/i) {
727     $resftype |= INITIALS;
728   }
729   if ($fontname =~ /symbol/i) {
730     if ($fontname !~ /^symbola/i) {
731       $resftype |= SYMBOL;
732     }
733   }
734   # Now check for fonts without a hint in font name
735   if ($fontname =~ /^([a-z])/i) {
736     my $key = lc($1);
737     for my $rFonts (\%sansFonts, \%scriptFonts, \%fraktFonts, \%fancyFonts, \%initialFonts, \%symbolFonts) {
738       if (defined($rFonts->{$key})) {
739         if ($fontname =~ $rFonts->{$key}) {
740           $resftype |= $rFonts->{"value"};
741         }
742       }
743     }
744   }
745   if ("$fontname" =~ /^bpg/i) {
746     if ("$fontname" =~ /bpg (courier gpl|elite)/i) {
747       $resftype |= SERIF;    # Serif
748     }
749     else {
750       $resftype |= SANS; # Sans Serif
751     }
752   }
753   elsif ("$fontname" =~ /^dustismo/i) {
754     if ("$fontname" =~ /^dustismo roman/i) {
755       $resftype |= SERIF;    # Serif
756     }
757     else {
758       $resftype |= SANS; # Sans Serif
759     }
760   }
761   elsif ("$fontname" =~ /^go\b/i) {
762     if ("$fontname" =~ /^go mono/i) {
763       $resftype |= SERIF;    # Serif
764     }
765     else {
766       $resftype |= SANS; # Sans Serif
767     }
768   }
769   # Create the string
770   my @ft = ();
771   if ($resftype == 0) {
772     $resftype = $ftypes{default};
773   }
774   elsif ($resftype & SANS) {
775     $resftype &= ~SERIF;
776   }
777   for (my $i = 1; $i < 513; $i *= 2) {
778     if ($resftype & $i) {
779       push(@ft, $ftypes{$i});
780     }
781   }
782   return(join(',', @ft));
783 }
784
785 sub getweight($$)
786 {
787   my ($fontname, $style) = @_;
788   my $result = undef;
789   for my $info ($style, $fontname) {
790     for my $key (keys %weights) {
791       next if ($key !~ /^\d+$/);
792       my $val = $weights{$key};
793       if ($info =~ /\b$val\b/i) {
794         return($val);
795       }
796     }
797   }
798   return($result);
799 }
800
801 sub getwidth($$)
802 {
803   my ($fontname, $style) = @_;
804   my $result = undef;
805   for my $key (keys %widths) {
806     next if ($key !~ /^\d+$/);
807     for my $info ($style, $fontname) {
808       if ($info =~ /\b$widths{$key}\b/i) {
809         return($widths{$key});
810       }
811       if ($info =~ /\bRegular\b/) {
812         if (!defined($result)) {
813           $result = $widths{100};
814         }
815       }
816     }
817   }
818   return($result);
819 }
820
821 sub getslant($$)
822 {
823   my ($fontname, $style) = @_;
824   for my $key (keys %slants) {
825     next if ($key !~ /^\d+$/);
826     if ($style =~ /\b$slants{$key}\b/i) {
827       return($slants{$key});
828     }
829   }
830   return(undef);
831 }
832
833 sub getspacing($$)
834 {
835   my ($fontname, $style) = @_;
836   for my $key (keys %spacings) {
837     next if ($key !~ /^\d+$/);
838     if ($style =~ /\b$spacings{$key}\b/i) {
839       return($spacings{$key});
840     }
841   }
842   if ("$fontname $style" =~ /(\bmono\b|luximono|typewriter|cursor|fixed)\b/i) {
843     return($spacings{100}); # Mono
844   }
845   else {
846     return(undef);
847   }
848 }
849
850 sub ismathfont($$)
851 {
852   my ($fontname, $rCapability) = @_;
853
854   return 1 if ($fontname =~ /math/i);
855   for my $cap (@{$rCapability}) {
856     return 1 if ($cap eq "math");
857   }
858   return 0;
859 }
860
861 sub getproperties($$$$)
862 {
863   my ($l, $fontname, $style, $rerrors) = @_;
864   my $newstyle = &correctstyle($style);
865   my $newfam = &correctstyle($fontname);
866   my @properties = ();
867
868   for my $txt (qw(ftype weight width slant spacing)) {
869     my ($map, $rget);
870     eval("\$map = " . '\%' . $txt . 's');
871     eval('$rget = \&' . "get$txt");
872     my $val2 = getsinglevalue($l, $txt, $map);
873     my $val1 = $rget->($newfam, $newstyle);
874     my $val;
875     if (defined($val2) && defined($val1) && ($val2 ne $val1)) {
876       if (($txt =~/^(weight|slant)$/) && ($newstyle =~ /$val1/)){
877         # style overrides weight and slant
878         push(@{$rerrors}, "Fontname($fontname),Style($style): Values for $txt ($val1 != $val2) differ, pick $val1 from style");
879         $val = $val1;
880       }
881       elsif ($newfam =~ /$val1/) {
882         push(@{$rerrors}, "Fontname($fontname),Style($style): Values for $txt ($val1 != $val2) differ, pick $val1 from fontname");
883         $val = $val1;
884       }
885       else {
886         push(@{$rerrors}, "Fontname($fontname),Style($style): Values for $txt ($val1 != $val2) differ, pick $val2 from $txt-property");
887         $val = $val2;
888       }
889     }
890     elsif (! defined($val2)) {
891       $val = $val1;
892     }
893     else {
894       $val = $val2;
895     }
896     if (defined($val)) {
897       push(@properties, &addTxt($txt,$val));
898     }
899     else {
900       if (defined($map->{"default"})) {
901         push(@properties, &addTxt($txt,$map->{"default"}));
902       }
903       else {
904         push(@{$rerrors}, "Undefined value for $txt");
905       }
906     }
907   }
908   return(join(' ', @properties));
909 }
910
911 sub correctstyle($)
912 {
913   my ($style) = @_;
914   $style =~ s/^\\040//;
915   $style =~ s/^\s*\d+\s*//;
916   $style =~ s/\s*\d+$//;
917   $style =~ s/italic/ Italic/i;
918   $style =~ s/oblique/ Oblique/i;
919   $style =~ s/[\-_]/ /g;
920   $style =~ s/\breg\b/Regular/i;
921   $style =~ s/\bregita(lic)?\b/Regular Italic/i;
922   $style =~ s/\bregobl(ique)?\b/Regular Oblique/i;
923   $style =~ s/medium/Medium /i;
924   $style =~ s/\bmedita(lic)?\b/Medium Italic/i;
925   $style =~ s/\bmedobl(ique)?\b/Medium Oblique/i;
926   $style =~ s/\bmed\b/Medium /i;
927   $style =~ s/\bdemi\b/SemiBold/i;
928   $style =~ s/\bex(pd|t)\b/Expanded/i;
929   $style =~ s/semi ?cond(ensed)?/SemiCondensed/i;
930   $style =~ s/[sd]emi ?(bold|bd|bol)/SemiBold/i;
931   $style =~ s/semi ?(expanded|extended|expd)/SemiExpanded/i;
932   $style =~ s/[sd]emi ?light/SemiLight/i;
933   $style =~ s/ultra ?(expanded|extended|expd)/UltraExpanded/i;
934   $style =~ s/light/Light /i;
935   $style =~ s/\blt\b/Light /i;
936   $style =~ s/(ultra|extra)(light|lt)/ExtraLight /i;
937   $style =~ s/\bheavy\b/Extrabold/i;
938   $style =~ s/\bhairline\b/Extralight/i;
939   $style =~ s/\bcond\b/Condensed/i;
940   $style =~ s/(roman)?slanted/ Italic/i;
941   $style =~ s/\bslant\b/Italic/i;
942   $style =~ s/\b(SC|Small(caps(alt)?)?)\b/SmallCaps/i;
943   $style =~ s/w3 mono/Dual/i;
944   $style =~ s/Regul[ea]r/Regular/i;
945   $style =~ s/Megablack/ExtraBlack/i;
946   $style =~ s/  +/ /g;
947   return($style);
948 }
949
950 # return list of unicode values of the input string
951 #Allow input of intervals (e.g. 'a-z')
952 sub decimalUnicode($)
953 {
954   my ($a) = @_;
955   my @res = ();
956   # Convert to unicode chars first
957   while ($a =~ /^(.*)u\+(0?x[\da-f]+|\d+)(.*)$/i) {
958     my ($prev, $d, $post) = ($1, $2, $3);
959     if ($d =~ /^0?x(.+)$/) {
960       $d = hex($1);
961     }
962     my $chr = encode('utf-8', chr($d));
963     $a = $prev . $chr . $post;
964   }
965   # $a is now a string of unicode chars
966   my $u = decode('utf-8', $a);
967   my @a = split(//, $u);
968   my $interval = 0;
969   my $start = undef;
970   for my $x (@a) {
971     if ($x eq '-') {    # Interval
972       $interval = 1;
973       next;
974     }
975     if ($interval && defined($start)) {
976       if (ord($x) < $start) {
977         for (my $i = $start - 1; $i >= ord($x); $i--) {
978           push(@res, $i);
979         }
980       }
981       else {
982         for (my $i = $start + 1; $i <= ord($x); $i++) {
983           push(@res, $i);
984         }
985       }
986       $start = undef;
987     }
988     else {
989       $start = ord($x);
990       push(@res, $start);
991     }
992     $interval = 0;
993   }
994   return(@res);
995 }
996
997
998 # check if the glyph-values in interval @{$ri} are contained
999 # in one of the (sorted) intervals
1000 sub contains($$)
1001 {
1002   # ok if
1003   # ...re0..........re1...
1004   # ......start..end......
1005   my ($ri, $rList) = @_;
1006   my $start = $ri->[0];
1007   my $end = $ri->[1];
1008
1009   for my $re (@{$rList}) {
1010     next if ($re->[1] < $start);
1011     # now we found a possible matching interval
1012     return 1 if (($start >= $re->[0]) && ($end <= $re->[1]));
1013     return 0;
1014   }
1015   return 0;
1016 }
1017
1018 sub sprintIntervalls($)
1019 {
1020   my ($rList) = @_;
1021   my @out = ();
1022   for my $rE (@{$rList}) {
1023     if ($rE->[0] != $rE->[1]) {
1024       push(@out, $rE->[0] . '-' . $rE->[1]);
1025     }
1026     else {
1027       push(@out, $rE->[0]);
1028     }
1029   }
1030   return join(',', @out);
1031 }
1032
1033 sub buildFontName($$$$)
1034 {
1035   my ($family, $style, $fullname, $postscriptname) = @_;
1036
1037   my $result = "";
1038   $style =~ s/\\040//;
1039   $family =~ s/\\040/\-/;
1040   $family =~ s/\bcond\b/Condensed/i;
1041   $family =~ s/\bblk\b/Black/i;
1042   $family =~ s/\bsembd\b/SemiBold/i;
1043   $family =~ s/\bsemcond\b/SemiCondensed/i;
1044   $family =~ s/\bextcond\b/ExtraCondensed/i;
1045   $family =~ s/\bextbd\b/ExtraBold/i;
1046   $family =~ s/\bextlt\b/ExtraLight/i;
1047   $style =~ s/\bextra\-light\b/ExtraLight/i;
1048   $style =~ s/\bbol\b/Bold/i;
1049   $family =~ s/\bmed\b/Medium/i;
1050   if ($family =~ /powerline/i) {
1051     my $a = 17;
1052   }
1053   $family =~ s/^([A-Z]+[a-z]+)([A-Z][a-z]+)\b/$1 $2/;
1054   my @style = split(' ', $style);
1055   for my $st (@style) {
1056     $st = ucfirst($st);
1057     if ($family !~ s/$st/$st/i) {
1058       $family .= " $st";
1059     }
1060     else {
1061       # check if $st in $family starts with ' '
1062       $family =~ s/(\w)$st/$1 $st/i;
1063     }
1064   }
1065   $postscriptname =~ s/[- ]?Regular$//;
1066   if ($fullname =~ /^(font)?\d+/) {
1067     $fullname = "";
1068   }
1069   if (length($fullname) <= length($family)) {
1070     $result = $family;
1071   }
1072   else {
1073     $result = $fullname;
1074   }
1075   return($result);
1076 }