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