|
159 | 159 | % |graphicscache| will fall back to |graphicx| in-place rendering. |
160 | 160 | % This can be used to perform a final release (see \cref{sec:release}). |
161 | 161 | % |
| 162 | +% \DescribeKey{fullclip}'=true|false' |
| 163 | +% When |fullclip| is enabled, graphicscache will call ImageMagick's |convert| |
| 164 | +% to trim images, ensuring that only the specified rectangle takes up file |
| 165 | +% space in the resulting PDF. |
| 166 | +% |
162 | 167 | % \DescribeKey{cachedir}'='<dir> |
163 | 168 | % This key can be used to move the cache directory to another location. |
164 | 169 | % The default value is |graphicscache|. |
|
220 | 225 | \RequirePackage{ifplatform} |
221 | 226 | \RequirePackage{pdftexcmds} |
222 | 227 | \RequirePackage{ltxcmds} |
| 228 | +\RequirePackage{etoolbox} |
| 229 | +\IfFileExists{xfp.sty}{\RequirePackage{xfp}}{} |
223 | 230 | \newif\ifgraphicscache@render |
224 | 231 | \newif\ifgraphicscache@disable |
225 | 232 | \newif\ifgraphicscache@compress |
226 | 233 | \newif\ifgraphicscache@listing |
227 | 234 | \newif\ifgraphicscache@hashshortnames |
228 | 235 | \newif\ifgraphicscache@gsnotavailable\graphicscache@gsnotavailablefalse |
229 | 236 | \newif\ifgraphicscache@dorender |
| 237 | +\newif\ifgraphicscache@trim |
| 238 | +\newif\ifgraphicscache@clip |
| 239 | +\newif\ifgraphicscache@fullclip |
230 | 240 | \def\graphicscache@graphicsargs{} |
231 | 241 | \newlength\graphicscache@tmplen |
232 | 242 | \newcommand{\graphicscache@addarg}[1]{% |
|
236 | 246 | \edef\graphicscache@graphicsargs{\graphicscache@graphicsargs,#1}% |
237 | 247 | \fi |
238 | 248 | } |
| 249 | +\def\graphicscache@parsetrim#1 #2 #3 #4 #5\\{% |
| 250 | + \Gin@defaultbp\graphicscache@trim@l{#1}% |
| 251 | + \Gin@defaultbp\graphicscache@trim@b{#2}% |
| 252 | + \Gin@defaultbp\graphicscache@trim@r{#3}% |
| 253 | + \Gin@defaultbp\graphicscache@trim@t{#4}% |
| 254 | + \graphicscache@trimtrue |
| 255 | +} |
239 | 256 | \pgfkeys{ |
240 | 257 | /graphicscache/.cd, |
241 | 258 | render/.is if=graphicscache@render, |
|
268 | 285 | hashshortnames/.is if=graphicscache@hashshortnames, |
269 | 286 | hashshortnames=false, |
270 | 287 | % \end{macrocode} |
| 288 | +% \begin{macrocode} |
| 289 | + fullclip/.is if=graphicscache@fullclip, |
| 290 | +% \end{macrocode} |
271 | 291 | % We now define the list of supported graphicx arguments: |
272 | 292 | % \begin{macrocode} |
273 | 293 | width/.code={% |
|
286 | 306 | \graphicscache@addarg{height=\the\graphicscache@tmplen}% |
287 | 307 | \fi |
288 | 308 | }, |
289 | | - trim/.code={\graphicscache@addarg{trim=#1}}, |
290 | | - clip/.code={\graphicscache@addarg{clip}}, |
| 309 | + trim/.code={\graphicscache@addarg{trim=#1}\graphicscache@parsetrim#1 \\}, |
| 310 | + clip/.code={\graphicscache@addarg{clip}\graphicscache@cliptrue}, |
291 | 311 | angle/.code={% |
292 | 312 | \edef\graphicscache@tmp{#1}% |
293 | 313 | \graphicscache@addarg{angle=\graphicscache@tmp}% |
|
336 | 356 | \directlua{os.execute("\luaescapestring{#1}")}} |
337 | 357 | \fi |
338 | 358 | % \end{macrocode} |
| 359 | +% \begin{macrocode} |
| 360 | +\newcommand{\graphicscache@isbitmap}[1]{% |
| 361 | + \fullexpandarg |
| 362 | + \IfSubStr*{,png,PNG,jpg,JPG,jpeg,JPEG,}{,#1,}% |
| 363 | +} |
| 364 | +\def\graphicscache@trunc#1.#2\@empty{#1}% |
| 365 | +\newcommand{\graphicscache@pix}[2]{% |
| 366 | + % Compute trim amount in pixels |
| 367 | + \edef\graphicscache@trimtmp{\fpeval{#2 * \graphicscache@fdpi}}% |
| 368 | + % Now truncate & compute remainder |
| 369 | + \edef#1{\expandafter\graphicscache@trunc\graphicscache@trimtmp.\@empty}% |
| 370 | + \edef#2{\fpeval{(\graphicscache@trimtmp - #1) / \graphicscache@fdpi}}% |
| 371 | +} |
| 372 | +% \end{macrocode} |
339 | 373 | % |
340 | 374 | % \begin{macro}{\graphicscache@callgswithname} |
341 | 375 | % This macro calls ghostscript using the name specified in the first argument. |
|
429 | 463 | \else |
430 | 464 | \graphicscache@ShellEscape{mkdir -p "\graphicscache@cachedir"}% |
431 | 465 | \fi |
| 466 | + \ifgraphicscache@trim |
| 467 | + \ifgraphicscache@clip |
| 468 | + \ifgraphicscache@fullclip |
| 469 | + \ifx\fpeval\undefined |
| 470 | + \PackageError{graphicscache}{The fullclip feature requires the xfp package.}% |
| 471 | + \fi |
| 472 | + \PackageInfo{graphicscache}{Trim: \graphicscache@trim@l, \graphicscache@trim@b, \graphicscache@trim@r, \graphicscache@trim@t}% |
| 473 | + \filename@parse{\graphicscache@fname}% |
| 474 | + \PackageInfo{graphicscache}{Ext: |\filename@ext|}% |
| 475 | + \graphicscache@isbitmap{\filename@ext}{% |
| 476 | + \PackageInfo{graphicscache}{is bitmap!}% |
| 477 | + \graphicscache@ShellEscape{identify -units PixelsPerInch -format '\@percentchar[fx:int(resolution.x)]' "\graphicscache@fname" > \graphicscache@output.dpi}% |
| 478 | + \CatchFileDef{\graphicscache@fdpi}{\graphicscache@output.dpi}{}% |
| 479 | + \PackageInfo{graphicscache}{Detected DPI: \graphicscache@fdpi (default 71)}% |
| 480 | + \ifnumequal{\graphicscache@fdpi}{0}{\edef\graphicscache@fdpi{71}}{}% |
| 481 | + \edef\graphicscache@fdpi{\fpeval{0.01389 * \graphicscache@fdpi}}% |
| 482 | + \graphicscache@pix{\graphicscache@trim@l@pix}{\graphicscache@trim@l}% |
| 483 | + \graphicscache@pix{\graphicscache@trim@b@pix}{\graphicscache@trim@b}% |
| 484 | + \graphicscache@pix{\graphicscache@trim@r@pix}{\graphicscache@trim@r}% |
| 485 | + \graphicscache@pix{\graphicscache@trim@t@pix}{\graphicscache@trim@t}% |
| 486 | + \PackageInfo{graphicscache}{Pixel trims: |
| 487 | + \graphicscache@trim@l@pix +\graphicscache@trim@l bp, |
| 488 | + \graphicscache@trim@b@pix +\graphicscache@trim@b bp, |
| 489 | + \graphicscache@trim@r@pix +\graphicscache@trim@r bp, |
| 490 | + \graphicscache@trim@t@pix +\graphicscache@trim@t bp |
| 491 | + }% |
| 492 | + \graphicscache@ShellEscape{convert "\graphicscache@fname" -gravity Northwest -chop \graphicscache@trim@l@pix x\graphicscache@trim@t@pix\space -gravity Southeast -chop \graphicscache@trim@r@pix x\graphicscache@trim@b@pix\space \graphicscache@output.crop.\filename@ext}% |
| 493 | + \edef\graphicscache@fname{\graphicscache@output.crop.\filename@ext}% |
| 494 | + \graphicscache@addarg{trim=\graphicscache@trim@l\space\graphicscache@trim@b\space\graphicscache@trim@r\space\graphicscache@trim@t}% |
| 495 | + }{% |
| 496 | + \PackageInfo{graphicscache}{no bitmap!}% |
| 497 | + }% |
| 498 | + \fi |
| 499 | + \fi |
| 500 | + \fi |
432 | 501 | % \end{macrocode} |
433 | 502 | % First, render the graphics. |
434 | 503 | % \begin{macrocode} |
|
650 | 719 | \else |
651 | 720 | \edef\graphicscache@hashedname{\graphicscache@fname}% |
652 | 721 | \fi |
653 | | - \edef\graphicscache@outputhash{\pdf@mdfivesum{\graphicscache@options\graphicscache@graphicsargs\graphicscache@hashedname}}% |
| 722 | + \ifgraphicscache@fullclip |
| 723 | + \def\graphicscache@clipmode{fullclip}% |
| 724 | + \else |
| 725 | + \def\graphicscache@clipmode{nclip}% |
| 726 | + \fi |
| 727 | + \edef\graphicscache@outputhash{\pdf@mdfivesum{\graphicscache@options\graphicscache@graphicsargs\graphicscache@clipmode\graphicscache@hashedname}}% |
654 | 728 | \edef\graphicscache@output{\graphicscache@cachedir/\graphicscache@outputhash.pdf}% |
655 | 729 | \ifgraphicscache@listing |
656 | 730 | \PackageInfo{graphicscache}{graphicscache: includegraphics\{#2\} => \graphicscache@output}% |
|
0 commit comments