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