Merge pull request #10982 from ahocevar/replay-scale

Handle scaled output canvas correctly
This commit is contained in:
Andreas Hocevar
2020-04-30 08:44:10 +02:00
committed by GitHub
6 changed files with 30 additions and 7 deletions

View File

@@ -293,6 +293,7 @@ class Executor {
/** /**
* @param {CanvasRenderingContext2D} context Context. * @param {CanvasRenderingContext2D} context Context.
* @param {number} contextScale Scale of the context.
* @param {number} x X. * @param {number} x X.
* @param {number} y Y. * @param {number} y Y.
* @param {import("../canvas.js").Label|HTMLImageElement|HTMLCanvasElement|HTMLVideoElement} imageOrLabel Image. * @param {import("../canvas.js").Label|HTMLImageElement|HTMLCanvasElement|HTMLVideoElement} imageOrLabel Image.
@@ -313,6 +314,7 @@ class Executor {
*/ */
replayImageOrLabel_( replayImageOrLabel_(
context, context,
contextScale,
x, x,
y, y,
imageOrLabel, imageOrLabel,
@@ -395,9 +397,9 @@ class Executor {
? (strokeInstruction[2] * scale) / 2 ? (strokeInstruction[2] * scale) / 2
: 0; : 0;
const intersects = const intersects =
tmpExtent[0] - strokePadding <= canvas.width && tmpExtent[0] - strokePadding <= canvas.width / contextScale &&
tmpExtent[2] + strokePadding >= 0 && tmpExtent[2] + strokePadding >= 0 &&
tmpExtent[1] - strokePadding <= canvas.height && tmpExtent[1] - strokePadding <= canvas.height / contextScale &&
tmpExtent[3] + strokePadding >= 0; tmpExtent[3] + strokePadding >= 0;
if (snapToPixel) { if (snapToPixel) {
@@ -593,6 +595,7 @@ class Executor {
/** /**
* @private * @private
* @param {CanvasRenderingContext2D} context Context. * @param {CanvasRenderingContext2D} context Context.
* @param {number} contextScale Scale of the context.
* @param {import("../../transform.js").Transform} transform Transform. * @param {import("../../transform.js").Transform} transform Transform.
* @param {Array<*>} instructions Instructions array. * @param {Array<*>} instructions Instructions array.
* @param {boolean} snapToPixel Snap point symbols and text to integer pixels. * @param {boolean} snapToPixel Snap point symbols and text to integer pixels.
@@ -604,6 +607,7 @@ class Executor {
*/ */
execute_( execute_(
context, context,
contextScale,
transform, transform,
instructions, instructions,
snapToPixel, snapToPixel,
@@ -826,6 +830,7 @@ class Executor {
} }
this.replayImageOrLabel_( this.replayImageOrLabel_(
context, context,
contextScale,
pixelCoordinates[d], pixelCoordinates[d],
pixelCoordinates[d + 1], pixelCoordinates[d + 1],
image, image,
@@ -918,6 +923,7 @@ class Executor {
offsetY; offsetY;
this.replayImageOrLabel_( this.replayImageOrLabel_(
context, context,
contextScale,
/** @type {number} */ (part[0]), /** @type {number} */ (part[0]),
/** @type {number} */ (part[1]), /** @type {number} */ (part[1]),
label, label,
@@ -947,6 +953,7 @@ class Executor {
anchorY = baseline * label.height - offsetY; anchorY = baseline * label.height - offsetY;
this.replayImageOrLabel_( this.replayImageOrLabel_(
context, context,
contextScale,
/** @type {number} */ (part[0]), /** @type {number} */ (part[0]),
/** @type {number} */ (part[1]), /** @type {number} */ (part[1]),
label, label,
@@ -1064,14 +1071,16 @@ class Executor {
/** /**
* @param {CanvasRenderingContext2D} context Context. * @param {CanvasRenderingContext2D} context Context.
* @param {number} contextScale Scale of the context.
* @param {import("../../transform.js").Transform} transform Transform. * @param {import("../../transform.js").Transform} transform Transform.
* @param {number} viewRotation View rotation. * @param {number} viewRotation View rotation.
* @param {boolean} snapToPixel Snap point symbols and text to integer pixels. * @param {boolean} snapToPixel Snap point symbols and text to integer pixels.
*/ */
execute(context, transform, viewRotation, snapToPixel) { execute(context, contextScale, transform, viewRotation, snapToPixel) {
this.viewRotation_ = viewRotation; this.viewRotation_ = viewRotation;
this.execute_( this.execute_(
context, context,
contextScale,
transform, transform,
this.instructions, this.instructions,
snapToPixel, snapToPixel,
@@ -1101,6 +1110,7 @@ class Executor {
this.viewRotation_ = viewRotation; this.viewRotation_ = viewRotation;
return this.execute_( return this.execute_(
context, context,
1,
transform, transform,
this.hitDetectionInstructions, this.hitDetectionInstructions,
true, true,

View File

@@ -310,6 +310,7 @@ class ExecutorGroup {
/** /**
* @param {CanvasRenderingContext2D} context Context. * @param {CanvasRenderingContext2D} context Context.
* @param {number} contextScale Scale of the context.
* @param {import("../../transform.js").Transform} transform Transform. * @param {import("../../transform.js").Transform} transform Transform.
* @param {number} viewRotation View rotation. * @param {number} viewRotation View rotation.
* @param {boolean} snapToPixel Snap point symbols and test to integer pixel. * @param {boolean} snapToPixel Snap point symbols and test to integer pixel.
@@ -319,6 +320,7 @@ class ExecutorGroup {
*/ */
execute( execute(
context, context,
contextScale,
transform, transform,
viewRotation, viewRotation,
snapToPixel, snapToPixel,
@@ -357,7 +359,13 @@ class ExecutorGroup {
declutter.push(replay, transform.slice(0)); declutter.push(replay, transform.slice(0));
} }
} else { } else {
replay.execute(context, transform, viewRotation, snapToPixel); replay.execute(
context,
contextScale,
transform,
viewRotation,
snapToPixel
);
} }
} }
} }
@@ -476,7 +484,7 @@ export function replayDeclutter(
}); });
} }
const transform = executorData[i++]; const transform = executorData[i++];
executor.execute(context, transform, rotation, snapToPixel); executor.execute(context, 1, transform, rotation, snapToPixel);
} }
} }
} }

View File

@@ -225,6 +225,7 @@ class CanvasVectorLayerRenderer extends CanvasLayerRenderer {
const declutterReplays = this.getLayer().getDeclutter() ? {} : null; const declutterReplays = this.getLayer().getDeclutter() ? {} : null;
replayGroup.execute( replayGroup.execute(
context, context,
1,
transform, transform,
rotation, rotation,
snapToPixel, snapToPixel,
@@ -255,6 +256,7 @@ class CanvasVectorLayerRenderer extends CanvasLayerRenderer {
); );
replayGroup.execute( replayGroup.execute(
context, context,
1,
transform, transform,
rotation, rotation,
snapToPixel, snapToPixel,
@@ -279,6 +281,7 @@ class CanvasVectorLayerRenderer extends CanvasLayerRenderer {
); );
replayGroup.execute( replayGroup.execute(
context, context,
1,
transform, transform,
rotation, rotation,
snapToPixel, snapToPixel,

View File

@@ -665,6 +665,7 @@ class CanvasVectorTileLayerRenderer extends CanvasTileLayerRenderer {
} }
executorGroup.execute( executorGroup.execute(
context, context,
1,
transform, transform,
rotation, rotation,
hifi, hifi,
@@ -813,6 +814,7 @@ class CanvasVectorTileLayerRenderer extends CanvasTileLayerRenderer {
const executorGroup = executorGroups[i]; const executorGroup = executorGroups[i];
executorGroup.execute( executorGroup.execute(
context, context,
renderScale,
transform, transform,
0, 0,
true, true,

View File

@@ -35,7 +35,7 @@ function executeInstructions(
const executor = new Executor(0.02, 1, false, builder.finish()); const executor = new Executor(0.02, 1, false, builder.finish());
sinon.spy(executor, 'drawLabelWithPointPlacement_'); sinon.spy(executor, 'drawLabelWithPointPlacement_');
const replayImageOrLabelStub = sinon.stub(executor, 'replayImageOrLabel_'); const replayImageOrLabelStub = sinon.stub(executor, 'replayImageOrLabel_');
executor.execute(context, transform); executor.execute(context, 1, transform);
expect(executor.drawLabelWithPointPlacement_.callCount).to.be( expect(executor.drawLabelWithPointPlacement_.callCount).to.be(
expectedDrawTextImageCalls expectedDrawTextImageCalls
); );

View File

@@ -40,7 +40,7 @@ describe('ol.render.canvas.BuilderGroup', function () {
!!overlaps, !!overlaps,
builder.finish() builder.finish()
); );
executor.execute(context, transform, 0, false); executor.execute(context, 1, transform, 0, false);
} }
beforeEach(function () { beforeEach(function () {