|
| 1 | +package com.demcha.compose.document.node; |
| 2 | + |
| 3 | +import com.demcha.compose.document.style.DocumentColor; |
| 4 | +import com.demcha.compose.document.style.DocumentInsets; |
| 5 | +import com.demcha.compose.document.style.DocumentPathSegment; |
| 6 | +import com.demcha.compose.document.style.DocumentStroke; |
| 7 | + |
| 8 | +import java.util.List; |
| 9 | +import java.util.Objects; |
| 10 | + |
| 11 | +/** |
| 12 | + * Atomic filled/stroked vector path inside a fixed-size box. Segments are |
| 13 | + * normalized {@link DocumentPathSegment}s scaled to the node's |
| 14 | + * {@code width × height} at render time — the open-path, curve-capable |
| 15 | + * sibling of {@link PolygonNode}. |
| 16 | + * |
| 17 | + * <p>This is the leaf vehicle for arbitrary vector geometry with real cubic |
| 18 | + * Bézier curves: smooth chart lines compile into it, decorative design |
| 19 | + * shapes can be authored against it, and imported SVG paths land here |
| 20 | + * tomorrow. The PDF backend emits native {@code curveTo} operators, so |
| 21 | + * curves stay perfectly smooth at any zoom level instead of being |
| 22 | + * tessellated into straight pieces.</p> |
| 23 | + * |
| 24 | + * @param name node name used in snapshots and layout graph paths |
| 25 | + * @param width resolved box width |
| 26 | + * @param height resolved box height |
| 27 | + * @param segments normalized path segments; must start with a |
| 28 | + * {@link DocumentPathSegment.MoveTo} |
| 29 | + * @param fillColor optional fill colour (non-zero winding rule) |
| 30 | + * @param stroke optional outline stroke |
| 31 | + * @param padding inner padding |
| 32 | + * @param margin outer margin |
| 33 | + * @author Artem Demchyshyn |
| 34 | + * @since 1.8.0 |
| 35 | + */ |
| 36 | +public record PathNode( |
| 37 | + String name, |
| 38 | + double width, |
| 39 | + double height, |
| 40 | + List<DocumentPathSegment> segments, |
| 41 | + DocumentColor fillColor, |
| 42 | + DocumentStroke stroke, |
| 43 | + DocumentInsets padding, |
| 44 | + DocumentInsets margin |
| 45 | +) implements DocumentNode { |
| 46 | + /** |
| 47 | + * Validates dimensions and the segment list; copy-protects the segments. |
| 48 | + */ |
| 49 | + public PathNode { |
| 50 | + name = name == null ? "" : name; |
| 51 | + Objects.requireNonNull(segments, "segments"); |
| 52 | + segments = List.copyOf(segments); |
| 53 | + if (segments.size() < 2) { |
| 54 | + throw new IllegalArgumentException( |
| 55 | + "path needs at least a MoveTo and one drawing segment: " + segments.size()); |
| 56 | + } |
| 57 | + if (!(segments.get(0) instanceof DocumentPathSegment.MoveTo)) { |
| 58 | + throw new IllegalArgumentException( |
| 59 | + "path must start with a MoveTo segment, found: " |
| 60 | + + segments.get(0).getClass().getSimpleName()); |
| 61 | + } |
| 62 | + padding = padding == null ? DocumentInsets.zero() : padding; |
| 63 | + margin = margin == null ? DocumentInsets.zero() : margin; |
| 64 | + if (width <= 0 || Double.isNaN(width) || Double.isInfinite(width)) { |
| 65 | + throw new IllegalArgumentException("width must be finite and positive: " + width); |
| 66 | + } |
| 67 | + if (height <= 0 || Double.isNaN(height) || Double.isInfinite(height)) { |
| 68 | + throw new IllegalArgumentException("height must be finite and positive: " + height); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + @Override |
| 73 | + public String nodeKind() { |
| 74 | + return "Path"; |
| 75 | + } |
| 76 | +} |
0 commit comments