<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.luatex.org/index.php?action=history&amp;feed=atom&amp;title=T_wo_T_r%3Aformat.lua</id>
	<title>T wo T r:format.lua - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.luatex.org/index.php?action=history&amp;feed=atom&amp;title=T_wo_T_r%3Aformat.lua"/>
	<link rel="alternate" type="text/html" href="https://wiki.luatex.org/index.php?title=T_wo_T_r:format.lua&amp;action=history"/>
	<updated>2026-05-31T15:12:28Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.31.1</generator>
	<entry>
		<id>https://wiki.luatex.org/index.php?title=T_wo_T_r:format.lua&amp;diff=3560&amp;oldid=prev</id>
		<title>Rkrug: Created page with &quot;  &lt;nowiki&gt;   --- Copyright (c) 2021 by Toadstone Enterprises. --- ISC-type license, see License.txt for details.   ------------------------------------------------------------...&quot;</title>
		<link rel="alternate" type="text/html" href="https://wiki.luatex.org/index.php?title=T_wo_T_r:format.lua&amp;diff=3560&amp;oldid=prev"/>
		<updated>2021-02-22T18:32:00Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;quot;  &amp;lt;nowiki&amp;gt;   --- Copyright (c) 2021 by Toadstone Enterprises. --- ISC-type license, see License.txt for details.   ------------------------------------------------------------...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;  &amp;lt;nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
