[latex3-commits] [l3svn] 02/02: Read/extract bounding box from .eps/.(x)bb files

noreply at latex-project.org noreply at latex-project.org
Sat May 13 17:43:25 CEST 2017


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

joseph pushed a commit to branch master
in repository l3svn.

commit a5f481ba61337df9e7177cc575a95a8e9919d2aa
Author: Joseph Wright <joseph.wright at morningstar2.co.uk>
Date:   Sat May 13 16:40:17 2017 +0100

    Read/extract bounding box from .eps/.(x)bb files
    
    This is the same concept as \Gread at eps but implemented using e-TeX,
    so no need to loop over chars, etc. It also covers the functionality
    needed to support dvipdfmx using extractbb for binary image files.
    
    At present the use of pipe syntax isn't covered by l3file: I have
    gone for primitives rather than (ab)use \ior_open:Nn. Thoughts on
    this welcome: should this be treated 'like a file' or not?
---
 l3trial/l3image/l3image.dtx |  240 +++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 240 insertions(+)

diff --git a/l3trial/l3image/l3image.dtx b/l3trial/l3image/l3image.dtx
index 75fe7b5..2762fe8 100644
--- a/l3trial/l3image/l3image.dtx
+++ b/l3trial/l3image/l3image.dtx
@@ -52,6 +52,92 @@
 %
 % \section{\pkg{l3image} documentation}
 %
+% \subsection{Image bounding boxes}
+%
+% \begin{function}{hiresbb}
+%   \begin{syntax}
+%     |hiresbb| = |true|\verb"|"|false|
+%   \end{syntax}
+%   This option determines whether bounding box searches use |HiResBoundingBox|
+%   lines when reading |.eps| or |.bb| files.
+% \end{function}
+%
+% \subsection{Internal functions}
+%
+% Inclusion of image files requires a range of low-level data be passed to
+% the driver layer. Individual drivers also require some common routines
+% which in themselves contain no driver-specific code. This functionality
+% is made available here.
+%
+% \begin{variable}[int]{\l__image_bb_found_bool}
+%   Boolean indicated that \cs{__image_extract_bb:n}/\cs{__image_read_bb:n}
+%   succeeding in obtaining bound box data for an image.
+% \end{variable}
+%
+% \begin{variable}[int]
+%   {
+%     \l__image_llx_dim, \l__image_lly_dim ,
+%     \l__image_urx_dim, \l__image_ury_dim
+%   }
+%   Dimensions holding the bounding box of the image file: |ll| = lower left,
+%   |ur| = upper right, |x| and |y| for horizontal and vertical, respectively.
+%   These values may be extracted/read from the image files themselves
+%   (see \cs{__image_extract_bb:n} and \cs{__image_read_bb:n}), or may
+%   be supplied by [TO DO].
+% \end{variable}
+%
+% \begin{function}{\__image_extract_bb:n}
+%   \begin{syntax}
+%     \cs{__image_extract_bb:n} \meta{file}
+%   \end{syntax}
+%   Extracts bounding box data for the image \meta{file} using the |extractbb|
+%   utility, and stores the returned information in the dimensions
+%   \cs{l__image_llx_dim}, \emph{etc.} The boolean \cs{l__image_bb_found_bool}
+%   is set |true| on successful determination of a bounding box. In the case
+%   where no data is obtained, this variable is |false| and the four dimensions
+%   are set to place holders: |llx|/|lly| to $0\,\mathrm{bp}$ and |urx|/|ury| to
+%   $72\,\mathrm{bp}$.
+%
+%   The \meta{file} name should be fully-qualified and sanitized: no search
+%   or other manipulation is carried out at this level. No check is made on
+%   the file \emph{type} at this stage: it is assumed that the driver code
+%   using this function has made such a check. File types such as |.pdf| and
+%   |.jpg| are appropriate for parsing using this function.
+%
+%   Note that this function requires pipe shell calls to be enabled: this is
+%   generally true but may require the option |--enable-pipes| to be enabled
+%   when running the \TeX{} job.
+% \end{function}
+%
+% \begin{function}{__image_read_bb:n}
+%   \begin{syntax}
+%     \cs{__image_read_bb:n} \meta{file}
+%   \end{syntax}
+%   Parses the image \meta{file} to find a PostScript-style bounding box
+%   line of the form
+%   \begin{verbatim}
+%   %%BoundingBox: llx lly urx urx
+%   \end{verbatim}
+%   or
+%   \begin{verbatim}
+%   %%HiResBoundingBox: llx lly urx urx
+%   \end{verbatim}
+%   where \meta{llx}, \meta{lly}, \meta{urx} and \meta{ury} are the corners
+%   of the bounding box expressed in PostScript (\enquote{big}) points. The
+%   four values are used to set the dimensions \cs{l__image_llx_dim},
+%   \emph{etc.} The data format searched for depends on the option |hiresbb|.
+%   The boolean \cs{l__image_bb_found_bool} is set |true| on successful determination of a bounding box. In the
+%   case where no data is obtained, this variable is |false| and the
+%   four dimensions are set to place holders: |llx|/|lly| to $0\,\mathrm{bp}$
+%   and |urx|/|ury| to $72\,\mathrm{bp}$.
+%
+%   The \meta{file} name should be fully-qualified and sanitized: no search
+%   or other manipulation is carried out at this level. No check is made on
+%   the file \emph{type} at this stage: it is assumed that the driver code
+%   using this function has made such a check. File types such as |.eps| and
+%   |.bb|/|.xbb| are appropriate for parsing using this function.
+% \end{function}
+%
 % \end{documentation}
 %
 % \begin{implementation}
