]> git.lyx.org Git - features.git/blob - development/autotests/useSystemFonts.pl
uk.po: update by Yuri
[features.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 Cwd 'abs_path';
47 use File::Copy "cp";
48 use File::Temp qw/ :POSIX /;
49 use lyxStatus;
50
51 # convert lyx file to be compilable with xetex
52
53 my ($source, $dest, $format, $rest) = @ARGV;
54
55 &diestack("Too many arguments") if (defined($rest));
56 &diestack("Sourcefilename not defined") if (! defined($source));
57 &diestack("Destfilename not defined") if (! defined($dest));
58 &diestack("Format (e.g. pdf4) not defined") if (! defined($format));
59
60 $source = &abs_path($source);
61 $dest = &abs_path($dest);
62
63 my %font = ();
64
65 if ($source =~ /\/(he|el|ru|uk)\//) {
66   $font{roman} = "FreeSans";
67   $font{sans} = "FreeSans";
68   $font{typewriter} = "FreeSans";
69 }
70 elsif ($source =~ /\/fa\//) {
71   $font{roman} = "FreeFarsi";
72   $font{sans} = "FreeFarsi";
73   $font{typewriter} = "FreeFarsi Monospace";
74 }
75 elsif ($source =~ /\/zh_CN\//) {
76   $font{roman} = "FreeSans";
77   $font{sans} = "WenQuanYi Micro Hei";
78   $font{typewriter} = "WenQuanYi Micro Hei";
79 }
80 else {
81   # Nothing to do?
82 }
83
84 my $sourcedir = dirname($source);
85 my $destdir = dirname($dest);
86 if (! -d $destdir) {
87   &diestack("could not make dir \"$destdir\"") if (! mkdir $destdir);
88 }
89
90 my $destdirOfSubdocuments;
91 {
92   my ($name, $pat, $suffix) = fileparse($source, qr/\.[^.]*/);
93   my $ext = $format;
94   if ($source =~ /\/([a-z][a-z](_[A-Z][A-Z])?)\//) {
95     $ext .= "_$1";
96   }
97   else {
98     $ext .= "_main";
99   }
100   $destdirOfSubdocuments = "$destdir/tmp_$ext" . "_$name"; # Global var, something TODO here
101 }
102
103 if(-d $destdirOfSubdocuments) {
104   rmtree($destdirOfSubdocuments);
105 }
106 mkdir($destdirOfSubdocuments);  #  for possibly included files
107
108 my %IncludedFiles = ();
109 my %type2hash = (
110   "copy_only" => "copyonly",
111   "interpret" => "interpret");
112
113 &addNewJob($source, $dest, "interpret", {}, \%IncludedFiles);
114
115 &copyFoundSubdocuments(\%IncludedFiles);
116
117 #&printCopiedDocuments(\%IncludedFiles);
118
119 exit(0);
120 ###########################################################
121
122 sub printCopiedDocuments($)
123 {
124   my ($rFiles) = @_;
125   for my $k (keys %{$rFiles}) {
126     my $rJob = $rFiles->{$k};
127     for my $j ( values %type2hash) {
128       if (defined($rJob->{$j})) {
129         print "$j: $k->$rJob->{$j}, " . $rJob->{$j . "copied"} . "\n";
130       }
131     }
132   }
133 }
134
135 sub interpretedCopy($$$$)
136 {
137   my ($source, $dest, $destdirOfSubdocuments, $rFiles) = @_;
138   my $sourcedir = dirname($source);
139   my $res = 0;
140
141   &diestack("could not read \"$source\"") if (!open(FI, $source));
142   &diestack("could not write \"$dest\"") if (! open(FO, '>', $dest));
143
144   &initLyxStack(\%font);
145
146   while (my $l = <FI>) {
147     chomp($l);
148     my $rStatus = &checkLyxLine($l);
149     if ($rStatus->{found}) {
150       my $rF = $rStatus->{result};
151       if ($rStatus->{"filetype"} eq "replace_only") {
152         # e.g. if no files involved (font chage etc)
153         $l = join('', @{$rF});
154       }
155       else {
156         my $filelist = $rStatus->{filelist};
157         my $fidx = $rStatus->{fileidx};
158         my $separator = $rStatus->{"separator"};
159         my $foundrelative = 0;
160         for my $f (@{$filelist}) {
161           my @isrel = &isrelative($f,
162                                   $sourcedir,
163                                   $rStatus->{ext});
164           if ($isrel[0]) {
165             $foundrelative = 1;
166             my $ext = $isrel[1];
167             if ($rStatus->{"filetype"} eq "prefix_only") {
168               $f = &getNewNameOf("$sourcedir/$f", $rFiles);
169             }
170             else {
171               my ($newname, $res1);
172               ($newname, $res1) = &addFileCopyJob("$sourcedir/$f$ext",
173                                                   "$destdirOfSubdocuments",
174                                                   $rStatus->{"filetype"},
175                                                   $rFiles);
176               print "Added ($res1) file \"$sourcedir/$f$ext\" to be copied to \"$newname\"\n";
177               if ($ext ne "") {
178                 $newname =~ s/$ext$//;
179               }
180               $f = $newname;
181               $res += $res1;
182             }
183           }
184         }
185         if ($foundrelative) {
186           $rF->[$fidx] = join($separator, @{$filelist});
187           $l = join('', @{$rF});
188         }
189       }
190     }
191     print FO "$l\n";
192   }
193   close(FI);
194   close(FO);
195
196   &closeLyxStack();
197   return($res);
198 }
199
200 sub copyFoundSubdocuments($)
201 {
202   my ($rFiles) = @_;
203   my $res = 0;
204   do {
205     $res = 0;
206     my %copylist = ();
207
208     for my $filename (keys  %{$rFiles}) {
209       next if (! &copyJobPending($filename, $rFiles));
210       $copylist{$filename} = 1;
211     }
212     for my $f (keys %copylist) {
213       # Second loop needed, because here $rFiles may change
214       my ($res1, @destfiles) = &copyJob($f, $rFiles);
215       $res += $res1;
216       for my $destfile (@destfiles) {
217         print "res1 = $res1 for \"$f\" to be copied to $destfile\n";
218       }
219     }
220   } while($res > 0);            #  loop, while $rFiles changed
221 }
222
223 sub copyJob($$)
224 {
225   my ($source, $rFiles) = @_;
226   my $sourcedir = dirname($source);
227   my $res = 0;
228   my @dest = ();
229
230   for my $k (values %type2hash) {
231     if ($rFiles->{$source}->{$k}) {
232       if (! $rFiles->{$source}->{$k . "copied"}) {
233         $rFiles->{$source}->{$k . "copied"} = 1;
234         my $dest = $rFiles->{$source}->{$k};
235         push(@dest, $dest);
236         if ($k eq "copyonly") {
237           &diestack("Could not copy \"$source\" to \"$dest\"") if (! cp($source, $dest));
238         }
239         else {
240           &interpretedCopy($source, $dest, $destdirOfSubdocuments, $rFiles);
241         }
242         $res += 1;
243       }
244     }
245   }
246   return($res, @dest);
247 }
248
249 # Trivial check
250 sub isrelativeFix($$$)
251 {
252   my ($f, $sourcedir, $ext) = @_;
253
254   return(1, $ext) if  (-e "$sourcedir/$f$ext");
255   return(0,0);
256 }
257
258 sub isrelative($$$)
259 {
260   my ($f, $sourcedir, $ext) = @_;
261
262   if (ref($ext) eq "ARRAY") {
263     for my $ext2 (@{$ext}) {
264       my @res = &isrelativeFix($f, $sourcedir, $ext2);
265       if ($res[0]) {
266         return(@res);
267       }
268     }
269     return(0,0);
270   }
271   else {
272     return(&isrelativeFix($f, $sourcedir, $ext));
273   }
274 }
275
276 sub createTemporaryFileName($$)
277 {
278   my ($source, $destdir) = @_;
279
280   # get the basename to be used for the template
281   my ($name, $path, $suffix) = fileparse($source, qr/\.[^.]*/);
282   #print "source = $source, name = $name, path = $path, suffix = $suffix\n";
283   my $template = "xx_$name" . "_";
284   my $fname = File::Temp::tempnam($destdir, $template);
285
286   # Append extension from source
287   if ($suffix ne "") {
288     $fname .= "$suffix";
289   }
290   return($fname);
291 }
292
293 # Check, if file not copied yet
294 sub copyJobPending($$)
295 {
296   my ($f, $rFiles) = @_;
297   for my $t (values %type2hash) {
298     if (defined($rFiles->{$f}->{$t})) {
299       return 1 if (! $rFiles->{$f}->{$t . "copied"});
300     }
301   }
302   return 0;
303 }
304
305 sub addNewJob($$$)
306 {
307   my ($source, $newname, $hashname, $rJob, $rFiles) = @_;
308
309   $rJob->{$hashname} = $newname;
310   $rJob->{$hashname . "copied"} = 0;
311   $rFiles->{$source} = $rJob;
312 }
313
314 sub addFileCopyJob($$$$)
315 {
316   my ($source, $destdirOfSubdocuments, $filetype, $rFiles) = @_;
317   my ($res, $newname) = (0, undef);
318   my $rJob = $rFiles->{$source};
319
320   my $hashname = $type2hash{$filetype};
321   if (! defined($hashname)) {
322     &diestack("unknown filetype \"$filetype\"");
323   }
324   if (!defined($rJob->{$hashname})) {
325     &addNewJob($source,
326                &createTemporaryFileName($source, $destdirOfSubdocuments),
327                "$hashname", $rJob, $rFiles);
328     $res = 1;
329   }
330   $newname = $rJob->{$hashname};
331   return($newname, $res);
332 }
333
334 sub getNewNameOf($$)
335 {
336   my ($f, $rFiles) = @_;
337   my $resultf = $f;
338
339   if (defined($rFiles->{$f})) {
340     for my $t (values %type2hash) {
341       if (defined($rFiles->{$f}->{$t})) {
342         $resultf = $rFiles->{$f}->{$t};
343         last;
344       }
345     }
346   }
347   return($resultf);
348 }