[latex3-commits] [git/LaTeX3-latex3-latex3] main: Add \graphics_get_pagecount:nN (d9b14a39a)
Joseph Wright
joseph.wright at morningstar2.co.uk
Wed Apr 6 22:42:36 CEST 2022
Repository : https://github.com/latex3/latex3
On branch : main
Link : https://github.com/latex3/latex3/commit/d9b14a39a3cc3497e01840000a59fd5e9f3c70a0
>---------------------------------------------------------------
commit d9b14a39a3cc3497e01840000a59fd5e9f3c70a0
Author: Joseph Wright <joseph.wright at morningstar2.co.uk>
Date: Wed Apr 6 21:42:26 2022 +0100
Add \graphics_get_pagecount:nN
>---------------------------------------------------------------
d9b14a39a3cc3497e01840000a59fd5e9f3c70a0
l3backend/CHANGELOG.md | 3 +
l3backend/l3backend-graphics.dtx | 50 ++++++++++++++
l3experimental/CHANGELOG.md | 3 +
l3experimental/l3graphics/l3graphics.dtx | 76 ++++++++++++++++++++++
.../testfiles/m3graphics002.etex-dvips.tlg | 23 +++++++
.../testfiles/m3graphics002.etex-dvisvgm.tlg | 23 +++++++
.../l3graphics/testfiles/m3graphics002.lvt | 37 +++++++++++
.../l3graphics/testfiles/m3graphics002.ptex.tlg | 23 +++++++
.../l3graphics/testfiles/m3graphics002.tlg | 26 ++++++++
.../l3graphics/testfiles/m3graphics002.uptex.tlg | 23 +++++++
.../l3graphics/testfiles/m3graphics002.xetex.tlg | 23 +++++++
11 files changed, 310 insertions(+)
diff --git a/l3backend/CHANGELOG.md b/l3backend/CHANGELOG.md
index 56f763d0a..b0f065542 100644
--- a/l3backend/CHANGELOG.md
+++ b/l3backend/CHANGELOG.md
@@ -6,6 +6,9 @@ this project uses date-based 'snapshot' version identifiers.
## [Unreleased]
+### Added
+- Support for graphics (PDF) page count extraction
+
### Fixed
- Setup for graphics extension searching
- Various internal issues in graphics inclusion
diff --git a/l3backend/l3backend-graphics.dtx b/l3backend/l3backend-graphics.dtx
index 8147ee025..f22eab425 100644
--- a/l3backend/l3backend-graphics.dtx
+++ b/l3backend/l3backend-graphics.dtx
@@ -120,6 +120,13 @@
% \end{macrocode}
% \end{macro}
%
+% \begin{macro}{\@@_backend_get_pagecount:n}
+% \begin{macrocode}
+\@@_backend_loaded:n
+ { \cs_new_eq:NN \@@_backend_get_pagecount:n \@@_get_pagecount:n }
+% \end{macrocode}
+% \end{macro}
+%
% \begin{macrocode}
%</dvips>
% \end{macrocode}
@@ -338,6 +345,18 @@
% \end{macro}
% \end{macro}
%
+% \begin{macro}{\@@_backend_get_pagecount:n}
+% Simply load and store.
+% \begin{macrocode}
+\cs_new_protected:Npn \@@_backend_get_pagecount:n #1
+ {
+ \tex_immediate:D \tex_pdfximage:D {#1}
+ \int_const:cn { c_@@_ #1 _pages_int }
+ { \int_use:N \tex_pdflastximagepages:D }
+ }
+% \end{macrocode}
+% \end{macro}
+%
% \begin{macrocode}
%</luatex|pdftex>
% \end{macrocode}
@@ -512,6 +531,15 @@
% \end{macro}
% \end{macro}
%
+% \begin{macro}{\@@_backend_get_pagecount:n}
+% \begin{macrocode}
+%<*dvipdfmx>
+\@@_backend_loaded:n
+ { \cs_new_eq:NN \@@_backend_get_pagecount:n \@@_get_pagecount:n }
+%</dvipdfmx>
+% \end{macrocode}
+% \end{macro}
+%
% \begin{macrocode}
%</dvipdfmx|xetex>
% \end{macrocode}
@@ -624,6 +652,21 @@
% \end{macrocode}
% \end{macro}
%
+% \begin{macro}{\@@_backend_get_pagecount:n}
+% Very little to do here other than cover the case of a non-PDF file.
+% \begin{macrocode}
+\cs_new_protected:Npn \@@_backend_get_pagecount:n #1
+ {
+ \int_const:cn { c_@@_ #1 _pages_int }
+ {
+ \int_max:nn
+ { \int_use:N \tex_XeTeXpdfpagecount:D #1 ~ }
+ { 1 }
+ }
+ }
+% \end{macrocode}
+% \end{macro}
+%
% \begin{macrocode}
%</xetex>
% \end{macrocode}
@@ -735,6 +778,13 @@
% \end{macro}
% \end{macro}
%
+% \begin{macro}{\@@_backend_get_pagecount:n}
+% \begin{macrocode}
+\@@_backend_loaded:n
+ { \cs_new_eq:NN \@@_backend_get_pagecount:n \@@_get_pagecount:n }
+% \end{macrocode}
+% \end{macro}
+%
% \begin{macrocode}
%</dvisvgm>
% \end{macrocode}
diff --git a/l3experimental/CHANGELOG.md b/l3experimental/CHANGELOG.md
index 5fec52051..52a5d6c43 100644
--- a/l3experimental/CHANGELOG.md
+++ b/l3experimental/CHANGELOG.md
@@ -7,6 +7,9 @@ this project uses date-based 'snapshot' version identifiers.
## [Unreleased]
+### Added
+- `\graphics_get_pagecount:nN`
+
### Changed
- Return an empty `seq` when `\sys_shell_split_ls:nN` is applied with a
glob which matched no entries
diff --git a/l3experimental/l3graphics/l3graphics.dtx b/l3experimental/l3graphics/l3graphics.dtx
index aa4fa8c57..c7fee2cfe 100644
--- a/l3experimental/l3graphics/l3graphics.dtx
+++ b/l3experimental/l3graphics/l3graphics.dtx
@@ -137,6 +137,16 @@
% variable content. Spaces need not be quoted.
% \end{variable}
%
+% \subsection{Utility functions}
+%
+% \begin{function}{\graphics_get_pagecount:nN}
+% \begin{syntax}
+% \cs{graphics_get_pagecount:nn} \Arg{file} \meta{tl~var}
+% \end{syntax}
+% Reads the graphics \meta{file} and extracts the number of pages, which
+% are stored in the \meta{tl~var}.
+% \end{function}
+%
% \subsection{Showing and logging included graphics}
%
% \begin{function}{\graphics_show_list:, \graphics_log_list:}
@@ -621,6 +631,72 @@
% \end{macro}
% \end{macro}
%
+% \subsection{Utility functions}
+%
+% \begin{macro}{\graphics_get_pagecount:nN}
+% \begin{macro}{\@@_get_pagecount:n}
+% \begin{macro}{\@@_get_pagecount:nw}
+% A generic function to read the number of pages in a graphic file. This is
+% used by all of the backend where there is not a dedicated primitive. The
+% plan is essentially the same as reading the bounding box. To avoid multiple
+% calls, the value is cached either here or in the backend.
+% \begin{macrocode}
+\cs_new_protected:Npn \graphics_get_pagecount:nN #1#2
+ {
+ \group_begin:
+ \seq_set_eq:NN \l_file_search_path_seq \l_graphics_search_path_seq
+ \file_get_full_name:nNTF {#1} \l_@@_full_name_str
+ {
+ \int_if_exist:cF { c_@@_ \l_@@_full_name_str _pages_int }
+ {
+ \exp_args:NV \@@_backend_get_pagecount:n
+ \l_@@_full_name_str
+ }
+ \tl_set:Nv #2 { c_@@_ \l_@@_full_name_str _pages_int }
+ }
+ {
+ \tl_set:Nn #2 { 0 }
+ \msg_error:nnn { graphics } { graphic-not-found } {#1}
+ }
+ \exp_args:NNNV \group_end:
+ \tl_set:Nn #2 #2
+ }
+\cs_new_protected:Npx \@@_get_pagecount:n #1
+ {
+ \ior_shell_open:Nn \exp_not:N \l_@@_internal_ior
+ { extractbb~-O~#1 }
+ \exp_not:N \ior_if_eof:NTF \exp_not:N \l_@@_internal_ior
+ { \msg_error:nnn { graphics } { pipe-failed } }
+ {
+ \ior_str_map_inline:Nn \exp_not:N \l_@@_internal_ior
+ {
+ \exp_not:N \@@_get_pagecount:nw {#1}
+ ##1 ~ \c_colon_str \c_colon_str \s_@@_stop
+ }
+ \exp_not:N \int_if_exist:cF { c_@@_ #1 _pages_int }
+ { \int_const:cn { c_@@_ #1 _pages_int } { 1 } }
+ }
+ \ior_close:N \exp_not:N \l_@@_internal_ior
+ }
+\use:x
+ {
+ \cs_new_protected:Npn \exp_not:N \@@_get_pagecount:nw
+ ##1##2 \c_colon_str ##3 \c_colon_str ##4 \s_@@_stop
+ {
+ \exp_not:N \str_if_eq:nnT
+ { \c_percent_str \c_percent_str Pages }
+ {##2}
+ {
+ \int_const:cn { c_@@_ ##1 _pages_int } {##3}
+ \exp_not:N \ior_map_break:
+ }
+ }
+ }
+% \end{macrocode}
+% \end{macro}
+% \end{macro}
+% \end{macro}
+%
% \subsection{Messages}
%
% \begin{macrocode}
diff --git a/l3experimental/l3graphics/testfiles/m3graphics002.etex-dvips.tlg b/l3experimental/l3graphics/testfiles/m3graphics002.etex-dvips.tlg
new file mode 100644
index 000000000..a0078aa5e
--- /dev/null
+++ b/l3experimental/l3graphics/testfiles/m3graphics002.etex-dvips.tlg
@@ -0,0 +1,23 @@
+This is a generated file for the LaTeX (2e + expl3) validation system.
+Don't change this file in any respect.
+============================================================
+TEST 1: Getting the number of pages
+============================================================
+> \l_tmpa_tl=1.
+<recently read> }
+l. ... }
+> \l_tmpa_tl=1.
+<recently read> }
+l. ... }
+> \l_tmpa_tl=12.
+<recently read> }
+l. ... }
+! Package graphics Error: Image file 'not-a-file' not found.
+For immediate help type H <return>.
+ ...
+l. ... }
+LaTeX tried to open graphic file 'not-a-file', but the file could not be read.
+> \l_tmpa_tl=0.
+<recently read> }
+l. ... }
+============================================================
diff --git a/l3experimental/l3graphics/testfiles/m3graphics002.etex-dvisvgm.tlg b/l3experimental/l3graphics/testfiles/m3graphics002.etex-dvisvgm.tlg
new file mode 100644
index 000000000..a0078aa5e
--- /dev/null
+++ b/l3experimental/l3graphics/testfiles/m3graphics002.etex-dvisvgm.tlg
@@ -0,0 +1,23 @@
+This is a generated file for the LaTeX (2e + expl3) validation system.
+Don't change this file in any respect.
+============================================================
+TEST 1: Getting the number of pages
+============================================================
+> \l_tmpa_tl=1.
+<recently read> }
+l. ... }
+> \l_tmpa_tl=1.
+<recently read> }
+l. ... }
+> \l_tmpa_tl=12.
+<recently read> }
+l. ... }
+! Package graphics Error: Image file 'not-a-file' not found.
+For immediate help type H <return>.
+ ...
+l. ... }
+LaTeX tried to open graphic file 'not-a-file', but the file could not be read.
+> \l_tmpa_tl=0.
+<recently read> }
+l. ... }
+============================================================
diff --git a/l3experimental/l3graphics/testfiles/m3graphics002.lvt b/l3experimental/l3graphics/testfiles/m3graphics002.lvt
new file mode 100644
index 000000000..be766e081
--- /dev/null
+++ b/l3experimental/l3graphics/testfiles/m3graphics002.lvt
@@ -0,0 +1,37 @@
+%
+% Copyright (C) 2022 The LaTeX Project
+%
+
+\documentclass{minimal}
+\input{regression-test}
+
+\RequirePackage[enable-debug]{expl3}
+\ExplSyntaxOn
+\debug_on:n { check-declarations , deprecation }
+\ExplSyntaxOff
+
+\RequirePackage{l3graphics}
+
+\begin{document}
+
+\ExplSyntaxOn
+
+\OMIT
+\cs_set_protected:Npn \test:n #1
+ {
+ \graphics_get_pagecount:nN {#1} \l_tmpa_tl
+ \tl_show:N \l_tmpa_tl
+ }
+\TIMO
+
+\START
+
+\TEST { Getting~the~number~of~pages }
+ {
+ \test:n { miau.pdf }
+ \test:n { foo.bar.png }
+ \test:n { example-image-a4-numbered.pdf }
+ \test:n { not-a-file }
+ }
+
+\END
diff --git a/l3experimental/l3graphics/testfiles/m3graphics002.ptex.tlg b/l3experimental/l3graphics/testfiles/m3graphics002.ptex.tlg
new file mode 100644
index 000000000..a0078aa5e
--- /dev/null
+++ b/l3experimental/l3graphics/testfiles/m3graphics002.ptex.tlg
@@ -0,0 +1,23 @@
+This is a generated file for the LaTeX (2e + expl3) validation system.
+Don't change this file in any respect.
+============================================================
+TEST 1: Getting the number of pages
+============================================================
+> \l_tmpa_tl=1.
+<recently read> }
+l. ... }
+> \l_tmpa_tl=1.
+<recently read> }
+l. ... }
+> \l_tmpa_tl=12.
+<recently read> }
+l. ... }
+! Package graphics Error: Image file 'not-a-file' not found.
+For immediate help type H <return>.
+ ...
+l. ... }
+LaTeX tried to open graphic file 'not-a-file', but the file could not be read.
+> \l_tmpa_tl=0.
+<recently read> }
+l. ... }
+============================================================
diff --git a/l3experimental/l3graphics/testfiles/m3graphics002.tlg b/l3experimental/l3graphics/testfiles/m3graphics002.tlg
new file mode 100644
index 000000000..a82e1583e
--- /dev/null
+++ b/l3experimental/l3graphics/testfiles/m3graphics002.tlg
@@ -0,0 +1,26 @@
+This is a generated file for the LaTeX (2e + expl3) validation system.
+Don't change this file in any respect.
+============================================================
+TEST 1: Getting the number of pages
+============================================================
+ <./miau.pdf>
+> \l_tmpa_tl=1.
+<recently read> }
+l. ... }
+ <./foo.bar.png>
+> \l_tmpa_tl=1.
+<recently read> }
+l. ... }
+ <./example-image-a4-numbered.pdf>
+> \l_tmpa_tl=12.
+<recently read> }
+l. ... }
+! Package graphics Error: Image file 'not-a-file' not found.
+For immediate help type H <return>.
+ ...
+l. ... }
+LaTeX tried to open graphic file 'not-a-file', but the file could not be read.
+> \l_tmpa_tl=0.
+<recently read> }
+l. ... }
+============================================================
diff --git a/l3experimental/l3graphics/testfiles/m3graphics002.uptex.tlg b/l3experimental/l3graphics/testfiles/m3graphics002.uptex.tlg
new file mode 100644
index 000000000..a0078aa5e
--- /dev/null
+++ b/l3experimental/l3graphics/testfiles/m3graphics002.uptex.tlg
@@ -0,0 +1,23 @@
+This is a generated file for the LaTeX (2e + expl3) validation system.
+Don't change this file in any respect.
+============================================================
+TEST 1: Getting the number of pages
+============================================================
+> \l_tmpa_tl=1.
+<recently read> }
+l. ... }
+> \l_tmpa_tl=1.
+<recently read> }
+l. ... }
+> \l_tmpa_tl=12.
+<recently read> }
+l. ... }
+! Package graphics Error: Image file 'not-a-file' not found.
+For immediate help type H <return>.
+ ...
+l. ... }
+LaTeX tried to open graphic file 'not-a-file', but the file could not be read.
+> \l_tmpa_tl=0.
+<recently read> }
+l. ... }
+============================================================
diff --git a/l3experimental/l3graphics/testfiles/m3graphics002.xetex.tlg b/l3experimental/l3graphics/testfiles/m3graphics002.xetex.tlg
new file mode 100644
index 000000000..a0078aa5e
--- /dev/null
+++ b/l3experimental/l3graphics/testfiles/m3graphics002.xetex.tlg
@@ -0,0 +1,23 @@
+This is a generated file for the LaTeX (2e + expl3) validation system.
+Don't change this file in any respect.
+============================================================
+TEST 1: Getting the number of pages
+============================================================
+> \l_tmpa_tl=1.
+<recently read> }
+l. ... }
+> \l_tmpa_tl=1.
+<recently read> }
+l. ... }
+> \l_tmpa_tl=12.
+<recently read> }
+l. ... }
+! Package graphics Error: Image file 'not-a-file' not found.
+For immediate help type H <return>.
+ ...
+l. ... }
+LaTeX tried to open graphic file 'not-a-file', but the file could not be read.
+> \l_tmpa_tl=0.
+<recently read> }
+l. ... }
+============================================================
More information about the latex3-commits
mailing list.