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