From mboxrd@z Thu Jan 1 00:00:00 1970 X-Msuck: nntp://news.gmane.io/gmane.comp.tex.context/87187 Path: news.gmane.org!not-for-mail From: Aditya Mahajan Newsgroups: gmane.comp.tex.context Subject: Re: resize and center and clip image Date: Thu, 1 May 2014 14:00:25 -0400 (EDT) Message-ID: References: <7BE13927-B54E-4FC6-8C9E-2669D51CC6F2@gmail.com> Reply-To: mailing list for ConTeXt users NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii"; Format="flowed" Content-Transfer-Encoding: 7bit X-Trace: ger.gmane.org 1398967258 21746 80.91.229.3 (1 May 2014 18:00:58 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Thu, 1 May 2014 18:00:58 +0000 (UTC) To: mailing list for ConTeXt users Original-X-From: ntg-context-bounces@ntg.nl Thu May 01 20:00:52 2014 Return-path: Envelope-to: gctc-ntg-context-518@m.gmane.org Original-Received: from balder.ntg.nl ([5.39.185.229]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1WfvHw-0004Ia-5K for gctc-ntg-context-518@m.gmane.org; Thu, 01 May 2014 20:00:52 +0200 Original-Received: from localhost (localhost [127.0.0.1]) by balder.ntg.nl (Postfix) with ESMTP id A51551025A for ; Thu, 1 May 2014 20:00:51 +0200 (CEST) X-Virus-Scanned: Debian amavisd-new at balder.ntg.nl Original-Received: from balder.ntg.nl ([127.0.0.1]) by localhost (balder.ntg.nl [127.0.0.1]) (amavisd-new, port 10024) with LMTP id nhrlPaX1JUOZ for ; Thu, 1 May 2014 20:00:51 +0200 (CEST) Original-Received: from balder.ntg.nl (localhost [IPv6:::1]) by balder.ntg.nl (Postfix) with ESMTP id 2AC9D10201 for ; Thu, 1 May 2014 20:00:47 +0200 (CEST) Original-Received: from localhost (localhost [127.0.0.1]) by balder.ntg.nl (Postfix) with ESMTP id A50CB101E8 for ; Thu, 1 May 2014 20:00:43 +0200 (CEST) X-Virus-Scanned: Debian amavisd-new at balder.ntg.nl Original-Received: from balder.ntg.nl ([127.0.0.1]) by localhost (balder.ntg.nl [127.0.0.1]) (amavisd-new, port 10024) with LMTP id X8rH7nq2YKzu for ; Thu, 1 May 2014 20:00:42 +0200 (CEST) Original-Received: from filter3-til.mf.surf.net (filter3-til.mf.surf.net [194.171.167.219]) by balder.ntg.nl (Postfix) with ESMTP id ECFB5101E1 for ; Thu, 1 May 2014 20:00:32 +0200 (CEST) Original-Received: from tombraider.mr.itd.umich.edu (smtp.mail.umich.edu [141.211.12.86]) by filter3-til.mf.surf.net (8.14.3/8.14.3/Debian-9.4) with ESMTP id s41I0S3S007876 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NOT) for ; Thu, 1 May 2014 20:00:30 +0200 Original-Received: FROM adi-thinkpad.local ((null) [135.0.226.162]) By tombraider.mr.itd.umich.edu ID 53628BBA.6762F.15170 ; Authuser adityam; 1 May 2014 14:00:26 EDT In-Reply-To: User-Agent: Alpine 2.02 (DEB 1266 2009-07-14) X-Bayes-Prob: 0.0001 (Score 0, tokens from: ntg-context@ntg.nl, base:default, @@RPTN) X-CanIt-Geo: ip=141.211.12.86; country=US; region=Michigan; city=Ann Arbor; latitude=42.2734; longitude=-83.7133; http://maps.google.com/maps?q=42.2734,-83.7133&z=6 X-CanItPRO-Stream: uu:ntg-context@ntg.nl (inherits from uu:default, base:default) X-Canit-Stats-ID: 06LUG0tME - 8fb4e5277089 - 20140501 (trained as not-spam) X-Scanned-By: CanIt (www . roaringpenguin . com) on 194.171.167.219 X-BeenThere: ntg-context@ntg.nl X-Mailman-Version: 2.1.14 Precedence: list List-Id: mailing list for ConTeXt users List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: ntg-context-bounces@ntg.nl Original-Sender: ntg-context-bounces@ntg.nl Xref: news.gmane.org gmane.comp.tex.context:87187 Archived-At: On Thu, 1 May 2014, Mikael P. Sundqvist wrote: > \startluacode > userdata = userdata or {} > function userdata.lundklippfyllbild(str, bredd, hojd) > if tex.dimen[0] / tex.dimen[2] < tex.dimen[4] / tex.dimen[6] then > local skalning = > -0.5*(tex.dimen[4]/tex.dimen[0]*tex.dimen[2]-tex.dimen[6]) > context("\\vskip%ssp", skalning) > context.externalfigure({str},{width=bredd}) > context.sprint("foo") > else > local skalning = > -0.5*(tex.dimen[6]/tex.dimen[2]*tex.dimen[0]-tex.dimen[4]) > context("\\hskip%ssp", skalning) > context.externalfigure({str},{height=hojd}) > context.sprint("bar") > end > end > \stopluacode > > \unexpanded\def\lundklippfyllbild#1#2#3{% > \startexternalfigurecollection[#1]% > \useexternalfigure[#1][#1]% > \stopexternalfigurecollection% > \clip[ > width=#2, > height=#3, > hoffset=0mm, > voffset=0mm, > ]{% > \framed[width=#2,height=#3,align={middle,lohi},offset=overlay,frame=off]{% > \dimen0=\externalfigurecollectionminwidth{#1}% > \dimen2=\externalfigurecollectionmaxheight{#1}% > \dimen4=#2% > \dimen6=#3% > {\ctxlua{userdata.lundklippfyllbild([==[#1]==],[==[#2]==],[==[#3]==])}}}}} > > > > and then run > > > \lundklippfyllbild{k.jpg}{\textwidth}{\textheight} Here is an eTeX solution. \unprotect \newdimen\d_scaleandclip_actual_wd \newdimen\d_scaleandclip_actual_ht \newdimen\d_scaleandclip_requested_wd \newdimen\d_scaleandclip_requested_ht \newbox\scaleandclip_box \installnamespace{scaleandclip} \installcommandhandler \????scaleandclip {scaleandclip} \????scaleandclip \setupscaleandclip [width=\textwidth, height=\textheight] \unexpanded\def\scaleandclip{\dodoubleempty\doscaleandclip} \def\doscaleandclip[#1][#2]% {\bgroup \ifsecondargument \edef\currentscaleandclip{#1}% \setupcurrentscaleandclip[#2]% \else\iffirstargument \doifassignmentelse{#1} {\let\currentscaleandclip\empty \setupcurrentscaleandclip[#1]} {\edef\currentscaleandclip{#1}} \else \let\currentscaleandclip\empty \fi\fi \dowithnextboxcs\scaleandclip_finish\hbox} \def\scaleandclip_finish {% \d_scaleandclip_requested_wd \dimexpr\scaleandclipparameter\c!width\relax \d_scaleandclip_requested_ht \dimexpr\scaleandclipparameter\c!height\relax % \d_scaleandclip_actual_wd\wd\nextbox \d_scaleandclip_actual_ht\dimexpr\ht\nextbox + \dp\nextbox\relax % \ifdim\dimexpr\d_scaleandclip_actual_wd*100/\d_scaleandclip_requested_wd < \dimexpr\d_scaleandclip_actual_ht*100/\d_scaleandclip_requested_ht \relax \setbox\scaleandclip_box\hbox {\scale[\c!width=\d_scaleandclip_requested_wd]{\box\nextbox}}% \scratchdimen=\the\dimexpr(\ht\scaleandclip_box - \d_scaleandclip_requested_ht)/2\relax \clip [ \c!voffset=\scratchdimen, \c!height=\d_scaleandclip_requested_ht, ]{\box\scaleandclip_box}% \else \setbox\scaleandclip_box\hbox {\scale[\c!height=\d_scaleandclip_requested_ht]{\box\nextbox}}% \scratchdimen=\the\dimexpr(\wd\scaleandclip_box - \d_scaleandclip_requested_wd)/2\relax \clip [ \c!hoffset=\scratchdimen, \c!width=\d_scaleandclip_requested_wd, ]{\box\scaleandclip_box}% \fi \egroup} \protect The usage is: \scaleandclip[width=..., height=...]{ ... any box ...} simimlar to the usage of \scale macro. \starttext \dontleavehmode \scaleandclip[width=3cm, height=3cm]{\externalfigure[cow.pdf][width=3cm,frame=on]} \scale[maxwidth=3cm, maxheight=3cm]{\externalfigure[cow.pdf][width=3cm,frame=on]} \dontleavehmode \scaleandclip[width=3cm, height=3cm]{\externalfigure[cow.pdf][width=3cm,height=10cm,frame=on]} \scale[maxwidth=3cm, maxheight=3cm]{\externalfigure[cow.pdf][width=3cm,height=10cm,frame=on]} \dontleavehmode \scaleandclip[width=3cm, height=3cm]{\externalfigure[cow.pdf][width=10cm,height=3cm,frame=on]} \scale[maxwidth=3cm, maxheight=3cm]{\externalfigure[cow.pdf][width=10cm,height=3cm,frame=on]} \stoptext ___________________________________________________________________________________ If your question is of interest to others as well, please add an entry to the Wiki! maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context webpage : http://www.pragma-ade.nl / http://tex.aanhet.net archive : http://foundry.supelec.fr/projects/contextrev/ wiki : http://contextgarden.net ___________________________________________________________________________________