ntg-context - mailing list for ConTeXt users
 help / color / mirror / Atom feed
* Setting and measuring boxes TeX/Lua
@ 2020-11-25 18:31 Jano Kula
  2020-11-25 20:25 ` Hans Hagen
  0 siblings, 1 reply; 6+ messages in thread
From: Jano Kula @ 2020-11-25 18:31 UTC (permalink / raw)
  To: mailing list for ConTeXt users


[-- Attachment #1.1: Type: text/plain, Size: 1804 bytes --]

Hello!

I would like to set framed boxes, measure their dimensions and proceed
based on the measurement. It is a combination of TeX and Lua ends.

   - Boxes are set on the TeX side. Stacking boxes (hbox, framed, vbox) in
   Lua looked too complex.
   - The content of the inner box is set on the Lua side (some
   computations).

On TeX side boxes and their dimensions are accessible, but not at Lua side,
until luacode is finished (iterations over boxes happen here). I would say
Hans already named the problem in the list: *that is always tricky as what
happens is postponing till the group ends*.

Do you have an idea how to expand the box sooner or would you use a
different approach?

Sorry for longer MWE (attached also).

Thank you,
Jano

\def\StartBox{\setbox0=\hbox\bgroup% open hbox
                \framed[frame=on]\bgroup% open framed
                    \vbox\bgroup% open vbox (\par needed)
             }
\def\StopBox{\egroup\egroup\egroup% close vbox, framed, hbox
             \setvariable{Test}{Height}{\the\ht0}%
            }%

\startluacode
ctx = context

function TestBox()
    ctx("TeX Box: ")    ctx.copy(false,0)ctx.par()
    ctx("TeX Getvar: ") ctx.getvariable("Test", "Height")ctx.par()
    var = tokens.getters.macro(tokens.getters.macro("??variables") ..
"Test:Height")
    ctx("Lua Type: ")   ctx(type(var))ctx.par()
    ctx("Lua Value: ")  ctx(var)ctx.par()
end
\stopluacode

\starttext

\startluacode
    ctx.bold("First luacode (more boxes)")ctx.par()
    ctx.StartBox()
    ctx("AAA")ctx.par()
    ctx("BBB")ctx.par()
    ctx.StopBox()
    TestBox()
    ctx.StartBox()
    ctx("CCC")ctx.par()
    ctx.StopBox()
    TestBox()
\stopluacode

\blank[4*big]

\startluacode
    ctx.bold("Second luacode (last content of box0)")ctx.par()
    TestBox()
\stopluacode

\stoptext

[-- Attachment #1.2: Type: text/html, Size: 2397 bytes --]

[-- Attachment #2: texbox-complex.tex --]
[-- Type: application/x-tex, Size: 1100 bytes --]

[-- Attachment #3: Type: text/plain, Size: 493 bytes --]

___________________________________________________________________________________
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://context.aanhet.net
archive  : https://bitbucket.org/phg/context-mirror/commits/
wiki     : http://contextgarden.net
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Setting and measuring boxes TeX/Lua
  2020-11-25 18:31 Setting and measuring boxes TeX/Lua Jano Kula
@ 2020-11-25 20:25 ` Hans Hagen
  2020-11-25 21:28   ` Jano Kula
  0 siblings, 1 reply; 6+ messages in thread
From: Hans Hagen @ 2020-11-25 20:25 UTC (permalink / raw)
  To: mailing list for ConTeXt users, Jano Kula

On 11/25/2020 7:31 PM, Jano Kula wrote:
> Hello!
> 
> I would like to set framed boxes, measure their dimensions and proceed 
> based on the measurement. It is a combination of TeX and Lua ends.
> 
>   * Boxes are set on the TeX side. Stacking boxes (hbox, framed, vbox)
>     in Lua looked too complex.
>   * The content of the inner box is set on the Lua side (some computations).
> 
> On TeX side boxes and their dimensions are accessible, but not at Lua 
> side, until luacode is finished (iterations over boxes happen here). I 
> would say Hans already named the problem in the list: /that is always 
> tricky as what happens is postponing till the group ends/.
> 
> Do you have an idea how to expand the box sooner or would you use a 
> different approach?
> 
> Sorry for longer MWE (attached also).
> 
> Thank you,
> Jano
> 
> \def\StartBox{\setbox0=\hbox\bgroup% open hbox
>                  \framed[frame=on]\bgroup% open framed
>                      \vbox\bgroup% open vbox (\par needed)
>               }
> \def\StopBox{\egroup\egroup\egroup% close vbox, framed, hbox
>               \setvariable{Test}{Height}{\the\ht0}%
>              }%
> 
> \startluacode
> ctx = context
> 
> function TestBox()
>      ctx("TeX Box: ")    ctx.copy(false,0)ctx.par()
>      ctx("TeX Getvar: ") ctx.getvariable("Test", "Height")ctx.par()
>      var = tokens.getters.macro(tokens.getters.macro("??variables") .. 
> "Test:Height")
>      ctx("Lua Type: ")   ctx(type(var))ctx.par()
>      ctx("Lua Value: ")  ctx(var)ctx.par()
> end
> \stopluacode
> 
> \starttext
> 
> \startluacode
>      ctx.bold("First luacode (more boxes)")ctx.par()
>      ctx.StartBox()
>      ctx("AAA")ctx.par()
>      ctx("BBB")ctx.par()
>      ctx.StopBox()
>      TestBox()
>      ctx.StartBox()
>      ctx("CCC")ctx.par()
>      ctx.StopBox()
>      TestBox()
> \stopluacode
> 
> \blank[4*big]
> 
> \startluacode
>      ctx.bold("Second luacode (last content of box0)")ctx.par()
>      TestBox()
> \stopluacode
> 
> \stoptext
\startluacode
  context.stepwise (function()
     ctx.bold("First luacode (more boxes)")ctx.par()
     ctx.StartBox()
     ctx("AAA")ctx.par()
     ctx("BBB")ctx.par()
     ctx.StopBox()
     context.step()
     TestBox()
     context.step()
     ctx.StartBox()
     ctx("CCC")ctx.par()
     ctx.StopBox()
     context.step()
     TestBox()
     context.step()
end)
\stopluacode

-----------------------------------------------------------------
                                           Hans Hagen | PRAGMA ADE
               Ridderstraat 27 | 8061 GH Hasselt | The Netherlands
        tel: 038 477 53 69 | www.pragma-ade.nl | www.pragma-pod.nl
-----------------------------------------------------------------
___________________________________________________________________________________
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://context.aanhet.net
archive  : https://bitbucket.org/phg/context-mirror/commits/
wiki     : http://contextgarden.net
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Setting and measuring boxes TeX/Lua
  2020-11-25 20:25 ` Hans Hagen
