texinfo-commits
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

branch master updated: Handle @image.


From: Patrice Dumas
Subject: branch master updated: Handle @image.
Date: Thu, 12 Aug 2021 12:32:12 -0400

This is an automated email from the git hooks/post-receive script.

pertusus pushed a commit to branch master
in repository texinfo.

The following commit(s) were added to refs/heads/master by this push:
     new ecb1fd6  Handle @image.
ecb1fd6 is described below

commit ecb1fd663db7e45945bbcc2d8c4177deab70890b
Author: Patrice Dumas <pertusus@free.fr>
AuthorDate: Thu Aug 12 18:31:53 2021 +0200

    Handle @image.
---
 tp/Texinfo/Convert/LaTeX.pm                        | 166 +++++++++++----------
 .../res_parser/formatting_latex/formatting.tex     |  93 +++++-------
 2 files changed, 126 insertions(+), 133 deletions(-)

diff --git a/tp/Texinfo/Convert/LaTeX.pm b/tp/Texinfo/Convert/LaTeX.pm
index f5f6620..5ea8692 100644
--- a/tp/Texinfo/Convert/LaTeX.pm
+++ b/tp/Texinfo/Convert/LaTeX.pm
@@ -39,6 +39,8 @@ use if $] >= 5.012, feature => qw(unicode_strings);
 
 use strict;
 
+use File::Spec;
+
 use Texinfo::Convert::Converter;
 use Texinfo::Common;
 use Texinfo::Convert::Texinfo;
@@ -139,6 +141,10 @@ foreach my $misc_command (keys(%misc_commands)) {
     unless ($formatting_misc_commands{$misc_command});
 }
 
