]> git.lyx.org Git - lyx.git/blobdiff - development/autotests/useSystemFonts.pl
Customization: correct some color names.
[lyx.git] / development / autotests / useSystemFonts.pl
index c5a59c70d51bf67f84025ce65bc20e6f66aa7980..f0183fa776fd5ac6d6daa62802b80bdcf58e4556 100644 (file)
@@ -47,55 +47,118 @@ use File::Copy "cp";
 use File::Temp qw/ :POSIX /;
 use lyxStatus;
 
+# Prototypes
+sub printCopiedDocuments($);
+sub interpretedCopy($$$$);
+sub copyFoundSubdocuments($);
+sub copyJob($$);
+sub isrelativeFix($$$);
+sub isrelative($$$);
+sub createTemporaryFileName($$);
+sub copyJobPending($$);
+sub addNewJob($$$$$);
+sub addFileCopyJob($$$$);
+sub getNewNameOf($$);
+sub getlangs($$);
+sub simplifylangs($);
+sub getLangEntry();
+
 # convert lyx file to be compilable with xetex
 
-my ($source, $dest, $format, $rest) = @ARGV;
+my ($source, $dest, $format, $fontT, $languageFile, $rest) = @ARGV;
+my %encodings = ();      # Encoding with TeX fonts, depending on language tag
 
-&diestack("Too many arguments") if (defined($rest));
-&diestack("Sourcefilename not defined") if (! defined($source));
-&diestack("Destfilename not defined") if (! defined($dest));
-&diestack("Format (e.g. pdf4) not defined") if (! defined($format));
+diestack("Too many arguments") if (defined($rest));
+diestack("Sourcefilename not defined") if (! defined($source));
+diestack("Destfilename not defined") if (! defined($dest));
+diestack("Format (e.g. pdf4) not defined") if (! defined($format));
+diestack("Font type (e.g. texF) not defined") if (! defined($fontT));
 
 $source = File::Spec->rel2abs($source);
 $dest = File::Spec->rel2abs($dest);
 
 my %font = ();