@@ -73,6 +159,160 @@
 %</package>
 %    \end{macrocode}
 %
+% \begin{variable}{\l_@@_tmp_ior, \l_@@_tmp_tl}
+%   Scratch space.
+%    \begin{macrocode}
+\ior_new:N \l_@@_tmp_ior
+\tl_new:N  \l_@@_tmp_tl
+%    \end{macrocode}
+% \end{variable}
+%
+% \subsection{Obtaining bounding box data}
+%
+% \begin{variable}[int]{\l_@@_bb_hires_bool}
+%   Options which control the parsing of bounding box data.
+%    \begin{macrocode}
+\keys_define:nn { image }
+  {
+    hiresbb .bool_set:N = \l_@@_bb_hires_bool
+  }
+%    \end{macrocode}
+% \end{variable}
+%
+% \begin{variable}[int]{\l__image_bb_found_dim}
+% \begin{variable}[int]
+%   {
+%     \l__image_llx_dim , \l__image_lly_dim ,
+%     \l__image_urx_dim , \l__image_ury_dim
+%   }
+%   Storage for the return of results.
+%    \begin{macrocode}
+\bool_new:N \l__image_bb_found_bool
+\dim_new:N \l__image_llx_dim
+\dim_new:N \l__image_lly_dim
+\dim_new:N \l__image_urx_dim
+\dim_new:N \l__image_ury_dim
+%    \end{macrocode}
+% \end{variable}
+% \end{variable}
+%
+% \begin{macro}[int]{\__image_extract_bb:n, \__image_read_bb:n}
+% \begin{macro}[aux]{\@@_read_bb_auxi:nn}
+% \begin{macro}[aux]
+%   {
+%     \@@_read_bb_auxii:w  ,
+%     \@@_read_bb_auxiii:w ,
+%     \@@_read_bb_auxiv:w
+%   }
+%  Extracting the bounding box from an |.eps| or |.bb| file is done by
+%  reading each line and searching for the marker text (|%%BoundingBox:|
+%  or |%%HiResBoundingBox|.) The data is read as a string with a mapping over
+%  the lines: as there is a colon involved, a little bit of work is needed to
+%  get the matching correct. The same approach covers cases where the bounding
+%  box has to be calculated by |extractbb|, with just the initial phase
+%  different.
+%    \begin{macrocode}
+\cs_new_protected:Npn \__image_extract_bb:n #1
+  {
+    \tex_openin:D \l_@@_tmp_ior = "|extractbb~-O~#1"
+    \@@_read_bb_auxi:nn {#1} { pipe-failed }
+  }
+\cs_new_protected:Npn \__image_read_bb:n #1
+  {
+    \ior_open:Nn \l_@@_tmp_ior {#1}
+    \@@_read_bb_auxi:nn {#1} { image-not-found }
+  }
+\cs_new_protected:Npx \@@_read_bb_auxi:nn #1#2
+  {
+    \dim_zero:N \l__image_llx_dim
+    \dim_zero:N \l__image_lly_dim
+    \dim_set:Nn \l__image_urx_dim { 72bp }
+    \dim_set:Nn \l__image_ury_dim { 72bp }
+    \bool_set_false:N \l__image_bb_found_bool
+    \exp_not:N \ior_if_eof:NTF \exp_not:N \l_@@_tmp_ior
+      {
+        \__msg_kernel_error:nnn { kernel } {#2} {#1}
+      }
+      {
+        \ior_str_map_inline:Nn \exp_not:N \l_@@_tmp_ior
+          {
+            \exp_not:N \@@_image_read_bb_auxii:w
+              ##1 ~ \c_colon_str \exp_not:N \q_stop
+          }
+      }
+    \ior_close:N \exp_not:N \l_@@_tmp_ior
+  }
+\use:x
+  {
+    \cs_new_protected:Npn \exp_not:N \@@_read_bb_auxii:w
+      ##1 \c_colon_str ##2 \exp_not:N \q_stop
+  }
+  {
+    \str_if_eq_x:nnT
+      {
+        \c_percent_str \c_percent_str
+        \bool_if:NT \l_@@_bb_hires_bool { HiRes }
+        BoundingBox
+      }
+      {#1}
+      { \@@_read_bb_auxiii:w #2 ( ) \q_stop }
+  }
+%    \end{macrocode}
+%   If the bounding box is |atend|, just ignore the current one and keep going.
+%   Otherwise, we need to allow for tabs and multiple spaces (as the line has
+%   been read as a string). The easiest way to deal with that is to scan the
+%   tokens: at this stage the line fragment should be just numbers and
+%   whitespace. \TeX{} will then tidy up for us, with just a leading space to
+%   worry about: that is taken out by the |\use:n| here.
+%    \begin{macrocode}
+\cs_new_protected:Npn \@@_read_bb_auxiii:w #1 ( #2 ) #3 \q_stop
+  {
+    \str_if_eq:nnF {#2} { atend }
+      {
+        \tl_set_rescan:Nnx \l_@@_tmp_tl
+          {
+            \char_set_catcode_space:n {  9 }
+            \char_set_catcode_space:n { 32 }
+          }
+          { \use:n #1 }
+        \exp_after:wN \@@_read_bb_auxiv:w \l_@@_tmp_tl \q_stop
+      }
+  }
+%    \end{macrocode}
+%   A trailing space was deliberately added earlier so we know that the final
+%   data point is terminated by a space.
+%    \begin{macrocode}
+\cs_new_protected:Npn \@@_read_bb_auxiv:w #1~#2~#3~#4~#5 \q_stop
+  {
+    \dim_set:Nn \l__image_llx_dim { #1 bp }
+    \dim_set:Nn \l__image_lly_dim { #2 bp }
+    \dim_set:Nn \l__image_urx_dim { #3 bp }
+    \dim_set:Nn \l__image_ury_dim { #4 bp }
+    \bool_set_true:N \l__image_bb_found_bool
+    \ior_map_break:
+  }
+%    \end{macrocode}
+% \end{macro}
+% \end{macro}
+% \end{macro}
+%
+% \subsection{Messages}
+%
+%    \begin{macrocode}
+\__msg_kernel_new:nnnn { image } { image-not-found }
+  { Image~file~'#1'~not~found. }
+  {
+    LaTeX~tried~to~open~image~file~'#1',
+    ~but~the~file~could~not~be~read.
+  }
+\__msg_kernel_new:nnnn { image } { pipe-failed }
+  { Cannot~run~piped~system~commands. }
+  {
+    LaTeX~tried~to~call~a~system~process~but~this~was~not~possible.\\
+    Try~the~"--shell-escape"~(or~"--enable-pipes")~option.
+  }
+%    \end{macrocode}
+%
 %    \begin{macrocode}
 %</initex|package>
 %    \end{macrocode}

-- 
To stop receiving notification emails like this one, please contact
the administrator of this repository.


More information about the latex3-commits mailing list