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