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($$$$);
66 # convert lyx file to be compilable with xetex
68 my ($source, $dest, $format, $fontT, $languageFile, $rest) = @ARGV;
69 my %encodings = (); # Encoding with TeX fonts, depending on language tag
71 diestack("Too many arguments") if (defined($rest));
72 diestack("Sourcefilename not defined") if (! defined($source));
73 diestack("Destfilename not defined") if (! defined($dest));
74 diestack("Format (e.g. pdf4) not defined") if (! defined($format));
75 diestack("Font type (e.g. texF) not defined") if (! defined($fontT));
77 $source = File::Spec->rel2abs($source);
78 $dest = File::Spec->rel2abs($dest);
82 if ($source =~ /\/([a-z][a-z](_[A-Z][A-Z])?)[\/_]/) {
86 my $inputEncoding = undef;
87 if ($fontT eq "systemF") {
88 if ($lang =~ /^(ru|uk|sk|el)$/) {
89 $font{roman} = "DejaVu Serif";
90 $font{sans} = "DejaVu Sans";
91 $font{typewriter} = "DejaVu Sans Mono";
93 elsif ($lang =~ /^(he|el|main)$/) {
94 $font{roman} = "FreeSans";
95 $font{sans} = "FreeSans";
96 $font{typewriter} = "FreeSans";
98 elsif ($lang eq "fa") {
99 $font{roman} = "FreeFarsi";
100 $font{sans} = "FreeFarsi";
101 $font{typewriter} = "FreeFarsi Monospace";
103 elsif ($lang eq "zh_CN") {
104 $font{roman} = "WenQuanYi Micro Hei";
105 $font{sans} = "WenQuanYi Micro Hei";
106 $font{typewriter} = "WenQuanYi Micro Hei";
108 elsif ($lang eq "ko" ) {
109 $font{roman} = "NanumGothic"; # NanumMyeongjo, NanumGothic Eco, NanumGothicCoding
110 $font{sans} = "NanumGothic";
111 $font{typewriter} = "NanumGothic";
113 elsif ($lang eq "ar" ) {
114 # available in 'fonts-sil-scheherazade' package
115 $font{roman} = "Scheherazade";
116 $font{sans} = "Scheherazade";
117 $font{typewriter} = "Scheherazade";
120 # default system fonts
121 $font{roman} = "FreeSerif";
122 $font{sans} = "FreeSans";
123 $font{typewriter} = "FreeMono";
126 elsif (0) { # set to '1' to enable setting of inputencoding
129 if (defined($languageFile)) {
130 # The 2 lines below does not seem to have any effect
131 #&getlangs($languageFile, \%encoding);
132 #&simplifylangs(\%encoding);
134 if ($format =~ /^(pdf4)$/) { # xelatex
135 # set input encoding to 'ascii' always
137 "search" => '.*', # this will be substituted from '\inputencoding'-line
141 elsif ($format =~ /^(dvi3|pdf5)$/) { # (dvi)?lualatex
142 # when to set input encoding to 'ascii'?
143 if (defined($encoding{$lang})) {
145 "search" => '.*', # this will be substituted from '\inputencoding'-line
146 "out" => $encoding{$lang},
152 my $sourcedir = dirname($source);
153 my $destdir = dirname($dest);
155 diestack("could not make dir \"$destdir\"") if (! mkdir $destdir);
158 my $destdirOfSubdocuments;
160 my ($name, $pat, $suffix) = fileparse($source, qr/\.[^.]*/);
161 my $ext = $format . "_$lang";
162 $destdirOfSubdocuments = "$destdir/tmp_$ext" . "_$name"; # Global var, something TODO here
165 if(-d $destdirOfSubdocuments) {
166 rmtree($destdirOfSubdocuments);
168 mkdir($destdirOfSubdocuments); # for possibly included files
170 my %IncludedFiles = ();
172 "copy_only" => "copyonly",
173 "interpret" => "interpret");
175 addNewJob($source, $dest, "interpret", {}, \%IncludedFiles);
177 copyFoundSubdocuments(\%IncludedFiles);
179 #printCopiedDocuments(\%IncludedFiles);
182 ###########################################################
184 sub printCopiedDocuments($)
187 for my $k (keys %{$rFiles}) {
188 my $rJob = $rFiles->{$k};
189 for my $j ( values %type2hash) {
190 if (defined($rJob->{$j})) {
191 print "$j: $k->$rJob->{$j}, " . $rJob->{$j . "copied"} . "\n";
197 sub interpretedCopy($$$$)
199 my ($source, $dest, $destdirOfSubdocuments, $rFiles) = @_;
200 my $sourcedir = dirname($source);
203 diestack("could not read \"$source\"") if (!open(FI, $source));
204 diestack("could not write \"$dest\"") if (! open(FO, '>', $dest));
206 initLyxStack(\%font, $fontT, $inputEncoding);
208 while (my $l = <FI>) {
211 my $rStatus = checkLyxLine($l);
212 if ($rStatus->{found}) {
213 my $rF = $rStatus->{result};
214 if ($rStatus->{"filetype"} eq "replace_only") {
215 # e.g. if no files involved (font chage etc)
216 $l = join('', @{$rF});
219 my $filelist = $rStatus->{filelist};
220 my $fidx = $rStatus->{fileidx};
221 my $separator = $rStatus->{"separator"};
222 my $foundrelative = 0;
223 for my $f (@{$filelist}) {
224 my @isrel = isrelative($f,
230 if ($rStatus->{"filetype"} eq "prefix_only") {
231 $f = getNewNameOf("$sourcedir/$f", $rFiles);
234 my ($newname, $res1);
235 ($newname, $res1) = addFileCopyJob("$sourcedir/$f$ext",
236 "$destdirOfSubdocuments",
237 $rStatus->{"filetype"},
239 print "Added ($res1) file \"$sourcedir/$f$ext\" to be copied to \"$newname\"\n";
241 $newname =~ s/$ext$//;
248 if ($foundrelative) {
249 $rF->[$fidx] = join($separator, @{$filelist});
250 $l = join('', @{$rF});
263 sub copyFoundSubdocuments($)
271 for my $filename (keys %{$rFiles}) {
272 next if (! copyJobPending($filename, $rFiles));
273 $copylist{$filename} = 1;
275 for my $f (keys %copylist) {
276 # Second loop needed, because here $rFiles may change
277 my ($res1, @destfiles) = copyJob($f, $rFiles);
279 for my $destfile (@destfiles) {
280 print "res1 = $res1 for \"$f\" to be copied to $destfile\n";
283 } while($res > 0); # loop, while $rFiles changed
288 my ($source, $rFiles) = @_;
289 my $sourcedir = dirname($source);
293 for my $k (values %type2hash) {
294 if ($rFiles->{$source}->{$k}) {
295 if (! $rFiles->{$source}->{$k . "copied"}) {
296 $rFiles->{$source}->{$k . "copied"} = 1;
297 my $dest = $rFiles->{$source}->{$k};
299 if ($k eq "copyonly") {
300 diestack("Could not copy \"$source\" to \"$dest\"") if (! cp($source, $dest));
303 interpretedCopy($source, $dest, $destdirOfSubdocuments, $rFiles);
313 sub isrelativeFix($$$)
315 my ($f, $sourcedir, $ext) = @_;
317 return(1, $ext) if (-e "$sourcedir/$f$ext");
323 my ($f, $sourcedir, $ext) = @_;
325 if (ref($ext) eq "ARRAY") {
326 for my $ext2 (@{$ext}) {
327 my @res = isrelativeFix($f, $sourcedir, $ext2);
335 return(isrelativeFix($f, $sourcedir, $ext));
339 sub createTemporaryFileName($$)
341 my ($source, $destdir) = @_;
343 # get the basename to be used for the template
344 my ($name, $path, $suffix) = fileparse($source, qr/\.[^.]*/);
345 #print "source = $source, name = $name, path = $path, suffix = $suffix\n";
346 my $template = "xx_$name" . "_";
347 my $fname = File::Temp::tempnam($destdir, $template);
349 # Append extension from source
356 # Check, if file not copied yet
357 sub copyJobPending($$)
359 my ($f, $rFiles) = @_;
360 for my $t (values %type2hash) {
361 if (defined($rFiles->{$f}->{$t})) {
362 return 1 if (! $rFiles->{$f}->{$t . "copied"});
370 my ($source, $newname, $hashname, $rJob, $rFiles) = @_;
372 $rJob->{$hashname} = $newname;
373 $rJob->{$hashname . "copied"} = 0;
374 $rFiles->{$source} = $rJob;
377 sub addFileCopyJob($$$$)
379 my ($source, $destdirOfSubdocuments, $filetype, $rFiles) = @_;
380 my ($res, $newname) = (0, undef);
381 my $rJob = $rFiles->{$source};
383 my $hashname = $type2hash{$filetype};
384 if (! defined($hashname)) {
385 diestack("unknown filetype \"$filetype\"");
387 if (!defined($rJob->{$hashname})) {
389 createTemporaryFileName($source, $destdirOfSubdocuments),
390 "$hashname", $rJob, $rFiles);
393 $newname = $rJob->{$hashname};
394 return($newname, $res);
399 my ($f, $rFiles) = @_;
402 if (defined($rFiles->{$f})) {
403 for my $t (values %type2hash) {
404 if (defined($rFiles->{$f}->{$t})) {
405 $resultf = $rFiles->{$f}->{$t};
415 my ($languagefile, $rencoding) = @_;
417 if (open(FI, $languagefile)) {
418 while (my $l = <FI>) {
419 if ($l =~ /^Language/) {
420 my ($lng, $enc) = &getLangEntry();
422 $rencoding->{$lng} = $enc;
432 my ($rencoding) = @_;
438 for my $k (reverse sort keys %{$rencoding}) {
439 my @tag = split('_', $k);
440 if ($tag[0] eq $base) {
442 if ($rencoding->{$k} ne $enc) {
447 # new base, check that old base was OK
450 $rencoding->{$base} = $enc;
451 push(@klist2, @klist);
456 $enc = $rencoding->{$k};
461 # close handling for last entry too
463 $rencoding->{$base} = $enc;
464 push(@klist2, @klist);
467 for my $k (@klist2) {
468 delete($rencoding->{$k});
474 my ($lng, $enc) = (undef, undef);
475 while (my $l = <FI>) {
477 if ($l =~ /^\s*Encoding\s+([^ ]+)\s*$/) {
480 elsif ($l =~ /^\s*LangCode\s+([^ ]+)\s*$/) {
483 elsif ($l =~ /^\s*End\s*$/) {
487 if (defined($lng) && defined($enc)) {
491 return(undef, undef);