ntg-context - mailing list for ConTeXt users
 help / color / mirror / Atom feed
From: Otared Kavian <otared@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 18:43:36 +0200	[thread overview]
Message-ID: <0CC7AD92-0342-4501-98C8-B7E0196322E1@gmail.com> (raw)
In-Reply-To: <CAHy-LL-B0L3aF3H0-E=Hofbyjj6LoDEEAwJsWqU5XkGhrtizMg@mail.gmail.com>

Hi Mikael,

With your new definitions and lua code I could typeset and test a few examples: it is very nice, thanks for sharing!

Best regards: OK

On 1 mai 2014, at 17:07, Mikael P. Sundqvist <mickep@gmail.com> wrote:

> Interesting,
> 
> I use ConTeXt  ver: 2014.04.28 23:24 MKIV beta  fmt: 2014.4.29  int:
> english/english
> 
> and it works. I now defined this with argument:
> 
> \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}
> 
> 
> it works well. Of course you should replace k.jpg with some other image name...
> 
> /Mikael
> 
> PS And please, if someone has opinions about the code, don't hesitate
> to tell me. This is my first time using lua...
> 
> 
> On Thu, May 1, 2014 at 4:59 PM, Otared Kavian <otared@gmail.com> wrote:
>> Hi Mikael,
>> 
>> Out of curiosity I tried to typeset your example (after adding the usual \starttext, \stoptext), but using
>>        ConTeXt  ver: 2014.04.28 23:24 MKIV beta  fmt: 2014.4.29  int: english/english
>> I obtained a TeX error:
>> 
>> tex error       > error on line 13 in file /Users/kavian/Context/essais/mkii/figures/resize-and-center.tex: ! Undefined control sequence
>> 
>> 
>> \startexternalfigurecollection ...igurecollection
>>                                                  {#1}\settrue \c_grph_inclu...
>> l.13 \clip[width=8cm,height=7cm]
>>                              {%
>> 
>> 3     \startexternalfigurecollection[k.jpg]
>> 4         \useexternalfigure[k.jpg][k.jpg]
>> 5     \stopexternalfigurecollection
>> 
>> I did not understand what is the « Undefined control sequence », since I think \clip is a known control sequence.
>> 
>> Best regards: OK
>> 
>> On 1 mai 2014, at 15:38, Mikael P. Sundqvist <mickep@gmail.com> wrote:
>> 
>>> OK, sorry for the noise. I think I have the solution:
>>> 
>>> \startexternalfigurecollection[k.jpg]
>>>    \useexternalfigure[k.jpg][k.jpg]
>>> \stopexternalfigurecollection
>>> 
>>> 
>>> \externalfigurecollectionminwidth{k.jpg}
>>> \externalfigurecollectionmaxheight{k.jpg}
>>> 
>>> \clip[width=8cm,height=7cm]{%
>>> \framed[width=8cm,height=7cm,align={middle,lohi},offset=overlay]{%
>>> \dimen0=\externalfigurecollectionminwidth{k.jpg}
>>> \dimen2=\externalfigurecollectionmaxheight{k.jpg}
>>> \dimen4=8cm
>>> \dimen6=7cm
>>> \startluacode
>>> 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({"k.jpg"},{width="8cm"})
>>>     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({"k.jpg"},{height="7cm"})
>>>     context.sprint("bar")
>>> end
>>> \stopluacode
>>> }}
>>> 
>>> If there are any bad side effects of this or simplifications, please
>>> do not hesitate to tell me :)
>>> 
>>> /Mikael
>>> 
>>> On Thu, May 1, 2014 at 1:50 PM, Mikael P. Sundqvist <mickep@gmail.com> wrote:
>>>> Hi!
>>>> 
>>>> Assume that I want to fill in the text area with images, and I want to
>>>> clip and scale them according to these rules:
>>>> 
>>>> 1) If the image is "too wide" (in sense of proportions), then scale it
>>>> so that the height fits the text area height, center it, and clip it
>>>> to the text area. (See the attached image, on top, where the blue area
>>>> is supposed to show the text area and the red rectangle the image.)
>>>> 
>>>> 2) If the image is "too high" (in sense of proportions), then scale it
>>>> so that the width fits the text area width, center it, and clip it to
>>>> the text area. (See the attached image, on bottom.)
>>>> 
>>>> The background why I want to do this is that I'm making a small
>>>> template for presentations, and I'd like the users to insert images as
>>>> background without really having to think about if their proportions
>>>> fit or not.
>>>> 
>>>> Best regards, 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
>>> ___________________________________________________________________________________
>> 
>> ___________________________________________________________________________________
>> 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
>> ___________________________________________________________________________________
> ___________________________________________________________________________________
> 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
> ___________________________________________________________________________________

___________________________________________________________________________________
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 16:43 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 [this message]
2014-05-01 18:00       ` Aditya Mahajan
2014-05-01 19:27         ` Mikael P. Sundqvist
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=0CC7AD92-0342-4501-98C8-B7E0196322E1@gmail.com \
    --to=otared@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).