Search in sources :

Example 1 with LabelDescriptor

use of org.geotoolkit.display2d.style.labeling.LabelDescriptor 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)

Example 2 with LabelDescriptor

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

the class DisplacementLabelRenderer method portrayLabels.

/**
 * {@inheritDoc }
 */
@Override
public boolean portrayLabels() {
    final Graphics2D g2 = context.getGraphics();
    // enable antialiasing for labels
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    List<Candidate> candidates = new ArrayList<Candidate>();
    // generate all the candidates
    // priority is in the order of the layers provided.
    int priority = layers.size();
    for (final LabelLayer layer : layers) {
        for (LabelDescriptor label : layer.labels()) {
            Candidate[] cs = null;
            if (label instanceof PointLabelDescriptor) {
                cs = pointRenderer.generateCandidat((PointLabelDescriptor) label);
            } else if (label instanceof LinearLabelDescriptor) {
                cs = LinearRenderer.generateCandidat((LinearLabelDescriptor) label);
            } else {
                cs = null;
            }
            if (cs != null) {
                for (Candidate c : cs) {
                    c.setPriority(priority);
                    candidates.add(c);
                }
            }
        }
        priority--;
    }
    // displace or remove the candidates
    candidates = optimize(candidates);
    // paint the remaining candidates
    for (Candidate candidate : candidates) {
        if (candidate instanceof PointCandidate) {
            pointRenderer.render(candidate);
        } else if (candidate instanceof LinearCandidate) {
            LinearRenderer.render(candidate);
        }
    }
    layers.clear();
    return !candidates.isEmpty();
}
Also used : Candidate(org.geotoolkit.display2d.style.labeling.candidate.Candidate) PointCandidate(org.geotoolkit.display2d.style.labeling.candidate.PointCandidate) LinearCandidate(org.geotoolkit.display2d.style.labeling.candidate.LinearCandidate) LinearCandidate(org.geotoolkit.display2d.style.labeling.candidate.LinearCandidate) LabelLayer(org.geotoolkit.display2d.style.labeling.LabelLayer) LinearLabelDescriptor(org.geotoolkit.display2d.style.labeling.LinearLabelDescriptor) ArrayList(java.util.ArrayList) PointCandidate(org.geotoolkit.display2d.style.labeling.candidate.PointCandidate) LabelDescriptor(org.geotoolkit.display2d.style.labeling.LabelDescriptor) LinearLabelDescriptor(org.geotoolkit.display2d.style.labeling.LinearLabelDescriptor) PointLabelDescriptor(org.geotoolkit.display2d.style.labeling.PointLabelDescriptor) Graphics2D(java.awt.Graphics2D) PointLabelDescriptor(org.geotoolkit.display2d.style.labeling.PointLabelDescriptor)

Aggregations

LabelDescriptor (org.geotoolkit.display2d.style.labeling.LabelDescriptor)2 Font (java.awt.Font)1 Graphics2D (java.awt.Graphics2D)1 Paint (java.awt.Paint)1 AffineTransform (java.awt.geom.AffineTransform)1 ArrayList (java.util.ArrayList)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 LabelLayer (org.geotoolkit.display2d.style.labeling.LabelLayer)1 LinearLabelDescriptor (org.geotoolkit.display2d.style.labeling.LinearLabelDescriptor)1 PointLabelDescriptor (org.geotoolkit.display2d.style.labeling.PointLabelDescriptor)1 Candidate (org.geotoolkit.display2d.style.labeling.candidate.Candidate)1