Skip to content

Commit f234061

Browse files
authored
Merge pull request #27 from xqms/full_trim
Full trim
2 parents 4d99a89 + 3658754 commit f234061

2 files changed

Lines changed: 88 additions & 3 deletions

File tree

example/trim.tex

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
\documentclass[]{article}
2+
3+
\usepackage{graphicscache}
4+
5+
\begin{document}
6+
7+
\includegraphics[clip,trim=10 20pt 1mm 10]{myimage.png}
8+
9+
\includegraphics{myimage.png}
10+
11+
\end{document}

graphicscache.dtx

Lines changed: 77 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,11 @@
159159
% |graphicscache| will fall back to |graphicx| in-place rendering.
160160
% This can be used to perform a final release (see \cref{sec:release}).
161161
%
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+
%
162167
% \DescribeKey{cachedir}'='<dir>
163168
% This key can be used to move the cache directory to another location.
164169
% The default value is |graphicscache|.
@@ -220,13 +225,18 @@
220225
\RequirePackage{ifplatform}
221226
\RequirePackage{pdftexcmds}
222227
\RequirePackage{ltxcmds}
228+
\RequirePackage{etoolbox}
229+
\IfFileExists{xfp.sty}{\RequirePackage{xfp}}{}
223230
\newif\ifgraphicscache@render
224231
\newif\ifgraphicscache@disable
225232
\newif\ifgraphicscache@compress
226233
\newif\ifgraphicscache@listing
227234
\newif\ifgraphicscache@hashshortnames
228235
\newif\ifgraphicscache@gsnotavailable\graphicscache@gsnotavailablefalse
229236
\newif\ifgraphicscache@dorender
237+
\newif\ifgraphicscache@trim
238+
\newif\ifgraphicscache@clip
239+
\newif\ifgraphicscache@fullclip
230240
\def\graphicscache@graphicsargs{}
231241
\newlength\graphicscache@tmplen
232242
\newcommand{\graphicscache@addarg}[1]{%
@@ -236,6 +246,13 @@
236246
\edef\graphicscache@graphicsargs{\graphicscache@graphicsargs,#1}%
237247
\fi
238248
}
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+
}
239256
\pgfkeys{
240257
/graphicscache/.cd,
241258
render/.is if=graphicscache@render,
@@ -268,6 +285,9 @@
268285
hashshortnames/.is if=graphicscache@hashshortnames,
269286
hashshortnames=false,
270287
% \end{macrocode}
288+
% \begin{macrocode}
289+
fullclip/.is if=graphicscache@fullclip,
290+
% \end{macrocode}
271291
% We now define the list of supported graphicx arguments:
272292
% \begin{macrocode}
273293
width/.code={%
@@ -286,8 +306,8 @@
286306
\graphicscache@addarg{height=\the\graphicscache@tmplen}%
287307
\fi
288308
},
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},
291311
angle/.code={%
292312
\edef\graphicscache@tmp{#1}%
293313
\graphicscache@addarg{angle=\graphicscache@tmp}%
@@ -336,6 +356,20 @@
336356
\directlua{os.execute("\luaescapestring{#1}")}}
337357
\fi
338358
% \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}
339373
%
340374
% \begin{macro}{\graphicscache@callgswithname}
341375
% This macro calls ghostscript using the name specified in the first argument.
@@ -429,6 +463,41 @@
429463
\else
430464
\graphicscache@ShellEscape{mkdir -p "\graphicscache@cachedir"}%
431465
\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
432501
% \end{macrocode}
433502
% First, render the graphics.
434503
% \begin{macrocode}
@@ -650,7 +719,12 @@
650719
\else
651720
\edef\graphicscache@hashedname{\graphicscache@fname}%
652721
\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}}%
654728
\edef\graphicscache@output{\graphicscache@cachedir/\graphicscache@outputhash.pdf}%
655729
\ifgraphicscache@listing
656730
\PackageInfo{graphicscache}{graphicscache: includegraphics\{#2\} => \graphicscache@output}%

0 commit comments

Comments
 (0)