]> git.lyx.org Git - lyx.git/blob - development/autotests/useSystemFonts.pl
Update \origin specifications
[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   $destdirOfSubdocuments = "$destdir/tmp_$ext" . "_$name"; # Global var, something TODO here
133 }
134
135 if(-d $destdirOfSubdocuments) {
136   rmtree($destdirOfSubdocuments);
137 }
138 mkpath($destdirOfSubdocuments); #  for possibly included files
139
140 my %IncludedFiles = ();
141 my %type2hash = (
142   "copy_only" => "copyonly",
143   "interpret" => "interpret");
144
145 addNewJob($source, $dest, "interpret", {}, \%IncludedFiles);
146
147 copyFoundSubdocuments(\%IncludedFiles);
148
149 #printCopiedDocuments(\%IncludedFiles);
150
151 exit(0);
152 ###########################################################
153
154 sub printCopiedDocuments($)
155 {
156   my ($rFiles) = @_;
157   for my $k (keys %{$rFiles}) {
158     my $rJob = $rFiles->{$k};
159     for my $j ( values %type2hash) {
160       if (defined($rJob->{$j})) {
161         print "$j: $k->$rJob->{$j}, " . $rJob->{$j . "copied"} . "\n";
162       }
163     }
164   }
165 }
166
167 sub interpretedCopy($$$$)
168 {
169   my ($source, $dest, $destdirOfSubdocuments, $rFiles) = @_;
170   my $sourcedir = dirname($source);
171   my $res = 0;
172
173   diestack("could not read \"$source\"") if (!open(FI, $source));
174   diestack("could not write \"$dest\"") if (! open(FO, '>', $dest));
175
176   initLyxStack(\%font, $fontT, $inputEncoding);
177
178   my $fi_line_no = 0;
179   my @path_errors = ();
180   while (my $l = <FI>) {
181     $fi_line_no += 1;
182     $l =~ s/[\n\r]+$//;
183     #chomp($l);
184     my $rStatus = checkLyxLine($l);
185     if ($rStatus->{found}) {
186       my $rF = $rStatus->{result};
187       if ($rStatus->{"filetype"} eq "replace_only") {
188         # e.g. if no files involved (font chage etc)
189         $l = join('', @{$rF});
190       }
191       else {
192         my $filelist = $rStatus->{filelist};
193         my $fidx = $rStatus->{fileidx};
194         my $separator = $rStatus->{"separator"};
195         my $foundrelative = 0;
196         for my $f (@{$filelist}) {
197           my @isrel = isrelative($f,
198                                   $sourcedir,
199                                   $rStatus->{ext});
200           if ($isrel[0]) {
201             $foundrelative = 1;
202             my $ext = $isrel[1];
203             if ($rStatus->{"filetype"} eq "prefix_only") {
204               $f = getNewNameOf("$sourcedir/$f", $rFiles);
205             }
206             else {
207               my ($newname, $res1);
208               ($newname, $res1) = addFileCopyJob("$sourcedir/$f$ext",
209                                                   "$destdirOfSubdocuments",
210                                                   $rStatus->{"filetype"},
211                                                   $rFiles);
212               print "Added ($res1) file \"$sourcedir/$f$ext\" to be copied to \"$newname\"\n";
213               if ($ext ne "") {
214                 $newname =~ s/$ext$//;
215               }
216               $f = $newname;
217               $res += $res1;
218             }
219           }
220           else {
221             if (! -e "$f") {
222               # Non relative (e.g. with absolute path) file should exist
223               if ($rStatus->{"filetype"} eq "interpret") {
224                 # filetype::interpret should be interpreted by lyx or latex and therefore emit error
225                 # We prinnt a warning instead
226                 print "WARNING: Interpreted file \"$f\" not found, at \"$source:$fi_line_no\"\n";
227               }
228               elsif ($rStatus->{"filetype"} eq "prefix_only") {
229                 # filetype::prefix_only should be interpreted by latex
230                 print "WARNING: Prefixed file \"$f\" not found, at \"$source:$fi_line_no\"\n";
231               }
232               else {
233                 # Collect the path-error-messages
234                 push(@path_errors, "File \"$f(" . $rStatus->{"filetype"} . ")\" not found, at \"$source:$fi_line_no\"");
235               }
236             }
237           }
238         }
239         if ($foundrelative) {
240           $rF->[$fidx] = join($separator, @{$filelist});
241           $l = join('', @{$rF});
242         }
243       }
244     }
245     print FO "$l\n";
246   }
247   close(FI);
248   close(FO);
249   if (@path_errors > 0) {
250     for my $entry (@path_errors) {
251       print "ERROR: $entry\n";
252     }
253     diestack("Aborted because of path errors in \"$source\"");
254   }
255
256   closeLyxStack();
257   return($res);
258 }
259
260 sub copyFoundSubdocuments($)
261 {
262   my ($rFiles) = @_;
263   my $res = 0;
264   do {
265     $res = 0;
266     my %copylist = ();
267
268     for my $filename (keys  %{$rFiles}) {
269       next if (! copyJobPending($filename, $rFiles));
270       $copylist{$filename} = 1;
271     }
272     for my $f (keys %copylist) {
273       # Second loop needed, because here $rFiles may change
274       my ($res1, @destfiles) = copyJob($f, $rFiles);
275       $res += $res1;
276       for my $destfile (@destfiles) {
277         print "res1 = $res1 for \"$f\" to be copied to $destfile\n";
278       }
279     }
280   } while($res > 0);            #  loop, while $rFiles changed
281 }
282
283 sub copyJob($$)
284 {
285   my ($source, $rFiles) = @_;
286   my $sourcedir = dirname($source);
287   my $res = 0;
288   my @dest = ();
289
290   for my $k (values %type2hash) {
291     if ($rFiles->{$source}->{$k}) {
292       if (! $rFiles->{$source}->{$k . "copied"}) {
293         $rFiles->{$source}->{$k . "copied"} = 1;
294         my $dest = $rFiles->{$source}->{$k};
295         push(@dest, $dest);
296         if ($k eq "copyonly") {
297           diestack("Could not copy \"$source\" to \"$dest\"") if (! cp($source, $dest));
298         }
299         else {
300           interpretedCopy($source, $dest, $destdirOfSubdocuments, $rFiles);
301         }
302         $res += 1;
303       }
304     }
305   }
306   return($res, @dest);
307 }
308
309 # Trivial check
310 sub isrelativeFix($$$)
311 {
312   my ($f, $sourcedir, $ext) = @_;
313
314   return(1, $ext) if  (-e "$sourcedir/$f$ext");
315   return(0,0);
316 }
317
318 sub isrelative($$$)
319 {
320   my ($f, $sourcedir, $ext) = @_;
321
322   if (ref($ext) eq "ARRAY") {
323     for my $ext2 (@{$ext}) {
324       my @res = isrelativeFix($f, $sourcedir, $ext2);
325       if ($res[0]) {
326         return(@res);
327       }
328     }
329     return(0,0);
330   }
331   else {
332     return(isrelativeFix($f, $sourcedir, $ext));
333   }
334 }
335
336 sub createTemporaryFileName($$)
337 {
338   my ($source, $destdir) = @_;
339
340   # get the basename to be used for the template
341   my ($name, $path, $suffix) = fileparse($source, qr/\.[^.]*/);
342   #print "source = $source, name = $name, path = $path, suffix = $suffix\n";
343   my $template = "xx_$name" . "_";
344   my $fname = File::Temp::tempnam($destdir, $template);
345
346   # Append extension from source
347   if ($suffix ne "") {
348     $fname .= "$suffix";
349   }
350   return($fname);
351 }
352
353 # Check, if file not copied yet
354 sub copyJobPending($$)
355 {
356   my ($f, $rFiles) = @_;
357   for my $t (values %type2hash) {
358     if (defined($rFiles->{$f}->{$t})) {
359       return 1 if (! $rFiles->{$f}->{$t . "copied"});
360     }
361   }
362   return 0;
363 }
364
365 sub addNewJob($$$$$)
366 {
367   my ($source, $newname, $hashname, $rJob, $rFiles) = @_;
368
369   $rJob->{$hashname} = $newname;
370   $rJob->{$hashname . "copied"} = 0;
371   $rFiles->{$source} = $rJob;
372 }
373
374 sub addFileCopyJob($$$$)
375 {
376   my ($source, $destdirOfSubdocuments, $filetype, $rFiles) = @_;
377   my ($res, $newname) = (0, undef);
378   my $rJob = $rFiles->{$source};
379
380   my $hashname = $type2hash{$filetype};
381   if (! defined($hashname)) {
382     diestack("unknown filetype \"$filetype\"");
383   }
384   if (!defined($rJob->{$hashname})) {
385     addNewJob($source,
386                createTemporaryFileName($source, $destdirOfSubdocuments),
387                "$hashname", $rJob, $rFiles);
388     $res = 1;
389   }
390   $newname = $rJob->{$hashname};
391   return($newname, $res);
392 }
393
394 sub getNewNameOf($$)
395 {
396   my ($f, $rFiles) = @_;
397   my $resultf = $f;
398
399   if (defined($rFiles->{$f})) {
400     for my $t (values %type2hash) {
401       if (defined($rFiles->{$f}->{$t})) {
402         $resultf = $rFiles->{$f}->{$t};
403         last;
404       }
405     }
406   }
407   return($resultf);
408 }
409
410 sub getlangs($$)
411 {
412   my ($languagefile, $rencoding) = @_;
413
414   if (open(FI, $languagefile)) {
415     while (my $l = <FI>) {
416       if ($l =~ /^Language/) {
417         my ($lng, $enc) = &getLangEntry();
418         if (defined($lng)) {
419           $rencoding->{$lng} = $enc;
420         }
421       }
422     }
423     close(FI);
424   }
425 }
426
427 sub simplifylangs($)
428 {
429   my ($rencoding) = @_;
430   my $base = "";
431   my $enc = "";
432   my $differ = 0;
433   my @klist = ();
434   my @klist2 = ();
435   for my $k (reverse sort keys %{$rencoding}) {
436     my @tag = split('_', $k);
437     if ($tag[0] eq $base) {
438       push(@klist, $k);
439       if ($rencoding->{$k} ne $enc) {
440         $differ = 1;
441       }
442     }
443     else {
444       # new base, check that old base was OK
445       if ($base ne "") {
446         if ($differ == 0) {
447           $rencoding->{$base} = $enc;
448           push(@klist2, @klist);
449         }
450       }
451       @klist = ($k);
452       $base = $tag[0];
453       $enc = $rencoding->{$k};
454       $differ = 0;
455     }
456   }
457   if ($base ne "") {
458     # close handling for last entry too
459     if ($differ == 0) {
460       $rencoding->{$base} = $enc;
461       push(@klist2, @klist);
462     }
463   }
464   for my $k (@klist2) {
465     delete($rencoding->{$k});
466   }
467 }
468
469 sub getLangEntry()
470 {
471   my ($lng, $enc) = (undef, undef);
472   while (my $l = <FI>) {
473     chomp($l);
474     if ($l =~ /^\s*Encoding\s+([^ ]+)\s*$/) {
475       $enc = $1;
476     }
477     elsif ($l =~ /^\s*LangCode\s+([^ ]+)\s*$/) {
478       $lng = $1;
479     }
480     elsif ($l =~ /^\s*End\s*$/) {
481       last;
482     }
483   }
484   if (defined($lng) && defined($enc)) {
485     return($lng, $enc);
486   }
487   else {
488     return(undef, undef);
489   }
490 }