diff --git a/lib/services/html_service.dart b/lib/services/html_service.dart
index 13af2a45..e540264f 100644
--- a/lib/services/html_service.dart
+++ b/lib/services/html_service.dart
@@ -4644,27 +4644,39 @@ Technical details: $message''';
final oldY = double.tryParse(parts[3]) ?? 0;
if (pageH <= 0 || vh <= 0 || iw <= 0) return;
final shots = <(Uint8List shot, double offset)>[];
- final steps = (pageH / vh).ceil();
- for (var i = 0; i < steps && i < _maxFullPageShots; i++) {
+ // How much page one shot covers. The snapshot is the web view's own
+ // bounds, which can be shorter than window.innerHeight, so the step
+ // follows the first shot's aspect ratio (it covers iw CSS px of
+ // width) instead of assuming innerHeight.
+ var step = vh;
+ var y = 0.0;
+ while (y < pageH && shots.length < _maxFullPageShots) {
if (stale()) return;
- await _scrollPageTo(c, (i * vh).round());
+ await _scrollPageTo(c, y.round());
// Let the new scroll position render and lazy content start.
await Future.delayed(const Duration(milliseconds: 80));
// Where the browser actually scrolled to: rounding and clamping at
- // the bottom make this differ from i*vh. The stitch places each shot
+ // the bottom make this differ from y. The stitch places each shot
// at its real offset, so the image stays aligned with the page.
final pos = await c.evaluateJavascript(
source: 'Math.round(window.scrollY)',
contentWorld: collectorWorld(defaultTargetPlatform));
- final y = double.tryParse(pos?.toString() ?? '') ?? i * vh;
+ final at = double.tryParse(pos?.toString() ?? '') ?? y;
final shot = await c.takeScreenshot(
screenshotConfiguration: _fullPageScreenshotCfg);
- if (shot != null && shot.isNotEmpty) shots.add((shot, y));
+ if (shot != null && shot.isNotEmpty) {
+ if (shots.isEmpty) {
+ final covered = await _coveredHeight(shot, iw);
+ if (covered != null) step = covered;
+ }
+ shots.add((shot, at));
+ }
+ y += step;
}
await _scrollPageTo(c, oldY.round());
if (stale() || shots.isEmpty) return;
final image = await stitchScreenshots(shots,
- pageHeight: pageH, viewportHeight: vh, viewportWidth: iw);
+ pageHeight: pageH, viewportHeight: step, viewportWidth: iw);
if (image == null) return;
final bytes = await image.toByteData(format: ui.ImageByteFormat.png);
image.dispose();
@@ -4680,6 +4692,21 @@ Technical details: $message''';
}
}
+ /// The CSS height a screenshot covers, given that it covers [cssWidth] of
+ /// width: from its own aspect ratio. Null when it does not decode.
+ Future _coveredHeight(Uint8List shot, double cssWidth) async {
+ try {
+ final codec = await ui.instantiateImageCodec(shot);
+ final frame = await codec.getNextFrame();
+ codec.dispose();
+ final h = cssWidth * frame.image.height / frame.image.width;
+ frame.image.dispose();
+ return h > 0 ? h.floorToDouble() : null;
+ } catch (_) {
+ return null;
+ }
+ }
+
/// Measures the live page's boxes for the Structure tab. Dropped when the
/// user navigates meanwhile.
Future collectLayout() async {
diff --git a/lib/services/screenshot_stitch.dart b/lib/services/screenshot_stitch.dart
index a7b660d2..145707de 100644
--- a/lib/services/screenshot_stitch.dart
+++ b/lib/services/screenshot_stitch.dart
@@ -1,8 +1,12 @@
+import 'dart:math' as math;
import 'dart:typed_data';
import 'dart:ui'
show Canvas, FilterQuality, Image, Paint, PictureRecorder, Rect,
instantiateImageCodec;
+/// Longest side of the stitched image in pixels (safe on mobile GPUs).
+const double _maxSide = 8192;
+
/// Stitches viewport screenshots taken while scrolling through a page into
/// one full-page image. Each entry is a shot plus the page's actual scroll
/// offset (CSS px) it was taken at: the browser rounds and clamps scroll
@@ -37,8 +41,15 @@ Future stitchScreenshots(
final slotH = viewportHeight * scale;
final targetH =
(pageHeight * scale).clamp(1.0, 100000.0).ceilToDouble();
+ // GPU textures have a size limit and an over-size picture is cropped, not
+ // scaled (only the top-left of the page would survive): shrink everything
+ // so the result stays within [_maxSide].
+ final k = math.min(1.0, _maxSide / math.max(w, targetH));
+ final outW = math.max(1, (w * k).ceil());
+ final outH = math.max(1, (targetH * k).ceil());
final recorder = PictureRecorder();
final canvas = Canvas(recorder);
+ canvas.scale(k);
canvas.clipRect(Rect.fromLTWH(0, 0, w, targetH));
for (final (img, offset) in images) {
canvas.drawImageRect(
@@ -50,7 +61,7 @@ Future stitchScreenshots(
img.dispose();
}
final picture = recorder.endRecording();
- final out = await picture.toImage(w.ceil(), targetH.ceil());
+ final out = await picture.toImage(outW, outH);
picture.dispose();
return out;
}
diff --git a/lib/widgets/structure_painter.dart b/lib/widgets/structure_painter.dart
index 29fb99e7..875cb843 100644
--- a/lib/widgets/structure_painter.dart
+++ b/lib/widgets/structure_painter.dart
@@ -1,4 +1,3 @@
-import 'dart:math' as math;
import 'dart:ui' as ui;
import 'package:material_ui/material_ui.dart';
@@ -77,6 +76,20 @@ class StructurePainter extends CustomPainter {
this.backgroundFullPage = false,
});
+ /// Where the screenshot goes on the canvas. The shot is as wide as the
+ /// viewport (not the document, which can overflow it, esp. on phones), so
+ /// it is drawn [layout.viewportWidth] * [scale] wide with its own aspect
+ /// ratio: one uniform scale, no stretching, so it lines up with the blocks
+ /// at the top and drifts only if the page height changed after capture.
+ /// Without a viewport width it falls back to the canvas width.
+ static Rect backgroundRect(
+ int imageWidth, int imageHeight, PageLayout layout, double scale) {
+ final w = (layout.viewportWidth > 0 ? layout.viewportWidth : layout.width) *
+ scale;
+ final h = imageWidth > 0 ? w * imageHeight / imageWidth : 0.0;
+ return Rect.fromLTWH(0, 0, w, h);
+ }
+
static const double minLabelWidth = 60;
static const double minLabelHeight = 16;
@@ -96,14 +109,12 @@ class StructurePainter extends CustomPainter {
// image and the blocks share coordinates), else over the first screen
// only.
if (background != null) {
- final bgHeight = backgroundFullPage
- ? size.height
- : math.min(model.layout.viewportHeight * scale, size.height);
canvas.drawImageRect(
background!,
Rect.fromLTWH(
0, 0, background!.width.toDouble(), background!.height.toDouble()),
- Rect.fromLTWH(0, 0, size.width, bgHeight),
+ backgroundRect(
+ background!.width, background!.height, model.layout, scale),
Paint()..filterQuality = FilterQuality.medium,
);
}
diff --git a/test/services/screenshot_stitch_test.dart b/test/services/screenshot_stitch_test.dart
index 384c9481..1cfee1be 100644
--- a/test/services/screenshot_stitch_test.dart
+++ b/test/services/screenshot_stitch_test.dart
@@ -113,4 +113,16 @@ void main() {
viewportWidth: 100),
isNull);
});
+
+ test('a very tall page is scaled down whole, not cropped', () async {
+ final shot = await solidPng(100, 100, const ui.Color(0xFF0000FF));
+ // 100 CSS px wide at scale 1, page 40000 px tall: over the size limit.
+ final img = await stitchScreenshots([(shot, 0)],
+ pageHeight: 40000, viewportHeight: 100, viewportWidth: 100);
+ expect(img, isNotNull);
+ expect(img!.height, lessThanOrEqualTo(8192));
+ // The aspect ratio of the page is kept.
+ expect(img.width / img.height, closeTo(100 / 40000, 0.001));
+ img.dispose();
+ });
}
diff --git a/test/widgets/structure_data_test.dart b/test/widgets/structure_data_test.dart
index 7cc6006f..51008c4a 100644
--- a/test/widgets/structure_data_test.dart
+++ b/test/widgets/structure_data_test.dart
@@ -63,6 +63,7 @@ PageInspection pageWithLayout() => fullPage().copyWith(
));
void main() {
+ _backgroundRectTests();
late HtmlService service;
Future show(WidgetTester tester, PageInspection p) async {
@@ -230,3 +231,24 @@ void main() {
expect(find.byType(Image), findsOneWidget);
});
}
+
+void _backgroundRectTests() {
+ group('StructurePainter.backgroundRect', () {
+ const layout = PageLayout(
+ viewportWidth: 400,
+ viewportHeight: 800,
+ width: 600, // document overflows the viewport horizontally
+ height: 3000,
+ totalBlocks: 0,
+ blocks: []);
+
+ test('draws the shot viewport-wide at its own aspect ratio', () {
+ // 480x2400 px shot of a 400x2000 CSS px page, at scale 0.5.
+ final r = StructurePainter.backgroundRect(480, 2400, layout, 0.5);
+ expect(r.left, 0);
+ expect(r.top, 0);
+ expect(r.width, closeTo(200, 1e-9));
+ expect(r.height, closeTo(1000, 1e-9));
+ });
+ });
+}