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, $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 "zh_CN") {
105 $font{roman} = "WenQuanYi Micro Hei";
106 $font{sans} = "WenQuanYi Micro Hei";
107 $font{typewriter} = "WenQuanYi Micro Hei";
109 elsif ($lang eq "ko" ) {
110 $font{roman} = "NanumGothic"; # NanumMyeongjo, NanumGothic Eco, NanumGothicCoding
111 $font{sans} = "NanumGothic";
112 $font{typewriter} = "NanumGothic";
114 elsif ($lang eq "ar" ) {
115 # available in 'fonts-sil-scheherazade' package
116 $font{roman} = "Scheherazade";
117 $font{sans} = "Scheherazade";
118 $font{typewriter} = "Scheherazade";
121 # default system fonts
122 $font{roman} = "FreeSerif";
123 $font{sans} = "FreeSans";
124 $font{typewriter} = "FreeMono";
127 elsif ($encodingT ne "default") {
128 # set input encoding to the requested value
130 "search" => '.*', # this will be substituted from '\inputencoding'-line
134 elsif (0) { # set to '1' to enable setting of inputencoding
137 if (defined($languageFile)) {
138 # The 2 lines below does not seem to have any effect
139 #&getlangs($languageFile, \%encoding);
140 #&simplifylangs(\%encoding);
142 if ($format =~ /^(pdf4)$/) { # xelatex
143 # set input encoding to 'ascii' always
145 "search" => '.*', # this will be substituted from '\inputencoding'-line
149 elsif ($format =~ /^(dvi3|pdf5)$/) { # (dvi)?lualatex
150 # when to set input encoding to 'ascii'?
151 if (defined($encoding{$lang})) {
153 "search" => '.*', # this will be substituted from '\inputencoding'-line
154 "out" => $encoding{$lang},
160 my $sourcedir = dirname($source);
161 my $destdir = dirname($dest);
163 diestack("could not make dir \"$destdir\"") if (! mkpath $destdir);
166 my $destdirOfSubdocuments;
168 my ($name, $pat, $suffix) = fileparse($source, qr/\.[^.]*/);
169 my $ext = $format . "_$lang";
170 $destdirOfSubdocuments = "$destdir/tmp_$ext" . "_$name"; # Global var, something TODO here
173 if(-d $destdirOfSubdocuments) {
174 rmtree($destdirOfSubdocuments);
176 mkpath($destdirOfSubdocuments); # for possibly included files
178 my %IncludedFiles = ();
180 "copy_only" => "copyonly",
181 "interpret" => "interpret");
183 addNewJob($source, $dest, "interpret", {}, \%IncludedFiles);
185 copyFoundSubdocuments(\%IncludedFiles);
187 #printCopiedDocuments(\%IncludedFiles);
190 ###########################################################
192 sub printCopiedDocuments($)
195 for my $k (keys %{$rFiles}) {
196 my $rJob = $rFiles->{$k};
197 for my $j ( values %type2hash) {
198 if (defined($rJob->{$j})) {
199 print "$j: $k->$rJob->{$j}, " . $rJob->{$j . "copied"} . "\n";
205 sub interpretedCopy($$$$)
207 my ($source, $dest, $destdirOfSubdocuments, $rFiles) = @_;
208 my $sourcedir = dirname($source);
211 diestack("could not read \"$source\"") if (!open(FI, $source));
212 diestack("could not write \"$dest\"") if (! open(FO, '>', $dest));
214 initLyxStack(\%font, $fontT, $inputEncoding);
216 while (my $l = <FI>) {
219 my $rStatus = checkLyxLine($l);
220 if ($rStatus->{found}) {
221 my $rF = $rStatus->{result};
222 if ($rStatus->{"filetype"} eq "replace_only") {
223 # e.g. if no files involved (font chage etc)
224 $l = join('', @{$rF});
227 my $filelist = $rStatus->{filelist};
228 my $fidx = $rStatus->{fileidx};
229 my $separator = $rStatus->{"separator"};
230 my $foundrelative = 0;
231 for my $f (@{$filelist}) {
232 my @isrel = isrelative($f,
238 if ($rStatus->{"filetype"} eq "prefix_only") {
239 $f = getNewNameOf("$sourcedir/$f", $rFiles);
242 my ($newname, $res1);
243 ($newname, $res1) = addFileCopyJob("$sourcedir/$f$ext",
244 "$destdirOfSubdocuments",
245 $rStatus->{"filetype"},
247 print "Added ($res1) file \"$sourcedir/$f$ext\" to be copied to \"$newname\"\n";
249 $newname =~ s/$ext$//;
257 # Non relative (e.g. with absolute path) file should exist
258 print "File $f not found\n";
263 if ($foundrelative) {
264 $rF->[$fidx] = join($separator, @{$filelist});
265 $l = join('', @{$rF});
278 sub copyFoundSubdocuments($)
286 for my $filename (keys %{$rFiles}) {
287 next if (! copyJobPending($filename, $rFiles));
288 $copylist{$filename} = 1;
290 for my $f (keys %copylist) {
291 # Second loop needed, because here $rFiles may change
292 my ($res1, @destfiles) = copyJob($f, $rFiles);
294 for my $destfile (@destfiles) {
295 print "res1 = $res1 for \"$f\" to be copied to $destfile\n";
298 } while($res > 0); # loop, while $rFiles changed
303 my ($source, $rFiles) = @_;
304 my $sourcedir = dirname($source);
308 for my $k (values %type2hash) {
309 if ($rFiles->{$source}->{$k}) {
310 if (! $rFiles->{$source}->{$k . "copied"}) {
311 $rFiles->{$source}->{$k . "copied"} = 1;
312 my $dest = $rFiles->{$source}->{$k};
314 if ($k eq "copyonly") {
315 diestack("Could not copy \"$source\" to \"$dest\"") if (! cp($source, $dest));
318 interpretedCopy($source, $dest, $destdirOfSubdocuments, $rFiles);
328 sub isrelativeFix($$$)
330 my ($f, $sourcedir, $ext) = @_;
332 return(1, $ext) if (-e "$sourcedir/$f$ext");
338 my ($f, $sourcedir, $ext) = @_;
340 if (ref($ext) eq "ARRAY") {
341 for my $ext2 (@{$ext}) {
342 my @res = isrelativeFix($f, $sourcedir, $ext2);
350 return(isrelativeFix($f, $sourcedir, $ext));
354 sub createTemporaryFileName($$)
356 my ($source, $destdir) = @_;
358 # get the basename to be used for the template
359 my ($name, $path, $suffix) = fileparse($source, qr/\.[^.]*/);
360 #print "source = $source, name = $name, path = $path, suffix = $suffix\n";
361 my $template = "xx_$name" . "_";
362 my $fname = File::Temp::tempnam($destdir, $template);
364 # Append extension from source
371 # Check, if file not copied yet
372 sub copyJobPending($$)
374 my ($f, $rFiles) = @_;
375 for my $t (values %type2hash) {
376 if (defined($rFiles->{$f}->{$t})) {
377 return 1 if (! $rFiles->{$f}->{$t . "copied"});
385 my ($source, $newname, $hashname, $rJob, $rFiles) = @_;
387 $rJob->{$hashname} = $newname;
388 $rJob->{$hashname . "copied"} = 0;
389 $rFiles->{$source} = $rJob;
392 sub addFileCopyJob($$$$)
394 my ($source, $destdirOfSubdocuments, $filetype, $rFiles) = @_;
395 my ($res, $newname) = (0, undef);
396 my $rJob = $rFiles->{$source};
398 my $hashname = $type2hash{$filetype};
399 if (! defined($hashname)) {
400 diestack("unknown filetype \"$filetype\"");
402 if (!defined($rJob->{$hashname})) {
404 createTemporaryFileName($source, $destdirOfSubdocuments),
405 "$hashname", $rJob, $rFiles);
408 $newname = $rJob->{$hashname};
409 return($newname, $res);
414 my ($f, $rFiles) = @_;
417 if (defined($rFiles->{$f})) {
418 for my $t (values %type2hash) {
419 if (defined($rFiles->{$f}->{$t})) {
420 $resultf = $rFiles->{$f}->{$t};
430 my ($languagefile, $rencoding) = @_;
432 if (open(FI, $languagefile)) {
433 while (my $l = <FI>) {
434 if ($l =~ /^Language/) {
435 my ($lng, $enc) = &getLangEntry();
437 $rencoding->{$lng} = $enc;
447 my ($rencoding) = @_;
453 for my $k (reverse sort keys %{$rencoding}) {
454 my @tag = split('_', $k);
455 if ($tag[0] eq $base) {
457 if ($rencoding->{$k} ne $enc) {
462 # new base, check that old base was OK
465 $rencoding->{$base} = $enc;
466 push(@klist2, @klist);
471 $enc = $rencoding->{$k};
476 # close handling for last entry too
478 $rencoding->{$base} = $enc;
479 push(@klist2, @klist);
482 for my $k (@klist2) {
483 delete($rencoding->{$k});
489 my ($lng, $enc) = (undef, undef);
490 while (my $l = <FI>) {
492 if ($l =~ /^\s*Encoding\s+([^ ]+)\s*$/) {
495 elsif ($l =~ /^\s*LangCode\s+([^ ]+)\s*$/) {
498 elsif ($l =~ /^\s*End\s*$/) {
502 if (defined($lng) && defined($enc)) {
506 return(undef, undef);