This is an automated email from the ASF dual-hosted git repository. asf-gitbox-commits pushed a commit to branch geoapi-4.0 in repository https://gitbox.apache.org/repos/asf/sis.git
commit 3771918adaac34bfd38418faec517ef1b3a6d114 Author: jsorel <[email protected]> AuthorDate: Tue Nov 17 16:11:05 2026 +0100 feat(Portrayal): add support for cross layer rendering order --- .../apache/sis/map/service/GraphicsPortrayer.java | 72 ++++++----- .../apache/sis/map/service/PainterRegistry.java | 103 ++++++++++++++++ .../org/apache/sis/map/service/StylePainter.java | 17 +++ .../sis/map/service/se1/SymbolizerToScene2D.java | 5 +- .../sis/map/service/PainterRegistryTest.java | 131 +++++++++++++++++++++ 5 files changed, 300 insertions(+), 28 deletions(-) diff --git a/incubator/src/org.apache.sis.portrayal.map/main/org/apache/sis/map/service/GraphicsPortrayer.java b/incubator/src/org.apache.sis.portrayal.map/main/org/apache/sis/map/service/GraphicsPortrayer.java index 4ed1e56d0f..0fda332d40 100644 --- a/incubator/src/org.apache.sis.portrayal.map/main/org/apache/sis/map/service/GraphicsPortrayer.java +++ b/incubator/src/org.apache.sis.portrayal.map/main/org/apache/sis/map/service/GraphicsPortrayer.java @@ -19,8 +19,8 @@ package org.apache.sis.map.service; import java.awt.Graphics2D; import java.awt.Shape; import java.awt.image.BufferedImage; -import java.util.HashMap; -import java.util.Map; +import java.util.ArrayList; +import java.util.List; import java.util.Objects; import java.util.ServiceLoader; import java.util.stream.Stream; @@ -41,15 +41,10 @@ import org.apache.sis.style.Style; public final class GraphicsPortrayer { /** - * Reference all painters. + * All painters found on the module path or class path, indexed by the style class they support. */ - private static final Map<Class<? extends Style>,StylePainter> PAINTERS = new HashMap<>(); - static { - final ServiceLoader<StylePainter> loader = ServiceLoader.load(StylePainter.class, StylePainter.class.getClassLoader()); - for (StylePainter painter : loader) { - PAINTERS.put(painter.getStyleClass(), painter); - } - } + private static final PainterRegistry PAINTERS = new PainterRegistry( + ServiceLoader.load(StylePainter.class, StylePainter.class.getClassLoader())); private Graphics2D graphics; private GridGeometry domain; @@ -137,30 +132,53 @@ public final class GraphicsPortrayer { * @throws RenderingException if a rendering procedure fails. */ public synchronized GraphicsPortrayer portray(MapItem map) throws RenderingException { - portray(init(), map); - return this; + final Scene2D scene = init(); + final List<MapLayer> layers = new ArrayList<>(); + collectVisibleLayers(map, layers); + /* + * Consecutive layers using the same painter are given together to that painter, + * which can then draw across layers (labels on top of all layers, rendering passes, etc.). + */ + StylePainter painter = null; + final List<MapLayer> group = new ArrayList<>(); + for (final MapLayer layer : layers) { + final StylePainter p = PAINTERS.get(layer.getStyle().getClass()); + if (p != painter) { + if (painter != null) { + painter.paint(scene, List.copyOf(group)); + } + group.clear(); + painter = p; + } + if (p != null) { + group.add(layer); + } + } + if (painter != null) { + painter.paint(scene, List.copyOf(group)); + } + return this; } - private void portray(Scene2D scene, MapItem map) throws RenderingException { + /** + * Adds the visible layers having a style to the given list, in drawing order. + * + * @param map the map item to traverse. + * @param target where to add the layers. + */ + private static void collectVisibleLayers(final MapItem map, final List<MapLayer> target) { if (map == null || !map.isVisible()) return; - if (map instanceof MapLayer) { - portray(scene, (MapLayer) map); - } else if (map instanceof MapLayers) { - final MapLayers layers = (MapLayers) map; - for (MapItem item : layers.getComponents()) { - portray(scene, item); + if (map instanceof MapLayer layer) { + if (layer.getStyle() != null) { + target.add(layer); + } + } else if (map instanceof MapLayers layers) { + for (final MapItem item : layers.getComponents()) { + collectVisibleLayers(item, target); } } } - private void portray(Scene2D scene, MapLayer layer) throws RenderingException { - final Style style = layer.getStyle(); - if (style == null) return; - final StylePainter painter = PAINTERS.get(style.getClass()); - if (painter == null) return; - painter.paint(scene, layer); - } - /** * Present given map. * diff --git a/incubator/src/org.apache.sis.portrayal.map/main/org/apache/sis/map/service/PainterRegistry.java b/incubator/src/org.apache.sis.portrayal.map/main/org/apache/sis/map/service/PainterRegistry.java new file mode 100644 index 0000000000..4e2c064962 --- /dev/null +++ b/incubator/src/org.apache.sis.portrayal.map/main/org/apache/sis/map/service/PainterRegistry.java @@ -0,0 +1,103 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.sis.map.service; + +import java.util.ArrayDeque; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.Queue; +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; + + +/** + * Painters indexed by the style class they support. + * A style whose class has no painter uses the painter of its nearest parent class or interface. + * The search order is: + * <ul> + * <li>the style class itself,</li> + * <li>its superclasses, nearest first,</li> + * <li>the interfaces of all those classes, in breadth-first order.</li> + * </ul> + * + * @author Johann Sorel (Geomatys) + */ +final class PainterRegistry { + + /** + * Painters registered for the exact class returned by {@link StylePainter#getStyleClass()}. + */ + private final Map<Class<?>, StylePainter> painters; + + /** + * Result of the searches done so far, including the classes which have no painter. + */ + private final Map<Class<?>, Optional<StylePainter>> cache; + + /** + * Creates a registry for the given painters. + * If two painters support the same style class, the last one wins. + * + * @param candidates the painters to register. + */ + PainterRegistry(final Iterable<? extends StylePainter> candidates) { + painters = new HashMap<>(); + for (final StylePainter painter : candidates) { + painters.put(painter.getStyleClass(), painter); + } + cache = new ConcurrentHashMap<>(); + } + + /** + * Returns the painter for the given style class, or {@code null} if none. + * + * @param styleClass class of the style to paint. + * @return painter for the style class or one of its parents, or {@code null}. + */ + StylePainter get(final Class<?> styleClass) { + return cache.computeIfAbsent(styleClass, this::search).orElse(null); + } + + /** + * Searches the painter of the given class or of its nearest parent. + */ + private Optional<StylePainter> search(final Class<?> styleClass) { + final Queue<Class<?>> interfaces = new ArrayDeque<>(); + for (Class<?> c = styleClass; c != null; c = c.getSuperclass()) { + final StylePainter painter = painters.get(c); + if (painter != null) { + return Optional.of(painter); + } + interfaces.addAll(List.of(c.getInterfaces())); + } + final Set<Class<?>> visited = new HashSet<>(); + Class<?> c; + while ((c = interfaces.poll()) != null) { + if (visited.add(c)) { + final StylePainter painter = painters.get(c); + if (painter != null) { + return Optional.of(painter); + } + interfaces.addAll(List.of(c.getInterfaces())); + } + } + return Optional.empty(); + } +} diff --git a/incubator/src/org.apache.sis.portrayal.map/main/org/apache/sis/map/service/StylePainter.java b/incubator/src/org.apache.sis.portrayal.map/main/org/apache/sis/map/service/StylePainter.java index 29da097ea1..e6527ba1b8 100644 --- a/incubator/src/org.apache.sis.portrayal.map/main/org/apache/sis/map/service/StylePainter.java +++ b/incubator/src/org.apache.sis.portrayal.map/main/org/apache/sis/map/service/StylePainter.java @@ -17,6 +17,7 @@ package org.apache.sis.map.service; import java.awt.Shape; +import java.util.List; import java.util.stream.Stream; import org.apache.sis.map.MapLayer; import org.apache.sis.map.Presentation; @@ -46,6 +47,22 @@ public interface StylePainter { */ void paint(Scene2D scene, MapLayer layer) throws RenderingException; + /** + * Stateless portraying of consecutive map layers using this painter. + * Painters can override this method for portraying across layers, for example for drawing + * labels on top of all layers or for ordering the drawings of all layers by rendering pass. + * The default implementation paints each layer in order. + * + * @param scene parameters for rendering. + * @param layers layers having supported style class, in drawing order. + * @throws RenderingException if an error occurred while rendering. + */ + default void paint(Scene2D scene, List<MapLayer> layers) throws RenderingException { + for (final MapLayer layer : layers) { + paint(scene, layer); + } + } + /** * Statefull portraying of the given map layer. * <p> diff --git a/incubator/src/org.apache.sis.portrayal.map/main/org/apache/sis/map/service/se1/SymbolizerToScene2D.java b/incubator/src/org.apache.sis.portrayal.map/main/org/apache/sis/map/service/se1/SymbolizerToScene2D.java index 9127c7f199..033a9a96c4 100644 --- a/incubator/src/org.apache.sis.portrayal.map/main/org/apache/sis/map/service/se1/SymbolizerToScene2D.java +++ b/incubator/src/org.apache.sis.portrayal.map/main/org/apache/sis/map/service/se1/SymbolizerToScene2D.java @@ -60,7 +60,10 @@ public abstract class SymbolizerToScene2D<S extends Symbolizer> { } } - protected static final Logger LOGGER = Logger.getLogger("com.examind.sdk.render"); + /** + * The logger of the renderer, shared with {@link Scene2D}. + */ + protected static final Logger LOGGER = Scene2D.LOGGER; protected final Scene2D state; protected final S symbolizer; diff --git a/incubator/src/org.apache.sis.portrayal.map/test/org/apache/sis/map/service/PainterRegistryTest.java b/incubator/src/org.apache.sis.portrayal.map/test/org/apache/sis/map/service/PainterRegistryTest.java new file mode 100644 index 0000000000..cd12b36c12 --- /dev/null +++ b/incubator/src/org.apache.sis.portrayal.map/test/org/apache/sis/map/service/PainterRegistryTest.java @@ -0,0 +1,131 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.sis.map.service; + +import java.awt.Shape; +import java.util.List; +import java.util.stream.Stream; +import org.apache.sis.map.MapLayer; +import org.apache.sis.map.Presentation; +import org.apache.sis.style.Style; + +// Test dependencies +import static org.junit.jupiter.api.Assertions.*; +import org.junit.jupiter.api.Test; + + +/** + * Tests {@link PainterRegistry}. + * + * @author Johann Sorel (Geomatys) + */ +public final class PainterRegistryTest { + + /** + * Creates a new test case. + */ + public PainterRegistryTest() { + } + + /** + * A style class with a registered painter. + */ + private static class BaseStyle implements Style { + } + + /** + * A style class without registered painter, inheriting the painter of {@link BaseStyle}. + */ + private static class SubStyle extends BaseStyle { + } + + /** + * A style interface with a registered painter. + */ + private interface MarkerStyle extends Style { + } + + /** + * A style class inheriting the painter of {@link MarkerStyle}. + */ + private static final class MarkedStyle implements MarkerStyle { + } + + /** + * A style class unrelated to any registered painter. + */ + private static final class OtherStyle implements Style { + } + + /** + * A painter which only declares the style class it supports. + */ + private static final class TestPainter implements StylePainter { + private final Class<? extends Style> styleClass; + + TestPainter(final Class<? extends Style> styleClass) { + this.styleClass = styleClass; + } + + @Override + public Class<? extends Style> getStyleClass() { + return styleClass; + } + + @Override + public void paint(final Scene2D scene, final MapLayer layer) { + throw new UnsupportedOperationException(); + } + + @Override + public Stream<Presentation> present(final Scene2D scene, final MapLayer layer) { + throw new UnsupportedOperationException(); + } + + @Override + public Stream<Presentation> intersects(final Scene2D scene, final MapLayer layer, final Shape mask) { + throw new UnsupportedOperationException(); + } + } + + /** + * Tests that the painter of a parent class or interface is found. + */ + @Test + public void testInheritedPainter() { + final TestPainter base = new TestPainter(BaseStyle.class); + final TestPainter marker = new TestPainter(MarkerStyle.class); + final PainterRegistry registry = new PainterRegistry(List.of(base, marker)); + assertSame(base, registry.get(BaseStyle.class)); + assertSame(base, registry.get(SubStyle.class)); + assertSame(marker, registry.get(MarkedStyle.class)); + assertNull(registry.get(OtherStyle.class)); + assertNull(registry.get(OtherStyle.class), "Cached negative result."); + } + + /** + * Tests that a painter registered for the exact class has precedence over the parent painter. + */ + @Test + public void testExactPainterFirst() { + final TestPainter base = new TestPainter(BaseStyle.class); + final TestPainter sub = new TestPainter(SubStyle.class); + final PainterRegistry registry = new PainterRegistry(List.of(base, sub)); + assertSame(sub, registry.get(SubStyle.class)); + assertSame(base, registry.get(BaseStyle.class)); + } +}