-
-if ($source =~ /\/(he|el|ru|uk)\//) {
-  $font{roman} = "FreeSans";
-  $font{sans} = "FreeSans";
-  $font{typewriter} = "FreeSans";
+my $lang = "main";
+if ($source =~ /\/([a-z][a-z](_[A-Z][A-Z])?)[\/_]/) {
+  $lang = $1;
 }
-elsif ($source =~ /\/fa\//) {
-  $font{roman} = "FreeFarsi";
-  $font{sans} = "FreeFarsi";
-  $font{typewriter} = "FreeFarsi Monospace";
-}
-elsif ($source =~ /\/zh_CN\//) {
-  $font{roman} = "FreeSans";
-  $font{sans} = "WenQuanYi Micro Hei";
-  $font{typewriter} = "WenQuanYi Micro Hei";
+
+my $inputEncoding = undef;
+if ($fontT eq "systemF") {
+  if ($lang =~ /^(ru|uk|sk|el)$/) {
+    $font{roman} = "DejaVu Serif";
+    $font{sans} = "DejaVu Sans";
+    $font{typewriter} = "DejaVu Sans Mono";
+  }
+  elsif ($lang =~ /^(he)$/) {
+    $font{roman} = "FreeSans";
+    $font{sans} = "FreeSans";
+    $font{typewriter} = "FreeSans";
+  }
+  elsif ($lang eq "fa") {
+    $font{roman} = "FreeFarsi";
+    $font{sans} = "FreeFarsi";
+    $font{typewriter} = "FreeFarsi Monospace";
+  }
+  elsif ($lang eq "zh_CN") {
+    $font{roman} = "WenQuanYi Micro Hei";
+    $font{sans} = "WenQuanYi Micro Hei";
+    $font{typewriter} = "WenQuanYi Micro Hei";
+  }
+  elsif ($lang eq "ko" ) {
+    $font{roman} = "NanumGothic"; # NanumMyeongjo, NanumGothic Eco, NanumGothicCoding
+    $font{sans} = "NanumGothic";
+    $font{typewriter} = "NanumGothic";
+  }
+  elsif ($lang eq "ar" ) {
+    # available in 'fonts-sil-scheherazade' package
+    $font{roman} = "Scheherazade";
+    $font{sans} = "Scheherazade";
+    $font{typewriter} = "Scheherazade";
+  }
+  else {
+    # default system fonts
+    $font{roman} = "FreeSerif";
+    $font{sans} = "FreeSans";
+    $font{typewriter} = "FreeMono";
+  }
 }
-else {
-  # Nothing to do?
+elsif (0) { # set to '1' to enable setting of inputencoding
+  # use tex font here
+  my %encoding = ();
+  if (defined($languageFile)) {
+    # The 2 lines below does not seem to have any effect
+    #&getlangs($languageFile, \%encoding);
+    #&simplifylangs(\%encoding);
+  }
+  if ($format =~ /^(pdf4)$/) { # xelatex
+    # set input encoding to 'ascii' always
+    $inputEncoding = {
+      "search" => '.*', # this will be substituted from '\inputencoding'-line
+      "out" => "ascii",
+    };
+  }
+  elsif ($format =~ /^(dvi3|pdf5)$/) { # (dvi)?lualatex
+    # when to set input encoding to 'ascii'?
+    if (defined($encoding{$lang})) {
+      $inputEncoding = {
+       "search" => '.*', # this will be substituted from '\inputencoding'-line
+       "out" => $encoding{$lang},
+      };
+    }
+  }
 }
 
 my $sourcedir = dirname($source);
 my $destdir = dirname($dest);
 if (! -d $destdir) {
-  &diestack("could not make dir \"$destdir\"") if (! mkdir $destdir);
+  diestack("could not make dir \"$destdir\"") if (! mkdir $destdir);
 }
 
 my $destdirOfSubdocuments;
 {
   my ($name, $pat, $suffix) = fileparse($source, qr/\.[^.]*/);
-  my $ext = $format;
-  if ($source =~ /\/([a-z][a-z](_[A-Z][A-Z])?)\//) {
-    $ext .= "_$1";
-  }
-  else {
-    $ext .= "_main";
-  }
+  my $ext = $format . "_$lang";
   $destdirOfSubdocuments = "$destdir/tmp_$ext" . "_$name"; # Global var, something TODO here
 }
 
@@ -109,11 +172,11 @@ my %type2hash = (
   "copy_only" => "copyonly",
   "interpret" => "interpret");
 
-&addNewJob($source, $dest, "interpret", {}, \%IncludedFiles);
+addNewJob($source, $dest, "interpret", {}, \%IncludedFiles);
 
-&copyFoundSubdocuments(\%IncludedFiles);
+copyFoundSubdocuments(\%IncludedFiles);
 
-#&printCopiedDocuments(\%IncludedFiles);
+#printCopiedDocuments(\%IncludedFiles);
 
 exit(0);
 ###########################################################
@@ -137,14 +200,15 @@ sub interpretedCopy($$$$)
   my $sourcedir = dirname($source);
   my $res = 0;
 
-  &diestack("could not read \"$source\"") if (!open(FI, $source));
-  &diestack("could not write \"$dest\"") if (! open(FO, '>', $dest));
+  diestack("could not read \"$source\"") if (!open(FI, $source));
+  diestack("could not write \"$dest\"") if (! open(FO, '>', $dest));
 
-  &initLyxStack(\%font);
+  initLyxStack(\%font, $fontT, $inputEncoding);
 
   while (my $l = <FI>) {
-    chomp($l);
-    my $rStatus = &checkLyxLine($l);
+    $l =~ s/[\n\r]+$//;
+    #chomp($l);
+    my $rStatus = checkLyxLine($l);
     if ($rStatus->{found}) {
       my $rF = $rStatus->{result};
       if ($rStatus->{"filetype"} eq "replace_only") {
@@ -157,18 +221,18 @@ sub interpretedCopy($$$$)
        my $separator = $rStatus->{"separator"};
        my $foundrelative = 0;
        for my $f (@{$filelist}) {
-         my @isrel = &isrelative($f,
+         my @isrel = isrelative($f,
                                  $sourcedir,
                                  $rStatus->{ext});
          if ($isrel[0]) {
            $foundrelative = 1;
            my $ext = $isrel[1];
            if ($rStatus->{"filetype"} eq "prefix_only") {
-             $f = &getNewNameOf("$sourcedir/$f", $rFiles);
+             $f = getNewNameOf("$sourcedir/$f", $rFiles);
            }
            else {
              my ($newname, $res1);
-             ($newname, $res1) = &addFileCopyJob("$sourcedir/$f$ext",
+             ($newname, $res1) = addFileCopyJob("$sourcedir/$f$ext",
                                                  "$destdirOfSubdocuments",
                                                  $rStatus->{"filetype"},
                                                  $rFiles);
@@ -192,7 +256,7 @@ sub interpretedCopy($$$$)
   close(FI);
   close(FO);
 
-  &closeLyxStack();
+  closeLyxStack();
   return($res);
 }
 
@@ -205,12 +269,12 @@ sub copyFoundSubdocuments($)
     my %copylist = ();
 
     for my $filename (keys  %{$rFiles}) {
-      next if (! &copyJobPending($filename, $rFiles));
+      next if (! copyJobPending($filename, $rFiles));
       $copylist{$filename} = 1;
     }
     for my $f (keys %copylist) {
       # Second loop needed, because here $rFiles may change
-      my ($res1, @destfiles) = &copyJob($f, $rFiles);
+      my ($res1, @destfiles) = copyJob($f, $rFiles);
       $res += $res1;
       for my $destfile (@destfiles) {
        print "res1 = $res1 for \"$f\" to be copied to $destfile\n";
@@ -233,10 +297,10 @@ sub copyJob($$)
        my $dest = $rFiles->{$source}->{$k};
        push(@dest, $dest);
        if ($k eq "copyonly") {
-         &diestack("Could not copy \"$source\" to \"$dest\"") if (! cp($source, $dest));
+         diestack("Could not copy \"$source\" to \"$dest\"") if (! cp($source, $dest));
        }
        else {
-         &interpretedCopy($source, $dest, $destdirOfSubdocuments, $rFiles);
+         interpretedCopy($source, $dest, $destdirOfSubdocuments, $rFiles);
        }
        $res += 1;
       }
@@ -260,7 +324,7 @@ sub isrelative($$$)
 
   if (ref($ext) eq "ARRAY") {
     for my $ext2 (@{$ext}) {
-      my @res = &isrelativeFix($f, $sourcedir, $ext2);
+      my @res = isrelativeFix($f, $sourcedir, $ext2);
       if ($res[0]) {
        return(@res);
       }
@@ -268,7 +332,7 @@ sub isrelative($$$)
     return(0,0);
   }
   else {
-    return(&isrelativeFix($f, $sourcedir, $ext));
+    return(isrelativeFix($f, $sourcedir, $ext));
   }
 }
 
@@ -301,7 +365,7 @@ sub copyJobPending($$)
   return 0;
 }
 
-sub addNewJob($$$)
+sub addNewJob($$$$$)
 {
   my ($source, $newname, $hashname, $rJob, $rFiles) = @_;
 
@@ -318,11 +382,11 @@ sub addFileCopyJob($$$$)
 
   my $hashname = $type2hash{$filetype};
   if (! defined($hashname)) {
-    &diestack("unknown filetype \"$filetype\"");
+    diestack("unknown filetype \"$filetype\"");
   }
   if (!defined($rJob->{$hashname})) {
-    &addNewJob($source,
-              &createTemporaryFileName($source, $destdirOfSubdocuments),
+    addNewJob($source,
+              createTemporaryFileName($source, $destdirOfSubdocuments),
               "$hashname", $rJob, $rFiles);
     $res = 1;
   }
@@ -345,3 +409,85 @@ sub getNewNameOf($$)
   }
   return($resultf);
 }
+
+sub getlangs($$)
+{
+  my ($languagefile, $rencoding) = @_;
+
+  if (open(FI, $languagefile)) {
+    while (my $l = <FI>) {
+      if ($l =~ /^Language/) {
+        my ($lng, $enc) = &getLangEntry();
+        if (defined($lng)) {
+          $rencoding->{$lng} = $enc;
+        }
+      }
+    }
+    close(FI);
+  }
+}
+
+sub simplifylangs($)
+{
+  my ($rencoding) = @_;
+  my $base = "";
+  my $enc = "";
+  my $differ = 0;
+  my @klist = ();
+  my @klist2 = ();
+  for my $k (reverse sort keys %{$rencoding}) {
+    my @tag = split('_', $k);
+    if ($tag[0] eq $base) {
+      push(@klist, $k);
+      if ($rencoding->{$k} ne $enc) {
+       $differ = 1;
+      }
+    }
+    else {
+      # new base, check that old base was OK
+      if ($base ne "") {
+       if ($differ == 0) {
+         $rencoding->{$base} = $enc;
+         push(@klist2, @klist);
+       }
+      }
+      @klist = ($k);
+      $base = $tag[0];
+      $enc = $rencoding->{$k};
+      $differ = 0;
+    }
+  }
+  if ($base ne "") {
+    # close handling for last entry too
+    if ($differ == 0) {
+      $rencoding->{$base} = $enc;
+      push(@klist2, @klist);
+    }
+  }
+  for my $k (@klist2) {
+    delete($rencoding->{$k});
+  }
+}
+
+sub getLangEntry()
+{
+  my ($lng, $enc) = (undef, undef);
+  while (my $l = <FI>) {
+    chomp($l);
+    if ($l =~ /^\s*Encoding\s+([^ ]+)\s*$/) {
+      $enc = $1;
+    }
+    elsif ($l =~ /^\s*LangCode\s+([^ ]+)\s*$/) {
+      $lng = $1;
+    }
+    elsif ($l =~ /^\s*End\s*$/) {
+      last;
+    }
+  }
+  if (defined($lng) && defined($enc)) {
+    return($lng, $enc);
+  }
+  else {
+    return(undef, undef);
+  }
+}