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/[\/\\]?[^\/\\]+$//;
47 use File::Temp qw/ :POSIX /;
51 sub printCopiedDocuments($);
52 sub interpretedCopy($$$$);
53 sub copyFoundSubdocuments($);
55 sub isrelativeFix($$$);
57 sub createTemporaryFileName($$);
58 sub copyJobPending($$);
60 sub addFileCopyJob($$$$);
63 # convert lyx file to be compilable with xetex
65 my ($source, $dest, $format, $fontT, $rest) = @ARGV;
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));
73 $source = File::Spec->rel2abs($source);
74 $dest = File::Spec->rel2abs($dest);
78 if ($source =~ /\/([a-z][a-z](_[A-Z][A-Z])?)[\/_]/) {
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";
88 elsif ($lang =~ /^(he|el|main)$/) {
89 $font{roman} = "FreeSans";
90 $font{sans} = "FreeSans";
91 $font{typewriter} = "FreeSans";
93 elsif ($lang eq "fa") {
94 $font{roman} = "FreeFarsi";
95 $font{sans} = "FreeFarsi";
96 $font{typewriter} = "FreeFarsi Monospace";
98 elsif ($lang eq "zh_CN") {
99 $font{roman} = "WenQuanYi Micro Hei";
100 $font{sans} = "WenQuanYi Micro Hei";
101 $font{typewriter} = "WenQuanYi Micro Hei";
103 elsif ($lang eq "ko" ) {
104 $font{roman} = "NanumGothic"; # NanumMyeongjo, NanumGothic Eco, NanumGothicCoding
105 $font{sans} = "NanumGothic";
106 $font{typewriter} = "NanumGothic";
108 elsif ($lang eq "ar" ) {
109 # available in 'fonts-sil-scheherazade' package
110 $font{roman} = "Scheherazade";
111 $font{sans} = "Scheherazade";
112 $font{typewriter} = "Scheherazade";
115 # default system fonts
116 $font{roman} = "FreeSans";
117 $font{sans} = "FreeSans";
118 $font{typewriter} = "FreeSans";
125 my $sourcedir = dirname($source);
126 my $destdir = dirname($dest);
128 diestack("could not make dir \"$destdir\"") if (! mkdir $destdir);
131 my $destdirOfSubdocuments;
133 my ($name, $pat, $suffix) = fileparse($source, qr/\.[^.]*/);
134 my $ext = $format . "_$lang";
135 $destdirOfSubdocuments = "$destdir/tmp_$ext" . "_$name"; # Global var, something TODO here
138 if(-d $destdirOfSubdocuments) {
139 rmtree($destdirOfSubdocuments);
141 mkdir($destdirOfSubdocuments); # for possibly included files
143 my %IncludedFiles = ();
145 "copy_only" => "copyonly",
146 "interpret" => "interpret");
148 addNewJob($source, $dest, "interpret", {}, \%IncludedFiles);
150 copyFoundSubdocuments(\%IncludedFiles);
152 #printCopiedDocuments(\%IncludedFiles);
155 ###########################################################
157 sub printCopiedDocuments($)
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";
170 sub interpretedCopy($$$$)
172 my ($source, $dest, $destdirOfSubdocuments, $rFiles) = @_;
173 my $sourcedir = dirname($source);
176 diestack("could not read \"$source\"") if (!open(FI, $source));
177 diestack("could not write \"$dest\"") if (! open(FO, '>', $dest));
179 initLyxStack(\%font, $fontT);
181 while (my $l = <FI>) {
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});
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,
202 if ($rStatus->{"filetype"} eq "prefix_only") {
203 $f = getNewNameOf("$sourcedir/$f", $rFiles);
206 my ($newname, $res1);
207 ($newname, $res1) = addFileCopyJob("$sourcedir/$f$ext",
208 "$destdirOfSubdocuments",
209 $rStatus->{"filetype"},
211 print "Added ($res1) file \"$sourcedir/$f$ext\" to be copied to \"$newname\"\n";
213 $newname =~ s/$ext$//;
220 if ($foundrelative) {
221 $rF->[$fidx] = join($separator, @{$filelist});
222 $l = join('', @{$rF});
235 sub copyFoundSubdocuments($)
243 for my $filename (keys %{$rFiles}) {
244 next if (! copyJobPending($filename, $rFiles));
245 $copylist{$filename} = 1;
247 for my $f (keys %copylist) {
248 # Second loop needed, because here $rFiles may change
249 my ($res1, @destfiles) = copyJob($f, $rFiles);
251 for my $destfile (@destfiles) {
252 print "res1 = $res1 for \"$f\" to be copied to $destfile\n";
255 } while($res > 0); # loop, while $rFiles changed
260 my ($source, $rFiles) = @_;
261 my $sourcedir = dirname($source);
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};
271 if ($k eq "copyonly") {
272 diestack("Could not copy \"$source\" to \"$dest\"") if (! cp($source, $dest));
275 interpretedCopy($source, $dest, $destdirOfSubdocuments, $rFiles);
285 sub isrelativeFix($$$)
287 my ($f, $sourcedir, $ext) = @_;
289 return(1, $ext) if (-e "$sourcedir/$f$ext");
295 my ($f, $sourcedir, $ext) = @_;
297 if (ref($ext) eq "ARRAY") {
298 for my $ext2 (@{$ext}) {
299 my @res = isrelativeFix($f, $sourcedir, $ext2);
307 return(isrelativeFix($f, $sourcedir, $ext));
311 sub createTemporaryFileName($$)
313 my ($source, $destdir) = @_;
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);
321 # Append extension from source
328 # Check, if file not copied yet
329 sub copyJobPending($$)
331 my ($f, $rFiles) = @_;
332 for my $t (values %type2hash) {
333 if (defined($rFiles->{$f}->{$t})) {
334 return 1 if (! $rFiles->{$f}->{$t . "copied"});
342 my ($source, $newname, $hashname, $rJob, $rFiles) = @_;
344 $rJob->{$hashname} = $newname;
345 $rJob->{$hashname . "copied"} = 0;
346 $rFiles->{$source} = $rJob;
349 sub addFileCopyJob($$$$)
351 my ($source, $destdirOfSubdocuments, $filetype, $rFiles) = @_;
352 my ($res, $newname) = (0, undef);
353 my $rJob = $rFiles->{$source};
355 my $hashname = $type2hash{$filetype};
356 if (! defined($hashname)) {
357 diestack("unknown filetype \"$filetype\"");
359 if (!defined($rJob->{$hashname})) {
361 createTemporaryFileName($source, $destdirOfSubdocuments),
362 "$hashname", $rJob, $rFiles);
365 $newname = $rJob->{$hashname};
366 return($newname, $res);
371 my ($f, $rFiles) = @_;
374 if (defined($rFiles->{$f})) {
375 for my $t (values %type2hash) {
376 if (defined($rFiles->{$f}->{$t})) {
377 $resultf = $rFiles->{$f}->{$t};