]> git.lyx.org Git - lyx.git/blob - development/autotests/useSystemFonts.pl
Fix screen display of parts and chapters in default classes
[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.) Changes default fonts to use non-tex-fonts
10 #
11 # Syntax: perl useSystemFonts.pl sourceFile destFile format
12 # Each param represents a path to a file
13 # sourceFile: full path to a lyx file
14 # destFile: destination path
15 #   Each subdocument will be copied into a subdirectory of dirname(destFile)
16 # format: any string of the form '[a-zA-Z0-9]+', e.g. pdf5
17 #
18 # This file is free software; you can redistribute it and/or
19 # modify it under the terms of the GNU General Public
20 # License as published by the Free Software Foundation; either
21 # version 2 of the License, or (at your option) any later version.
22 #
23 # This software is distributed in the hope that it will be useful,
24 # but WITHOUT ANY WARRANTY; without even the implied warranty of
25 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
26 # General Public License for more details.
27 #
28 # You should have received a copy of the GNU General Public
29 # License along with this software; if not, write to the Free Software
30 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
31 #
32 # Copyright (c) 2013 Kornel Benko <kornel@lyx.org>
33 #           (c) 2013 Scott Kostyshak <skotysh@lyx.org>
34
35 use strict;
36
37 BEGIN {
38   use File::Spec;
39   my $p = File::Spec->rel2abs( __FILE__ );
40   $p =~ s/[\/\\]?[^\/\\]+$//;
41   unshift(@INC, "$p");
42 }
43 use File::Basename;
44 use File::Path;
45 use File::Copy "cp";
46 use File::Temp qw/ :POSIX /;
47 use lyxStatus;
48
49 # Prototypes
50 sub printCopiedDocuments($);
51 sub interpretedCopy($$$$);
52 sub copyFoundSubdocuments($);
53 sub copyJob($$);
54 sub isrelativeFix($$$);
55 sub isrelative($$$);
56 sub createTemporaryFileName($$$);
57 sub copyJobPending($$);
58 sub addNewJob($$$$$);
59 sub addFileCopyJob($$$$$);
60 sub getNewNameOf($$);
61 sub getlangs($$);
62 sub simplifylangs($);
63 sub getLangEntry();
64
65 # convert lyx file to be compilable with xetex
66
67 my ($source, $dest, $format, $fontT, $encodingT, $languageFile, $rest) = @ARGV;
68 my %encodings = ();      # Encoding with TeX fonts, depending on language tag
69
70 diestack("Too many arguments") if (defined($rest));
71 diestack("Sourcefilename not defined") if (! defined($source));
72 diestack("Destfilename not defined") if (! defined($dest));
73 diestack("Format (e.g. pdf4) not defined") if (! defined($format));
74 diestack("Font type (e.g. texF) not defined") if (! defined($fontT));
75 diestack("Encoding (e.g. ascii) not defined") if (! defined($encodingT));
76
77 $source = File::Spec->rel2abs($source);
78 $dest = File::Spec->rel2abs($dest);
79
80 my %font = ();
81 my $lang = "main";
82 if ($source =~ /\/([a-z][a-z](_[A-Z][A-Z])?)[\/_]/) {
83   $lang = $1;
84 }
85
86 my $inputEncoding = undef;
87 if ($fontT eq "systemF") {
88 }
89 elsif ($encodingT ne "default") {
90   # set input encoding to the requested value
91   $inputEncoding = {
92         "search" => '.*', # this will be substituted from '\inputencoding'-line
93         "out" => $encodingT,
94     };
95 }
96 elsif (0) { # set to '1' to enable setting of inputencoding
97   # use tex font here
98   my %encoding = ();
99   if (defined($languageFile)) {
100     # The 2 lines below does not seem to have any effect
101     #&getlangs($languageFile, \%encoding);
102     #&simplifylangs(\%encoding);
103   }
104   if ($format =~ /^(pdf4)$/) { # xelatex
105     # set input encoding to 'ascii' always
106     $inputEncoding = {
107       "search" => '.*', # this will be substituted from '\inputencoding'-line
108       "out" => "ascii",
109     };
110   }
111   elsif ($format =~ /^(dvi3|pdf5)$/) { # (dvi)?lualatex
112     # when to set input encoding to 'ascii'?
113     if (defined($encoding{$lang})) {
114       $inputEncoding = {
115         "search" => '.*', # this will be substituted from '\inputencoding'-line
116         "out" => $encoding{$lang},
117       };
118     }
119   }
120 }
121
122 my $sourcedir = dirname($source);
123 my $destdir = dirname($dest);
124 if (! -d $destdir) {
125   diestack("could not make dir \"$destdir\"") if (! mkpath $destdir);
126 }
127
128 my $destdirOfSubdocuments;
129 {
130   my ($name, $pat, $suffix) = fileparse($source, qr/\.[^.]*/);
131   my $ext = $format . "-$lang";
132   $name =~ s/[%_]/-/g;
133   $destdirOfSubdocuments = "$destdir/tmp-$ext" . "-$name"; # Global var, something TODO here
134 }
135
136 if(-d $destdirOfSubdocuments) {
137   rmtree($destdirOfSubdocuments);
138 }
139 mkpath($destdirOfSubdocuments); #  for possibly included files
140
141 my %IncludedFiles = ();
142 my %type2hash = (
143   "copy_only" => "copyonly",
144   "interpret" => "interpret");
145
146 addNewJob($source, $dest, "interpret", {}, \%IncludedFiles);
147
148 copyFoundSubdocuments(\%IncludedFiles);
149
150 #printCopiedDocuments(\%IncludedFiles);
151
152 exit(0);
153 ###########################################################
154
155 sub printCopiedDocuments($)
156 {
157   my ($rFiles) = @_;
158   for my $k (keys %{$rFiles}) {
159     my $rJob = $rFiles->{$k};
160     for my $j ( values %type2hash) {
161       if (defined($rJob->{$j})) {
162         print "$j: $k->$rJob->{$j}, " . $rJob->{$j . "copied"} . "\n";
163       }
164     }
165   }
166 }
167
168 sub interpretedCopy($$$$)
169 {
170   my ($source, $dest, $destdirOfSubdocuments, $rFiles) = @_;
171   my $sourcedir = dirname($source);
172   my $res = 0;
173
174   diestack("could not read \"$source\"") if (!open(FI, $source));
175   diestack("could not write \"$dest\"") if (! open(FO, '>', $dest));
176
177   initLyxStack(\%font, $fontT, $inputEncoding);
178
179   my $fi_line_no = 0;
180   my @path_errors = ();
181   while (my $l = <FI>) {
182     $fi_line_no += 1;
183     $l =~ s/[\n\r]+$//;
184     #chomp($l);
185     my $rStatus = checkLyxLine($l, $sourcedir);
186     if ($rStatus->{found}) {
187       my $rF = $rStatus->{result};
188       if ($rStatus->{"filetype"} eq "replace_only") {
189         # e.g. if no files involved (font chage etc)
190         $l = join('', @{$rF});
191       }
192       else {
193         my $filelist = $rStatus->{filelist};
194         my $fidx = $rStatus->{fileidx};
195         my $separator = $rStatus->{"separator"};
196         my $foundrelative = 0;
197         for my $f (@{$filelist}) {
198           my @isrel = isrelative($f,
199                                   $sourcedir,
200                                   $rStatus->{ext});
201           if ($isrel[0]) {
202             $foundrelative = 1;
203             my $ext = $isrel[1];
204             if ($rStatus->{"filetype"} eq "prefix_only") {
205               $f = getNewNameOf("$sourcedir/$f", $rFiles);
206             }
207             else {
208               my ($newname, $res1);
209               my @extlist = ();
210               if (ref($rStatus->{ext}) eq "ARRAY") {
211                 my @extlist = @{$rStatus->{ext}};
212                 my $created = 0;
213                 for my $extx (@extlist) {
214                   if (-e "$sourcedir/$f$extx") {
215                     ($newname, $res1) = addFileCopyJob("$sourcedir/$f$extx",
216                                                        "$destdirOfSubdocuments",
217                                                        $rStatus->{"filetype"},
218                                                        $rFiles, $created);
219                     print "Added ($res1) file \"$sourcedir/$f$extx\" to be copied to \"$newname\"\n";
220                     if (!$created && $extx ne "") {
221                       $newname =~ s/$extx$//;
222                     }
223                     $created = 1;
224                   }
225                 }
226                 print "WARNING: No prefixed file.(" . join('|', @extlist) . ") seens to exist, at \"$source:$fi_line_no\"\n" if (!$created);
227               }
228               else {
229               ($newname, $res1) = addFileCopyJob("$sourcedir/$f$ext",
230                                                   "$destdirOfSubdocuments",
231                                                   $rStatus->{"filetype"},
232                                                    $rFiles, 0);
233               print "Added ($res1) file \"$sourcedir/$f$ext\" to be copied to \"$newname\"\n";
234               if ($ext ne "") {
235                 $newname =~ s/$ext$//;
236               }
237               }
238               $f = $newname;
239               $res += $res1;
240             }
241           }
242           else {
243             if (! -e "$f") {
244               # Non relative (e.g. with absolute path) file should exist
245               if ($rStatus->{"filetype"} eq "interpret") {
246                 # filetype::interpret should be interpreted by lyx or latex and therefore emit error
247                 # We prinnt a warning instead
248                 print "WARNING: Interpreted file \"$f\" not found, at \"$source:$fi_line_no\"\n";
249               }
250               elsif ($rStatus->{"filetype"} eq "prefix_only") {
251                 # filetype::prefix_only should be interpreted by latex
252                 print "WARNING: Prefixed file \"$f\" not found, at \"$source:$fi_line_no\"\n";
253               }
254               else {
255                 # Collect the path-error-messages
256                 push(@path_errors, "File \"$f(" . $rStatus->{"filetype"} . ")\" not found, at \"$source:$fi_line_no\"");
257               }
258             }
259           }
260         }
261         if ($foundrelative && $rStatus->{"filetype"} !~ /^(prefix_for_list|prefix_only)$/) {
262           # The result can be relative too
263           # but, since prefix_for_list does no copy, we have to use absolute paths
264           # to address files inside the source dir
265           my @rel_list = ();
266           for my $fr (@{$filelist}) {
267             push(@rel_list, File::Spec->abs2rel($fr, $destdir));
268           }
269           $rF->[$fidx] = join($separator, @rel_list);
270           $l = join('', @{$rF});
271         }
272       }
273     }
274     print FO "$l\n";
275   }
276   close(FI);
277   close(FO);
278   if (@path_errors > 0) {
279     for my $entry (@path_errors) {
280       print "ERROR: $entry\n";
281     }
282     diestack("Aborted because of path errors in \"$source\"");
283   }
284
285   closeLyxStack();
286   return($res);
287 }
288
289 sub copyFoundSubdocuments($)
290 {
291   my ($rFiles) = @_;
292   my $res = 0;
293   do {
294     $res = 0;
295     my %copylist = ();
296
297     for my $filename (keys  %{$rFiles}) {
298       next if (! copyJobPending($filename, $rFiles));
299       $copylist{$filename} = 1;
300     }
301     for my $f (keys %copylist) {
302       # Second loop needed, because here $rFiles may change
303       my ($res1, @destfiles) = copyJob($f, $rFiles);
304       $res += $res1;
305       for my $destfile (@destfiles) {
306         print "res1 = $res1 for \"$f\" to be copied to $destfile\n";
307       }
308     }
309   } while($res > 0);            #  loop, while $rFiles changed
310 }
311
312 sub copyJob($$)
313 {
314   my ($source, $rFiles) = @_;
315   my $sourcedir = dirname($source);
316   my $res = 0;
317   my @dest = ();
318
319   for my $k (values %type2hash) {
320     if ($rFiles->{$source}->{$k}) {
321       if (! $rFiles->{$source}->{$k . "copied"}) {
322         $rFiles->{$source}->{$k . "copied"} = 1;
323         my $dest = $rFiles->{$source}->{$k};
324         push(@dest, $dest);
325         if ($k eq "copyonly") {
326           diestack("Could not copy \"$source\" to \"$dest\"") if (! cp($source, $dest));
327         }
328         else {
329           interpretedCopy($source, $dest, $destdirOfSubdocuments, $rFiles);
330         }
331         $res += 1;
332       }
333     }
334   }
335   return($res, @dest);
336 }
337
338 # Trivial check
339 sub isrelativeFix($$$)
340 {
341   my ($f, $sourcedir, $ext) = @_;
342
343   return(1, $ext) if  (-e "$sourcedir/$f$ext");
344   return(0,0);
345 }
346
347 sub isrelative($$$)
348 {
349   my ($f, $sourcedir, $ext) = @_;
350
351   if (ref($ext) eq "ARRAY") {
352     for my $ext2 (@{$ext}) {
353       my @res = isrelativeFix($f, $sourcedir, $ext2);
354       if ($res[0]) {
355         return(@res);
356       }
357     }
358     return(0,0);
359   }
360   else {
361     return(isrelativeFix($f, $sourcedir, $ext));
362   }
363 }
364
365 my $oldfname = "";
366
367 sub createTemporaryFileName($$$)
368 {
369   my ($source, $destdir, $created) = @_;
370
371   # get the basename to be used for the template
372   my ($name, $path, $suffix) = fileparse($source, qr/\.[^.]*/);
373   #print "source = $source, name = $name, path = $path, suffix = $suffix\n";
374   my $template = "xx-$name" . "-";
375   my $fname;
376   if (! $created) {
377     $fname = File::Temp::tempnam($destdir, $template);
378     $oldfname = $fname;
379   }
380   else {
381     $fname = $oldfname;
382   }
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, $created) = @_;
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, $created),
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 }