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