Skip to content

Commit 45d2c88

Browse files
committed
full clip works
1 parent bb03dfb commit 45d2c88

1 file changed

Lines changed: 58 additions & 29 deletions

File tree

graphicscache.dtx

Lines changed: 58 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,8 @@
220220
\RequirePackage{ifplatform}
221221
\RequirePackage{pdftexcmds}
222222
\RequirePackage{ltxcmds}
223+
\RequirePackage{xfp}
224+
\RequirePackage{etoolbox}
223225
\newif\ifgraphicscache@render
224226
\newif\ifgraphicscache@disable
225227
\newif\ifgraphicscache@compress
@@ -229,6 +231,7 @@
229231
\newif\ifgraphicscache@dorender
230232
\newif\ifgraphicscache@trim
231233
\newif\ifgraphicscache@clip
234+
\newif\ifgraphicscache@fullclip
232235
\def\graphicscache@graphicsargs{}
233236
\newlength\graphicscache@tmplen
234237
\newcommand{\graphicscache@addarg}[1]{%
@@ -239,10 +242,10 @@
239242
\fi
240243
}
241244
\def\graphicscache@parsetrim#1 #2 #3 #4 #5\\{%
242-
\Gin@defaultbp\graphicscache@trim@left{#1}%
243-
\Gin@defaultbp\graphicscache@trim@bottom{#2}%
244-
\Gin@defaultbp\graphicscache@trim@right{#3}%
245-
\Gin@defaultbp\graphicscache@trim@top{#4}%
245+
\Gin@defaultbp\graphicscache@trim@l{#1}%
246+
\Gin@defaultbp\graphicscache@trim@b{#2}%
247+
\Gin@defaultbp\graphicscache@trim@r{#3}%
248+
\Gin@defaultbp\graphicscache@trim@t{#4}%
246249
\graphicscache@trimtrue
247250
}
248251
\pgfkeys{
@@ -277,6 +280,9 @@
277280
hashshortnames/.is if=graphicscache@hashshortnames,
278281
hashshortnames=false,
279282
% \end{macrocode}
283+
% \begin{macrocode}
284+
fullclip/.is if=graphicscache@fullclip,
285+
% \end{macrocode}
280286
% We now define the list of supported graphicx arguments:
281287
% \begin{macrocode}
282288
width/.code={%
@@ -345,6 +351,20 @@
345351
\directlua{os.execute("\luaescapestring{#1}")}}
346352
\fi
347353
% \end{macrocode}
354+
% \begin{macrocode}
355+
\newcommand{\graphicscache@isbitmap}[1]{%
356+
\fullexpandarg
357+
\IfSubStr*{,png,PNG,jpg,JPG,jpeg,JPEG,}{,#1,}%
358+
}
359+
\def\graphicscache@trunc#1.#2\@empty{#1}%
360+
\newcommand{\graphicscache@pix}[2]{%
361+
% Compute trim amount in pixels
362+
\edef\graphicscache@trimtmp{\fpeval{#2 * \graphicscache@fdpi}}%
363+
% Now truncate & compute remainder
364+
\edef#1{\expandafter\graphicscache@trunc\graphicscache@trimtmp.\@empty}%
365+
\edef#2{\fpeval{(\graphicscache@trimtmp - #1) / \graphicscache@fdpi}}%
366+
}
367+
% \end{macrocode}
348368
%
349369
% \begin{macro}{\graphicscache@callgswithname}
350370
% This macro calls ghostscript using the name specified in the first argument.
@@ -431,10 +451,6 @@
431451
% Here, we actually perform the rendering. Sadly, this is quite complex due
432452
% to cross-platform support.
433453
% \begin{macrocode}
434-
\newcommand{\graphicscache@isbitmap}[1]{%
435-
\fullexpandarg
436-
\IfSubStr*{,png,PNG,jpg,JPG,jpeg,JPEG,}{,#1,}%
437-
}
438454
\newcommand{\graphicscache@dorender}{%
439455
\PackageInfo{graphicscache}{Rendering \graphicscache@outputhash: \graphicscache@fname\space with args: \graphicscache@graphicsargs\space (master file)}%
440456
\ifwindows
@@ -444,26 +460,34 @@
444460
\fi
445461
\ifgraphicscache@trim
446462
\ifgraphicscache@clip
447-
\PackageInfo{graphicscache}{Trim: \graphicscache@trim@left, \graphicscache@trim@bottom, \graphicscache@trim@right, \graphicscache@trim@top}%
448-
\filename@parse{\graphicscache@fname}%
449-
\PackageInfo{graphicscache}{Ext: |\filename@ext|}%
450-
\graphicscache@isbitmap{\filename@ext}{%
451-
\PackageInfo{graphicscache}{is bitmap!}%
452-
\graphicscache@ShellEscape{identify -units PixelsPerInch -format '\@percentchar[fx:int(resolution.x)]' "\graphicscache@fname" > \graphicscache@output.dpi}%
453-
\CatchFileDef{\graphicscache@fdpi}{\graphicscache@output.dpi}{}%
454-
% \input{\graphicscache@output.dpi}
455-
\PackageInfo{graphicscache}{Detected DPI: \graphicscache@fdpi}%
456-
\edef\graphicscache@trim@left@pix{\strip@pt\dimexpr1bp*\graphicscache@trim@left*\graphicscache@fdpi*65535/\dimexpr 1in\relax\relax}%
457-
\edef\graphicscache@trim@bottom@pix{\strip@pt\dimexpr1bp*\graphicscache@trim@bottom*\graphicscache@fdpi*65535/\dimexpr 1in\relax\relax}%
458-
\edef\graphicscache@trim@right@pix{\strip@pt\dimexpr1bp*\graphicscache@trim@right*\graphicscache@fdpi*65535/\dimexpr 1in\relax\relax}%
459-
\edef\graphicscache@trim@top@pix{\strip@pt\dimexpr1bp*\graphicscache@trim@top*\graphicscache@fdpi*65535/\dimexpr 1in\relax\relax}%
460-
\PackageInfo{graphicscache}{Detected left: \graphicscache@trim@left@pix}%
461-
}{%
462-
\PackageInfo{graphicscache}{no bitmap!}%
463-
}%
464-
% Compute pixels
465-
% \setlength{\dimen@}{
466-
% \setlength{\graphicscache@trim@pixel@left}{\graphicscache@trim@left bp}
463+
\ifgraphicscache@fullclip
464+
\PackageInfo{graphicscache}{Trim: \graphicscache@trim@l, \graphicscache@trim@b, \graphicscache@trim@r, \graphicscache@trim@t}%
465+
\filename@parse{\graphicscache@fname}%
466+
\PackageInfo{graphicscache}{Ext: |\filename@ext|}%
467+
\graphicscache@isbitmap{\filename@ext}{%
468+
\PackageInfo{graphicscache}{is bitmap!}%
469+
\graphicscache@ShellEscape{identify -units PixelsPerInch -format '\@percentchar[fx:int(resolution.x)]' "\graphicscache@fname" > \graphicscache@output.dpi}%
470+
\CatchFileDef{\graphicscache@fdpi}{\graphicscache@output.dpi}{}%
471+
\PackageInfo{graphicscache}{Detected DPI: \graphicscache@fdpi (default 71)}%
472+
\ifnumequal{\graphicscache@fdpi}{0}{\edef\graphicscache@fdpi{71}}{}%
473+
\edef\graphicscache@fdpi{\fpeval{0.01389 * \graphicscache@fdpi}}%
474+
\graphicscache@pix{\graphicscache@trim@l@pix}{\graphicscache@trim@l}%
475+
\graphicscache@pix{\graphicscache@trim@b@pix}{\graphicscache@trim@b}%
476+
\graphicscache@pix{\graphicscache@trim@r@pix}{\graphicscache@trim@r}%
477+
\graphicscache@pix{\graphicscache@trim@t@pix}{\graphicscache@trim@t}%
478+
\PackageInfo{graphicscache}{Pixel trims:
479+
\graphicscache@trim@l@pix +\graphicscache@trim@l bp,
480+
\graphicscache@trim@b@pix +\graphicscache@trim@b bp,
481+
\graphicscache@trim@r@pix +\graphicscache@trim@r bp,
482+
\graphicscache@trim@t@pix +\graphicscache@trim@t bp
483+
}%
484+
\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}%
485+
\edef\graphicscache@fname{\graphicscache@output.crop.\filename@ext}%
486+
\graphicscache@addarg{trim=\graphicscache@trim@l\space\graphicscache@trim@b\space\graphicscache@trim@r\space\graphicscache@trim@t}%
487+
}{%
488+
\PackageInfo{graphicscache}{no bitmap!}%
489+
}%
490+
\fi
467491
\fi
468492
\fi
469493
% \end{macrocode}
@@ -687,7 +711,12 @@
687711
\else
688712
\edef\graphicscache@hashedname{\graphicscache@fname}%
689713
\fi
690-
\edef\graphicscache@outputhash{\pdf@mdfivesum{\graphicscache@options\graphicscache@graphicsargs\graphicscache@hashedname}}%
714+
\ifgraphicscache@fullclip
715+
\def\graphicscache@clipmode{fullclip}%
716+
\else
717+
\def\graphicscache@clipmode{nclip}%
718+
\fi
719+
\edef\graphicscache@outputhash{\pdf@mdfivesum{\graphicscache@options\graphicscache@graphicsargs\graphicscache@clipmode\graphicscache@hashedname}}%
691720
\edef\graphicscache@output{\graphicscache@cachedir/\graphicscache@outputhash.pdf}%
692721
\ifgraphicscache@listing
693722
\PackageInfo{graphicscache}{graphicscache: includegraphics\{#2\} => \graphicscache@output}%

0 commit comments

Comments
 (0)