| 1 |
% #!rm -f tigerpsdfmt4* && ptex2pdf -l -u -ot '-shell-escape' -od '-v' test-gin-rule-psd |
| 2 |
% #!rm -f tigerpsdfmt4* && xelatex -shell-escape test-gin-rule-psd |
| 3 |
% #!rm -f tigerpsdfmt4* && pdflatex -shell-escape test-gin-rule-psd |
| 4 |
%#!rm -f tigerpsdfmt4* && lualatex -shell-escape test-gin-rule-psd |
| 5 |
%% |
| 6 |
%% This is file 'graphicxpsd.sty'. |
| 7 |
%% |
| 8 |
%% Copyright (c) 2017-2026 Munehiro Yamamoto |
| 9 |
%% |
| 10 |
%% This package is licensed under the terms of the MIT License. |
| 11 |
%% |
| 12 |
\RequirePackage{expl3} |
| 13 |
\ProvidesExplPackage {graphicxpsd} {2026/03/01} {2.0} |
| 14 |
{Adobe~Photoshop~Data~format~(PSD)~support~for~graphicx~package~with~sips/magick} |
| 15 |
|
| 16 |
%% ------------------------------------------------------------------- |
| 17 |
%% declare package messages |
| 18 |
%% ------------------------------------------------------------------- |
| 19 |
\msg_new:nnn { graphicxpsd } { no-graphicx } |
| 20 |
{ Please~load~'graphicxpsd'~package~after~loading~'graphicx'~package. } |
| 21 |
|
| 22 |
\msg_new:nnn { graphicxpsd } { no-driver } |
| 23 |
{ |
| 24 |
Please~choose~a~specific~dviware~driver~from~the~following~list~ |
| 25 |
and~set~the~one~as~a~documentclass~option.\msg_line_context: \newline |
| 26 |
Available~dviware~drivers:~dvipdfmx,~xetex,~luatex,~pdftex |
| 27 |
} |
| 28 |
|
| 29 |
%% ------------------------------------------------------------------- |
| 30 |
%% declare variables |
| 31 |
%% ------------------------------------------------------------------- |
| 32 |
\bool_new:N \l__gfxpsd_imagemagick_bool |
| 33 |
\bool_new:N \l__gfxpsd_cache_bool |
| 34 |
\tl_new:N \l__gfxpsd_psdtopdf_tl |
| 35 |
\tl_new:N \l__gfxpsd_driver_type_tl |
| 36 |
|
| 37 |
%% default value for psdtopdf command |
| 38 |
\tl_set:Nn \l__gfxpsd_psdtopdf_tl { sips } |
| 39 |
|
| 40 |
%% ------------------------------------------------------------------- |
| 41 |
%% define and process options (Key-Value) |
| 42 |
%% ------------------------------------------------------------------- |
| 43 |
\keys_define:nn { graphicxpsd } |
| 44 |
{ |
| 45 |
%% support major graphicx drivers |
| 46 |
dvipdfmx .code:n = { \tl_set:Nn \l__gfxpsd_driver_type_tl { dvipdfmx } }, |
| 47 |
xetex .code:n = { \tl_set:Nn \l__gfxpsd_driver_type_tl { dvipdfmx } }, |
| 48 |
luatex .code:n = { \tl_set:Nn \l__gfxpsd_driver_type_tl { pdftex } }, |
| 49 |
pdftex .code:n = { \tl_set:Nn \l__gfxpsd_driver_type_tl { pdftex } }, |
| 50 |
|
| 51 |
%% support two psd-to-pdf commands: |
| 52 |
%% sips (Darwin/macOS), magick/convert (ImageMagick), |
| 53 |
%% graphicsmagick (GraphicsMagick) |
| 54 |
sips .code:n = { |
| 55 |
\bool_set_false:N \l__gfxpsd_imagemagick_bool |
| 56 |
\tl_set:Nn \l__gfxpsd_psdtopdf_tl { sips } |
| 57 |
}, |
| 58 |
imagemagick .code:n = { |
| 59 |
\bool_set_true:N \l__gfxpsd_imagemagick_bool |
| 60 |
\tl_set:Nn \l__gfxpsd_psdtopdf_tl { magick } |
| 61 |
}, |
| 62 |
magick .code:n = { |
| 63 |
\bool_set_true:N \l__gfxpsd_imagemagick_bool |
| 64 |
\tl_set:Nn \l__gfxpsd_psdtopdf_tl { magick } |
| 65 |
}, |
| 66 |
convert .code:n = { |
| 67 |
\bool_set_true:N \l__gfxpsd_imagemagick_bool |
| 68 |
\tl_set:Nn \l__gfxpsd_psdtopdf_tl { convert } |
| 69 |
}, |
| 70 |
graphicsmagick .code:n = { |
| 71 |
\bool_set_true:N \l__gfxpsd_imagemagick_bool |
| 72 |
\tl_set:Nn \l__gfxpsd_psdtopdf_tl { gm~convert } %% use ~ to represent a space safely |
| 73 |
}, |
| 74 |
|
| 75 |
%% image cache flag (maintaining True/False compatibility) |
| 76 |
cache .bool_set:N = \l__gfxpsd_cache_bool, |
| 77 |
cache .default:n = false, |
| 78 |
cache / True .code:n = { \bool_set_true:N \l__gfxpsd_cache_bool }, |
| 79 |
cache / False .code:n = { \bool_set_false:N \l__gfxpsd_cache_bool }, |
| 80 |
} |
| 81 |
|
| 82 |
\ProcessKeyOptions [ graphicxpsd ] |
| 83 |
|
| 84 |
%% ------------------------------------------------------------------- |
| 85 |
%% check graphicx loading and map base extensions |
| 86 |
%% ------------------------------------------------------------------- |
| 87 |
\cs_if_exist:NF \Ginclude@pdf |
| 88 |
{ \msg_fatal:nn { graphicxpsd } { no-graphicx } } |
| 89 |
|
| 90 |
%% support the extension .ai |
| 91 |
\cs_set:cpn { Gin@[email protected] } #1 { {pdf}{.ai}{#1} } |
| 92 |
\cs_set:cpn { Gin@[email protected] } #1 { {pdf}{.AI}{#1} } |
| 93 |
|
| 94 |
%% support the extension .psd (psdtopdf) according to driver |
| 95 |
\str_case:VnF \l__gfxpsd_driver_type_tl |
| 96 |
{ |
| 97 |
{ dvipdfmx } |
| 98 |
{ |
| 99 |
\cs_set:cpn { Gin@[email protected] } #1 { {psdtopdf}{.xbb}{#1} } |
| 100 |
\cs_set:cpn { Gin@[email protected] } #1 { {psdtopdf}{.xbb}{#1} } |
| 101 |
} |
| 102 |
{ pdftex } |
| 103 |
{ |
| 104 |
\cs_set:cpn { Gin@[email protected] } #1 { {psdtopdf}{.pdf}{#1} } |
| 105 |
\cs_set:cpn { Gin@[email protected] } #1 { {psdtopdf}{.pdf}{#1} } |
| 106 |
} |
| 107 |
} |
| 108 |
{ |
| 109 |
%% fallback: attempt automatic detection using l3sys if no driver is specified |
| 110 |
\sys_if_output_pdf:TF |
| 111 |
{ |
| 112 |
\cs_set:cpn { Gin@[email protected] } #1 { {psdtopdf}{.pdf}{#1} } |
| 113 |
\cs_set:cpn { Gin@[email protected] } #1 { {psdtopdf}{.pdf}{#1} } |
| 114 |
} |
| 115 |
{ |
| 116 |
\sys_if_output_dvi:TF |
| 117 |
{ |
| 118 |
\cs_set:cpn { Gin@[email protected] } #1 { {psdtopdf}{.xbb}{#1} } |
| 119 |
\cs_set:cpn { Gin@[email protected] } #1 { {psdtopdf}{.xbb}{#1} } |
| 120 |
} |
| 121 |
{ |
| 122 |
\msg_error:nn { graphicxpsd } { no-driver } |
| 123 |
} |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
%% then, include .psd file as converted PDF |
| 128 |
\cs_set_eq:NN \Ginclude@psdtopdf \Ginclude@pdf |
| 129 |
|
| 130 |
%% ------------------------------------------------------------------- |
| 131 |
%% file reading and conversion hook definitions |
| 132 |
%% ------------------------------------------------------------------- |
| 133 |
|
| 134 |
%% create a clone of \Gread@pdf |
| 135 |
\cs_set_eq:NN \__gfxpsd_clone_Gread_pdf:n \Gread@pdf |
| 136 |
|
| 137 |
%% internal macro to execute PDF conversion |
| 138 |
\cs_new_protected:Nn \__gfxpsd_convert_psd_to_pdf: |
| 139 |
{ |
| 140 |
\iow_term:e { converting:~ \Gin@base\Gin@ext \c_space_tl ->~ \Gin@base 4gfxpsd.pdf } |
| 141 |
\bool_if:NTF \l__gfxpsd_imagemagick_bool |
| 142 |
{ |
| 143 |
\sys_shell_now:e { \l__gfxpsd_psdtopdf_tl \c_space_tl \Gin@base\Gin@ext \c_space_tl \Gin@base 4gfxpsd.pdf } |
| 144 |
} |
| 145 |
{ |
| 146 |
\sys_shell_now:e { sips~ -s~ format~ pdf~ \Gin@base\Gin@ext \c_space_tl --out~ \Gin@base 4gfxpsd.pdf } |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
%% read .psd file |
| 151 |
\cs_set_protected:Npn \Gread@psdtopdf #1 |
| 152 |
{ |
| 153 |
\bool_if:NTF \l__gfxpsd_cache_bool |
| 154 |
{ |
| 155 |
\file_if_exist:nTF { \Gin@base 4gfxpsd.pdf } |
| 156 |
{ |
| 157 |
\iow_term:e { including~cached~image:~ \Gin@base 4gfxpsd.pdf } |
| 158 |
} |
| 159 |
{ |
| 160 |
\iow_term:e { not~found:~ \Gin@base 4gfxpsd.pdf } |
| 161 |
\__gfxpsd_convert_psd_to_pdf: |
| 162 |
} |
| 163 |
} |
| 164 |
{ |
| 165 |
\__gfxpsd_convert_psd_to_pdf: |
| 166 |
} |
| 167 |
|
| 168 |
%% renamed inside: foo.psd -> foo4gfxpsd.pdf |
| 169 |
\cs_set_eq:NN \__gfxpsd_clone_Gin_base: \Gin@base |
| 170 |
\cs_set:Npn \Gin@base { \__gfxpsd_clone_Gin_base: 4gfxpsd } |
| 171 |
\cs_set:Npn \Gin@ext { .pdf } |
| 172 |
|
| 173 |
\__gfxpsd_clone_Gread_pdf:n { #1 } |
| 174 |
} |
| 175 |
|
| 176 |
\endinput |
| 177 |
%% |
| 178 |
%% End of file `graphicxpsd.sty'. |