PdfGeoCanvas: clip from MapPrinterLayoutAsyncLayer stays active after drawing a layer with Opacity < 1

Environment

  • ThinkGeo.Core 15.0.0-beta197 (also present in beta191)
  • .NET 10, Windows, PdfGeoCanvas with MapPrinterLayoutAsyncLayer in MapPrinterDrawingMode.Vector

Summary

We export a print layout to PDF: a page layer, a MapPrinterLayoutAsyncLayer , and our own printer layers drawn afterwards (a frame around the map, scale bar, text). Once the map contains a feature layer with Opacity < 1 , anything drawn after the map layer is clipped to the map’s area. Our frame lies just outside the map area, so it disappears from the PDF entirely. Without such a layer, the frame is drawn correctly.

The PDF content stream shows that the frame stroke is written with the map’s clip still active:

q
6.5866 6.5866 582.10 828.72 re W* n      % map clip
/G7 gs
5.6693 5.6693 583.94 830.55 re S         % frame stroke, entirely outside the clip
...                                       % everything after it is inside the same clip group
Q                                         % only closed at the end of the page

Cause (as far as we can tell from the beta197 assembly)

  1. Layer.Draw with Opacity != 1 renders to an off-screen image and then calls canvas.DrawWorldImageWithoutScaling(image, …, 0f, 0f, 0f, …) .
  2. SkiaGeoCanvas.DrawScreenImageWithoutScalingCore always calls Save() , but only calls Restore() when flag is true:
n2.b();                       // Save
bool flag = true;
if (rotateAngle % 360f == 0f)
{
    ...
    flag = num5 != 0f || num6 != 0f;   // false when xOffset/yOffset are 0
    if (flag) n2.A(num5, num6);
}
...
finally
{
    ...
    if (flag) n2.B();         // Restore skipped → one Save leaks
}
  1. PdfGeoCanvas wraps each queued command in Save() → ClipRect(ClippingArea) → command → Restore() . Because of the leaked save, its Restore() pops the image’s save instead of its own. The ClipRect from MapPrinterLayoutAsyncLayer.ClippingArea therefore remains on the SKCanvas until the page ends, even though ClippingArea is set back to null .

Minimal repro

using var stream = File.Create("clip-leak.pdf");
var canvas = new PdfGeoCanvas { PageWidth = 595, PageHeight = 842 };
canvas.BeginDrawing(stream, new RectangleShape(0, 842, 595, 0), GeographyUnit.Meter);

// What MapPrinterLayoutAsyncLayer does for a layer with Opacity < 1
canvas.ClippingArea = new RectangleShape(100, 742, 495, 100);
using (var image = new GeoImage(50, 50))
    canvas.DrawWorldImageWithoutScaling(image, 297.5, 421, DrawingLevel.LevelOne);
canvas.Flush();
canvas.ClippingArea = null;

// Drawn after the clip was reset; lies outside the former clip
canvas.DrawArea(new RectangleShape(20, 822, 575, 20), new GeoPen(GeoColors.Black, 4), DrawingLevel.LevelOne);
canvas.EndDrawing();
  • Expected: a black rectangle near the page edge.
  • Actual: the rectangle is missing. Without the DrawWorldImageWithoutScaling call, it appears.

Suggested fix: restore unconditionally in DrawScreenImageWithoutScalingCore . Alternatively, have PdfGeoCanvas use int count = canvas.Save(); … canvas.RestoreToCount(count); so that unbalanced saves inside a command can’t leak.

Related: Background on a printer layer fills the entire page

AsyncLayer calls canvas.Clear(new GeoSolidBrush(Background)) when Background is not transparent. GeoCanvas.ClearCore fills CurrentWorldExtent scaled up 100×. On a PdfGeoCanvas , setting MapPrinterLayoutAsyncLayer.Background therefore paints the whole page, including the page layer and the margins, not just the map area. We expected Background to fill only the map frame, as BackgroundMask does. Is that intended for printer layers?

Our workaround: we draw the frame and the map background ourselves before the MapPrinterLayoutAsyncLayer , and leave its Background unset. Elements drawn after the map still get the leaked clip, but ours happen to lie inside the map area.

Hi Julian,

Your analysis is right on every point, and the clip is fixed in beta200.

DrawScreenImageWithoutScalingCore saved the canvas state and gave it back only when it had translated. An image drawn with no offset and no rotation - which is how a layer with Opacity below 1 arrives - left one save behind, and PdfGeoCanvas’s own Restore then popped that one instead of the one holding the clip. We took your first suggestion: the restore is unconditional now. The other save and restore pairs in SkiaGeoCanvas are balanced, so PdfGeoCanvas itself is unchanged.

Your minimal repro loses the rectangle on beta191 and beta197 and draws it on beta199. A layout shaped like yours - a page, a MapPrinterLayoutAsyncLayer in vector mode holding a layer at Opacity 0.5, and a frame just outside the map drawn afterwards - loses its frame on beta197 and keeps it on beta199. You can draw things after the map layer again.

Background is a property on LayerBase, and a layer with a Background erases whatever the layers beneath it drew - on a print page, the page layer and the margins. It does not belong on a layer, so as of beta199 it is marked obsolete and it will be removed in 16.0.

For the map’s fill and its frame, set BackgroundMask on the MapPrinterLayoutAsyncLayer itself:

mapPrinterLayer.BackgroundMask = new AreaStyle(new GeoPen(GeoColors.Black, 2), new GeoSolidBrush(yourFillColour));

The brush fills the map frame only, beneath the map’s layers, and replaces the fill you draw by hand. The pen draws the frame: centered on the map’s edge, over the map’s content, with the content cut at the pen’s outer edge. Its width is in the layout’s unit, 1/96 inch, like every other printer layer’s - so it prints at the size you see in the preview.

Thanks,
Ben

1 Like