--- Copyright (c) 2021 by Toadstone Enterprises.&lt;br /&gt;
--- ISC-type license, see License.txt for details.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
-----------------------------------------------------------------&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
local util = require(&amp;quot;utils&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
local filll = util.filll&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
-----------------------------------------------------------------&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
--- Page Layout&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
-- This should be set based on the current font!&lt;br /&gt;
tex.baselineskip = tex.sp(&amp;quot;14pt&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
-- set tex&amp;#039;s margins to the edge of the page, so that we can define&lt;br /&gt;
-- our own page layout more easily&lt;br /&gt;
tex.hoffset = tex.sp(&amp;quot;-1.in&amp;quot;)&lt;br /&gt;
tex.voffset = tex.sp(&amp;quot;-1.in&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
-- American paper size. USA!&lt;br /&gt;
local paper_width  = tex.sp(&amp;quot;8.5in&amp;quot;)&lt;br /&gt;
local paper_height = tex.sp(&amp;quot;11in&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
local top_margin    = tex.sp(&amp;quot;0.75in&amp;quot;)&lt;br /&gt;
local right_margin  = tex.sp(&amp;quot;0.75in&amp;quot;)&lt;br /&gt;
local bottom_margin = tex.sp(&amp;quot;0.75in&amp;quot;)&lt;br /&gt;
local left_margin   = tex.sp(&amp;quot;0.75in&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
-- header_height is the height of the header box, and it is&lt;br /&gt;
-- separated from the main text box by header_sep.&lt;br /&gt;
local header_height = tex.sp(&amp;quot;0.5in&amp;quot;)&lt;br /&gt;
local header_sep    = tex.sp(&amp;quot;0.25in&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
local footer_height = tex.sp(&amp;quot;0.5in&amp;quot;)&lt;br /&gt;
local footer_sep    = tex.sp(&amp;quot;0.25in&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
-- The main text box.&lt;br /&gt;
local text_width  = paper_width - (right_margin + left_margin)&lt;br /&gt;
local text_height = paper_height - (top_margin +&lt;br /&gt;
                                    header_height + header_sep +&lt;br /&gt;
                                    footer_height + footer_sep +&lt;br /&gt;
                                    bottom_margin)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
local page_number = 1&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
-----------------------------------------------------------------&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
--- We define all the fonts we will be using here.&lt;br /&gt;
--- This requires the luatex-plain format, or (untested) luaotfload.&lt;br /&gt;
&lt;br /&gt;
-- fonts.definers.read returns a table representing the font.&lt;br /&gt;
-- font.define ties this font to an integer which we can use to refer&lt;br /&gt;
-- to the desired font.&lt;br /&gt;
&lt;br /&gt;
local main_text_font_table   = fonts.definers.read(&amp;quot;lmroman12-regular.otf:mode=node;liga=true;kern=true;&amp;quot;, tex.sp(&amp;quot;12pt&amp;quot;))&lt;br /&gt;
local main_text_font         = font.define(main_text_font_table)&lt;br /&gt;
&lt;br /&gt;
local emph_text_font_table   = fonts.definers.read(&amp;quot;lmroman12-italic.otf:mode=node;liga=true;kern=true;&amp;quot;, tex.sp(&amp;quot;12pt&amp;quot;))&lt;br /&gt;
local emph_text_font         = font.define(emph_text_font_table)&lt;br /&gt;
&lt;br /&gt;
local bold_text_font_table   = fonts.definers.read(&amp;quot;lmroman12-bold.otf:mode=node;liga=true;kern=true;&amp;quot;, tex.sp(&amp;quot;12pt&amp;quot;))&lt;br /&gt;
local bold_text_font         = font.define(bold_text_font_table)&lt;br /&gt;
&lt;br /&gt;
local title_font_table   = fonts.definers.read(&amp;quot;lmroman17-regular.otf:mode=node;liga=true;kern=true;&amp;quot;, tex.sp(&amp;quot;17pt&amp;quot;))&lt;br /&gt;
local title_font         = font.define(title_font_table)&lt;br /&gt;
&lt;br /&gt;
local header_font_table      = fonts.definers.read(&amp;quot;lmroman12-regular.otf:mode=node;liga=true;kern=true;&amp;quot;, tex.sp(&amp;quot;12pt&amp;quot;))&lt;br /&gt;
local header_font            = font.define(header_font_table)&lt;br /&gt;
&lt;br /&gt;
local footer_font_table      = fonts.definers.read(&amp;quot;lmroman12-regular.otf:mode=node;liga=true;kern=true;&amp;quot;, tex.sp(&amp;quot;12pt&amp;quot;))&lt;br /&gt;
local footer_font            = font.define(footer_font_table)&lt;br /&gt;
&lt;br /&gt;
local footnote_font_table    = fonts.definers.read(&amp;quot;lmroman10-regular.otf::mode=node;liga=true;kern=true;&amp;quot;, tex.sp(&amp;quot;10pt&amp;quot;))&lt;br /&gt;
local footnote_font          = font.define(footnote_font_table)&lt;br /&gt;
&lt;br /&gt;
-- For the footnote markers.&lt;br /&gt;
local superscript_font_table = fonts.definers.read(&amp;quot;lmroman7-regular.otf:mode=node;liga=true;kern=true;&amp;quot;, tex.sp(&amp;quot;7pt&amp;quot;))&lt;br /&gt;
local superscript_font       = font.define(superscript_font_table)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
-----------------------------------------------------------------&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
--- This is the state that we will be passing around as we typeset.&lt;br /&gt;
&lt;br /&gt;
State = {-- What are we typesetting now?&lt;br /&gt;
         -- One of &amp;quot;main_text&amp;quot;, &amp;quot;header&amp;quot;, &amp;quot;footer&amp;quot;, or &amp;quot;footnote&amp;quot;&lt;br /&gt;
         mode = &amp;quot;main_text&amp;quot;,&lt;br /&gt;
&lt;br /&gt;
         -- Where we will store the main text as we build our paragraphs.&lt;br /&gt;
         main   = nil,&lt;br /&gt;
         -- Where we store the header and footer, as we build them.&lt;br /&gt;
         header = nil,&lt;br /&gt;
         footer = nil,&lt;br /&gt;
         -- We use a table of notes, indexed by footnote number&lt;br /&gt;
         max_notes = 0,&lt;br /&gt;
         notes     = {},&lt;br /&gt;
&lt;br /&gt;
         -- We store some of main_loops local variables, so that we&lt;br /&gt;
         -- can pass them around as needed.&lt;br /&gt;
         head          = nil,&lt;br /&gt;
         tail          = nil,&lt;br /&gt;
         eat_the_white = false,&lt;br /&gt;
         eol_seen      = false,&lt;br /&gt;
&lt;br /&gt;
         -- We want to be able to use different parameters for typesetting&lt;br /&gt;
         -- the different pieces of text. A header is not the same as a&lt;br /&gt;
         -- footnote. We therefore keep a stack of tbls containing the&lt;br /&gt;
         -- settings for formatting a paragraph (these are passed to&lt;br /&gt;
         -- tex.linebreak).&lt;br /&gt;
         max_tbls = 0,&lt;br /&gt;
         tbls     = {},&lt;br /&gt;
&lt;br /&gt;
         -- When we see a command, do_command_start will (potentially)&lt;br /&gt;
         -- be filled in with the part of the command to do upon&lt;br /&gt;
         -- encountering a &amp;quot;{&amp;quot;&lt;br /&gt;
         do_command_start     = nil,&lt;br /&gt;
         -- And a stack of do_command_stops: do_command_stops&lt;br /&gt;
         -- We need a stack, because commands can be nested.&lt;br /&gt;
         -- e.g., an Emph inside a Bold.&lt;br /&gt;
         -- We need only one do_command_start, because that is for&lt;br /&gt;
         -- the current command and the next &amp;quot;{&amp;quot; seen.&lt;br /&gt;
         max_do_command_stops = 0,&lt;br /&gt;
         do_command_stops     = {},&lt;br /&gt;
&lt;br /&gt;
         -- A place to store the text to be made into the header.&lt;br /&gt;
         header_text = nil,&lt;br /&gt;
&lt;br /&gt;
         -- So we can format the first paragraph of a chapter&lt;br /&gt;
         -- differently from the rest.&lt;br /&gt;
         first_paragraph_of_chapter = false,&lt;br /&gt;
         -- So we can format the first page of a chapter&lt;br /&gt;
         -- differently from the rest.&lt;br /&gt;
         first_page_of_chapter      = false,&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
local main_text_tbl = {hsize = text_width,&lt;br /&gt;
&lt;br /&gt;
                       parindent                = tex.parindent,&lt;br /&gt;
                       parfillskipstretch       = 2^16,&lt;br /&gt;
                       parfillskipstretch_order = 2,&lt;br /&gt;
&lt;br /&gt;
                       font           = main_text_font,&lt;br /&gt;
                       space          = font.fonts[main_text_font].parameters.space,&lt;br /&gt;
                       space_stretch  = font.fonts[main_text_font].parameters.space_stretch,&lt;br /&gt;
                       space_shrink   = font.fonts[main_text_font].parameters.space_shrink,&lt;br /&gt;
&lt;br /&gt;
                       lang           = tex.language,&lt;br /&gt;
                       lefthyphenmin  = tex.lefthyphenmin,&lt;br /&gt;
                       righthyphenmin = tex.righthyphenmin,&lt;br /&gt;
                      }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
local header_tbl = {hsize = text_width,&lt;br /&gt;
&lt;br /&gt;
                    -- The header will be centered.&lt;br /&gt;
                    leftskip                 = filll(),&lt;br /&gt;
                    rightskip                = filll(),&lt;br /&gt;
                    parindent                = 0,&lt;br /&gt;
                    parfillskipstretch       = 2^16,&lt;br /&gt;
                    parfillskipstretch_order = 2,&lt;br /&gt;
&lt;br /&gt;
                    font           = header_font,&lt;br /&gt;
                    space          = font.fonts[header_font].parameters.space,&lt;br /&gt;
                    space_stretch  = font.fonts[header_font].parameters.space_stretch,&lt;br /&gt;
                    space_shrink   = font.fonts[header_font].parameters.space_shrink,&lt;br /&gt;
                          &lt;br /&gt;
                    lang           = tex.language,&lt;br /&gt;
                    lefthyphenmin  = tex.lefthyphenmin,&lt;br /&gt;
                    righthyphenmin = tex.righthyphenmin,&lt;br /&gt;
                   }&lt;br /&gt;
&lt;br /&gt;
local footer_tbl = {hsize = text_width,&lt;br /&gt;
                &lt;br /&gt;
                    leftskip                 = filll(),&lt;br /&gt;
                    rightskip                = filll(),&lt;br /&gt;
                    parindent                = 0,&lt;br /&gt;
                    parfillskipstretch       = 2^16,&lt;br /&gt;
                    parfillskipstretch_order = 2,&lt;br /&gt;
&lt;br /&gt;
                    font           = footer_font,&lt;br /&gt;
                    space          = font.fonts[footer_font].parameters.space,&lt;br /&gt;
                    space_stretch  = font.fonts[footer_font].parameters.space_stretch,&lt;br /&gt;
                    space_shrink   = font.fonts[footer_font].parameters.space_shrink,&lt;br /&gt;
                          &lt;br /&gt;
                    lang           = tex.language,&lt;br /&gt;
                    lefthyphenmin  = tex.lefthyphenmin,&lt;br /&gt;
                    righthyphenmin = tex.righthyphenmin,&lt;br /&gt;
                   }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
local footnote_tbl = {hsize = text_width,&lt;br /&gt;
                &lt;br /&gt;
                       parindent                = 0,&lt;br /&gt;
                       parfillskipstretch       = 2^16,&lt;br /&gt;
                       parfillskipstretch_order = 2,&lt;br /&gt;
&lt;br /&gt;
                       font           = footnote_font,&lt;br /&gt;
                       space          = font.fonts[footnote_font].parameters.space,&lt;br /&gt;
                       space_stretch  = font.fonts[footnote_font].parameters.space_stretch,&lt;br /&gt;
                       space_shrink   = font.fonts[footnote_font].parameters.space_shrink,&lt;br /&gt;
                          &lt;br /&gt;
                       lang           = tex.language,&lt;br /&gt;
                       lefthyphenmin  = tex.lefthyphenmin,&lt;br /&gt;
                       righthyphenmin = tex.righthyphenmin,&lt;br /&gt;
                      }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
local update_state = function(head, tail, eat_the_white, eol_seen)&lt;br /&gt;
  -- As we pass the State around, we wil want to pass along the&lt;br /&gt;
  -- current values of head, tail, eat_the_white, and eol_seen.&lt;br /&gt;
  -- See main_loop and the commands for examples.&lt;br /&gt;
  State.head          = head&lt;br /&gt;
  State.tail          = tail&lt;br /&gt;
  State.eat_the_white = eat_the_white&lt;br /&gt;
  State.eol_seen      = eol_seen&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
local update_locals = function()&lt;br /&gt;
  -- As we return from calls in which we passed the State out, the&lt;br /&gt;
  -- values of head, tail, eat_the_white, and eol_seen may have been&lt;br /&gt;
  -- changed. So we update them from the current State. See main_loop&lt;br /&gt;
  -- and the commands for examples.&lt;br /&gt;
  local head          = State.head&lt;br /&gt;
  local tail          = State.tail&lt;br /&gt;
  local eat_the_white = State.eat_the_white&lt;br /&gt;
  local eol_seen      = State.eol_seen&lt;br /&gt;
  return head, tail, eat_the_white, eol_seen&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
local push_tbl = function(tbl)&lt;br /&gt;
  local max           = State.max_tbls&lt;br /&gt;
  State.max_tbls      = max + 1&lt;br /&gt;
  State.tbls[max + 1] = tbl&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
local pop_tbl = function()&lt;br /&gt;
  local max       = State.max_tbls&lt;br /&gt;
  if (max &amp;gt; 0) then&lt;br /&gt;
    local tbl = State.tbls[max]&lt;br /&gt;
    State.tbls[max] = nil&lt;br /&gt;
    State.max_tbls  = max - 1&lt;br /&gt;
    return tbl&lt;br /&gt;
  else&lt;br /&gt;
    return nil&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
local top_tbl = function()&lt;br /&gt;
  local max       = State.max_tbls&lt;br /&gt;
  if (max &amp;gt; 0) then&lt;br /&gt;
    return State.tbls[max]&lt;br /&gt;
  else&lt;br /&gt;
    return nil&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
local push_do_command_stop = function(do_command_stop)&lt;br /&gt;
  local max                       = State.max_do_command_stops&lt;br /&gt;
  State.max_do_command_stops      = max + 1&lt;br /&gt;
  State.do_command_stops[max + 1] = do_command_stop&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
local pop_do_command_stop = function()&lt;br /&gt;
  local max                     = State.max_do_command_stops&lt;br /&gt;
  if (max &amp;gt; 0) then&lt;br /&gt;
    local do_command_stop       = State.do_command_stops[max]&lt;br /&gt;
    State.do_command_stops[max] = nil&lt;br /&gt;
    State.max_do_command_stops  = max - 1&lt;br /&gt;
    return do_command_stop&lt;br /&gt;
  else&lt;br /&gt;
    return nil&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
local format = {paper_width  = paper_width,&lt;br /&gt;
                paper_height = paper_height,&lt;br /&gt;
                &lt;br /&gt;
                top_margin    = top_margin,&lt;br /&gt;
                right_margin  = right_margin,&lt;br /&gt;
                bottom_margin = bottom_margin,&lt;br /&gt;
                left_margin   = left_margin,&lt;br /&gt;
                header_height = header_height,&lt;br /&gt;
                header_sep    = header_sep,&lt;br /&gt;
                footer_height = footer_height,&lt;br /&gt;
                footer_sep    = footer_sep,&lt;br /&gt;
                text_width    = text_width,&lt;br /&gt;
                text_height   = text_height,&lt;br /&gt;
                &lt;br /&gt;
                page_number = page_number,&lt;br /&gt;
&lt;br /&gt;
                update_state         = update_state,&lt;br /&gt;
                update_locals        = update_locals,&lt;br /&gt;
                push_tbl             = push_tbl,&lt;br /&gt;
                pop_tbl              = pop_tbl,&lt;br /&gt;
                top_tbl              = top_tbl,&lt;br /&gt;
                push_do_command_stop = push_do_command_stop,&lt;br /&gt;
                pop_do_command_stop  = pop_do_command_stop,&lt;br /&gt;
                &lt;br /&gt;
                main_text_tbl = main_text_tbl,&lt;br /&gt;
                header_tbl    = header_tbl,&lt;br /&gt;
                footer_tbl    = footer_tbl,&lt;br /&gt;
                footnote_tbl  = footnote_tbl,&lt;br /&gt;
&lt;br /&gt;
                main_text_font   = main_text_font,&lt;br /&gt;
                emph_text_font   = emph_text_font,&lt;br /&gt;
                bold_text_font   = bold_text_font,&lt;br /&gt;
                title_font       = title_font,&lt;br /&gt;
                header_font      = header_font,&lt;br /&gt;
                footer_font      = footer_font,&lt;br /&gt;
                footnote_font    = footnote_font,&lt;br /&gt;
                superscript_font = superscript_font,&lt;br /&gt;
               }&lt;br /&gt;
&lt;br /&gt;
return format&lt;br /&gt;
&amp;lt;/nowiki&amp;gt;&lt;/div&gt;</summary>
		<author><name>Rkrug</name></author>
		
	</entry>
</feed>