@ 2020-11-25 21:28   ` Jano Kula
  2020-11-26  8:18     ` Hans Hagen
  0 siblings, 1 reply; 6+ messages in thread
From: Jano Kula @ 2020-11-25 21:28 UTC (permalink / raw)
  To: Hans Hagen; +Cc: mailing list for ConTeXt users


[-- Attachment #1.1: Type: text/plain, Size: 533 bytes --]

Hello Hans!

On Wed, 25 Nov 2020 at 21:25, Hans Hagen <j.hagen@xs4all.nl> wrote:

> \startluacode
> context.stepwise (function()
>      context.step()
> end)
> \stopluacode
>

Thank you very much!

I was trying context.stepwise several times (and \dowithnextbox,
\nextboxht and
other things). I bet there was no example of empty context.step().

The usage was: stepping in TeX end with
context.step("\\texcommands\\with\\doubleslashes") without leaving Lua.
What is the purpose of empty context.step()? Knocking on TeX's door?

Jano

[-- Attachment #1.2: Type: text/html, Size: 1158 bytes --]

[-- Attachment #2: Type: text/plain, Size: 493 bytes --]

___________________________________________________________________________________
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://context.aanhet.net
archive  : https://bitbucket.org/phg/context-mirror/commits/
wiki     : http://contextgarden.net
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Setting and measuring boxes TeX/Lua
  2020-11-25 21:28   ` Jano Kula
@ 2020-11-26  8:18     ` Hans Hagen
  2020-11-27 16:47       ` Jano Kula
  0 siblings, 1 reply; 6+ messages in thread
From: Hans Hagen @ 2020-11-26  8:18 UTC (permalink / raw)
  To: Jano Kula; +Cc: mailing list for ConTeXt users

On 11/25/2020 10:28 PM, Jano Kula wrote:
> Hello Hans!
> 
> On Wed, 25 Nov 2020 at 21:25, Hans Hagen <j.hagen@xs4all.nl 
> <mailto:j.hagen@xs4all.nl>> wrote:
> 
>     \startluacode
>     context.stepwise (function()
>           context.step()
>     end)
>     \stopluacode
> 
> 
> Thank you very much!
> 
> I was trying context.stepwise several times (and \dowithnextbox, 
> \nextboxht and other things). I bet there was no example of empty 
> context.step().
> 
> The usage was: stepping in TeX end with 
> context.step("\\texcommands\\with\\doubleslashes") without leaving Lua.
> What is the purpose of empty context.step()? Knocking on TeX's door?
no, kind of quit, goto tex and then pickup again (coroutine magic)

(this mechanism is already kind of old, there are also other cheats 
possible)

Hans

-----------------------------------------------------------------
                                           Hans Hagen | PRAGMA ADE
               Ridderstraat 27 | 8061 GH Hasselt | The Netherlands
        tel: 038 477 53 69 | www.pragma-ade.nl | www.pragma-pod.nl
-----------------------------------------------------------------
___________________________________________________________________________________
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://context.aanhet.net
archive  : https://bitbucket.org/phg/context-mirror/commits/
wiki     : http://contextgarden.net
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Setting and measuring boxes TeX/Lua
  2020-11-26  8:18     ` Hans Hagen
@ 2020-11-27 16:47       ` Jano Kula
  2020-11-27 18:05         ` Hans Hagen
  0 siblings, 1 reply; 6+ messages in thread
From: Jano Kula @ 2020-11-27 16:47 UTC (permalink / raw)
  To: Hans Hagen; +Cc: mailing list for ConTeXt users


[-- Attachment #1.1: Type: text/plain, Size: 2836 bytes --]

Hello again, Hans!

On Thu, 26 Nov 2020 at 09:18, Hans Hagen <j.hagen@xs4all.nl> wrote:

> (this mechanism is already kind of old, there are also other cheats
> possible)
>

Diving into functions and using context.stepwise() worked, until I've got
to: /cldf-stp.lua:48: bad argument #1 to 'resume' (thread expected) and
other errors. It was triar/error approach, not exactly knowing how
context.stepwise() works, and I was some more lua functions and iterations
before needing expansion.

Therefore I've changed the strategy (cannot give up):

   - Create named buffers with formatted content (and store measures)
   before they are needed.
   - When needed, use the reference to the buffer.

Tests looked promising. After \starttext buffers and measures are known
(both TeX & Lua). Also tested in functions, etc.
But inside the first \xmlsetup, they are not expanded anymore, they are
typeset (like \typebuffer).

   - How to have them expanded in \xmlsetup?
   - Is other mechanism better suited for storing typeset blocks than
   buffers (and will survive \xmlsetup)?
   - If none of them is possible, what are the *other **cheats *then?

Thanks,
Jano

MWE attached

% BUFFERS
\startluacode
-- fill buffers
for i=1,1000 do
    local content = "framed-" .. i
    context.startbuffer( { i } )
    context.StartBox()
    context.bold(i)context.par()
    context.color( { "red" }, content )
    context.StopBox()
    context.copy(false,0)
    context.stopbuffer()
    print(">>", i)
end
\stopluacode

\def\StartBox{\setbox0=\hbox\bgroup\framed[width=50mm,
                                           background=color,
                                           backgroundcolor=gray,
                                           frame=off,align={flushleft}]%
                                           \bgroup%
                                                \vbox\bgroup}
\def\StopBox{\egroup\egroup\egroup}% close vbox, framed, hbox

\def\MeasureBuffer[#1]{\setbox0\hbox{\getbuffer[#1]}%
                       ht: \the\ht0\par}


% XML
\startbuffer[doc]
<?xml version "1.0"?>
<document>
    <section>section</section>
    <section>section</section>
</document>
\stopbuffer

\startxmlsetups xml:name
    \xmlsetsetup{\xmldocument}{*}{-}
    \xmlsetsetup{\xmldocument}{document}{xml:name:*}
\stopxmlsetups

\xmlregistersetup{xml:name}

\startxmlsetups xml:name:document
    \Title{inside xml:name:document}
    \Title{TeX}
    \MeasureBuffer[333]
    \getbuffer[333]
    \Title{Lua}
    \startluacode
    context.MeasureBuffer({"444"})
    context.getbuffer({"444"})
    \stopluacode
\stopxmlsetups

\def\Title#1{\blank{\bf #1}\par}


\starttext

\Title{TeX}
\MeasureBuffer[111]
\getbuffer[111]

\Title{Lua}
\startluacode
context.MeasureBuffer({"222"})
context.getbuffer({"222"})
\stopluacode

\xmlprocessbuffer{xml:name}{doc}{}

\stoptext

[-- Attachment #1.2: Type: text/html, Size: 4327 bytes --]

[-- Attachment #2: xml-buffers.tex --]
[-- Type: application/x-tex, Size: 1740 bytes --]

[-- Attachment #3: Type: text/plain, Size: 493 bytes --]

___________________________________________________________________________________
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://context.aanhet.net
archive  : https://bitbucket.org/phg/context-mirror/commits/
wiki     : http://contextgarden.net
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Setting and measuring boxes TeX/Lua
  2020-11-27 16:47       ` Jano Kula
@ 2020-11-27 18:05         ` Hans Hagen
  0 siblings, 0 replies; 6+ messages in thread
From: Hans Hagen @ 2020-11-27 18:05 UTC (permalink / raw)
  To: Jano Kula; +Cc: mailing list for ConTeXt users

Hi Jano,

It's not clear to me what you want to measure when but here is a weekend 
playground (you're locked down anyway).

\def\MacroJano#1%
   {\writestatus{JANO}{ONE}
    \setbox0=\hbox\bgroup\xmlflush{#1}\egroup}

\startluacode
     function document.FunctionJano(id)
         context.writestatus("JANO","ZERO")
         token.expand_macro("MacroJano",true,id)
         context("%p",tex.box[0].width)
         context.writestatus("JANO","TWO")
     end
\stopluacode

\startbuffer[doc]
<?xml version "1.0"?>
<document>
     <section>one</section>
     <section>one two</section>
     <section>one two three</section>
</document>
\stopbuffer

\startxmlsetups xml:name
     \xmlsetsetup{#1}{*}{xml:*}
\stopxmlsetups

\xmlregistersetup{xml:name}

\startxmlsetups xml:section
     width: \ctxlua{document.FunctionJano("#1")}\par
\stopxmlsetups

\startxmlsetups xml:document
     \xmlflush{#1}
\stopxmlsetups

\starttext

     \xmlprocessbuffer{xml:name}{doc}{}

\stoptext

A clue: token.expand_macro will kind of 'run a local tex job'.

If this works for you. you know the price you have to pay ... wikify

Hans


-----------------------------------------------------------------
                                           Hans Hagen | PRAGMA ADE
               Ridderstraat 27 | 8061 GH Hasselt | The Netherlands
        tel: 038 477 53 69 | www.pragma-ade.nl | www.pragma-pod.nl
-----------------------------------------------------------------
___________________________________________________________________________________
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://context.aanhet.net
archive  : https://bitbucket.org/phg/context-mirror/commits/
wiki     : http://contextgarden.net
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2020-11-27 18:05 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-11-25 18:31 Setting and measuring boxes TeX/Lua Jano Kula
2020-11-25 20:25 ` Hans Hagen
2020-11-25 21:28   ` Jano Kula
2020-11-26  8:18     ` Hans Hagen
2020-11-27 16:47       ` Jano Kula
2020-11-27 18:05         ` Hans Hagen

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).