4 # file useSystemFonts.pl
5 # 1.) Copies lyx-files to another location
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
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
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.
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.
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
33 # Copyright (c) 2013 Kornel Benko <kornel@lyx.org>
34 # (c) 2013 Scott Kostyshak <skotysh@lyx.org>
40 my $p = File::Spec->rel2abs( __FILE__ );
41 $p =~ s/[\/\\]?[^\/\\]+$//;
48 use File::Temp qw/ :POSIX /;
51 # convert lyx file to be compilable with xetex
53 my ($source, $dest, $format, $rest) = @ARGV;
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));
60 $source = &abs_path($source);
61 $dest = &abs_path($dest);
65 if ($source =~ /\/(he|el|ru|uk)\//) {
66 $font{roman} = "FreeSans";
67 $font{sans} = "FreeSans";
68 $font{typewriter} = "FreeSans";
70 elsif ($source =~ /\/fa\//) {
71 $font{roman} = "FreeFarsi";
72 $font{sans} = "FreeFarsi";
73 $font{typewriter} = "FreeFarsi Monospace";
75 elsif ($source =~ /\/zh_CN\//) {
76 $font{roman} = "FreeSans";
77 $font{sans} = "WenQuanYi Micro Hei";
78 $font{typewriter} = "WenQuanYi Micro Hei";
84 my $sourcedir = dirname($source);
85 my $destdir = dirname($dest);
87 &diestack("could not make dir \"$destdir\"") if (! mkdir $destdir);
90 my $destdirOfSubdocuments;
92 my ($name, $pat, $suffix) = fileparse($source, qr/\.[^.]*/);
94 if ($source =~ /\/([a-z][a-z](_[A-Z][A-Z])?)\//) {
100 $destdirOfSubdocuments = "$destdir/tmp_$ext" . "_$name"; # Global var, something TODO here
103 if(-d $destdirOfSubdocuments) {
104 rmtree($destdirOfSubdocuments);
106 mkdir($destdirOfSubdocuments); # for possibly included files
108 my %IncludedFiles = ();
110 "copy_only" => "copyonly",
111 "interpret" => "interpret");
113 &addNewJob($source, $dest, "interpret", {}, \%IncludedFiles);
115 ©FoundSubdocuments(\%IncludedFiles);
117 #&printCopiedDocuments(\%IncludedFiles);
120 ###########################################################
122 sub printCopiedDocuments($)
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";
135 sub interpretedCopy($$$$)
137 my ($source, $dest, $destdirOfSubdocuments, $rFiles) = @_;
138 my $sourcedir = dirname($source);
141 &diestack("could not read \"$source\"") if (!open(FI, $source));
142 &diestack("could not write \"$dest\"") if (! open(FO, '>', $dest));
144 &initLyxStack(\%font);
146 while (my $l = <FI>) {
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});
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,
167 if ($rStatus->{"filetype"} eq "prefix_only") {
168 $f = &getNewNameOf("$sourcedir/$f", $rFiles);
171 my ($newname, $res1);
172 ($newname, $res1) = &addFileCopyJob("$sourcedir/$f$ext",
173 "$destdirOfSubdocuments",
174 $rStatus->{"filetype"},
176 print "Added ($res1) file \"$sourcedir/$f$ext\" to be copied to \"$newname\"\n";
178 $newname =~ s/$ext$//;
185 if ($foundrelative) {
186 $rF->[$fidx] = join($separator, @{$filelist});
187 $l = join('', @{$rF});
200 sub copyFoundSubdocuments($)
208 for my $filename (keys %{$rFiles}) {
209 next if (! ©JobPending($filename, $rFiles));
210 $copylist{$filename} = 1;
212 for my $f (keys %copylist) {
213 # Second loop needed, because here $rFiles may change
214 my ($res1, @destfiles) = ©Job($f, $rFiles);
216 for my $destfile (@destfiles) {
217 print "res1 = $res1 for \"$f\" to be copied to $destfile\n";
220 } while($res > 0); # loop, while $rFiles changed
225 my ($source, $rFiles) = @_;
226 my $sourcedir = dirname($source);
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};
236 if ($k eq "copyonly") {
237 &diestack("Could not copy \"$source\" to \"$dest\"") if (! cp($source, $dest));
240 &interpretedCopy($source, $dest, $destdirOfSubdocuments, $rFiles);
250 sub isrelativeFix($$$)
252 my ($f, $sourcedir, $ext) = @_;
254 return(1, $ext) if (-e "$sourcedir/$f$ext");
260 my ($f, $sourcedir, $ext) = @_;
262 if (ref($ext) eq "ARRAY") {
263 for my $ext2 (@{$ext}) {
264 my @res = &isrelativeFix($f, $sourcedir, $ext2);
272 return(&isrelativeFix($f, $sourcedir, $ext));
276 sub createTemporaryFileName($$)
278 my ($source, $destdir) = @_;
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);
286 # Append extension from source
293 # Check, if file not copied yet
294 sub copyJobPending($$)
296 my ($f, $rFiles) = @_;
297 for my $t (values %type2hash) {
298 if (defined($rFiles->{$f}->{$t})) {
299 return 1 if (! $rFiles->{$f}->{$t . "copied"});
307 my ($source, $newname, $hashname, $rJob, $rFiles) = @_;
309 $rJob->{$hashname} = $newname;
310 $rJob->{$hashname . "copied"} = 0;
311 $rFiles->{$source} = $rJob;
314 sub addFileCopyJob($$$$)
316 my ($source, $destdirOfSubdocuments, $filetype, $rFiles) = @_;
317 my ($res, $newname) = (0, undef);
318 my $rJob = $rFiles->{$source};
320 my $hashname = $type2hash{$filetype};
321 if (! defined($hashname)) {
322 &diestack("unknown filetype \"$filetype\"");
324 if (!defined($rJob->{$hashname})) {
326 &createTemporaryFileName($source, $destdirOfSubdocuments),
327 "$hashname", $rJob, $rFiles);
330 $newname = $rJob->{$hashname};
331 return($newname, $res);
336 my ($f, $rFiles) = @_;
339 if (defined($rFiles->{$f})) {
340 for my $t (values %type2hash) {
341 if (defined($rFiles->{$f}->{$t})) {
342 $resultf = $rFiles->{$f}->{$t};