Skip to content

Commit

Permalink
[pdf] Metrics fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
KhromovNikita committed Sep 13, 2024
1 parent fb03d16 commit e7af24f
Show file tree
Hide file tree
Showing 12 changed files with 322 additions and 331 deletions.
27 changes: 10 additions & 17 deletions common/Drawings/TrackObjects/ResizeTracks.js
Original file line number Diff line number Diff line change
Expand Up @@ -522,43 +522,36 @@ function ResizeTrackShapeImage(originalObject, cardDirection, drawingsController
// этот метод обрабатывает данный случай и корректирует координаты

let oFreeText = this.originalObject.group;
let oFreeTextRect = oFreeText.GetTextBoxRect(true).map(function(measure) {
return measure * AscCommon.g_dKoef_pix_to_mm;
let oFreeTextRect = oFreeText.GetTextBoxRect().map(function(measure) {
return measure * g_dKoef_pt_to_mm;
});
let aCallout = oFreeText.GetCallout();
let oExitPoint = undefined; // перпендикулярная линия выходящая из freetext аннотации
let oCalloutArrowPt = undefined; // x2, y2 точка линии (точка начала стрелки)
let oCalloutArrowEndPt = undefined; // x1, y1 точка линии (точка конца стрелки)
let oViewer = Asc.editor.getDocumentRenderer();
let nPage = oFreeText.GetPage();
let nScaleY = oViewer.drawingPages[nPage].H / oViewer.file.pages[nPage].H / oViewer.zoom * AscCommon.g_dKoef_pix_to_mm;
let nScaleX = oViewer.drawingPages[nPage].W / oViewer.file.pages[nPage].W / oViewer.zoom * AscCommon.g_dKoef_pix_to_mm;

if (aCallout && aCallout.length == 6) {
// точка выхода callout из аннотации
oExitPoint = {
x: (aCallout[2 * 2]) * nScaleX,
y: (aCallout[2 * 2 + 1]) * nScaleY
x: (aCallout[2 * 2]) * g_dKoef_pt_to_mm,
y: (aCallout[2 * 2 + 1]) * g_dKoef_pt_to_mm
};

// x2, y2 линии
oCalloutArrowPt = {
x: aCallout[1 * 2] * nScaleX,
y: (aCallout[1 * 2 + 1]) * nScaleY
x: aCallout[1 * 2] * g_dKoef_pt_to_mm,
y: (aCallout[1 * 2 + 1]) * g_dKoef_pt_to_mm
};

oCalloutArrowEndPt = {
x: aCallout[0 * 2] * nScaleX,
y: (aCallout[0 * 2 + 1]) * nScaleY
x: aCallout[0 * 2] * g_dKoef_pt_to_mm,
y: (aCallout[0 * 2 + 1]) * g_dKoef_pt_to_mm
}
}
else {
return;
}

let nFreeTextW = oFreeTextRect[2] - oFreeTextRect[0];
let nFreeTextH = oFreeTextRect[3] - oFreeTextRect[1];

if (this.numberHandle == 4) {
// если x начала стрелки находится в пределах ректа аннотации то фиксируем x
if (oCalloutArrowPt.x < oFreeTextRect[0] || oCalloutArrowPt.x > oFreeTextRect[2]) {
Expand All @@ -574,8 +567,8 @@ function ResizeTrackShapeImage(originalObject, cardDirection, drawingsController
this.correctXYForPdfFreeText = function(x, y) {
let oFreeText = this.originalObject.group;
let aCallout = oFreeText.GetCallout(true);
let aCalloutMM = aCallout ? aCallout.map(function(measure) {return measure * AscCommon.g_dKoef_pix_to_mm}) : undefined;
let aTextBoxRectMM = oFreeText.GetTextBoxRect(true).map(function(measure) {return measure * AscCommon.g_dKoef_pix_to_mm});
let aCalloutMM = aCallout ? aCallout.map(function(measure) {return measure * g_dKoef_pt_to_mm}) : undefined;
let aTextBoxRectMM = oFreeText.GetTextBoxRect().map(function(measure) {return measure * g_dKoef_pt_to_mm});
let nExitPos = oFreeText.GetCalloutExitPos();

if (!aCalloutMM)
Expand Down
1 change: 1 addition & 0 deletions common/HistoryCommon.js
Original file line number Diff line number Diff line change
Expand Up @@ -4359,6 +4359,7 @@
AscDFH.historyitem_Pdf_Annot_Opacity = AscDFH.historyitem_type_Pdf_Annot | 18;
AscDFH.historyitem_Pdf_Annot_Quads = AscDFH.historyitem_type_Pdf_Annot | 19;
AscDFH.historyitem_Pdf_Annot_Intent = AscDFH.historyitem_type_Pdf_Annot | 20;
AscDFH.historyitem_Pdf_Annot_WasChanged = AscDFH.historyitem_type_Pdf_Annot | 21;

// Comment
AscDFH.historyitem_Pdf_Comment_Data = AscDFH.historyitem_type_Pdf_Comment | 1;
Expand Down
6 changes: 2 additions & 4 deletions pdf/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -2040,10 +2040,8 @@
let nNativeW = oViewer.file.pages[nPage].W;
let nNativeH = oViewer.file.pages[nPage].H;
let nPageRotate = oViewer.getPageRotate(nPage);
let nScaleY = oViewer.drawingPages[nPage].H / oViewer.file.pages[nPage].H;
let nScaleX = oViewer.drawingPages[nPage].W / oViewer.file.pages[nPage].W;
let nCommentWidth = 40 * nScaleX;
let nCommentHeight = 40 * nScaleY;
let nCommentWidth = 40;
let nCommentHeight = 40;
let oDoc = oViewer.getPDFDoc();

let oBasePos = {
Expand Down
82 changes: 35 additions & 47 deletions pdf/src/annotations/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,8 @@
CAnnotationBase.prototype.SetWasChanged = function(isChanged) {
let oViewer = editor.getDocumentRenderer();

if (oViewer.IsOpenAnnotsInProgress == false) {
if (this._wasChanged !== isChanged && oViewer.IsOpenAnnotsInProgress == false) {
// AscCommon.History.Add(new CChangesPDFAnnotWasChanged(this, this.IsChanged(), isChanged));
this._wasChanged = isChanged;
this.SetDrawFromStream(!isChanged);
}
Expand All @@ -285,23 +286,23 @@
AscPDF.endMultiplyMode(oGraphicsPDF.GetContext());
}

// oGraphicsPDF.SetLineWidth(1);
// let aOringRect = this.GetOrigRect();
// let X = aOringRect[0];
// let Y = aOringRect[1];
// let nWidth = aOringRect[2] - aOringRect[0];
// let nHeight = aOringRect[3] - aOringRect[1];
oGraphicsPDF.SetLineWidth(1);
let aOringRect = this.GetOrigRect();
let X = aOringRect[0];
let Y = aOringRect[1];
let nWidth = aOringRect[2] - aOringRect[0];
let nHeight = aOringRect[3] - aOringRect[1];

// Y += 1 / 2;
// X += 1 / 2;
// nWidth -= 1;
// nHeight -= 1;
Y += 1 / 2;
X += 1 / 2;
nWidth -= 1;
nHeight -= 1;

// oGraphicsPDF.SetStrokeStyle(0, 255, 255);
// oGraphicsPDF.SetLineDash([]);
// oGraphicsPDF.BeginPath();
// oGraphicsPDF.Rect(X, Y, nWidth, nHeight);
// oGraphicsPDF.Stroke();
oGraphicsPDF.SetStrokeStyle(0, 255, 255);
oGraphicsPDF.SetLineDash([]);
oGraphicsPDF.BeginPath();
oGraphicsPDF.Rect(X, Y, nWidth, nHeight);
oGraphicsPDF.Stroke();
};
CAnnotationBase.prototype.SetSubject = function(sSubject) {
this._subject = sSubject;
Expand Down Expand Up @@ -678,20 +679,7 @@
return !(this.IsFreeText() || this.IsLine() && this.IsDoCaption());
};
CAnnotationBase.prototype.Recalculate = function() {
if (false == this.IsNeedRecalc()) {
return;
}

let oViewer = editor.getDocumentRenderer();
let nPage = this.GetPage();
let aOrigRect = this.GetOrigRect();

let nScaleY = oViewer.drawingPages[nPage].H / oViewer.file.pages[nPage].H / oViewer.zoom;
let nScaleX = oViewer.drawingPages[nPage].W / oViewer.file.pages[nPage].W / oViewer.zoom;

this.recalculate();
this.updatePosition(aOrigRect[0] * g_dKoef_pix_to_mm * nScaleX, aOrigRect[1] * g_dKoef_pix_to_mm * nScaleY);
this.SetNeedRecalc(false);
return;
};
CAnnotationBase.prototype.Draw = function(oGraphicsPDF, oGraphicsWord) {
if (this.IsHidden() == true)
Expand All @@ -701,23 +689,23 @@
this.draw(oGraphicsWord);

// draw annot rect
// oGraphicsPDF.SetLineWidth(1);
// let aOringRect = this.GetOrigRect();
// let X = aOringRect[0];
// let Y = aOringRect[1];
// let nWidth = aOringRect[2] - aOringRect[0];
// let nHeight = aOringRect[3] - aOringRect[1];

// Y += 1 / 2;
// X += 1 / 2;
// nWidth -= 1;
// nHeight -= 1;

// oGraphicsPDF.SetStrokeStyle(0, 255, 255);
// oGraphicsPDF.SetLineDash([]);
// oGraphicsPDF.BeginPath();
// oGraphicsPDF.Rect(X, Y, nWidth, nHeight);
// oGraphicsPDF.Stroke();
oGraphicsPDF.SetLineWidth(1);
let aOringRect = this.GetOrigRect();
let X = aOringRect[0];
let Y = aOringRect[1];
let nWidth = aOringRect[2] - aOringRect[0];
let nHeight = aOringRect[3] - aOringRect[1];

Y += 1 / 2;
X += 1 / 2;
nWidth -= 1;
nHeight -= 1;

oGraphicsPDF.SetStrokeStyle(0, 255, 255);
oGraphicsPDF.SetLineDash([]);
oGraphicsPDF.BeginPath();
oGraphicsPDF.Rect(X, Y, nWidth, nHeight);
oGraphicsPDF.Stroke();
};
CAnnotationBase.prototype.SetReplies = function(aReplies) {
let oDoc = this.GetDocument();
Expand Down
135 changes: 57 additions & 78 deletions pdf/src/annotations/circle.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,19 +67,8 @@
oDoc.StartNoHistoryMode();

let oCircle = new CAnnotationCircle(AscCommon.CreateGUID(), this.GetPage(), this.GetOrigRect().slice(), oDoc);

oCircle.lazyCopy = true;

oCircle._pagePos = {
x: this._pagePos.x,
y: this._pagePos.y,
w: this._pagePos.w,
h: this._pagePos.h
}

oCircle._origRect = this._origRect.slice();
oCircle._rect = this._rect.slice();

this.fillObject(oCircle);

let aStrokeColor = this.GetStrokeColor();
Expand Down Expand Up @@ -107,18 +96,16 @@
let oViewer = editor.getDocumentRenderer();
let oDoc = oViewer.getPDFDoc();

let nScaleY = oViewer.drawingPages[this.GetPage()].H / oViewer.file.pages[this.GetPage()].H / oViewer.zoom * g_dKoef_pix_to_mm;
let nScaleX = oViewer.drawingPages[this.GetPage()].W / oViewer.file.pages[this.GetPage()].W / oViewer.zoom * g_dKoef_pix_to_mm;

let aRD = this.GetRectangleDiff() || [0, 0, 0, 0];
let aOrigRect = this.GetOrigRect();

if (!oGeometry)
if (!oGeometry) {
oGeometry = this.spPr.geometry;
}
if (!aShapeRectInMM) {
aShapeRectInMM = [
(aOrigRect[0] + aRD[0]) * nScaleX, (aOrigRect[1] + aRD[1]) * nScaleY,
(aOrigRect[2] - aRD[2]) * nScaleX, (aOrigRect[3] - aRD[3]) * nScaleY
(aOrigRect[0] + aRD[0]) * g_dKoef_pt_to_mm, (aOrigRect[1] + aRD[1]) * g_dKoef_pt_to_mm,
(aOrigRect[2] - aRD[2]) * g_dKoef_pt_to_mm, (aOrigRect[3] - aRD[3]) * g_dKoef_pt_to_mm
];
}

Expand All @@ -131,97 +118,89 @@
}
oDoc.EndNoHistoryMode();
};
CAnnotationCircle.prototype.SetRect = function(aRect) {
CAnnotationCircle.prototype.SetRect = function(aOrigRect) {
let oViewer = editor.getDocumentRenderer();
let oDoc = oViewer.getPDFDoc();
let nPage = this.GetPage();
let aCurRect = this.GetRect();

let nScaleY = oViewer.drawingPages[nPage].H / oViewer.file.pages[nPage].H / oViewer.zoom;
let nScaleX = oViewer.drawingPages[nPage].W / oViewer.file.pages[nPage].W / oViewer.zoom;

this._rect = aRect;
this._pagePos = {
x: aRect[0],
y: aRect[1],
w: (aRect[2] - aRect[0]),
h: (aRect[3] - aRect[1])
};

AscCommon.History.StartNoHistoryMode();
let oXfrm = this.getXfrm();
oXfrm.setOffX(aRect[0] * g_dKoef_pix_to_mm);
oXfrm.setOffY(aRect[1] * g_dKoef_pix_to_mm);
oXfrm.setExtX((aRect[2] - aRect[0]) * g_dKoef_pix_to_mm);
oXfrm.setExtY((aRect[3] - aRect[1]) * g_dKoef_pix_to_mm);

this._origRect[0] = this._rect[0] / nScaleX;
this._origRect[1] = this._rect[1] / nScaleY;
this._origRect[2] = this._rect[2] / nScaleX;
this._origRect[3] = this._rect[3] / nScaleY;

if (false == AscCommon.History.UndoRedoInProgress) {
let aCurRect = this.GetOrigRect();

let bCalcRDandRect = this._origRect.length != 0 && false == AscCommon.History.UndoRedoInProgress;

this._origRect = aOrigRect;

if (bCalcRDandRect) {
AscCommon.History.StartNoHistoryMode();

let aCurRD = this._rectDiff;
this.SetRectangleDiff([0, 0, 0, 0], true);
let nLineW = this.GetWidth() * g_dKoef_pt_to_mm;
this.SetRectangleDiff([0, 0, 0, 0]);
this.recalcBounds();
this.recalcGeometry();
this.Recalculate(true);
this.recalcInfo.recalculateGeometry = false;

AscCommon.History.EndNoHistoryMode();

let oGrBounds = this.bounds;
let oShapeBounds = this.getRectBounds();

this._origRect[0] = Math.round(oGrBounds.l - 1) * g_dKoef_mm_to_pix / nScaleX;
this._origRect[1] = Math.round(oGrBounds.t - 1) * g_dKoef_mm_to_pix / nScaleY;
this._origRect[2] = Math.round(oGrBounds.r + 1) * g_dKoef_mm_to_pix / nScaleX;
this._origRect[3] = Math.round(oGrBounds.b + 1) * g_dKoef_mm_to_pix / nScaleY;
this._origRect[0] = Math.round(oGrBounds.l - nLineW) * g_dKoef_mm_to_pt;
this._origRect[1] = Math.round(oGrBounds.t - nLineW) * g_dKoef_mm_to_pt;
this._origRect[2] = Math.round(oGrBounds.r + nLineW) * g_dKoef_mm_to_pt;
this._origRect[3] = Math.round(oGrBounds.b + nLineW) * g_dKoef_mm_to_pt;

oDoc.History.Add(new CChangesPDFAnnotRect(this, aCurRect, aOrigRect));

this._rectDiff = aCurRD;
this.SetRectangleDiff([
Math.round(oShapeBounds.l - oGrBounds.l) * g_dKoef_mm_to_pix / nScaleX,
Math.round(oShapeBounds.t - oGrBounds.t) * g_dKoef_mm_to_pix / nScaleY,
Math.round(oGrBounds.r - oShapeBounds.r) * g_dKoef_mm_to_pix / nScaleX,
Math.round(oGrBounds.b - oShapeBounds.b) * g_dKoef_mm_to_pix / nScaleY
], true);

oDoc.History.Add(new CChangesPDFAnnotRect(this, aCurRect, aRect));
Math.round(oShapeBounds.l - oGrBounds.l + nLineW) * g_dKoef_mm_to_pt,
Math.round(oShapeBounds.t - oGrBounds.t + nLineW) * g_dKoef_mm_to_pt,
Math.round(oGrBounds.r - oShapeBounds.r + nLineW) * g_dKoef_mm_to_pt,
Math.round(oGrBounds.b - oShapeBounds.b + nLineW) * g_dKoef_mm_to_pt
]);
}

this.SetWasChanged(true);
this.SetNeedRecalcSizes(true);
};
CAnnotationCircle.prototype.SetRectangleDiff = function(aDiff, bOnResize) {
let oDoc = this.GetDocument();
oDoc.History.Add(new CChangesPDFAnnotRD(this, this.GetRectangleDiff(), aDiff));

this._rectDiff = aDiff;

if (true != bOnResize) {
let oViewer = editor.getDocumentRenderer();
let nPage = this.GetPage();

let nScaleY = oViewer.drawingPages[nPage].H / oViewer.file.pages[nPage].H / oViewer.zoom * g_dKoef_pix_to_mm;
let nScaleX = oViewer.drawingPages[nPage].W / oViewer.file.pages[nPage].W / oViewer.zoom * g_dKoef_pix_to_mm;

let aOrigRect = this.GetOrigRect();

let extX = ((aOrigRect[2] - aOrigRect[0]) - aDiff[0] - aDiff[2]) * nScaleX;
let extY = ((aOrigRect[3] - aOrigRect[1]) - aDiff[1] - aDiff[3]) * nScaleY;

this.spPr.xfrm.setOffX(aDiff[0] * nScaleX + this.spPr.xfrm.offX);
this.spPr.xfrm.setOffY(aDiff[1] * nScaleY + this.spPr.xfrm.offY);

this.spPr.xfrm.setExtX(extX);
this.spPr.xfrm.setExtY(extY);
}
this.SetWasChanged(true);
this.SetNeedRecalcSizes(true);
this.SetNeedRecalc(true);
};
CAnnotationCircle.prototype.SetNeedRecalcSizes = function(bRecalc) {
this._needRecalcSizes = bRecalc;
this.recalcGeometry();
};
CAnnotationCircle.prototype.IsNeedRecalcSizes = function() {
return this
};
CAnnotationCircle.prototype.Recalculate = function(bForce) {
if (true !== bForce && false == this.IsNeedRecalc()) {
return;
}

if (this.recalcInfo.recalculateGeometry)
if (this.IsNeedRecalcSizes()) {
let aOrigRect = this.GetOrigRect();
let aRD = this.GetRectangleDiff();

let extX = ((aOrigRect[2] - aOrigRect[0]) - aRD[0] - aRD[2]) * g_dKoef_pt_to_mm;
let extY = ((aOrigRect[3] - aOrigRect[1]) - aRD[1] - aRD[3]) * g_dKoef_pt_to_mm;

this.spPr.xfrm.offX = (aOrigRect[0] + aRD[0]) * g_dKoef_pt_to_mm;
this.spPr.xfrm.offY = (aOrigRect[1] + aRD[1]) * g_dKoef_pt_to_mm;

this.spPr.xfrm.extX = extX;
this.spPr.xfrm.extY = extY;

this.SetNeedRecalcSizes(false);
}
if (this.recalcInfo.recalculateGeometry) {
this.RefillGeometry();
}

this.recalculateTransform();
this.updateTransformMatrix();
Expand Down
Loading

0 comments on commit e7af24f

Please sign in to comment.