Search in sources :

Example 1 with DefaultPointLabelDescriptor

use of org.geotoolkit.display2d.style.labeling.DefaultPointLabelDescriptor in project geotoolkit by Geomatys.

the class TextSymbolizerRenderer method presentations.

@Override
public Stream<Presentation> presentations(MapLayer layer, Feature feature) {
    // test if the symbol is visible on this feature
    if (!symbol.isVisible(feature))
        return Stream.empty();
    // we adjust coefficient for rendering ------------------------------
    final float coeff;
    if (dispGeom) {
        // symbol is in display unit
        coeff = 1;
    } else {
        // we have a special unit we must adjust the coefficient. We adapt it to the current region of interest,
        // by computin scale difference between objective and display
        final AffineTransform inverse = renderingContext.getObjectiveToDisplay();
        coeff = (float) (this.coeff * Math.abs(AffineTransforms2D.getScale(inverse)));
    }
    // start to extract label parameters---------------------------------
    String label = symbol.getLabel(feature);
    // nothing to paint
    if (label == null)
        return Stream.empty();
    label = label.trim();
    // nothing to paint
    if (label.isEmpty())
        return Stream.empty();
    final CachedHalo halo = symbol.getHalo();
    final CachedLabelPlacement placement = symbol.getPlacement();
    // extract halo parameters
    final float haloWidth;
    final Paint haloPaint;
    if (halo != null) {
        haloWidth = halo.getWidth(feature);
        haloPaint = halo.getJ2DPaint(feature, 0, 0, hints);
    } else {
        haloWidth = 0;
        haloPaint = Color.WHITE;
    }
    /* Extract text parameters.
         * Note:
         * There's an ambiguity in SE encoding. Section 11 defines that any size, including font size, should use
         * defined UOMs. However, text symbology section 11.4.3 states that font size unit is always in pixels.
         * For now, we force no coefficient, to match both:
         *  - Most specific information (11.4.3)
         *  - Most common use-case. Unit of measurement is useful mostly for displacements, to ensure text does not
         *    overlap geometry at given zoom levels. However, for texts, pixel values is often preferred, to ensure
         *    lisibility on screen.
         *
         * TODO: A better way might be to allow expressions suffixed with "px", as defined in SE section 11, last phrase.
         * For now, however, we do not code this solution, because it should be applied on any size in style,
         * and would require a big rework. Sizes should provide Measures instead of numeric values to check if measure
         * has a local uom (expressed as a suffix), or none, in case the context defined uom should be used.
         */
    final Paint fontPaint = symbol.getFontPaint(feature, 0, 0, 1, hints);
    final Font j2dFont = symbol.getJ2dFont(feature, 1);
    // symbolizer doesnt match the featuretype, no geometry found with this name.
    final ProjectedGeometry projectedGeometry = new ProjectedGeometry(renderingContext);
    projectedGeometry.setDataGeometry(GO2Utilities.getGeometry(feature, symbol.getSource().getGeometry()), null);
    if (placement instanceof CachedPointPlacement) {
        final CachedPointPlacement pp = (CachedPointPlacement) placement;
        final float[] anchor = pp.getAnchor(feature, null);
        final float[] disp = pp.getDisplacement(feature, null);
        final float rotation = pp.getRotation(feature);
        final LabelDescriptor descriptor = new DefaultPointLabelDescriptor(label, j2dFont, fontPaint, haloWidth, haloPaint, anchor[0], anchor[1], // SE 11.3.2: displacement is expressed in defined unit of measurement
        disp[0] * coeff, disp[1] * coeff, rotation, renderingContext.getDisplayCRS(), projectedGeometry);
        final TextPresentation tp = new TextPresentation(layer, layer.getData(), feature);
        tp.forGrid(renderingContext);
        tp.labelDesc = descriptor;
        return Stream.of(tp);
    } else if (placement instanceof CachedLinePlacement) {
        final CachedLinePlacement lp = (CachedLinePlacement) placement;
        final LabelDescriptor descriptor = new DefaultLinearLabelDescriptor(label, j2dFont, fontPaint, haloWidth, haloPaint, // SE section 11: Gap and initial gap are defined using UOM.
        lp.getGap(feature) * coeff, lp.getInitialGap(feature) * coeff, // SE 11.1.4 and 11.4.4: Perpendicular offset is in defined unit of measurement
        lp.getOffset(feature) * coeff, lp.isRepeated(), lp.isAligned(), lp.isGeneralizeLine(), projectedGeometry);
        final TextPresentation tp = new TextPresentation(layer, layer.getData(), feature);
        tp.forGrid(renderingContext);
        tp.labelDesc = descriptor;
        return Stream.of(tp);
    } else {
        ExceptionPresentation ep = new ExceptionPresentation(new PortrayalException("Text symbolizer has no label placement, this should not be possible."));
        ep.setLayer(layer);
        ep.setResource(layer.getData());
        return Stream.of(ep);
    }
}
Also used : CachedHalo(org.geotoolkit.display2d.style.CachedHalo) DefaultLinearLabelDescriptor(org.geotoolkit.display2d.style.labeling.DefaultLinearLabelDescriptor) CachedPointPlacement(org.geotoolkit.display2d.style.CachedPointPlacement) DefaultPointLabelDescriptor(org.geotoolkit.display2d.style.labeling.DefaultPointLabelDescriptor) Paint(java.awt.Paint) CachedLabelPlacement(org.geotoolkit.display2d.style.CachedLabelPlacement) Font(java.awt.Font) ProjectedGeometry(org.geotoolkit.display2d.primitive.ProjectedGeometry) ExceptionPresentation(org.apache.sis.internal.map.ExceptionPresentation) AffineTransform(java.awt.geom.AffineTransform) CachedLinePlacement(org.geotoolkit.display2d.style.CachedLinePlacement) DefaultPointLabelDescriptor(org.geotoolkit.display2d.style.labeling.DefaultPointLabelDescriptor) LabelDescriptor(org.geotoolkit.display2d.style.labeling.LabelDescriptor) DefaultLinearLabelDescriptor(org.geotoolkit.display2d.style.labeling.DefaultLinearLabelDescriptor) TextPresentation(org.geotoolkit.display2d.presentation.TextPresentation) PortrayalException(org.geotoolkit.display.PortrayalException)

Aggregations

Font (java.awt.Font)1 Paint (java.awt.Paint)1 AffineTransform (java.awt.geom.AffineTransform)1 ExceptionPresentation (org.apache.sis.internal.map.ExceptionPresentation)1 PortrayalException (org.geotoolkit.display.PortrayalException)1 TextPresentation (org.geotoolkit.display2d.presentation.TextPresentation)1 ProjectedGeometry (org.geotoolkit.display2d.primitive.ProjectedGeometry)1 CachedHalo (org.geotoolkit.display2d.style.CachedHalo)1 CachedLabelPlacement (org.geotoolkit.display2d.style.CachedLabelPlacement)1 CachedLinePlacement (org.geotoolkit.display2d.style.CachedLinePlacement)1 CachedPointPlacement (org.geotoolkit.display2d.style.CachedPointPlacement)1 DefaultLinearLabelDescriptor (org.geotoolkit.display2d.style.labeling.DefaultLinearLabelDescriptor)1 DefaultPointLabelDescriptor (org.geotoolkit.display2d.style.labeling.DefaultPointLabelDescriptor)1 LabelDescriptor (org.geotoolkit.display2d.style.labeling.LabelDescriptor)1