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
11 # Syntax: perl useSystemFonts.pl sourceFile destFile format
12 # Each param represents a path to a file
13 # sourceFile: full path to a lyx file
14 # destFile: destination path
15 # Each subdocument will be copied into a subdirectory of dirname(destFile)
16 # format: any string of the form '[a-zA-Z0-9]+', e.g. pdf5
18 # This file is free software; you can redistribute it and/or
19 # modify it under the terms of the GNU General Public
20 # License as published by the Free Software Foundation; either
21 # version 2 of the License, or (at your option) any later version.
23 # This software is distributed in the hope that it will be useful,
24 # but WITHOUT ANY WARRANTY; without even the implied warranty of
25 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
26 # General Public License for more details.
28 # You should have received a copy of the GNU General Public
29 # License along with this software; if not, write to the Free Software
30 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
32 # Copyright (c) 2013 Kornel Benko <kornel@lyx.org>
33 # (c) 2013 Scott Kostyshak <skotysh@lyx.org>
39 my $p = File::Spec->rel2abs( __FILE__ );
40 $p =~ s/[\/\\]?[^\/\\]+$//;
46 use File::Temp qw/ :POSIX /;
50 sub printCopiedDocuments($);
51 sub interpretedCopy($$$$);
52 sub copyFoundSubdocuments($);
54 sub isrelativeFix($$$);
56 sub createTemporaryFileName($$);
57 sub copyJobPending($$);
59 sub addFileCopyJob($$$$);
65 # convert lyx file to be compilable with xetex
67 my ($source, $dest, $format, $fontT, $encodingT, $languageFile, $rest) = @ARGV;
68 my %encodings = (); # Encoding with TeX fonts, depending on language tag
70 diestack("Too many arguments") if (defined($rest));
71 diestack("Sourcefilename not defined") if (! defined($source));
72 diestack("Destfilename not defined") if (! defined($dest));
73 diestack("Format (e.g. pdf4) not defined") if (! defined($format));
74 diestack("Font type (e.g. texF) not defined") if (! defined($fontT));
75 diestack("Encoding (e.g. ascii) not defined") if (! defined($encodingT));
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 =~ /^(ar|ca|cs|da|de|el|es|eu|fa|fr|gl|he|hu|id|it|ko|nb|nl|pl|pt|ro|ru|se|sk|sl|sr|sv|uk)$/) {
91 # # default system fonts
92 # $font{roman} = "FreeSerif";
93 # $font{sans} = "FreeSans";
94 # $font{typewriter} = "FreeMono";
97 elsif ($encodingT ne "default") {
98 # set input encoding to the requested value
100 "search" => '.*', # this will be substituted from '\inputencoding'-line
104 elsif (0) { # set to '1' to enable setting of inputencoding
107 if (defined($languageFile)) {
108 # The 2 lines below does not seem to have any effect
109 #&getlangs($languageFile, \%encoding);
110 #&simplifylangs(\%encoding);
112 if ($format =~ /^(pdf4)$/) { # xelatex
113 # set input encoding to 'ascii' always
115 "search" => '.*', # this will be substituted from '\inputencoding'-line
119 elsif ($format =~ /^(dvi3|pdf5)$/) { # (dvi)?lualatex
120 # when to set input encoding to 'ascii'?
121 if (defined($encoding{$lang})) {
123 "search" => '.*', # this will be substituted from '\inputencoding'-line
124 "out" => $encoding{$lang},
130 my $sourcedir = dirname($source);
131 my $destdir = dirname($dest);
133 diestack("could not make dir \"$destdir\"") if (! mkpath $destdir);
136 my $destdirOfSubdocuments;
138 my ($name, $pat, $suffix) = fileparse($source, qr/\.[^.]*/);
139 my $ext = $format . "_$lang";
140 $destdirOfSubdocuments = "$destdir/tmp_$ext" . "_$name"; # Global var, something TODO here
143 if(-d $destdirOfSubdocuments) {
144 rmtree($destdirOfSubdocuments);
146 mkpath($destdirOfSubdocuments); # for possibly included files
148 my %IncludedFiles = ();
150 "copy_only" => "copyonly",
151 "interpret" => "interpret");
153 addNewJob($source, $dest, "interpret", {}, \%IncludedFiles);
155 copyFoundSubdocuments(\%IncludedFiles);
157 #printCopiedDocuments(\%IncludedFiles);
160 ###########################################################
162 sub printCopiedDocuments($)
165 for my $k (keys %{$rFiles}) {
166 my $rJob = $rFiles->{$k};
167 for my $j ( values %type2hash) {
168 if (defined($rJob->{$j})) {
169 print "$j: $k->$rJob->{$j}, " . $rJob->{$j . "copied"} . "\n";
175 sub interpretedCopy($$$$)
177 my ($source, $dest, $destdirOfSubdocuments, $rFiles) = @_;
178 my $sourcedir = dirname($source);
181 diestack("could not read \"$source\"") if (!open(FI, $source));
182 diestack("could not write \"$dest\"") if (! open(FO, '>', $dest));
184 initLyxStack(\%font, $fontT, $inputEncoding);
187 my @path_errors = ();
188 while (my $l = <FI>) {
192 my $rStatus = checkLyxLine($l);
193 if ($rStatus->{found}) {
194 my $rF = $rStatus->{result};
195 if ($rStatus->{"filetype"} eq "replace_only") {
196 # e.g. if no files involved (font chage etc)
197 $l = join('', @{$rF});
200 my $filelist = $rStatus->{filelist};
201 my $fidx = $rStatus->{fileidx};
202 my $separator = $rStatus->{"separator"};
203 my $foundrelative = 0;
204 for my $f (@{$filelist}) {
205 my @isrel = isrelative($f,
211 if ($rStatus->{"filetype"} eq "prefix_only") {
212 $f = getNewNameOf("$sourcedir/$f", $rFiles);
215 my ($newname, $res1);
216 ($newname, $res1) = addFileCopyJob("$sourcedir/$f$ext",
217 "$destdirOfSubdocuments",
218 $rStatus->{"filetype"},
220 print "Added ($res1) file \"$sourcedir/$f$ext\" to be copied to \"$newname\"\n";
222 $newname =~ s/$ext$//;
230 # Non relative (e.g. with absolute path) file should exist
231 if ($rStatus->{"filetype"} eq "interpret") {
232 # filetype::interpret should be interpreted by lyx or latex and therefore emit error
233 # We prinnt a warning instead
234 print "WARNING: Interpreted file \"$f\" not found, at \"$source:$fi_line_no\"\n";
236 elsif ($rStatus->{"filetype"} eq "prefix_only") {
237 # filetype::prefix_only should be interpreted by latex
238 print "WARNING: Prefixed file \"$f\" not found, at \"$source:$fi_line_no\"\n";
241 # Collect the path-error-messages
242 push(@path_errors, "File \"$f(" . $rStatus->{"filetype"} . ")\" not found, at \"$source:$fi_line_no\"");
247 if ($foundrelative) {
248 $rF->[$fidx] = join($separator, @{$filelist});
249 $l = join('', @{$rF});
257 if (@path_errors > 0) {
258 for my $entry (@path_errors) {
259 print "ERROR: $entry\n";
261 diestack("Aborted because of path errors in \"$source\"");
268 sub copyFoundSubdocuments($)
276 for my $filename (keys %{$rFiles}) {
277 next if (! copyJobPending($filename, $rFiles));
278 $copylist{$filename} = 1;
280 for my $f (keys %copylist) {
281 # Second loop needed, because here $rFiles may change
282 my ($res1, @destfiles) = copyJob($f, $rFiles);
284 for my $destfile (@destfiles) {
285 print "res1 = $res1 for \"$f\" to be copied to $destfile\n";
288 } while($res > 0); # loop, while $rFiles changed
293 my ($source, $rFiles) = @_;
294 my $sourcedir = dirname($source);
298 for my $k (values %type2hash) {
299 if ($rFiles->{$source}->{$k}) {
300 if (! $rFiles->{$source}->{$k . "copied"}) {
301 $rFiles->{$source}->{$k . "copied"} = 1;
302 my $dest = $rFiles->{$source}->{$k};
304 if ($k eq "copyonly") {
305 diestack("Could not copy \"$source\" to \"$dest\"") if (! cp($source, $dest));
308 interpretedCopy($source, $dest, $destdirOfSubdocuments, $rFiles);
318 sub isrelativeFix($$$)
320 my ($f, $sourcedir, $ext) = @_;
322 return(1, $ext) if (-e "$sourcedir/$f$ext");
328 my ($f, $sourcedir, $ext) = @_;
330 if (ref($ext) eq "ARRAY") {
331 for my $ext2 (@{$ext}) {
332 my @res = isrelativeFix($f, $sourcedir, $ext2);
340 return(isrelativeFix($f, $sourcedir, $ext));
344 sub createTemporaryFileName($$)
346 my ($source, $destdir) = @_;
348 # get the basename to be used for the template
349 my ($name, $path, $suffix) = fileparse($source, qr/\.[^.]*/);
350 #print "source = $source, name = $name, path = $path, suffix = $suffix\n";
351 my $template = "xx_$name" . "_";
352 my $fname = File::Temp::tempnam($destdir, $template);
354 # Append extension from source
361 # Check, if file not copied yet
362 sub copyJobPending($$)
364 my ($f, $rFiles) = @_;
365 for my $t (values %type2hash) {
366 if (defined($rFiles->{$f}->{$t})) {
367 return 1 if (! $rFiles->{$f}->{$t . "copied"});
375 my ($source, $newname, $hashname, $rJob, $rFiles) = @_;
377 $rJob->{$hashname} = $newname;
378 $rJob->{$hashname . "copied"} = 0;
379 $rFiles->{$source} = $rJob;
382 sub addFileCopyJob($$$$)
384 my ($source, $destdirOfSubdocuments, $filetype, $rFiles) = @_;
385 my ($res, $newname) = (0, undef);
386 my $rJob = $rFiles->{$source};
388 my $hashname = $type2hash{$filetype};
389 if (! defined($hashname)) {
390 diestack("unknown filetype \"$filetype\"");
392 if (!defined($rJob->{$hashname})) {
394 createTemporaryFileName($source, $destdirOfSubdocuments),
395 "$hashname", $rJob, $rFiles);
398 $newname = $rJob->{$hashname};
399 return($newname, $res);
404 my ($f, $rFiles) = @_;
407 if (defined($rFiles->{$f})) {
408 for my $t (values %type2hash) {
409 if (defined($rFiles->{$f}->{$t})) {
410 $resultf = $rFiles->{$f}->{$t};
420 my ($languagefile, $rencoding) = @_;
422 if (open(FI, $languagefile)) {
423 while (my $l = <FI>) {
424 if ($l =~ /^Language/) {
425 my ($lng, $enc) = &getLangEntry();
427 $rencoding->{$lng} = $enc;
437 my ($rencoding) = @_;
443 for my $k (reverse sort keys %{$rencoding}) {
444 my @tag = split('_', $k);
445 if ($tag[0] eq $base) {
447 if ($rencoding->{$k} ne $enc) {
452 # new base, check that old base was OK
455 $rencoding->{$base} = $enc;
456 push(@klist2, @klist);
461 $enc = $rencoding->{$k};
466 # close handling for last entry too
468 $rencoding->{$base} = $enc;
469 push(@klist2, @klist);
472 for my $k (@klist2) {
473 delete($rencoding->{$k});
479 my ($lng, $enc) = (undef, undef);
480 while (my $l = <FI>) {
482 if ($l =~ /^\s*Encoding\s+([^ ]+)\s*$/) {
485 elsif ($l =~ /^\s*LangCode\s+([^ ]+)\s*$/) {
488 elsif ($l =~ /^\s*End\s*$/) {
492 if (defined($lng) && defined($enc)) {
496 return(undef, undef);