import { ChangeDetectionStrategy, Component, computed, effect, inject, signal, untracked, ViewEncapsulation, } from '@angular/core'; import { EpdfDocumentGate, provideEmbedPdf } from '@embedpdf/angular/runtime'; import type { OpenInput } from '@embedpdf/angular/runtime'; import { EpdfPageTemplate, EpdfStage, withStage } from '@embedpdf/angular/stage'; import { EpdfRenderLayer, withRender } from '@embedpdf/angular/render'; import { EpdfInteraction, withInteraction } from '@embedpdf/angular/interaction'; import { EpdfAnnotationLayer, withAnnotation } from '@embedpdf/angular/annotation'; import { EpdfMeasurement, withMeasurement, type CalibrationRequest, type LengthUnit, } from '@embedpdf/angular/measurement'; import { localEngine } from '@embedpdf/engine'; // [!doc-source sketch] const sketch = async (): Promise => { const response = await fetch('https://www.embedpdf.com/pdfs/sketch.pdf'); return { kind: 'bytes', id: 'sketch', bytes: new Uint8Array(await response.arrayBuffer()) }; }; // [!/doc-source] @Component({ selector: 'demo-calibration', changeDetection: ChangeDetectionStrategy.OnPush, template: ` @if (measurement.calibrationRequest(); as request) {
That line is
} @else {
Drag along something you know the length of, then measure with the new scale {{ ratio() }}
} `, }) export class Calibration { protected readonly measurement = inject(EpdfMeasurement); private readonly interaction = inject(EpdfInteraction); protected readonly units = this.measurement.listUnits(); protected readonly length = signal('20'); protected readonly value = computed(() => Number(this.length())); protected readonly unit = signal('cm'); private readonly scale = this.measurement.scaleOf(0); protected readonly ratio = computed(() => { const measure = this.scale().measure; return measure?.subtype === 'rectilinear' ? measure.ratio : '…'; }); private started = false; constructor() { // On load: calibrating, so the next line you drag is the known length. effect(() => { if (!this.scale().ready || this.started) return; this.started = true; untracked(() => { if (this.measurement.canCalibrate()) this.measurement.startCalibration(); }); }); } // A