Files
BadNote/lib/editor/pdf/slide_export.dart

37 lines
1.6 KiB
Dart
Raw Permalink Normal View History

// lib/editor/pdf/slide_export.dart
//
// Pure geometry for exporting pen-first slide annotations to PDF. Because the
// pen canvas captures strokes NORMALIZED to the page rect ([0,1]), the export
// just maps each normalized point into the slide image's draw rectangle on the
// PDF page — no live-widget-size guessing, which is what made the old PPT
// exporter misalign ink (see the removed ppt_annotator_screen comment).
import 'dart:ui' show Offset, Rect, Size;
/// The rectangle a slide [image] occupies when drawn "contain"-fit and centered
/// on a PDF page of size [page]. Mirrors the live canvas's fit-to-view so the
/// exported ink lands exactly where it was drawn.
Rect slideDrawRect(Size page, Size image) {
final iw = image.width <= 0 ? 1.0 : image.width;
final ih = image.height <= 0 ? 1.0 : image.height;
final scale = (page.width / iw) < (page.height / ih)
? page.width / iw
: page.height / ih;
final drawW = iw * scale;
final drawH = ih * scale;
final offX = (page.width - drawW) / 2;
final offY = (page.height - drawH) / 2;
return Rect.fromLTWH(offX, offY, drawW, drawH);
}
/// Map a normalized stroke point ([0,1] of the page rect) to an absolute point
/// inside the slide's [drawRect] on the PDF page.
Offset normToSlide(double nx, double ny, Rect drawRect) =>
Offset(drawRect.left + nx * drawRect.width,
drawRect.top + ny * drawRect.height);
/// Absolute pen width (PDF units) for a stroke whose width is a fraction of the
/// page width, scaled into [drawRect].
double slideStrokeWidth(double normalizedWidth, Rect drawRect) =>
normalizedWidth * drawRect.width;