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>) {
210 my $rStatus = checkLyxLine($l);
211 if ($rStatus->{found}) {
212 my $rF = $rStatus->{result};
213 if ($rStatus->{"filetype"} eq "replace_only") {
214 # e.g. if no files involved (font chage etc)
215 $l = join('', @{$rF});
218 my $filelist = $rStatus->{filelist};
219 my $fidx = $rStatus->{fileidx};
220 my $separator = $rStatus->{"separator"};
221 my $foundrelative = 0;
222 for my $f (@{$filelist}) {
223 my @isrel = isrelative($f,
229 if ($rStatus->{"filetype"} eq "prefix_only") {
230 $f = getNewNameOf("$sourcedir/$f", $rFiles);
233 my ($newname, $res1);
234 ($newname, $res1) = addFileCopyJob("$sourcedir/$f$ext",
235 "$destdirOfSubdocuments",
236 $rStatus->{"filetype"},
238 print "Added ($res1) file \"$sourcedir/$f$ext\" to be copied to \"$newname\"\n";
240 $newname =~ s/$ext$//;
247 if ($foundrelative) {
248 $rF->[$fidx] = join($separator, @{$filelist});
249 $l = join('', @{$rF});
262 sub copyFoundSubdocuments($)
270 for my $filename (keys %{$rFiles}) {
271 next if (! copyJobPending($filename, $rFiles));
272 $copylist{$filename} = 1;
274 for my $f (keys %copylist) {
275 # Second loop needed, because here $rFiles may change
276 my ($res1, @destfiles) = copyJob($f, $rFiles);
278 for my $destfile (@destfiles) {
279 print "res1 = $res1 for \"$f\" to be copied to $destfile\n";
282 } while($res > 0); # loop, while $rFiles changed
287 my ($source, $rFiles) = @_;
288 my $sourcedir = dirname($source);
292 for my $k (values %type2hash) {
293 if ($rFiles->{$source}->{$k}) {
294 if (! $rFiles->{$source}->{$k . "copied"}) {
295 $rFiles->{$source}->{$k . "copied"} = 1;
296 my $dest = $rFiles->{$source}->{$k};
298 if ($k eq "copyonly") {
299 diestack("Could not copy \"$source\" to \"$dest\"") if (! cp($source, $dest));
302 interpretedCopy($source, $dest, $destdirOfSubdocuments, $rFiles);
312 sub isrelativeFix($$$)
314 my ($f, $sourcedir, $ext) = @_;
316 return(1, $ext) if (-e "$sourcedir/$f$ext");
322 my ($f, $sourcedir, $ext) = @_;
324 if (ref($ext) eq "ARRAY") {
325 for my $ext2 (@{$ext}) {
326 my @res = isrelativeFix($f, $sourcedir, $ext2);
334 return(isrelativeFix($f, $sourcedir, $ext));
338 sub createTemporaryFileName($$)
340 my ($source, $destdir) = @_;
342 # get the basename to be used for the template
343 my ($name, $path, $suffix) = fileparse($source, qr/\.[^.]*/);
344 #print "source = $source, name = $name, path = $path, suffix = $suffix\n";
345 my $template = "xx_$name" . "_";
346 my $fname = File::Temp::tempnam($destdir, $template);
348 # Append extension from source
355 # Check, if file not copied yet
356 sub copyJobPending($$)
358 my ($f, $rFiles) = @_;
359 for my $t (values %type2hash) {
360 if (defined($rFiles->{$f}->{$t})) {
361 return 1 if (! $rFiles->{$f}->{$t . "copied"});
369 my ($source, $newname, $hashname, $rJob, $rFiles) = @_;
371 $rJob->{$hashname} = $newname;
372 $rJob->{$hashname . "copied"} = 0;
373 $rFiles->{$source} = $rJob;
376 sub addFileCopyJob($$$$)
378 my ($source, $destdirOfSubdocuments, $filetype, $rFiles) = @_;
379 my ($res, $newname) = (0, undef);
380 my $rJob = $rFiles->{$source};
382 my $hashname = $type2hash{$filetype};
383 if (! defined($hashname)) {
384 diestack("unknown filetype \"$filetype\"");
386 if (!defined($rJob->{$hashname})) {
388 createTemporaryFileName($source, $destdirOfSubdocuments),
389 "$hashname", $rJob, $rFiles);
392 $newname = $rJob->{$hashname};
393 return($newname, $res);
398 my ($f, $rFiles) = @_;
401 if (defined($rFiles->{$f})) {
402 for my $t (values %type2hash) {
403 if (defined($rFiles->{$f}->{$t})) {
404 $resultf = $rFiles->{$f}->{$t};
414 my ($languagefile, $rencoding) = @_;
416 if (open(FI, $languagefile)) {
417 while (my $l = <FI>) {
418 if ($l =~ /^Language/) {
419 my ($lng, $enc) = &getLangEntry();
421 $rencoding->{$lng} = $enc;
431 my ($rencoding) = @_;
437 for my $k (reverse sort keys %{$rencoding}) {
438 my @tag = split('_', $k);
439 if ($tag[0] eq $base) {
441 if ($rencoding->{$k} ne $enc) {
446 # new base, check that old base was OK
449 $rencoding->{$base} = $enc;
450 push(@klist2, @klist);
455 $enc = $rencoding->{$k};
460 # close handling for last entry too
462 $rencoding->{$base} = $enc;
463 push(@klist2, @klist);
466 for my $k (@klist2) {
467 delete($rencoding->{$k});
473 my ($lng, $enc) = (undef, undef);
474 while (my $l = <FI>) {
476 if ($l =~ /^\s*Encoding\s+([^ ]+)\s*$/) {
479 elsif ($l =~ /^\s*LangCode\s+([^ ]+)\s*$/) {
482 elsif ($l =~ /^\s*End\s*$/) {
486 if (defined($lng) && defined($enc)) {
490 return(undef, undef);