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.) Changes default fonts to use non-tex-fonts
10 # 2c.) Changes the non-tex fonts setting if it is "default" (see below).
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, $encodingT, $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));
76 diestack("Encoding (e.g. ascii) not defined") if (! defined($encodingT));
78 $source = File::Spec->rel2abs($source);
79 $dest = File::Spec->rel2abs($dest);
83 if ($source =~ /\/([a-z][a-z](_[A-Z][A-Z])?)[\/_]/) {
87 my $inputEncoding = undef;
88 if ($fontT eq "systemF") {
89 if ($lang =~ /^(ru|uk|sk|el)$/) {
90 $font{roman} = "DejaVu Serif";
91 $font{sans} = "DejaVu Sans";
92 $font{typewriter} = "DejaVu Sans Mono";
94 elsif ($lang =~ /^(he)$/) {
95 $font{roman} = "FreeSans";
96 $font{sans} = "FreeSans";
97 $font{typewriter} = "FreeSans";
99 elsif ($lang eq "fa") {
100 $font{roman} = "FreeFarsi";
101 $font{sans} = "FreeFarsi";
102 $font{typewriter} = "FreeFarsi Monospace";
104 # elsif ($lang eq "ko" ) {
105 # $font{roman} = "NanumGothic"; # NanumMyeongjo, NanumGothic Eco, NanumGothicCoding
106 # $font{sans} = "NanumGothic";
107 # $font{typewriter} = "NanumGothic";
109 elsif ($lang eq "ar" ) {
110 # available in 'fonts-sil-scheherazade' package
111 $font{roman} = "Scheherazade";
112 $font{sans} = "Scheherazade";
113 $font{typewriter} = "Scheherazade";
116 # default system fonts
117 $font{roman} = "FreeSerif";
118 $font{sans} = "FreeSans";
119 $font{typewriter} = "FreeMono";
122 elsif ($encodingT ne "default") {
123 # set input encoding to the requested value
125 "search" => '.*', # this will be substituted from '\inputencoding'-line
129 elsif (0) { # set to '1' to enable setting of inputencoding
132 if (defined($languageFile)) {
133 # The 2 lines below does not seem to have any effect
134 #&getlangs($languageFile, \%encoding);
135 #&simplifylangs(\%encoding);
137 if ($format =~ /^(pdf4)$/) { # xelatex
138 # set input encoding to 'ascii' always
140 "search" => '.*', # this will be substituted from '\inputencoding'-line
144 elsif ($format =~ /^(dvi3|pdf5)$/) { # (dvi)?lualatex
145 # when to set input encoding to 'ascii'?
146 if (defined($encoding{$lang})) {
148 "search" => '.*', # this will be substituted from '\inputencoding'-line
149 "out" => $encoding{$lang},
155 my $sourcedir = dirname($source);
156 my $destdir = dirname($dest);
158 diestack("could not make dir \"$destdir\"") if (! mkpath $destdir);
161 my $destdirOfSubdocuments;
163 my ($name, $pat, $suffix) = fileparse($source, qr/\.[^.]*/);
164 my $ext = $format . "_$lang";
165 $destdirOfSubdocuments = "$destdir/tmp_$ext" . "_$name"; # Global var, something TODO here
168 if(-d $destdirOfSubdocuments) {
169 rmtree($destdirOfSubdocuments);
171 mkpath($destdirOfSubdocuments); # for possibly included files
173 my %IncludedFiles = ();
175 "copy_only" => "copyonly",
176 "interpret" => "interpret");
178 addNewJob($source, $dest, "interpret", {}, \%IncludedFiles);
180 copyFoundSubdocuments(\%IncludedFiles);
182 #printCopiedDocuments(\%IncludedFiles);
185 ###########################################################
187 sub printCopiedDocuments($)
190 for my $k (keys %{$rFiles}) {
191 my $rJob = $rFiles->{$k};
192 for my $j ( values %type2hash) {
193 if (defined($rJob->{$j})) {
194 print "$j: $k->$rJob->{$j}, " . $rJob->{$j . "copied"} . "\n";
200 sub interpretedCopy($$$$)
202 my ($source, $dest, $destdirOfSubdocuments, $rFiles) = @_;
203 my $sourcedir = dirname($source);
206 diestack("could not read \"$source\"") if (!open(FI, $source));
207 diestack("could not write \"$dest\"") if (! open(FO, '>', $dest));
209 initLyxStack(\%font, $fontT, $inputEncoding);
212 my @path_errors = ();
213 while (my $l = <FI>) {
217 my $rStatus = checkLyxLine($l);
218 if ($rStatus->{found}) {
219 my $rF = $rStatus->{result};
220 if ($rStatus->{"filetype"} eq "replace_only") {
221 # e.g. if no files involved (font chage etc)
222 $l = join('', @{$rF});
225 my $filelist = $rStatus->{filelist};
226 my $fidx = $rStatus->{fileidx};
227 my $separator = $rStatus->{"separator"};
228 my $foundrelative = 0;
229 for my $f (@{$filelist}) {
230 my @isrel = isrelative($f,
236 if ($rStatus->{"filetype"} eq "prefix_only") {
237 $f = getNewNameOf("$sourcedir/$f", $rFiles);
240 my ($newname, $res1);
241 ($newname, $res1) = addFileCopyJob("$sourcedir/$f$ext",
242 "$destdirOfSubdocuments",
243 $rStatus->{"filetype"},
245 print "Added ($res1) file \"$sourcedir/$f$ext\" to be copied to \"$newname\"\n";
247 $newname =~ s/$ext$//;
255 # Non relative (e.g. with absolute path) file should exist
256 if ($rStatus->{"filetype"} eq "interpret") {
257 # filetype::interpret should be interpreted by lyx or latex and therefore emit error
258 # We prinnt a warning instead
259 print "WARNING: Interpreted file \"$f\" not found, at \"$source:$fi_line_no\"\n";
261 elsif ($rStatus->{"filetype"} eq "prefix_only") {
262 # filetype::prefix_only should be interpreted by latex
263 print "WARNING: Prefixed file \"$f\" not found, at \"$source:$fi_line_no\"\n";
266 # Collect the path-error-messages
267 push(@path_errors, "File \"$f(" . $rStatus->{"filetype"} . ")\" not found, at \"$source:$fi_line_no\"");
272 if ($foundrelative) {
273 $rF->[$fidx] = join($separator, @{$filelist});
274 $l = join('', @{$rF});
282 if (@path_errors > 0) {
283 for my $entry (@path_errors) {
284 print "ERROR: $entry\n";
286 diestack("Aborted because of path errors in \"$source\"");
293 sub copyFoundSubdocuments($)
301 for my $filename (keys %{$rFiles}) {
302 next if (! copyJobPending($filename, $rFiles));
303 $copylist{$filename} = 1;
305 for my $f (keys %copylist) {
306 # Second loop needed, because here $rFiles may change
307 my ($res1, @destfiles) = copyJob($f, $rFiles);
309 for my $destfile (@destfiles) {
310 print "res1 = $res1 for \"$f\" to be copied to $destfile\n";
313 } while($res > 0); # loop, while $rFiles changed
318 my ($source, $rFiles) = @_;
319 my $sourcedir = dirname($source);
323 for my $k (values %type2hash) {
324 if ($rFiles->{$source}->{$k}) {
325 if (! $rFiles->{$source}->{$k . "copied"}) {
326 $rFiles->{$source}->{$k . "copied"} = 1;
327 my $dest = $rFiles->{$source}->{$k};
329 if ($k eq "copyonly") {
330 diestack("Could not copy \"$source\" to \"$dest\"") if (! cp($source, $dest));
333 interpretedCopy($source, $dest, $destdirOfSubdocuments, $rFiles);
343 sub isrelativeFix($$$)
345 my ($f, $sourcedir, $ext) = @_;
347 return(1, $ext) if (-e "$sourcedir/$f$ext");
353 my ($f, $sourcedir, $ext) = @_;
355 if (ref($ext) eq "ARRAY") {
356 for my $ext2 (@{$ext}) {
357 my @res = isrelativeFix($f, $sourcedir, $ext2);
365 return(isrelativeFix($f, $sourcedir, $ext));
369 sub createTemporaryFileName($$)
371 my ($source, $destdir) = @_;
373 # get the basename to be used for the template
374 my ($name, $path, $suffix) = fileparse($source, qr/\.[^.]*/);
375 #print "source = $source, name = $name, path = $path, suffix = $suffix\n";
376 my $template = "xx_$name" . "_";
377 my $fname = File::Temp::tempnam($destdir, $template);
379 # Append extension from source
386 # Check, if file not copied yet
387 sub copyJobPending($$)
389 my ($f, $rFiles) = @_;
390 for my $t (values %type2hash) {
391 if (defined($rFiles->{$f}->{$t})) {
392 return 1 if (! $rFiles->{$f}->{$t . "copied"});
400 my ($source, $newname, $hashname, $rJob, $rFiles) = @_;
402 $rJob->{$hashname} = $newname;
403 $rJob->{$hashname . "copied"} = 0;
404 $rFiles->{$source} = $rJob;
407 sub addFileCopyJob($$$$)
409 my ($source, $destdirOfSubdocuments, $filetype, $rFiles) = @_;
410 my ($res, $newname) = (0, undef);
411 my $rJob = $rFiles->{$source};
413 my $hashname = $type2hash{$filetype};
414 if (! defined($hashname)) {
415 diestack("unknown filetype \"$filetype\"");
417 if (!defined($rJob->{$hashname})) {
419 createTemporaryFileName($source, $destdirOfSubdocuments),
420 "$hashname", $rJob, $rFiles);
423 $newname = $rJob->{$hashname};
424 return($newname, $res);
429 my ($f, $rFiles) = @_;
432 if (defined($rFiles->{$f})) {
433 for my $t (values %type2hash) {
434 if (defined($rFiles->{$f}->{$t})) {
435 $resultf = $rFiles->{$f}->{$t};
445 my ($languagefile, $rencoding) = @_;
447 if (open(FI, $languagefile)) {
448 while (my $l = <FI>) {
449 if ($l =~ /^Language/) {
450 my ($lng, $enc) = &getLangEntry();
452 $rencoding->{$lng} = $enc;
462 my ($rencoding) = @_;
468 for my $k (reverse sort keys %{$rencoding}) {
469 my @tag = split('_', $k);
470 if ($tag[0] eq $base) {
472 if ($rencoding->{$k} ne $enc) {
477 # new base, check that old base was OK
480 $rencoding->{$base} = $enc;
481 push(@klist2, @klist);
486 $enc = $rencoding->{$k};
491 # close handling for last entry too
493 $rencoding->{$base} = $enc;
494 push(@klist2, @klist);
497 for my $k (@klist2) {
498 delete($rencoding->{$k});
504 my ($lng, $enc) = (undef, undef);
505 while (my $l = <FI>) {
507 if ($l =~ /^\s*Encoding\s+([^ ]+)\s*$/) {
510 elsif ($l =~ /^\s*LangCode\s+([^ ]+)\s*$/) {
513 elsif ($l =~ /^\s*End\s*$/) {
517 if (defined($lng) && defined($enc)) {
521 return(undef, undef);