







After completing the orchestration structure, I had to reconnect work finished in the layout editor to EPUB editing. The layout editor is a tool for placing text, images and shapes at exact coordinates within a paper-like trim size. The body the EPUB editor handles, on the other hand, keeps reflowing according to the reader's screen and font size. They are two editors making the same book, but the way they interpret space is nearly opposite.
Turning a fixed page into an image and dropping it in wasn't a solution. The look survives, but paragraphs, headings, reading order, responsive columns and media queries disappear. Conversely, if I throw away all the coordinates and string the text together, the hierarchy and spacing set up in the typesetting, the side-by-side structure and the visual rhythm are lost. What I needed wasn't to copy the appearance of a fixed layout but to re-estimate the hidden document structure that produced that appearance.
I defined the process of turning layout results into reflowable HTML and CSS as a single inverse problem. What is observed on a page is the final coordinates, but what EPUB needs is the semantic structure behind those coordinates. The converted result had to be something I could edit again in the EPUB editor, fixing paragraphs, changing styles and extending media queries. The task was to treat the fixed result not as an endpoint but to turn it back into the starting point of an editable flow.
Ordinary typesetting can be seen as a forward function \(L(S,E)=G\) that takes a semantic structure \(S\) and an environment \(E\) and produces a geometric result \(G\). Given the order and styles of paragraphs and the column structure, the typesetting engine breaks lines, builds pages and decides each element's coordinates. Going from fixed layout to reflow is the opposite: finding a possible \(S\) from an observed \(G\).
But \(L\) generally has no inverse. Different HTML structures and CSS can produce the same screen, and whether an element belongs to the document flow or merely overlaps as decoration isn't uniquely determined by coordinates alone. In other words, there are plenty of cases where \(L(S_1,E)=L(S_2,E)\) while \(S_1\neq S_2\). Restoring the original structure exactly from a single fixed screen is inherently ill-posed.
So instead of trying to guess one solution, I set up an objective function. Putting together an error \(d(L(S,E_0),G)\), which says how well a candidate structure \(S\) reproduces the original geometry, and a structural complexity \(\Omega(S)\), the restoration becomes \(S^*=\arg\min_S\{d(L(S,E_0),G)+\lambda\Omega(S)\}\). It is a regularization that prefers a structure that's a little simpler but flows stably across many screens over a complicated absolute placement that merely resembles the original.
Here \(\lambda\) is the balance between fidelity and generalization. If it is too small, the result is a coordinate replica that fits only one trim size, and if it is too large, all visual hierarchy flattens out. The actual conversion doesn't lean on one particular number; it strikes this balance by treating preservation of meaning and responsive stability as invariants. Only after acknowledging that the inverse solution isn't unique could I consistently choose a solution suited to EPUB.
Elements of a fixed layout sit on a 2D plane, but an EPUB body basically has a 1D order. Being higher on the page, being further left and being inside the same box are different relationships. If I simply sort by \(y\) and then by \(x\), the reading order breaks down easily as soon as there are multi-column layouts, captions, background boxes and overlapping elements.
If the element set is \(V\), I can first think of a directed graph \(D=(V,A)\) that has only the unambiguous precedence relations as edges. I put \(u\to v\) only where \(u\prec v\) is clear, and elements whose order can't be decided right away, such as those on the same row or in a containment relation, stay as a partial order until more context becomes available. The reading order becomes one of the several linear extensions this DAG allows.
A good linear extension doesn't merely minimize geometric travel. The semantic cost must also be small: body text following a heading, an image staying close to its caption, and content of one column not jumping into another. Viewing this as a cost function \(C(\pi)=\alpha C_{geo}+\beta C_{semantic}+\gamma C_{discontinuity}\), order recovery becomes the problem of finding, among possible permutations \(\pi\), one with a small total cost.
When the original DOM or editing history survives, it can be used as a strong prior, and when only geometric information remains, alignment and containment relations carry more weight. What matters is not pushing every document through the same single coordinate sort. I built the partial order from both the explicit meaning and the observed geometry, and made it converge on a linear flow without contradictions.
Projecting each element of a page onto the \(x\) and \(y\) axes gives two intervals, \(I_x=[x_0,x_1]\) and \(I_y=[y_0,y_1]\). Whether two elements share a row, share a column, or one encloses the other is more stably described by the relationships between these intervals than by a single point coordinate. Letters and images of different sizes can be compared in the same language through intersections and containment of intervals.
The degree of overlap between intervals can be seen by normalizing \(\mu(I_a\cap I_b)\) by the union or by the length of the smaller interval. What matters, though, is not a particular threshold but that the normalized relationship doesn't change under enlargement or reduction. Even when the same trim size is observed at a different magnification, the relative overlap and order of intervals are preserved. Using relationships instead of coordinate magnitudes keeps the conversion from depending on the screen scale.
Elements whose vertical intervals overlap strongly may form one horizontal structure, and elements whose horizontal intervals are similarly aligned may be the flow of one column. Expressing this as an interval graph, the vertices are elements, and an edge appears between elements that intersect along a given axis. Connected components, cliques and containment trees are different viewpoints describing row candidates, overlap groups and container candidates respectively.
I didn't settle meaning with the interval graph alone. An element that spans widely, like a decorative rule, intersects with almost everything, and a large background can turn the whole page into one connected component. So I used the interval relations not as the answer that decides structure but as geometric evidence for narrowing the candidate space. Thanks to this distinction, even complicated pages weren't merged into one excessively large group.
Coordinates in a fixed layout are tied to a specific page width and height. A value like \(x=120\) loses its meaning the moment the page changes, whereas \(x/W\) keeps describing where in the full width the element sits. In the reflow conversion, I had to preserve ratios of lengths rather than the lengths themselves. The moment coordinates become dimensionless numbers, the proportional relationships of the structure can be separated from the size of the trim.
From the viewpoint of the Buckingham \(\Pi\) theorem, variables that carry dimensions, such as width, height, font size and spacing, can be organized into independent dimensionless combinations. Element width \(w\) can be seen as \(w/W\), vertical gap \(g\) as \(g/f\) relative to the body font size \(f\), and border thickness \(b\) as \(b/\min(w,h)\) relative to the element size. Which quantity you divide by becomes the meaning of the choice of CSS unit.
Horizontal widths and column positions should respond to the container width, so ratio coordinates are natural for them, while paragraph spacing and inner padding should respond together to font enlargement, so typography-based coordinates are natural for those. Rather than converting every value into one relative unit, I chose a different reference depending on which scale group each value has to covary with.
The resulting transformation has to have scale equivariance. Even if the input trim size is enlarged \(k\) times, the dimensionless structure stays the same, and when the result is placed on another screen, it gets its dimensions back from that screen's reference quantities. I aimed for a structural representation close to \(T(kG)=T(G)\) and a render relation close to \(R(T(G),kE)=kR(T(G),E)\). I didn't erase the fixed coordinates; I preserved the proportional laws the coordinates were carrying.
The outer rectangle of a text box and the boundary of the letters the reader actually sees are different. Letters are placed relative to the baseline, with an ascender above and a descender below, and line-height includes leading outside the glyphs. This is why text placed at the geometric center of a box can still look visually sunk toward the bottom.
If the body font size is \(f\), the line spacing is \(\ell\) and the baseline position is \(b\), the visual center of a line is not simply \(\ell/2\). A correction term \(\alpha f\) reflecting the font's x-height, cap-height and ascender ratios is needed, and a rough model can be written like \(c_{opt}=b-\alpha f\). \(\alpha\) is an optical coefficient that describes how ornaments and letters align, not an arbitrary pixel adjustment.
The spacing between paragraphs can't be computed from just the difference between the end of one text box and the start of the next either. When there is unused space inside a box, or the last paragraph is only partly visible, using the outer boundary makes the reflow result show larger gaps than the real ones or sentences that were never visible. What's needed is the boundary of the line boxes and of the visual support actually occupied by the content.
Empty paragraphs likewise don't all mean the same thing just because the string length is zero. An intentional blank line, an empty node left over from editing, and content pushed out of its box and not visible are all different. Only by looking at the topology and the metrics of the typography together can I tell which whitespace to keep and which to remove. The rhythm of headings, body text and captions held up after conversion because I used the geometry of the text lines, rather than the boxes, as the reference.
In fixed layouts, it's natural for elements to overlap. A heading can sit on a background color box, a thin line can pass through the middle of text, and a caption can sit over an image. In reflow, I can neither carry every overlap over as absolute positioning nor unroll every element vertically. First I have to classify what the overlap means.
If the region an element occupies is seen as a subset of the plane \(A_i\subset\mathbb{R}^2\), then containment \(A_i\subset A_j\), intersection \(A_i\cap A_j\neq\varnothing\) and disjointness \(A_i\cap A_j=\varnothing\) are different topological relations. If a large region contains smaller regions and sits visually behind them, it may be a container, and if a thin region only partly intersects the text, it may be decoration. The relation can be the same while the role differs, so z-order and semantic type are both needed.
If every connected component of the overlap graph is turned into one absolute-positioned group, the responsive properties are lost. If every edge is removed, the original's compositional relationships collapse. So some edges have to be contracted into set containment, some left as decorative relationships independent of the flow, and the rest unrolled into ordering relations. It's a process of relabeling the edges of the graph as a different kind of relation, not of deleting them.
Thinking of this conversion as topology preservation makes it clear what must be kept even when the screen size changes. The exact intersection area may vary, but the relationship that a background contains its content, that a caption is adjacent to an image and that a decoration moves along with a particular heading must hold. By putting preservation of relationships ahead of preservation of coordinate values, I kept the original's visual grammar from collapsing in reflow.
The environment of a reflowable document can't be expressed by screen width alone. Gathering width \(w\), height \(h\), orientation \(o\), color preference \(c\), resolution \(\rho\) and the user-set text scale \(s\), the environment becomes a multidimensional state \(e=(w,h,o,c,\rho,s)\). The render result is a function \(R(e)\) defined on this environment space.
Mobile, tablet and desktop are less device names than subsets that partition the environment space. \(E=E_1\cup E_2\cup E_3\), and within each region the same layout constraints are active. What matters is not the number of the split points themselves but the point at which the existing constraint system can no longer satisfy readability and structure at the same time. A breakpoint is closer to a threshold where the active constraints change than to a design convention.
For example, if the minimum readable width of several columns is \(m_i\) and the gap between columns is \(g_j\), the condition for a horizontal arrangement to be possible can be written as \(w\geq\sum_i m_i+\sum_j g_j\). The moment the condition fails, a new topological state is needed, one that reduces the columns or switches to a vertical flow. The problem of continuously shrinking sizes and the problem of changing the structure itself have to be kept apart.
Within each region, sizes and gaps change continuously, while at region boundaries the relationships of rows and columns change discretely. So a responsive layout isn't one smooth function but a piecewise function that is smooth within each piece. I designed the state transitions so that reading order and semantic adjacency are preserved on both sides of a boundary, which let me extend a single screen of a fixed layout into a function valid across many environments.
Before being CSS syntax, a media query is a predicate over the set of environments. Each query \(q_i:E\to\{0,1\}\) decides in which environments a rule is active, with conjunction corresponding to intersection, disjunction to union and negation to complement. The structure that several queries form is a Boolean algebra over the environment space.
A stable responsive document needs its query regions to cover the entire target space, with no unintended gaps. Along with completeness, \(\bigcup_i E_i=E\), it must be decided which rule takes priority in the overlapping regions \(E_i\cap E_j\). If queries are mutually exclusive, interpretation is simple, but orthogonal axes, like color mode and screen width, have to be deliberately crossed.
Handling width intervals \(W_i\) and color states \(C_j\) together, the environment divides into the product set \(W_i\times C_j\). Mobile light, mobile dark, wide-screen light and dark aren't separate files but cells of the same state function. If rules common along one axis are grouped under a higher-level term and only the differences needed in the crossing cells are left, the number of rules shrinks while every combination can still be expressed.
Media query conversion wasn't about inventing arbitrary rules from a single observed trim size. It was about decomposing which relationships are invariant in every environment, which change only in certain width ranges, and which properties depend only on the color axis. Once the domain of the environment predicates was clear, I could detect conflicting rules and unreachable conditions, and produce predictable responsive results across many readers.
When moving the columns of a fixed layout into CSS flow, what matters is the overall constraint more than the individual widths. If \(n\) columns make up a row, the ratios \(p_i\) lie on the simplex with \(p_i\geq0\) and \(\sum_{i=1}^{n}p_i=1\). If there are gaps and margins, I have to first set aside the share they take up, and then normalize the column ratios over the remaining area.
Not every column must have a fixed ratio, though. Content that shouldn't shrink, like heading text, has its own min-content constraint, and elements that fill leftover space, like a decorative rule, become elastic free variables. The layout then becomes a linear constraint system that satisfies \(\sum_i w_i+g=W\) while respecting the lower and upper bounds of each \(w_i\). Rather than copying fixed values, I have to recover which variables are fixed and which absorb the slack.
The CSS cascade is also an order structure. When several declarations reach the same property, the winner is decided by the lexicographic order of origin, importance, specificity and source order. In media query conversion, what mattered more than creating many rules was arranging common rules and per-environment rules so they don't conflict within this order. Even if two constraints are active on the same screen at once, the result must not hinge on the accident of authoring order.
The converted CSS became a description of a constraint system rather than a record listing the original numbers. On a wide screen the columns share a row, when the width runs short it moves to a different arrangement state while keeping the semantic order, and when the font size changes the margins follow the typographic ratios. In effect I turned the coordinates of one trim size into equations that can be solved again in many environments.
Not every visual element can be converted the same way. Text is better kept as semantic structure for search, accessibility and re-editability, and simple geometric shapes can be expressed with the constraints CSS has. Complex curves and compositing effects, on the other hand, may drift further from the original the harder I try to infer a semantic structure for them. Choosing the conversion method was itself an optimization problem.
The cost of a candidate representation \(r\) can be written as \(J(r)=\alpha D_{visual}(r)+\beta D_{semantic}(r)+\gamma C(r)\). \(D_{visual}\) is the visual distortion from the original, \(D_{semantic}\) is the cost of losing textuality and structure, and \(C\) is the complexity of running it in many readers. Minimizing only one term pushes toward extremes, such as a screenshot or an overly complicated DOM.
It lowers the total cost to prefer semantic representation for content that contributes to reflow, and to keep complex visual elements with no structural benefit as stable visual representations. What matters isn't publishing the internal criteria for this division but that, after conversion, both the shapes the reader should see and the meaning the editor should work with are each preserved. Even if the means of expression differ, the role in the document has to be maintained.
Values that directly affect the visual result, such as opacity, aspect ratio, rotation and clipping, were also examined within the same distortion function. When small errors accumulate, it's no longer a difference in one element but a difference in the rhythm of the whole page. I didn't take visual fidelity lightly in the name of preserving semantic structure, and I didn't turn everything into a flat image in the name of protecting visual fidelity either.
The result of the conversion isn't a finished screen but a reflowable document in which the HTML structure, the CSS constraints and the necessary resources are closed. HTML expresses reading order and containment, and CSS expresses the geometric constraints that get solved depending on the environment. The resources form the reachability closure referenced by these two structures. With all three together, the same document meaning can be reconstructed in another environment.
Once this document returns to the EPUB editor, it had to have the same editability as ordinary body text. Paragraphs can be fixed again, heading and body styles can be changed, and the column structure and media queries can be extended within the rules of the whole EPUB. I didn't insert the layout editor's result as a closed output; I made it an intermediate representation that the next editing stage can read.
In compiler terms, it's like placing a common IR between the layout document and the EPUB body. It doesn't expose the original editor's internal representation as is, and passes along only the meaning and constraints the downstream editor needs. If this boundary is stable, the semantic contract of the converted document holds even when the internal structures of the two editors change.
I also verified against the same semantic state, so that the preview and the actual delivered result don't go through different calculations. The paragraph order, column arrangement, image ratios and responsive changes that the user confirmed on the reflow screen have to stay the same after they carry into EPUB editing. I organized things so that the result being observed and the result being edited come from one intermediate representation.
Reviewing the conversion didn't end with eyeballing a few example pages. I first checked properties such as whether every paragraph enters the reading flow exactly once, whether the reading-order graph has no cycles, whether the sum of the column ratios stays within the available area, and whether resource references lie inside the closure. Before looking at the shape of the result, I checked the structural invariants.
In the responsive review, I sampled the environment space at many points. I checked not only the inside of each width range but both sides of the boundaries where the active constraints change, and crossed orientation, color state and text scale. I also verified that changing one axis preserves the state of the other independent axes, and that overlapping media predicates are evaluated with the intended priority.
For the typography and geometry review, I repeatedly converted headings and body text, multi-column structures, images and captions, background containers, decorative elements and complex visual material. I confirmed that text isn't clipped when the screen narrows, that the semantic order holds when columns change, and that the relationship between spacing and decoration doesn't collapse when the text size is raised. Even after re-editing a sentence in the EPUB editor, the reflow structure and media queries were recalculated correctly.
The whole conversion, which recovers the 2D coordinates of a fixed layout into a readable partial order, turns lengths into dimensionless constraints and extends the environment space into the Boolean algebra of media queries, was fully implemented and stabilized. A book made in the layout editor no longer stays on fixed pages; as HTML and CSS that preserve its structure and visual intent, it flows precisely into the next editing stage in the EPUB editor.
