2.6.2-beta-2, 0.17.6-10 PDFCropping

This commit is contained in:
zsviczian
2024-11-01 07:44:58 +01:00
parent 7233d1e037
commit dec2909db0
14 changed files with 203 additions and 94 deletions

View File

@@ -19,6 +19,7 @@ export async function insertImageToView(
file: TFile | string,
scale?: boolean,
shouldInsertToView: boolean = true,
repositionToCursor: boolean = false,
):Promise<string> {
if(shouldInsertToView) {ea.clear();}
ea.style.strokeColor = "transparent";
@@ -31,7 +32,7 @@ export async function insertImageToView(
file,
scale,
);
if(shouldInsertToView) {await ea.addElementsToView(false, true, true);}
if(shouldInsertToView) {await ea.addElementsToView(repositionToCursor, true, true);}
return id;
}

View File

@@ -1,27 +1,37 @@
//for future use, not used currently
import { ImageCrop } from "@zsviczian/excalidraw/types/excalidraw/element/types";
import { LinkParts } from "./Utils";
export function getPDFCropRect (props: {
scale: number,
linkParts: LinkParts,
link: string,
naturalHeight: number,
naturalWidth: number,
}) : ImageCrop | null {
const cropRect = props.linkParts.ref.split("rect=")[1]?.split(",").map(x=>parseInt(x));
const validRect = cropRect && cropRect.length === 4 && cropRect.every(x=>!isNaN(x));
if(!validRect) {
const rectVal = props.link.match(/&rect=(\d*),(\d*),(\d*),(\d*)/);
if (!rectVal || rectVal.length !== 5) {
return null;
}
const R0 = parseInt(rectVal[1]);
const R1 = parseInt(rectVal[2]);
const R2 = parseInt(rectVal[3]);
const R3 = parseInt(rectVal[4]);
return {
x: cropRect[0] * props.scale,
y: (props.naturalHeight/props.scale - cropRect[3]) * props.scale,
width: (cropRect[2] - cropRect[0]) * props.scale,
height: (cropRect[3] - cropRect[1]) * props.scale,
x: R0 * props.scale,
y: (props.naturalHeight/props.scale - R3) * props.scale,
width: (R2 - R0) * props.scale,
height: (R3 - R1) * props.scale,
naturalWidth: props.naturalWidth,
naturalHeight: props.naturalHeight,
}
}
export function getPDFRect(elCrop: ImageCrop, scale: number): string {
const R0 = elCrop.x / scale;
const R2 = elCrop.width / scale + R0;
const R3 = (elCrop.naturalHeight - elCrop.y) / scale;
const R1 = R3 - elCrop.height / scale;
return `&rect=${Math.round(R0)},${Math.round(R1)},${Math.round(R2)},${Math.round(R3)}`;
}

View File

@@ -477,8 +477,6 @@ export function scaleLoadedImage (
const ratioX = elCrop.x / (elCrop.naturalWidth - elCrop.x - elCrop.width);
const gapX = imgWidth - elCrop.width;
el.crop.x = ratioX * gapX / (1 + ratioX);
// const ratioA = elCrop.x / (elCrop.naturalWidth - elCrop.x);
// el.crop.x = ratioA * imgWidth / (1 + ratioA);
if(el.crop.x + elCrop.width > imgWidth) {
el.crop.x = (imgWidth - elCrop.width) / 2;
}
@@ -492,8 +490,6 @@ export function scaleLoadedImage (
const ratioY = elCrop.y / (elCrop.naturalHeight - elCrop.y - elCrop.height);
const gapY = imgHeight - elCrop.height;
el.crop.y = ratioY * gapY / (1 + ratioY);
// const ratioB = elCrop.y / (elCrop.naturalHeight - elCrop.y);
// el.crop.y = ratioB * imgHeight / (1 + ratioB);
if(el.crop.y + elCrop.height > imgHeight) {
el.crop.y = (imgHeight - elCrop.height)/2;
}