]> git.lyx.org Git - lyx.git/blob - development/autotests/useSystemFonts.pl
ctests: invert ko splash lyx22x and lyx23x tests
[lyx.git] / development / autotests / useSystemFonts.pl
1 #! /usr/bin/env perl
2 # -*- mode: perl; -*-
3 #
4 # file useSystemFonts.pl
5 # 1.) Copies lyx-files to another location
6 # 2.) While copying,
7 #   2a.) searches for relative references to files and
8 #        replaces them with absolute ones
9 #   2b.) In order to be able to compile with luatex or xetex
10 #        changes default fonts to use non-tex-fonts instead
11 #
12 # Syntax: perl useSystemFonts.pl sourceFile destFile format
13 # Each param represents a path to a file
14 # sourceFile: full path to a lyx file
15 # destFile: destination path
16 #   Each subdocument will be copied into a subdirectory of dirname(destFile)
17 # format: any string of the form '[a-zA-Z0-9]+', e.g. pdf5
18 #
19 # This file is free software; you can redistribute it and/or
20 # modify it under the terms of the GNU General Public
21 # License as published by the Free Software Foundation; either
22 # version 2 of the License, or (at your option) any later version.
23 #
24 # This software is distributed in the hope that it will be useful,
25 # but WITHOUT ANY WARRANTY; without even the implied warranty of
26 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
27 # General Public License for more details.
28 #
29 # You should have received a copy of the GNU General Public
30 # License along with this software; if not, write to the Free Software
31 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
32 #
33 # Copyright (c) 2013 Kornel Benko <kornel@lyx.org>
34 #           (c) 2013 Scott Kostyshak <skotysh@lyx.org>
35
36 use strict;
37
38 BEGIN {
39   use File::Spec;
40   my $p = File::Spec->rel2abs( __FILE__ );
41   $p =~ s/[\/\\]?[^\/\\]+$//;
42   unshift(@INC, "$p");
43 }
44 use File::Basename;
45 use File::Path;
46 use File::Copy "cp";
47 use File::Temp qw/ :POSIX /;
48 use lyxStatus;
49
50 # Prototypes
51 sub printCopiedDocuments($);
52 sub interpretedCopy($$$$);
53 sub copyFoundSubdocuments($);
54 sub copyJob($$);
55 sub isrelativeFix($$$);
56 sub isrelative($$$);
57 sub createTemporaryFileName($$);
58 sub copyJobPending($$);
59 sub addNewJob($$$$$);
60 sub addFileCopyJob($$$$);
61 sub getNewNameOf($$);
62 sub getlangs($$);
63 sub simplifylangs($);
64 sub getLangEntry();
65
66 # convert lyx file to be compilable with xetex
67
68 my ($source, $dest, $format, $fontT, $encodingT, $languageFile, $rest) = @ARGV;
69 my %encodings = ();      # Encoding with TeX fonts, depending on language tag
70
71 diestack("Too many arguments") if (defined($rest));
72 diestack("Sourcefilename not defined") if (! defined($source));
73 diestack("Destfilename not defined") if (! defined($dest));
74 diestack("Format (e.g. pdf4) not defined") if (! defined($format));
75 diestack("Font type (e.g. texF) not defined") if (! defined($fontT));
76 diestack("Encoding (e.g. ascii) not defined") if (! defined($encodingT));
77
78 $source = File::Spec->rel2abs($source);
79 $dest = File::Spec->rel2abs($dest);
80
81 my %font = ();
82 my $lang = "main";
83 if ($source =~ /\/([a-z][a-z](_[A-Z][A-Z])?)[\/_]/) {
84   $lang = $1;
85 }
86
87 my $inputEncoding = undef;
88 if ($fontT eq "systemF") {
89   if ($lang =~ /^(ru|uk|sk|el)$/) {
90     $font{roman} = "DejaVu Serif";
91     $font{sans} = "DejaVu Sans";
92     $font{typewriter} = "DejaVu Sans Mono";
93   }
94   elsif ($lang =~ /^(he)$/) {
95     $font{roman} = "FreeSans";
96     $font{sans} = "FreeSans";
97     $font{typewriter} = "FreeSans";
98   }
99   elsif ($lang eq "fa") {
100     $font{roman} = "FreeFarsi";
101     $font{sans} = "FreeFarsi";
102     $font{typewriter} = "FreeFarsi Monospace";
103   }
104   elsif ($lang eq "zh_CN") {
105     $font{roman} = "WenQuanYi Micro Hei";
106     $font{sans} = "WenQuanYi Micro Hei";
107     $font{typewriter} = "WenQuanYi Micro Hei";
108   }
109   elsif ($lang eq "ko" ) {
110     $font{roman} = "NanumGothic"; # NanumMyeongjo, NanumGothic Eco, NanumGothicCoding
111     $font{sans} = "NanumGothic";
112     $font{typewriter} = "NanumGothic";
113   }
114   elsif ($lang eq "ar" ) {
115     # available in 'fonts-sil-scheherazade' package
116     $font{roman} = "Scheherazade";
117     $font{sans} = "Scheherazade";
118     $font{typewriter} = "Scheherazade";
119   }
120   else {
121     # default system fonts
122     $font{roman} = "FreeSerif";
123     $font{sans} = "FreeSans";
124     $font{typewriter} = "FreeMono";
125   }
126 }
127 elsif ($encodingT ne "default") {
128   # set input encoding to the requested value
129   $inputEncoding = {
130         "search" => '.*', # this will be substituted from '\inputencoding'-line
131         "out" => $encodingT,
132     };
133 }
134 elsif (0) { # set to '1' to enable setting of inputencoding
135   # use tex font here
136   my %encoding = ();
137   if (defined($languageFile)) {
138     # The 2 lines below does not seem to have any effect
139     #&getlangs($languageFile, \%encoding);
140     #&simplifylangs(\%encoding);
141   }
142   if ($format =~ /^(pdf4)$/) { # xelatex
143     # set input encoding to 'ascii' always
144     $inputEncoding = {
145       "search" => '.*', # this will be substituted from '\inputencoding'-line
146       "out" => "ascii",
147     };
148   }
149   elsif ($format =~ /^(dvi3|pdf5)$/) { # (dvi)?lualatex
150     # when to set input encoding to 'ascii'?
151     if (defined($encoding{$lang})) {
152       $inputEncoding = {
153         "search" => '.*', # this will be substituted from '\inputencoding'-line
154         "out" => $encoding{$lang},
155       };
156     }
157   }
158 }
159
160 my $sourcedir = dirname($source);
161 my $destdir = dirname($dest);
162 if (! -d $destdir) {
163   diestack("could not make dir \"$destdir\"") if (! mkpath $destdir);
164 }
165
166 my $destdirOfSubdocuments;
167 {
168   my ($name, $pat, $suffix) = fileparse($source, qr/\.[^.]*/);
169   my $ext = $format . "_$lang";
170   $destdirOfSubdocuments = "$destdir/tmp_$ext" . "_$name"; # Global var, something TODO here
171 }
172
173 if(-d $destdirOfSubdocuments) {
174   rmtree($destdirOfSubdocuments);
175 }
176 mkpath($destdirOfSubdocuments); #  for possibly included files
177
178 my %IncludedFiles = ();
179 my %type2hash = (
180   "copy_only" => "copyonly",
181   "interpret" => "interpret");
182
183 addNewJob($source, $dest, "interpret", {}, \%IncludedFiles);
184
185 copyFoundSubdocuments(\%IncludedFiles);
186
187 #printCopiedDocuments(\%IncludedFiles);
188
189 exit(0);
190 ###########################################################
191
192 sub printCopiedDocuments($)
193 {
194   my ($rFiles) = @_;
195   for my $k (keys %{$rFiles}) {
196     my $rJob = $rFiles->{$k};
197     for my $j ( values %type2hash) {
198       if (defined($rJob->{$j})) {
199         print "$j: $k->$rJob->{$j}, " . $rJob->{$j . "copied"} . "\n";
200       }
201     }
202   }
203 }
204
205 sub interpretedCopy($$$$)
206 {
207   my ($source, $dest, $destdirOfSubdocuments, $rFiles) = @_;
208   my $sourcedir = dirname($source);
209   my $res = 0;
210
211   diestack("could not read \"$source\"") if (!open(FI, $source));
212   diestack("could not write \"$dest\"") if (! open(FO, '>', $dest));
213
214   initLyxStack(\%font, $fontT, $inputEncoding);
215
216   my $fi_line_no = 0;
217   my @path_errors = ();
218   while (my $l = <FI>) {
219     $fi_line_no += 1;
220     $l =~ s/[\n\r]+$//;
221     #chomp($l);
222     my $rStatus = checkLyxLine($l);
223     if ($rStatus->{found}) {
224       my $rF = $rStatus->{result};
225       if ($rStatus->{"filetype"} eq "replace_only") {
226         # e.g. if no files involved (font chage etc)
227         $l = join('', @{$rF});
228       }
229       else {
230         my $filelist = $rStatus->{filelist};
231         my $fidx = $rStatus->{fileidx};
232         my $separator = $rStatus->{"separator"};
233         my $foundrelative = 0;
234         for my $f (@{$filelist}) {
235           my @isrel = isrelative($f,
236                                   $sourcedir,
237                                   $rStatus->{ext});
238           if ($isrel[0]) {
239             $foundrelative = 1;
240             my $ext = $isrel[1];
241             if ($rStatus->{"filetype"} eq "prefix_only") {
242               $f = getNewNameOf("$sourcedir/$f", $rFiles);
243             }
244             else {
245               my ($newname, $res1);
246               ($newname, $res1) = addFileCopyJob("$sourcedir/$f$ext",
247                                                   "$destdirOfSubdocuments",
248                                                   $rStatus->{"filetype"},
249                                                   $rFiles);
250               print "Added ($res1) file \"$sourcedir/$f$ext\" to be copied to \"$newname\"\n";
251               if ($ext ne "") {
252                 $newname =~ s/$ext$//;
253               }
254               $f = $newname;
255               $res += $res1;
256             }
257           }
258           else {
259             if (! -e "$f") {
260               # Non relative (e.g. with absolute path) file should exist
261               if ($rStatus->{"filetype"} eq "interpret") {
262                 # filetype::interpret should be interpreted by lyx or latex and therefore emit error
263                 # We prinnt a warning instead
264                 print "WARNING: Interpreted file \"$f\" not found, at \"$source:$fi_line_no\"\n";
265               }
266               elsif ($rStatus->{"filetype"} eq "prefix_only") {
267                 # filetype::prefix_only should be interpreted by latex
268                 print "WARNING: Prefixed file \"$f\" not found, at \"$source:$fi_line_no\"\n";
269               }
270               else {
271                 # Collect the path-error-messages
272                 push(@path_errors, "File \"$f(" . $rStatus->{"filetype"} . ")\" not found, at \"$source:$fi_line_no\"");
273               }
274             }
275           }
276         }
277         if ($foundrelative) {
278           $rF->[$fidx] = join($separator, @{$filelist});
279           $l = join('', @{$rF});
280         }
281       }
282     }
283     print FO "$l\n";
284   }
285   close(FI);
286   close(FO);
287   if (@path_errors > 0) {
288     for my $entry (@path_errors) {
289       print "ERROR: $entry\n";
290     }
291     diestack("Aborted because of path errors in \"$source\"");
292   }
293
294   closeLyxStack();
295   return($res);
296 }
297
298 sub copyFoundSubdocuments($)
299 {
300   my ($rFiles) = @_;
301   my $res = 0;
302   do {
303     $res = 0;
304     my %copylist = ();
305
306     for my $filename (keys  %{$rFiles}) {
307       next if (! copyJobPending($filename, $rFiles));
308       $copylist{$filename} = 1;
309     }
310     for my $f (keys %copylist) {
311       # Second loop needed, because here $rFiles may change
312       my ($res1, @destfiles) = copyJob($f, $rFiles);
313       $res += $res1;
314       for my $destfile (@destfiles) {
315         print "res1 = $res1 for \"$f\" to be copied to $destfile\n";
316       }
317     }
318   } while($res > 0);            #  loop, while $rFiles changed
319 }
320
321 sub copyJob($$)
322 {
323   my ($source, $rFiles) = @_;
324   my $sourcedir = dirname($source);
325   my $res = 0;
326   my @dest = ();
327
328   for my $k (values %type2hash) {
329     if ($rFiles->{$source}->{$k}) {
330       if (! $rFiles->{$source}->{$k . "copied"}) {
331         $rFiles->{$source}->{$k . "copied"} = 1;
332         my $dest = $rFiles->{$source}->{$k};
333         push(@dest, $dest);
334         if ($k eq "copyonly") {
335           diestack("Could not copy \"$source\" to \"$dest\"") if (! cp($source, $dest));
336         }
337         else {
338           interpretedCopy($source, $dest, $destdirOfSubdocuments, $rFiles);
339         }
340         $res += 1;
341       }
342     }
343   }
344   return($res, @dest);
345 }
346
347 # Trivial check
348 sub isrelativeFix($$$)
349 {
350   my ($f, $sourcedir, $ext) = @_;
351
352   return(1, $ext) if  (-e "$sourcedir/$f$ext");
353   return(0,0);
354 }
355
356 sub isrelative($$$)
357 {
358   my ($f, $sourcedir, $ext) = @_;
359
360   if (ref($ext) eq "ARRAY") {
361     for my $ext2 (@{$ext}) {
362       my @res = isrelativeFix($f, $sourcedir, $ext2);
363       if ($res[0]) {
364         return(@res);
365       }
366     }
367     return(0,0);
368   }
369   else {
370     return(isrelativeFix($f, $sourcedir, $ext));
371   }
372 }
373
374 sub createTemporaryFileName($$)
375 {
376   my ($source, $destdir) = @_;
377
378   # get the basename to be used for the template
379   my ($name, $path, $suffix) = fileparse($source, qr/\.[^.]*/);
380   #print "source = $source, name = $name, path = $path, suffix = $suffix\n";
381   my $template = "xx_$name" . "_";
382   my $fname = File::Temp::tempnam($destdir, $template);
383
384   # Append extension from source
385   if ($suffix ne "") {
386     $fname .= "$suffix";
387   }
388   return($fname);
389 }
390
391 # Check, if file not copied yet
392 sub copyJobPending($$)
393 {
394   my ($f, $rFiles) = @_;
395   for my $t (values %type2hash) {
396     if (defined($rFiles->{$f}->{$t})) {
397       return 1 if (! $rFiles->{$f}->{$t . "copied"});
398     }
399   }
400   return 0;
401 }
402
403 sub addNewJob($$$$$)
404 {
405   my ($source, $newname, $hashname, $rJob, $rFiles) = @_;
406
407   $rJob->{$hashname} = $newname;
408   $rJob->{$hashname . "copied"} = 0;
409   $rFiles->{$source} = $rJob;
410 }
411
412 sub addFileCopyJob($$$$)
413 {
414   my ($source, $destdirOfSubdocuments, $filetype, $rFiles) = @_;
415   my ($res, $newname) = (0, undef);
416   my $rJob = $rFiles->{$source};
417
418   my $hashname = $type2hash{$filetype};
419   if (! defined($hashname)) {
420     diestack("unknown filetype \"$filetype\"");
421   }
422   if (!defined($rJob->{$hashname})) {
423     addNewJob($source,
424                createTemporaryFileName($source, $destdirOfSubdocuments),
425                "$hashname", $rJob, $rFiles);
426     $res = 1;
427   }
428   $newname = $rJob->{$hashname};
429   return($newname, $res);
430 }
431
432 sub getNewNameOf($$)
433 {
434   my ($f, $rFiles) = @_;
435   my $resultf = $f;
436
437   if (defined($rFiles->{$f})) {
438     for my $t (values %type2hash) {
439       if (defined($rFiles->{$f}->{$t})) {
440         $resultf = $rFiles->{$f}->{$t};
441         last;
442       }
443     }
444   }
445   return($resultf);
446 }
447
448 sub getlangs($$)
449 {
450   my ($languagefile, $rencoding) = @_;
451
452   if (open(FI, $languagefile)) {
453     while (my $l = <FI>) {
454       if ($l =~ /^Language/) {
455         my ($lng, $enc) = &getLangEntry();
456         if (defined($lng)) {
457           $rencoding->{$lng} = $enc;
458         }
459       }
460     }
461     close(FI);
462   }
463 }
464
465 sub simplifylangs($)
466 {
467   my ($rencoding) = @_;
468   my $base = "";
469   my $enc = "";
470   my $differ = 0;
471   my @klist = ();
472   my @klist2 = ();
473   for my $k (reverse sort keys %{$rencoding}) {
474     my @tag = split('_', $k);
475     if ($tag[0] eq $base) {
476       push(@klist, $k);
477       if ($rencoding->{$k} ne $enc) {
478         $differ = 1;
479       }
480     }
481     else {
482       # new base, check that old base was OK
483       if ($base ne "") {
484         if ($differ == 0) {
485           $rencoding->{$base} = $enc;
486           push(@klist2, @klist);
487         }
488       }
489       @klist = ($k);
490       $base = $tag[0];
491       $enc = $rencoding->{$k};
492       $differ = 0;
493     }
494   }
495   if ($base ne "") {
496     # close handling for last entry too
497     if ($differ == 0) {
498       $rencoding->{$base} = $enc;
499       push(@klist2, @klist);
500     }
501   }
502   for my $k (@klist2) {
503     delete($rencoding->{$k});
504   }
505 }
506
507 sub getLangEntry()
508 {
509   my ($lng, $enc) = (undef, undef);
510   while (my $l = <FI>) {
511     chomp($l);
512     if ($l =~ /^\s*Encoding\s+([^ ]+)\s*$/) {
513       $enc = $1;
514     }
515     elsif ($l =~ /^\s*LangCode\s+([^ ]+)\s*$/) {
516       $lng = $1;
517     }
518     elsif ($l =~ /^\s*End\s*$/) {
519       last;
520     }
521   }
522   if (defined($lng) && defined($enc)) {
523     return($lng, $enc);
524   }
525   else {
526     return(undef, undef);
527   }
528 }