







Once I had built the vector editor and the Rust graphics engine behind it, the next tool I needed was naturally settled. I could now make the images and shapes that go into a book myself, but placing them on actual pages was an entirely different kind of editing. If a graphics editor handles objects on a single plane, a layout editor has to understand multiple pages and spreads, bleed, margins, master pages and print specifications all at once.
From the start, I did not think of this editor as a vector canvas with page borders added. In a real book, an object with the same coordinates becomes a completely different thing depending on which page it belongs to, and left and right pages have different margins relative to the spine. A photo must be able to span the whole spread across the spine, and common headers and page numbers must be managed in a master rather than on individual pages.
That said, I had no intention of simply replicating the rules of the paper book either. Opkle's layout would later have to go back out as EPUB and the web, so it had to hold free coordinates and flow-based structure together. First, I decided to build the page space, the floor beneath all this work, exactly right. In order, I worked out how to describe the ownership of a scene, which physical unit to compute in, and whether an action on screen remains a document coordinate independent of zoom.
The center of a vector editor is the object. Rectangles, ellipses, paths, text, images and groups move on one coordinate system, and selection and transforms follow those objects. In a layout editor, one more ownership region, the page, rises above this scene graph. Even the same shape has a different local origin, clip and template context depending on which page it is a child of. A page is not a border on screen but a scene node that defines coordinates and ownership together.
In designing the document model, I first separated semantic state from display state. Values that make up the document's meaning, such as trim size, page order and styles, must be independent of zoom or the current selection. Conversely, pan, selection boxes, resize handles and hover indicators are view state, a brief way of looking at the same document. If the two are mixed, you get problems like the saved result changing because the screen was zoomed, or guides ending up in the printout. The separation of Model and View went beyond a UI pattern and became a device for protecting document invariants.
I treated order and identity as different concepts as well. An array index is good for fast traversal, but the moment an item is inserted in front, everything behind gets a different number. A stable identifier keeps pointing at the same object even when its position changes, and order is expressed as a separate relationship. This principle is the same as separating a primary key from a sort column in a database, and in a scene graph it is the basis for keeping selection, history and referential integrity intact.
I defined editing operations to target a single local coordinate space wherever possible. Translation is vector addition, and rotation and scaling are affine transforms, and these operations themselves need not know whether the target is on the first page or the hundredth. If you can move between local and world through the parent page transform, the same geometric operations can be reused in every ownership region. What scaled up the multi-page editor was not duplicating features but making the coordinate context explicit.
In a layout editor, I had to sort out the coordinate units again from scratch. The CSS pixel familiar on screen, the point used in print specifications, and the millimeter a user types when entering trim size and margins are not the same number. I decided to keep document specifications and output coordinates in the point family, and to convert trim size and margin inputs shown to users into millimeters. Since PDF and IDML work mainly in point space, it was more accurate to align the internal standard with that.
I set the canonical unit of conversion to the inch. `1 inch = 25.4 mm`, `1 inch = 72 pt`, and the CSS pixel is converted on the basis of 96 DPI by default. Any length is first normalized to an inch ratio and then returned in the target unit. Instead of scattering different conversion constants among mm and pt, and px and pt, I made them share a single unit path, so the input fields, document defaults, presets, SVG coordinates and PDF output all use the same values.
I did not store A4 as a rounded value like `595 × 842` either. I used the exact point conversion of `210 × 297 mm` as the default, so that displaying it again in mm brings back a clean 210 and 297. A small rounding difference is not noticeable on one page, but it can accumulate when page boxes, bleed and print marks are repeatedly computed. Trim size presets, new documents, reset and the fallback for past projects were all aligned to use the same reference values.
When print resolution is needed, I separated document coordinates from raster coordinates again. First you find the physical length that 1 unit on the page represents, then decide the pixel count from the output DPI. The zoom of the editing screen is only the magnification the user sees and does not affect the actual size of the document. Since I fixed this principle, screen zoom, PDF output and high-resolution image output no longer produce different sizes.
With units sorted out, I separated the coordinate spaces next. The value a mouse event gives is in the browser's client coordinates, and the SVG stage uses viewport coordinates with pan and zoom applied. Actual objects are stored in page local coordinates, which take each page's top left as `(0, 0)`. The same point has different numbers on screen, on the canvas and on the page.
Subtract the stage's translation from the screen point `p_screen` and divide by zoom to get the world coordinate. Subtract the translation of the target page group from that and you get page local coordinates. In a formula, `p_local = (p_screen - t_view) / zoom - t_page`. The master editing space uses a separate page translation, so I chose the last transform matrix depending on the current mode and the page being edited.
I applied this transform to every insertion path, beyond simple pointer computation. When a user drops an image onto another page, I first compute the intersection of the screen point with each page rect to find the target page, activate that page, and then convert to a local point. When moving an object to the other side of a spread, too, I lift the starting page's coordinates to root coordinates and then bring them down to the destination page's local coordinates. If you just add the x difference directly in between, the position drifts the moment zoom or the spread gap changes.
Selection regions and resize handles followed the same principle. The object geometry stays in page local coordinates, and only the selection overlay is drawn on the root layer. So even if you select several objects together or move to a page with a different page translation, the actual stored coordinates stay uncontaminated. Separating the coordinates that look right on screen from the coordinates that are stored right in the document became the reference point for multi-page editing.
For page placement, I computed single pages and spreads with different rules. With single pages you just line up the pages vertically, but in a spread the first page sits alone on the right, and from the next page on, left and right pages form pairs. I decide each side by page parity and move to the next spread row each time a pair is complete. When adding or deleting pages changes the parity, the margins and masters of the following pages are recomputed.
With bleed, you cannot simply space pages apart by the page width. The two bleed areas on the spine side might look overlapped, or an unnecessary gap might appear, so the spread gap accounts for both sides' bleed and the actual working gap. The page background, bleed guides, margin guides and trim boundaries are separated into dedicated layers below the body objects. This arrangement ensures that, even when the active page is raised to the front, its white background does not cover the spread image of the neighboring page.
I made the clip asymmetric as well, for objects that cross the spine. If every page gets a clip of its own width, a photo that starts on the left page is cut off the moment it passes over to the right. For facing pages, I extended the clip region only in the spine direction to the neighboring page, while the outer sides and top and bottom keep being clipped by the page box. The object keeps its membership in one page, yet in the actual spread it looks naturally continuous.
Margins carry the meaning of inside and outside before left and right. On a right-hand odd page, inside is physically the left, and on a left-hand even page, inside is the right. When applying document settings, I convert logical margins into physical margins matching the page parity and reflect them in the body guides and automatic text frames. Asymmetric binding margins and left/right masters flip correctly according to page order.
In layout work, the feel of moving things by hand to exact numbers mattered. When you drag an object, it should snap naturally to the page center and edges, margin lines, and the centers and edges of other objects. Rects, ellipses, images, foreignObjects, paths and groups store geometry differently, so I first normalized the actual bounds into a common form `(x, y, w, h)`.
I made the left, center and right and the top, middle and bottom of the selection into snap axes, and collected the same axes of the page and surrounding objects as candidates. I compared the delta to each candidate and applied to the movement only the nearest value within the threshold. So that the snap distance felt on screen does not vary too much as zoom grows, the threshold is converted between document coordinates and screen coordinates according to the current magnification.
With a single threshold, an object would keep snapping and unsnapping at the boundary. So I remember the mouse position where the snap began and the delta applied, and added hysteresis with a release threshold wider than the entry threshold. The earlier decision is kept until the user clearly leaves the snap axis, so the object does not jitter even when moved slowly. The x and y axes can be locked independently, so you can keep one-sided alignment while continuing to move in the other direction.
Equal-spacing snap is computed as a separate candidate group. I compare the gaps `g_left` and `g_right` between the selected object and its left and right neighbors, and find the movement that makes the two equal. For the vertical axis I compute the gaps above and below in the same way. On an axis where regular alignment already applies, I gave priority so that the equal-spacing judgment does not intervene, and the actual gap is shown with dimension lines and number badges. The guides are computed in page local space but drawn on the root overlay so that they follow accurately even during pan and zoom.
For the ruler too, I derive the world range the current viewport is looking at, in reverse, to build the ticks. Major and minor tick spacing changes with zoom, and ticks outside the screen are not generated. Re-reading the stage's layout values at every pointermove incurs the cost of forced reflow, so I cached the bounding rect and transform values and invalidated them only on pan, zoom and resize. The smoothness of manipulation was, in the end, the result of managing coordinate computation and browser layout cost together.
Objects within a page do not all sit at the same depth. A group that bundles several elements must move as a single transform target, but when needed you should be able to select and edit the inner elements again. In normal selection mode a group is treated as a single object, and double-clicking enters an isolation state that edits only that group.
In the isolated state, I limited the root of selection traversal to the current group rather than the whole page layer. Even if elements outside the group overlap under the mouse, they are not promoted to selection candidates, and only the group's direct children are selected. When you create a new shape or text, it is inserted into the isolated group rather than the current page. Tab traversal and marquee selection use the same root, so the isolation on screen matches the actual DOM ownership.
The current editing scope is shown with a dashed outline, but since this overlay is not document data, I separated it as a transient element. It is excluded before the save string and history snapshot are made, and when zoom changes, the stroke and dash spacing are readjusted. Clicking the outer background or switching pages ends isolation and selects the group itself again. Even if the group is deleted or detached from the DOM during editing, the state is cleaned up automatically so that no dead references remain in the selection.
I placed the page background and guides, the master DOM, individual objects and the selection overlay in separate layers for the same reason. Distinguishing layers that are saved from layers that exist only on screen prevented resize handles from lingering in undo or guides from mixing into the printout. A layout editor's page was completed not as a single SVG sheet but as a combination of several layers that distinguish ownership and editing lifetime.
You cannot manage elements that repeat across a whole book by copying them onto every page. Headers and footers, ornamental rules, backgrounds and page numbers are a template problem. If an ordinary page owns the template's result as actual children, fixing one common element means editing every page again, and it becomes unclear which value is the original. So I viewed repeated elements as a prototype relationship that distinguishes a definition from its instances.
A template is closer to a function that takes a context than to a fixed picture. Values like the page number, the total page count and left or right side become actual display values only when `T(context)` is evaluated. The same definition can produce a different result for each page, and when page order changes, you do not fix the original; you just re-evaluate in the new context. It is as if the strengths of declarative UI and functional templates were applied directly to publishing pages.
Inheritance needed priorities. Template values sit on top of document defaults, and a property that an individual page explicitly changed wins in the nearer scope. If you copy every property unconditionally, you can no longer tell overrides from the original when the original changes, so it is better for a page to remember only the values it actually overrode. Like the CSS cascade, knowing the origin and specificity is what lets repeated elements and individual edits coexist.
I separated the editing scope logically as well. When editing an ordinary page, the template's result is visible but cannot be selected directly, and the common definition can be edited only after entering the template context. Being drawn on the same screen does not make things objects of the same lifetime. By distinguishing render order, selectability and storage ownership separately, I kept the repeated structure from mixing with the body objects.
The first milestone of the layout editor was not the number of tools. What came first was that semantic state and view state are separated, that an object's identity holds even when the order changes, and that physical units round-trip exactly regardless of screen magnification. Only when the transforms among local, world and screen coordinates can be explained within a single matrix system can the later text typesetting and document conversion work trusting the same positions.
On top of that, I built spreads where the first page sits on the right, asymmetric clips toward the spine, mirrored inside and outside margins, and the containment relationships of bleed and page boxes. I normalized different shapes into a common bounds model to make alignment and equal-spacing snap, and group isolation and transient overlays made it safe to edit even complex scene graphs. Repeated elements were separated from the body with template functions and inheritance priority.
I verified the coordinate invariants by repeating page reordering, switching between single pages and spreads, asymmetric margins, different bleeds, high and low magnification, moving objects between pages, editing nested groups and applying templates. I confirmed that a position that was right on screen is restored to the same physical coordinates when reopened and also matches the output page box. The page engine that can assemble vector assets onto the surface of a real book was stabilized to a polished state at this stage.
