Parent repeater layout

A simple-as-possible set of page layout rules that allows for a wide range of automatic layout

by i_e_b

HTML

<canvas id="target" width="650" height="450"></canvas>
<pre id="diag"></pre>
<h3>Parent repeater layout</h3>
<p>Purpose: A simple-as-possible set of page layout rules that allows for a wide range of automatic layout</p>
<p>Concept: Outer most container is fixed size. There is a single chain/list (NOT tree) of
sub-container types for the document. Each type has two link points (next,prev), which are relative to one of the corners (TL,TR,BL,BR). Each type has either a fixed or relative horz and vert size. Zero size means it will grow to fit its children, as long as that doesn't overflow the parent.</p>
<p>Contents are added to the leaf type, giving a fixed size (like the image size or glyph size). The layout is filled by adding a list of contents. This list can also contain 'breaks' which ends containers up to a certain level (like BRK1 stops the word, BRK2 stops the line, BRK3 stops the column)</p>
<p>When a content item can't fit in its container, a new parent container is spawned, and joined next->prev. If that container can't fit, the next parent up is created the same way. This continues up until success. If no success in the entire chain, the document is full. If a newly created container can't hold a content item, is this a fail?</p>
<p>If the type has <tt>glue</tt> set, then the content will break it's parent container if not all items can be fit together. This is useful for keeping letters together in words, but requires you to manually break each run</p>

<h3>Format</h3>
<p>A JSON array of objects. Each object is a next layer down in the chain.
There are a set of known properties, and everything else is ignored.
Names are given in the example for clarity, but are not used in the code.
Most values have defaults that will be used if not provided.
</p>
<p>The first container must have a fixed <tt>w</tt> and <tt>h</tt> value.
All other containers assume a flexible size if not given.</p>
<p>The <code>next</code> and <code>prev</code> objects...

JavaScript

// A text layout in 3 columns
let exampleLayout = [
  {name:"Root", w:600, h:400},
  {name:"Column", w:"31%", h:"100%", next:{rel:"TR", x:"3.3%"}, prev:{rel:"TL"}},
  {name:"Line", next:{rel:"BL", y:"0.5em"}},
  {name:"Word", next:{rel:"TR", x:"0.5em"}},
  {name:"Glyph", glue:true}
];

// Text to fill this layout
// We will process this into glyphs/breaks in the code.
// https://penelope.uchicago.edu/Thayer/E/Roman/Texts/Cicero/de_Finibus/1*.html   10/32
let paramark = "¶";
let exampleInput = "But I must explain to you how all this mistaken idea of denouncing pleasure and praising "+
"pain was born and I will give you a complete account of the system, and expound the actual teachings"+
" of the great explorer of the truth, the master-builder of human happiness. No one rejects, dislikes, or"+
" avoids pleasure itself, because it is pleasure, but because those who do not know how to pursue pleasure "+
"rationally encounter consequences that are extremely painful. Nor again is there anyone who loves or pursues "+
"or desires to obtain pain of itself, because it is pain, but because occasionally circumstances occur in "+
"which toil and pain can procure him some great pleasure.¶To take a trivial example, which of us ever "+
"undertakes laborious physical exercise, except to obtain some advantage from it? But who has any right to "+
"find fault with a man who chooses to enjoy a pleasure that has no annoying consequences, or one who avoids "+
"a pain that produces no resultant pleasure? ¶On the other hand, we denounce with righteous indignation "+
"and dislike men who are so beguiled and demoralized by the charms of pleasure of the moment, so blinded "+
"by desire, that they cannot foresee the pain and trouble that are bound to ensue; and equal blame belongs "+
"to those who fail in their duty through weakness of will, which is the same as saying through shrinking "+
"from toil and pain.¶These cases are perfectly simple and easy to...