-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLayoutSystem.java
More file actions
474 lines (405 loc) · 19.6 KB
/
LayoutSystem.java
File metadata and controls
474 lines (405 loc) · 19.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
package com.demcha.compose.engine.layout;
import com.demcha.compose.engine.components.content.text.LineTextData;
import com.demcha.compose.engine.core.Canvas;
import com.demcha.compose.engine.components.content.text.BlockTextData;
import com.demcha.compose.engine.components.content.text.TextStyle;
import com.demcha.compose.engine.components.core.Entity;
import com.demcha.compose.engine.components.core.EntityName;
import com.demcha.compose.engine.components.geometry.ContentSize;
import com.demcha.compose.engine.components.geometry.InnerBoxSize;
import com.demcha.compose.engine.components.geometry.OuterBoxSize;
import com.demcha.compose.engine.components.layout.Align;
import com.demcha.compose.engine.components.layout.Anchor;
import com.demcha.compose.engine.components.layout.Layer;
import com.demcha.compose.engine.components.layout.ParentComponent;
import com.demcha.compose.engine.components.layout.coordinator.ComputedPosition;
import com.demcha.compose.engine.components.layout.coordinator.PaddingCoordinate;
import com.demcha.compose.engine.components.layout.coordinator.Position;
import com.demcha.compose.engine.components.renderable.BlockText;
import com.demcha.compose.engine.components.renderable.TextComponent;
import com.demcha.compose.engine.components.style.Padding;
import com.demcha.compose.engine.core.EntityManager;
import com.demcha.compose.engine.core.LayoutTraversalContext;
import com.demcha.compose.engine.exceptions.BigSizeElementException;
import com.demcha.compose.engine.render.RenderingSystemECS;
import com.demcha.compose.engine.core.SystemECS;
import com.demcha.compose.engine.measurement.TextMeasurementSystem;
import com.demcha.compose.engine.layout.container.ContainerExpander;
import com.demcha.compose.engine.layout.container.ContainerLayoutManager;
import com.demcha.compose.engine.layout.container.ModuleWidthResolver;
import com.demcha.compose.engine.pagination.PageBreaker;
import com.demcha.compose.engine.pagination.PaginationLayoutSystem;
import com.demcha.compose.engine.pagination.TextBlockProcessor;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.util.*;
import java.util.concurrent.TimeUnit;
/**
* Main layout pass for GraphCompose entities.
* <p>
* This system turns builder-produced entities into resolved geometry. It walks
* the entity hierarchy, computes container relationships, expands parent boxes
* when needed, resolves per-entity positions, and finally delegates to the
* pagination stage so entities receive final {@code Placement} metadata.
* </p>
*
* <p>Conceptually this is the "builder intent to geometry" phase of the engine.</p>
*
* <p>Box model summary:</p>
* <ul>
* <li>{@code ContentSize}: declared content dimensions</li>
* <li>{@code InnerBoxSize}: available child area after padding</li>
* <li>{@code OuterBoxSize}: content plus padding and margin</li>
* <li>{@code Placement}: final bounding box and page span</li>
* </ul>
*/
@Slf4j
@RequiredArgsConstructor
public class LayoutSystem<T extends RenderingSystemECS<?>> implements SystemECS {
private static final Logger LIFECYCLE_LOG = LoggerFactory.getLogger("com.demcha.compose.engine.layout");
private final Canvas canvas;
@Getter
private final T renderingSystem;
private TextBlockProcessor textBlockProcessor;
/**
* Resolves an entity position from its parent context, walking the ancestor
* chain when intermediate computed positions are still missing.
*
* @param childEntity the entity to position
* @param parentEntity the parent entity, or {@code null} for a root entity
* @param entityManager entity registry providing parent lookups
* @return the resolved position when computation succeeds
*/
private Optional<ComputedPosition> calculatePositionFromParent(Entity childEntity,
Entity parentEntity,
EntityManager entityManager) {
log.debug("Starting calculation of computed position for {} from parentEntity {}", childEntity, parentEntity);
// 0) Handle page-level (no parent)
if (parentEntity == null) {
InnerBoxSize pageArea = new InnerBoxSize(this.canvas.width(), this.canvas.height());
PaddingCoordinate paddingPercentCoordinate = new PaddingCoordinate(this.canvas.x(), this.canvas.y());
ComputedPosition local = positionWithAnchor(childEntity, pageArea, paddingPercentCoordinate);
log.debug("Final computed absolute position (page-level): {}", local);
return Optional.of(local);
}
// 1) Build ancestor chain: [root ... parent]
List<Entity> chain = new ArrayList<>();
Set<UUID> seen = new HashSet<>();
Entity cur = parentEntity;
while (cur != null) {
if (!seen.add(cur.getUuid())) {
log.error("Cycle detected in ParentComponent chain at entity {}", cur);
throw new IllegalStateException("Cycle detected in parent chain");
}
chain.add(cur);
var parentCompOpt = cur.getComponent(ParentComponent.class);
if (parentCompOpt.isEmpty()) {
break; // cur is root
}
UUID gpId = parentCompOpt.get().uuid();
cur = entityManager.getEntity(gpId).orElse(null);
}
int depth = chain.size(); // глубина
// 2) Ensure each ancestor has InnerBoxSize and ComputedPosition
for (int i = 0; i < depth; i++) {
Entity e = chain.get(i); // root -> ... -> parent
var inner = InnerBoxSize.from(e).orElseThrow();
if (e.getComponent(ComputedPosition.class).isEmpty()) {
var parentComp = e.getComponent(ParentComponent.class);
if (parentComp.isPresent()) {
Entity p = entityManager.getEntity(parentComp.get().uuid())
.orElseThrow(() -> new IllegalStateException("Parent not found for " + e));
var parentInner = InnerBoxSize.from(p).orElseThrow();
var pagingCoordinate = PaddingCoordinate.from(parentEntity);
ComputedPosition local = positionWithAnchor(e, parentInner, pagingCoordinate);
ComputedPosition parentAbs = p.getComponent(ComputedPosition.class)
.orElse(new ComputedPosition(0, 0));
e.addComponent(new ComputedPosition(local.x() + parentAbs.x(),
local.y() + parentAbs.y()));
} else {
InnerBoxSize refArea = new InnerBoxSize(this.canvas.width(), this.canvas.height());
var pagingCoordinate = PaddingCoordinate.from(parentEntity);
ComputedPosition local = positionWithAnchor(e, refArea, pagingCoordinate);
e.addComponent(local);
}
}
}
// 3) Compute child's final position
InnerBoxSize parentInner = InnerBoxSize.from(parentEntity).orElseThrow();
var pagingCoordinate = PaddingCoordinate.from(parentEntity);
ComputedPosition childLocal = positionWithAnchor(childEntity, parentInner, pagingCoordinate);
ComputedPosition parentAbs = parentEntity.getComponent(ComputedPosition.class)
.orElse(new ComputedPosition(0, 0));
ComputedPosition childAbs = new ComputedPosition(
childLocal.x() + parentAbs.x(),
childLocal.y() + parentAbs.y()
);
// Final summary log
log.debug("Position calculation summary: nodesTraversed={}, depth={}, finalPosition={}",
chain.size(), depth, childAbs);
return Optional.of(childAbs);
}
/**
* Computes a child's anchored position inside the supplied parent area and
* stores the result back on the entity.
*
* @param child entity being positioned
* @param parentInnerBoxSize available parent area
* @param paddingCoordinate parent padding origin
* @return the computed position, also attached to the entity
*/
private ComputedPosition positionWithAnchor(Entity child, InnerBoxSize parentInnerBoxSize, PaddingCoordinate paddingCoordinate) {
var computed = ComputedPosition.from(child, parentInnerBoxSize, paddingCoordinate);
child.addComponent(computed);
log.debug("Computed position with Anchor has been created: {}", computed);
log.debug("{} has been created in: {}", computed, child);
return computed;
}
@Override
/**
* Runs the full layout pass for the current entity graph.
*
* <p>
* The pass resets traversal metadata, materializes one canonical hierarchy
* snapshot, resolves module/container sizing helpers, computes per-entity
* positions and depth/layer metadata, and finally delegates to pagination so
* entities receive final {@code Placement} components.
* </p>
*
* @param entityManager entity registry for the current document
*/
public void process(EntityManager entityManager) {
long startNanos = System.nanoTime();
LIFECYCLE_LOG.debug("layout.system.start entities={}", entityManager.getEntities().size());
log.info("LayoutSystem: processing...");
textBlockProcessor = new TextBlockProcessor(entityManager);
LayoutTraversalContext.resetTraversalState(entityManager);
final var entities = entityManager.getEntities();
if (entities == null || entities.isEmpty()) {
log.info("LayoutSystem: no entities to lay out");
LIFECYCLE_LOG.debug("layout.system.end entities=0 durationMs={}", TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startNanos));
return;
}
// Build one canonical hierarchy snapshot for the whole pass so layout,
// pagination, and future backends all reason over the same sibling order.
final LayoutTraversalContext traversalContext = LayoutTraversalContext.from(entityManager);
final Map<UUID, UUID> parentById = traversalContext.parentById();
final Map<UUID, List<UUID>> childrenByParent = traversalContext.childrenByParent();
//TODO currently en testing stage
ModuleWidthResolver.process(entityManager, canvas);
ContainerLayoutManager.process(childrenByParent, entityManager);
// 3) Expand parent boxes if needed (any child larger than parent)
ContainerExpander.process(childrenByParent, entityManager);
// 4) Compute roots
final Set<UUID> roots = traversalContext.roots();
// 5) DFS with cycle detection
final Map<UUID, Visit> visit = new HashMap<>(entities.size());
entities.keySet().forEach(id -> visit.put(id, Visit.UNSEEN));
final var layers = entityManager.getLayers(); // layer → ordered ids (assumed provided)
final var depthById = entityManager.getDepthById();
for (UUID root : roots) {
dfsLayout(
root,
null,
entities,
childrenByParent,
visit,
layers,
depthById,
1
, entityManager
);
}
log.info("LayoutSystem: layout complete (nodes: {})", entities.size());
// Pagination
boolean withPagination = true;
if (withPagination) {
var pageBreaker = new PageBreaker(entityManager);
pageBreaker.process(entities, renderingSystem.canvas(), traversalContext, depthById);
} else {
PaginationLayoutSystem paginationLayoutSystem = new PaginationLayoutSystem();
paginationLayoutSystem.process(entityManager);
}
LIFECYCLE_LOG.debug(
"layout.system.end entities={} roots={} layers={} durationMs={}",
entities.size(),
roots.size(),
layers.size(),
TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startNanos));
}
/**
* Performs the recursive layout traversal for one hierarchy branch.
*
* <p>Parents are processed before children so child layout can depend on
* parent inner size, padding coordinates, and container ordering metadata.</p>
*/
private void dfsLayout(
UUID id,
UUID parentId,
Map<UUID, Entity> entities,
Map<UUID, List<UUID>> childrenByParent,
Map<UUID, Visit> visit,
Map<Integer, List<UUID>> layers, // NEW: layer → ordered ids
Map<UUID, Integer> depthById, // NEW: id → depth
int depth, // NEW: current depth
EntityManager entityManager
) {
Visit st = visit.getOrDefault(id, Visit.UNSEEN);
if (st == Visit.DONE) return;
if (st == Visit.ACTIVE) throw new IllegalStateException("LayoutSystem: cycle detected at entity " + id);
visit.put(id, Visit.ACTIVE);
Entity childEntity = entities.get(id);
if (childEntity == null) {
log.warn("LayoutSystem: entity {} not found — skipping", id);
visit.put(id, Visit.DONE);
return;
}
// --- LAYERS COLLECTION ---
layers.computeIfAbsent(depth, k -> new ArrayList<>()).add(id);
depthById.put(id, depth);
// --- POSITION CALC ---
ComputedPosition computedPosition;
if (parentId != null) {
Entity parent = entities.get(parentId);
if (parent != null) {
computedPosition = ComputedPosition.from(childEntity, parent);
} else {
log.warn("LayoutSystem: parent {} of {} not found — using root positioning (Position + Margin)", parentId, id);
computedPosition = ComputedPosition.from(childEntity, this.canvas);
}
} else {
computedPosition = ComputedPosition.from(childEntity, canvas);
}
// IMPORTANT: store the computed position, not (0,0)
childEntity.addComponent(computedPosition);
childEntity.addComponent(new Layer(depth));
if (childEntity.has(Align.class)) {
alignRearrangeBlockText(childEntity, entityManager);
}
if (log.isDebugEnabled()) {
String name = childEntity.getComponent(EntityName.class)
.map(EntityName::value)
.orElse(id.toString());
log.debug("LayoutSystem: {} [depth={}] positioned at ({}, {})",
name, depth, computedPosition.x(), computedPosition.y());
}
if (BlockText.class.isAssignableFrom(childEntity.getRender().getClass())) {
try {
textBlockProcessor.processLayoutSystemTextLines(childEntity);
} catch (IOException e) {
log.error("Error during processing block text entity {}", childEntity);
throw new RuntimeException(String.format("Error during processing block text entity %s", childEntity), e);
} catch (BigSizeElementException e) {
log.error(String.format("To big size line in block text, Error during processing block text entity %s", childEntity), e);
throw new RuntimeException(e);
}
}
// DFS to children with depth+1
for (UUID childId : childrenByParent.getOrDefault(id, Collections.emptyList())) {
dfsLayout(childId, id, entities, childrenByParent, visit, layers, depthById, depth + 1, entityManager);
}
visit.put(id, Visit.DONE);
}
/**
* Applies horizontal alignment offsets to block-text line fragments inside the
* entity's own content box.
*
* <p>
* This is a post-measurement adjustment step. It does not remeasure the full
* block unless a line payload has no cached width information.
* </p>
*
* @param blockTextBox block-text entity to realign
* @param entityManager entity registry used to resolve the text measurement
* system on demand
* @return the same entity instance after line coordinates are updated
*/
private Entity alignBlockText(Entity blockTextBox, EntityManager entityManager) {
Align align = blockTextBox
.getComponent(Align.class).orElse(Align.defaultAlign(2));
var component = blockTextBox.getComponent(BlockTextData.class).orElseThrow();
var size = blockTextBox.getComponent(ContentSize.class).orElseThrow();
var padding = blockTextBox.getComponent(Padding.class).orElse(Padding.zero());
TextStyle style = blockTextBox.getComponent(TextStyle.class).orElseThrow();
TextMeasurementSystem measurementSystem = null;
var lines = component.lines();
for (LineTextData line : lines) {
// Ordinary block-text lines already carry builder-time width caches.
// The fallback is only for manually assembled/un-cached payloads.
double lineWidth = line.lineWidth();
if (Double.isNaN(lineWidth)) {
if (measurementSystem == null) {
measurementSystem = entityManager.getSystems()
.getSystem(TextMeasurementSystem.class)
.orElseThrow(() -> new IllegalStateException("TextMeasurementSystem is required to align block text."));
}
lineWidth = line.width(measurementSystem, style);
}
switch (align.h()) {
case LEFT -> {
double x = line.x() + padding.left();
line.x(x);
}
case RIGHT -> {
double x = size.width() - lineWidth - padding.right();
line.x(x);
}
case CENTER -> {
double x = (size.width() - lineWidth + padding.left()) / 2;
line.x(x);
}
}
}
return blockTextBox;
}
/**
* Aligns block-text payloads when the entity actually carries block-text line
* data.
*
* @param entity entity to inspect
* @param entityManager entity registry used to resolve layout helpers
* @return the aligned entity wrapped in {@link Optional}, or empty when the
* entity is not a block-text payload holder
*/
public Optional<Entity> alignRearrangeBlockText(Entity entity, EntityManager entityManager) {
if (entity.has(BlockTextData.class)) {
return Optional.of(alignBlockText(entity, entityManager));
} else {
log.debug("Entity is not a BlockTextData");
return Optional.empty();
}
}
/**
* Returns whether the given entity must NOT be expanded.
*
* <p>Rules:
* <ul>
* <li>Has {@code TextComponent} → return {@code true} (do not expand)</li>
* <li>Has {@code Box} → return {@code false} (allow expansion)</li>
* <li>Otherwise → return {@code true} (do not expand)</li>
* </ul>
*
* @param entity the entity to check
* @return {@code true} to skip expansion; {@code false} to allow it
*/
@Deprecated
private boolean isExpandable(Entity entity) {
if (entity.has(TextComponent.class)) {
// Forbidden expend text Entitie
return false;
}
return true;
}
@Override
public String toString() {
return "LayoutSystem";
}
/**
* DFS visit states used during one layout traversal.
*/
public enum Visit {UNSEEN, ACTIVE, DONE;}
}