+# from \def\Gin@extensions in graphics-def/pdftex.def
+my @LaTeX_image_extensions = (
+'pdf','png','jpg','mps','jpeg','jbig2','jb2','PDF','PNG','JPG','JPEG','JBIG2','JB2');
+
 my %section_map = (
    'part' => 'part',
    'chapter' => 'chapter',
@@ -530,6 +536,7 @@ sub _latex_header {
   # T1 fontenc for \DH, \guillemotleft
   # eurosym for \euro
   # textcomp for \textdegree in older LaTeX
+  # graphicx for \includegraphics
   my $header = 
 '\documentclass{book}
 \usepackage{makeidx}\makeindex
@@ -538,7 +545,19 @@ sub _latex_header {
 \usepackage[gen]{eurosym}
 \usepackage[T1]{fontenc}
 \usepackage{textcomp}
+\usepackage{graphicx}
+
 ';
+  # this is in order to be able to run pdflatex even
+  # if files do not exist, or filenames cannot be
+  # processed by LaTeX
+  if ($self->get_conf('TEST')) {
+    $header .=
+'
+\renewcommand{\includegraphics}[1]{FIG #1}
+
+';
+  }
   if ($self->{'output_encoding_name'}) {
     my $encoding = $self->{'output_encoding_name'};
     if (defined($LaTeX_encoding_names_map{$encoding})) {
@@ -693,83 +712,6 @@ sub _node($$)
 }
 
 
-sub _image_text($$$)
-{
-  my ($self, $root, $basefile) = @_;
-
-  my $txt_file = $self->Texinfo::Common::locate_include_file($basefile.'.txt');
-  if (!defined($txt_file)) {
-    return undef;
-  } else {
-    my $filehandle = do { local *FH };
-    if (open ($filehandle, $txt_file)) {
-      my $enc = $root->{'extra'}->{'input_perl_encoding'};
-      binmode($filehandle, ":encoding($enc)")
-        if ($enc);
-      my $result = '';
-      my $max_width = 0;
-      while (<$filehandle>) {
-        my $width = Texinfo::Convert::Unicode::string_width($_);
-        if ($width > $max_width) {
-          $max_width = $width;
-        }
-        $result .= $_;
-      }
-      # remove last end of line
-      chomp ($result);
-      if (!close ($filehandle)) {
-        $self->document_warn(sprintf(__("error on closing image text file %s: 
%s"),
-                                     $txt_file, $!));
-      }
-      return ($result, $max_width);
-    } else {
-      $self->line_warn(sprintf(__("\@image file `%s' unreadable: %s"), 
-                               $txt_file, $!), $root->{'line_nr'});
-    }
-  }
-  return undef;
-}
-
-sub _image_formatted_text($$$$)
-{
-  my ($self, $root, $basefile, $text) = @_;
-
-  my $result;
-  if (defined($text)) {
-    $result = $text;
-  } elsif (defined($root->{'args'}->[3])
-      and @{$root->{'args'}->[3]->{'contents'}}) {
-    $result = '[' .Texinfo::Convert::Text::convert(
-      {'contents' => $root->{'args'}->[3]->{'contents'}},
-      $self->{'convert_text_options'}) .']';
-  } else {
-    $self->line_warn(sprintf(__(
-                    "could not find \@image file `%s.txt' nor alternate text"),
-                             $basefile), $root->{'line_nr'});
-    $result = '['.$basefile.']';
-  }
-  return _protect_text($self, $result);
-}
-
-sub _image($$)
-{
-  my ($self, $root) = @_;
-
-  if (defined($root->{'args'}->[0])
-        and @{$root->{'args'}->[0]->{'contents'}}) {
-    my $basefile = Texinfo::Convert::Text::convert(
-     {'contents' => $root->{'args'}->[0]->{'contents'}},
-     {'code' => 1, %{$self->{'convert_text_options'}}});
-    my ($text, $width) = $self->_image_text($root, $basefile);
-    my $result = $self->_image_formatted_text($root, $basefile, $text);
-    if (!defined($width)) {
-      $width = Texinfo::Convert::Unicode::string_width($result);
-    }
-    return $result;
-  }
-  return '';
-}
-
 sub _get_form_feeds($)
 {
   my $form_feeds = shift;
@@ -1004,8 +946,74 @@ sub _convert($$)
       $result .= $root->{'extra'}->{'delimiter'};
       return $result;
     } elsif ($command eq 'image') {
-      my $image = $self->_image($root);
-      $result .= $image; 
+      if (defined($root->{'args'}->[0])
+          and @{$root->{'args'}->[0]->{'contents'}}) {
+        # distinguish text basefile used to find the file and
+        # converted basefile with special characters escaped
+        my $basefile = Texinfo::Convert::Text::convert(
+         {'contents' => $root->{'args'}->[0]->{'contents'}},
+         {'code' => 1, %{$self->{'convert_text_options'}}});
+        # FIXME not clear at all what can be in filenames here,
+        # what should be escaped and how
+        my $converted_basefile = _protect_text($self, $basefile);
+
+        my $image_file;
+        foreach my $extension (@LaTeX_image_extensions) {
+          my $located_file =
+            
$self->Texinfo::Common::locate_include_file("$basefile.$extension");
+          if (defined($located_file)) {
+            my ($image_volume, $image_directories, $image_filename)
+                 = File::Spec->splitpath($located_file);
+            # using basefile with escaped characters
+            $image_file = File::Spec->catpath($image_volume,
+                                       $image_directories, 
$converted_basefile);
+          }
+        }
+        if (not defined($image_file)) {
+          $image_file = $converted_basefile;
+        }
+        my $width;
+        if ((@{$root->{'args'}} >= 2)
+              and defined($root->{'args'}->[1])
+              and @{$root->{'args'}->[1]->{'contents'}}){
+          push @{$self->{'style_context'}->[-1]->{'context'}}, 'raw';
+          $width = _convert($self, {'contents'
+                         => $root->{'args'}->[1]->{'contents'}});
+          my $old_context = pop @{$self->{'style_context'}->[-1]->{'context'}};
+          die if ($old_context ne 'raw');
+          if ($width !~ /\S/) {
+            $width = undef;
+          }
+        }
+        my $height;
+        if ((@{$root->{'args'}} >= 3)
+              and defined($root->{'args'}->[2])
+              and @{$root->{'args'}->[2]->{'contents'}}) {
+          push @{$self->{'style_context'}->[-1]->{'context'}}, 'raw';
+          $height = _convert($self, {'contents'
+                         => $root->{'args'}->[2]->{'contents'}});
+          my $old_context = pop @{$self->{'style_context'}->[-1]->{'context'}};
+          die if ($old_context ne 'raw');
+          if ($height !~ /\S/) {
+            $height = undef;
+          }
+        }
+        $result .= "\\includegraphics";
+        if (defined($width) or defined($height)) {
+          $result .= "[";
+          if (defined($width)) {
+            $result .= "width=$width";
+            if (defined($height)) {
+              $result .= ",";
+            }
+          }
+          if (defined($height)) {
+            $result .= "height=$height";
+          }
+          $result .= "]";
+        }
+        $result .= "{$image_file}";
+      }
       return $result;
     } elsif ($command eq 'email') {
       # nothing is output for email, instead the command is substituted.
diff --git a/tp/tests/layout/res_parser/formatting_latex/formatting.tex 
b/tp/tests/layout/res_parser/formatting_latex/formatting.tex
index c7a83e7..6c70be5 100644
--- a/tp/tests/layout/res_parser/formatting_latex/formatting.tex
+++ b/tp/tests/layout/res_parser/formatting_latex/formatting.tex
@@ -4,6 +4,12 @@
 \usepackage{amsmath}
 \usepackage[gen]{eurosym}
 \usepackage[T1]{fontenc}
+\usepackage{textcomp}
+\usepackage{graphicx}
+
+
+\renewcommand{\includegraphics}[1]{FIG #1}
+
 \usepackage[utf8]{inputenc}
 \begin{document}
 \label{anchor:Top}Insertcopying in normal text
@@ -214,20 +220,14 @@ but , ,\@
 }
 
 
-\texttt{@image\{f{-}{-}ile\}} ggg
-aaa
-\texttt{@image\{f{-}{-}ile,l{-}{-}i\}} ggg
-aaa
-\texttt{@image\{f{-}{-}ile,,l{-}{-}e\}} ggg
-aaa
-\texttt{@image\{f{-}{-}ile,,,alt\}} ggg
-aaa
-\texttt{@image\{f{-}{-}ile,,,,.e-d-xt\}} ggg
-aaa
-\texttt{@image\{f{-}{-}ile,aze,az,alt,.e{-}{-}xt\}} ggg
-aaa
-\texttt{@image\{f-ile,aze,,a{-}{-}lt\}} [a-lt]
-\texttt{@image\{@file\{f{-}{-}ile\}@@@.,aze,az,alt,@file\{.file ext\} 
e{-}{-}xt@\}} [alt]
+\texttt{@image\{f{-}{-}ile\}} \includegraphics{layout/f--ile}
+\texttt{@image\{f{-}{-}ile,l{-}{-}i\}} 
\includegraphics[width=l--i]{layout/f--ile}
+\texttt{@image\{f{-}{-}ile,,l{-}{-}e\}} 
\includegraphics[height=l--e]{layout/f--ile}
+\texttt{@image\{f{-}{-}ile,,,alt\}} \includegraphics{layout/f--ile}
+\texttt{@image\{f{-}{-}ile,,,,.e-d-xt\}} \includegraphics{layout/f--ile}
+\texttt{@image\{f{-}{-}ile,aze,az,alt,.e{-}{-}xt\}} 
\includegraphics[width=aze,height=az]{layout/f--ile}
+\texttt{@image\{f-ile,aze,,a{-}{-}lt\}} \includegraphics[width=aze]{f-ile}
+\texttt{@image\{@file\{f{-}{-}ile\}@@@.,aze,az,alt,@file\{.file ext\} 
e{-}{-}xt@\}} \includegraphics[width=aze,height=az]{f--ile@.}
 
 
 \texttt{@clicksequence\{click @click\{\} A\}} click $\rightarrow{}$ A
@@ -817,10 +817,9 @@ in verbatim ''
 \texttt{@math\{{-}{-}a@minus\{\} \{\textbackslash{}frac\{1\}\{2\}\}\}} $--a- 
{\frac{1}{2}}$
 
 
-\texttt{@image\{f-ile,,,alt@verb\{:jk \_" \%\@\}\}} [altjk \_" \%@]
-\texttt{@image\{f{-}{-}ile,aze,az,@verb\{:jk \_" \%@:\} @b\{in b 
"\},e{-}{-}xt\}} ggg
-aaa
-\texttt{@image\{file@verb\{:jk \_" \%@:\},,,alt@verb\{:jk \_" \%@:\}\}} [altjk 
\_" \%@]
+\texttt{@image\{f-ile,,,alt@verb\{:jk \_" \%\@\}\}} \includegraphics{f-ile}
+\texttt{@image\{f{-}{-}ile,aze,az,@verb\{:jk \_" \%@:\} @b\{in b 
"\},e{-}{-}xt\}} \includegraphics[width=aze,height=az]{layout/f--ile}
+\texttt{@image\{file@verb\{:jk \_" \%@:\},,,alt@verb\{:jk \_" \%@:\}\}} 
\includegraphics{filejk \_" \%@}
 
 
 Somehow invalid use of @,:\leavevmode{}\\
@@ -1142,20 +1141,14 @@ but , ,\@
 }
 
 
-\texttt{@image\{f{-}{-}ile\}} ggg
-aaa
-\texttt{@image\{f{-}{-}ile,l{-}{-}i\}} ggg
-aaa
-\texttt{@image\{f{-}{-}ile,,l{-}{-}e\}} ggg
-aaa
-\texttt{@image\{f{-}{-}ile,,,alt\}} ggg
-aaa
-\texttt{@image\{f{-}{-}ile,,,,.e-d-xt\}} ggg
-aaa
-\texttt{@image\{f{-}{-}ile,aze,az,alt,.e{-}{-}xt\}} ggg
-aaa
-\texttt{@image\{f-ile,aze,,a{-}{-}lt\}} [a-lt]
-\texttt{@image\{@file\{f{-}{-}ile\}@@@.,aze,az,alt,@file\{.file ext\} 
e{-}{-}xt@\}} [alt]
+\texttt{@image\{f{-}{-}ile\}} \includegraphics{layout/f--ile}
+\texttt{@image\{f{-}{-}ile,l{-}{-}i\}} 
\includegraphics[width=l--i]{layout/f--ile}
+\texttt{@image\{f{-}{-}ile,,l{-}{-}e\}} 
\includegraphics[height=l--e]{layout/f--ile}
+\texttt{@image\{f{-}{-}ile,,,alt\}} \includegraphics{layout/f--ile}
+\texttt{@image\{f{-}{-}ile,,,,.e-d-xt\}} \includegraphics{layout/f--ile}
+\texttt{@image\{f{-}{-}ile,aze,az,alt,.e{-}{-}xt\}} 
\includegraphics[width=aze,height=az]{layout/f--ile}
+\texttt{@image\{f-ile,aze,,a{-}{-}lt\}} \includegraphics[width=aze]{f-ile}
+\texttt{@image\{@file\{f{-}{-}ile\}@@@.,aze,az,alt,@file\{.file ext\} 
e{-}{-}xt@\}} \includegraphics[width=aze,height=az]{f--ile@.}
 
 
 \texttt{@clicksequence\{click @click\{\} A\}} click $\rightarrow{}$ A
@@ -1745,10 +1738,9 @@ in verbatim ''
 \texttt{@math\{{-}{-}a@minus\{\} \{\textbackslash{}frac\{1\}\{2\}\}\}} $--a- 
{\frac{1}{2}}$
 
 
-\texttt{@image\{f-ile,,,alt@verb\{:jk \_" \%\@\}\}} [altjk \_" \%@]
-\texttt{@image\{f{-}{-}ile,aze,az,@verb\{:jk \_" \%@:\} @b\{in b 
"\},e{-}{-}xt\}} ggg
-aaa
-\texttt{@image\{file@verb\{:jk \_" \%@:\},,,alt@verb\{:jk \_" \%@:\}\}} [altjk 
\_" \%@]
+\texttt{@image\{f-ile,,,alt@verb\{:jk \_" \%\@\}\}} \includegraphics{f-ile}
+\texttt{@image\{f{-}{-}ile,aze,az,@verb\{:jk \_" \%@:\} @b\{in b 
"\},e{-}{-}xt\}} \includegraphics[width=aze,height=az]{layout/f--ile}
+\texttt{@image\{file@verb\{:jk \_" \%@:\},,,alt@verb\{:jk \_" \%@:\}\}} 
\includegraphics{filejk \_" \%@}
 
 
 Somehow invalid use of @,:\leavevmode{}\\
@@ -2048,20 +2040,14 @@ but , ,\@
 \texttt{@footnote\{in footnote2\}} \footnote{in footnote2
 
 }
-\texttt{@image\{f{-}{-}ile\}} ggg
-aaa
-\texttt{@image\{f{-}{-}ile,l{-}{-}i\}} ggg
-aaa
-\texttt{@image\{f{-}{-}ile,,l{-}{-}e\}} ggg
-aaa
-\texttt{@image\{f{-}{-}ile,,,alt\}} ggg
-aaa
-\texttt{@image\{f{-}{-}ile,,,,.e-d-xt\}} ggg
-aaa
-\texttt{@image\{f{-}{-}ile,aze,az,alt,.e{-}{-}xt\}} ggg
-aaa
-\texttt{@image\{f-ile,aze,,a{-}{-}lt\}} [a-lt]
-\texttt{@image\{@file\{f{-}{-}ile\}@@@.,aze,az,alt,@file\{.file ext\} 
e{-}{-}xt@\}} [alt]
+\texttt{@image\{f{-}{-}ile\}} \includegraphics{layout/f--ile}
+\texttt{@image\{f{-}{-}ile,l{-}{-}i\}} 
\includegraphics[width=l--i]{layout/f--ile}
+\texttt{@image\{f{-}{-}ile,,l{-}{-}e\}} 
\includegraphics[height=l--e]{layout/f--ile}
+\texttt{@image\{f{-}{-}ile,,,alt\}} \includegraphics{layout/f--ile}
+\texttt{@image\{f{-}{-}ile,,,,.e-d-xt\}} \includegraphics{layout/f--ile}
+\texttt{@image\{f{-}{-}ile,aze,az,alt,.e{-}{-}xt\}} 
\includegraphics[width=aze,height=az]{layout/f--ile}
+\texttt{@image\{f-ile,aze,,a{-}{-}lt\}} \includegraphics[width=aze]{f-ile}
+\texttt{@image\{@file\{f{-}{-}ile\}@@@.,aze,az,alt,@file\{.file ext\} 
e{-}{-}xt@\}} \includegraphics[width=aze,height=az]{f--ile@.}
 \texttt{@clicksequence\{click @click\{\} A\}} click $\rightarrow{}$ A
 After clickstyle $\Rightarrow{}$
 \texttt{@clicksequence\{click @click\{\} A\}} click $\Rightarrow{}$ A
@@ -2823,10 +2809,9 @@ in verbatim ''
 \texttt{@abbr\{@'E{-}{-}. @comma\{\}A., @'Etude{-}{-}@comma\{\} @b\{Autonome\} 
\}} \'{E}--.\@ ,A.\@ (\'{E}tude--, \textbf{Autonome})
 \texttt{@abbr\{@'E{-}{-}. @comma\{\}A.\}} \'{E}--.\@ ,A.\@
 \texttt{@math\{{-}{-}a@minus\{\} \{\textbackslash{}frac\{1\}\{2\}\}\}} $--a- 
{\frac{1}{2}}$
-\texttt{@image\{f-ile,,,alt@verb\{:jk \_" \%\@\}\}} [altjk \_" \%@]
-\texttt{@image\{f{-}{-}ile,aze,az,@verb\{:jk \_" \%@:\} @b\{in b 
"\},e{-}{-}xt\}} ggg
-aaa
-\texttt{@image\{file@verb\{:jk \_" \%@:\},,,alt@verb\{:jk \_" \%@:\}\}} [altjk 
\_" \%@]
+\texttt{@image\{f-ile,,,alt@verb\{:jk \_" \%\@\}\}} \includegraphics{f-ile}
+\texttt{@image\{f{-}{-}ile,aze,az,@verb\{:jk \_" \%@:\} @b\{in b 
"\},e{-}{-}xt\}} \includegraphics[width=aze,height=az]{layout/f--ile}
+\texttt{@image\{file@verb\{:jk \_" \%@:\},,,alt@verb\{:jk \_" \%@:\}\}} 
\includegraphics{filejk \_" \%@}
 Somehow invalid use of @,:\leavevmode{}\\
 @, \c{}
 \leavevmode{}\\



reply via email to

[Prev in Thread] Current Thread [Next in Thread]