]> git.lyx.org Git - features.git/blobdiff - development/tools/listFontWithLang.pl
Tools(listFontWithLang.pl): Amend 58dfb1d8, Select fonts containig specified glyphs
[features.git] / development / tools / listFontWithLang.pl
index 88fed6bddae2e34f53dc67e866f080c48c102bf1..342e70e9f67e249f3dccb731b1f0cb66bd234a68 100644 (file)
@@ -32,6 +32,7 @@ BEGIN {
 
 use strict;
 use warnings;
+use Encode;
 use GetOptions;
 
 sub convertlang($);
@@ -41,6 +42,8 @@ sub getVal($$$);      # my ($l, $txtval, $txtlang) = @_;
 sub getproperties($$$$);
 sub ismathfont($$);
 sub correctstyle($);
+sub decimalUnicode($);
+sub contains($$);
 
 # Following fields for a parameter can be defined:
 # fieldname:         Name of entry in %options
@@ -77,10 +80,17 @@ my @optionsDef = (
   ["math",
    {fieldname => "Math",
     comment => "Select fonts probably containing math glyphs"},],
+  ["c",
+   {fieldname => "Contains",
+    type => "=s", listsep => ',',
+    comment => "Select fonts containing all these (possibly comma separated) glyphs",}],
   ["l",
    {fieldname => "Lang",
     type => "=s", alias=>["lang"],
     comment => "Comma separated list of desired languages"},],
+  ["pc",
+   {fieldname => "PrintCharset", alias => ["printcharset"],
+    comment => "Print intervals of supported unicode character values"},],
   ["pl",
    {fieldname => "PrintLangs", alias => ["printlangs"],
     comment => "Print supported languages"},],
@@ -108,6 +118,38 @@ for my $lg (@langs) {
   $lg = &convertlang($lg);
 }
 
+if (defined($options{Contains})) {
+  my %glyphs = ();         # To ignore duplicates
+  for my $a1 (@{$options{Contains}}) {
+    for my $e (decimalUnicode($a1)) {
+      $glyphs{$e} = 1;
+    }
+  }
+  # create intervalls
+  my @glyphs = sort {$a <=> $b;} keys %glyphs;
+
+  # $options{Contains} no longer needed, so use it for unicode-point intervalls
+  $options{Contains} = [];
+  my ($first, $last) = (undef, undef);
+  for my $i (@glyphs) {
+    if (! defined($last)) {
+      $first = $i;
+      $last = $i;
+      next;
+    }
+    if ($i == $last+1) {
+      $last = $i;
+      next;
+    }
+    push(@{$options{Contains}}, [$first, $last]);
+    $first = $i;
+    $last = $i;
+  }
+  if (defined($last)) {
+    push(@{$options{Contains}}, [$first, $last]);
+  }
+}
+
 my $cmd = "fc-list";
 if (defined($langs[0])) {
   $cmd .= " :lang=" . join(',', @langs);
@@ -125,9 +167,12 @@ if (exists($options{PrintScripts}) || defined($options{Scripts}) || defined($opt
 if (exists($options{PrintLangs}) || defined($langs[0])) {
   $format .= " lang=\"%{lang}\"";
 }
-if (exists($options{PrintProperties}) || defined($options{Property})) {
+if (exists($options{PrintProperties}) || defined($options{Property}) || defined($options{NProperty})) {
   $format .= " weight=%{weight} slant=%{slant} width=%{width} spacing=%{spacing}";
 }
+if (defined($options{Contains}) || exists($options{PrintCharset})) {
+  $format .= " charset=\"%{charset}\"";
+}
 $format .= " file=\"%{file}\" abcd\\n";
 $cmd .= " -f '$format'";
 #print "$cmd\n";
@@ -156,6 +201,7 @@ my %weights = (
   200 => "Bold",
   205 => "Extrabold",
   210 => "Black",
+  215 => "ExtraBlack",
 );
 
 my %slants = (
@@ -287,6 +333,7 @@ my %symbolFonts = (
   "o" => qr/^(octicons)/i,
   "q" => qr/^(qtdingbits)/i,
   "t" => qr/^(typicons|twemoji)/i,
+  "w" => qr/^(webdings)/i,
 );
 
 if (open(FI,  "$cmd |")) {
@@ -312,7 +359,6 @@ if (open(FI,  "$cmd |")) {
         $nexttype++;
       }
     }
-    my $nfound = 0;
     my %usedlangs = ();
     if ($l =~ / lang=\"([^\"]+)\"/) {
       my @ll = split(/\|/, $1);
@@ -324,7 +370,6 @@ if (open(FI,  "$cmd |")) {
     for my $lang (@langs) {
       next NXTLINE if (! defined($usedlangs{$lang}));
     }
-    next if ($nfound);
     my $style = &getVal($l, "style", "stylelang");
     $style =~ s/^\\040//;
     my $fullname = &getVal($l, "fn", "fnl");
@@ -356,6 +401,22 @@ if (open(FI,  "$cmd |")) {
         next NXTLINE if ($fontname !~ /$fn/i);
       }
     }
+    my @charlist = ();
+    if (defined($options{Contains}) || exists($options{PrintCharset})) {
+      if ($l =~ / charset=\"([^\"]+)\"/) {
+        my @list = split(/\s+/, $1);
+        for my $e (@list) {
+          my ($l, $h) = split('-', $e);
+          $h = $l if (! defined($h));
+          push(@charlist, [hex($l), hex($h)]);
+        }
+      }
+      if (defined($options{Contains})) {
+        for my $g (@{$options{Contains}}) {
+          next NXTLINE if (! contains($g, \@charlist));
+        }
+      }
+    }
     my $props = "";
     my @errors = ();
     if (exists($options{PrintProperties}) || defined($options{Property}) || defined($options{NProperty})) {
@@ -378,6 +439,18 @@ if (open(FI,  "$cmd |")) {
     if (exists($options{PrintLangs})) {
       $props .= '(' . join(',', sort keys %usedlangs) . ')';
     }
+    if (exists($options{PrintCharset})) {
+      my @out = ();
+      for my $rE (@charlist) {
+        if ($rE->[0] != $rE->[1]) {
+          push(@out, $rE->[0] . '-' . $rE->[1]);
+        }
+        else {
+          push(@out, $rE->[0]);
+        }
+      }
+      $props .= '(' . join(',', @out) . ')';
+    }
     if (exists($options{PrintScripts}) || defined($options{Scripts}) || defined($options{NScripts}) || exists($options{Math})) {
       my @scripts = ();
       my $scripts = "";
@@ -799,3 +872,72 @@ sub correctstyle($)
   $style =~ s/  +/ /g;
   return($style);
 }
+
+# return list of unicode values of the input string
+#Allow input of intervals (e.g. 'a-z')
+sub decimalUnicode($)
+{
+  my ($a) = @_;
+  my @res = ();
+  # Convert to unicode chars first
+  while ($a =~ /^(.*)u\+(0?x[\da-f]+|\d+)(.*)$/i) {
+    my ($prev, $d, $post) = ($1, $2, $3);
+    if ($d =~ /^0?x(.+)$/) {
+      $d = hex($1);
+    }
+    my $chr = encode('utf-8', chr($d));
+    $a = $prev . $chr . $post;
+  }
+  # $a is now a string of unicode chars
+  my $u = decode('utf-8', $a);
+  my @a = split(//, $u);
+  my $interval = 0;
+  my $start = undef;
+  for my $x (@a) {
+    if ($x eq '-') {    # Interval
+      $interval = 1;
+      next;
+    }
+    if ($interval && defined($start)) {
+      if (ord($x) < $start) {
+        for (my $i = $start - 1; $i >= ord($x); $i--) {
+          push(@res, $i);
+        }
+      }
+      else {
+        for (my $i = $start + 1; $i <= ord($x); $i++) {
+          push(@res, $i);
+        }
+      }
+      $start = undef;
+    }
+    else {
+      $start = ord($x);
+      push(@res, $start);
+    }
+    $interval = 0;
+  }
+  return(@res);
+}
+
+
+# check if the glyph-value $d is contained
+# in one of the (sorted) intervals
+# Inputs as intervals
+sub contains($$)
+{
+  # ok if
+  # ...re0..........re1...
+  # ......start..end......
+  my ($ri, $rList) = @_;
+  my $start = $ri->[0];
+  my $end = $ri->[1];
+
+  for my $re (@{$rList}) {
+    next if ($re->[1] < $start);
+    # now we found a possible matching interval
+    return 1 if (($start >= $re->[0]) && ($end <= $re->[1]));
+    return 0;
+  }
+  return 0;
+}