ntg-context - mailing list for ConTeXt users
 help / color / mirror / Atom feed
From: "Mikael P. Sundqvist" <mickep@gmail.com>
To: mailing list for ConTeXt users <ntg-context@ntg.nl>
Subject: Re: resize and center and clip image
Date: Thu, 1 May 2014 21:27:26 +0200	[thread overview]
Message-ID: <CAHy-LL_cjVCh4NAq=hmmL6jT_WEJnpABT3Q8Yuo_3uH2LSE2ag@mail.gmail.com> (raw)
In-Reply-To: <alpine.DEB.2.02.1405011353370.7265@nqv-guvaxcnq>

On Thu, May 1, 2014 at 8:00 PM, Aditya Mahajan <adityam@umich.edu> wrote:
> 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
> ___________________________________________________________________________________


Thanks Aditya!

That is more general and looks more ConTeXt-ish to me with the
options. I should study your code in detail to learn how that is done.

Mikael
___________________________________________________________________________________
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
___________________________________________________________________________________


  reply	other threads:[~2014-05-01 19:27 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-05-01 11:50 Mikael P. Sundqvist
2014-05-01 13:38 ` Mikael P. Sundqvist
2014-05-01 14:59   ` Otared Kavian
2014-05-01 15:07     ` Mikael P. Sundqvist
2014-05-01 16:43       ` Otared Kavian
2014-05-01 18:00       ` Aditya Mahajan
2014-05-01 19:27         ` Mikael P. Sundqvist [this message]
2014-05-01 19:31         ` Wolfgang Schuster

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAHy-LL_cjVCh4NAq=hmmL6jT_WEJnpABT3Q8Yuo_3uH2LSE2ag@mail.gmail.com' \
    --to=mickep@gmail.com \
    --cc=ntg-context@ntg.nl \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).