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